8000 Fix crash when columns have been added to the end of a view. · koderP/postgres@9d15b8b · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d15b8b

Browse files
committed
Fix crash when columns have been added to the end of a view.
expandRTE() supposed that an RTE_SUBQUERY subquery must have exactly as many non-junk tlist items as the RTE has column aliases for it. This was true at the time the code was written, and is still true so far as parse analysis is concerned --- but when the function is used during planning, the subquery might have appeared through insertion of a view that now has more columns than it did when the outer query was parsed. This results in a core dump if, for instance, we have to expand a whole-row Var that references the subquery. To avoid crashing, we can either stop expanding the RTE when we run out of aliases, or invent new aliases for the added columns. While the latter might be more useful, the former is consistent with what expandRTE() does for composite-returning functions in the RTE_FUNCTION case, so it seems like we'd better do it that way. Per bug #14876 from Samuel Horwitz. This has been busted since commit ff1ea21 allowed views to acquire more columns, so back-patch to all supported branches. Discussion: https://postgr.es/m/20171026184035.1471.82810@wrigleys.postgresql.org
1 parent be203c3 commit 9d15b8b

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed

src/backend/parser/parse_relation.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1709,9 +1709,19 @@ expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
17091709
varattno++;
17101710
Assert(varattno == te->resno);
17111711

1712+
/*
1713+
* In scenarios where columns have been added to a view
1714+
* since the outer query was originally parsed, there can
1715+
* be more items in the subquery tlist than the outer
1716+
* query expects. We should ignore such extra column(s)
1717+
* --- compare the behavior for composite-returning
1718+
* functions, in the RTE_FUNCTION case below.
1719+
*/
1720+
if (!aliasp_item)
1721+
break;
1722+
17121723
if (colnames)
17131724
{
1714-
/* Assume there is one alias per target item */
17151725
char *label = strVal(lfirst(aliasp_item));
17161726

17171727
*colnames = lappend(*colnames, makeString(pstrdup(label)));

src/test/regress/expected/alter_table.out

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,6 +2039,91 @@ Foreign-key constraints:
20392039
"check_fk_presence_2_id_fkey" FOREIGN KEY (id) REFERENCES check_fk_presence_1(id)
20402040

20412041
DROP TABLE check_fk_presence_1, check_fk_presence_2;
2042+
-- check column addition within a view (bug #14876)
2043+
create table at_base_table(id int, stuff text);
2044+
insert into at_base_table values (23, 'skidoo');
2045+
create view at_view_1 as select * from at_base_table bt;
2046+
create view at_view_2 as select *, to_json(v1) as j from at_view_1 v1;
2047+
\d+ at_view_1
2048+
View "public.at_view_1"
2049+
Column | Type | Modifiers | Storage | Description
2050+
--------+---------+-----------+----------+-------------
2051+
id | integer | | plain |
2052+
stuff | text | | extended |
2053+
View definition:
2054+
SELECT bt.id,
2055+
bt.stuff
2056+
FROM at_base_table bt;
2057+
2058+
\d+ at_view_2
2059+
View "public.at_view_2"
2060+
Column | Type | Modifiers | Storage | Description
2061+
--------+---------+-----------+----------+-------------
2062+
id | integer | | plain |
2063+
stuff | text | | extended |
2064+
j | json | | extended |
2065+
View definition:
2066+
SELECT v1.id,
2067+
v1.stuff,
2068+
to_json(v1.*) AS j
2069+
FROM at_view_1 v1;
2070+
2071+
explain (verbose, costs off) select * from at_view_2;
2072+
QUERY PLAN
2073+
----------------------------------------------------------
2074+
Seq Scan on public.at_base_table bt
2075+
Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff))
2076+
(2 rows)
2077+
2078+
select * from at_view_2;
2079+
id | stuff | j
2080+
----+--------+----------------------------
2081+
23 | skidoo | {"id":23,"stuff":"skidoo"}
2082+
(1 row)
2083+
2084+
create or replace view at_view_1 as select *, 2+2 as more from at_base_table bt;
2085+
\d+ at_view_1
2086+
View "public.at_view_1"
2087+
Column | Type | Modifiers | Storage | Description
2088+
--------+---------+-----------+----------+-------------
2089+
id | integer | | plain |
2090+
stuff | text | | extended |
2091+
more | integer | | plain |
2092+
View definition:
2093+
SELECT bt.id,
2094+
bt.stuff,
2095+
2 + 2 AS more
2096+
FROM at_base_table bt;
2097+
2098+
\d+ at_view_2
2099+
View "public.at_view_2"
2100+
Column | Type | Modifiers | Storage | Description
2101+
--------+---------+-----------+----------+-------------
2102+
id | integer | | plain |
2103+
stuff | text | | extended |
2104+
j | json | | extended |
2105+
View definition:
2106+
SELECT v1.id,
2107+
v1.stuff,
2108+
to_json(v1.*) AS j
2109+
FROM at_view_1 v1;
2110+
2111+
explain (verbose, costs off) select * from at_view_2;
2112+
QUERY PLAN
2113+
----------------------------------------------------------------
2114+
Seq Scan on public.at_base_table bt
2115+
Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff, NULL))
2116+
(2 rows)
2117+
2118+
select * from at_view_2;
2119+
id | stuff | j
2120+
----+--------+----------------------------------------
2121+
23 | skidoo | {"id":23,"stuff":"skidoo","more":null}
2122+
(1 row)
2123+
2124+
drop view at_view_2;
2125+
drop view at_view_1;
2126+
drop table at_base_table;
20422127
--
20432128
-- lock levels
20442129
--

src/test/regress/sql/alter_table.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,26 @@ ROLLBACK;
13321332
\d check_fk_presence_2
13331333
DROP TABLE check_fk_presence_1, check_fk_presence_2;
13341334

1335+
-- check column addition within a view (bug #14876)
1336+
create table at_base_table(id int, stuff text);
1337+
insert into at_base_table values (23, 'skidoo');
1338+
create view at_view_1 as select * from at_base_table bt;
1339+
create view at_view_2 as select *, to_json(v1) as j from at_view_1 v1;
1340+
\d+ at_view_1
1341+
\d+ at_view_2
1342+
explain (verbose, costs off) select * from at_view_2;
1343+
select * from at_view_2;
1344+
1345+
create or replace view at_view_1 as select *, 2+2 as more from at_base_table bt;
1346+
\d+ at_view_1
1347+
\d+ at_view_2
1348+
explain (verbose, costs off) select * from at_view_2;
1349+
select * from at_view_2;
1350+
1351+
drop view at_view_2;
1352+
drop view at_view_1;
1353+
drop table at_base_table;
1354+
13351355
--
13361356
-- lock levels
13371357
--

0 commit comments

Comments
 (0)
0