Skip to content

Commit f0f255a

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 0328bd1 commit f0f255a

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4033,6 +4033,8 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
40334033
char *ev_qual;
40344034
char *ev_action;
40354035
List *actions = NIL;
4036+
Relation ev_relation;
4037+
TupleDesc viewResultDesc = NULL;
40364038
int fno;
40374039
Datum dat;
40384040
bool isnull;
@@ -4069,6 +4071,8 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
40694071
if (ev_action != NULL)
40704072
actions = (List *) stringToNode(ev_action);
40714073

4074+
ev_relation = heap_open(ev_class, AccessShareLock);
4075+
40724076
/*
40734077
* Build the rules definition text
40744078
*/
@@ -4085,6 +4089,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
40854089
{
40864090
case '1':
40874091
appendStringInfoString(buf, "SELECT");
4092+
viewResultDesc = RelationGetDescr(ev_relation);
40884093
break;
40894094

40904095
case '2':
@@ -4173,7 +4178,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
41734178
foreach(action, actions)
41744179
{
41754180
query = (Query *) lfirst(action);
4176-
get_query_def(query, buf, NIL, NULL,
4181+
get_query_def(query, buf, NIL, viewResultDesc,
41774182
prettyFlags, WRAP_COLUMN_DEFAULT, 0);
41784183
if (prettyFlags)
41794184
appendStringInfoString(buf, ";\n");
@@ -4191,10 +4196,12 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
41914196
Query *query;
41924197

41934198
query = (Query *) linitial(actions);
4194-
get_query_def(query, buf, NIL, NULL,
4199+
get_query_def(query, buf, NIL, viewResultDesc,
41954200
prettyFlags, WRAP_COLUMN_DEFAULT, 0);
41964201
appendStringInfoChar(buf, ';');
41974202
}
4203+
4204+
heap_close(ev_relation, AccessShareLock);
41984205
}
41994206

42004207

@@ -4216,20 +4223,28 @@ make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
42164223
List *actions = NIL;
42174224
Relation ev_relation;
42184225
int fno;
4226+
Datum dat;
42194227
bool isnull;
42204228

42214229
/*
42224230
* Get the attribute values from the rules tuple
42234231
*/
42244232
fno = SPI_fnumber(rulettc, "ev_type");
4225-
ev_type = (char) SPI_getbinval(ruletup, rulettc, fno, &isnull);
4233+
dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
4234+
Assert(!isnull);
4235+
ev_type = DatumGetChar(dat);
42264236

42274237
fno = SPI_fnumber(rulettc, "ev_class");
4228-
ev_class = (Oid) SPI_getbinval(ruletup, rulettc, fno, &isnull);
4238+
dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
4239+
Assert(!isnull);
4240+
ev_class = DatumGetObjectId(dat);
42294241

42304242
fno = SPI_fnumber(rulettc, "is_instead");
4231-
is_instead = (bool) SPI_getbinval(ruletup, rulettc, fno, &isnull);
4243+
dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
4244+
Assert(!isnull);
4245+
is_instead = DatumGetBool(dat);
42324246

4247+
/* these could be nulls */
42334248
fno = SPI_fnumber(rulettc, "ev_qual");
42344249
ev_qual = SPI_getvalue(ruletup, rulettc, fno);
42354250

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)