pgsql: Adjust tuples estimate for appendrels

From: Richard Guo <rguo(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Adjust tuples estimate for appendrels
Date: 2025-02-17 02:13:52
Message-ID: E1tjqds-007X0r-4u@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Adjust tuples estimate for appendrels

In set_append_rel_size(), we currently set rel->tuples to rel->rows
for an appendrel. Generally, rel->tuples is the raw number of tuples
in the relation and rel->rows is the estimated number of tuples after
the relation's restriction clauses have been applied. Although an
appendrel itself doesn't directly enforce any quals today, its child
relations may. Therefore, setting rel->tuples equal to rel->rows for
an appendrel isn't always appropriate.

Doing so can lead to issues in cost estimates in some cases. For
instance, when estimating the number of distinct values from an
appendrel, we would not be able to adjust the estimate based on the
restriction selectivity.

This patch addresses this by setting an appendrel's tuples to the
total number of tuples accumulated from each live child, which better
aligns with reality.

This is arguably a bug, but nobody has complained about that until
now, so no back-patch.

Author: Richard Guo <guofenglinux(at)gmail(dot)com>
Reviewed-by: Tender Wang <tndrwang(at)gmail(dot)com>
Reviewed-by: Alena Rybakina <a(dot)rybakina(at)postgrespro(dot)ru>
Discussion: https://postgr.es/m/CAMbWs4_TG_+kVn6fjG-5GYzzukrNK57=g9eUo4gsrUG26OFawg@mail.gmail.com

Branch
------
master

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

Modified Files
--------------
src/backend/optimizer/path/allpaths.c | 19 +++++++++++++------
src/test/regress/expected/inherit.out | 30 ++++++++++++++++++++++++++++++
src/test/regress/sql/inherit.sql | 21 +++++++++++++++++++++
3 files changed, 64 insertions(+), 6 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Alexander Korotkov 2025-02-17 02:25:35 pgsql: Get rid of WALBufMappingLock
Previous Message Tom Lane 2025-02-16 19:20:52 pgsql: Release notes for 17.4, 16.8, 15.12, 14.17, 13.20.