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

Commit133f5ae

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 parent721963f commit133f5ae

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

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

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

3035+
/*
3036+
* We must skip inheritance parent tables as some of the child rels
3037+
* may cause duplicate rows. This cannot happen with partitioned
3038+
* tables, however.
3039+
*/
3040+
if (rte->inh&&rte->relkind!=RELKIND_PARTITIONED_TABLE)
3041+
continue;
3042+
30353043
/* Nothing to do unless this rel has multiple Vars in GROUP BY */
30363044
relattnos=groupbyattnos[relid];
30373045
if (bms_membership(relattnos)!=BMS_MULTIPLE)

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,9 +1017,52 @@ 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+
create temp table p_t1 (
1042+
a int,
1043+
b int,
1044+
c int,
1045+
d int,
1046+
primary key(a,b)
1047+
) partition by list(a);
1048+
create temp table p_t1_1 partition of p_t1 for values in(1);
1049+
create temp table p_t1_2 partition of p_t1 for values in(2);
1050+
-- Ensure we can remove non-PK columns for partitioned tables.
1051+
explain (costs off) select * from p_t1 group by a,b,c,d;
1052+
QUERY PLAN
1053+
---------------------------------
1054+
HashAggregate
1055+
Group Key: p_t1_1.a, p_t1_1.b
1056+
-> Append
1057+
-> Seq Scan on p_t1_1
1058+
-> Seq Scan on p_t1_2
1059+
(5 rows)
1060+
1061+
drop table t1 cascade;
1062+
NOTICE: drop cascades to table t1c
10211063
drop table t2;
10221064
drop table t3;
1065+
drop table p_t1;
10231066
--
10241067
-- Test combinations of DISTINCT and/or ORDER BY
10251068
--

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,31 @@ 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+
create temp table p_t1 (
374+
aint,
375+
bint,
376+
cint,
377+
dint,
378+
primary key(a,b)
379+
) partition by list(a);
380+
create temp table p_t1_1 partition of p_t1 forvaluesin(1);
381+
create temp table p_t1_2 partition of p_t1 forvaluesin(2);
382+
383+
-- Ensure we can remove non-PK columns for partitioned tables.
384+
explain (costs off)select*from p_t1group by a,b,c,d;
385+
386+
droptable t1 cascade;
366387
droptable t2;
367388
droptable t3;
389+
droptable p_t1;
368390

369391
--
370392
-- Test combinations of DISTINCT and/or ORDER BY

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp