Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit232019b

Browse files
committed
Don't remove surplus columns from GROUP BY for inheritance parents
d4c3a15 added code to remove columns that were not part of a table'sPRIMARY KEY constraint from the GROUP BY clause when all the primary keycolumns were present in the group by. This is fine to do since we knowthat there will only be one row per group coming from this relation.However, the logic failed to consider inheritance parent relations. Thesecan have child relations without a primary key, but even if they did, theycould duplicate one of the parent's rows or one from another childrelation. In this case, those additional GROUP BY columns are required.Fix this by disabling the optimization for inheritance parent tables.In v11 and beyond, partitioned tables are fine since partitions cannotoverlap and before v11 partitioned tables could not have a primary key.Reported-by: Manuel RiggerDiscussion:http://postgr.es/m/CA+u7OA7VLKf_vEr6kLF3MnWSA9LToJYncgpNX2tQ-oWzYCBQAw@mail.gmail.comBackpatch-through: 9.6
1 parent0ce8e49 commit232019b

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

‎src/backend/optimizer/plan/planner.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2849,6 +2849,14 @@ remove_useless_groupby_columns(PlannerInfo *root)
28492849
if (rte->rtekind!=RTE_RELATION)
28502850
continue;
28512851

2852+
/*
2853+
* We must skip inheritance parent tables as some of the child rels
2854+
* may cause duplicate rows. We can skip partitioned tables too since
2855+
* these cannot have a primary key.
2856+
*/
2857+
if (rte->inh)
2858+
continue;
2859+
28522860
/* Nothing to do unless this rel has multiple Vars in GROUP BY */
28532861
relattnos=groupbyattnos[relid];
28542862
if (bms_membership(relattnos)!=BMS_MULTIPLE)

‎src/test/regress/expected/aggregates.out

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,29 @@ explain (costs off) select * from t3 group by a,b,c;
10171017
-> Seq Scan on t3
10181018
(3 rows)
10191019

1020-
drop table t1;
1020+
create temp table t1c () inherits (t1);
1021+
-- Ensure we don't remove any columns when t1 has a child table
1022+
explain (costs off) select * from t1 group by a,b,c,d;
1023+
QUERY PLAN
1024+
-------------------------------------
1025+
HashAggregate
1026+
Group Key: t1.a, t1.b, t1.c, t1.d
1027+
-> Append
1028+
-> Seq Scan on t1
1029+
-> Seq Scan on t1c
1030+
(5 rows)
1031+
1032+
-- Okay to remove columns if we're only querying the parent.
1033+
explain (costs off) select * from only t1 group by a,b,c,d;
1034+
QUERY PLAN
1035+
----------------------
1036+
HashAggregate
1037+
Group Key: a, b
1038+
-> Seq Scan on t1
1039+
(3 rows)
1040+
1041+
drop table t1 cascade;
1042+
NOTICE: drop cascades to table t1c
10211043
drop table t2;
10221044
drop table t3;
10231045
--

‎src/test/regress/sql/aggregates.sql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,15 @@ group by t1.a,t1.b,t1.c,t1.d,t2.x,t2.z;
362362
-- Cannot optimize when PK is deferrable
363363
explain (costs off)select*from t3group by a,b,c;
364364

365-
droptable t1;
365+
create temp table t1c () inherits (t1);
366+
367+
-- Ensure we don't remove any columns when t1 has a child table
368+
explain (costs off)select*from t1group by a,b,c,d;
369+
370+
-- Okay to remove columns if we're only querying the parent.
371+
explain (costs off)select*from only t1group by a,b,c,d;
372+
373+
droptable t1 cascade;
366374
droptable t2;
367375
droptable t3;
368376

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp