pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Adjust tuples estimate for appendrels
Date: Mon, 17 Feb 2025 02:13:52 +0000
Message-ID: <E1tjqds-007X0r-4u@gemulon.postgresql.org> (raw)

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@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: Alena Rybakina <a.rybakina@postgrespro.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(-)



Message-ID: <E1tjqds-007X0r-4u@gemulon.postgresql.org>
Permalink:  ../E1tjqds-007X0r-4u@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1tjqds-007X0r-4u@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: rguo@postgresql.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Adjust tuples estimate for appendrels
  In-Reply-To: <E1tjqds-007X0r-4u@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