pganalyze Blog· Ryan Booz·
Postgres in Production, Part 6: Diagnosing High Cardinality Workloads in pg_stat_statements
Postgres in Production Special Series: Diagnosing High Cardinality Workloads in pg_stat_statements (Part 6)
Key Takeaway
Part 6 of pganalyze's Postgres in Production series explains how to tell whether a workload is generating more unique normalized queries than pg_stat_statements.
Why It Matters
For workloads that frequently generate variable-length IN lists, upgrading to PostgreSQL 18 can reduce statement entry usage through normalization, leaving room for other queries’ tuning metrics.
Technical Breakdown · Distilled from the original source
Query shapes and version differences
- Per-tenant SQL from ORMs, dynamic SQL in stored procedures when
pg_stat_statements.track = all, ad hoc reporting, and one-off queries from AI tools can increase the number of tracked entries. Dynamic column lists can also produce distinct query shapes. - Bluebox contains fewer than 50 distinct SQL texts. After about an hour with identical workload settings, PostgreSQL 17 recorded 671 entries and PostgreSQL 18 recorded 120. Version 18 normalizes variable-length
INlists, but the same query text can still produce multiple entries if a client initially binds a parameter asnumericand later switches tointeger.
Detecting entry eviction
- Check
pg_stat_statements.maxfirst. If the entry count consistently sits around 95% of capacity, monitor whetherdeallocinpg_stat_statements_infokeeps rising and how quickly entries refill after a statistics reset. - Also investigate eviction if known running queries routinely go missing or the top ten by total execution time change frequently despite an apparently steady workload.
Preserving history for tuning
- Review which extension settings can be changed online and which require a restart, and consider upgrading to PostgreSQL 18. Review ORM- and AI-generated SQL with developers and log when statistics go missing to see whether recurring workloads, such as weekend data transformations, are displacing older entries.
Source: pganalyze Blog· pganalyze.com
PG Radar summarizes verified changes and impact. Read the original post for full technical details and code samples.