@@ -125,6 +125,9 @@ static intLWLockTranchesAllocated = 0;
125125 */
126126LWLockPadded * MainLWLockArray = NULL ;
127127static LWLockTranche MainLWLockTranche ;
128+ static LWLockTranche BufMappingLWLockTranche ;
129+ static LWLockTranche LockManagerLWLockTranche ;
130+ static LWLockTranche PredicateLockManagerLWLockTranche ;
128131
129132/*
130133 * We use this structure to keep track of locked LWLocks for release
@@ -159,6 +162,9 @@ NamedLWLockTranche *NamedLWLockTrancheArray = NULL;
159162
160163static bool lock_named_request_allowed = true;
161164
165+ static void InitializeLWLocks (void );
166+ static void RegisterLWLockTranches (void );
167+
162168#ifdef LWLOCK_STATS
163169typedef struct lwlock_stats_key
164170{
@@ -395,14 +401,12 @@ LWLockShmemSize(void)
395401}
396402
397403/*
398- * Allocate shmem space for the main LWLock array andnamed tranches and
399- * initialize it. We also register themain and named tranche here.
404+ * Allocate shmem space for the main LWLock array andall tranches and
405+ * initialize it. We also registerall theLWLock tranches here.
400406 */
401407void
402408CreateLWLocks (void )
403409{
404- int i ;
405-
406410StaticAssertExpr (LW_VAL_EXCLUSIVE > (uint32 )MAX_BACKENDS ,
407411"MAX_BACKENDS too big for lwlock.c" );
408412
@@ -412,14 +416,9 @@ CreateLWLocks(void)
412416
413417if (!IsUnderPostmaster )
414418{
415- int numLocks = NUM_FIXED_LWLOCKS ;
416- int numNamedLocks = NumLWLocksByNamedTranches ();
417419Size spaceLocks = LWLockShmemSize ();
418- LWLockPadded * lock ;
419420int * LWLockCounter ;
420421char * ptr ;
421- int id ;
422- int j ;
423422
424423/* Allocate space */
425424ptr = (char * )ShmemAlloc (spaceLocks );
@@ -432,55 +431,99 @@ CreateLWLocks(void)
432431
433432MainLWLockArray = (LWLockPadded * )ptr ;
434433
435- /* Initialize all fixed LWLocks in main array */
436- for (id = 0 ,lock = MainLWLockArray ;id < numLocks ;id ++ ,lock ++ )
437- LWLockInitialize (& lock -> lock ,LWTRANCHE_MAIN );
438-
439434/*
440435 * Initialize the dynamic-allocation counter for tranches, which is
441436 * stored just before the first LWLock.
442437 */
443438LWLockCounter = (int * ) ((char * )MainLWLockArray - sizeof (int ));
444439* LWLockCounter = LWTRANCHE_FIRST_USER_DEFINED ;
445440
446- /* Initialize named tranches. */
447- if (NamedLWLockTrancheRequests > 0 )
448- {
449- char * trancheNames ;
441+ /* Initialize all LWLocks */
442+ InitializeLWLocks ();
443+ }
444+
445+ /* Register all LWLock tranches */
446+ RegisterLWLockTranches ();
447+ }
448+
449+ /*
450+ * Initialize LWLocks that are fixed and those belonging to named tranches.
451+ */
452+ static void
453+ InitializeLWLocks (void )
454+ {
455+ int numNamedLocks = NumLWLocksByNamedTranches ();
456+ int id ;
457+ int i ;
458+ int j ;
459+ LWLockPadded * lock ;
460+
461+ /* Initialize all individual LWLocks in main array */
462+ for (id = 0 ,lock = MainLWLockArray ;id < NUM_INDIVIDUAL_LWLOCKS ;id ++ ,lock ++ )
463+ LWLockInitialize (& lock -> lock ,LWTRANCHE_MAIN );
464+
465+ /* Initialize buffer mapping LWLocks in main array */
466+ lock = MainLWLockArray + NUM_INDIVIDUAL_LWLOCKS ;
467+ for (id = 0 ;id < NUM_BUFFER_PARTITIONS ;id ++ ,lock ++ )
468+ LWLockInitialize (& lock -> lock ,LWTRANCHE_BUFFER_MAPPING );
469+
470+ /* Initialize lmgrs' LWLocks in main array */
471+ lock = MainLWLockArray + NUM_INDIVIDUAL_LWLOCKS + NUM_BUFFER_PARTITIONS ;
472+ for (id = 0 ;id < NUM_LOCK_PARTITIONS ;id ++ ,lock ++ )
473+ LWLockInitialize (& lock -> lock ,LWTRANCHE_LOCK_MANAGER );
474+
475+ /* Initialize predicate lmgrs' LWLocks in main array */
476+ lock = MainLWLockArray + NUM_INDIVIDUAL_LWLOCKS +
477+ NUM_BUFFER_PARTITIONS + NUM_LOCK_PARTITIONS ;
478+ for (id = 0 ;id < NUM_PREDICATELOCK_PARTITIONS ;id ++ ,lock ++ )
479+ LWLockInitialize (& lock -> lock ,LWTRANCHE_PREDICATE_LOCK_MANAGER );
480+
481+ /* Initialize named tranches. */
482+ if (NamedLWLockTrancheRequests > 0 )
483+ {
484+ char * trancheNames ;
450485
451- NamedLWLockTrancheArray = (NamedLWLockTranche * )
452- & MainLWLockArray [numLocks + numNamedLocks ];
486+ NamedLWLockTrancheArray = (NamedLWLockTranche * )
487+ & MainLWLockArray [NUM_FIXED_LWLOCKS + numNamedLocks ];
453488
454- trancheNames = (char * )NamedLWLockTrancheArray +
455- (NamedLWLockTrancheRequests * sizeof (NamedLWLockTranche ));
456- lock = & MainLWLockArray [numLocks ];
489+ trancheNames = (char * )NamedLWLockTrancheArray +
490+ (NamedLWLockTrancheRequests * sizeof (NamedLWLockTranche ));
491+ lock = & MainLWLockArray [NUM_FIXED_LWLOCKS ];
457492
458- for (i = 0 ;i < NamedLWLockTrancheRequests ;i ++ )
459- {
460- NamedLWLockTrancheRequest * request ;
461- NamedLWLockTranche * tranche ;
462- char * name ;
463-
464- request = & NamedLWLockTrancheRequestArray [i ];
465- tranche = & NamedLWLockTrancheArray [i ];
466-
467- name = trancheNames ;
468- trancheNames += strlen (request -> tranche_name )+ 1 ;
469- strcpy (name ,request -> tranche_name );
470- tranche -> lwLockTranche .name = name ;
471- tranche -> trancheId = LWLockNewTrancheId ();
472- tranche -> lwLockTranche .array_base = lock ;
473- tranche -> lwLockTranche .array_stride = sizeof (LWLockPadded );
474-
475- for (j = 0 ;j < request -> num_lwlocks ;j ++ ,lock ++ )
476- LWLockInitialize (& lock -> lock ,tranche -> trancheId );
477- }
493+ for (i = 0 ;i < NamedLWLockTrancheRequests ;i ++ )
494+ {
495+ NamedLWLockTrancheRequest * request ;
496+ NamedLWLockTranche * tranche ;
497+ char * name ;
498+
499+ request = & NamedLWLockTrancheRequestArray [i ];
500+ tranche = & NamedLWLockTrancheArray [i ];
501+
502+ name = trancheNames ;
503+ trancheNames += strlen (request -> tranche_name )+ 1 ;
504+ strcpy (name ,request -> tranche_name );
505+ tranche -> lwLockTranche .name = name ;
506+ tranche -> trancheId = LWLockNewTrancheId ();
507+ tranche -> lwLockTranche .array_base = lock ;
508+ tranche -> lwLockTranche .array_stride = sizeof (LWLockPadded );
509+
510+ for (j = 0 ;j < request -> num_lwlocks ;j ++ ,lock ++ )
511+ LWLockInitialize (& lock -> lock ,tranche -> trancheId );
478512}
479513}
514+ }
515+
516+ /*
517+ * Register named tranches and tranches for fixed LWLocks.
518+ */
519+ static void
520+ RegisterLWLockTranches (void )
521+ {
522+ int i ;
480523
481524if (LWLockTrancheArray == NULL )
482525{
483- LWLockTranchesAllocated = 16 ;
526+ LWLockTranchesAllocated = 32 ;
484527LWLockTrancheArray = (LWLockTranche * * )
485528MemoryContextAlloc (TopMemoryContext ,
486529LWLockTranchesAllocated * sizeof (LWLockTranche * ));
@@ -492,6 +535,23 @@ CreateLWLocks(void)
492535MainLWLockTranche .array_stride = sizeof (LWLockPadded );
493536LWLockRegisterTranche (LWTRANCHE_MAIN ,& MainLWLockTranche );
494537
538+ BufMappingLWLockTranche .name = "buffer_mapping" ;
539+ BufMappingLWLockTranche .array_base = MainLWLockArray + NUM_INDIVIDUAL_LWLOCKS ;
540+ BufMappingLWLockTranche .array_stride = sizeof (LWLockPadded );
541+ LWLockRegisterTranche (LWTRANCHE_BUFFER_MAPPING ,& BufMappingLWLockTranche );
542+
543+ LockManagerLWLockTranche .name = "lock_manager" ;
544+ LockManagerLWLockTranche .array_base = MainLWLockArray + NUM_INDIVIDUAL_LWLOCKS +
545+ NUM_BUFFER_PARTITIONS ;
546+ LockManagerLWLockTranche .array_stride = sizeof (LWLockPadded );
547+ LWLockRegisterTranche (LWTRANCHE_LOCK_MANAGER ,& LockManagerLWLockTranche );
548+
549+ PredicateLockManagerLWLockTranche .name = "predicate_lock_manager" ;
550+ PredicateLockManagerLWLockTranche .array_base = MainLWLockArray + NUM_INDIVIDUAL_LWLOCKS +
551+ NUM_BUFFER_PARTITIONS + NUM_LOCK_PARTITIONS ;
552+ PredicateLockManagerLWLockTranche .array_stride = sizeof (LWLockPadded );
553+ LWLockRegisterTranche (LWTRANCHE_PREDICATE_LOCK_MANAGER ,& PredicateLockManagerLWLockTranche );
554+
495555/* Register named tranches. */
496556for (i = 0 ;i < NamedLWLockTrancheRequests ;i ++ )
497557LWLockRegisterTranche (NamedLWLockTrancheArray [i ].trancheId ,