Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YQekx-0007Wu-2j for pgsql-bugs@arkaria.postgresql.org; Wed, 25 Feb 2015 16:24:15 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YQekw-0004fE-Dx for pgsql-bugs@arkaria.postgresql.org; Wed, 25 Feb 2015 16:24:14 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YQekv-0004e3-8C for pgsql-bugs@postgresql.org; Wed, 25 Feb 2015 16:24:13 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YQekn-00042K-JH for pgsql-bugs@postgresql.org; Wed, 25 Feb 2015 16:24:11 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id t1PGO2rx018996; Wed, 25 Feb 2015 11:24:02 -0500 From: Tom Lane To: Andrew Gierth cc: programble@gmail.com, pgsql-bugs@postgresql.org Subject: Re: BUG #12789: Views defined with VALUES lose their column names when dumped In-reply-to: <87ioexfb91.fsf@news-spur.riddles.org.uk> References: <20150220043045.8730.61379@wrigleys.postgresql.org> <87ioexfb91.fsf@news-spur.riddles.org.uk> Comments: In-reply-to Andrew Gierth message dated "Fri, 20 Feb 2015 04:48:49 +0000" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <18981.1424881413.0@sss.pgh.pa.us> Date: Wed, 25 Feb 2015 11:24:02 -0500 Message-ID: <18995.1424881442@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <18981.1424881413.1@sss.pgh.pa.us> Andrew Gierth writes: > For future reference and as a simpler testcase than the one in the > script: > # create view v1(a) as values (1); > CREATE VIEW > # select pg_get_viewdef('v1'::regclass); > pg_get_viewdef > ---------------- > VALUES (1); > Notice that the column name "a" is lost. Since pg_dump and so on rely on > pg_get_viewdef, dump and restore changes the column name back to > "column1". > The culprit is obviously in ruleutils.c: > get_simple_values_rte/get_values_def, which mistakenly thinks it only > has to deal with the result of transformValuesClause(), not considering > that the result of transformValuesClause might have been further > mogrified by DefineView. Treating this case as not being "simple" might > be one approach... not sure of the best way to detect that. Yeah --- we can check to see if the tlist resnames match what's in the RTE. It turns out that get_from_clause_item() is also buggy: apparently the RTE_VALUES path through that has never been exercised, or at least nobody has pointed out to us that it prints bad syntax. I'm guessing that up to now, get_simple_values_rte *always* succeeds for situations involving a VALUES RTE and so we never got there. The attached seems to fix it though. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="view-values-fix.patch"; charset="us-ascii" Content-ID: <18981.1424881413.2@sss.pgh.pa.us> Content-Description: view-values-fix.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/rule= utils.c index c1d860c..2fa30be 100644 *** a/src/backend/utils/adt/ruleutils.c --- b/src/backend/utils/adt/ruleutils.c *************** get_simple_values_rte(Query *query) *** 4498,4507 **** /* * 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 don't look at the targetlist at ! * all. This is okay because parser/analyze.c will never generate a ! * "bare" VALUES RTE --- they only appear inside auto-generated ! * sub-queries with very restricted structure. */ foreach(lc, query->rtable) { --- 4498,4504 ---- /* * 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. */ foreach(lc, query->rtable) { *************** get_simple_values_rte(Query *query) *** 4518,4523 **** --- 4515,4547 ---- else return NULL; /* something else -> not simple VALUES */ } +=20 + /* + * We don't need to check the targetlist in any great detail, because + * 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. + */ + if (result) + { + ListCell *lcn; +=20 + if (list_length(query->targetList) !=3D list_length(result->eref->colna= mes)) + return NULL; /* this probably cannot happen */ + forboth(lc, query->targetList, lcn, result->eref->colnames) + { + TargetEntry *tle =3D (TargetEntry *) lfirst(lc); + char *cname =3D strVal(lfirst(lcn)); +=20 + if (tle->resjunk) + return NULL; /* this probably cannot happen */ + if (tle->resname =3D=3D NULL || strcmp(tle->resname, cname) !=3D 0) + return NULL; /* column name has been changed */ + } + } +=20 return result; } =20=20 *************** get_from_clause_item(Node *jtnode, Query *** 8517,8523 **** --- 8541,8549 ---- break; case RTE_VALUES: /* Values list RTE */ + appendStringInfoChar(buf, '('); get_values_def(rte->values_lists, context); + appendStringInfoChar(buf, ')'); break; case RTE_CTE: appendStringInfoString(buf, quote_identifier(rte->ctename)); *************** get_from_clause_item(Node *jtnode, Query *** 8559,8564 **** --- 8585,8595 ---- */ printalias =3D true; } + else if (rte->rtekind =3D=3D RTE_VALUES) + { + /* Alias is syntactically required for VALUES */ + printalias =3D true; + } else if (rte->rtekind =3D=3D RTE_CTE) { /* diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expecte= d/rules.out index d50b103..c3e3f09 100644 *** a/src/test/regress/expected/rules.out --- b/src/test/regress/expected/rules.out *************** ALTER RULE "_RETURN" ON rule_v1 RENAME T *** 2678,2680 **** --- 2678,2733 ---- ERROR: renaming an ON SELECT rule is not allowed DROP VIEW rule_v1; DROP TABLE rule_t1; + -- + -- check display of VALUES in view definitions + -- + create view rule_v1 as values(1,2); + \d+ rule_v1 + View "public.rule_v1" + Column | Type | Modifiers | Storage | Description=20 + ---------+---------+-----------+---------+------------- + column1 | integer | | plain |=20 + column2 | integer | | plain |=20 + View definition: + VALUES (1,2); +=20 + drop view rule_v1; + create view rule_v1(x) as values(1,2); + \d+ rule_v1 + View "public.rule_v1" + Column | Type | Modifiers | Storage | Description=20 + ---------+---------+-----------+---------+------------- + x | integer | | plain |=20 + column2 | integer | | plain |=20 + View definition: + SELECT "*VALUES*".column1 AS x, + "*VALUES*".column2 + FROM (VALUES (1,2)) "*VALUES*"; +=20 + drop view rule_v1; + create view rule_v1(x) as select * from (values(1,2)) v; + \d+ rule_v1 + View "public.rule_v1" + Column | Type | Modifiers | Storage | Description=20 + ---------+---------+-----------+---------+------------- + x | integer | | plain |=20 + column2 | integer | | plain |=20 + View definition: + SELECT v.column1 AS x, + v.column2 + FROM ( VALUES (1,2)) v; +=20 + drop view rule_v1; + create view rule_v1(x) as select * from (values(1,2)) v(q,w); + \d+ rule_v1 + View "public.rule_v1" + Column | Type | Modifiers | Storage | Description=20 + --------+---------+-----------+---------+------------- + x | integer | | plain |=20 + w | integer | | plain |=20 + View definition: + SELECT v.q AS x, + v.w + FROM ( VALUES (1,2)) v(q, w); +=20 + drop view rule_v1; diff --git a/src/test/regress/sql/rules.sql b/src/test/regress/sql/rules.sql index 1e15f84..4b3e65e 100644 *** a/src/test/regress/sql/rules.sql --- b/src/test/regress/sql/rules.sql *************** ALTER RULE "_RETURN" ON rule_v1 RENAME T *** 1007,1009 **** --- 1007,1025 ---- =20=20 DROP VIEW rule_v1; DROP TABLE rule_t1; +=20 + -- + -- check display of VALUES in view definitions + -- + create view rule_v1 as values(1,2); + \d+ rule_v1 + drop view rule_v1; + create view rule_v1(x) as values(1,2); + \d+ rule_v1 + drop view rule_v1; + create view rule_v1(x) as select * from (values(1,2)) v; + \d+ rule_v1 + drop view rule_v1; + create view rule_v1(x) as select * from (values(1,2)) v(q,w); + \d+ rule_v1 + drop view rule_v1; ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--