pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Rowley <drowley@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix UNION planner estimate_num_groups with varno==0
Date: Thu, 06 Nov 2025 03:35:12 +0000
Message-ID: <E1vGqmG-005EXq-2D@gemulon.postgresql.org> (raw)

Fix UNION planner estimate_num_groups with varno==0

03d40e4b5 added code to provide better row estimates for when a UNION
query ended up only with a single child due to other children being
found to be dummy rels.  In that case, ordinarily it would be ok to call
estimate_num_groups() on the targetlist of the only child path, however
that's not safe to do if the UNION child is the result of some other set
operation as we generate targetlists containing Vars with varno==0 for
those, which estimate_num_groups() can't handle.  This could lead to:

ERROR:  XX000: no relation entry for relid 0

Fix this by avoiding doing this when the only child is the result of
another set operation.  In that case we'll fall back on the
assume-all-rows-are-unique method.

Reported-by: Alexander Lakhin <exclusion@gmail.com>
Author: David Rowley <dgrowleyml@gmail.com>
Discussion: https://postgr.es/m/cfbc99e5-9d44-4806-ba3c-f36b57a85e21@gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/eaa159632d034a59ede1695f4b86098b1a372b48

Modified Files
--------------
src/backend/optimizer/prep/prepunion.c | 23 +++++++++++++----------
src/test/regress/expected/union.out    | 22 ++++++++++++++++++++++
src/test/regress/sql/union.sql         |  8 ++++++++
3 files changed, 43 insertions(+), 10 deletions(-)



Message-ID: <E1vGqmG-005EXq-2D@gemulon.postgresql.org>
Permalink:  ../E1vGqmG-005EXq-2D@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1vGqmG-005EXq-2D@gemulon.postgresql.org

 · 

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-committers@postgresql.org
  Cc: drowley@postgresql.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Fix UNION planner estimate_num_groups with varno==0
  In-Reply-To: <E1vGqmG-005EXq-2D@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox