Skip to content

Commit 3d3a81f

Browse files
committed
Fix tuple_fraction calculation in generate_orderedappend_paths()
6b94e7a adjusted generate_orderedappend_paths() to consider fractional paths. However, it didn't manage to interpret the tuple_fraction value correctly. According to the header comment of grouping_planner(), the tuple_fraction >= 1 specifies the absolute number of expected tuples. That number must be divided by the expected total number of tuples to get the actual fraction. Even though this is a bug fix, we don't backpatch it. The risks of the side effects of plan changes on stable branches are too high. Reported-by: Andrei Lepikhov <lepihov@gmail.com> Discussion: https://postgr.es/m/3ca271fa-ca5c-458c-8934-eb148622b270%40gmail.com Author: Andrei Lepikhov <lepihov@gmail.com> Reviewed-by: Junwang Zhao <zhjwpku@gmail.com> Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
1 parent 12eee85 commit 3d3a81f

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/backend/optimizer/path/allpaths.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1891,7 +1891,17 @@ generate_orderedappend_paths(PlannerInfo *root, RelOptInfo *rel,
18911891
*/
18921892
if (root->tuple_fraction > 0)
18931893
{
1894-
double path_fraction = (1.0 / root->tuple_fraction);
1894+
double path_fraction = root->tuple_fraction;
1895+
1896+
/*
1897+
* Merge Append considers only live children relations. Dummy
1898+
* relations must be filtered out before.
1899+
*/
1900+
Assert(childrel->rows > 0);
1901+
1902+
/* Convert absolute limit to a path fraction */
1903+
if (path_fraction >= 1.0)
1904+
path_fraction /= childrel->rows;
18951905

18961906
cheapest_fractional =
18971907
get_cheapest_fractional_path_for_pathkeys(childrel->pathlist,

src/test/regress/expected/partition_join.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5260,6 +5260,24 @@ SELECT x.id, y.id FROM fract_t x LEFT JOIN fract_t y USING (id) ORDER BY x.id DE
52605260
Index Cond: (id = x_2.id)
52615261
(11 rows)
52625262

5263+
EXPLAIN (COSTS OFF) -- Should use NestLoop with parameterised inner scan
5264+
SELECT x.id, y.id FROM fract_t x LEFT JOIN fract_t y USING (id)
5265+
ORDER BY x.id DESC LIMIT 2;
5266+
QUERY PLAN
5267+
--------------------------------------------------------------------------------
5268+
Limit
5269+
-> Merge Append
5270+
Sort Key: x.id DESC
5271+
-> Nested Loop Left Join
5272+
-> Index Only Scan Backward using fract_t0_pkey on fract_t0 x_1
5273+
-> Index Only Scan using fract_t0_pkey on fract_t0 y_1
5274+
Index Cond: (id = x_1.id)
5275+
-> Nested Loop Left Join
5276+
-> Index Only Scan Backward using fract_t1_pkey on fract_t1 x_2
5277+
-> Index Only Scan using fract_t1_pkey on fract_t1 y_2
5278+
Index Cond: (id = x_2.id)
5279+
(11 rows)
5280+
52635281
--
52645282
-- Test Append's fractional paths
52655283
--

src/test/regress/sql/partition_join.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,9 @@ SELECT x.id, y.id FROM fract_t x LEFT JOIN fract_t y USING (id) ORDER BY x.id AS
12241224

12251225
EXPLAIN (COSTS OFF)
12261226
SELECT x.id, y.id FROM fract_t x LEFT JOIN fract_t y USING (id) ORDER BY x.id DESC LIMIT 10;
1227+
EXPLAIN (COSTS OFF) -- Should use NestLoop with parameterised inner scan
1228+
SELECT x.id, y.id FROM fract_t x LEFT JOIN fract_t y USING (id)
1229+
ORDER BY x.id DESC LIMIT 2;
12271230

12281231
--
12291232
-- Test Append's fractional paths

0 commit comments

Comments
 (0)