Reject SELECT ... GROUP BY GROUPING SETS (()) FOR UPDATE.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 1 Jun 2021 15:12:56 +0000 (11:12 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 1 Jun 2021 15:12:56 +0000 (11:12 -0400)
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.

src/backend/parser/analyze.c
src/backend/utils/adt/selfuncs.c
src/test/regress/expected/errors.out
src/test/regress/sql/errors.sql

index 502b10368543e94933d9e2994df3bdb9331a1749..b903b89466ca59b6fc5bf294a830cf73784055f2 100644 (file)
@@ -2566,7 +2566,7 @@ CheckSelectLocking(Query *qry, LockClauseStrength strength)
          translator: %s is a SQL row locking clause such as FOR UPDATE */
                 errmsg("%s is not allowed with DISTINCT clause",
                        LCS_asString(strength))));
-   if (qry->groupClause != NIL)
+   if (qry->groupClause != NIL || qry->groupingSets != NIL)
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
        /*------
index 5aa8308b0573ae536c042cce8dba006570c867c4..72e9409b784dba4785ac8959a5fcdd9e8a4a5cf3 100644 (file)
@@ -4746,7 +4746,8 @@ examine_simple_variable(PlannerInfo *root, Var *var,
         * of learning something even with it.
         */
        if (subquery->setOperations ||
-           subquery->groupClause)
+           subquery->groupClause ||
+           subquery->groupingSets)
            return;
 
        /*
index 210e5ff39cbcacf7844d70088403b9af0f640cf9..848f2c03de7983d385c7cf7e94e8b74b91bad8a8 100644 (file)
@@ -50,6 +50,11 @@ select distinct on (foobar) * from pg_database;
 ERROR:  column "foobar" does not exist
 LINE 1: select distinct on (foobar) * from pg_database;
                             ^
+-- grouping with FOR UPDATE
+select null from pg_database group by datname for update;
+ERROR:  FOR UPDATE is not allowed with GROUP BY clause
+select null from pg_database group by grouping sets (()) for update;
+ERROR:  FOR UPDATE is not allowed with GROUP BY clause
 --
 -- DELETE
 -- missing relation name (this had better not wildcard!)
index cd370b4781e09d9f6ab66272117e1fed99742de2..1b2c7d391afa9a27cd780b2fd58a6c17e422013d 100644 (file)
@@ -37,6 +37,10 @@ select * from pg_database where pg_database.datname = nonesuch;
 -- bad attribute name in select distinct on
 select distinct on (foobar) * from pg_database;
 
+-- grouping with FOR UPDATE
+select null from pg_database group by datname for update;
+select null from pg_database group by grouping sets (()) for update;
+
 
 --
 -- DELETE