<refsect1>
<title>Notes</title>
+ <para>
+ <command>WAIT FOR</command> must be executed as a top-level command.
+ It cannot be executed from a function, procedure, or
+ <command>DO</command> 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 <literal>READ COMMITTED</literal>.
+ </para>
<para>
<command>WAIT FOR</command> waits until the specified
void
-ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, DestReceiver *dest)
+ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, bool isTopLevel,
+ DestReceiver *dest)
{
XLogRecPtr lsn;
int64 timeout = 0;
bool no_throw_specified = false;
bool mode_specified = false;
+ /*
+ * WAIT FOR must not be run as a non-top-level statement (e.g., inside a
+ * function, procedure, or DO block). Forbid this case upfront.
+ */
+ if (!isTopLevel)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("%s can only be executed as a top-level statement",
+ "WAIT FOR"),
+ errdetail("WAIT FOR cannot be used within a function, procedure, or DO block.")));
+
/* Parse and validate the mandatory LSN */
lsn = DatumGetLSN(DirectFunctionCall1(pg_lsn_in,
CStringGetDatum(stmt->lsn_literal)));
* implying a kind of self-deadlock. This is the reason why WAIT FOR is a
* command, not a procedure or function.
*
- * At first, we should check there is no active snapshot. According to
- * PlannedStmtRequiresSnapshot(), even in an atomic context, CallStmt is
- * processed with a snapshot. Thankfully, we can pop this snapshot,
- * because PortalRunUtility() can tolerate this.
+ * Non-top-level contexts are rejected above, but be defensive and pop any
+ * active snapshot if one is present. PortalRunUtility() can tolerate
+ * utility commands that remove the active snapshot.
*/
if (ActiveSnapshotSet())
PopActiveSnapshot();
ereport(ERROR,
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("WAIT FOR must be called without an active or registered snapshot"),
- errdetail("WAIT FOR cannot be executed from a function or procedure, nor within a transaction with an isolation level higher than READ COMMITTED."));
+ errdetail("WAIT FOR cannot be executed within a transaction with an isolation level higher than READ COMMITTED."));
/*
* As the result we should hold no snapshot, and correspondingly our xmin
'postgres',
"SELECT pg_wal_replay_wait_wrap('${lsn3}');",
stderr => \$stderr);
-ok( $stderr =~
- /WAIT FOR must be called without an active or registered snapshot/,
- "get an error when running within another function");
+ok($stderr =~ /WAIT FOR can only be executed as a top-level statement/,
+ "get an error when running within a function");
+
+$node_primary->safe_psql(
+ 'postgres', qq[
+CREATE PROCEDURE pg_wal_replay_wait_proc(target_lsn pg_lsn) AS \$\$
+ BEGIN
+ EXECUTE format('WAIT FOR LSN %L;', target_lsn);
+ END
+\$\$
+LANGUAGE plpgsql;
+]);
+
+$node_primary->wait_for_catchup($node_standby);
+$node_standby->psql(
+ 'postgres',
+ "CALL pg_wal_replay_wait_proc('${lsn3}');",
+ stderr => \$stderr);
+ok($stderr =~ /WAIT FOR can only be executed as a top-level statement/,
+ "get an error when running within a procedure");
+
+$node_standby->psql(
+ 'postgres',
+ "DO \$\$ BEGIN EXECUTE format('WAIT FOR LSN %L;', '${lsn3}'); END \$\$;",
+ stderr => \$stderr);
+ok($stderr =~ /WAIT FOR can only be executed as a top-level statement/,
+ "get an error when running within a DO block");
# 6. Check parameter validation error cases on standby before promotion
my $test_lsn =