From af720b4c50a122647182f4a030bb0ea8f750fe2f Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Mon, 14 Aug 2023 14:47:27 +0900 Subject: 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 --- doc/src/sgml/monitoring.sgml | 5 ++--- doc/src/sgml/xfunc.sgml | 26 ++++---------------------- 2 files changed, 6 insertions(+), 25 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index f4fc5d814fb..70511a2388e 100644 --- a/doc/src/sgml/monitoring.sgml +++ b/doc/src/sgml/monitoring.sgml @@ -1121,9 +1121,8 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i LWLock types to the list shown in and . In some cases, the name - assigned by an extension will not be available in all server processes; - so an Extension or LWLock wait - event might be reported as just + of LWLock assigned by an extension will not be + available in all server processes; It might be reported as just extension rather than the extension-assigned name. diff --git a/doc/src/sgml/xfunc.sgml b/doc/src/sgml/xfunc.sgml index d6345a775b6..281c178b0e8 100644 --- a/doc/src/sgml/xfunc.sgml +++ b/doc/src/sgml/xfunc.sgml @@ -3454,33 +3454,15 @@ if (!ptr) - Shared Memory and Custom Wait Events + Custom Wait Events Add-ins can define custom wait events under the wait event type - Extension. The add-in's shared library must be - preloaded by specifying it in shared_preload_libraries, - and register a shmem_request_hook and a - shmem_startup_hook in its - _PG_init function. - shmem_request_hook can request a shared memory size - to be later used at startup by calling: + Extension by calling: -void RequestAddinShmemSpace(int size) - - - - shmem_startup_hook can allocate in shared memory - custom wait events by calling while holding the LWLock - AddinShmemInitLock to avoid any race conditions: - -uint32 WaitEventExtensionNew(void) - - Next, each process needs to associate the wait event allocated previously - to a user-facing custom string, which is something done by calling: - -void WaitEventExtensionRegisterName(uint32 wait_event_info, const char *wait_event_name) +uint32 WaitEventExtensionNew(const char *wait_event_name) + The wait event is associated to a user-facing custom string. An example can be found in src/test/modules/worker_spi in the PostgreSQL source tree. -- cgit v1.2.3