8000 Ensure that pg_get_ruledef()'s output matches pg_get_viewdef()'s. · satanson/postgres@f579580 · GitHub
[go: up one dir, main page]

Skip to content

Commit f579580

Browse files
< 8000 div class="prc-PageHeader-Description-kFg8r">
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 bef96e5 commit f579580

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
@@ -4081,6 +4081,8 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
40814081
char *ev_qual;
40824082
char *ev_action;
40834083
List *actions = NIL;
4084+
Relation ev_relation;
4085+
TupleDesc viewResultDesc = NULL;
40844086
int fno;
40854087
Datum dat;
40864088
bool isnull;
@@ -4117,6 +4119,8 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
41174119
if (ev_action != NULL)
41184120
actions = (List *) stringToNode(ev_action);
41194121

4122+
ev_relation = heap_open(ev_class, AccessShareLock);
4123+
41204124
/*
41214125
* Build the rules definition text
41224126
*/
@@ -4133,6 +4137,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
41334137
{
41344138
case '1':
41354139
appendStringInfoString(buf, "SELECT");
4140+
viewResultDesc = RelationGetDescr(ev_relation);
41364141
break;
41374142

41384143
case '2':
@@ -4222,7 +4227,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
42224227
foreach(action, actions)
42234228
{
42244229
query = (Query *) lfirst(action);
4225-
get_query_def(query, buf, NIL, NULL,
4230+
get_query_def(query, buf, NIL, viewResultDesc,
42264231
prettyFlags, WRAP_COLUMN_DEFAULT, 0);
42274232
if (prettyFlags)
42284233
appendStringInfoString(buf, ";\n");
@@ -4240,10 +4245,12 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
42404245
Query *query;
42414246

42424247
query = (Query *) linitial(actions);
4243-
get_query_def(query, buf, NIL, NULL,
4248+
get_query_def(query, buf, NIL, viewResultDesc,
42444249
prettyFlags, WRAP_COLUMN_DEFAULT, 0);
42454250
appendStringInfoChar(buf, ';');
42464251
}
4252+
4253+
heap_close(ev_relation, AccessShareLock);
42474254
}
42484255

42494256

@@ -4265,20 +4272,28 @@ make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
42654272
List *actions = NIL;
42664273
Relation ev_relation;
42674274
int fno;
4275+
Datum dat;
42684276
bool isnull;
42694277

42704278
/*
42714279
* Get the attribute values from the rules tuple
42724280
*/
42734281
fno = SPI_fnumber(rulettc, "ev_type");
4274-
ev_type = (char) SPI_getbinval(ruletup, rulettc, fno, &isnull);
4282+
dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
4283+
Assert(!isnull);
4284+
ev_type = DatumGetChar(dat);
42754285

42764286
fno = SPI_fnumber(rulettc, "ev_class");
4277-
ev_class = (Oid) SPI_getbinval(ruletup, rulettc, fno, &isnull);
4287+
dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
4288+
Assert(!isnull);
4289+
ev_class = DatumGetObjectId(dat);
42784290

42794291
fno = SPI_fnumber(rulettc, "is_instead");
4280-
is_instead = (bool) SPI_getbinval(ruletup, rulettc, fno, &isnull);
4292+
dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
4293+
Assert(!isnull);
4294+
is_instead = DatumGetBool(dat);
42814295

4296+
/* these could be nulls */
42824297
fno = SPI_fnumber(rulettc, "ev_qual");
42834298
ev_qual = SPI_getvalue(ruletup, rulettc, fno);
42844299

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)
0