Skip to content

Commit 2817525

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 f044638 commit 2817525

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
@@ -4497,9 +4497,23 @@ ExpireTreeKnownAssignedTransactionIds(TransactionId xid, int nsubxids,
44974497
void
44984498
ExpireAllKnownAssignedTransactionIds(void)
44994499
{
4500+
FullTransactionId latestXid;
4501+
45004502
LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
45014503
KnownAssignedXidsRemovePreceding(InvalidTransactionId);
45024504

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

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

0 commit comments

Comments
 (0)