summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorDavid Rowley2019-07-03 11:44:54 +0000
committerDavid Rowley2019-07-03 11:44:54 +0000
commita5be4062f7bf2ae9487c5a31ee337a56425cdc84 (patch)
tree84b7b867271994a429dde6acc7e7d9a0633179ef /src/test
parent2a1612104cadbfdc739ff0370f279779f323c3b5 (diff)
Don't remove surplus columns from GROUP BY for inheritance parents
d4c3a156c added code to remove columns that were not part of a table's PRIMARY KEY constraint from the GROUP BY clause when all the primary key columns were present in the group by. This is fine to do since we know that there will only be one row per group coming from this relation. However, the logic failed to consider inheritance parent relations. These can have child relations without a primary key, but even if they did, they could duplicate one of the parent's rows or one from another child relation. 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 cannot overlap and before v11 partitioned tables could not have a primary key. Reported-by: Manuel Rigger Discussion: http://postgr.es/m/CA+u7OA7VLKf_vEr6kLF3MnWSA9LToJYncgpNX2tQ-oWzYCBQAw@mail.gmail.com Backpatch-through: 9.6
Diffstat (limited to 'src/test')
-rw-r--r--src/test/regress/expected/aggregates.out45
-rw-r--r--src/test/regress/sql/aggregates.sql24
2 files changed, 67 insertions, 2 deletions
diff --git a/src/test/regress/expected/aggregates.out b/src/test/regress/expected/aggregates.out
index 2e5ce8cc32b..ef8eec3fbf2 100644
--- a/src/test/regress/expected/aggregates.out
+++ b/src/test/regress/expected/aggregates.out
@@ -1147,9 +1147,52 @@ explain (costs off) select * from t3 group by a,b,c;
-> Seq Scan on t3
(3 rows)
-drop table t1;
+create temp table t1c () inherits (t1);
+-- Ensure we don't remove any columns when t1 has a child table
+explain (costs off) select * from t1 group by a,b,c,d;
+ QUERY PLAN
+-------------------------------------
+ HashAggregate
+ Group Key: t1.a, t1.b, t1.c, t1.d
+ -> Append
+ -> Seq Scan on t1
+ -> Seq Scan on t1c
+(5 rows)
+
+-- Okay to remove columns if we're only querying the parent.
+explain (costs off) select * from only t1 group by a,b,c,d;
+ QUERY PLAN
+----------------------
+ HashAggregate
+ Group Key: a, b
+ -> Seq Scan on t1
+(3 rows)
+
+create temp table p_t1 (
+ a int,
+ b int,
+ c int,
+ d int,
+ primary key(a,b)
+) partition by list(a);
+create temp table p_t1_1 partition of p_t1 for values in(1);
+create temp table p_t1_2 partition of p_t1 for values in(2);
+-- Ensure we can remove non-PK columns for partitioned tables.
+explain (costs off) select * from p_t1 group by a,b,c,d;
+ QUERY PLAN
+---------------------------------
+ HashAggregate
+ Group Key: p_t1_1.a, p_t1_1.b
+ -> Append
+ -> Seq Scan on p_t1_1
+ -> Seq Scan on p_t1_2
+(5 rows)
+
+drop table t1 cascade;
+NOTICE: drop cascades to table t1c
drop table t2;
drop table t3;
+drop table p_t1;
--
-- Test combinations of DISTINCT and/or ORDER BY
--
diff --git a/src/test/regress/sql/aggregates.sql b/src/test/regress/sql/aggregates.sql
index ca0d5e66b2a..17fb256aec5 100644
--- a/src/test/regress/sql/aggregates.sql
+++ b/src/test/regress/sql/aggregates.sql
@@ -406,9 +406,31 @@ group by t1.a,t1.b,t1.c,t1.d,t2.x,t2.z;
-- Cannot optimize when PK is deferrable
explain (costs off) select * from t3 group by a,b,c;
-drop table t1;
+create temp table t1c () inherits (t1);
+
+-- Ensure we don't remove any columns when t1 has a child table
+explain (costs off) select * from t1 group by a,b,c,d;
+
+-- Okay to remove columns if we're only querying the parent.
+explain (costs off) select * from only t1 group by a,b,c,d;
+
+create temp table p_t1 (
+ a int,
+ b int,
+ c int,
+ d int,
+ primary key(a,b)
+) partition by list(a);
+create temp table p_t1_1 partition of p_t1 for values in(1);
+create temp table p_t1_2 partition of p_t1 for values in(2);
+
+-- Ensure we can remove non-PK columns for partitioned tables.
+explain (costs off) select * from p_t1 group by a,b,c,d;
+
+drop table t1 cascade;
drop table t2;
drop table t3;
+drop table p_t1;
--
-- Test combinations of DISTINCT and/or ORDER BY