Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd5b760e

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 exactlyas 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 sofar as parse analysis is concerned --- but when the function is usedduring planning, the subquery might have appeared through insertionof a view that now has more columns than it did when the outer querywas parsed. This results in a core dump if, for instance, we haveto expand a whole-row Var that references the subquery.To avoid crashing, we can either stop expanding the RTE when we runout of aliases, or invent new aliases for the added columns. Whilethe latter might be more useful, the former is consistent with whatexpandRTE() does for composite-returning functions in the RTE_FUNCTIONcase, so it seems like we'd better do it that way.Per bug #14876 from Samuel Horwitz. This has been busted since commitff1ea21 allowed views to acquire more columns, so back-patch to allsupported branches.Discussion:https://postgr.es/m/20171026184035.1471.82810@wrigleys.postgresql.org
1 parent682ce91 commitd5b760e

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

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

22132223
*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
@@ -2180,6 +2180,91 @@ Foreign-key constraints:
21802180
"check_fk_presence_2_id_fkey" FOREIGN KEY (id) REFERENCES check_fk_presence_1(id)
21812181

21822182
DROP TABLE check_fk_presence_1, check_fk_presence_2;
2183+
-- check column addition within a view (bug #14876)
2184+
create table at_base_table(id int, stuff text);
2185+
insert into at_base_table values (23, 'skidoo');
2186+
create view at_view_1 as select * from at_base_table bt;
2187+
create view at_view_2 as select *, to_json(v1) as j from at_view_1 v1;
2188+
\d+ at_view_1
2189+
View "public.at_view_1"
2190+
Column | Type | Collation | Nullable | Default | Storage | Description
2191+
--------+---------+-----------+----------+---------+----------+-------------
2192+
id | integer | | | | plain |
2193+
stuff | text | | | | extended |
2194+
View definition:
2195+
SELECT bt.id,
2196+
bt.stuff
2197+
FROM at_base_table bt;
2198+
2199+
\d+ at_view_2
2200+
View "public.at_view_2"
2201+
Column | Type | Collation | Nullable | Default | Storage | Description
2202+
--------+---------+-----------+----------+---------+----------+-------------
2203+
id | integer | | | | plain |
2204+
stuff | text | | | | extended |
2205+
j | json | | | | extended |
2206+
View definition:
2207+
SELECT v1.id,
2208+
v1.stuff,
2209+
to_json(v1.*) AS j
2210+
FROM at_view_1 v1;
2211+
2212+
explain (verbose, costs off) select * from at_view_2;
2213+
QUERY PLAN
2214+
----------------------------------------------------------
2215+
Seq Scan on public.at_base_table bt
2216+
Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff))
2217+
(2 rows)
2218+
2219+
select * from at_view_2;
2220+
id | stuff | j
2221+
----+--------+----------------------------
2222+
23 | skidoo | {"id":23,"stuff":"skidoo"}
2223+
(1 row)
2224+
2225+
create or replace view at_view_1 as select *, 2+2 as more from at_base_table bt;
2226+
\d+ at_view_1
2227+
View "public.at_view_1"
2228+
Column | Type | Collation | Nullable | Default | Storage | Description
2229+
--------+---------+-----------+----------+---------+----------+-------------
2230+
id | integer | | | | plain |
2231+
stuff | text | | | | extended |
2232+
more | integer | | | | plain |
2233+
View definition:
2234+
SELECT bt.id,
2235+
bt.stuff,
2236+
2 + 2 AS more
2237+
FROM at_base_table bt;
2238+
2239+
\d+ at_view_2
2240+
View "public.at_view_2"
2241+
Column | Type | Collation | Nullable | Default | Storage | Description
2242+
--------+---------+-----------+----------+---------+----------+-------------
2243+
id | integer | | | | plain |
2244+
stuff | text | | | | extended |
2245+
j | json | | | | extended |
2246+
View definition:
2247+
SELECT v1.id,
2248+
v1.stuff,
2249+
to_json(v1.*) AS j
2250+
FROM at_view_1 v1;
2251+
2252+
explain (verbose, costs off) select * from at_view_2;
2253+
QUERY PLAN
2254+
----------------------------------------------------------------
2255+
Seq Scan on public.at_base_table bt
2256+
Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff, NULL))
2257+
(2 rows)
2258+
2259+
select * from at_view_2;
2260+
id | stuff | j
2261+
----+--------+----------------------------------------
2262+
23 | skidoo | {"id":23,"stuff":"skidoo","more":null}
2263+
(1 row)
2264+
2265+
drop view at_view_2;
2266+
drop view at_view_1;
2267+
drop table at_base_table;
21832268
--
21842269
-- lock levels
21852270
--

‎src/test/regress/sql/alter_table.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,26 @@ ROLLBACK;
14181418
\d check_fk_presence_2
14191419
DROPTABLE check_fk_presence_1, check_fk_presence_2;
14201420

1421+
-- check column addition within a view (bug #14876)
1422+
createtableat_base_table(idint, stufftext);
1423+
insert into at_base_tablevalues (23,'skidoo');
1424+
createviewat_view_1asselect*from at_base_table bt;
1425+
createviewat_view_2asselect*, to_json(v1)as jfrom at_view_1 v1;
1426+
\d+ at_view_1
1427+
\d+ at_view_2
1428+
explain (verbose, costs off)select*from at_view_2;
1429+
select*from at_view_2;
1430+
1431+
create or replaceviewat_view_1asselect*,2+2as morefrom at_base_table bt;
1432+
\d+ at_view_1
1433+
\d+ at_view_2
1434+
explain (verbose, costs off)select*from at_view_2;
1435+
select*from at_view_2;
1436+
1437+
dropview at_view_2;
1438+
dropview at_view_1;
1439+
droptable at_base_table;
1440+
14211441
--
14221442
-- lock levels
14231443
--

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp