Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f890223

Browse files
committed
Fix alias matching in transformLockingClause().
When locking a specific named relation for a FOR [KEY] UPDATE/SHARE clause, transformLockingClause() finds the relation to lock by scanning the rangetable for an RTE with a matching eref->aliasname. However, it failed to account for the visibility rules of a join RTE. If a join RTE doesn't have a user-supplied alias, it will have a generated eref->aliasname of "unnamed_join" that is not visible as a relation name in the parse namespace. Such an RTE needs to be skipped, otherwise it might be found in preference to a regular base relation with a user-supplied alias of "unnamed_join", preventing it from being locked. In addition, if a join RTE doesn't have a user-supplied alias, but does have a join_using_alias, then the RTE needs to be matched using that alias rather than the generated eref->aliasname, otherwise a misleading "relation not found" error will be reported rather than a "join cannot be locked" error. Backpatch all the way, except for the second part which only goes back to 14, where JOIN USING aliases were added. Dean Rasheed, reviewed by Tom Lane. Discussion: https://postgr.es/m/CAEZATCUY_KOBnqxbTSPf=7fz9HWPnZ5Xgb9SwYzZ8rFXe7nb=w@mail.gmail.com
1 parent d232357 commit f890223

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/backend/parser/analyze.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,15 @@ transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc,
28182818
++i;
28192819
if (!rte->inFromCl)
28202820
continue;
2821+
2822+
/*
2823+
* A join RTE without an alias is not visible as a relation
2824+
* name and needs to be skipped (otherwise it might hide a
2825+
* base relation with the same name).
2826+
*/
2827+
if (rte->rtekind == RTE_JOIN && rte->alias == NULL)
2828+
continue;
2829+
28212830
if (strcmp(rte->eref->aliasname, thisrel->relname) == 0)
28222831
{
28232832
switch (rte->rtekind)

src/test/regress/expected/join.out

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,6 +2449,14 @@ ERROR: column t1.x does not exist
24492449
LINE 1: select t1.x from t1 join t3 on (t1.a = t3.x);
24502450
^
24512451
HINT: Perhaps you meant to reference the column "t3.x".
2452+
-- Test matching of locking clause with wrong alias
2453+
select t1.*, t2.*, unnamed_join.* from
2454+
t1 join t2 on (t1.a = t2.a), t3 as unnamed_join
2455+
for update of unnamed_join;
2456+
a | b | a | b | x | y
2457+
---+---+---+---+---+---
2458+
(0 rows)
2459+
24522460
--
24532461
-- regression test for 8.1 merge right join bug
24542462
--

src/test/regress/sql/join.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,12 @@ select * from t1 left join t2 on (t1.a = t2.a);
509509

510510
select t1.x from t1 join t3 on (t1.a = t3.x);
511511

512+
-- Test matching of locking clause with wrong alias
513+
514+
select t1.*, t2.*, unnamed_join.* from
515+
t1 join t2 on (t1.a = t2.a), t3 as unnamed_join
516+
for update of unnamed_join;
517+
512518
--
513519
-- regression test for 8.1 merge right join bug
514520
--

0 commit comments

Comments
 (0)