Skip to content

Commit f95d8f8

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 e23338c commit f95d8f8

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10245,8 +10245,16 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
1024510245
/* Yes, it's correct to put alias after the right paren ... */
1024610246
if (j->alias != NULL)
1024710247
{
10248+
/*
10249+
* Note that it's correct to emit an alias clause if and only if
10250+
* there was one originally. Otherwise we'd be converting a named
10251+
* join to unnamed or vice versa, which creates semantic
10252+
* subtleties we don't want. However, we might print a different
10253+
* alias name than was there originally.
10254+
*/
1024810255
appendStringInfo(buf, " %s",
10249-
quote_identifier(j->alias->aliasname));
10256+
quote_identifier(get_rtable_name(j->rtindex,
10257+
context)));
1025010258
get_column_alias_list(colinfo, context);
1025110259
}
1025210260
}

src/test/regress/expected/create_view.out

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,41 @@ View definition:
769769
FROM temp_view_test.tx1 tx1_1
770770
WHERE tx1.y1 = tx1_1.f1));
771771

772+
-- Test aliasing of joins
773+
create view view_of_joins as
774+
select * from
775+
(select * from (tbl1 cross join tbl2) same) ss,
776+
(tbl3 cross join tbl4) same;
777+
\d+ view_of_joins
778+
View "testviewschm2.view_of_joins"
779+
Column | Type | Collation | Nullable | Default | Storage | Description
780+
--------+---------+-----------+----------+---------+---------+-------------
781+
a | integer | | | | plain |
782+
b | integer | | | | plain |
783+
c | integer | | | | plain |
784+
d | integer | | | | plain |
785+
e | integer | | | | plain |
786+
f | integer | | | | plain |
787+
g | integer | | | | plain |
788+
h | integer | | | | plain |
789+
View definition:
790+
SELECT ss.a,
791+
ss.b,
792+
ss.c,
793+
ss.d,
794+
same.e,
795+
same.f,
796+
same.g,
797+
same.h
798+
FROM ( SELECT same_1.a,
799+
same_1.b,
800+
same_1.c,
801+
same_1.d
802+
FROM (tbl1
803+
CROSS JOIN tbl2) same_1) ss,
804+
(tbl3
805+
CROSS JOIN tbl4) same;
806+
772807
-- Test view decompilation in the face of column addition/deletion/renaming
773808
create table tt2 (a int, b int, c int);
774809
create table tt3 (ax int8, b int2, c numeric);
@@ -1721,4 +1756,4 @@ select pg_get_ruledef(oid, true) from pg_rewrite
17211756
DROP SCHEMA temp_view_test CASCADE;
17221757
NOTICE: drop cascades to 27 other objects
17231758
DROP SCHEMA testviewschm2 CASCADE;
1724-
NOTICE: drop cascades to 62 other objects
1759+
NOTICE: drop cascades to 63 other objects

src/test/regress/sql/create_view.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,15 @@ ALTER TABLE tmp1 RENAME TO tx1;
319319
\d+ aliased_view_3
320320
\d+ aliased_view_4
321321

322+
-- Test aliasing of joins
323+
324+
create view view_of_joins as
325+
select * from
326+
(select * from (tbl1 cross join tbl2) same) ss,
327+
(tbl3 cross join tbl4) same;
328+
329+
\d+ view_of_joins
330+
322331
-- Test view decompilation in the face of column addition/deletion/renaming
323332

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

0 commit comments

Comments
 (0)