From c9af054653077699189884c336a65e23a7c8aebb Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Mon, 31 Jul 2023 17:09:24 +0900 Subject: Support custom wait events for wait event type "Extension" Two backend routines are added to allow extension to allocate and define custom wait events, all of these being allocated in the type "Extension": * WaitEventExtensionNew(), that allocates a wait event ID computed from a counter in shared memory. * WaitEventExtensionRegisterName(), to associate a custom string to the wait event ID allocated. Note that this includes an example of how to use this new facility in worker_spi with tests in TAP for various scenarios, and some documentation about how to use them. Any code in the tree that currently uses WAIT_EVENT_EXTENSION could switch to this new facility to define custom wait events. This is left as work for future patches. Author: Masahiro Ikeda Reviewed-by: Andres Freund, Michael Paquier, Tristan Partin, Bharath Rupireddy Discussion: https://postgr.es/m/b9f5411acda0cf15c8fbb767702ff43e@oss.nttdata.com --- doc/src/sgml/monitoring.sgml | 11 +++++++---- doc/src/sgml/xfunc.sgml | 45 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index 588b720f57e..f4fc5d814fb 100644 --- a/doc/src/sgml/monitoring.sgml +++ b/doc/src/sgml/monitoring.sgml @@ -1117,11 +1117,14 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i - Extensions can add LWLock types to the list shown in - . In some cases, the name + Extensions can add Extension and + 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 LWLock wait event might be reported as - just extension rather than the + so an Extension or LWLock wait + event 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 9620ea9ae38..d6345a775b6 100644 --- a/doc/src/sgml/xfunc.sgml +++ b/doc/src/sgml/xfunc.sgml @@ -3453,6 +3453,51 @@ if (!ptr) + + Shared Memory and 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: + +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) + + An example can be found in src/test/modules/worker_spi + in the PostgreSQL source tree. + + + Custom wait events can be viewed in + pg_stat_activity: + +=# SELECT wait_event_type, wait_event FROM pg_stat_activity + WHERE backend_type ~ 'worker_spi'; + wait_event_type | wait_event +-----------------+----------------- + Extension | worker_spi_main +(1 row) + + + + Using C++ for Extensibility -- cgit v1.2.3