From ecb533af623738f49a8bd748e688c7f02c1c0f9f Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sat, 16 Nov 2019 20:00:20 -0500 Subject: [PATCH] Further fix dumping of views that contain just VALUES(...). 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 | 34 ++++++++++++++++++++--------- src/test/regress/expected/rules.out | 12 ++++++++++ src/test/regress/sql/rules.sql | 2 ++ 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 9f7eb2ba9f..81a7603ebe 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -4685,19 +4685,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) { @@ -4720,23 +4721,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 */ } } @@ -4764,7 +4778,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); diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index ef72c3ddb6..615e14fbe4 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -2774,6 +2774,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 diff --git a/src/test/regress/sql/rules.sql b/src/test/regress/sql/rules.sql index c5693db903..12cd4a012b 100644 --- a/src/test/regress/sql/rules.sql +++ b/src/test/regress/sql/rules.sql @@ -1036,6 +1036,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