pgsql: Change custom wait events to use dynamic shared hash tables

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Change custom wait events to use dynamic shared hash tables
Date: 2023-08-14 05:49:02
Message-ID: E1qVQRp-0002M7-KU@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Change custom wait events to use dynamic shared hash tables

Currently, the names of the custom wait event must be registered for
each backend, requiring all these to link to the shared memory area of
an extension, even if these are not loaded with
shared_preload_libraries.

This patch relaxes the constraints related to this infrastructure by
storing the wait events and their names in two dynamic hash tables in
shared memory. This has the advantage to simplify the registration of
custom wait events to a single routine call that returns an event ID
ready for consumption:
uint32 WaitEventExtensionNew(const char *wait_event_name);

The caller of this routine can then cache locally the ID returned, to be
used for pgstat_report_wait_start(), WaitLatch() or a similar routine.

The implementation uses two hash tables: one with a key based on the
event name to avoid duplicates and a second using the event ID as key
for event lookups, like on pg_stat_activity. These tables can hold a
minimum of 16 entries, and a maximum of 128 entries, which should be plenty
enough.

The code changes done in worker_spi show how things are simplified (most
of the code removed in this commit comes from there):
- worker_spi_init() is gone.
- No more shared memory hooks required (size requested and
initialization).
- The custom wait event ID is cached in the process that needs to set
it, with one single call to WaitEventExtensionNew() to retrieve it.

Per suggestion from Andres Freund.

Author: Masahiro Ikeda, with a few tweaks from me.
Discussion: https://postgr.es/m/20230801032349.aaiuvhtrcvvcwzcx@awork3.anarazel.de

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/af720b4c50a122647182f4a030bb0ea8f750fe2f

Modified Files
--------------
doc/src/sgml/monitoring.sgml | 5 +-
doc/src/sgml/xfunc.sgml | 26 +--
src/backend/storage/lmgr/lwlocknames.txt | 1 +
src/backend/utils/activity/wait_event.c | 218 +++++++++++++++---------
src/backend/utils/activity/wait_event_names.txt | 1 +
src/include/utils/wait_event.h | 18 +-
src/test/modules/worker_spi/t/001_worker_spi.pl | 18 +-
src/test/modules/worker_spi/worker_spi--1.0.sql | 5 -
src/test/modules/worker_spi/worker_spi.c | 109 +-----------
src/tools/pgindent/typedefs.list | 2 +
10 files changed, 165 insertions(+), 238 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Eisentraut 2023-08-14 08:34:42 Re: pgsql: Allow tailoring of ICU locales with custom rules
Previous Message Amit Kapila 2023-08-14 03:15:13 pgsql: Simplify determining logical replication worker types.