Further fix dumping of views that contain just VALUES(...).
authorTom Lane <[email protected]>
Sun, 17 Nov 2019 01:00:20 +0000 (20:00 -0500)
committerTom Lane <[email protected]>
Sun, 17 Nov 2019 01:00:20 +0000 (20:00 -0500)
It turns out that commit e9f1c01b7 missed a case: we must print a
VALUES clause in long format if get_query_def is given a resultDesc
that would require the query's output column name(s) to be different
from what the bare VALUES clause would produce.

This applies in case an ALTER ... RENAME COLUMN has been done to
a view that formerly could be printed in simple format, as shown
in the added regression test case.  It also explains bug #16119
from Dmitry Telpt, because it turns out that (unlike CREATE VIEW)
CREATE MATERIALIZED VIEW fails to apply any column aliases it's
given to the stored ON SELECT rule.  So to get them to be printed,
we have to account for the resultDesc renaming.  It might be worth
changing the matview code so that it creates the ON SELECT rule
with the correct aliases; but we'd still need these messy checks in
get_simple_values_rte to handle the case of a subsequent column
rename, so any such change would be just neatnik-ism not a bug fix.

Like the previous patch, back-patch to all supported branches.

Discussion: https://postgr.es/m/16119-e64823f30a45a754@postgresql.org

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

index 1c2a4a574d1785600aaa4c4967710e816ff58e73..78482ff9bc6d4016809e852e66b8e231508047ef 100644 (file)
@@ -4628,19 +4628,20 @@ get_select_query_def(Query *query, deparse_context *context,
 }
 
 /*
- * Detect whether query looks like SELECT ... FROM VALUES();
- * if so, return the VALUES RTE.  Otherwise return NULL.
+ * Detect whether query looks like SELECT ... FROM VALUES(),
+ * with no need to rename the output columns of the VALUES RTE.
+ * If so, return the VALUES RTE.  Otherwise return NULL.
  */
 static RangeTblEntry *
-get_simple_values_rte(Query *query)
+get_simple_values_rte(Query *query, TupleDesc resultDesc)
 {
    RangeTblEntry *result = NULL;
    ListCell   *lc;
 
    /*
-    * We want to return TRUE even if the Query also contains OLD or NEW rule
-    * RTEs.  So the idea is to scan the rtable and see if there is only one
-    * inFromCl RTE that is a VALUES RTE.
+    * We want to detect a match even if the Query also contains OLD or NEW
+    * rule RTEs.  So the idea is to scan the rtable and see if there is only
+    * one inFromCl RTE that is a VALUES RTE.
     */
    foreach(lc, query->rtable)
    {
@@ -4663,23 +4664,36 @@ get_simple_values_rte(Query *query)
     * parser/analyze.c will never generate a "bare" VALUES RTE --- they only
     * appear inside auto-generated sub-queries with very restricted
     * structure.  However, DefineView might have modified the tlist by
-    * injecting new column aliases; so compare tlist resnames against the
-    * RTE's names to detect that.
+    * injecting new column aliases, or we might have some other column
+    * aliases forced by a resultDesc.  We can only simplify if the RTE's
+    * column names match the names that get_target_list() would select.
     */
    if (result)
    {
        ListCell   *lcn;
+       int         colno;
 
        if (list_length(query->targetList) != list_length(result->eref->colnames))
            return NULL;        /* this probably cannot happen */
+       colno = 0;
        forboth(lc, query->targetList, lcn, result->eref->colnames)
        {
            TargetEntry *tle = (TargetEntry *) lfirst(lc);
            char       *cname = strVal(lfirst(lcn));
+           char       *colname;
 
            if (tle->resjunk)
                return NULL;    /* this probably cannot happen */
-           if (tle->resname == NULL || strcmp(tle->resname, cname) != 0)
+
+           /* compute name that get_target_list would use for column */
+           colno++;
+           if (resultDesc && colno <= resultDesc->natts)
+               colname = NameStr(TupleDescAttr(resultDesc, colno - 1)->attname);
+           else
+               colname = tle->resname;
+
+           /* does it match the VALUES RTE? */
+           if (colname == NULL || strcmp(colname, cname) != 0)
                return NULL;    /* column name has been changed */
        }
    }
@@ -4707,7 +4721,7 @@ get_basic_select_query(Query *query, deparse_context *context,
     * VALUES part.  This reverses what transformValuesClause() did at parse
     * time.
     */
-   values_rte = get_simple_values_rte(query);
+   values_rte = get_simple_values_rte(query, resultDesc);
    if (values_rte)
    {
        get_values_def(values_rte->values_lists, context);
index 22cc716167b366a1d23dcf5748248f2e8418e50c..02cfbcbb67a7d1750f1de0ec3657b117fb847f9c 100644 (file)
@@ -2674,6 +2674,18 @@ create view rule_v1 as values(1,2);
 View definition:
  VALUES (1,2);
 
+alter table rule_v1 rename column column2 to q2;
+\d+ rule_v1
+                 View "public.rule_v1"
+ Column  |  Type   | Modifiers | Storage | Description 
+---------+---------+-----------+---------+-------------
+ column1 | integer |           | plain   | 
+ q2      | integer |           | plain   | 
+View definition:
+ SELECT "*VALUES*".column1,
+    "*VALUES*".column2 AS q2
+   FROM (VALUES (1,2)) "*VALUES*";
+
 drop view rule_v1;
 create view rule_v1(x) as values(1,2);
 \d+ rule_v1
index be822bece346c7823d21e7b6e08ede5353acd626..79585499a116882e2bbb635fc5dab3d7598c5d2c 100644 (file)
@@ -1013,6 +1013,8 @@ DROP TABLE rule_t1;
 --
 create view rule_v1 as values(1,2);
 \d+ rule_v1
+alter table rule_v1 rename column column2 to q2;
+\d+ rule_v1
 drop view rule_v1;
 create view rule_v1(x) as values(1,2);
 \d+ rule_v1