Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ace122

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 f5e4d64 commit 8ace122

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
@@ -2783,6 +2783,15 @@ transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc,
27832783
++i;
27842784
if (!rte->inFromCl)
27852785
continue;
2786+
2787+
/*
2788+
* A join RTE without an alias is not visible as a relation
2789+
* name and needs to be skipped (otherwise it might hide a
2790+
* base relation with the same name).
2791+
*/
2792+
if (rte->rtekind == RTE_JOIN && rte->alias == NULL)
2793+
continue;
2794+
27862795
if (strcmp(rte->eref->aliasname, thisrel->relname) == 0)
27872796
{
27882797
switch (rte->rtekind)

src/test/regress/expected/join.out

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,6 +2412,14 @@ ERROR: column t1.x does not exist
24122412
LINE 1: select t1.x from t1 join t3 on (t1.a = t3.x);
24132413
^
24142414
HINT: Perhaps you meant to reference the column "t3.x".
2415+
-- Test matching of locking clause with wrong alias
2416+
select t1.*, t2.*, unnamed_join.* from
2417+
t1 join t2 on (t1.a = t2.a), t3 as unnamed_join
2418+
for update of unnamed_join;
2419+
a | b | a | b | x | y
2420+
---+---+---+---+---+---
2421+
(0 rows)
2422+
24152423
--
24162424
-- regression test for 8.1 merge right join bug
24172425
--

src/test/regress/sql/join.sql

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

488488
select t1.x from t1 join t3 on (t1.a = t3.x);
489489

490+
-- Test matching of locking clause with wrong alias
491+
492+
select t1.*, t2.*, unnamed_join.* from
493+
t1 join t2 on (t1.a = t2.a), t3 as unnamed_join
494+
for update of unnamed_join;
495+
490496
--
491497
-- regression test for 8.1 merge right join bug
492498
--

0 commit comments

Comments
 (0)