Обсуждение: pgsql: Add support for tracking of entry count in pgstats

Поиск
Список
Период
Сортировка

pgsql: Add support for tracking of entry count in pgstats

От
Michael Paquier
Дата:
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(-)