aboutsummaryrefslogtreecommitdiff
path: root/src/backend/parser/analyze.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2014-01-07 15:25:16 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2014-01-07 15:25:27 -0500
commit0c051c90082da0b7e5bcaf9aabcbd4f361137cdc (patch)
tree457a1ae1db6b3f135f7a56a6d3512916687b7d34 /src/backend/parser/analyze.c
parentf68220df92cb56f0452919f51eeef16262ec8f3b (diff)
downloadpostgresql-0c051c90082da0b7e5bcaf9aabcbd4f361137cdc.tar.gz
postgresql-0c051c90082da0b7e5bcaf9aabcbd4f361137cdc.zip
Fix LATERAL references to target table of UPDATE/DELETE.
I failed to think much about UPDATE/DELETE when implementing LATERAL :-(. The implemented behavior ended up being that subqueries in the FROM or USING clause (respectively) could access the update/delete target table as though it were a lateral reference; which seems fine if they said LATERAL, but certainly ought to draw an error if they didn't. Fix it so you get a suitable error when you omit LATERAL. Per report from Emre Hasegeli.
Diffstat (limited to 'src/backend/parser/analyze.c')
-rw-r--r--src/backend/parser/analyze.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c
index 60cce378453..6560b86e4b2 100644
--- a/src/backend/parser/analyze.c
+++ b/src/backend/parser/analyze.c
@@ -342,6 +342,7 @@ static Query *
transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
{
Query *qry = makeNode(Query);
+ ParseNamespaceItem *nsitem;
Node *qual;
qry->commandType = CMD_DELETE;
@@ -360,8 +361,15 @@ transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
true,
ACL_DELETE);
+ /* grab the namespace item made by setTargetTable */
+ nsitem = (ParseNamespaceItem *) llast(pstate->p_namespace);
+
+ /* there's no DISTINCT in DELETE */
qry->distinctClause = NIL;
+ /* subqueries in USING can see the result relation only via LATERAL */
+ nsitem->p_lateral_only = true;
+
/*
* The USING clause is non-standard SQL syntax, and is equivalent in
* functionality to the FROM list that can be specified for UPDATE. The
@@ -370,6 +378,9 @@ transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
*/
transformFromClause(pstate, stmt->usingClause);
+ /* remaining clauses can see the result relation normally */
+ nsitem->p_lateral_only = false;
+
qual = transformWhereClause(pstate, stmt->whereClause,
EXPR_KIND_WHERE, "WHERE");
@@ -1889,6 +1900,7 @@ static Query *
transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
{
Query *qry = makeNode(Query);
+ ParseNamespaceItem *nsitem;
RangeTblEntry *target_rte;
Node *qual;
ListCell *origTargetList;
@@ -1910,12 +1922,21 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
true,
ACL_UPDATE);
+ /* grab the namespace item made by setTargetTable */
+ nsitem = (ParseNamespaceItem *) llast(pstate->p_namespace);
+
+ /* subqueries in FROM can see the result relation only via LATERAL */
+ nsitem->p_lateral_only = true;
+
/*
* the FROM clause is non-standard SQL syntax. We used to be able to do
* this with REPLACE in POSTQUEL so we keep the feature.
*/
transformFromClause(pstate, stmt->fromClause);
+ /* remaining clauses can see the result relation normally */
+ nsitem->p_lateral_only = false;
+
qry->targetList = transformTargetList(pstate, stmt->targetList,
EXPR_KIND_UPDATE_SOURCE);