Teach Append to consider tuple_fraction when accumulating subpaths.
authorAlexander Korotkov <akorotkov@postgresql.org>
Mon, 10 Mar 2025 11:38:39 +0000 (13:38 +0200)
committerAlexander Korotkov <akorotkov@postgresql.org>
Mon, 10 Mar 2025 11:38:39 +0000 (13:38 +0200)
commitfae535da0ac2a8d0bb279cc66d62b0dcc4b5409b
tree8040d2e6c2fd5e6d4a4497b8cdcbdeea9b8f4ea4
parentb83e8a2ca2eb381ea0a48e5b2d4e4cdb74febc45
Teach Append to consider tuple_fraction when accumulating subpaths.

This change is dedicated to more active usage of IndexScan and parameterized
NestLoop paths in partitioned cases under an Append node, as it already works
with plain tables.  As newly added regression tests demonstrate, it should
provide more smartness to the partitionwise technique.

With an indication of how many tuples are needed, it may be more meaningful
to use the 'fractional branch' subpaths of the Append path list, which are
more optimal for this specific number of tuples.  Planning on a higher level,
if the optimizer needs all the tuples, it will choose non-fractional paths.
In the case when, during execution, Append needs to return fewer tuples than
declared by tuple_fraction, it would not be harmful to use the 'intermediate'
variant of paths.  However, it will earn a considerable profit if a sensible
set of tuples is selected.

The change of the existing regression test demonstrates the positive outcome
of this feature: instead of scanning the whole table, the optimizer prefers
to use a parameterized scan, being aware of the only single tuple the join
has to produce to perform the query.

Discussion: https://www.postgresql.org/message-id/flat/CAN-LCVPxnWB39CUBTgOQ9O7Dd8DrA_tpT1EY3LNVnUuvAX1NjA%40mail.gmail.com
Author: Nikita Malakhov <hukutoc@gmail.com>
Author: Andrei Lepikhov <lepihov@gmail.com>
Reviewed-by: Andy Fan <zhihuifan1213@163.com>
Reviewed-by: Alexander Korotkov <aekorotkov@gmail.com>
src/backend/optimizer/path/allpaths.c
src/backend/optimizer/plan/planner.c
src/test/regress/expected/partition_join.out
src/test/regress/expected/union.out
src/test/regress/sql/partition_join.sql