pgsql: Fix UNION planner estimate_num_groups with varno==0 - Mailing list pgsql-committers

From David Rowley
Subject pgsql: Fix UNION planner estimate_num_groups with varno==0
Date
Msg-id E1vGqmG-005EXq-2D@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
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(-)


pgsql-committers by date:

Previous
From: Etsuro Fujita
Date:
Subject: pgsql: Update obsolete comment in ExecScanReScan().
Next
From: Peter Eisentraut
Date:
Subject: pgsql: Update code comment