statements Decides What to Keep
This title could be clearer and more informative.Try out Clickbait Shieldfor free (5 uses left this month).
A deep dive into the pg_stat_statements source code revealing how it decides which queries to keep when its hash table fills up. Key findings: there's a hidden per-query usage counter (not visible in the view) that drives eviction; deallocation decreases all usage values by 1%, sorts every entry, and drops the bottom 5%; only completed executions are stored (aborted/timed-out queries leave no record); and increasing pg_stat_statements.max is not free because the deallocation sort is linear in the number of entries. The post also explains the fast path (shared lock, counter update) vs. slow path (exclusive lock, deallocation, garbage collection of the external query text file) for query storage.