pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: PetSerAl <petseral@gmail.com>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: Server crash when selecting from pg_cursors
Date: Sun, 06 Oct 2024 14:08:25 -0400
Message-ID: <1375463.1728238105@sss.pgh.pa.us> (raw)
In-Reply-To: <CAKygsHTBXLXjwV43kpZa+Cs+XTiaeeJiZdL4cPBm9f4MTdw7wg@mail.gmail.com>
References: <CAKygsHTBXLXjwV43kpZa+Cs+XTiaeeJiZdL4cPBm9f4MTdw7wg@mail.gmail.com>

PetSerAl <petseral@gmail.com> 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

Attachments:

harden-pg_cursor-against-null-sourceText.patch (text/x-diff, 548B) download diff --git a/src/backend/utils/mmgr/portalmem.c b/src/backend/utils/mmgr/portalmem.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] = CStringGetTextDatum(portal->name); values[1] = CStringGetTextDatum(portal->sourceText);

view thread (2+ messages)

Message-ID: <1375463.1728238105@sss.pgh.pa.us>
Permalink:  ../1375463.1728238105@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1375463.1728238105@sss.pgh.pa.us

 ·  · 

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: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, petseral@gmail.com, pgsql-bugs@lists.postgresql.org
  Subject: Re: Server crash when selecting from pg_cursors
  In-Reply-To: <1375463.1728238105@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox