Refactor lock manager initialization to make it a bit less special
authorHeikki Linnakangas <[email protected]>
Thu, 29 Aug 2024 06:46:06 +0000 (09:46 +0300)
committerHeikki Linnakangas <[email protected]>
Thu, 29 Aug 2024 06:46:06 +0000 (09:46 +0300)
Split the shared and local initialization to separate functions, and
follow the common naming conventions. With this, we no longer create
the LockMethodLocalHash hash table in the postmaster process, which
was always pointless.

Reviewed-by: Andreas Karlsson
Discussion: https://www.postgresql.org/message-id/c09694ff-2453-47e5-b26c-32a16cd75ce6@iki.fi

src/backend/storage/ipc/ipci.c
src/backend/storage/lmgr/lock.c
src/backend/utils/init/postinit.c
src/include/storage/lock.h

index 35fa2e1dda64bc475505d52637bfbb59d26f3d26..8f8a89124e84f174629a9d8e16578a45dd921ddb 100644 (file)
@@ -116,7 +116,7 @@ CalculateShmemSize(int *num_semaphores)
    size = add_size(size, dsm_estimate_size());
    size = add_size(size, DSMRegistryShmemSize());
    size = add_size(size, BufferShmemSize());
-   size = add_size(size, LockShmemSize());
+   size = add_size(size, LockManagerShmemSize());
    size = add_size(size, PredicateLockShmemSize());
    size = add_size(size, ProcGlobalShmemSize());
    size = add_size(size, XLogPrefetchShmemSize());
@@ -291,7 +291,7 @@ CreateOrAttachShmemStructs(void)
    /*
     * Set up lock manager
     */
-   InitLocks();
+   LockManagerShmemInit();
 
    /*
     * Set up predicate lock manager
index 0400a5077799ffd11fd8a025fc3fd9490bb62b55..6dbc41dae707db413c62a73b8668d2f9852c4501 100644 (file)
@@ -377,19 +377,17 @@ static void GetSingleProcBlockerStatusData(PGPROC *blocked_proc,
 
 
 /*
- * InitLocks -- Initialize the lock manager's data structures.
+ * Initialize the lock manager's shmem data structures.
  *
- * This is called from CreateSharedMemoryAndSemaphores(), which see for
- * more comments.  In the normal postmaster case, the shared hash tables
- * are created here, as well as a locallock hash table that will remain
- * unused and empty in the postmaster itself.  Backends inherit the pointers
- * to the shared tables via fork(), and also inherit an image of the locallock
- * hash table, which they proceed to use.  In the EXEC_BACKEND case, each
- * backend re-executes this code to obtain pointers to the already existing
- * shared hash tables and to create its locallock hash table.
+ * This is called from CreateSharedMemoryAndSemaphores(), which see for more
+ * comments.  In the normal postmaster case, the shared hash tables are
+ * created here, and backends inherit pointers to them via fork().  In the
+ * EXEC_BACKEND case, each backend re-executes this code to obtain pointers to
+ * the already existing shared hash tables.  In either case, each backend must
+ * also call InitLockManagerAccess() to create the locallock hash table.
  */
 void
-InitLocks(void)
+LockManagerShmemInit(void)
 {
    HASHCTL     info;
    long        init_table_size,
@@ -444,18 +442,19 @@ InitLocks(void)
                        sizeof(FastPathStrongRelationLockData), &found);
    if (!found)
        SpinLockInit(&FastPathStrongRelationLocks->mutex);
+}
 
+/*
+ * Initialize the lock manager's backend-private data structures.
+ */
+void
+InitLockManagerAccess(void)
+{
    /*
     * Allocate non-shared hash table for LOCALLOCK structs.  This stores lock
     * counts and resource owner information.
-    *
-    * The non-shared table could already exist in this process (this occurs
-    * when the postmaster is recreating shared memory after a backend crash).
-    * If so, delete and recreate it.  (We could simply leave it, since it
-    * ought to be empty in the postmaster, but for safety let's zap it.)
     */
-   if (LockMethodLocalHash)
-       hash_destroy(LockMethodLocalHash);
+   HASHCTL     info;
 
    info.keysize = sizeof(LOCALLOCKTAG);
    info.entrysize = sizeof(LOCALLOCK);
@@ -3571,7 +3570,7 @@ PostPrepare_Locks(TransactionId xid)
  * Estimate shared-memory space used for lock tables
  */
 Size
-LockShmemSize(void)
+LockManagerShmemSize(void)
 {
    Size        size = 0;
    long        max_table_size;
index 13524ea488aa39b441f390587332a4fba06fc237..04a47d6e704d6ccc6420cb31f4474572e7a150ab 100644 (file)
@@ -606,6 +606,9 @@ BaseInit(void)
     */
    InitXLogInsert();
 
+   /* Initialize lock manager's local structs */
+   InitLockManagerAccess();
+
    /*
     * Initialize replication slots after pgstat. The exit hook might need to
     * drop ephemeral slots, which in turn triggers stats reporting.
index cc1f6e78c396c1a6c590d167e08a898e54bc3d9f..8b328a06d9e0f3d03e17531b583175763e1672d6 100644 (file)
@@ -544,7 +544,9 @@ typedef enum
 /*
  * function prototypes
  */
-extern void InitLocks(void);
+extern void LockManagerShmemInit(void);
+extern Size LockManagerShmemSize(void);
+extern void InitLockManagerAccess(void);
 extern LockMethod GetLocksMethodTable(const LOCK *lock);
 extern LockMethod GetLockTagsMethodTable(const LOCKTAG *locktag);
 extern uint32 LockTagHashCode(const LOCKTAG *locktag);
@@ -584,7 +586,6 @@ extern bool LockCheckConflicts(LockMethod lockMethodTable,
 extern void GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode);
 extern void GrantAwaitedLock(void);
 extern void RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode);
-extern Size LockShmemSize(void);
 extern LockData *GetLockStatusData(void);
 extern BlockedProcsData *GetBlockerStatusData(int blocked_pid);