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

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Alexander Lakhin <exclusion(at)gmail(dot)com>
Cc: David Rowley <dgrowleyml(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Teaching planner to short-circuit empty UNION/EXCEPT/INTERSECT inputs
Date: 2025-11-04 10:02:18
Message-ID: CAMbWs49yA=Z+x8PMWBCyJa5L+AmK+SWV-52Smwxv8U_7=PyVMA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Nov 4, 2025 at 6:19 PM Richard Guo <guofenglinux(at)gmail(dot)com> wrote:
> On Tue, Nov 4, 2025 at 5:00 PM Alexander Lakhin <exclusion(at)gmail(dot)com> wrote:
> > Please look at a new anomaly, introduced with 03d40e4b5:
> > CREATE TABLE t(i integer);
> > CREATE TABLE pt(i integer) PARTITION BY LIST(i);
> >
> > SET enable_seqscan = 'off';
> > SELECT * FROM t UNION SELECT * FROM t
> > UNION ALL
> > SELECT * FROM pt;
> > produces:
> > ERROR: XX000: unrecognized node type: 0
> > LOCATION: create_plan_recurse, createplan.c:538
>
> I looked into this. The child relation with relid 3 (the scan on the
> partitioned table) is a dummy, so it is skipped in
> generate_union_paths(). As a result, the final setop relation ends up
> the same as the child relation with relid set to (1, 2). Then,
> generate_union_paths() creates an Append path using this relation's
> cheapest path as its subpath. Somehow, add_path() determines that
> this new Append path dominates the original cheapest path, causing the
> original cheapest path to be freed. This leads to the final Append
> path referencing a subpath that has already been freed.

I think a quick fix is to always include the involved child relids
when building the relid set for the setop relation, even if the child
is dummy. A dummy child does not yield any rows, but theoretically it
is still a relation that we should account for.

- Richard

Attachment Content-Type Size
v1-0001-Fix-generate_union_paths.patch application/octet-stream 2.1 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Nishant Sharma 2025-11-04 10:37:14 Re: [PATCH] Add pg_get_tablespace_ddl() function to reconstruct CREATE TABLESPACE statement
Previous Message David Rowley 2025-11-04 10:00:09 Re: Teaching planner to short-circuit empty UNION/EXCEPT/INTERSECT inputs