pg_stat_statements stores query text not in its in-memory hash table but in a separate on-disk file (pgss_query_texts.stat) inside the Postgres data directory. Each hash table entry holds an offset and length pointer into this append-only file. The file grows to roughly 2× the mean query length multiplied by pg_stat_statements.max, and when it hits that ceiling, Postgres rewrites it entirely under a brief lock. Because evicted queryids are forgotten, the same query text can appear multiple times. ORM-heavy workloads are the most common cause of the file ballooning to hundreds of megabytes due to verbose SQL and high queryid cardinality. Using pg_stat_statements(false) skips the file read entirely, which is useful when only metrics are needed.

8m read timeFrom pganalyze.com
Post cover image
Table of contents
A quick recapWhere the query text actually livesLocating the query text fileHow the file grows: append-only with a 2× size ceilingThe pointer mechanism: offset and length per entryA performance note: when the file actually gets readNo memory of previously tracked queriesThe rewrite cycle when the file fills upA real-world size exampleWhy ORMs make this worseKey takeawaysWhat’s coming nextWhat we discussed in this episode
702 Impressions