pgsql: Add support for tracking of entry count in pgstats - Mailing list pgsql-committers

From Michael Paquier
Subject pgsql: Add support for tracking of entry count in pgstats
Date
Msg-id E1v31Hj-000XkC-0r@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Add support for tracking of entry count in pgstats

Stats kinds can set a new option called "track_entry_count" (disabled by
default, available for variable-numbered stats) that will make pgstats
track the number of entries that exist in its shared hashtable.

As there is only one code path where a new entry is added, and one code
path where entries are freed, the count tracking is straight-forward in
its implementation.  Reads of these counters are optimistic, and may
change across two calls.  The counter is incremented when an entry is
created (not when reused), and is decremented when an entry is freed
from the hashtable (marked for drop with its refcount reaching 0), which
is something that pgstats decides internally.

A first use case of this facility would be pg_stat_statements, where we
need to be able to cap the number of entries that would be stored in the
shared hashtable, based on its "max" GUC.  The module currently relies
on hash_get_num_entries(), which offers a cheap way to count how many
entries are in its hash table, but we cannot do that in pgstats for
variable-sized stats kinds as a single hashtable is used for all the
stats kinds.  Independently of PGSS, this is useful for other custom
stats kinds that want to cap, control, or track the number of entries
they have, without depending on a potentially expensive sequential scan
to know the number of entries while holding an extra exclusive lock.

Author: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Sami Imseih <samimseih@gmail.com>
Reviewed-by: Keisuke Kuroda <keisuke.kuroda.3862@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/aMPKWR81KT5UXvEr@paquier.xyz

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/7bd2975fa92bd6b5ade5e34f75d474ac838ec0c6

Modified Files
--------------
src/backend/utils/activity/pgstat.c       |  4 +++
src/backend/utils/activity/pgstat_shmem.c | 44 ++++++++++++++++++++++---------
src/include/utils/pgstat_internal.h       | 27 +++++++++++++++++++
3 files changed, 62 insertions(+), 13 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Refactor to avoid code duplication in transformPLAssignStmt.
Next
From: Michael Paquier
Date:
Subject: pgsql: injection_points: Enable entry count in its variable-sized stats