Determines the behavior when cumulative statistics are accessed multiple times within a transaction.
At a glance
| Property | Value |
|---|---|
| Parameter | stats_fetch_consistency |
| Category | Run-time Statistics |
| Default | cache |
| Value type | enum |
| Change scope | Per-session (SET) |
| Available in | PostgreSQL 15, 16, 17, 18, 19 (added in 15) |
What it does
Determines the behavior when cumulative statistics are accessed multiple times within a transaction. When set to none, each access re-fetches counters from shared memory. When set to cache, the first access to statistics for an object caches those statistics until the end of the transaction unless pg_stat_clear_snapshot() is called. When set to snapshot, the first statistics access caches all statistics accessible in the current database, until the end of the transaction unless pg_stat_clear_snapshot() is called. Changing this parameter in a transaction discards the statistics snapshot. The default is cache.
none is most suitable for monitoring systems. If values are only accessed once, it is the most efficient. cache ensures repeat accesses yield the same values, which is important for queries involving e.g. self-joins. snapshot can be useful when interactively inspecting statistics, but has higher overhead, particularly if many database objects exist.
(Description quoted from the official PostgreSQL documentation.)
How to apply a change
Can be set per session with SET, per role/database with ALTER ROLE/DATABASE ... SET, or globally in postgresql.conf.
Inspect the current value and source with SHOW stats_fetch_consistency; or SELECT name, setting, unit, context, source FROM pg_settings WHERE name = 'stats_fetch_consistency';.
Tuning guidance
This governs how much activity and timing data the server collects. Collecting more improves visibility in pg_stat_* views and monitoring but adds a little overhead; collecting less is cheaper but blinds your tooling. Enable the tracking your monitoring actually consumes and leave the rest at default.