Re: Teaching planner to short-circuit empty UNION/EXCEPT/INTERSECT inputs

Zhang Mingli <zmlpostgres@gmail.com>

From: Mingli Zhang <zmlpostgres@gmail.com>
To: David Rowley <dgrowleyml@gmail.com>
Cc: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>, Tom Lane <tgl@sss.pgh.pa.us>
Date: 2025-10-02T13:55:36Z
Lists: pgsql-hackers
Hi,

David Rowley <dgrowleyml@gmail.com>于2025年10月2日 周四20:09写道:

> On Thu, 2 Oct 2025 at 16:21, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> > I'm wondering if it could be shortened a great deal by
> > handling left-input-dummy and EXCEPT-ALL-with-right-input-dummy
> > but leaving the EXCEPT-with-right-input-dummy case unimproved.
>
> Good idea. Less code and still get to keep the one that did well in
> the benchmark. See attached.
>
> I ended up splitting the patch in two. 0001 for UNION only, then 0002
> for the INTERSECT and EXCEPT.
>
> David


It seems that the optimization for `UNION ALL` is already implemented in
the patch: it removes empty sub-paths and preserves the remaining ones.
Should we add a test case to formally validate this behavior like Union
cases?

Commits

  1. Fix UNION planner estimate_num_groups with varno==0

  2. Fix possible usage of incorrect UPPERREL_SETOP RelOptInfo

  3. Teach planner to short-circuit EXCEPT/INTERSECT with dummy inputs

  4. Fix incorrect targetlist in dummy UNIONs

  5. Teach UNION planner to remove dummy inputs