Skip to content

Commit b6d6400

Browse files
committed
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
1 parent 9842c1b commit b6d6400

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3885,6 +3885,8 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
38853885
char *ev_qual;
38863886
char *ev_action;
38873887
List *actions = NIL;
3888+
Relation ev_relation;
3889+
TupleDesc viewResultDesc = NULL;
38883890
int fno;
38893891
Datum dat;
38903892
bool isnull;
@@ -3926,6 +3928,8 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
39263928
if (ev_action != NULL)
39273929
actions = (List *) stringToNode(ev_action);
39283930

3931+
ev_relation = heap_open(ev_class, AccessShareLock);
3932+
39293933
/*
39303934
* Build the rules definition text
39313935
*/
@@ -3942,6 +3946,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
39423946
{
39433947
case '1':
39443948
appendStringInfo(buf, "SELECT");
3949+
viewResultDesc = RelationGetDescr(ev_relation);
39453950
break;
39463951

39473952
case '2':
@@ -4034,7 +4039,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
40344039
foreach(action, actions)
40354040
{
40364041
query = (Query *) lfirst(action);
4037-
get_query_def(query, buf, NIL, NULL,
4042+
get_query_def(query, buf, NIL, viewResultDesc,
40384043
prettyFlags, WRAP_COLUMN_DEFAULT, 0);
40394044
if (prettyFlags)
40404045
appendStringInfo(buf, ";\n");
@@ -4052,10 +4057,12 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
40524057
Query *query;
40534058

40544059
query = (Query *) linitial(actions);
4055-
get_query_def(query, buf, NIL, NULL,
4060+
get_query_def(query, buf, NIL, viewResultDesc,
40564061
prettyFlags, WRAP_COLUMN_DEFAULT, 0);
40574062
appendStringInfo(buf, ";");
40584063
}
4064+
4065+
heap_close(ev_relation, AccessShareLock);
40594066
}
40604067

40614068

src/test/regress/expected/create_view.out

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,35 @@ select pg_get_viewdef('tt22v', true);
16191619
LEFT JOIN tt6 ON TRUE;
16201620
(1 row)
16211621

1622+
-- check handling of views with immediately-renamed columns
1623+
create view tt23v (col_a, col_b) as
1624+
select q1 as other_name1, q2 as other_name2 from int8_tbl
1625+
union
1626+
select 42, 43;
1627+
select pg_get_viewdef('tt23v', true);
1628+
pg_get_viewdef
1629+
-------------------------------
1630+
SELECT int8_tbl.q1 AS col_a,+
1631+
int8_tbl.q2 AS col_b +
1632+
FROM int8_tbl +
1633+
UNION +
1634+
SELECT 42 AS col_a, +
1635+
43 AS col_b;
1636+
(1 row)
1637+
1638+
select pg_get_ruledef(oid, true) from pg_rewrite
1639+
where ev_class = 'tt23v'::regclass and ev_type = '1';
1640+
pg_get_ruledef
1641+
-----------------------------------------------------------------
1642+
CREATE RULE "_RETURN" AS +
1643+
ON SELECT TO tt23v DO INSTEAD SELECT int8_tbl.q1 AS col_a,+
1644+
int8_tbl.q2 AS col_b +
1645+
FROM int8_tbl +
1646+
UNION +
1647+
SELECT 42 AS col_a, +
1648+
43 AS col_b;
1649+
(1 row)
1650+
16221651
-- clean up all the random objects we made above
16231652
set client_min_messages = warning;
16241653
DROP SCHEMA temp_view_test CASCADE;

src/test/regress/sql/create_view.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,17 @@ create view tt22v as
541541
select * from tt5 natural left join tt6;
542542
select pg_get_viewdef('tt22v', true);
543543

544+
-- check handling of views with immediately-renamed columns
545+
546+
create view tt23v (col_a, col_b) as
547+
select q1 as other_name1, q2 as other_name2 from int8_tbl
548+
union
549+
select 42, 43;
550+
551+
select pg_get_viewdef('tt23v', true);
552+
select pg_get_ruledef(oid, true) from pg_rewrite
553+
where ev_class = 'tt23v'::regclass and ev_type = '1';
554+
544555
-- clean up all the random objects we made above
545556
set client_min_messages = warning;
546557
DROP SCHEMA temp_view_test CASCADE;

0 commit comments

Comments
 (0)