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.94.2) (envelope-from ) id 1sxVgJ-00AY5A-Ql for pgsql-bugs@arkaria.postgresql.org; Sun, 06 Oct 2024 18:08:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sxVgI-00Hak5-2g for pgsql-bugs@arkaria.postgresql.org; Sun, 06 Oct 2024 18:08:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sxVgH-00Hajx-O1 for pgsql-bugs@lists.postgresql.org; Sun, 06 Oct 2024 18:08:33 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sxVgB-002tiC-JA for pgsql-bugs@lists.postgresql.org; Sun, 06 Oct 2024 18:08:33 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 496I8PUP1375464; Sun, 6 Oct 2024 14:08:25 -0400 From: Tom Lane To: PetSerAl cc: pgsql-bugs@lists.postgresql.org Subject: Re: Server crash when selecting from pg_cursors In-reply-to: References: Comments: In-reply-to PetSerAl message dated "Sun, 06 Oct 2024 19:15:10 +0300" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <1375361.1728238046.0@sss.pgh.pa.us> Date: Sun, 06 Oct 2024 14:08:25 -0400 Message-ID: <1375463.1728238105@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1375361.1728238046.1@sss.pgh.pa.us> PetSerAl writes: > postgres=# CREATE TABLE t (a integer, b integer); > CREATE TABLE > postgres=# CREATE FUNCTION f() RETURNS integer > postgres-# STABLE STRICT LANGUAGE plpgsql > postgres-# AS $$ > postgres$# BEGIN > postgres$# PERFORM FROM pg_cursors; > postgres$# RETURN null; > postgres$# END > postgres$# $$; > CREATE FUNCTION > postgres=# DO $$ > postgres$# DECLARE > postgres$# a integer; > postgres$# BEGIN > postgres$# FOR a IN SELECT t.a FROM t WHERE t.b = f() LOOP > postgres$# END LOOP; > postgres$# END > postgres$# $$; > server closed the connection unexpectedly Huh, nice one. It's not new in v17 though --- it seems quite ancient. The proximate cause is that pg_cursor() is assuming portal->sourceText can't be NULL: values[1] = CStringGetTextDatum(portal->sourceText); which is not unreasonable considering that portal.h quoth const char *sourceText; /* text of query (as of 8.4, never NULL) */ But there's a problem: we may be examining a portal that has been added to the portal hashtable, but for which PortalDefineQuery hasn't been called yet. There are a few ways we might try to deal with this, but I think the most reasonable one is to make pg_cursor() ignore portals that haven't been defined yet, as attached. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="harden-pg_cursor-against-null-sourceText.patch"; charset="us-ascii" Content-ID: <1375361.1728238046.2@sss.pgh.pa.us> Content-Description: harden-pg_cursor-against-null-sourceText.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/mmgr/portalmem.c b/src/backend/utils/mmgr/p= ortalmem.c index 4a24613537..93137820ac 100644 --- a/src/backend/utils/mmgr/portalmem.c +++ b/src/backend/utils/mmgr/portalmem.c @@ -1150,6 +1150,9 @@ pg_cursor(PG_FUNCTION_ARGS) /* report only "visible" entries */ if (!portal->visible) continue; + /* also ignore it if PortalDefineQuery hasn't been called yet */ + if (!portal->sourceText) + continue; = values[0] =3D CStringGetTextDatum(portal->name); values[1] =3D CStringGetTextDatum(portal->sourceText); ------- =_aaaaaaaaaa0--