Commit 158b7fa6 authored by Tom Lane's avatar Tom Lane

Disallow LATERAL references to the target table of an UPDATE/DELETE.

On second thought, commit 0c051c90 was
over-hasty: rather than allowing this case, we ought to reject it for now.
That leaves the field clear for a future feature that allows the target
table to be re-specified in the FROM (or USING) clause, which will enable
left-joining the target table to something else.  We can then also allow
LATERAL references to such an explicitly re-specified target table.
But allowing them right now will create ambiguities or worse for such a
feature, and it isn't something we documented 9.3 as supporting.

While at it, add a convenience subroutine to avoid having several copies
of the ereport for disalllowed-LATERAL-reference cases.
parent 910bac59
...@@ -367,8 +367,9 @@ transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt) ...@@ -367,8 +367,9 @@ transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
/* there's no DISTINCT in DELETE */ /* there's no DISTINCT in DELETE */
qry->distinctClause = NIL; qry->distinctClause = NIL;
/* subqueries in USING can see the result relation only via LATERAL */ /* subqueries in USING cannot access the result relation */
nsitem->p_lateral_only = true; nsitem->p_lateral_only = true;
nsitem->p_lateral_ok = false;
/* /*
* The USING clause is non-standard SQL syntax, and is equivalent in * The USING clause is non-standard SQL syntax, and is equivalent in
...@@ -378,8 +379,9 @@ transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt) ...@@ -378,8 +379,9 @@ transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
*/ */
transformFromClause(pstate, stmt->usingClause); transformFromClause(pstate, stmt->usingClause);
/* remaining clauses can see the result relation normally */ /* remaining clauses can reference the result relation normally */
nsitem->p_lateral_only = false; nsitem->p_lateral_only = false;
nsitem->p_lateral_ok = true;
qual = transformWhereClause(pstate, stmt->whereClause, qual = transformWhereClause(pstate, stmt->whereClause,
EXPR_KIND_WHERE, "WHERE"); EXPR_KIND_WHERE, "WHERE");
...@@ -1925,8 +1927,9 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt) ...@@ -1925,8 +1927,9 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
/* grab the namespace item made by setTargetTable */ /* grab the namespace item made by setTargetTable */
nsitem = (ParseNamespaceItem *) llast(pstate->p_namespace); nsitem = (ParseNamespaceItem *) llast(pstate->p_namespace);
/* subqueries in FROM can see the result relation only via LATERAL */ /* subqueries in FROM cannot access the result relation */
nsitem->p_lateral_only = true; nsitem->p_lateral_only = true;
nsitem->p_lateral_ok = false;
/* /*
* the FROM clause is non-standard SQL syntax. We used to be able to do * the FROM clause is non-standard SQL syntax. We used to be able to do
...@@ -1934,8 +1937,9 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt) ...@@ -1934,8 +1937,9 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
*/ */
transformFromClause(pstate, stmt->fromClause); transformFromClause(pstate, stmt->fromClause);
/* remaining clauses can see the result relation normally */ /* remaining clauses can reference the result relation normally */
nsitem->p_lateral_only = false; nsitem->p_lateral_only = false;
nsitem->p_lateral_ok = true;
qry->targetList = transformTargetList(pstate, stmt->targetList, qry->targetList = transformTargetList(pstate, stmt->targetList,
EXPR_KIND_UPDATE_SOURCE); EXPR_KIND_UPDATE_SOURCE);
......
...@@ -37,6 +37,8 @@ static RangeTblEntry *scanNameSpaceForRefname(ParseState *pstate, ...@@ -37,6 +37,8 @@ static RangeTblEntry *scanNameSpaceForRefname(ParseState *pstate,
const char *refname, int location); const char *refname, int location);
static RangeTblEntry *scanNameSpaceForRelid(ParseState *pstate, Oid relid, static RangeTblEntry *scanNameSpaceForRelid(ParseState *pstate, Oid relid,
int location); int location);
static void check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem,
int location);
static void markRTEForSelectPriv(ParseState *pstate, RangeTblEntry *rte, static void markRTEForSelectPriv(ParseState *pstate, RangeTblEntry *rte,
int rtindex, AttrNumber col); int rtindex, AttrNumber col);
static void expandRelation(Oid relid, Alias *eref, static void expandRelation(Oid relid, Alias *eref,
...@@ -170,14 +172,7 @@ scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location) ...@@ -170,14 +172,7 @@ scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
errmsg("table reference \"%s\" is ambiguous", errmsg("table reference \"%s\" is ambiguous",
refname), refname),
parser_errposition(pstate, location))); parser_errposition(pstate, location)));
/* SQL:2008 demands this be an error, not an invisible item */ check_lateral_ref_ok(pstate, nsitem, location);
if (nsitem->p_lateral_only && !nsitem->p_lateral_ok)
ereport(ERROR,
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
errmsg("invalid reference to FROM-clause entry for table \"%s\"",
refname),
errdetail("The combining JOIN type must be INNER or LEFT for a LATERAL reference."),
parser_errposition(pstate, location)));
result = rte; result = rte;
} }
} }
...@@ -221,14 +216,7 @@ scanNameSpaceForRelid(ParseState *pstate, Oid relid, int location) ...@@ -221,14 +216,7 @@ scanNameSpaceForRelid(ParseState *pstate, Oid relid, int location)
errmsg("table reference %u is ambiguous", errmsg("table reference %u is ambiguous",
relid), relid),
parser_errposition(pstate, location))); parser_errposition(pstate, location)));
/* SQL:2008 demands this be an error, not an invisible item */ check_lateral_ref_ok(pstate, nsitem, location);
if (nsitem->p_lateral_only && !nsitem->p_lateral_ok)
ereport(ERROR,
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
errmsg("invalid reference to FROM-clause entry for table \"%s\"",
rte->eref->aliasname),
errdetail("The combining JOIN type must be INNER or LEFT for a LATERAL reference."),
parser_errposition(pstate, location)));
result = rte; result = rte;
} }
} }
...@@ -410,6 +398,37 @@ checkNameSpaceConflicts(ParseState *pstate, List *namespace1, ...@@ -410,6 +398,37 @@ checkNameSpaceConflicts(ParseState *pstate, List *namespace1,
} }
} }
/*
* Complain if a namespace item is currently disallowed as a LATERAL reference.
* This enforces both SQL:2008's rather odd idea of what to do with a LATERAL
* reference to the wrong side of an outer join, and our own prohibition on
* referencing the target table of an UPDATE or DELETE as a lateral reference
* in a FROM/USING clause.
*
* Convenience subroutine to avoid multiple copies of a rather ugly ereport.
*/
static void
check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem,
int location)
{
if (nsitem->p_lateral_only && !nsitem->p_lateral_ok)
{
/* SQL:2008 demands this be an error, not an invisible item */
RangeTblEntry *rte = nsitem->p_rte;
char *refname = rte->eref->aliasname;
ereport(ERROR,
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
errmsg("invalid reference to FROM-clause entry for table \"%s\"",
refname),
(rte == pstate->p_target_rangetblentry) ?
errhint("There is an entry for table \"%s\", but it cannot be referenced from this part of the query.",
refname) :
errdetail("The combining JOIN type must be INNER or LEFT for a LATERAL reference."),
parser_errposition(pstate, location)));
}
}
/* /*
* given an RTE, return RT index (starting with 1) of the entry, * given an RTE, return RT index (starting with 1) of the entry,
* and optionally get its nesting depth (0 = current). If sublevels_up * and optionally get its nesting depth (0 = current). If sublevels_up
...@@ -632,15 +651,8 @@ colNameToVar(ParseState *pstate, char *colname, bool localonly, ...@@ -632,15 +651,8 @@ colNameToVar(ParseState *pstate, char *colname, bool localonly,
(errcode(ERRCODE_AMBIGUOUS_COLUMN), (errcode(ERRCODE_AMBIGUOUS_COLUMN),
errmsg("column reference \"%s\" is ambiguous", errmsg("column reference \"%s\" is ambiguous",
colname), colname),
parser_errposition(orig_pstate, location))); parser_errposition(pstate, location)));
/* SQL:2008 demands this be an error, not an invisible item */ check_lateral_ref_ok(pstate, nsitem, location);
if (nsitem->p_lateral_only && !nsitem->p_lateral_ok)
ereport(ERROR,
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
errmsg("invalid reference to FROM-clause entry for table \"%s\"",
rte->eref->aliasname),
errdetail("The combining JOIN type must be INNER or LEFT for a LATERAL reference."),
parser_errposition(orig_pstate, location)));
result = newresult; result = newresult;
} }
} }
......
...@@ -186,7 +186,8 @@ struct ParseState ...@@ -186,7 +186,8 @@ struct ParseState
* inside such a subexpression at the moment.) If p_lateral_ok is not set, * inside such a subexpression at the moment.) If p_lateral_ok is not set,
* it's an error to actually use such a namespace item. One might think it * it's an error to actually use such a namespace item. One might think it
* would be better to just exclude such items from visibility, but the wording * would be better to just exclude such items from visibility, but the wording
* of SQL:2008 requires us to do it this way. * of SQL:2008 requires us to do it this way. We also use p_lateral_ok to
* forbid LATERAL references to an UPDATE/DELETE target table.
* *
* At no time should a namespace list contain two entries that conflict * At no time should a namespace list contain two entries that conflict
* according to the rules in checkNameSpaceConflicts; but note that those * according to the rules in checkNameSpaceConflicts; but note that those
......
...@@ -4105,17 +4105,7 @@ LINE 1: select 1 from tenk1 a, lateral (select max(a.unique1) from i... ...@@ -4105,17 +4105,7 @@ LINE 1: select 1 from tenk1 a, lateral (select max(a.unique1) from i...
^ ^
-- check behavior of LATERAL in UPDATE/DELETE -- check behavior of LATERAL in UPDATE/DELETE
create temp table xx1 as select f1 as x1, -f1 as x2 from int4_tbl; create temp table xx1 as select f1 as x1, -f1 as x2 from int4_tbl;
select * from xx1; -- error, can't do this:
x1 | x2
-------------+-------------
0 | 0
123456 | -123456
-123456 | 123456
2147483647 | -2147483647
-2147483647 | 2147483647
(5 rows)
-- error, can't do this without LATERAL:
update xx1 set x2 = f1 from (select * from int4_tbl where f1 = x1) ss; update xx1 set x2 = f1 from (select * from int4_tbl where f1 = x1) ss;
ERROR: column "x1" does not exist ERROR: column "x1" does not exist
LINE 1: ... set x2 = f1 from (select * from int4_tbl where f1 = x1) ss; LINE 1: ... set x2 = f1 from (select * from int4_tbl where f1 = x1) ss;
...@@ -4126,28 +4116,28 @@ ERROR: invalid reference to FROM-clause entry for table "xx1" ...@@ -4126,28 +4116,28 @@ ERROR: invalid reference to FROM-clause entry for table "xx1"
LINE 1: ...t x2 = f1 from (select * from int4_tbl where f1 = xx1.x1) ss... LINE 1: ...t x2 = f1 from (select * from int4_tbl where f1 = xx1.x1) ss...
^ ^
HINT: There is an entry for table "xx1", but it cannot be referenced from this part of the query. HINT: There is an entry for table "xx1", but it cannot be referenced from this part of the query.
-- OK: -- can't do it even with LATERAL:
update xx1 set x2 = f1 from lateral (select * from int4_tbl where f1 = x1) ss; update xx1 set x2 = f1 from lateral (select * from int4_tbl where f1 = x1) ss;
select * from xx1; ERROR: invalid reference to FROM-clause entry for table "xx1"
x1 | x2 LINE 1: ...= f1 from lateral (select * from int4_tbl where f1 = x1) ss;
-------------+------------- ^
0 | 0 HINT: There is an entry for table "xx1", but it cannot be referenced from this part of the query.
123456 | 123456 -- we might in future allow something like this, but for now it's an error:
-123456 | -123456 update xx1 set x2 = f1 from xx1, lateral (select * from int4_tbl where f1 = x1) ss;
2147483647 | 2147483647 ERROR: table name "xx1" specified more than once
-2147483647 | -2147483647 -- also errors:
(5 rows)
-- error:
delete from xx1 using (select * from int4_tbl where f1 = x1) ss; delete from xx1 using (select * from int4_tbl where f1 = x1) ss;
ERROR: column "x1" does not exist ERROR: column "x1" does not exist
LINE 1: ...te from xx1 using (select * from int4_tbl where f1 = x1) ss; LINE 1: ...te from xx1 using (select * from int4_tbl where f1 = x1) ss;
^ ^
HINT: There is a column named "x1" in table "xx1", but it cannot be referenced from this part of the query. HINT: There is a column named "x1" in table "xx1", but it cannot be referenced from this part of the query.
-- OK: delete from xx1 using (select * from int4_tbl where f1 = xx1.x1) ss;
ERROR: invalid reference to FROM-clause entry for table "xx1"
LINE 1: ...from xx1 using (select * from int4_tbl where f1 = xx1.x1) ss...
^
HINT: There is an entry for table "xx1", but it cannot be referenced from this part of the query.
delete from xx1 using lateral (select * from int4_tbl where f1 = x1) ss; delete from xx1 using lateral (select * from int4_tbl where f1 = x1) ss;
select * from xx1; ERROR: invalid reference to FROM-clause entry for table "xx1"
x1 | x2 LINE 1: ...xx1 using lateral (select * from int4_tbl where f1 = x1) ss;
----+---- ^
(0 rows) HINT: There is an entry for table "xx1", but it cannot be referenced from this part of the query.
...@@ -1151,17 +1151,16 @@ select 1 from tenk1 a, lateral (select max(a.unique1) from int4_tbl b) ss; ...@@ -1151,17 +1151,16 @@ select 1 from tenk1 a, lateral (select max(a.unique1) from int4_tbl b) ss;
-- check behavior of LATERAL in UPDATE/DELETE -- check behavior of LATERAL in UPDATE/DELETE
create temp table xx1 as select f1 as x1, -f1 as x2 from int4_tbl; create temp table xx1 as select f1 as x1, -f1 as x2 from int4_tbl;
select * from xx1;
-- error, can't do this without LATERAL: -- error, can't do this:
update xx1 set x2 = f1 from (select * from int4_tbl where f1 = x1) ss; update xx1 set x2 = f1 from (select * from int4_tbl where f1 = x1) ss;
update xx1 set x2 = f1 from (select * from int4_tbl where f1 = xx1.x1) ss; update xx1 set x2 = f1 from (select * from int4_tbl where f1 = xx1.x1) ss;
-- OK: -- can't do it even with LATERAL:
update xx1 set x2 = f1 from lateral (select * from int4_tbl where f1 = x1) ss; update xx1 set x2 = f1 from lateral (select * from int4_tbl where f1 = x1) ss;
select * from xx1; -- we might in future allow something like this, but for now it's an error:
update xx1 set x2 = f1 from xx1, lateral (select * from int4_tbl where f1 = x1) ss;
-- error: -- also errors:
delete from xx1 using (select * from int4_tbl where f1 = x1) ss; delete from xx1 using (select * from int4_tbl where f1 = x1) ss;
-- OK: delete from xx1 using (select * from int4_tbl where f1 = xx1.x1) ss;
delete from xx1 using lateral (select * from int4_tbl where f1 = x1) ss; delete from xx1 using lateral (select * from int4_tbl where f1 = x1) ss;
select * from xx1;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment