Re: Teaching planner to short-circuit empty UNION/EXCEPT/INTERSECT inputs
David Rowley <dgrowleyml@gmail.com>
From: David Rowley <dgrowleyml@gmail.com>
To: Alexander Lakhin <exclusion@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>, PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>
Date: 2025-11-05T05:26:53Z
Lists: pgsql-hackers
On Wed, 5 Nov 2025 at 17:00, Alexander Lakhin <exclusion@gmail.com> wrote: > But while playing around, I've just discovered another interesting error: > > SET enable_seqscan = 'off'; > SELECT * FROM t EXCEPT SELECT * FROM t > UNION SELECT * FROM pt; > > leads to: > ERROR: XX000: no relation entry for relid 0 > LOCATION: find_base_rel, relnode.c:541 :-( This happens because of the code I added to try to give better estimates to the number of groups when only a single child remains in the UNION. Because the only surviving Path is for the EXCEPT set-op, there are Vars with varno==0, which estimate_num_groups() does not like. I'm considering restricting that code Path to where the path->parent->reloptkind != RELOPT_UPPER_REL. It doesn't feel great adding the special case, but likely having better row estimates is worthwhile, when it's possible. I'll sit on that for a bit and see if I can think of a better way. (Yet another reason we should probably fix the varno==0 issue.) Thanks for the report and reproducer, again! David
Commits
-
Fix UNION planner estimate_num_groups with varno==0
- eaa159632d03 19 (unreleased) landed
-
Fix possible usage of incorrect UPPERREL_SETOP RelOptInfo
- fdda78e361f1 19 (unreleased) landed
-
Teach planner to short-circuit EXCEPT/INTERSECT with dummy inputs
- 9c9d41af4db7 19 (unreleased) landed
-
Fix incorrect targetlist in dummy UNIONs
- 928df067d1e6 19 (unreleased) landed
-
Teach UNION planner to remove dummy inputs
- 03d40e4b523b 19 (unreleased) landed