pgsql: Allow a stats kind to use its own dshash in pgstats

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Allow a stats kind to use its own dshash in pgstats
Date: 2026-10-13 04:02:57
Message-ID: E1xGTj7-00000001P4c-18fg@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Allow a stats kind to use its own dshash in pgstats

Variable-numbered pgstats entries currently all live in one shared dshash
table. This works for stats kinds with each one having a rather "small"
number of entries, but there are limits if a stats kind wants to
allocate a large number of entries, especially if these need to be
scanned on a periodic basis.

This commit adds a new own_hash option for variable-numbered stats
kinds, allowing these to use an independent dshash table backed by its
own DSA area. This gives a way for stats kind to leverage their efforts
across more than one shared hashtable, splitting the load, with the same
guarantees as the main hashtable. Fixed-sized stats kind cannot use
this option. All the hashtables use the same key pattern,
PgStat_HashKey.

The motivation of this change is pg_stat_statements, to add it as a
custom stats kind. Out-of-core custom stats kind, or even built-in
stats kinds can also use this option.

At startup, each stats kind that opts in creates one initial DSA area in
shared memory at startup. At attach time, each registered
variable-numbered stats kind is mapped to either the shared hash and DSA
or its dedicated hash and DSA. The local references are extended to
track which hash table to use on a per-kind basis, with arrays based on
the kind ID.

The test module test_custom_stats is extended and refactored to provide
coverage for this new own_hash.

The idea of being able to use more than one hash table for pgstats has
been originally mentioned off-list during a conference by Robert Haas
(note: if my memory does not fail me here).

Author: Sami Imseih <samimseih(at)gmail(dot)com>
Reviewed-by: Michael Paquier <michael(at)paquier(dot)xyz>
Reviewed-by: Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAA5RZ0supQBxSkh=CWB39=j+cL3hHcLPki3tcBk0B1r4fesg_g@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/44539786b8296b6e73035142670e0d1ff2639078

Modified Files
--------------
src/backend/utils/activity/pgstat.c | 254 ++++++++-------
src/backend/utils/activity/pgstat_shmem.c | 360 +++++++++++++++------
src/include/utils/pgstat_internal.h | 69 ++--
.../test_custom_stats/t/001_custom_stats.pl | 232 +++++++------
.../test_custom_var_stats--1.0.sql | 5 +
.../test_custom_stats/test_custom_var_stats.c | 59 +++-
6 files changed, 630 insertions(+), 349 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Fujii Masao 2026-10-13 04:19:19 pgsql: pg_dump: Don't emit OVERRIDING SYSTEM VALUE for dropped identity
Previous Message Bruce Momjian 2026-10-13 01:39:44 Re: pgsql: doc PG 19 relnotes: remove reverted item