From b6d64004765710eab097c40e76bb689b464643ef Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 24 Jul 2017 15:16:31 -0400 Subject: Ensure that pg_get_ruledef()'s output matches pg_get_viewdef()'s. 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 | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/backend/utils/adt/ruleutils.c') diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 6ae5c34ab9c..9d1a9e8c8ac 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -3885,6 +3885,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; @@ -3926,6 +3928,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 */ @@ -3942,6 +3946,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc, { case '1': appendStringInfo(buf, "SELECT"); + viewResultDesc = RelationGetDescr(ev_relation); break; case '2': @@ -4034,7 +4039,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"); @@ -4052,10 +4057,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); } -- cgit v1.2.3