Re: BUG #19742: `INTERSECT` under a `UNION ALL` with an empty arm fails with "could not find pathkey item t"

From: shihao zhong <zhong950419(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: David Rowley <dgrowleyml(at)gmail(dot)com>, feasiblechart(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19742: `INTERSECT` under a `UNION ALL` with an empty arm fails with "could not find pathkey item t"
Date: 2026-10-04 21:43:20
Message-ID: CAGRkXqTFwygKmjLG_Y=kbXRHsePFmD6k+qrzVLP4_KrG+-=oRg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Tom,

> In particular, I wonder whether v19/HEAD are at risk of such bugs in
> cases that couldn't occur before we started eliding dummy child
> setops.

I looked for one with random queries. The attached script builds
nested setops with empty arms and mixed int and numeric columns,
puts an ORDER BY, a join or a second setop nest above them, and
checks the output against an answer worked out in Python. Obviously
I worked with Cladue to get that script generated.

I ran 20000 queries, each with seven sets of planner settings. On
HEAD 2516 of the 140000 runs hit the pathkey error. With both
Appends forced to NIL pathkeys none did. There were no wrong
answers in either build.

It did find one more varno 0 problem, in EXPLAIN only. 18 is fine.
create table d1 (a int);
create table d2 (b numeric);
explain (verbose, costs off)
select b from d2 except
(select a from d1 where false except all select a from d1);
ERROR: bogus varno: 0

928df067d1e handles a plain Var in the dummy Result's tlist. Here
the Var is under an int to numeric cast, so set_plan_refs() misses
it.

The attached patch walks the whole expression. It keeps the
varno 1 rewrite, so for a nested setop the name shown can come from
another child, as in the new test's output.

Thanks,
Shihao

Attachment Content-Type Size
setop_check.py text/x-python-script 9.4 KB
check-head-seed19742.out application/octet-stream 981 bytes
check-v2-seed19742.out application/octet-stream 1.0 KB
v1-0003-Fix-EXPLAIN.patch application/octet-stream 5.3 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2026-10-05 00:36:16 Re: BUG #19742: `INTERSECT` under a `UNION ALL` with an empty arm fails with "could not find pathkey item t"
Previous Message Tom Lane 2026-10-04 19:39:25 Re: BUG #19742: `INTERSECT` under a `UNION ALL` with an empty arm fails with "could not find pathkey item t"