Add support for entry counting in pgstats
Michael Paquier <michael@paquier.xyz>
From: Michael Paquier <michael@paquier.xyz>
To: Postgres hackers <pgsql-hackers@lists.postgresql.org>
Date: 2025-09-12T07:23:05Z
Lists: pgsql-hackers
Attachments
- 0001-Add-support-for-entry-counting-in-pgstats.patch (text/x-diff) patch 0001
- 0002-injection_points-Add-entry-counting.patch (text/x-diff) patch 0002
Hi all, One obstacle to the move of pg_stat_statements to the shmem pgstats that we have in core is that there is no easy way to track the total number of entries that are stored in the shared hash table of pgstats for variable-sized stats kinds. PGSS currently hash_get_num_entries() as a cheap way to get the number of entries stored in its hash table, but moving PGSS to the in-core stats facility means that we need a different approach to count these entries. I have looked at what can be done, and finished with a rather simple approach, as we have only one code path when a new entry is inserted, and one code path when an entry is removed when the refcount of an entry reaches 0 (includes resets, drops for kind matches, etc.). The counters are stored in a uint64 atomic, one for each stats kind in PgStat_ShmemControl, set only if the option is enabled. I am wondering how much protection we want for the reads, and chose the lightest "lossy" approach, leaving to stats kind what they want to do when deallocating entries. Hence, a stats kind may want to protect the entry deallocations with an extra lock, but that's not required either depending on how aggressive removals should happen. Please find attached that adds an option for variable-sized stats kinds given these the possibility to track the number of entries in the shared hash table. The option is named track_counts. The patch has some coverage added in the test module injection_points, in its TAP test. Thoughts are welcome. -- Michael
Commits
-
injection_points: Enable entry count in its variable-sized stats
- acf0960c2309 19 (unreleased) landed
-
Add support for tracking of entry count in pgstats
- 7bd2975fa92b 19 (unreleased) landed
-
Keep track of what RTIs a Result node is scanning.
- f2bae51dfd5b 19 (unreleased) cited