From: Dirkjan Bussink <d.bussink@gmail.com>
To: pgsql-hackers@lists.postgresql.org
Subject: Server crash when describing a FETCH statement after its cursor is closed
Date: Fri, 25 Sep 2026 13:29:40 +0200
Message-ID: <7DE248EB-7509-4F8A-9F37-4694A40BBAB7@gmail.com> (raw)
Hi,
The following extended-protocol sequence crashes a backend (SIGSEGV
in production builds, assertion failure in assert-enabled ones), and
the postmaster terminates all other sessions for crash recovery:
Query BEGIN
Query DECLARE c CURSOR FOR SELECT id FROM t ORDER BY id
Parse "" "FETCH 5 FROM c"
Bind portal "p", statement ""
Sync
Query CLOSE c
Describe portal "p"
Sync -> connection drops; server log shows
"terminated by signal 11", then
"terminating any other active
server processes"
Describing the prepared statement instead of the portal crashes the
same way. Reproduced on REL_18_STABLE (18.6) and current master,
the code is identical on all supported branches.
At Bind time, PortalStart() copies the cursor's tuple descriptor into
the portal via UtilityTupleDescriptor(), which deliberately tolerates
a missing cursor. But Describe re-resolves the target list through
FetchStatementTargetList() (tcop/pquery.c), whose FetchStmt branch does:
subportal = GetPortalByName(fstmt->portalname);
Assert(PortalIsValid(subportal));
return FetchPortalTargetList(subportal);
With the cursor closed, GetPortalByName() returns NULL and the Assert
is compiled out in production builds, so FetchPortalTargetList()
dereferences NULL->strategy. The statement variant reaches the same
branch via CachedPlanGetTargetList().
The attached patch replaces the Assert with a runtime check returning
NIL, mirroring the guard in UtilityTupleDescriptor(). A regression
test is included.
--
Cheers,
Dirkjan Bussink
Attachments:v1-0001-Fix-crash-when-describing-a-FETCH-statement-wh.patch (application/octet-stream, 4.2K) download
From 508fa7c2808178caf694cc61282bf4fbe2e0509f Mon Sep 17 00:00:00 2001
From: Dirkjan Bussink <d.bussink@gmail.com>
Date: Fri, 25 Sep 2026 13:19:44 +0200
Subject: [PATCH] Fix crash when describing a FETCH statement whose cursor is
gone
FetchStatementTargetList() dereferenced the portal named by a
FetchStmt without checking that it still exists. With the extended
query protocol, a client can Parse "FETCH n FROM c" while cursor c
is open (which caches a result descriptor), then CLOSE c, and then
Describe the prepared statement or a portal bound to it. Describe
re-resolves the target list through the named cursor, and in
non-assert builds the missing check becomes a NULL pointer
dereference, crashing the backend and forcing a cluster-wide
restart and crash recovery.
Fix by returning NIL when the named portal is invalid, mirroring
the existing guard in UtilityTupleDescriptor(): it is not this
function's business to raise an error, and SendRowDescriptionMessage()
then builds the RowDescription from the descriptor saved at Parse
or Bind time.
---
src/backend/tcop/pquery.c | 3 +-
.../modules/libpq_pipeline/libpq_pipeline.c | 52 +++++++++++++++++++
2 files changed, 54 insertions(+), 1 deletion(-)
diff --git a/src/backend/tcop/pquery.c b/src/backend/tcop/pquery.cindex 9f3ea1c9a75..385772ec374 100644--- a/src/backend/tcop/pquery.c+++ b/src/backend/tcop/pquery.c@@ -389,7 +389,8 @@ FetchStatementTargetList(Node *stmt)
Assert(!fstmt->ismove);
subportal = GetPortalByName(fstmt->portalname);
- Assert(PortalIsValid(subportal));+ if (!PortalIsValid(subportal))+ return NIL; /* not our business to raise error */
return FetchPortalTargetList(subportal);
}
if (IsA(stmt, ExecuteStmt))
diff --git a/src/test/modules/libpq_pipeline/libpq_pipeline.c b/src/test/modules/libpq_pipeline/libpq_pipeline.cindex ad007038dc9..168f9a7d3ab 100644--- a/src/test/modules/libpq_pipeline/libpq_pipeline.c+++ b/src/test/modules/libpq_pipeline/libpq_pipeline.c@@ -456,6 +456,55 @@ test_cancel(PGconn *conn)
fprintf(stderr, "ok\n");
}
+/*+ * Test Describe of a prepared FETCH statement after the cursor it+ * references has been closed.+ */+static void+test_describe_fetch(PGconn *conn)+{+ PGresult *res;++ res = PQexec(conn, "BEGIN");+ if (PQresultStatus(res) != PGRES_COMMAND_OK)+ pg_fatal("BEGIN failed: %s", PQerrorMessage(conn));+ PQclear(res);++ res = PQexec(conn, "DECLARE fetch_cursor CURSOR FOR SELECT 1");+ if (PQresultStatus(res) != PGRES_COMMAND_OK)+ pg_fatal("DECLARE CURSOR failed: %s", PQerrorMessage(conn));+ PQclear(res);++ /* prepare while the cursor exists, so that it caches a result desc */+ res = PQprepare(conn, "fetch_one", "FETCH 1 FROM fetch_cursor", 0, NULL);+ if (PQresultStatus(res) != PGRES_COMMAND_OK)+ pg_fatal("PQprepare failed: %s", PQerrorMessage(conn));+ PQclear(res);++ res = PQexec(conn, "CLOSE fetch_cursor");+ if (PQresultStatus(res) != PGRES_COMMAND_OK)+ pg_fatal("CLOSE failed: %s", PQerrorMessage(conn));+ PQclear(res);++ /*+ * Describe must still work, using the cached row descriptor. (This used+ * to crash the server.)+ */+ res = PQdescribePrepared(conn, "fetch_one");+ if (PQresultStatus(res) != PGRES_COMMAND_OK)+ pg_fatal("PQdescribePrepared failed: %s", PQerrorMessage(conn));+ if (PQnfields(res) != 1 || PQftype(res, 0) != INT4OID)+ pg_fatal("expected one int4 column, got %d fields", PQnfields(res));+ PQclear(res);++ res = PQexec(conn, "ROLLBACK");+ if (PQresultStatus(res) != PGRES_COMMAND_OK)+ pg_fatal("ROLLBACK failed: %s", PQerrorMessage(conn));+ PQclear(res);++ fprintf(stderr, "ok\n");+}+
static void
test_disallowed_in_pipeline(PGconn *conn)
{
@@ -2117,6 +2166,7 @@ static void
print_test_list(void)
{
printf("cancel\n");
+ printf("describe_fetch\n");
printf("disallowed_in_pipeline\n");
printf("multi_pipelines\n");
printf("nosync\n");
@@ -2223,6 +2273,8 @@ main(int argc, char **argv)
if (strcmp(testname, "cancel") == 0)
test_cancel(conn);
+ else if (strcmp(testname, "describe_fetch") == 0)+ test_describe_fetch(conn);
else if (strcmp(testname, "disallowed_in_pipeline") == 0)
test_disallowed_in_pipeline(conn);
else if (strcmp(testname, "multi_pipelines") == 0)
--
2.55.0
=
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: d.bussink@gmail.com
Subject: Re: Server crash when describing a FETCH statement after its cursor is closed
In-Reply-To: <7DE248EB-7509-4F8A-9F37-4694A40BBAB7@gmail.com>
References: <7DE248EB-7509-4F8A-9F37-4694A40BBAB7@gmail.com>
* 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