public inbox for [email protected]
help / color / mirror / Atom feedFrom: SATYANARAYANA NARLAPURAM <[email protected]>
To: Alexander Korotkov <[email protected]>
Cc: PostgreSQL Hackers <[email protected]>
Cc: PostgreSQL-development <[email protected]>
Subject: Re: Bug: WAIT FOR LSN crashes with assertion failure inside PL/pgSQL DO blocks and procedures
Date: Thu, 9 Apr 2026 00:26:54 -0700
Message-ID: <CAHg+QDevycEwWfTMJ6ADZQJR9YSMcsAf=Dg=vH2R8Efcx1NH1w@mail.gmail.com> (raw)
In-Reply-To: <CAPpHfdsZ6YrzX-5uenwvw1VXuG7mpBXSOT6JFTg6aCCC0SaNEg@mail.gmail.com>
References: <CAHg+QDcN-n3NUqgRtj=BQb9fFQmH8-DeEROCr=PDbw_BBRKOYA@mail.gmail.com>
<CAPpHfdsZ6YrzX-5uenwvw1VXuG7mpBXSOT6JFTg6aCCC0SaNEg@mail.gmail.com>
Hi Alexnader,
On Wed, Apr 8, 2026 at 11:00 PM Alexander Korotkov <[email protected]>
wrote:
> Hi, Satya!
>
> On Thu, Apr 9, 2026 at 5:03 AM SATYANARAYANA NARLAPURAM
> <[email protected]> wrote:
> > An assertion failure (server crash in assert-enabled builds) occurs when
> WAIT FOR LSN ... INTO is used inside PL/pgSQL DO blocks or within void
> procedures.
> >
> > Repro:
> >
> > -- Run this on a standby
> >
> > CREATE PROCEDURE test_wait()
> > LANGUAGE plpgsql AS $$
> > DECLARE
> > result text;
> > BEGIN
> > WAIT FOR LSN '0/1234' INTO result;
> > RAISE NOTICE '%', result;
> > END;
> > $$;
> > CALL test_wait();
> >
> >
> > The WAIT FOR itself succeeds, but the very next PL/pgSQL statement that
> requires a snapshot crashes the backend with:
> >
> > TRAP: failed Assert("portal->portalSnapshot == NULL"),
> > File: "pquery.c", Line: 1776
> >
> > Attached patches for both the test case and a potential fix. Please
> review.
>
> Thank you for reporting. But I doubt the fix is correct. Even that
> this particular might work OK, I don't think it's safe to release
> snapshots belonging to functions/procedures: it might affect them. I
> tend to think we must forbid wrapping WAIT FOR LSN with
> functions/procedures. I'll explore more on this today.
Agreed, attached a v2 patch with your suggestion on preventing it running
from procedures.
Thanks,
Satya
Attachments:
[application/octet-stream] v2-0001-waitforlsn-forbid-in-functions.patch (1.8K, 3-v2-0001-waitforlsn-forbid-in-functions.patch)
download | inline diff:
diff --git a/src/backend/commands/wait.c b/src/backend/commands/wait.c
index 85fcd463..b84fa815 100644
--- a/src/backend/commands/wait.c
+++ b/src/backend/commands/wait.c
@@ -31,7 +31,8 @@
void
-ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, DestReceiver *dest)
+ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, bool isTopLevel,
+ DestReceiver *dest)
{
XLogRecPtr lsn;
int64 timeout = 0;
@@ -135,6 +136,16 @@ ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, DestReceiver *dest)
}
}
+ /*
+ * WAIT FOR must not run inside a function or procedure.
+ * Forbid this case upfront.
+ */
+ if (!isTopLevel)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("%s cannot be executed from a function or procedure",
+ "WAIT FOR")));
+
/*
* We are going to wait for the LSN. We should first care that we don't
* hold a snapshot and correspondingly our MyProc->xmin is invalid.
diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c
index 1d34c199..73a56f1d 100644
--- a/src/backend/tcop/utility.c
+++ b/src/backend/tcop/utility.c
@@ -1062,7 +1062,8 @@ standard_ProcessUtility(PlannedStmt *pstmt,
case T_WaitStmt:
{
- ExecWaitStmt(pstate, (WaitStmt *) parsetree, dest);
+ ExecWaitStmt(pstate, (WaitStmt *) parsetree, isTopLevel,
+ dest);
}
break;
diff --git a/src/include/commands/wait.h b/src/include/commands/wait.h
index 521a3129..d7b3ee85 100644
--- a/src/include/commands/wait.h
+++ b/src/include/commands/wait.h
@@ -16,7 +16,8 @@
#include "parser/parse_node.h"
#include "tcop/dest.h"
-extern void ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, DestReceiver *dest);
+extern void ExecWaitStmt(ParseState *pstate, WaitStmt *stmt, bool isTopLevel,
+ DestReceiver *dest);
extern TupleDesc WaitStmtResultDesc(WaitStmt *stmt);
#endif /* WAIT_H */
[application/octet-stream] v2-0001-waitforlsn-tests.patch (614B, 4-v2-0001-waitforlsn-tests.patch)
download | inline diff:
diff --git a/src/test/recovery/t/049_wait_for_lsn.pl b/src/test/recovery/t/049_wait_for_lsn.pl
index bf61b8c4..ca07873e 100644
--- a/src/test/recovery/t/049_wait_for_lsn.pl
+++ b/src/test/recovery/t/049_wait_for_lsn.pl
@@ -216,7 +216,7 @@ $node_standby->psql(
"SELECT pg_wal_replay_wait_wrap('${lsn3}');",
stderr => \$stderr);
ok( $stderr =~
- /WAIT FOR must be called without an active or registered snapshot/,
+ /WAIT FOR cannot be executed from a function or procedure/,
"get an error when running within another function");
# 6. Check parameter validation error cases on standby before promotion
view thread (11+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected]
Subject: Re: Bug: WAIT FOR LSN crashes with assertion failure inside PL/pgSQL DO blocks and procedures
In-Reply-To: <CAHg+QDevycEwWfTMJ6ADZQJR9YSMcsAf=Dg=vH2R8Efcx1NH1w@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox