Skip to content

Commit 9d091e0

Browse files
Fix bug in early startup of Hot Standby with subtransactions.
When HS startup is deferred because of overflowed subtransactions, ensure that we re-initialize KnownAssignedXids for when both existing and incoming snapshots have non-zero qualifying xids. Fixes bug #6661 reported by Valentine Gogichashvili. Analysis and fix by Andres Freund
1 parent da6bb58 commit 9d091e0

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/backend/storage/ipc/procarray.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ static int KnownAssignedXidsGetAndSetXmin(TransactionId *xarray,
158158
TransactionId xmax);
159159
static TransactionId KnownAssignedXidsGetOldestXmin(void);
160160
static void KnownAssignedXidsDisplay(int trace_level);
161+
static void KnownAssignedXidsReset(void);
161162

162163
/*
163164
* Report shared-memory space needed by CreateSharedProcArray.
@@ -493,6 +494,11 @@ ProcArrayApplyRecoveryInfo(RunningTransactions running)
493494
*/
494495
if (!running->subxid_overflow || running->xcnt == 0)
495496
{
497+
/*
498+
* If we have already collected known assigned xids, we need to
499+
* throw them away before we apply the recovery snapshot.
500+
*/
501+
KnownAssignedXidsReset();
496502
standbyState = STANDBY_INITIALIZED;
497503
}
498504
else
@@ -536,7 +542,6 @@ ProcArrayApplyRecoveryInfo(RunningTransactions running)
536542
* xids to subtrans. If RunningXacts is overflowed then we don't have
537543
* enough information to correctly update subtrans anyway.
538544
*/
539-
Assert(procArray->numKnownAssignedXids == 0);
540545

541546
/*
542547
* Allocate a temporary array to avoid modifying the array passed as
@@ -566,6 +571,12 @@ ProcArrayApplyRecoveryInfo(RunningTransactions running)
566571

567572
if (nxids > 0)
568573
{
574+
if (procArray->numKnownAssignedXids != 0)
575+
{
576+
LWLockRelease(ProcArrayLock);
577+
elog(ERROR, "KnownAssignedXids is not empty");
578+
}
579+
569580
/*
570581
* Sort the array so that we can add them safely into
571582
* KnownAssignedXids.
@@ -3133,3 +3144,22 @@ KnownAssignedXidsDisplay(int trace_level)
31333144

31343145
pfree(buf.data);
31353146
}
3147+
3148+
/*
3149+
* KnownAssignedXidsReset
3150+
* Resets KnownAssignedXids to be empty
3151+
*/
3152+
static void
3153+
KnownAssignedXidsReset(void)
3154+
{
3155+
/* use volatile pointer to prevent code rearrangement */
3156+
volatile ProcArrayStruct *pArray = procArray;
3157+
3158+
LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3159+
3160+
pArray->numKnownAssignedXids = 0;
3161+
pArray->tailKnownAssignedXids = 0;
3162+
pArray->headKnownAssignedXids = 0;
3163+
3164+
LWLockRelease(ProcArrayLock);
3165+
}

0 commit comments

Comments
 (0)