diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index a309dc36665..447db2863d5 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -2818,6 +2818,15 @@ transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc, ++i; if (!rte->inFromCl) continue; + + /* + * A join RTE without an alias is not visible as a relation + * name and needs to be skipped (otherwise it might hide a + * base relation with the same name). + */ + if (rte->rtekind == RTE_JOIN && rte->alias == NULL) + continue; + if (strcmp(rte->eref->aliasname, thisrel->relname) == 0) { switch (rte->rtekind) diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out index c8d3716a15e..3cef1b3525c 100644 --- a/src/test/regress/expected/join.out +++ b/src/test/regress/expected/join.out @@ -2449,6 +2449,14 @@ ERROR: column t1.x does not exist LINE 1: select t1.x from t1 join t3 on (t1.a = t3.x); ^ HINT: Perhaps you meant to reference the column "t3.x". +-- Test matching of locking clause with wrong alias +select t1.*, t2.*, unnamed_join.* from + t1 join t2 on (t1.a = t2.a), t3 as unnamed_join + for update of unnamed_join; + a | b | a | b | x | y +---+---+---+---+---+--- +(0 rows) + -- -- regression test for 8.1 merge right join bug -- diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql index 691fae38d50..7a04d67a145 100644 --- a/src/test/regress/sql/join.sql +++ b/src/test/regress/sql/join.sql @@ -509,6 +509,12 @@ select * from t1 left join t2 on (t1.a = t2.a); select t1.x from t1 join t3 on (t1.a = t3.x); +-- Test matching of locking clause with wrong alias + +select t1.*, t2.*, unnamed_join.* from + t1 join t2 on (t1.a = t2.a), t3 as unnamed_join + for update of unnamed_join; + -- -- regression test for 8.1 merge right join bug --