]> granicus.if.org Git - postgresql/commitdiff
Fix incorrect printing of queries with duplicated join names.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 12 Jun 2019 23:42:38 +0000 (19:42 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 12 Jun 2019 23:43:08 +0000 (19:43 -0400)
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

src/backend/utils/adt/ruleutils.c
src/test/regress/expected/create_view.out
src/test/regress/sql/create_view.sql

index 9dda4820afd367dac5e5c36b0ffddabedc047c8a..b9fdd99db8e891cf36531c1349e4e94936e8d937 100644 (file)
@@ -10309,8 +10309,16 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
                /* Yes, it's correct to put alias after the right paren ... */
                if (j->alias != NULL)
                {
+                       /*
+                        * Note that it's correct to emit an alias clause if and only if
+                        * there was one originally.  Otherwise we'd be converting a named
+                        * join to unnamed or vice versa, which creates semantic
+                        * subtleties we don't want.  However, we might print a different
+                        * alias name than was there originally.
+                        */
                        appendStringInfo(buf, " %s",
-                                                        quote_identifier(j->alias->aliasname));
+                                                        quote_identifier(get_rtable_name(j->rtindex,
+                                                                                                                         context)));
                        get_column_alias_list(colinfo, context);
                }
        }
index e367ea2571685c89daafb838f0cb2f9cf6a6d284..2fd36ca9a1465c17da401161c68076b478c9f773 100644 (file)
@@ -769,6 +769,41 @@ View definition:
            FROM temp_view_test.tx1 tx1_1
           WHERE tx1.y1 = tx1_1.f1));
 
+-- Test aliasing of joins
+create view view_of_joins as
+select * from
+  (select * from (tbl1 cross join tbl2) same) ss,
+  (tbl3 cross join tbl4) same;
+\d+ view_of_joins
+                    View "testviewschm2.view_of_joins"
+ Column |  Type   | Collation | Nullable | Default | Storage | Description 
+--------+---------+-----------+----------+---------+---------+-------------
+ a      | integer |           |          |         | plain   | 
+ b      | integer |           |          |         | plain   | 
+ c      | integer |           |          |         | plain   | 
+ d      | integer |           |          |         | plain   | 
+ e      | integer |           |          |         | plain   | 
+ f      | integer |           |          |         | plain   | 
+ g      | integer |           |          |         | plain   | 
+ h      | integer |           |          |         | plain   | 
+View definition:
+ SELECT ss.a,
+    ss.b,
+    ss.c,
+    ss.d,
+    same.e,
+    same.f,
+    same.g,
+    same.h
+   FROM ( SELECT same_1.a,
+            same_1.b,
+            same_1.c,
+            same_1.d
+           FROM (tbl1
+             CROSS JOIN tbl2) same_1) ss,
+    (tbl3
+     CROSS JOIN tbl4) same;
+
 -- Test view decompilation in the face of column addition/deletion/renaming
 create table tt2 (a int, b int, c int);
 create table tt3 (ax int8, b int2, c numeric);
@@ -1747,7 +1782,7 @@ drop cascades to view aliased_view_2
 drop cascades to view aliased_view_3
 drop cascades to view aliased_view_4
 DROP SCHEMA testviewschm2 CASCADE;
-NOTICE:  drop cascades to 62 other objects
+NOTICE:  drop cascades to 63 other objects
 DETAIL:  drop cascades to table t1
 drop cascades to view temporal1
 drop cascades to view temporal2
@@ -1771,6 +1806,7 @@ drop cascades to view mysecview4
 drop cascades to view unspecified_types
 drop cascades to table tt1
 drop cascades to table tx1
+drop cascades to view view_of_joins
 drop cascades to table tt2
 drop cascades to table tt3
 drop cascades to table tt4
index c69f8f6eda34ea57d15e8decc9c216f14eef8f52..8c0f45cc5268e846e5b99b909222fb6848ba5d57 100644 (file)
@@ -319,6 +319,15 @@ ALTER TABLE tmp1 RENAME TO tx1;
 \d+ aliased_view_3
 \d+ aliased_view_4
 
+-- Test aliasing of joins
+
+create view view_of_joins as
+select * from
+  (select * from (tbl1 cross join tbl2) same) ss,
+  (tbl3 cross join tbl4) same;
+
+\d+ view_of_joins
+
 -- Test view decompilation in the face of column addition/deletion/renaming
 
 create table tt2 (a int, b int, c int);