agora inbox for pgsql-bugs@postgresql.org  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: David Rowley <dgrowleyml@gmail.com>
Cc: feasiblechart@gmail.com
Cc: 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"
Date: Sun, 04 Oct 2026 13:01:39 -0400
Message-ID: <401041.1791133299@sss.pgh.pa.us> (raw)
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>
	<CAApHDvreoQsZs=CGNz2GjAsHV=j-tb_ycOHW3EFsBp1TO8BxcA@mail.gmail.com>
	<290582.1791092578@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

Attachments:

  [text/x-diff] wip-fix-bad-pathkeys-for-UNION-append.patch (1.0K, ../401041.1791133299@sss.pgh.pa.us/2-wip-fix-bad-pathkeys-for-UNION-append.patch)
  download | inline diff:
diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/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, PlannerInfo *root,
 	apath = (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 surviving
+	 * 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 pathkeys.
+	 * We need to fix that someday, but for now, just force the AppendPath's
+	 * pathkeys back to NIL.
+	 */
+	apath->pathkeys = 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 with

view thread (14+ messages)  latest in thread

Message-ID: <401041.1791133299@sss.pgh.pa.us>
Permalink:  ../401041.1791133299@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/401041.1791133299@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, dgrowleyml@gmail.com, 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: <401041.1791133299@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 agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox