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.98.2) (envelope-from ) id 1xA47q-00000002QqM-3uwT for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Sep 2026 11:29:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xA47p-00000000Tvj-0I1o for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Sep 2026 11:29:57 +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.98.2) (envelope-from ) id 1xA47o-00000000Tvb-2i4E for pgsql-hackers@lists.postgresql.org; Fri, 25 Sep 2026 11:29:56 +0000 Received: from mail-ej2-x10.google.com ([2a00:1450:4864:34::10]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1xA47l-00000001Dzi-3i9x for pgsql-hackers@lists.postgresql.org; Fri, 25 Sep 2026 11:29:55 +0000 Received: by mail-ej2-x10.google.com with SMTP id a640c23a62f3a-c254f70553dso107630666b.0 for ; Fri, 25 Sep 2026 04:29:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790335792; x=1790940592; darn=lists.postgresql.org; h=to:date:message-id:subject:mime-version:content-type:from:from:to :cc:subject:date:message-id:reply-to:content-type; bh=koqJ0SflvwaNFx0HjLcabJ0f1Az1Pxy1fclZ9pf7New=; b=Z+ZsqJ33tEVJ0N+IoMA+78o7iBU9pXdAyHEKU0f7pGM0b8TWOqQpzjcwtf10savlrm l0dff3L7z6F78QVIMdLiv20cD7RLCvV9SYDKCDekQfFcOhQ4SA3exFbopoG7iDGWikrN 1l2/TPwFQ6N2laGyLhe5EUzsQHUzkToB+1tgh9QNibvdyRyeg/VTUvEVcPX096FanbKd PFjoLt1E1xSwAaLEyMXoojTohDWfgCY7vKyz6wzOrYwrM+UpSTsXL6QDetrQKRM2QxKr t4ZGBUU10UJCYpF4qO36NxRdK27OR60r2FJ2URSRxHkVWNdz52QHgkvNUsWN+sDQLexd fkHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790335792; x=1790940592; h=to:date:message-id:subject:mime-version:content-type:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=koqJ0SflvwaNFx0HjLcabJ0f1Az1Pxy1fclZ9pf7New=; b=GVqCxbTAcvHwSLanGH92fKUXJcOBxzsAlbRCsXk5mRqLj4S8bH8q9OsdMaOAzBmyih GELcP/BsXLBD9CMqSXkQwtIDemgJiJh4tsSMhqoamG6bKt7XGSRcdeS7F45oTWSwSK+y uSTFgZidK+7cRXTg1Z+FIGmV0SHYt1XPSxjvGb0PWTSxVQfLBIw3CcdVoEA1PGAgNgFP k6FeKvwSqCV/tpG1ipskw91dtMruWkS+ScAYozqJXNdUg2yZRwEpOhnB1t58op8HdT3h 7HlJThgVT94mtTUlzmdZ9gcHtTXtdzGEwHB95qCJAqe89iZNcdqCQi1g2Hu4d1wMHlan tVnw== X-Gm-Message-State: AFuF++k4C72wZCi+dmgLZCVmvSBi707BTiDQO+56GdTrvq+VnzojtNCT a59Mcx97YK5K4XMSY+pG78tnXF99/cmkpnpecYwlGSg4F5yZ4NDVz90DiuyuXw== X-Gm-Gg: AYBFou2K+DMYtgxUumNEPBDjOMQwl+LN4T1bwU54El7nfyY6hvTukE0uhMYyK0l1CRN NKdFFjwW1QtwW16+ESqKvZ7gbnH+30avRLF3UFUkjzvPiDBf69YW51onjdNU2X7YPEEumeppdt+ 0Cr1DfLYhgy2XtTtt4Wx5N6vfLX3BEGQ1kDupWSUOth7bxyaR1/p4skIEaZ2lfxT/bz0cV0GlIi jwKTGVFnsBx+4DqdMoyGA9esdE5qbQlWPPKtiVDcpAenlCJ56dna0ui0dBJ3NAp3/MUz0jyZ5LJ 57u5KmX3iLLzX26bF9hc9+w0qf/T27umY0kx736XvRk2FHLm5WPAFGCyJzHZ15+TXx0a+zLSQSB qt3OZAADytVYwLsG4lcx4zQ5ShaNwNg0SaiCPSAWlVUMF33ovPnWKymmu17j324T98YB9SXEW1S mTd5BI/ZarVPxGcEJR+s0PC4bpS0DoKKc6SzYwYAk0MFr5nLOqASIwiLCwfESlskBNtjYW4i+oA PEJomQ9GGOaiLKZRBFVI4gK1Wz5/E4I7Vy80OKiWdv0JQF2VF3XkLHmrWQnimI6zHz0NSy6V9xp jgG1LgefwDpgzw7fHQ== X-Received: by 2002:a17:907:a312:b0:c29:d0a8:79dd with SMTP id a640c23a62f3a-c2ac24eae39mr454266866b.34.1790335791919; Fri, 25 Sep 2026 04:29:51 -0700 (PDT) Received: from smtpclient.apple (2a02-a445-9da0-0-8947-8e1-35fc-291f.fixed6.kpn.net. [2a02:a445:9da0:0:8947:8e1:35fc:291f]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c2ae7356d1csm103419966b.18.2026.09.25.04.29.51 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 Sep 2026 04:29:51 -0700 (PDT) From: Dirkjan Bussink Content-Type: multipart/mixed; boundary="Apple-Mail=_EB51FDE3-D74E-4239-A7FE-A345006F402F" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3901.100.1.1.11\)) Subject: Server crash when describing a FETCH statement after its cursor is closed Message-Id: <7DE248EB-7509-4F8A-9F37-4694A40BBAB7@gmail.com> Date: Fri, 25 Sep 2026 13:29:40 +0200 To: pgsql-hackers@lists.postgresql.org X-Mailer: Apple Mail (2.3901.100.1.1.11) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_EB51FDE3-D74E-4239-A7FE-A345006F402F Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii 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 --Apple-Mail=_EB51FDE3-D74E-4239-A7FE-A345006F402F Content-Disposition: attachment; filename=v1-0001-Fix-crash-when-describing-a-FETCH-statement-wh.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v1-0001-Fix-crash-when-describing-a-FETCH-statement-wh.patch" Content-Transfer-Encoding: quoted-printable =46rom=20508fa7c2808178caf694cc61282bf4fbe2e0509f=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20Dirkjan=20Bussink=20=0A= Date:=20Fri,=2025=20Sep=202026=2013:19:44=20+0200=0ASubject:=20[PATCH]=20= Fix=20crash=20when=20describing=20a=20FETCH=20statement=20whose=20cursor=20= is=0A=20gone=0A=0AFetchStatementTargetList()=20dereferenced=20the=20= portal=20named=20by=20a=0AFetchStmt=20without=20checking=20that=20it=20= still=20exists.=20=20With=20the=20extended=0Aquery=20protocol,=20a=20= client=20can=20Parse=20"FETCH=20n=20FROM=20c"=20while=20cursor=20c=0Ais=20= open=20(which=20caches=20a=20result=20descriptor),=20then=20CLOSE=20c,=20= and=20then=0ADescribe=20the=20prepared=20statement=20or=20a=20portal=20= bound=20to=20it.=20=20Describe=0Are-resolves=20the=20target=20list=20= through=20the=20named=20cursor,=20and=20in=0Anon-assert=20builds=20the=20= missing=20check=20becomes=20a=20NULL=20pointer=0Adereference,=20crashing=20= the=20backend=20and=20forcing=20a=20cluster-wide=0Arestart=20and=20crash=20= recovery.=0A=0AFix=20by=20returning=20NIL=20when=20the=20named=20portal=20= is=20invalid,=20mirroring=0Athe=20existing=20guard=20in=20= UtilityTupleDescriptor():=20it=20is=20not=20this=0Afunction's=20business=20= to=20raise=20an=20error,=20and=20SendRowDescriptionMessage()=0Athen=20= builds=20the=20RowDescription=20from=20the=20descriptor=20saved=20at=20= Parse=0Aor=20Bind=20time.=0A---=0A=20src/backend/tcop/pquery.c=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20|=20=203=20+-=0A=20= .../modules/libpq_pipeline/libpq_pipeline.c=20=20=20|=2052=20= +++++++++++++++++++=0A=202=20files=20changed,=2054=20insertions(+),=201=20= deletion(-)=0A=0Adiff=20--git=20a/src/backend/tcop/pquery.c=20= b/src/backend/tcop/pquery.c=0Aindex=209f3ea1c9a75..385772ec374=20100644=0A= ---=20a/src/backend/tcop/pquery.c=0A+++=20b/src/backend/tcop/pquery.c=0A= @@=20-389,7=20+389,8=20@@=20FetchStatementTargetList(Node=20*stmt)=0A=20=0A= =20=09=09Assert(!fstmt->ismove);=0A=20=09=09subportal=20=3D=20= GetPortalByName(fstmt->portalname);=0A-=09=09= Assert(PortalIsValid(subportal));=0A+=09=09if=20= (!PortalIsValid(subportal))=0A+=09=09=09return=20NIL;=09=09=09/*=20not=20= our=20business=20to=20raise=20error=20*/=0A=20=09=09return=20= FetchPortalTargetList(subportal);=0A=20=09}=0A=20=09if=20(IsA(stmt,=20= ExecuteStmt))=0Adiff=20--git=20= a/src/test/modules/libpq_pipeline/libpq_pipeline.c=20= b/src/test/modules/libpq_pipeline/libpq_pipeline.c=0Aindex=20= ad007038dc9..168f9a7d3ab=20100644=0A---=20= a/src/test/modules/libpq_pipeline/libpq_pipeline.c=0A+++=20= b/src/test/modules/libpq_pipeline/libpq_pipeline.c=0A@@=20-456,6=20= +456,55=20@@=20test_cancel(PGconn=20*conn)=0A=20=09fprintf(stderr,=20= "ok\n");=0A=20}=0A=20=0A+/*=0A+=20*=20Test=20Describe=20of=20a=20= prepared=20FETCH=20statement=20after=20the=20cursor=20it=0A+=20*=20= references=20has=20been=20closed.=0A+=20*/=0A+static=20void=0A= +test_describe_fetch(PGconn=20*conn)=0A+{=0A+=09PGresult=20=20=20*res;=0A= +=0A+=09res=20=3D=20PQexec(conn,=20"BEGIN");=0A+=09if=20= (PQresultStatus(res)=20!=3D=20PGRES_COMMAND_OK)=0A+=09=09pg_fatal("BEGIN=20= failed:=20%s",=20PQerrorMessage(conn));=0A+=09PQclear(res);=0A+=0A+=09= res=20=3D=20PQexec(conn,=20"DECLARE=20fetch_cursor=20CURSOR=20FOR=20= SELECT=201");=0A+=09if=20(PQresultStatus(res)=20!=3D=20PGRES_COMMAND_OK)=0A= +=09=09pg_fatal("DECLARE=20CURSOR=20failed:=20%s",=20= PQerrorMessage(conn));=0A+=09PQclear(res);=0A+=0A+=09/*=20prepare=20= while=20the=20cursor=20exists,=20so=20that=20it=20caches=20a=20result=20= desc=20*/=0A+=09res=20=3D=20PQprepare(conn,=20"fetch_one",=20"FETCH=201=20= FROM=20fetch_cursor",=200,=20NULL);=0A+=09if=20(PQresultStatus(res)=20!=3D= =20PGRES_COMMAND_OK)=0A+=09=09pg_fatal("PQprepare=20failed:=20%s",=20= PQerrorMessage(conn));=0A+=09PQclear(res);=0A+=0A+=09res=20=3D=20= PQexec(conn,=20"CLOSE=20fetch_cursor");=0A+=09if=20(PQresultStatus(res)=20= !=3D=20PGRES_COMMAND_OK)=0A+=09=09pg_fatal("CLOSE=20failed:=20%s",=20= PQerrorMessage(conn));=0A+=09PQclear(res);=0A+=0A+=09/*=0A+=09=20*=20= Describe=20must=20still=20work,=20using=20the=20cached=20row=20= descriptor.=20=20(This=20used=0A+=09=20*=20to=20crash=20the=20server.)=0A= +=09=20*/=0A+=09res=20=3D=20PQdescribePrepared(conn,=20"fetch_one");=0A+=09= if=20(PQresultStatus(res)=20!=3D=20PGRES_COMMAND_OK)=0A+=09=09= pg_fatal("PQdescribePrepared=20failed:=20%s",=20PQerrorMessage(conn));=0A= +=09if=20(PQnfields(res)=20!=3D=201=20||=20PQftype(res,=200)=20!=3D=20= INT4OID)=0A+=09=09pg_fatal("expected=20one=20int4=20column,=20got=20%d=20= fields",=20PQnfields(res));=0A+=09PQclear(res);=0A+=0A+=09res=20=3D=20= PQexec(conn,=20"ROLLBACK");=0A+=09if=20(PQresultStatus(res)=20!=3D=20= PGRES_COMMAND_OK)=0A+=09=09pg_fatal("ROLLBACK=20failed:=20%s",=20= PQerrorMessage(conn));=0A+=09PQclear(res);=0A+=0A+=09fprintf(stderr,=20= "ok\n");=0A+}=0A+=0A=20static=20void=0A=20= test_disallowed_in_pipeline(PGconn=20*conn)=0A=20{=0A@@=20-2117,6=20= +2166,7=20@@=20static=20void=0A=20print_test_list(void)=0A=20{=0A=20=09= printf("cancel\n");=0A+=09printf("describe_fetch\n");=0A=20=09= printf("disallowed_in_pipeline\n");=0A=20=09printf("multi_pipelines\n");=0A= =20=09printf("nosync\n");=0A@@=20-2223,6=20+2273,8=20@@=20main(int=20= argc,=20char=20**argv)=0A=20=0A=20=09if=20(strcmp(testname,=20"cancel")=20= =3D=3D=200)=0A=20=09=09test_cancel(conn);=0A+=09else=20if=20= (strcmp(testname,=20"describe_fetch")=20=3D=3D=200)=0A+=09=09= test_describe_fetch(conn);=0A=20=09else=20if=20(strcmp(testname,=20= "disallowed_in_pipeline")=20=3D=3D=200)=0A=20=09=09= test_disallowed_in_pipeline(conn);=0A=20=09else=20if=20(strcmp(testname,=20= "multi_pipelines")=20=3D=3D=200)=0A--=20=0A2.55.0=0A=0A= --Apple-Mail=_EB51FDE3-D74E-4239-A7FE-A345006F402F--