Skip to content

Commit 302ce5b

Browse files
committed
Fix rare assertion failure in standby, if primary is restarted
During hot standby, ExpireAllKnownAssignedTransactionIds() and ExpireOldKnownAssignedTransactionIds() functions mark old transactions as no-longer running, but they failed to update xactCompletionCount and latestCompletedXid. AFAICS it would not lead to incorrect query results, because those functions effectively turn in-progress transactions into aborted transactions and an MVCC snapshot considers both as "not visible". But it could surprise GetSnapshotDataReuse() and trigger the "TransactionIdPrecedesOrEquals(TransactionXmin, RecentXmin))" assertion in it, if the apparent xmin in a backend would move backwards. We saw this happen when GetCatalogSnapshot() would reuse an older catalog snapshot, when GetTransactionSnapshot() had already advanced TransactionXmin. The bug goes back all the way to commit 623a9ba in v14 that introduced the snapshot reuse mechanism, but it started to happen more frequently with commit 952365c which removed a GetTransactionSnapshot() call from backend startup. That made it more likely for ExpireOldKnownAssignedTransactionIds() to be called between GetCatalogSnapshot() and the first GetTransactionSnapshot() in a backend. Andres Freund first spotted this assertion failure on buildfarm member 'skink'. Reproduction and analysis by Tomas Vondra. Backpatch-through: 14 Discussion: https://www.postgresql.org/message-id/oey246mcw43cy4qw2hqjmurbd62lfdpcuxyqiu7botx3typpax%40h7o7mfg5zmdj
1 parent 1353b11 commit 302ce5b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/backend/storage/ipc/procarray.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4496,9 +4496,23 @@ ExpireTreeKnownAssignedTransactionIds(TransactionId xid, int nsubxids,
44964496
void
44974497
ExpireAllKnownAssignedTransactionIds(void)
44984498
{
4499+
FullTransactionId latestXid;
4500+
44994501
LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
45004502
KnownAssignedXidsRemovePreceding(InvalidTransactionId);
45014503

4504+
/* Reset latestCompletedXid to nextXid - 1 */
4505+
Assert(FullTransactionIdIsValid(TransamVariables->nextXid));
4506+
latestXid = TransamVariables->nextXid;
4507+
FullTransactionIdRetreat(&latestXid);
4508+
TransamVariables->latestCompletedXid = latestXid;
4509+
4510+
/*
4511+
* Any transactions that were in-progress were effectively aborted, so
4512+
* advance xactCompletionCount.
4513+
*/
4514+
TransamVariables->xactCompletionCount++;
4515+
45024516
/*
45034517
* Reset lastOverflowedXid. Currently, lastOverflowedXid has no use after
45044518
* the call of this function. But do this for unification with what
@@ -4516,8 +4530,18 @@ ExpireAllKnownAssignedTransactionIds(void)
45164530
void
45174531
ExpireOldKnownAssignedTransactionIds(TransactionId xid)
45184532
{
4533+
TransactionId latestXid;
4534+
45194535
LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
45204536

4537+
/* As in ProcArrayEndTransaction, advance latestCompletedXid */
4538+
latestXid = xid;
4539+
TransactionIdRetreat(latestXid);
4540+
MaintainLatestCompletedXidRecovery(latestXid);
4541+
4542+
/* ... and xactCompletionCount */
4543+
TransamVariables->xactCompletionCount++;
4544+
45214545
/*
45224546
* Reset lastOverflowedXid if we know all transactions that have been
45234547
* possibly running are being gone. Not doing so could cause an incorrect

0 commit comments

Comments
 (0)