Skip to content

Commit 80e7971

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 adcfa7b commit 80e7971

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

src/backend/parser/parse_relation.c

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

1606+
/*
1607+
* In scenarios where columns have been added to a view
1608+
* since the outer query was originally parsed, there can
1609+
* be more items in the subquery tlist than the outer
1610+
* query expects. We should ignore such extra column(s)
1611+
* --- compare the behavior for composite-returning
1612+
* functions, in the RTE_FUNCTION case below.
1613+
*/
1614+
if (!aliasp_item)
1615+
break;
1616+
16061617
if (colnames)
16071618
{
1608-
/* Assume there is one alias per target item */
16091619
char *label = strVal(lfirst(aliasp_item));
16101620

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

src/test/regress/expected/alter_table.out

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,6 +2070,84 @@ Foreign-key constraints:
20702070
"check_fk_presence_2_id_fkey" FOREIGN KEY (id) REFERENCES check_fk_presence_1(id)
20712071

20722072
DROP TABLE check_fk_presence_1, check_fk_presence_2;
2073+
-- check column addition within a view (bug #14876)
2074+
create table at_base_table(id int, stuff text);
2075+
insert into at_base_table values (23, 'skidoo');
2076+
create view at_view_1 as select * from at_base_table bt;
2077+
create view at_view_2 as select *, v1 as j from at_view_1 v1;
2078+
\d+ at_view_1
2079+
View "public.at_view_1"
2080+
Column | Type | Modifiers | Storage | Description
2081+
--------+---------+-----------+----------+-------------
2082+
id | integer | | plain |
2083+
stuff | text | | extended |
2084+
View definition:
2085+
SELECT bt.id, bt.stuff
2086+
FROM at_base_table bt;
2087+
2088+
\d+ at_view_2
2089+
View "public.at_view_2"
2090+
Column | Type | Modifiers | Storage | Description
2091+
--------+-----------+-----------+----------+-------------
2092+
id | integer | | plain |
2093+
stuff | text | | extended |
2094+
j | at_view_1 | | extended |
2095+
View definition:
2096+
SELECT v1.id, v1.stuff, v1.*::at_view_1 AS j
2097+
FROM at_view_1 v1;
2098+
2099+
explain (verbose, costs off) select * from at_view_2;
2100+
QUERY PLAN
2101+
-------------------------------------------------
2102+
Seq Scan on public.at_base_table bt
2103+
Output: bt.id, bt.stuff, ROW(bt.id, bt.stuff)
2104+
(2 rows)
2105+
2106+
select * from at_view_2;
2107+
id | stuff | j
2108+
----+--------+-------------
2109+
23 | skidoo | (23,skidoo)
2110+
(1 row)
2111+
2112+
create or replace view at_view_1 as select *, 2+2 as more from at_base_table bt;
2113+
\d+ at_view_1
2114+
View "public.at_view_1"
2115+
Column | Type | Modifiers | Storage | Description
2116+
--------+---------+-----------+----------+-------------
2117+
id | integer | | plain |
2118+
stuff | text | | extended |
2119+
more | integer | | plain |
2120+
View definition:
2121+
SELECT bt.id, bt.stuff, 2 + 2 AS more
2122+
FROM at_base_table bt;
2123+
2124+
\d+ at_view_2
2125+
View "public.at_view_2"
2126+
Column | Type | Modifiers | Storage | Description
2127+
--------+-----------+-----------+----------+-------------
2128+
id | integer | | plain |
2129+
stuff | text | | extended |
2130+
j | at_view_1 | | extended |
2131+
View definition:
2132+
SELECT v1.id, v1.stuff, v1.*::at_view_1 AS j
2133+
FROM at_view_1 v1;
2134+
2135+
explain (verbose, costs off) select * from at_view_2;
2136+
QUERY PLAN
2137+
-------------------------------------------------------
2138+
Seq Scan on public.at_base_table bt
2139+
Output: bt.id, bt.stuff, ROW(bt.id, bt.stuff, NULL)
2140+
(2 rows)
2141+
2142+
select * from at_view_2;
2143+
id | stuff | j
2144+
----+--------+--------------
2145+
23 | skidoo | (23,skidoo,)
2146+
(1 row)
2147+
2148+
drop view at_view_2;
2149+
drop view at_view_1;
2150+
drop table at_base_table;
20732151
--
20742152
-- lock levels
20752153
--

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 *, 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)