Skip to content

Commit 8ad35c7

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 f0f255a commit 8ad35c7

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
@@ -804,6 +804,7 @@ OldSerXidInit(void)
804804
oldSerXidControl = (OldSerXidControl)
805805
ShmemInitStruct("OldSerXidControlData", sizeof(OldSerXidControlData), &found);
806806

807+
Assert(found == IsUnderPostmaster);
807808
if (!found)
808809
{
809810
/*
@@ -1099,6 +1100,10 @@ InitPredicateLocks(void)
10991100
Size requestSize;
11001101
bool found;
11011102

1103+
#ifndef EXEC_BACKEND
1104+
Assert(!IsUnderPostmaster);
1105+
#endif
1106+
11021107
/*
11031108
* Compute size of predicate lock target hashtable. Note these
11041109
* calculations must agree with PredicateLockShmemSize!
@@ -1122,16 +1127,22 @@ InitPredicateLocks(void)
11221127
&info,
11231128
hash_flags);
11241129

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

11361147
/*
11371148
* Allocate hash table for PREDICATELOCK structs. This stores per
@@ -1144,6 +1155,9 @@ InitPredicateLocks(void)
11441155
info.num_partitions = NUM_PREDICATELOCK_PARTITIONS;
11451156
hash_flags = (HASH_ELEM | HASH_FUNCTION | HASH_PARTITION | HASH_FIXED_SIZE);
11461157

1158+
/* Assume an average of 2 xacts per target */
1159+
max_table_size *= 2;
1160+
11471161
PredicateLockHash = ShmemInitHash("PREDICATELOCK hash",
11481162
max_table_size,
11491163
max_table_size,
@@ -1169,6 +1183,7 @@ InitPredicateLocks(void)
11691183
PredXact = ShmemInitStruct("PredXactList",
11701184
PredXactListDataSize,
11711185
&found);
1186+
Assert(found == IsUnderPostmaster);
11721187
if (!found)
11731188
{
11741189
int i;
@@ -1248,6 +1263,7 @@ InitPredicateLocks(void)
12481263
RWConflictPool = ShmemInitStruct("RWConflictPool",
12491264
RWConflictPoolHeaderDataSize,
12501265
&found);
1266+
Assert(found == IsUnderPostmaster);
12511267
if (!found)
12521268
{
12531269
int i;
@@ -1279,6 +1295,7 @@ InitPredicateLocks(void)
12791295
ShmemInitStruct("FinishedSerializableTransactions",
12801296
sizeof(SHM_QUEUE),
12811297
&found);
1298+
Assert(found == IsUnderPostmaster);
12821299
if (!found)
12831300
SHMQueueInit(FinishedSerializableTransactions);
12841301

@@ -1287,10 +1304,6 @@ InitPredicateLocks(void)
12871304
* transactions.
12881305
*/
12891306
OldSerXidInit();
1290-
1291-
/* Pre-calculate the hash and partition lock of the scratch entry */
1292-
ScratchTargetTagHash = PredicateLockTargetTagHashCode(&ScratchTargetTag);
1293-
ScratchPartitionLock = PredicateLockHashPartitionLock(ScratchTargetTagHash);
12941307
}
12951308

12961309
/*

0 commit comments

Comments
 (0)