Re: Server crash when selecting from pg_cursors
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: PetSerAl <petseral@gmail.com>
Cc: pgsql-bugs@lists.postgresql.org
Date: 2024-10-06T18:08:25Z
Lists: pgsql-bugs
Attachments
- harden-pg_cursor-against-null-sourceText.patch (text/x-diff) patch
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
Commits
-
Ignore not-yet-defined Portals in pg_cursors view.
- f8d9a9f21e2f 18.0 landed
- aef75219cc22 15.9 landed
- 5de77b609cbe 16.5 landed
- 5c17f5a6392f 12.21 landed
- 4a17acd0dbb6 13.17 landed
- 3daeb539a66a 17.1 landed
- 3922c9e9f3de 14.14 landed