From 8fa0e22801e002b3f70e3912b1460951bb034f2c Mon Sep 17 00:00:00 2001 From: Alexander Korotkov Date: Thu, 17 Sep 2026 01:54:09 +0300 Subject: [PATCH v5] Say what WAIT's snapshot restriction actually is The reference page said WAIT "cannot be used in contexts where such a snapshot must remain active, including transactions running at isolation levels higher than READ COMMITTED", which reads as a blanket ban at those levels. It is not one. The rule the code enforces is that no active or registered snapshot may be held, and a transaction-snapshot-mode transaction has no snapshot until its first query: WaitStmt is exempt in PlannedStmtRequiresSnapshot(), so the command itself does not take one. WAIT therefore works before that first query, which is the order an application wants anyway, and it is what 447aae13b03 set out to allow by keeping the wait snapshot-free. State the rule in terms of the snapshot rather than the isolation level, and name the two ways to hold one: the level, and an open cursor or an exported snapshot. The error for a held snapshot attached the isolation-level explanation as DETAIL unconditionally, so a READ COMMITTED session with a cursor was told its isolation level was too high. Emit that DETAIL only when the isolation level is in fact the reason. Reported-by: Sami Imseih Reported-by: Xuneng Zhou Discussion: https://postgr.es/m/CAN12%2BYJddgAZzA36JZFX%2BYrnNR7Uubp9qWFz6ASs3v0kL30AMA%40mail.gmail.com Backpatch-through: 19 --- doc/src/sgml/ref/wait.sgml | 10 ++++++---- src/backend/commands/wait.c | 4 +++- src/test/recovery/t/049_wait_for_lsn.pl | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/doc/src/sgml/ref/wait.sgml b/doc/src/sgml/ref/wait.sgml index 9056eb6726b..0b3f3ea13d3 100644 --- a/doc/src/sgml/ref/wait.sgml +++ b/doc/src/sgml/ref/wait.sgml @@ -227,10 +227,12 @@ WAIT FOR LSN 'lsn' WAIT must be executed as a top-level command. It cannot be executed from a function, procedure, or - DO block. It also requires that no active or - registered snapshot be held, and therefore cannot be used in contexts - where such a snapshot must remain active, including transactions running - at isolation levels higher than READ COMMITTED. + DO block. It also requires that no active or + registered snapshot be held. A transaction running at an isolation + level higher than READ COMMITTED holds one from its + first query until it ends, so WAIT is accepted before + that query and rejected after it. An open cursor or an exported + snapshot has the same effect at any isolation level. diff --git a/src/backend/commands/wait.c b/src/backend/commands/wait.c index 2223e91a3e3..e1e98c8c9ea 100644 --- a/src/backend/commands/wait.c +++ b/src/backend/commands/wait.c @@ -13,6 +13,7 @@ */ #include "postgres.h" +#include "access/xact.h" #include "access/xlog.h" #include "access/xlogrecovery.h" #include "access/xlogwait.h" @@ -156,7 +157,8 @@ ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, bool isTopLevel, ereport(ERROR, errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("WAIT must be called without an active or registered snapshot"), - errdetail("WAIT cannot be executed within a transaction with an isolation level higher than READ COMMITTED.")); + IsolationUsesXactSnapshot() ? + errdetail("A transaction running at an isolation level higher than READ COMMITTED holds its snapshot from its first query until it ends.") : 0); /* * As the result we should hold no snapshot, and correspondingly our xmin diff --git a/src/test/recovery/t/049_wait_for_lsn.pl b/src/test/recovery/t/049_wait_for_lsn.pl index 3bc936e0f91..df15e5e96fd 100644 --- a/src/test/recovery/t/049_wait_for_lsn.pl +++ b/src/test/recovery/t/049_wait_for_lsn.pl @@ -282,6 +282,15 @@ $node_standby->psql( ok($stderr =~ /recovery is in progress/, "get an error when running primary_flush on the standby"); +# A transaction-snapshot-mode transaction has no snapshot until its first +# query, so WAIT FOR is allowed before that point. The target is already +# replayed here, so the command returns immediately. +$output = $node_standby->safe_psql( + 'postgres', + "BEGIN ISOLATION LEVEL REPEATABLE READ; WAIT FOR LSN '${lsn2}'; COMMIT;"); +ok($output eq "success", + "WAIT FOR is allowed before the transaction snapshot is taken"); + $node_standby->psql( 'postgres', "BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1; WAIT FOR LSN '${lsn3}';", @@ -289,6 +298,18 @@ $node_standby->psql( ok( $stderr =~ /WAIT must be called without an active or registered snapshot/, "get an error when running in a transaction with an isolation level higher than REPEATABLE READ" ); +ok( $stderr =~ /isolation level higher than READ COMMITTED holds its snapshot/, + "the isolation level is given as the reason when it is the reason"); + +# The same error at READ COMMITTED must not blame the isolation level. +$node_standby->psql( + 'postgres', + "BEGIN; DECLARE c CURSOR FOR SELECT 1; WAIT FOR LSN '${lsn3}';", + stderr => \$stderr); +ok( $stderr =~ /WAIT must be called without an active or registered snapshot/, + "get an error when a cursor holds a snapshot"); +unlike($stderr, qr/isolation level/, + "the isolation level is not blamed at READ COMMITTED"); # Test wrapping WAIT FOR into function, procedure, and anonymous DO block -- # should error -- 2.55.0