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

Commit ddde3b4

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 8be1022 commit ddde3b4

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
@@ -2204,9 +2204,19 @@ expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
22042204
varattno++;
22052205
Assert(varattno == te->resno);
22062206

2207+
/*
2208+
* In scenarios where columns have been added to a view
2209+
* since the outer query was originally parsed, there can
2210+
* be more items in the subquery tlist than the outer
2211+
* query expects. We should ignore such extra column(s)
2212+
* --- compare the behavior for composite-returning
2213+
* functions, in the RTE_FUNCTION case below.
2214+
*/
2215+
if (!aliasp_item)
2216+
break;
2217+
22072218
if (colnames)
22082219
{
2209-
/* Assume there is one alias per target item */
22102220
char *label = strVal(lfirst(aliasp_item));
22112221

22122222
*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
@@ -2156,6 +2156,91 @@ Foreign-key constraints:
21562156
"check_fk_presence_2_id_fkey" FOREIGN KEY (id) REFERENCES check_fk_presence_1(id)
21572157

21582158
DROP TABLE check_fk_presence_1, check_fk_presence_2;
2159+
-- check column addition within a view (bug #14876)
2160+
create table at_base_table(id int, stuff text);
2161+
insert into at_base_table values (23, 'skidoo');
2162+
create view at_view_1 as select * from at_base_table bt;
2163+
create view at_view_2 as select *, to_json(v1) as j from at_view_1 v1;
2164+
\d+ at_view_1
2165+
View "public.at_view_1"
2166+
Column | Type | Collation | Nullable | Default | Storage | Description
2167+
--------+---------+-----------+----------+---------+----------+-------------
2168+
id | integer | | | | plain |
2169+
stuff | text | | | | extended |
2170+
View definition:
2171+
SELECT bt.id,
2172+
bt.stuff
2173+
FROM at_base_table bt;
2174+
2175+
\d+ at_view_2
2176+
View "public.at_view_2"
2177+
Column | Type | Collation | Nullable | Default | Storage | Description
2178+
--------+---------+-----------+----------+---------+----------+-------------
2179+
id | integer | | | | plain |
2180+
stuff | text | | | | extended |
2181+
j | json | | | | extended |
2182+
View definition:
2183+
SELECT v1.id,
2184+
v1.stuff,
2185+
to_json(v1.*) AS j
2186+
FROM at_view_1 v1;
2187+
2188+
explain (verbose, costs off) select * from at_view_2;
2189+
QUERY PLAN
2190+
----------------------------------------------------------
2191+
Seq Scan on public.at_base_table bt
2192+
Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff))
2193+
(2 rows)
2194+
2195+
select * from at_view_2;
2196+
id | stuff | j
2197+
----+--------+----------------------------
2198+
23 | skidoo | {"id":23,"stuff":"skidoo"}
2199+
(1 row)
2200+
2201+
create or replace view at_view_1 as select *, 2+2 as more from at_base_table bt;
2202+
\d+ at_view_1
2203+
View "public.at_view_1"
2204+
Column | Type | Collation | Nullable | Default | Storage | Description
2205+
--------+---------+-----------+----------+---------+----------+-------------
2206+
id | integer | | | | plain |
2207+
stuff | text | | | | extended |
2208+
more | integer | | | | plain |
2209+
View definition:
2210+
SELECT bt.id,
2211+
bt.stuff,
2212+
2 + 2 AS more
2213+
FROM at_base_table bt;
2214+
2215+
\d+ at_view_2
2216+
View "public.at_view_2"
2217+
Column | Type | Collation | Nullable | Default | Storage | Description
2218+
--------+---------+-----------+----------+---------+----------+-------------
2219+
id | integer | | | | plain |
2220+
stuff | text | | | | extended |
2221+
j | json | | | | extended |
2222+
View definition:
2223+
SELECT v1.id,
2224+
v1.stuff,
2225+
to_json(v1.*) AS j
2226+
FROM at_view_1 v1;
2227+
2228+
explain (verbose, costs off) select * from at_view_2;
2229+
QUERY PLAN
2230+
----------------------------------------------------------------
2231+
Seq Scan on public.at_base_table bt
2232+
Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff, NULL))
2233+
(2 rows)
2234+
2235+
select * from at_view_2;
2236+
id | stuff | j
2237+
----+--------+----------------------------------------
2238+
23 | skidoo | {"id":23,"stuff":"skidoo","more":null}
2239+
(1 row)
2240+
2241+
drop view at_view_2;
2242+
drop view at_view_1;
2243+
drop table at_base_table;
21592244
--
21602245
-- lock levels
21612246
--

src/test/regress/sql/alter_table.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,26 @@ ROLLBACK;
14021402
\d check_fk_presence_2
14031403
DROP TABLE check_fk_presence_1, check_fk_presence_2;
14041404

1405+
-- check column addition within a view (bug #14876)
1406+
create table at_base_table(id int, stuff text);
1407+
insert into at_base_table values (23, 'skidoo');
1408+
create view at_view_1 as select * from at_base_table bt;
1409+
create view at_view_2 as select *, to_json(v1) as j from at_view_1 v1;
1410+
\d+ at_view_1
1411+
\d+ at_view_2
1412+
explain (verbose, costs off) select * from at_view_2;
1413+
select * from at_view_2;
1414+
1415+
create or replace view at_view_1 as select *, 2+2 as more from at_base_table bt;
1416+
\d+ at_view_1
1417+
\d+ at_view_2
1418+
explain (verbose, costs off) select * from at_view_2;
1419+
select * from at_view_2;
1420+
1421+
drop view at_view_2;
1422+
drop view at_view_1;
1423+
drop table at_base_table;
1424+
14051425
--
14061426
-- lock levels
14071427
--

0 commit comments

Comments
 (0)