Introduce the dynamic shared memory registry.
authorNathan Bossart <[email protected]>
Fri, 19 Jan 2024 20:24:36 +0000 (14:24 -0600)
committerNathan Bossart <[email protected]>
Fri, 19 Jan 2024 20:24:36 +0000 (14:24 -0600)
commit8b2bcf3f287c79eaebf724cba57e5ff664b01e06
tree60a986a9824dced9cca2a5b53b93bfe7ea59216a
parent964152c476f25ada4c5832a014999ec2d2980358
Introduce the dynamic shared memory registry.

Presently, the most straightforward way for a shared library to use
shared memory is to request it at server startup via a
shmem_request_hook, which requires specifying the library in
shared_preload_libraries.  Alternatively, the library can create a
dynamic shared memory (DSM) segment, but absent a shared location
to store the segment's handle, other backends cannot use it.  This
commit introduces a registry for DSM segments so that these other
backends can look up existing segments with a library-specified
string.  This allows libraries to easily use shared memory without
needing to request it at server startup.

The registry is accessed via the new GetNamedDSMSegment() function.
This function handles allocating the segment and initializing it
via a provided callback.  If another backend already created and
initialized the segment, it simply attaches the segment.
GetNamedDSMSegment() locks the registry appropriately to ensure
that only one backend initializes the segment and that all other
backends just attach it.

The registry itself is comprised of a dshash table that stores the
DSM segment handles keyed by a library-specified string.

Reviewed-by: Michael Paquier, Andrei Lepikhov, Nikita Malakhov, Robert Haas, Bharath Rupireddy, Zhang Mingli, Amul Sul
Discussion: https://postgr.es/m/20231205034647.GA2705267%40nathanxps13
21 files changed:
doc/src/sgml/xfunc.sgml
src/backend/storage/ipc/Makefile
src/backend/storage/ipc/dsm_registry.c [new file with mode: 0644]
src/backend/storage/ipc/ipci.c
src/backend/storage/ipc/meson.build
src/backend/storage/lmgr/lwlock.c
src/backend/storage/lmgr/lwlocknames.txt
src/backend/utils/activity/wait_event_names.txt
src/include/storage/dsm_registry.h [new file with mode: 0644]
src/include/storage/lwlock.h
src/test/modules/Makefile
src/test/modules/meson.build
src/test/modules/test_dsm_registry/.gitignore [new file with mode: 0644]
src/test/modules/test_dsm_registry/Makefile [new file with mode: 0644]
src/test/modules/test_dsm_registry/expected/test_dsm_registry.out [new file with mode: 0644]
src/test/modules/test_dsm_registry/meson.build [new file with mode: 0644]
src/test/modules/test_dsm_registry/sql/test_dsm_registry.sql [new file with mode: 0644]
src/test/modules/test_dsm_registry/test_dsm_registry--1.0.sql [new file with mode: 0644]
src/test/modules/test_dsm_registry/test_dsm_registry.c [new file with mode: 0644]
src/test/modules/test_dsm_registry/test_dsm_registry.control [new file with mode: 0644]
src/tools/pgindent/typedefs.list