Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wAnqz-000MII-1t for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Apr 2026 11:47:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wAnqx-005cLi-1a for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Apr 2026 11:47:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wAnqx-005cLZ-0I for pgsql-hackers@lists.postgresql.org; Thu, 09 Apr 2026 11:47:20 +0000 Received: from mail-ot1-x332.google.com ([2607:f8b0:4864:20::332]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wAnqw-000000009wV-03CX for pgsql-hackers@postgresql.org; Thu, 09 Apr 2026 11:47:19 +0000 Received: by mail-ot1-x332.google.com with SMTP id 46e09a7af769-7d7e9b97a73so409012a34.0 for ; Thu, 09 Apr 2026 04:47:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1775735238; cv=none; d=google.com; s=arc-20240605; b=O4iJ5XplNEWu4qYvIeRRzeVT6ZQBk4qCsJqjMw1MTqx+HFDMBJxFGYtvwSmSFXO3jk Fd1meb31eh1s5jjNG9GWL0AkYUMk6rWVZI58rEAj4LFdAKAyR7Z5JUhiZytXLFrFlGxg VNmhjPK3YgukcfJWhMq9chyaAudv5qs7aQ5CvlrN6c7DsvpT4kj6iZfElZmW3wDLG2og IlumJH6WsZqvx6G2lJUg+lrLwNI55hSvTevUH3lNTOQP9kL580SDVet2HbyyA1xtUVU7 xGEAUuzmvKjAGLznjp8VMZEWBbFAPKOa6JRnPSeS7oxfP85sum8LmEGszQ54JCnuxzuY +UKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=x7T+zg683BvcZoxgFw0abrzpJyAcIZpFEPVOmnHh+H0=; fh=e9t3PJea5mea7ixYLqEWGRm46nNYBZE9GHoYl7w/1wk=; b=FPYoel/qnW4gid0NctGf5zmXp0Tu/zyMqECrHa5szrmOsdyDXgeORwrAykoiESuZEl XqVCmY0kB8sDYaWFWpn664bWluPXH8MMHh4JKhVq8CQuYM8FyBAtdInrbxHe/1kYp2NO 60qGI3zKOBrrxZFsfOU70UNuZROupQ1GCD7++C8CLFoJmAruX4NOGo5lmJCevZqW3oiW OOkAJ8JWyFLIC0otMJc62GvnIeU1yXTsmZ8D6Nv9nqW3cFTgE1jZBYgZ/8bCpNKxEYHQ Jw5j9NtqYrH061ClUPkMhZ1PFMGMJRVHDg/71GXN6SmP2XQuK4qbAXosjL/3QFumHEWY x/aQ==; darn=postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775735238; x=1776340038; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=x7T+zg683BvcZoxgFw0abrzpJyAcIZpFEPVOmnHh+H0=; b=qgac/S+l7F0wZxLjO4Ggcrp/JcujGQ3ttKRQuLavZe6NcGHKx4uJs6hDSsQuzfwZDZ 1nBRYFPLLunDCv/MoZuYpg6tP0wlhXgbO4+k8bYnYCUtqWwTk6T5w/FaPWn6TZkBYs8x RZ5mBmMKOCQVj4kBP9NF7GfwdW7tp1esWWEgRaOKU0uDriu1fwJMTv9UFmYaTaRcvNo1 n45IogY9enb3A6Ghbhixw1w6gOfnAYPSGbWUsiwW+5TTukDjCFPt8rjSFvC9iupaaWGg n6FU2kc0Jdb+RDSw5jpJsBFoh6iGwEvdVMQQADg4t/aTsegIfcfWHFqiv9mnF6HjZjBo Eq1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775735238; x=1776340038; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=x7T+zg683BvcZoxgFw0abrzpJyAcIZpFEPVOmnHh+H0=; b=qq7nAbX864Ny6KvkaWUByLhIQJQA/L83wIPQ1ht/ao8XuSj/IT2WpJS71mUCxa6V66 MMcTAcWnO+WOp50gpc3UXa5TtnsVudo5uYiVKx6K6fYFIfNJpY7zgWvyl2UhU+pZXkd9 2K4X/KG/lGZgIPnBzB3vnm4T7/VfYvmn8D4u+Qtf/fIhSaDoty/WQoQjOQjF32HeX2Vs eW8AEJ8Nfk8dpJZS+yuucAk2i8NvuE2hmEifnGRoRUvVLlyX6OdzN8+0q1r6/pyIXvWu HNlKbRMgBA2+hNvewStxti8BWKOqLoxcwcN/21tYROZqo3FyGTqkOF9lV853BjwVmhwG ad7g== X-Forwarded-Encrypted: i=1; AJvYcCVvKYmIJN/VXrOKP0DpbRps2ypZ2dwN9qZKlxnUaqkQAs6YDZFe9cZF85rY+tNbz2TH0PYOl5BBybE3sxCf@postgresql.org X-Gm-Message-State: AOJu0YypLxJtdlepzXtV+V/Lt8NQYYyIJM1aoGHr/qD3feDl/okAWZMo 5VAFP4brxKqbMFJFPuuMo+0mCj9HRA/DLePuFaj2ybSRH6aX09ICgpsxRS1lcmDK43sJGItSR2I sxBjjgUxQPL9Z+chdUA6dpyyCi9Hj+W4= X-Gm-Gg: AeBDievO3Pxm4KOQ0PiGNtYDXeYXWTVRCEYTgpG+sOpo2ixIPWaLkO4wPPXNLeLu2El EK79TbFMq8R/NVutJgL5UIKTkIKGg/ZSp3xOBhKuyFh9OeS3kjcONJjK1emPmSNPZ1rt9u7S1oC JkOgWWpxrr057Ps+vmLOLsm1a27sUXiIn9+GLdqtYZflZC+RPZkaW72DX3JgDAyTBs7OPw3YJf+ YIb+TPaNYMNoAJlxKCtOk+8mtvhF4gCnkofMudvIbLpQnpXytTbc6q9YJLtgb1RbfWzwzhns6dI Pb+Jf94JHjsJqaNbmR2UO7EHDIfF4KacybwlgZT1858v+pcTzrcaRug961ddnwUNzqohU2MEGy2 6kBGXBX1lFTkppJQfM/Sdt5tuKkYsTdokYxUjlaI7iDGkn/Mq8Q== X-Received: by 2002:a05:6820:658a:b0:687:6c5f:1331 with SMTP id 006d021491bc7-68a6ae7fbbamr1019771eaf.34.1775735237770; Thu, 09 Apr 2026 04:47:17 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Alexander Korotkov Date: Thu, 9 Apr 2026 14:47:05 +0300 X-Gm-Features: AQROBzD0RjhH-IdOtSRIlSv9CMi19SWGjLb82j80sTh1kKOeSO3xZr9VAAvbeG8 Message-ID: Subject: Re: Bug: WAIT FOR LSN crashes with assertion failure inside PL/pgSQL DO blocks and procedures To: Xuneng Zhou Cc: SATYANARAYANA NARLAPURAM , PostgreSQL Hackers , PostgreSQL-development Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Apr 9, 2026 at 10:04=E2=80=AFAM Xuneng Zhou = wrote: > On Thu, Apr 9, 2026 at 2:38=E2=80=AFPM Xuneng Zhou = wrote: > > > > On Thu, Apr 9, 2026 at 2:00=E2=80=AFPM Alexander Korotkov wrote: > > > > > > Hi, Satya! > > > > > > On Thu, Apr 9, 2026 at 5:03=E2=80=AFAM SATYANARAYANA NARLAPURAM > > > 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 voi= d 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 =3D=3D 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. > > > > > Opus, sorry for clicking send incidentally before typing anything... > > I had looked at these patches before and didn=E2=80=99t see anything > particularly wrong except: > 1. patch 1 unconditionally nulled ActivePortal->portalSnapshot > whenever it was non-NULL after the pop; > 2. patch 2 used RAISE NOTICE after WAIT FOR, which seems not excercise > the bug straightforwardly. > > I didn't realized the safety implications of releasing a procedure/DO > snapshot during PL execution. I=E2=80=99ve noticed several warnings in th= e > tree advising against this. > > /* > * Ensure there's an active snapshot whilst we execute whatever's > * involved here. Note that this is *not* sufficient to make the > * world safe for TOAST pointers to be included in the returned data: > * the referenced data could have gone away while we didn't hold a > * snapshot. Hence, it's incumbent on PLs that can do COMMIT/ROLLBACK > * to not return TOAST pointers, unless those pointers were fetched > * after the last COMMIT/ROLLBACK in the procedure. > * > * XXX that is a really nasty, hard-to-test requirement. Is there a > * way to remove it? > */ > EnsurePortalSnapshotExists(); Regarding functions, function may be part of bigger query running with particular snapshot. Note that single query is always executed within the single snapshot (barring EvalPlanQual()) even in read committed mode. Releasing a snapshot in the middle of the query could cause, if even we somehow re-acquire a new snapshot, could cause rest of query to be executed inconsistently with its beginning. It is probably different for procedures, which don't have to stick to a single snapshot. But I can still imagine WAIT FOR LSN to be inside a loop over some query results or something similar. We are now past FF for PG19. I suggest we should now forbid WAIT FOR LSN both in functions and procedures. For PG20 we can reconsider some cases when running WAIT FOR LSN inside the stored procedure is safe. ------ Regards, Alexander Korotkov Supabase