mirror of
https://github.com/postgres/postgres.git
synced 2025-12-19 17:02:53 +03:00
Move dynamically-allocated LWLock tranche names to shared memory.
There are two ways for shared libraries to allocate their own LWLock tranches. One way is to call RequestNamedLWLockTranche() in a shmem_request_hook, which requires the library to be loaded via shared_preload_libraries. The other way is to call LWLockNewTrancheId(), which is not subject to the same restrictions. However, LWLockNewTrancheId() does require each backend to store the tranche's name in backend-local memory via LWLockRegisterTranche(). This API is a little cumbersome and leads to things like unhelpful pg_stat_activity.wait_event values in backends that haven't loaded the library. This commit moves these LWLock tranche names to shared memory, thus eliminating the need for each backend to call LWLockRegisterTranche(). Instead, the tranche name must be provided to LWLockNewTrancheId(), which immediately makes the name available to all backends. Since the tranche name array is append-only, lookups can ordinarily avoid locking as long as their local copy of the LWLock counter is greater than the requested tranche ID. One downside of this approach is that we now have a hard limit on both the length of tranche names (NAMEDATALEN-1 bytes) and the number of dynamically-allocated tranches (256). Besides a limit of NAMEDATALEN-1 bytes for tranche names registered via RequestNamedLWLockTranche(), no such limits previously existed. We could avoid these new limits by using dynamic shared memory, but the complexity involved didn't seem worth it. We briefly considered making the tranche limit user-configurable but ultimately decided against that, too. Since there is still a lot of time left in the v19 development cycle, it's possible we will revisit this choice. Author: Sami Imseih <samimseih@gmail.com> Reviewed-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Rahila Syed <rahilasyed90@gmail.com> Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/CAA5RZ0vvED3naph8My8Szv6DL4AxOVK3eTPS0qXsaKi%3DbVdW2A%40mail.gmail.com
This commit is contained in:
@@ -29,8 +29,7 @@ test_dsa_basic(PG_FUNCTION_ARGS)
|
||||
dsa_pointer p[100];
|
||||
|
||||
/* XXX: this tranche is leaked */
|
||||
tranche_id = LWLockNewTrancheId();
|
||||
LWLockRegisterTranche(tranche_id, "test_dsa");
|
||||
tranche_id = LWLockNewTrancheId("test_dsa");
|
||||
|
||||
a = dsa_create(tranche_id);
|
||||
for (int i = 0; i < 100; i++)
|
||||
@@ -70,8 +69,7 @@ test_dsa_resowners(PG_FUNCTION_ARGS)
|
||||
ResourceOwner childowner;
|
||||
|
||||
/* XXX: this tranche is leaked */
|
||||
tranche_id = LWLockNewTrancheId();
|
||||
LWLockRegisterTranche(tranche_id, "test_dsa");
|
||||
tranche_id = LWLockNewTrancheId("test_dsa");
|
||||
|
||||
/* Create DSA in parent resource owner */
|
||||
a = dsa_create(tranche_id);
|
||||
|
||||
@@ -48,7 +48,7 @@ init_tdr_dsm(void *ptr)
|
||||
{
|
||||
TestDSMRegistryStruct *dsm = (TestDSMRegistryStruct *) ptr;
|
||||
|
||||
LWLockInitialize(&dsm->lck, LWLockNewTrancheId());
|
||||
LWLockInitialize(&dsm->lck, LWLockNewTrancheId("test_dsm_registry"));
|
||||
dsm->val = 0;
|
||||
}
|
||||
|
||||
@@ -61,7 +61,6 @@ tdr_attach_shmem(void)
|
||||
sizeof(TestDSMRegistryStruct),
|
||||
init_tdr_dsm,
|
||||
&found);
|
||||
LWLockRegisterTranche(tdr_dsm->lck.tranche, "test_dsm_registry");
|
||||
|
||||
if (tdr_dsa == NULL)
|
||||
tdr_dsa = GetNamedDSA("test_dsm_registry_dsa", &found);
|
||||
|
||||
@@ -124,10 +124,9 @@ test_empty(void)
|
||||
rt_iter *iter;
|
||||
uint64 key;
|
||||
#ifdef TEST_SHARED_RT
|
||||
int tranche_id = LWLockNewTrancheId();
|
||||
int tranche_id = LWLockNewTrancheId("test_radix_tree");
|
||||
dsa_area *dsa;
|
||||
|
||||
LWLockRegisterTranche(tranche_id, "test_radix_tree");
|
||||
dsa = dsa_create(tranche_id);
|
||||
radixtree = rt_create(dsa, tranche_id);
|
||||
#else
|
||||
@@ -167,10 +166,9 @@ test_basic(rt_node_class_test_elem *test_info, int shift, bool asc)
|
||||
uint64 *keys;
|
||||
int children = test_info->nkeys;
|
||||
#ifdef TEST_SHARED_RT
|
||||
int tranche_id = LWLockNewTrancheId();
|
||||
int tranche_id = LWLockNewTrancheId("test_radix_tree");
|
||||
dsa_area *dsa;
|
||||
|
||||
LWLockRegisterTranche(tranche_id, "test_radix_tree");
|
||||
dsa = dsa_create(tranche_id);
|
||||
radixtree = rt_create(dsa, tranche_id);
|
||||
#else
|
||||
@@ -304,10 +302,9 @@ test_random(void)
|
||||
int num_keys = 100000;
|
||||
uint64 *keys;
|
||||
#ifdef TEST_SHARED_RT
|
||||
int tranche_id = LWLockNewTrancheId();
|
||||
int tranche_id = LWLockNewTrancheId("test_radix_tree");
|
||||
dsa_area *dsa;
|
||||
|
||||
LWLockRegisterTranche(tranche_id, "test_radix_tree");
|
||||
dsa = dsa_create(tranche_id);
|
||||
radixtree = rt_create(dsa, tranche_id);
|
||||
#else
|
||||
|
||||
@@ -232,11 +232,9 @@ test_slru_shmem_startup(void)
|
||||
(void) MakePGDirectory(slru_dir_name);
|
||||
|
||||
/* initialize the SLRU facility */
|
||||
test_tranche_id = LWLockNewTrancheId();
|
||||
LWLockRegisterTranche(test_tranche_id, "test_slru_tranche");
|
||||
test_tranche_id = LWLockNewTrancheId("test_slru_tranche");
|
||||
|
||||
test_buffer_tranche_id = LWLockNewTrancheId();
|
||||
LWLockRegisterTranche(test_tranche_id, "test_buffer_tranche");
|
||||
test_buffer_tranche_id = LWLockNewTrancheId("test_buffer_tranche");
|
||||
|
||||
TestSlruCtl->PagePrecedes = test_slru_page_precedes_logically;
|
||||
SimpleLruInit(TestSlruCtl, "TestSLRU",
|
||||
|
||||
@@ -103,8 +103,7 @@ test_create(PG_FUNCTION_ARGS)
|
||||
{
|
||||
int tranche_id;
|
||||
|
||||
tranche_id = LWLockNewTrancheId();
|
||||
LWLockRegisterTranche(tranche_id, "test_tidstore");
|
||||
tranche_id = LWLockNewTrancheId("test_tidstore");
|
||||
|
||||
tidstore = TidStoreCreateShared(tidstore_max_size, tranche_id);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user