]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix crash when columns have been added to the end of a view.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 27 Oct 2017 21:10:21 +0000 (17:10 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 27 Oct 2017 21:10:21 +0000 (17:10 -0400)
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
ff1ea2173 allowed views to acquire more columns, so back-patch to all
supported branches.

Discussion: https://postgr.es/m/20171026184035.1471.82810@wrigleys.postgresql.org

src/backend/parser/parse_relation.c
src/test/regress/expected/alter_table.out
src/test/regress/sql/alter_table.sql

index c2d1bb5ea68b731e442c735ef777f2287946e2b7..b8307d5cf51f2f2b1b108b227a894ae759b7133d 100644 (file)
@@ -1603,9 +1603,19 @@ expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
                                        varattno++;
                                        Assert(varattno == te->resno);
 
+                                       /*
+                                        * In scenarios where columns have been added to a view
+                                        * since the outer query was originally parsed, there can
+                                        * be more items in the subquery tlist than the outer
+                                        * query expects.  We should ignore such extra column(s)
+                                        * --- compare the behavior for composite-returning
+                                        * functions, in the RTE_FUNCTION case below.
+                                        */
+                                       if (!aliasp_item)
+                                               break;
+
                                        if (colnames)
                                        {
-                                               /* Assume there is one alias per target item */
                                                char       *label = strVal(lfirst(aliasp_item));
 
                                                *colnames = lappend(*colnames, makeString(pstrdup(label)));
index 042cccec9bf95ecdd657008f6d7f17fa19a6466a..d07a2e3a401841256d8bad204c279c06aa2daebb 100644 (file)
@@ -2070,6 +2070,84 @@ Foreign-key constraints:
     "check_fk_presence_2_id_fkey" FOREIGN KEY (id) REFERENCES check_fk_presence_1(id)
 
 DROP TABLE check_fk_presence_1, check_fk_presence_2;
+-- check column addition within a view (bug #14876)
+create table at_base_table(id int, stuff text);
+insert into at_base_table values (23, 'skidoo');
+create view at_view_1 as select * from at_base_table bt;
+create view at_view_2 as select *, v1 as j from at_view_1 v1;
+\d+ at_view_1
+                View "public.at_view_1"
+ Column |  Type   | Modifiers | Storage  | Description 
+--------+---------+-----------+----------+-------------
+ id     | integer |           | plain    | 
+ stuff  | text    |           | extended | 
+View definition:
+ SELECT bt.id, bt.stuff
+   FROM at_base_table bt;
+
+\d+ at_view_2
+                 View "public.at_view_2"
+ Column |   Type    | Modifiers | Storage  | Description 
+--------+-----------+-----------+----------+-------------
+ id     | integer   |           | plain    | 
+ stuff  | text      |           | extended | 
+ j      | at_view_1 |           | extended | 
+View definition:
+ SELECT v1.id, v1.stuff, v1.*::at_view_1 AS j
+   FROM at_view_1 v1;
+
+explain (verbose, costs off) select * from at_view_2;
+                   QUERY PLAN                    
+-------------------------------------------------
+ Seq Scan on public.at_base_table bt
+   Output: bt.id, bt.stuff, ROW(bt.id, bt.stuff)
+(2 rows)
+
+select * from at_view_2;
+ id | stuff  |      j      
+----+--------+-------------
+ 23 | skidoo | (23,skidoo)
+(1 row)
+
+create or replace view at_view_1 as select *, 2+2 as more from at_base_table bt;
+\d+ at_view_1
+                View "public.at_view_1"
+ Column |  Type   | Modifiers | Storage  | Description 
+--------+---------+-----------+----------+-------------
+ id     | integer |           | plain    | 
+ stuff  | text    |           | extended | 
+ more   | integer |           | plain    | 
+View definition:
+ SELECT bt.id, bt.stuff, 2 + 2 AS more
+   FROM at_base_table bt;
+
+\d+ at_view_2
+                 View "public.at_view_2"
+ Column |   Type    | Modifiers | Storage  | Description 
+--------+-----------+-----------+----------+-------------
+ id     | integer   |           | plain    | 
+ stuff  | text      |           | extended | 
+ j      | at_view_1 |           | extended | 
+View definition:
+ SELECT v1.id, v1.stuff, v1.*::at_view_1 AS j
+   FROM at_view_1 v1;
+
+explain (verbose, costs off) select * from at_view_2;
+                      QUERY PLAN                       
+-------------------------------------------------------
+ Seq Scan on public.at_base_table bt
+   Output: bt.id, bt.stuff, ROW(bt.id, bt.stuff, NULL)
+(2 rows)
+
+select * from at_view_2;
+ id | stuff  |      j       
+----+--------+--------------
+ 23 | skidoo | (23,skidoo,)
+(1 row)
+
+drop view at_view_2;
+drop view at_view_1;
+drop table at_base_table;
 --
 -- lock levels
 --
index 59943434415c8f609d5a902e8c1c6141577a029b..10d7186ddb7a44a873ac83da2b86088c7ae30bcd 100644 (file)
@@ -1332,6 +1332,26 @@ ROLLBACK;
 \d check_fk_presence_2
 DROP TABLE check_fk_presence_1, check_fk_presence_2;
 
+-- check column addition within a view (bug #14876)
+create table at_base_table(id int, stuff text);
+insert into at_base_table values (23, 'skidoo');
+create view at_view_1 as select * from at_base_table bt;
+create view at_view_2 as select *, v1 as j from at_view_1 v1;
+\d+ at_view_1
+\d+ at_view_2
+explain (verbose, costs off) select * from at_view_2;
+select * from at_view_2;
+
+create or replace view at_view_1 as select *, 2+2 as more from at_base_table bt;
+\d+ at_view_1
+\d+ at_view_2
+explain (verbose, costs off) select * from at_view_2;
+select * from at_view_2;
+
+drop view at_view_2;
+drop view at_view_1;
+drop table at_base_table;
+
 --
 -- lock levels
 --