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.

10m read timeFrom pganalyze.com
Post cover image
Table of contents
A quick recapWhat the source code revealsThe three values that drive allocationOnly completed executions get storedThe three functions that do the workThe fast path: when the entry already existsThe slow path: when the entry doesn’t existWhy a larger pg_stat_statements.max is not freeKey takeawaysWhat’s coming nextWhat we discussed in this episode
479 Impressions