Ensure that pg_get_ruledef()'s output matches pg_get_viewdef()'s.
authorTom Lane <[email protected]>
Mon, 24 Jul 2017 19:16:31 +0000 (15:16 -0400)
committerTom Lane <[email protected]>
Mon, 24 Jul 2017 19:16:31 +0000 (15:16 -0400)
Various cases involving renaming of view columns are handled by having
make_viewdef pass down the view's current relation tupledesc to
get_query_def, which then takes care to use the column names from the
tupledesc for the output column names of the SELECT.  For some reason
though, we'd missed teaching make_ruledef to do similarly when it is
printing an ON SELECT rule, even though this is exactly the same case.
The results from pg_get_ruledef would then be different and arguably wrong.
In particular, this breaks pre-v10 versions of pg_dump, which in some
situations would define views by means of emitting a CREATE RULE ... ON
SELECT command.  Third-party tools might not be happy either.

In passing, clean up some crufty code in make_viewdef; we'd apparently
modernized the equivalent code in make_ruledef somewhere along the way,
and missed this copy.

Per report from Gilles Darold.  Back-patch to all supported versions.

Discussion: https://postgr.es/m/ec05659a-40ff-4510-fc45-ca9d965d0838@dalibo.com

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

index 0f044dee2174c75687ca9f81de99f4230af169ff..df110bb130fe306403cb823550fbc22a030d5814 100644 (file)
@@ -2402,6 +2402,8 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
    char       *ev_qual;
    char       *ev_action;
    List       *actions = NIL;
+   Relation    ev_relation;
+   TupleDesc   viewResultDesc = NULL;
    int         fno;
    Datum       dat;
    bool        isnull;
@@ -2443,6 +2445,8 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
    if (ev_action != NULL)
        actions = (List *) stringToNode(ev_action);
 
+   ev_relation = heap_open(ev_class, AccessShareLock);
+
    /*
     * Build the rules definition text
     */
@@ -2459,6 +2463,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
    {
        case '1':
            appendStringInfo(buf, "SELECT");
+           viewResultDesc = RelationGetDescr(ev_relation);
            break;
 
        case '2':
@@ -2553,7 +2558,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
        foreach(action, actions)
        {
            query = (Query *) lfirst(action);
-           get_query_def(query, buf, NIL, NULL,
+           get_query_def(query, buf, NIL, viewResultDesc,
                          prettyFlags, WRAP_COLUMN_DEFAULT, 0);
            if (prettyFlags)
                appendStringInfo(buf, ";\n");
@@ -2571,10 +2576,12 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
        Query      *query;
 
        query = (Query *) linitial(actions);
-       get_query_def(query, buf, NIL, NULL,
+       get_query_def(query, buf, NIL, viewResultDesc,
                      prettyFlags, WRAP_COLUMN_DEFAULT, 0);
        appendStringInfo(buf, ";");
    }
+
+   heap_close(ev_relation, AccessShareLock);
 }
 
 
index 0aaad2d63d5345a65d3802be4b89d671de12872e..5ad5869edd2ad76ff0c8cdfdc373f0ba30258bba 100644 (file)
@@ -426,6 +426,32 @@ select pg_get_viewdef('tt20v', true);
      CAST((1 + 2)::bigint AS bigint) i8(i8);
 (1 row)
 
+-- check handling of views with immediately-renamed columns
+create view tt23v (col_a, col_b) as
+select q1 as other_name1, q2 as other_name2 from int8_tbl
+union
+select 42, 43;
+select pg_get_viewdef('tt23v', true);
+                       pg_get_viewdef                       
+------------------------------------------------------------
+          SELECT int8_tbl.q1 AS col_a, int8_tbl.q2 AS col_b+
+            FROM int8_tbl                                  +
+ UNION                                                     +
+          SELECT 42 AS col_a, 43 AS col_b;
+(1 row)
+
+select pg_get_ruledef(oid, true) from pg_rewrite
+  where ev_class = 'tt23v'::regclass and ev_type = '1';
+                              pg_get_ruledef                              
+--------------------------------------------------------------------------
+ CREATE RULE "_RETURN" AS                                                +
+     ON SELECT TO tt23v DO INSTEAD          SELECT int8_tbl.q1 AS col_a, +
+             int8_tbl.q2 AS col_b                                        +
+            FROM int8_tbl                                                +
+ UNION                                                                   +
+          SELECT 42 AS col_a, 43 AS col_b;
+(1 row)
+
 -- clean up all the random objects we made above
 set client_min_messages = warning;
 DROP SCHEMA temp_view_test CASCADE;
index 6f6b442c688a43e1beaea41928c99510c94d3ec9..be9dd8d420d2e6db7d7d26a2cd4dfbbc316b9eda 100644 (file)
@@ -272,6 +272,17 @@ select * from
   cast(1+2 as int8) as i8;
 select pg_get_viewdef('tt20v', true);
 
+-- check handling of views with immediately-renamed columns
+
+create view tt23v (col_a, col_b) as
+select q1 as other_name1, q2 as other_name2 from int8_tbl
+union
+select 42, 43;
+
+select pg_get_viewdef('tt23v', true);
+select pg_get_ruledef(oid, true) from pg_rewrite
+  where ev_class = 'tt23v'::regclass and ev_type = '1';
+
 -- clean up all the random objects we made above
 set client_min_messages = warning;
 DROP SCHEMA temp_view_test CASCADE;