Skip to content

Commit 4ecee11

Browse files
committed
Fix race condition in predicate-lock init code in EXEC_BACKEND builds.
Trading a little too heavily on letting the code path be the same whether we were creating shared data structures or only attaching to them, InitPredicateLocks() inserted the "scratch" PredicateLockTargetHash entry unconditionally. This is just wrong if we're in a postmaster child, which would only reach this code in EXEC_BACKEND builds. Most of the time, the hash_search(HASH_ENTER) call would simply report that the entry already existed, causing no visible effect since the code did not bother to check for that possibility. However, if this happened while some other backend had transiently removed the "scratch" entry, then that other backend's eventual RestoreScratchTarget would suffer an assert failure; this appears to be the explanation for a recent failure on buildfarm member culicidae. In non-assert builds, there would be no visible consequences there either. But nonetheless this is a pretty bad bug for EXEC_BACKEND builds, for two reasons: 1. Each new backend would perform the hash_search(HASH_ENTER) call without holding any lock that would prevent concurrent access to the PredicateLockTargetHash hash table. This creates a low but certainly nonzero risk of corruption of that hash table. 2. In the event that the race condition occurred, by reinserting the scratch entry too soon, we were defeating the entire purpose of the scratch entry, namely to guarantee that transaction commit could move hash table entries around with no risk of out-of-memory failure. The odds of an actual OOM failure are quite low, but not zero, and if it did happen it would again result in corruption of the hash table. The user-visible symptoms of such corruption are a little hard to predict, but would presumably amount to misbehavior of SERIALIZABLE transactions that'd require a crash or postmaster restart to fix. To fix, just skip the hash insertion if IsUnderPostmaster. I also inserted a bunch of assertions that the expected things happen depending on whether IsUnderPostmaster is true. That might be overkill, since most comparable code in other functions isn't quite that paranoid, but once burnt twice shy. In passing, also move a couple of lines to places where they seemed to make more sense. Diagnosis of problem by Thomas Munro, patch by me. Back-patch to all supported branches. Discussion: https://postgr.es/m/10593.1500670709@sss.pgh.pa.us
1 parent b6d6400 commit 4ecee11

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/backend/storage/lmgr/predicate.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ OldSerXidInit(void)
801801
oldSerXidControl = (OldSerXidControl)
802802
ShmemInitStruct("OldSerXidControlData", sizeof(OldSerXidControlData), &found);
803803

804+
Assert(found == IsUnderPostmaster);
804805
if (!found)
805806
{
806807
/*
@@ -1096,6 +1097,10 @@ InitPredicateLocks(void)
10961097
Size requestSize;
10971098
bool found;
10981099

1100+
#ifndef EXEC_BACKEND
1101+
Assert(!IsUnderPostmaster);
1102+
#endif
1103+
10991104
/*
11001105
* Compute size of predicate lock target hashtable. Note these
11011106
* calculations must agree with PredicateLockShmemSize!
@@ -1119,16 +1124,22 @@ InitPredicateLocks(void)
11191124
&info,
11201125
hash_flags);
11211126

1122-
/* Assume an average of 2 xacts per target */
1123-
max_table_size *= 2;
1124-
11251127
/*
11261128
* Reserve a dummy entry in the hash table; we use it to make sure there's
11271129
* always one entry available when we need to split or combine a page,
11281130
* because running out of space there could mean aborting a
11291131
* non-serializable transaction.
11301132
*/
1131-
hash_search(PredicateLockTargetHash, &ScratchTargetTag, HASH_ENTER, NULL);
1133+
if (!IsUnderPostmaster)
1134+
{
1135+
(void) hash_search(PredicateLockTargetHash, &ScratchTargetTag,
1136+
HASH_ENTER, &found);
1137+
Assert(!found);
1138+
}
1139+
1140+
/* Pre-calculate the hash and partition lock of the scratch entry */
1141+
ScratchTargetTagHash = PredicateLockTargetTagHashCode(&ScratchTargetTag);
1142+
ScratchPartitionLock = PredicateLockHashPartitionLock(ScratchTargetTagHash);
11321143

11331144
/*
11341145
* Allocate hash table for PREDICATELOCK structs. This stores per
@@ -1141,6 +1152,9 @@ InitPredicateLocks(void)
11411152
info.num_partitions = NUM_PREDICATELOCK_PARTITIONS;
11421153
hash_flags = (HASH_ELEM | HASH_FUNCTION | HASH_PARTITION | HASH_FIXED_SIZE);
11431154

1155+
/* Assume an average of 2 xacts per target */
1156+
max_table_size *= 2;
1157+
11441158
PredicateLockHash = ShmemInitHash("PREDICATELOCK hash",
11451159
max_table_size,
11461160
max_table_size,
@@ -1166,6 +1180,7 @@ InitPredicateLocks(void)
11661180
PredXact = ShmemInitStruct("PredXactList",
11671181
PredXactListDataSize,
11681182
&found);
1183+
Assert(found == IsUnderPostmaster);
11691184
if (!found)
11701185
{
11711186
int i;
@@ -1245,6 +1260,7 @@ InitPredicateLocks(void)
12451260
RWConflictPool = ShmemInitStruct("RWConflictPool",
12461261
RWConflictPoolHeaderDataSize,
12471262
&found);
1263+
Assert(found == IsUnderPostmaster);
12481264
if (!found)
12491265
{
12501266
int i;
@@ -1276,6 +1292,7 @@ InitPredicateLocks(void)
12761292
ShmemInitStruct("FinishedSerializableTransactions",
12771293
sizeof(SHM_QUEUE),
12781294
&found);
1295+
Assert(found == IsUnderPostmaster);
12791296
if (!found)
12801297
SHMQueueInit(FinishedSerializableTransactions);
12811298

@@ -1284,10 +1301,6 @@ InitPredicateLocks(void)
12841301
* transactions.
12851302
*/
12861303
OldSerXidInit();
1287-
1288-
/* Pre-calculate the hash and partition lock of the scratch entry */
1289-
ScratchTargetTagHash = PredicateLockTargetTagHashCode(&ScratchTargetTag);
1290-
ScratchPartitionLock = PredicateLockHashPartitionLock(ScratchTargetTagHash);
12911304
}
12921305

12931306
/*

0 commit comments

Comments
 (0)