Add aggregation flags to reduce cardinality in high-database deployments#29
Merged
Add aggregation flags to reduce cardinality in high-database deployments#29
Conversation
Add two new flags to significantly reduce memory usage and time series cardinality in high-database-count deployments: - `--collector.locks.per_database` (default: false): When false, aggregates lock counts across all databases instead of per-database - `--collector.stat_database.detailed` (default: false): When false, only collects xact_commit instead of all 18 metrics These changes reduce time series from 22,144 to 701 (96.8% reduction) on a 692-database cluster, resolving OOM issues. Backward compatibility maintained: set both flags to true to restore original behavior. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> Signed-off-by: Max Englander <max@planetscale.com>
tom-pang
reviewed
Apr 8, 2026
tom-pang
left a comment
There was a problem hiding this comment.
pgLocksDesc is being allocated via prometheus.NewDesc() inside updatePerDatabase() and updateAggregated(), which means it runs on every scrape. Every other collector in the codebase defines descriptors as package-level vars.
Since the two modes need different label sets (["datname", "mode"] vs ["mode"]), two separate descriptors makes sense — they just need to be hoisted to package scope:
var (
pgLocksDescPerDB = prometheus.NewDesc(
prometheus.BuildFQName(namespace, locksSubsystem, "count"),
"Number of locks per database",
[]string{"datname", "mode"}, nil,
)
pgLocksDescAggregated = prometheus.NewDesc(
prometheus.BuildFQName(namespace, locksSubsystem, "count"),
"Number of locks across all databases",
[]string{"mode"}, nil,
)
)Signed-off-by: Max Englander <max@planetscale.com>
tom-pang
approved these changes
Apr 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
postgres_exporter is OOMing on high-cardinality Postgres clusters with many databases:
Cardinality Breakdown (692 databases)
pg_lockspg_stat_databaseSolution
Add two new flags to allow aggregation/filtering at collection time:
1.
--collector.locks.per_database(default:false)false(default): Aggregates lock counts across all databasespg_locks_countfrom 6,228 series → 9 series (99.9% reduction)modeonly (removesdatnamelabel)true: Original behavior (per-database, per-mode metrics)Rationale: Our recording rules already aggregate away the
datnamelabel:sum by (cluster, database_branch_id, mode, ...) (pg_locks_count)So per-database collection was unnecessary cardinality.
2.
--collector.stat_database.detailed(default:false)false(default): Only collectsxact_commitmetricdatidanddatnamelabels (needed by recording rules)true: Original behavior (all 18 metrics)Rationale: Only
xact_commitis used in production metrics. Other metrics (blks_read, tup_fetched, etc.) are unused but consuming memory.Implementation Details
Changes
collector/pg_locks.go:Update()intoupdatePerDatabase()andupdateAggregated()datnamefrom SELECT and JOINcollector/pg_stat_database.go:Update()intoupdateDetailed()andupdateMinimal()Tests: Added test coverage for both modes of each collector
README: Documented new flags
Backward Compatibility
✅ Fully backward compatible - users can restore original behavior:
Expected Impact
Cardinality Reduction (692 databases)
pg_locks_countpg_stat_database_*Memory Savings
Recording Rules
No changes needed - recording rules will continue to work:
pg_locks: Already aggregates awaydatnamepg_stat_database: Preservesdatnamefor downstream useTesting
Deployment Plan
Related