Skip to content

Commit acd3287

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 37fb01c commit acd3287

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
@@ -2012,9 +2012,19 @@ expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
20122012
varattno++;
20132013
Assert(varattno == te->resno);
20142014

2015+
/*
2016+
* In scenarios where columns have been added to a view
2017+
* since the outer query was originally parsed, there can
2018+
* be more items in the subquery tlist than the outer
2019+
* query expects. We should ignore such extra column(s)
2020+
* --- compare the behavior for composite-returning
2021+
* functions, in the RTE_FUNCTION case below.
2022+
*/
2023+
if (!aliasp_item)
2024+
break;
2025+
20152026
if (colnames)
20162027
{
2017-
/* Assume there is one alias per target item */
20182028
char *label = strVal(lfirst(aliasp_item));
20192029

20202030
*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
@@ -2126,6 +2126,91 @@ Foreign-key constraints:
21262126
"check_fk_presence_2_id_fkey" FOREIGN KEY (id) REFERENCES check_fk_presence_1(id)
21272127

21282128
DROP TABLE check_fk_presence_1, check_fk_presence_2;
2129+
-- check column addition within a view (bug #14876)
2130+
create table at_base_table(id int, stuff text);
2131+
insert into at_base_table values (23, 'skidoo');
2132+
create view at_view_1 as select * from at_base_table bt;
2133+
create view at_view_2 as select *, to_json(v1) as j from at_view_1 v1;
2134+
\d+ at_view_1
2135+
View "public.at_view_1"
2136+
Column | Type | Modifiers | Storage | Description
2137+
--------+---------+-----------+----------+-------------
2138+
id | integer | | plain |
2139+
stuff | text | | extended |
2140+
View definition:
2141+
SELECT bt.id,
2142+
bt.stuff
2143+
FROM at_base_table bt;
2144+
2145+
\d+ at_view_2
2146+
View "public.at_view_2"
2147+
Column | Type | Modifiers | Storage | Description
2148+
--------+---------+-----------+----------+-------------
2149+
id | integer | | plain |
2150+
stuff | text | | extended |
2151+
j | json | | extended |
2152+
View definition:
2153+
SELECT v1.id,
2154+
v1.stuff,
2155+
to_json(v1.*) AS j
2156+
FROM at_view_1 v1;
2157+
2158+
explain (verbose, costs off) select * from at_view_2;
2159+
QUERY PLAN
2160+
----------------------------------------------------------
2161+
Seq Scan on public.at_base_table bt
2162+
Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff))
2163+
(2 rows)
2164+
2165+
select * from at_view_2;
2166+
id | stuff | j
2167+
----+--------+----------------------------
2168+
23 | skidoo | {"id":23,"stuff":"skidoo"}
2169+
(1 row)
2170+
2171+
create or replace view at_view_1 as select *, 2+2 as more from at_base_table bt;
2172+
\d+ at_view_1
2173+
View "public.at_view_1"
2174+
Column | Type | Modifiers | Storage | Description
2175+
--------+---------+-----------+----------+-------------
2176+
id | integer | | plain |
2177+
stuff | text | | extended |
2178+
more | integer | | plain |
2179+
View definition:
2180+
SELECT bt.id,
2181+
bt.stuff,
2182+
2 + 2 AS more
2183+
FROM at_base_table bt;
2184+
2185+
\d+ at_view_2
2186+
View "public.at_view_2"
2187+
Column | Type | Modifiers | Storage | Description
2188+
--------+---------+-----------+----------+-------------
2189+
id | integer | | plain |
2190+
stuff | text | | extended |
2191+
j | json | | extended |
2192+
View definition:
2193+
SELECT v1.id,
2194+
v1.stuff,
2195+
to_json(v1.*) AS j
2196+
FROM at_view_1 v1;
2197+
2198+
explain (verbose, costs off) select * from at_view_2;
2199+
QUERY PLAN
2200+
----------------------------------------------------------------
2201+
Seq Scan on public.at_base_table bt
2202+
Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff, NULL))
2203+
(2 rows)
2204+
2205+
select * from at_view_2;
2206+
id | stuff | j
2207+
----+--------+----------------------------------------
2208+
23 | skidoo | {"id":23,"stuff":"skidoo","more":null}
2209+
(1 row)
2210+
2211+
drop view at_view_2;
2212+
drop view at_view_1;
2213+
drop table at_base_table;
21292214
--
21302215
-- lock levels
21312216
--

src/test/regress/sql/alter_table.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,26 @@ ROLLBACK;
13881388
\d check_fk_presence_2
13891389
DROP TABLE check_fk_presence_1, check_fk_presence_2;
13901390

1391+
-- check column addition within a view (bug #14876)
1392+
create table at_base_table(id int, stuff text);
1393+
insert into at_base_table values (23, 'skidoo');
1394+
create view at_view_1 as select * from at_base_table bt;
1395+
create view at_view_2 as select *, to_json(v1) as j from at_view_1 v1;
1396+
\d+ at_view_1
1397+
\d+ at_view_2
1398+
explain (verbose, costs off) select * from at_view_2;
1399+
select * from at_view_2;
1400+
1401+
create or replace view at_view_1 as select *, 2+2 as more from at_base_table bt;
1402+
\d+ at_view_1
1403+
\d+ at_view_2
1404+
explain (verbose, costs off) select * from at_view_2;
1405+
select * from at_view_2;
1406+
1407+
drop view at_view_2;
1408+
drop view at_view_1;
1409+
drop table at_base_table;
1410+
13911411
--
13921412
-- lock levels
13931413
--

0 commit comments

Comments
 (0)