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

Appearance settings

Commit e5b0fff

Browse filesBrowse files
committed
Reject SELECT ... GROUP BY GROUPING SETS (()) FOR UPDATE.
This case should be disallowed, just as FOR UPDATE with a plain GROUP BY is disallowed; FOR UPDATE only makes sense when each row of the query result can be identified with a single table row. However, we missed teaching CheckSelectLocking() to check groupingSets as well as groupClause, so that it would allow degenerate grouping sets. That resulted in a bad plan and a null-pointer dereference in the executor. Looking around for other instances of the same bug, the only one I found was in examine_simple_variable(). That'd just lead to silly estimates, but it should be fixed too. Per private report from Yaoguang Chen. Back-patch to all supported branches.
1 parent d250568 commit e5b0fff
Copy full SHA for e5b0fff

File tree

Expand file treeCollapse file tree

4 files changed

+12
-2
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+12
-2
lines changed

‎src/backend/parser/analyze.c

Copy file name to clipboardExpand all lines: src/backend/parser/analyze.c
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2688,7 +2688,7 @@ CheckSelectLocking(Query *qry, LockClauseStrength strength)
26882688
translator: %s is a SQL row locking clause such as FOR UPDATE */
26892689
errmsg("%s is not allowed with DISTINCT clause",
26902690
LCS_asString(strength))));
2691-
if (qry->groupClause != NIL)
2691+
if (qry->groupClause != NIL || qry->groupingSets != NIL)
26922692
ereport(ERROR,
26932693
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
26942694
/*------

‎src/backend/utils/adt/selfuncs.c

Copy file name to clipboardExpand all lines: src/backend/utils/adt/selfuncs.c
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5150,7 +5150,8 @@ examine_simple_variable(PlannerInfo *root, Var *var,
51505150
* of learning something even with it.
51515151
*/
51525152
if (subquery->setOperations ||
5153-
subquery->groupClause)
5153+
subquery->groupClause ||
5154+
subquery->groupingSets)
51545155
return;
51555156

51565157
/*

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

Copy file name to clipboardExpand all lines: src/test/regress/expected/errors.out
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ select distinct on (foobar) * from pg_database;
5050
ERROR: column "foobar" does not exist
5151
LINE 1: select distinct on (foobar) * from pg_database;
5252
^
53+
-- grouping with FOR UPDATE
54+
select null from pg_database group by datname for update;
55+
ERROR: FOR UPDATE is not allowed with GROUP BY clause
56+
select null from pg_database group by grouping sets (()) for update;
57+
ERROR: FOR UPDATE is not allowed with GROUP BY clause
5358
--
5459
-- DELETE
5560
-- missing relation name (this had better not wildcard!)

‎src/test/regress/sql/errors.sql

Copy file name to clipboardExpand all lines: src/test/regress/sql/errors.sql
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ select * from pg_database where pg_database.datname = nonesuch;
3737
-- bad attribute name in select distinct on
3838
select distinct on (foobar) * from pg_database;
3939

40+
-- grouping with FOR UPDATE
41+
select null from pg_database group by datname for update;
42+
select null from pg_database group by grouping sets (()) for update;
43+
4044

4145
--
4246
-- DELETE

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.