8000 Fix alias matching in transformLockingClause(). · postgres/postgres@e88b1f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit e88b1f1

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 1cad30e commit e88b1f1

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
@@ -2823,6 +2823,15 @@ transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc,
28232823
++i;
28242824
if (!rte->inFromCl)
28252825
continue;
2826+
2827+
/*
2828+
* A join RTE without an alias is not visible as a relation
2829+
* name and needs to be skipped (otherwise it might hide a
2830+
* base relation with the same name).
2831+
*/
2832+
if (rte->rtekind == RTE_JOIN && rte->alias == NULL)
2833+
continue;
2834+
28262835
if (strcmp(rte->eref->aliasname, thisrel->relname) == 0)
28272836
{
28282837
switch (rte->rtekind)

src/test/regress/expected/join.out

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,6 +2434,14 @@ ERROR: column t1.x does not exist
24342434
LINE 1: select t1.x from t1 join t3 on (t1.a = t3.x);
24352435
^
24362436
HINT: Perhaps you meant to reference the column "t3.x".
2437+
-- Test matching of locking clause with wrong alias
2438+
select t1.*, t2.*, unnamed_join.* from
2439+
t1 join t2 on (t1.a = t2.a), t3 as unnamed_join
2440+
for update of unnamed_join;
2441+
a | b | a | b | x | y
2442+
---+---+---+---+---+---
2443+
(0 rows)
2444+
24372445
--
24382446
-- regression test for 8.1 merge right join bug
24392447
--

src/test/regress/sql/join.sql

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

497497
select t1.x from t1 join t3 on (t1.a = t3.x);
498498

499+
-- Test matching of locking clause with wrong alias
500+
501+
select t1.*, t2.*, unnamed_join.* from
502+
t1 join t2 on (t1.a = t2.a), t3 as unnamed_join
503+
for update of unnamed_join;
504+
499505
--
500506
-- regression test for 8.1 merge right join bug
501507
--

0 commit comments

Comments
 (0)
0