Re: Bug: WAIT FOR LSN crashes with assertion failure inside PL/pgSQL DO blocks and procedures

From: SATYANARAYANA NARLAPURAM <satyanarlapuram(at)gmail(dot)com>
To: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug: WAIT FOR LSN crashes with assertion failure inside PL/pgSQL DO blocks and procedures
Date: 2026-04-09 07:26:54
Message-ID: CAHg+QDevycEwWfTMJ6ADZQJR9YSMcsAf=Dg=vH2R8Efcx1NH1w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Alexnader,

On Wed, Apr 8, 2026 at 11:00 PM Alexander Korotkov <aekorotkov(at)gmail(dot)com>
wrote:

> Hi, Satya!
>
> On Thu, Apr 9, 2026 at 5:03 AM SATYANARAYANA NARLAPURAM
> <satyanarlapuram(at)gmail(dot)com> 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

Attachment Content-Type Size
v2-0001-waitforlsn-forbid-in-functions.patch application/octet-stream 1.8 KB
v2-0001-waitforlsn-tests.patch application/octet-stream 614 bytes

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Xuneng Zhou 2026-04-09 07:33:08 Re: Optimize SnapBuild by maintaining committed.xip in sorted order
Previous Message Xuneng Zhou 2026-04-09 07:04:08 Re: Bug: WAIT FOR LSN crashes with assertion failure inside PL/pgSQL DO blocks and procedures