Skip to content

Commit 8ace51a

Browse files
committed
Fix incorrect printing of queries with duplicated join names.
Given a query in which multiple JOIN nodes used the same alias (which'd necessarily be in different sub-SELECTs), ruleutils.c would assign the JOIN nodes distinct aliases for clarity ... but then it forgot to print the modified aliases when dumping the JOIN nodes themselves. This results in a dump/reload hazard for views, because the emitted query is flat-out incorrect: Vars will be printed with table names that have no referent. This has been wrong for a long time, so back-patch to all supported branches. Philip Dubé Discussion: https://postgr.es/m/CY4PR2101MB080246F2955FF58A6ED1FEAC98140@CY4PR2101MB0802.namprd21.prod.outlook.com
1 parent 9695ecf commit 8ace51a

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8889,8 +8889,16 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
88898889
/* Yes, it's correct to put alias after the right paren ... */
88908890
if (j->alias != NULL)
88918891
{
8892+
/*
8893+
* Note that it's correct to emit an alias clause if and only if
8894+
* there was one originally. Otherwise we'd be converting a named
8895+
* join to unnamed or vice versa, which creates semantic
8896+
* subtleties we don't want. However, we might print a different
8897+
* alias name than was there originally.
8898+
*/
88928899
appendStringInfo(buf, " %s",
8893-
quote_identifier(j->alias->aliasname));
8900+
quote_identifier(get_rtable_name(j->rtindex,
8901+
context)));
88948902
get_column_alias_list(colinfo, context);
88958903
}
88968904
}

src/test/regress/expected/create_view.out

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,41 @@ View definition:
743743
FROM temp_view_test.tx1 tx1_1
744744
WHERE tx1.y1 = tx1_1.f1));
745745

746+
-- Test aliasing of joins
747+
create view view_of_joins as
748+
select * from
749+
(select * from (tbl1 cross join tbl2) same) ss,
750+
(tbl3 cross join tbl4) same;
751+
\d+ view_of_joins
752+
View "testviewschm2.view_of_joins"
753+
Column | Type | Modifiers | Storage | Description
754+
--------+---------+-----------+---------+-------------
755+
a | integer | | plain |
756+
b | integer | | plain |
757+
c | integer | | plain |
758+
d | integer | | plain |
759+
e | integer | | plain |
760+
f | integer | | plain |
761+
g | integer | | plain |
762+
h | integer | | plain |
763+
View definition:
764+
SELECT ss.a,
765+
ss.b,
766+
ss.c,
767+
ss.d,
768+
same.e,
769+
same.f,
770+
same.g,
771+
same.h
772+
FROM ( SELECT same_1.a,
773+
same_1.b,
774+
same_1.c,
775+
same_1.d
776+
FROM (tbl1
777+
CROSS JOIN tbl2) same_1) ss,
778+
(tbl3
779+
CROSS JOIN tbl4) same;
780+
746781
-- Test view decompilation in the face of column addition/deletion/renaming
747782
create table tt2 (a int, b int, c int);
748783
create table tt3 (ax int8, b int2, c numeric);

src/test/regress/sql/create_view.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,15 @@ ALTER TABLE tmp1 RENAME TO tx1;
311311
\d+ aliased_view_3
312312
\d+ aliased_view_4
313313

314+
-- Test aliasing of joins
315+
316+
create view view_of_joins as
317+
select * from
318+
(select * from (tbl1 cross join tbl2) same) ss,
319+
(tbl3 cross join tbl4) same;
320+
321+
\d+ view_of_joins
322+
314323
-- Test view decompilation in the face of column addition/deletion/renaming
315324

316325
create table tt2 (a int, b int, c int);

0 commit comments

Comments
 (0)