@@ -3687,18 +3687,32 @@ LWLockRelease(AddinShmemInitLock);
36873687 </sect3>
36883688
36893689 <sect3 id="xfunc-shared-addin-after-startup">
3690- <title>Requesting Shared Memory After Startup </title>
3690+ <title>DSM Registry </title>
36913691
36923692 <para>
3693- There is another, more flexible method of reserving shared memory that
3694- can be done after server startup and outside a
3695- <literal>shmem_request_hook</literal>. To do so, each backend that will
3696- use the shared memory should obtain a pointer to it by calling:
3697- <programlisting>
3698- void *GetNamedDSMSegment(const char *name, size_t size,
3693+ Another, more flexible way to reserve shared memory after server startup
3694+ (and outside a <literal>shmem_request_hook</literal>) is to use the dynamic shared memory (DSM) registry.
3695+ The registry maps library-defined string keys to DSM handles.
3696+ It supports creating named DSM segments with <function>GetNamedDSMSegment</function>,
3697+ named dynamic shared memory areas (DSAs) with <function>GetNamedDSA</function>,
3698+ and named hash tables backed by a DSA with <function>GetNamedDSHash</function>.
3699+ </para>
3700+
3701+ <para>
3702+ Unlike shared memory reserved at server startup, there is no need to
3703+ acquire <function>AddinShmemInitLock</function> or otherwise take action
3704+ to avoid race conditions when reserving shared memory with these functions.
3705+ They ensure that only one backend allocates and initializes the memory structure and that all other
3706+ backends receive an appropriate pointer to the fully allocated and initialized segment, area or hash table.
3707+ </para>
3708+
3709+ <para>
3710+ Consider for example this function to allocate a named DSM segment.
3711+ <programlisting>
3712+ void *GetNamedDSMSegment(const char *name, size_t size,
36993713 void (*init_callback) (void *ptr),
37003714 bool *found)
3701- </programlisting>
3715+ </programlisting>
37023716 If a dynamic shared memory segment with the given name does not yet
37033717 exist, this function will allocate it and initialize it with the provided
37043718 <function>init_callback</function> callback function. If the segment has
@@ -3707,19 +3721,34 @@ void *GetNamedDSMSegment(const char *name, size_t size,
37073721 backend.
37083722 </para>
37093723
3724+ <para>The same logic applies to the other functions offered by the registry.</para>
3725+
37103726 <para>
3711- Unlike shared memory reserved at server startup, there is no need to
3712- acquire <function>AddinShmemInitLock</function> or otherwise take action
3713- to avoid race conditions when reserving shared memory with
3714- <function>GetNamedDSMSegment</function>. This function ensures that only
3715- one backend allocates and initializes the segment and that all other
3716- backends receive a pointer to the fully allocated and initialized
3717- segment.
3727+ To allocate a named DSA, one can use
3728+ <programlisting>
3729+ dsa_area *GetNamedDSA(const char *name, bool *found)
3730+ </programlisting>
3731+
3732+ Νote that this should be called at most once for a given DSA in each backend.
3733+ </para>
3734+
3735+ <para>
3736+ To allocate a named hash table backed by shared memory, one can use
3737+ <programlisting>
3738+ dshash_table *GetNamedDSHash(const char *name, const dshash_parameters *params, bool *found)
3739+ </programlisting>
3740+
3741+ Also note that this should be called at most once for a given table in each backend.
3742+ </para>
3743+
3744+ <para>
3745+ To monitor the allocations of the DSM registry
3746+ the <literal>pg_dsm_registry_allocations</literal> system view
3747+ documented in <xref linkend="view-pg-dsm-registry-allocations"/> can be used.
37183748 </para>
37193749
37203750 <para>
3721- A complete usage example of <function>GetNamedDSMSegment</function> can
3722- be found in
3751+ A complete usage example of the DSM Registry and its facilities can be found in
37233752 <filename>src/test/modules/test_dsm_registry/test_dsm_registry.c</filename>
37243753 in the <productname>PostgreSQL</productname> source tree.
37253754 </para>
0 commit comments