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 1wAjRB-000GoW-0Y for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Apr 2026 07:04:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wAjR9-0048CM-1I for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Apr 2026 07:04:24 +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 1wAjR9-0048CD-0C for pgsql-hackers@lists.postgresql.org; Thu, 09 Apr 2026 07:04:24 +0000 Received: from mail-ed1-x52f.google.com ([2a00:1450:4864:20::52f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wAjR7-000000007iq-3Hz1 for pgsql-hackers@lists.postgresql.org; Thu, 09 Apr 2026 07:04:23 +0000 Received: by mail-ed1-x52f.google.com with SMTP id 4fb4d7f45d1cf-66d24c6963bso923498a12.1 for ; Thu, 09 Apr 2026 00:04:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1775718261; cv=none; d=google.com; s=arc-20240605; b=lPfeCobxILOhMC5BBuM9i5o05eBGQ4Rm41ZgfjauWRfmNZtpdXXldTGI3DUZ9MPOJy s3CzLQRCuavMcphUeLpAXar2DbBNZgZIhqLC/zyPDDDF3+mGhPM+aa3qaOuFc4RgUtRR LD3hgOcykY4e1ajodu9/73CXanJKu+a7r1yweE3sxsuBUMarkFYGbwL5HpaX2ml+Zf2D 6uyRybgwbASjc3gb/ingmtunBSQ6yj3Ry77zJktHekQwYnOp8GD8I/Oa6EleJEDSQtG8 c/AjL7WM9GNbsS/iUfEBYtRx5X9IcbTcfH0UPmhsCopNLFfuOxsVBXjZ/f5R0PvS7yAz 7+Sg== 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=zwtNETBQ3JQ8UG/nkDk0kVfwdCZBBV8TawRRaHN13dQ=; fh=fB8Pt05on2KM2ehHJH+923wJ7rutXYM5ZKkKtCpEhsk=; b=QPGFH7Jwc24KW2mNnlHltYz7UcgaKgOaIk7nFxtVz3PbonMT7RUeQsAcRYLAzukZ4r 2UmXiAxWCVmDunW8CM613FM1aBKmZGgt6J/Mo3DwFfelgKVrjbpnoR34xQy2r6DKLFAv aiUFnXOMrEsTcEmpIgFVYt6KYyN4T1tRD8U4MzqSODa/g7sfzWVgx5EUiDo9tBBf6HDz TPVYk6h/WrbxVKyf2NqGc1IPQY4Zk7ZUL0Z07Wq0sVSJDC7ObQgpZ17foHMc2KJjE0E8 Fa42Zqm/dq5+P0fig+eoK9jguQGAu7fwOohbFUXN5uyejdxLtEessWAIDZQtq+s8Uhti 0GPw==; darn=lists.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=1775718261; x=1776323061; darn=lists.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=zwtNETBQ3JQ8UG/nkDk0kVfwdCZBBV8TawRRaHN13dQ=; b=eBWXL9uDTwLfWThO06IY0DeZb9Y0o0lEkEeei/92Z58p8Y7jdWwZUm1kFR4SWLTjd5 CASOCyP1szvZ/qZvlIqPQWfiAMTinD3PmgqEQLrbeBAs4dTggbmYjfjSWFjJxK2Tdd5w wPLWgEriefJC5pyp6guWZPLLOYr5o8RsNtnqhDzeja7p/gkeMsnFJsN4Gyf7/mV/t1U8 GFDTxGPiXmx/mqyGdW4XqwKn65oTjtAwzCmcVHElQxn5b3k/ABVB5xgzuWqUVmZuQOMq BVGyvqQAkKxvcrnsBPblIQR/GsEXr/OaIqQsYNNq+5Es3IecH16zhbLsw5qYTFi+tYBG B5ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775718261; x=1776323061; 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=zwtNETBQ3JQ8UG/nkDk0kVfwdCZBBV8TawRRaHN13dQ=; b=Q4yTGmEgrlBxCkzErZKpR1GycQXu9ES2naC+FpRyRGM7VCvHbhPOrzFZOd8nVtTHOo w2GR2p5pLsvwsJ+m2dJ2To/vbKnan923lk8DfFV0N1YAREagyGmBlqr5cdNpgvQ+CqX6 RWihpFPP5YvqlZVZJdHiG3LlmGY8jpoHMW3wTU3JckqFfK39Bi+fwqzL1qRcaEr77IL5 LLuubSHIqt2reC8FFPsR2vmaPAxA0tOQH4YO2p9dkrbgSZZk0JF5a1c/rrdKcAk74W7B GHNzEJTFvtM4FqtwfGVCkaGVDUdlSnUq9a8B2SwyJbyVc0k2Jvggn9u4yoG1TZHKDCEj rKUA== X-Forwarded-Encrypted: i=1; AJvYcCVQxNqUWeG+mzJJ8LOQr5DJLyXZVUfTIQptdCTCMsgfiWn8/Hd6CXWBtB30zz/37j/lOc9jBQHUw49l1HU+@lists.postgresql.org X-Gm-Message-State: AOJu0YyZq4A/iMrwginL1J/dRiSsswdHAMkqBGlX+9loBRLYOIiC39qk O1bCHL7cjuEdXXA6G2WGMxAUftRZTwxe7QS80FEq6VHIUfJr48fiQLa/OuIQv8fIonSjbWKn2Wa 0QidTjQcQE8O81ebIW729nWSKImKCw5HlVO7X4cQ= X-Gm-Gg: AeBDieu+VUB+wFjKTBr+2BGL2e6v6XvS86sJOp+G7dRSPm6O4hijCvUCIV3a5kUW9hS CSJrLf1QzFcPTTTJEPS2sMYMtNjlpbn+rHB45VtiRbZ94pMTvlhQf5FO2/D5Yy9zXz9AMrvtuCA 3ltG02CGP5SFf0z/CCqOv3ldvB3BpbfQ/PDZgJySttw/xtZ77f+GA9RyPCFqEMxL8vD480hmIdj AlNw8FJkFtjUsWPC6nGL+NsCkJoiASPDvFA/VK1QsAmz0Y4jmaH3R6zaaZEMlY+aVyQrj6SJOQc o0VdnWLXfwRQxPDA3KdQAZUrSOnCT9lo6geHBrmnYx38UYj8tJCzY86aWO0lmDdfzvmwJUjyqOz kPMwa4xYX X-Received: by 2002:a17:906:ee83:b0:b9c:1089:47b8 with SMTP id a640c23a62f3a-b9d476dadbemr125180266b.47.1775718260329; Thu, 09 Apr 2026 00:04:20 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Xuneng Zhou Date: Thu, 9 Apr 2026 15:04:08 +0800 X-Gm-Features: AQROBzDgZGnwBXYJK7tMWGMFKt2eOdOUE2hn1qhR1x3DLhXvhorPWScSo6AYoI8 Message-ID: Subject: Re: Bug: WAIT FOR LSN crashes with assertion failure inside PL/pgSQL DO blocks and procedures To: Alexander Korotkov 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 Hi Satya, Alexander, On Thu, Apr 9, 2026 at 2:38=E2=80=AFPM Xuneng Zhou w= rote: > > 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 w= hen 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 th= at 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 r= eview. > > > > 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 the 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(); --=20 Best, Xuneng