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 1xDPay-00000000UhN-3HUR for pgsql-bugs@arkaria.postgresql.org; Sun, 04 Oct 2026 17:01:52 +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 1xDPaw-00000005L0R-3dcb for pgsql-bugs@arkaria.postgresql.org; Sun, 04 Oct 2026 17:01:50 +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 1xDPaw-00000005L0I-2Zwz for pgsql-bugs@lists.postgresql.org; Sun, 04 Oct 2026 17:01:50 +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.98.2) (envelope-from ) id 1xDPas-00000000OAZ-2pfi for pgsql-bugs@lists.postgresql.org; Sun, 04 Oct 2026 17:01:50 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 694H1d6Z401042; Sun, 4 Oct 2026 13:01:39 -0400 From: Tom Lane To: David Rowley cc: feasiblechart@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #19742: `INTERSECT` under a `UNION ALL` with an empty arm fails with "could not find pathkey item t" In-reply-to: <290582.1791092578@sss.pgh.pa.us> References: <19742-dc403ca277cad1d3@postgresql.org> <261146.1791064428@sss.pgh.pa.us> <273647.1791075609@sss.pgh.pa.us> <290582.1791092578@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Sun, 04 Oct 2026 01:42:58 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <401029.1791133291.0@sss.pgh.pa.us> Date: Sun, 04 Oct 2026 13:01:39 -0400 Message-ID: <401041.1791133299@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: <401029.1791133291.1@sss.pgh.pa.us> I wrote: > My own thoughts were along the lines of "don't ever assign pathkeys to > an AppendPath"; not sure if that's equivalent to your first idea. Concretely, the attached fixes the given test case. There are other calls to create_append_path in prepunion.c, and I think they may all need to do likewise, but I didn't analyze them. It'd be nominally cleaner to add a flag to create_append_path telling it whether it's allowed to override the given pathkeys. I didn't do that here because it seems like this is a localized problem that should eventually be fixed inside prepunion.c, but there's room to argue differently. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="wip-fix-bad-pathkeys-for-UNION-append.patch"; charset="us-ascii" Content-ID: <401029.1791133291.2@sss.pgh.pa.us> Content-Description: wip-fix-bad-pathkeys-for-UNION-append.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimize= r/prep/prepunion.c index b136f12ff3b..c4e95f14dc5 100644 --- a/src/backend/optimizer/prep/prepunion.c +++ b/src/backend/optimizer/prep/prepunion.c @@ -862,6 +862,17 @@ generate_union_paths(SetOperationStmt *op, PlannerInf= o *root, apath =3D (Path *) create_append_path(root, result_rel, cheapest, NIL, NULL, 0, false, -1); = + /* + * Although we told create_append_path to assign NIL pathkeys to the + * AppendPath, it may have overridden that (if there's just one survivin= g + * child path, it will use that path's pathkeys). However, createplan.c + * will fail because the append relation's tlist contains varno-0 Vars + * (cf. generate_append_tlist), which won't match what is in the pathkey= s. + * We need to fix that someday, but for now, just force the AppendPath's + * pathkeys back to NIL. + */ + apath->pathkeys =3D NIL; + /* * Initialize the result row estimate to the total input size. This is * correct for UNION ALL; for the UNION case it is overwritten below wit= h ------- =_aaaaaaaaaa0--