Skip to content

Commit 7d69338

Browse files
committed
fix: limit pg_stat_statements queryid cardinality to prevent VictoriaMetrics OOM
Add ORDER BY calls DESC LIMIT 100 to both pg_stat_statements metric SQL variants (PG 11+ and PG 17+) so pgwatch only exports the top 100 most-called queries. Previously ALL queryids were exported — on PostgREST databases this meant ~7K new unique queryids per minute, creating 16M+ time series that OOM-killed VictoriaMetrics (3.9GB/4GB). Also add sample_limit: 5000 to the pgwatch scrape config as a safety net to reject any scrape that produces more than 5000 samples. Closes #143 Related to #130 https://claude.ai/code/session_01SzJxzZNQjDQphaHyaX3RU7
1 parent 2f3c31f commit 7d69338

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

config/pgwatch-prometheus/metrics.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,8 @@ metrics:
10351035
temp_bytes_read::int8 as temp_bytes_read,
10361036
temp_bytes_written::int8 as temp_bytes_written
10371037
from aggregated_statements
1038+
order by calls desc
1039+
limit 100
10381040
17: |
10391041
with aggregated_statements as ( /* pgwatch_generated */
10401042
select
@@ -1081,6 +1083,8 @@ metrics:
10811083
temp_bytes_read::int8 as temp_bytes_read,
10821084
temp_bytes_written::int8 as temp_bytes_written
10831085
from aggregated_statements
1086+
order by calls desc
1087+
limit 100
10841088
gauges:
10851089
- calls
10861090
- plans_total

config/prometheus/prometheus.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ scrape_configs:
1515
scrape_interval: 30s # How often to scrape PGWatch
1616
scrape_timeout: 25s # Timeout for each scrape (must be < scrape_interval)
1717
metrics_path: /pgwatch
18+
sample_limit: 5000 # Safety net: reject scrapes with >5000 samples to prevent cardinality explosion
1819

1920
# Self-monitoring: Victoria Metrics internal metrics
2021
- job_name: 'victoriametrics'

0 commit comments

Comments
 (0)