Skip to content

Commit 1f5ca04

Browse files
committed
Disallow aggregate functions in UPDATE commands (unless within a sub-SELECT).
This is disallowed by the SQL spec because it doesn't have any very sensible interpretation. Historically Postgres has allowed it but behaved strangely. As of PG 8.1 a server crash is possible if the MIN/MAX index optimization gets applied; rather than try to "fix" that, it seems best to just enforce the spec restriction. Per report from Josh Drake and Alvaro Herrera.
1 parent e256baf commit 1f5ca04

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/backend/parser/analyze.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.334 2006/04/30 18:30:39 tgl Exp $
9+
* $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.335 2006/06/21 18:30:11 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -1613,7 +1613,7 @@ transformRuleStmt(ParseState *pstate, RuleStmt *stmt,
16131613
if (pstate->p_hasAggs)
16141614
ereport(ERROR,
16151615
(errcode(ERRCODE_GROUPING_ERROR),
1616-
errmsg("rule WHERE condition may not contain aggregate functions")));
1616+
errmsg("cannot use aggregate function in rule WHERE condition")));
16171617

16181618
/* save info about sublinks in where clause */
16191619
qry->hasSubLinks = pstate->p_hasSubLinks;
@@ -2346,9 +2346,16 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
23462346
qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
23472347

23482348
qry->hasSubLinks = pstate->p_hasSubLinks;
2349-
qry->hasAggs = pstate->p_hasAggs;
2349+
2350+
/*
2351+
* Top-level aggregates are simply disallowed in UPDATE, per spec.
2352+
* (From an implementation point of view, this is forced because the
2353+
* implicit ctid reference would otherwise be an ungrouped variable.)
2354+
*/
23502355
if (pstate->p_hasAggs)
2351-
parseCheckAggregates(pstate, qry);
2356+
ereport(ERROR,
2357+
(errcode(ERRCODE_GROUPING_ERROR),
2358+
errmsg("cannot use aggregate function in UPDATE")));
23522359

23532360
/*
23542361
* Now we are done with SELECT-like processing, and can get on with

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy