Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3a07ba1

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 bcc2c3b commit 3a07ba1

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
@@ -4220,6 +4220,8 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
42204220
char *ev_qual;
42214221
char *ev_action;
42224222
List *actions = NIL;
4223+
Relation ev_relation;
4224+
TupleDesc viewResultDesc = NULL;
42234225
int fno;
42244226
Datum dat;
42254227
bool isnull;
@@ -4256,6 +4258,8 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
42564258
if (ev_action != NULL)
42574259
actions = (List *) stringToNode(ev_action);
42584260

4261+
ev_relation = heap_open(ev_class, AccessShareLock);
4262+
42594263
/*
42604264
* Build the rules definition text
42614265
*/
@@ -4272,6 +4276,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
42724276
{
42734277
case '1':
42744278
appendStringInfoString(buf, "SELECT");
4279+
viewResultDesc = RelationGetDescr(ev_relation);
42754280
break;
42764281

42774282
case '2':
@@ -4361,7 +4366,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
43614366
foreach(action, actions)
43624367
{
43634368
query = (Query *) lfirst(action);
4364-
get_query_def(query, buf, NIL, NULL,
4369+
get_query_def(query, buf, NIL, viewResultDesc,
43654370
prettyFlags, WRAP_COLUMN_DEFAULT, 0);
43664371
if (prettyFlags)
43674372
appendStringInfoString(buf, ";\n");
@@ -4379,10 +4384,12 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
43794384
Query *query;
43804385

43814386
query = (Query *) linitial(actions);
4382-
get_query_def(query, buf, NIL, NULL,
4387+
get_query_def(query, buf, NIL, viewResultDesc,
43834388
prettyFlags, WRAP_COLUMN_DEFAULT, 0);
43844389
appendStringInfoChar(buf, ';');
43854390
}
4391+
4392+
heap_close(ev_relation, AccessShareLock);
43864393
}
43874394

43884395

@@ -4404,20 +4411,28 @@ make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
44044411
List *actions = NIL;
44054412
Relation ev_relation;
44064413
int fno;
4414+
Datum dat;
44074415
bool isnull;
44084416

44094417
/*
44104418
* Get the attribute values from the rules tuple
44114419
*/
44124420
fno = SPI_fnumber(rulettc, "ev_type");
4413-
ev_type = (char) SPI_getbinval(ruletup, rulettc, fno, &isnull);
4421+
dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
4422+
Assert(!isnull);
4423+
ev_type = DatumGetChar(dat);
44144424

44154425
fno = SPI_fnumber(rulettc, "ev_class");
4416-
ev_class = (Oid) SPI_getbinval(ruletup, rulettc, fno, &isnull);
4426+
dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
4427+
Assert(!isnull);
4428+
ev_class = DatumGetObjectId(dat);
44174429

44184430
fno = SPI_fnumber(rulettc, "is_instead");
4419-
is_instead = (bool) SPI_getbinval(ruletup, rulettc, fno, &isnull);
4431+
dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
4432+
Assert(!isnull);
4433+
is_instead = DatumGetBool(dat);
44204434

4435+
/* these could be nulls */
44214436
fno = SPI_fnumber(rulettc, "ev_qual");
44224437
ev_qual = SPI_getvalue(ruletup, rulettc, fno);
44234438

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)