Fix costing bug in MergeAppend
authorDavid Rowley <drowley@postgresql.org>
Wed, 31 Jan 2024 20:48:26 +0000 (09:48 +1300)
committerDavid Rowley <drowley@postgresql.org>
Wed, 31 Jan 2024 20:48:26 +0000 (09:48 +1300)
When building a MergeAppendPath which has child paths that are not
sorted correctly for the MergeAppend's sort order, we apply the cost of
sorting those paths to the MergeAppendPath costs.

Here we fix a bug where the number of tuples specified that needed to be
sorted was effectively pg_class.reltuples rather than the number of
expected row in the subpath.  This effectively penalizes MergeAppend
plans any time any filter is present on the MergeAppend subpath as the
sort cost added is to sort all tuples in the table rather than just the
ones expected the path to return.

This did not affect UNION ALL type queries as the RelOptInfo tuples is
set from the subquery's path rows.  It does affect MergeAppends uses for
inheritance and partitioned tables.

This is a long-standing bug introduced when MergeAppend was first added
in 11cad29c9.  No backpatch as this could result in plan changes.

Author: Alexander Kuzmenkov
Reviewed-by: Ashutosh Bapat, Aleksander Alekseev, David Rowley
Discussion: https://postgr.es/m/CALzhyqyhoXQDR-Usd_0HeWk%3DuqNLzoVeT8KhRoo%3DpV_KzgO3QQ%40mail.gmail.com

src/backend/optimizer/util/pathnode.c
src/test/regress/expected/inherit.out
src/test/regress/sql/inherit.sql

index 8dbf790e893e61f4b57366911c7398e30e81026a..2e1ec41a5412acbfd6fae5e5297119f734a13153 100644 (file)
@@ -1470,7 +1470,7 @@ create_merge_append_path(PlannerInfo *root,
                      root,
                      pathkeys,
                      subpath->total_cost,
-                     subpath->parent->tuples,
+                     subpath->rows,
                      subpath->pathtarget->width,
                      0.0,
                      work_mem,
index 56a40d50f906374be5054dc15e22fc33d01230e3..130a92422889ace43e1fed73dc4873639639e11c 100644 (file)
@@ -1714,6 +1714,29 @@ order by t1.b limit 10;
 (14 rows)
 
 reset enable_nestloop;
+drop table matest0 cascade;
+NOTICE:  drop cascades to table matest1
+-- Test a MergeAppend plan where one child requires a sort
+create table matest0(a int primary key);
+create table matest1() inherits (matest0);
+insert into matest0 select generate_series(1, 400);
+insert into matest1 select generate_series(1, 200);
+analyze matest0;
+analyze matest1;
+explain (costs off)
+select * from matest0 where a < 100 order by a;
+                          QUERY PLAN                           
+---------------------------------------------------------------
+ Merge Append
+   Sort Key: matest0.a
+   ->  Index Only Scan using matest0_pkey on matest0 matest0_1
+         Index Cond: (a < 100)
+   ->  Sort
+         Sort Key: matest0_2.a
+         ->  Seq Scan on matest1 matest0_2
+               Filter: (a < 100)
+(8 rows)
+
 drop table matest0 cascade;
 NOTICE:  drop cascades to table matest1
 --
index 971aac54fda5be8f24502798756c9f9039d52de4..0ef9a61bc16b677e211ad88df033b4ae8aa5f74b 100644 (file)
@@ -624,6 +624,19 @@ reset enable_nestloop;
 
 drop table matest0 cascade;
 
+-- Test a MergeAppend plan where one child requires a sort
+create table matest0(a int primary key);
+create table matest1() inherits (matest0);
+insert into matest0 select generate_series(1, 400);
+insert into matest1 select generate_series(1, 200);
+analyze matest0;
+analyze matest1;
+
+explain (costs off)
+select * from matest0 where a < 100 order by a;
+
+drop table matest0 cascade;
+
 --
 -- Test merge-append for UNION ALL append relations
 --