diff options
author | Michael Paquier <michael@paquier.xyz> | 2025-09-29 08:57:57 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2025-09-29 08:57:57 +0900 |
commit | 7bd2975fa92bd6b5ade5e34f75d474ac838ec0c6 (patch) | |
tree | ce54add8396362b120d5fdd34a442ce711948fc1 /src/include/utils/pgstat_internal.h | |
parent | b0fb2c6aa5a485e28210e13ae5536c1231b1261f (diff) |
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
Diffstat (limited to 'src/include/utils/pgstat_internal.h')
-rw-r--r-- | src/include/utils/pgstat_internal.h | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/include/utils/pgstat_internal.h b/src/include/utils/pgstat_internal.h index bf75ebcef31..88d09ea20ba 100644 --- a/src/include/utils/pgstat_internal.h +++ b/src/include/utils/pgstat_internal.h @@ -232,6 +232,12 @@ typedef struct PgStat_KindInfo bool write_to_file:1; /* + * Should the number of entries be tracked? For variable-numbered stats, + * to update its PgStat_ShmemControl.entry_counts. + */ + bool track_entry_count:1; + + /* * The size of an entry in the shared stats hash table (pointed to by * PgStatShared_HashEntry->body). For fixed-numbered statistics, this is * the size of an entry in PgStat_ShmemControl->custom_data. @@ -501,6 +507,16 @@ typedef struct PgStat_ShmemControl pg_atomic_uint64 gc_request_count; /* + * Counters for the number of entries associated to a single stats kind + * that uses variable-numbered objects stored in the shared hash table. + * These counters can be enabled on a per-kind basis, when + * track_entry_count is set. This counter is incremented each time a new + * entry is created (not reused) in the shared hash table, and is + * decremented each time an entry is freed from the shared hash table. + */ + pg_atomic_uint64 entry_counts[PGSTAT_KIND_MAX]; + + /* * Stats data for fixed-numbered objects. */ PgStatShared_Archiver archiver; @@ -926,6 +942,17 @@ pgstat_get_entry_data(PgStat_Kind kind, PgStatShared_Common *entry) } /* + * Returns the number of entries counted for a stats kind. + */ +static inline uint64 +pgstat_get_entry_count(PgStat_Kind kind) +{ + Assert(pgstat_get_kind_info(kind)->track_entry_count); + + return pg_atomic_read_u64(&pgStatLocal.shmem->entry_counts[kind - 1]); +} + +/* * Returns a pointer to the shared memory area of custom stats for * fixed-numbered statistics. */ |