Skip to content

Commit fbce7df

Browse files
committed
Refactor lock manager initialization to make it a bit less special
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
1 parent 9f87da1 commit fbce7df

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

src/backend/storage/ipc/ipci.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ CalculateShmemSize(int *num_semaphores)
116116
size = add_size(size, dsm_estimate_size());
117117
size = add_size(size, DSMRegistryShmemSize());
118118
size = add_size(size, BufferShmemSize());
119-
size = add_size(size, LockShmemSize());
119+
size = add_size(size, LockManagerShmemSize());
120120
size = add_size(size, PredicateLockShmemSize());
121121
size = add_size(size, ProcGlobalShmemSize());
122122
size = add_size(size, XLogPrefetchShmemSize());
@@ -291,7 +291,7 @@ CreateOrAttachShmemStructs(void)
291291
/*
292292
* Set up lock manager
293293
*/
294-
InitLocks();
294+
LockManagerShmemInit();
295295

296296
/*
297297
* Set up predicate lock manager

src/backend/storage/lmgr/lock.c

+17-18
Original file line numberDiff line numberDiff line change
@@ -377,19 +377,17 @@ static void GetSingleProcBlockerStatusData(PGPROC *blocked_proc,
377377

378378

379379
/*
380-
* InitLocks -- Initialize the lock manager's data structures.
380+
* Initialize the lock manager's shmem data structures.
381381
*
382-
* This is called from CreateSharedMemoryAndSemaphores(), which see for
383-
* more comments. In the normal postmaster case, the shared hash tables
384-
* are created here, as well as a locallock hash table that will remain
385-
* unused and empty in the postmaster itself. Backends inherit the pointers
386-
* to the shared tables via fork(), and also inherit an image of the locallock
387-
* hash table, which they proceed to use. In the EXEC_BACKEND case, each
388-
* backend re-executes this code to obtain pointers to the already existing
389-
* shared hash tables and to create its locallock hash table.
382+
* This is called from CreateSharedMemoryAndSemaphores(), which see for more
383+
* comments. In the normal postmaster case, the shared hash tables are
384+
* created here, and backends inherit pointers to them via fork(). In the
385+
* EXEC_BACKEND case, each backend re-executes this code to obtain pointers to
386+
* the already existing shared hash tables. In either case, each backend must
387+
* also call InitLockManagerAccess() to create the locallock hash table.
390388
*/
391389
void
392-
InitLocks(void)
390+
LockManagerShmemInit(void)
393391
{
394392
HASHCTL info;
395393
long init_table_size,
@@ -444,18 +442,19 @@ InitLocks(void)
444442
sizeof(FastPathStrongRelationLockData), &found);
445443
if (!found)
446444
SpinLockInit(&FastPathStrongRelationLocks->mutex);
445+
}
447446

447+
/*
448+
* Initialize the lock manager's backend-private data structures.
449+
*/
450+
void
451+
InitLockManagerAccess(void)
452+
{
448453
/*
449454
* Allocate non-shared hash table for LOCALLOCK structs. This stores lock
450455
* counts and resource owner information.
451-
*
452-
* The non-shared table could already exist in this process (this occurs
453-
* when the postmaster is recreating shared memory after a backend crash).
454-
* If so, delete and recreate it. (We could simply leave it, since it
455-
* ought to be empty in the postmaster, but for safety let's zap it.)
456456
*/
457-
if (LockMethodLocalHash)
458-
hash_destroy(LockMethodLocalHash);
457+
HASHCTL info;
459458

460459
info.keysize = sizeof(LOCALLOCKTAG);
461460
info.entrysize = sizeof(LOCALLOCK);
@@ -3571,7 +3570,7 @@ PostPrepare_Locks(TransactionId xid)
35713570
* Estimate shared-memory space used for lock tables
35723571
*/
35733572
Size
3574-
LockShmemSize(void)
3573+
LockManagerShmemSize(void)
35753574
{
35763575
Size size = 0;
35773576
long max_table_size;

src/backend/utils/init/postinit.c

+3
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,9 @@ BaseInit(void)
606606
*/
607607
InitXLogInsert();
608608

609+
/* Initialize lock manager's local structs */
610+
InitLockManagerAccess();
611+
609612
/*
610613
* Initialize replication slots after pgstat. The exit hook might need to
611614
* drop ephemeral slots, which in turn triggers stats reporting.

src/include/storage/lock.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,9 @@ typedef enum
544544
/*
545545
* function prototypes
546546
*/
547-
extern void InitLocks(void);
547+
extern void LockManagerShmemInit(void);
548+
extern Size LockManagerShmemSize(void);
549+
extern void InitLockManagerAccess(void);
548550
extern LockMethod GetLocksMethodTable(const LOCK *lock);
549551
extern LockMethod GetLockTagsMethodTable(const LOCKTAG *locktag);
550552
extern uint32 LockTagHashCode(const LOCKTAG *locktag);
@@ -584,7 +586,6 @@ extern bool LockCheckConflicts(LockMethod lockMethodTable,
584586
extern void GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode);
585587
extern void GrantAwaitedLock(void);
586588
extern void RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode);
587-
extern Size LockShmemSize(void);
588589
extern LockData *GetLockStatusData(void);
589590
extern BlockedProcsData *GetBlockerStatusData(int blocked_pid);
590591

0 commit comments

Comments
 (0)