Re: Assertion failure in GetSnapshotDataReuse() during promotion

From: Hannu Krosing <hannuk(at)google(dot)com>
To: Fujii Masao <masao(dot)fujii(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Assertion failure in GetSnapshotDataReuse() during promotion
Date: 2026-12-04 20:23:28
Message-ID: CAMT0RQTTkHMv1RVGfbeGKvp5csvQVPaPac+CRPmC5v9C-+XyFA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Dec 2, 2026 at 1:23 PM Fujii Masao <masao(dot)fujii(at)gmail(dot)com> wrote:
> Attached are patches to fix this issue and add a regression test.
>
> The 0001 patch updates StartupXLOG() so that it increments
> TransamVariables->xactCompletionCount while holding ProcArrayLock in
> exclusive mode when:
>
> 1. latestCompletedXid is initialized at the end of recovery
> 2. SharedRecoveryState is updated to RECOVERY_STATE_DONE

Thanks for the report and patches! I had just run into the same
assertion failure in recovery/061_recovery_target_prefetch on macOS in
cfbot, and I had reported this as well.

Your patches are definitely better and cover some cases I had missed.

Updating xactCompletionCount in StartupXLOG() looks much cleaner than
adding a recovery-state check to GetSnapshotDataReuse() — it keeps
zero extra overhead on the GetSnapshotDataReuse() hot path and also
closes the earlier window when latestCompletedXid is initialized to
nextXid - 1 while RecoveryInProgress() is still true.

A few review comments:

1. v1-0001 (and v16/v15 backpatch):

Both hunks look solid to me. The new lock ordering
(ControlFileLock -> ProcArrayLock -> XLogCtl->info_lck) doesn't
conflict with any existing ProcArrayLock caller, and ProcArrayLock is
released before UpdateControlFile().

One minor optional cleanup: the comment in GetSnapshotData() around
src/backend/storage/ipc/procarray.c:2338 says:

/*
* It is possible for recovery to end before we finish taking the
* snapshot, and for newly assigned transaction ids to be added to the
* ProcArray. ...
*/

With 0001 holding ProcArrayLock in exclusive mode across the
SharedRecoveryState = RECOVERY_STATE_DONE transition, recovery can no
longer finish while GetSnapshotData() holds ProcArrayLock, so that
comment may be worth updating.

2. v1-0002 (timing sensitivity in 062_promotion_snapshot.pl):

> Unfortunately, the test is somewhat timing-sensitive, so you may need to
> run it several times to reproduce the failure.

I think the timing sensitivity comes from these lines:

my $writer = $primary->background_psql('postgres');
$writer->query_safe('BEGIN; INSERT INTO t VALUES (1)');
$primary->safe_psql('postgres', 'SELECT pg_log_standby_snapshot()');
$primary->wait_for_catchup($standby);

$writer's INSERT is uncommitted, and pg_log_standby_snapshot() calls
LogCurrentRunningXacts() -> XLogSetAsyncXactLSN() without calling
XLogFlush(), leaving the flush to WALWriter. Meanwhile,
wait_for_catchup($standby) defaults its target LSN to
pg_current_wal_lsn() (the flush LSN, not insert LSN). If WALWriter
hasn't flushed the INSERT / XLOG_RUNNING_XACTS records yet,
wait_for_catchup() returns early before the standby replays $writer's
XID, so KnownAssignedXids is empty and the assertion doesn't fire.

Passing the LSN returned by pg_log_standby_snapshot() explicitly should
make it deterministic:

my $lsn = $primary->safe_psql('postgres', 'SELECT
pg_log_standby_snapshot()');
$primary->wait_for_catchup($standby, 'replay', $lsn);

Two small nits on 062_promotion_snapshot.pl:
- Checking the result of SELECT NOT pg_is_in_recovery() (e.g.,
is($res, 't', ...)) would also verify that recovery had already ended
when $reader ran.
- Calling injection_points_detach() for both injection points before
$standby->stop would match the other injection-point tests.

Regards,
Hannu

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Kacper Kuras 2026-12-04 20:39:06 Re: Proposal: SELECT * EXCLUDE (...) command
Previous Message Jan Nidzwetzki 2026-12-04 20:18:53 Re: Open pg_rewind issues