LCOV - code coverage report
Current view: top level - src/backend/storage/ipc - dsm_registry.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 139 156 89.1 %
Date: 2025-11-28 14:17:41 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * dsm_registry.c
       4             :  *    Functions for interfacing with the dynamic shared memory registry.
       5             :  *
       6             :  * This provides a way for libraries to use shared memory without needing
       7             :  * to request it at startup time via a shmem_request_hook.  The registry
       8             :  * stores dynamic shared memory (DSM) segment handles keyed by a
       9             :  * library-specified string.
      10             :  *
      11             :  * The registry is accessed by calling GetNamedDSMSegment().  If a segment
      12             :  * with the provided name does not yet exist, it is created and initialized
      13             :  * with the provided init_callback callback function.  Otherwise,
      14             :  * GetNamedDSMSegment() simply ensures that the segment is attached to the
      15             :  * current backend.  This function guarantees that only one backend
      16             :  * initializes the segment and that all other backends just attach it.
      17             :  *
      18             :  * A DSA can be created in or retrieved from the registry by calling
      19             :  * GetNamedDSA().  As with GetNamedDSMSegment(), if a DSA with the provided
      20             :  * name does not yet exist, it is created.  Otherwise, GetNamedDSA()
      21             :  * ensures the DSA is attached to the current backend.  This function
      22             :  * guarantees that only one backend initializes the DSA and that all other
      23             :  * backends just attach it.
      24             :  *
      25             :  * A dshash table can be created in or retrieved from the registry by
      26             :  * calling GetNamedDSHash().  As with GetNamedDSMSegment(), if a hash
      27             :  * table with the provided name does not yet exist, it is created.
      28             :  * Otherwise, GetNamedDSHash() ensures the hash table is attached to the
      29             :  * current backend.  This function guarantees that only one backend
      30             :  * initializes the table and that all other backends just attach it.
      31             :  *
      32             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
      33             :  * Portions Copyright (c) 1994, Regents of the University of California
      34             :  *
      35             :  * IDENTIFICATION
      36             :  *    src/backend/storage/ipc/dsm_registry.c
      37             :  *
      38             :  *-------------------------------------------------------------------------
      39             :  */
      40             : 
      41             : #include "postgres.h"
      42             : 
      43             : #include "funcapi.h"
      44             : #include "lib/dshash.h"
      45             : #include "storage/dsm_registry.h"
      46             : #include "storage/lwlock.h"
      47             : #include "storage/shmem.h"
      48             : #include "utils/builtins.h"
      49             : #include "utils/memutils.h"
      50             : 
      51             : typedef struct DSMRegistryCtxStruct
      52             : {
      53             :     dsa_handle  dsah;
      54             :     dshash_table_handle dshh;
      55             : } DSMRegistryCtxStruct;
      56             : 
      57             : static DSMRegistryCtxStruct *DSMRegistryCtx;
      58             : 
      59             : typedef struct NamedDSMState
      60             : {
      61             :     dsm_handle  handle;
      62             :     size_t      size;
      63             : } NamedDSMState;
      64             : 
      65             : typedef struct NamedDSAState
      66             : {
      67             :     dsa_handle  handle;
      68             :     int         tranche;
      69             : } NamedDSAState;
      70             : 
      71             : typedef struct NamedDSHState
      72             : {
      73             :     dsa_handle dsa_handle;
      74             :     dshash_table_handle dsh_handle;
      75             :     int         tranche;
      76             : } NamedDSHState;
      77             : 
      78             : typedef enum DSMREntryType
      79             : {
      80             :     DSMR_ENTRY_TYPE_DSM,
      81             :     DSMR_ENTRY_TYPE_DSA,
      82             :     DSMR_ENTRY_TYPE_DSH,
      83             : } DSMREntryType;
      84             : 
      85             : static const char *const DSMREntryTypeNames[] =
      86             : {
      87             :     [DSMR_ENTRY_TYPE_DSM] = "segment",
      88             :     [DSMR_ENTRY_TYPE_DSA] = "area",
      89             :     [DSMR_ENTRY_TYPE_DSH] = "hash",
      90             : };
      91             : 
      92             : typedef struct DSMRegistryEntry
      93             : {
      94             :     char        name[NAMEDATALEN];
      95             :     DSMREntryType type;
      96             :     union
      97             :     {
      98             :         NamedDSMState dsm;
      99             :         NamedDSAState dsa;
     100             :         NamedDSHState dsh;
     101             :     };
     102             : } DSMRegistryEntry;
     103             : 
     104             : static const dshash_parameters dsh_params = {
     105             :     offsetof(DSMRegistryEntry, type),
     106             :     sizeof(DSMRegistryEntry),
     107             :     dshash_strcmp,
     108             :     dshash_strhash,
     109             :     dshash_strcpy,
     110             :     LWTRANCHE_DSM_REGISTRY_HASH
     111             : };
     112             : 
     113             : static dsa_area *dsm_registry_dsa;
     114             : static dshash_table *dsm_registry_table;
     115             : 
     116             : Size
     117        6312 : DSMRegistryShmemSize(void)
     118             : {
     119        6312 :     return MAXALIGN(sizeof(DSMRegistryCtxStruct));
     120             : }
     121             : 
     122             : void
     123        2204 : DSMRegistryShmemInit(void)
     124             : {
     125             :     bool        found;
     126             : 
     127        2204 :     DSMRegistryCtx = (DSMRegistryCtxStruct *)
     128        2204 :         ShmemInitStruct("DSM Registry Data",
     129             :                         DSMRegistryShmemSize(),
     130             :                         &found);
     131             : 
     132        2204 :     if (!found)
     133             :     {
     134        2204 :         DSMRegistryCtx->dsah = DSA_HANDLE_INVALID;
     135        2204 :         DSMRegistryCtx->dshh = DSHASH_HANDLE_INVALID;
     136             :     }
     137        2204 : }
     138             : 
     139             : /*
     140             :  * Initialize or attach to the dynamic shared hash table that stores the DSM
     141             :  * registry entries, if not already done.  This must be called before accessing
     142             :  * the table.
     143             :  */
     144             : static void
     145         106 : init_dsm_registry(void)
     146             : {
     147             :     /* Quick exit if we already did this. */
     148         106 :     if (dsm_registry_table)
     149          16 :         return;
     150             : 
     151             :     /* Otherwise, use a lock to ensure only one process creates the table. */
     152          90 :     LWLockAcquire(DSMRegistryLock, LW_EXCLUSIVE);
     153             : 
     154          90 :     if (DSMRegistryCtx->dshh == DSHASH_HANDLE_INVALID)
     155             :     {
     156             :         /* Initialize dynamic shared hash table for registry. */
     157          22 :         dsm_registry_dsa = dsa_create(LWTRANCHE_DSM_REGISTRY_DSA);
     158          22 :         dsm_registry_table = dshash_create(dsm_registry_dsa, &dsh_params, NULL);
     159             : 
     160          22 :         dsa_pin(dsm_registry_dsa);
     161          22 :         dsa_pin_mapping(dsm_registry_dsa);
     162             : 
     163             :         /* Store handles in shared memory for other backends to use. */
     164          22 :         DSMRegistryCtx->dsah = dsa_get_handle(dsm_registry_dsa);
     165          22 :         DSMRegistryCtx->dshh = dshash_get_hash_table_handle(dsm_registry_table);
     166             :     }
     167             :     else
     168             :     {
     169             :         /* Attach to existing dynamic shared hash table. */
     170          68 :         dsm_registry_dsa = dsa_attach(DSMRegistryCtx->dsah);
     171          68 :         dsa_pin_mapping(dsm_registry_dsa);
     172          68 :         dsm_registry_table = dshash_attach(dsm_registry_dsa, &dsh_params,
     173          68 :                                            DSMRegistryCtx->dshh, NULL);
     174             :     }
     175             : 
     176          90 :     LWLockRelease(DSMRegistryLock);
     177             : }
     178             : 
     179             : /*
     180             :  * Initialize or attach a named DSM segment.
     181             :  *
     182             :  * This routine returns the address of the segment.  init_callback is called to
     183             :  * initialize the segment when it is first created.
     184             :  */
     185             : void *
     186          94 : GetNamedDSMSegment(const char *name, size_t size,
     187             :                    void (*init_callback) (void *ptr), bool *found)
     188             : {
     189             :     DSMRegistryEntry *entry;
     190             :     MemoryContext oldcontext;
     191             :     void       *ret;
     192             :     NamedDSMState *state;
     193             :     dsm_segment *seg;
     194             : 
     195             :     Assert(found);
     196             : 
     197          94 :     if (!name || *name == '\0')
     198           0 :         ereport(ERROR,
     199             :                 (errmsg("DSM segment name cannot be empty")));
     200             : 
     201          94 :     if (strlen(name) >= offsetof(DSMRegistryEntry, type))
     202           0 :         ereport(ERROR,
     203             :                 (errmsg("DSM segment name too long")));
     204             : 
     205          94 :     if (size == 0)
     206           0 :         ereport(ERROR,
     207             :                 (errmsg("DSM segment size must be nonzero")));
     208             : 
     209             :     /* Be sure any local memory allocated by DSM/DSA routines is persistent. */
     210          94 :     oldcontext = MemoryContextSwitchTo(TopMemoryContext);
     211             : 
     212             :     /* Connect to the registry. */
     213          94 :     init_dsm_registry();
     214             : 
     215          94 :     entry = dshash_find_or_insert(dsm_registry_table, name, found);
     216          94 :     state = &entry->dsm;
     217          94 :     if (!(*found))
     218             :     {
     219          22 :         entry->type = DSMR_ENTRY_TYPE_DSM;
     220          22 :         state->handle = DSM_HANDLE_INVALID;
     221          22 :         state->size = size;
     222             :     }
     223          72 :     else if (entry->type != DSMR_ENTRY_TYPE_DSM)
     224           0 :         ereport(ERROR,
     225             :                 (errmsg("requested DSM segment does not match type of existing entry")));
     226          72 :     else if (state->size != size)
     227           0 :         ereport(ERROR,
     228             :                 (errmsg("requested DSM segment size does not match size of existing segment")));
     229             : 
     230          94 :     if (state->handle == DSM_HANDLE_INVALID)
     231             :     {
     232          22 :         *found = false;
     233             : 
     234             :         /* Initialize the segment. */
     235          22 :         seg = dsm_create(size, 0);
     236             : 
     237          22 :         if (init_callback)
     238          22 :             (*init_callback) (dsm_segment_address(seg));
     239             : 
     240          22 :         dsm_pin_segment(seg);
     241          22 :         dsm_pin_mapping(seg);
     242          22 :         state->handle = dsm_segment_handle(seg);
     243             :     }
     244             :     else
     245             :     {
     246             :         /* If the existing segment is not already attached, attach it now. */
     247          72 :         seg = dsm_find_mapping(state->handle);
     248          72 :         if (seg == NULL)
     249             :         {
     250          66 :             seg = dsm_attach(state->handle);
     251          66 :             if (seg == NULL)
     252           0 :                 elog(ERROR, "could not map dynamic shared memory segment");
     253             : 
     254          66 :             dsm_pin_mapping(seg);
     255             :         }
     256             :     }
     257             : 
     258          94 :     ret = dsm_segment_address(seg);
     259          94 :     dshash_release_lock(dsm_registry_table, entry);
     260          94 :     MemoryContextSwitchTo(oldcontext);
     261             : 
     262          94 :     return ret;
     263             : }
     264             : 
     265             : /*
     266             :  * Initialize or attach a named DSA.
     267             :  *
     268             :  * This routine returns a pointer to the DSA.  A new LWLock tranche ID will be
     269             :  * generated if needed.  Note that the lock tranche will be registered with the
     270             :  * provided name.  Also note that this should be called at most once for a
     271             :  * given DSA in each backend.
     272             :  */
     273             : dsa_area *
     274           4 : GetNamedDSA(const char *name, bool *found)
     275             : {
     276             :     DSMRegistryEntry *entry;
     277             :     MemoryContext oldcontext;
     278             :     dsa_area   *ret;
     279             :     NamedDSAState *state;
     280             : 
     281             :     Assert(found);
     282             : 
     283           4 :     if (!name || *name == '\0')
     284           0 :         ereport(ERROR,
     285             :                 (errmsg("DSA name cannot be empty")));
     286             : 
     287           4 :     if (strlen(name) >= offsetof(DSMRegistryEntry, type))
     288           0 :         ereport(ERROR,
     289             :                 (errmsg("DSA name too long")));
     290             : 
     291             :     /* Be sure any local memory allocated by DSM/DSA routines is persistent. */
     292           4 :     oldcontext = MemoryContextSwitchTo(TopMemoryContext);
     293             : 
     294             :     /* Connect to the registry. */
     295           4 :     init_dsm_registry();
     296             : 
     297           4 :     entry = dshash_find_or_insert(dsm_registry_table, name, found);
     298           4 :     state = &entry->dsa;
     299           4 :     if (!(*found))
     300             :     {
     301           2 :         entry->type = DSMR_ENTRY_TYPE_DSA;
     302           2 :         state->handle = DSA_HANDLE_INVALID;
     303           2 :         state->tranche = -1;
     304             :     }
     305           2 :     else if (entry->type != DSMR_ENTRY_TYPE_DSA)
     306           0 :         ereport(ERROR,
     307             :                 (errmsg("requested DSA does not match type of existing entry")));
     308             : 
     309           4 :     if (state->tranche == -1)
     310             :     {
     311           2 :         *found = false;
     312             : 
     313             :         /* Initialize the LWLock tranche for the DSA. */
     314           2 :         state->tranche = LWLockNewTrancheId(name);
     315             :     }
     316             : 
     317           4 :     if (state->handle == DSA_HANDLE_INVALID)
     318             :     {
     319           2 :         *found = false;
     320             : 
     321             :         /* Initialize the DSA. */
     322           2 :         ret = dsa_create(state->tranche);
     323           2 :         dsa_pin(ret);
     324           2 :         dsa_pin_mapping(ret);
     325             : 
     326             :         /* Store handle for other backends to use. */
     327           2 :         state->handle = dsa_get_handle(ret);
     328             :     }
     329           2 :     else if (dsa_is_attached(state->handle))
     330           0 :         ereport(ERROR,
     331             :                 (errmsg("requested DSA already attached to current process")));
     332             :     else
     333             :     {
     334             :         /* Attach to existing DSA. */
     335           2 :         ret = dsa_attach(state->handle);
     336           2 :         dsa_pin_mapping(ret);
     337             :     }
     338             : 
     339           4 :     dshash_release_lock(dsm_registry_table, entry);
     340           4 :     MemoryContextSwitchTo(oldcontext);
     341             : 
     342           4 :     return ret;
     343             : }
     344             : 
     345             : /*
     346             :  * Initialize or attach a named dshash table.
     347             :  *
     348             :  * This routine returns the address of the table.  The tranche_id member of
     349             :  * params is ignored; a new LWLock tranche ID will be generated if needed.
     350             :  * Note that the lock tranche will be registered with the provided name.  Also
     351             :  * note that this should be called at most once for a given table in each
     352             :  * backend.
     353             :  */
     354             : dshash_table *
     355           4 : GetNamedDSHash(const char *name, const dshash_parameters *params, bool *found)
     356             : {
     357             :     DSMRegistryEntry *entry;
     358             :     MemoryContext oldcontext;
     359             :     dshash_table *ret;
     360             :     NamedDSHState *dsh_state;
     361             : 
     362             :     Assert(params);
     363             :     Assert(found);
     364             : 
     365           4 :     if (!name || *name == '\0')
     366           0 :         ereport(ERROR,
     367             :                 (errmsg("DSHash name cannot be empty")));
     368             : 
     369           4 :     if (strlen(name) >= offsetof(DSMRegistryEntry, type))
     370           0 :         ereport(ERROR,
     371             :                 (errmsg("DSHash name too long")));
     372             : 
     373             :     /* Be sure any local memory allocated by DSM/DSA routines is persistent. */
     374           4 :     oldcontext = MemoryContextSwitchTo(TopMemoryContext);
     375             : 
     376             :     /* Connect to the registry. */
     377           4 :     init_dsm_registry();
     378             : 
     379           4 :     entry = dshash_find_or_insert(dsm_registry_table, name, found);
     380           4 :     dsh_state = &entry->dsh;
     381           4 :     if (!(*found))
     382             :     {
     383           2 :         entry->type = DSMR_ENTRY_TYPE_DSH;
     384           2 :         dsh_state->dsa_handle = DSA_HANDLE_INVALID;
     385           2 :         dsh_state->dsh_handle = DSHASH_HANDLE_INVALID;
     386           2 :         dsh_state->tranche = -1;
     387             :     }
     388           2 :     else if (entry->type != DSMR_ENTRY_TYPE_DSH)
     389           0 :         ereport(ERROR,
     390             :                 (errmsg("requested DSHash does not match type of existing entry")));
     391             : 
     392           4 :     if (dsh_state->tranche == -1)
     393             :     {
     394           2 :         *found = false;
     395             : 
     396             :         /* Initialize the LWLock tranche for the hash table. */
     397           2 :         dsh_state->tranche = LWLockNewTrancheId(name);
     398             :     }
     399             : 
     400           4 :     if (dsh_state->dsa_handle == DSA_HANDLE_INVALID)
     401             :     {
     402             :         dshash_parameters params_copy;
     403             :         dsa_area   *dsa;
     404             : 
     405           2 :         *found = false;
     406             : 
     407             :         /* Initialize the DSA for the hash table. */
     408           2 :         dsa = dsa_create(dsh_state->tranche);
     409             : 
     410             :         /* Initialize the dshash table. */
     411           2 :         memcpy(&params_copy, params, sizeof(dshash_parameters));
     412           2 :         params_copy.tranche_id = dsh_state->tranche;
     413           2 :         ret = dshash_create(dsa, &params_copy, NULL);
     414             : 
     415           2 :         dsa_pin(dsa);
     416           2 :         dsa_pin_mapping(dsa);
     417             : 
     418             :         /* Store handles for other backends to use. */
     419           2 :         dsh_state->dsa_handle = dsa_get_handle(dsa);
     420           2 :         dsh_state->dsh_handle = dshash_get_hash_table_handle(ret);
     421             :     }
     422           2 :     else if (dsa_is_attached(dsh_state->dsa_handle))
     423           0 :         ereport(ERROR,
     424             :                 (errmsg("requested DSHash already attached to current process")));
     425             :     else
     426             :     {
     427             :         dsa_area   *dsa;
     428             : 
     429             :         /* XXX: Should we verify params matches what table was created with? */
     430             : 
     431             :         /* Attach to existing DSA for the hash table. */
     432           2 :         dsa = dsa_attach(dsh_state->dsa_handle);
     433           2 :         dsa_pin_mapping(dsa);
     434             : 
     435             :         /* Attach to existing dshash table. */
     436           2 :         ret = dshash_attach(dsa, params, dsh_state->dsh_handle, NULL);
     437             :     }
     438             : 
     439           4 :     dshash_release_lock(dsm_registry_table, entry);
     440           4 :     MemoryContextSwitchTo(oldcontext);
     441             : 
     442           4 :     return ret;
     443             : }
     444             : 
     445             : Datum
     446           4 : pg_get_dsm_registry_allocations(PG_FUNCTION_ARGS)
     447             : {
     448           4 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
     449             :     DSMRegistryEntry *entry;
     450             :     MemoryContext oldcontext;
     451             :     dshash_seq_status status;
     452             : 
     453           4 :     InitMaterializedSRF(fcinfo, MAT_SRF_USE_EXPECTED_DESC);
     454             : 
     455             :     /* Be sure any local memory allocated by DSM/DSA routines is persistent. */
     456           4 :     oldcontext = MemoryContextSwitchTo(TopMemoryContext);
     457           4 :     init_dsm_registry();
     458           4 :     MemoryContextSwitchTo(oldcontext);
     459             : 
     460           4 :     dshash_seq_init(&status, dsm_registry_table, false);
     461          10 :     while ((entry = dshash_seq_next(&status)) != NULL)
     462             :     {
     463             :         Datum       vals[3];
     464           6 :         bool        nulls[3] = {0};
     465             : 
     466             :         /* Do not show partially-initialized entries. */
     467           6 :         if (entry->type == DSMR_ENTRY_TYPE_DSM &&
     468           2 :             entry->dsm.handle == DSM_HANDLE_INVALID)
     469           0 :             continue;
     470           6 :         if (entry->type == DSMR_ENTRY_TYPE_DSA &&
     471           2 :             entry->dsa.handle == DSA_HANDLE_INVALID)
     472           0 :             continue;
     473           6 :         if (entry->type == DSMR_ENTRY_TYPE_DSH &&
     474           2 :             entry->dsh.dsa_handle == DSA_HANDLE_INVALID)
     475           0 :             continue;
     476             : 
     477           6 :         vals[0] = CStringGetTextDatum(entry->name);
     478           6 :         vals[1] = CStringGetTextDatum(DSMREntryTypeNames[entry->type]);
     479             : 
     480             :         /*
     481             :          * Since we can't know the size of DSA/dshash entries without first
     482             :          * attaching to them, return NULL for those.
     483             :          */
     484           6 :         if (entry->type == DSMR_ENTRY_TYPE_DSM)
     485           2 :             vals[2] = Int64GetDatum(entry->dsm.size);
     486             :         else
     487           4 :             nulls[2] = true;
     488             : 
     489           6 :         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, vals, nulls);
     490             :     }
     491           4 :     dshash_seq_term(&status);
     492             : 
     493           4 :     return (Datum) 0;
     494             : }

Generated by: LCOV version 1.16