pgsql: Fix rare assertion failure in standby, if primary is restarted

From: Heikki Linnakangas <heikki(dot)linnakangas(at)iki(dot)fi>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix rare assertion failure in standby, if primary is restarted
Date: 2025-03-23 18:45:45
Message-ID: E1twQKO-000eUW-2t@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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 623a9ba79b in v14 that
introduced the snapshot reuse mechanism, but it started to happen more
frequently with commit 952365cded6 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

Branch
------
REL_14_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/66235baab72b22456c88a28db788048b52712100

Modified Files
--------------
src/backend/storage/ipc/procarray.c | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2025-03-23 21:16:15 pgsql: psql: Add tab completion for VACUUM and ANALYZE ... ONLY option.
Previous Message Noah Misch 2025-03-23 13:16:01 pgsql: Fix "make clean" for new TAP suite.