Skip to content

Latest commit

 

History

History
105 lines (69 loc) · 5.36 KB

20230817_02.md

File metadata and controls

105 lines (69 loc) · 5.36 KB

PostgreSQL 17 preview - 支持自定义等待事件

作者

digoal

日期

2023-08-17

标签

PostgreSQL , PolarDB , 等待事件 , wait event


背景

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=c9af054653077699189884c336a65e23a7c8aebb

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  

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=af720b4c50a122647182f4a030bb0ea8f750fe2f

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  

digoal's wechat