Fix more bugs caused by adding columns to the end of a view.
authorTom Lane
Tue, 7 Mar 2023 23:21:37 +0000 (18:21 -0500)
committerTom Lane
Tue, 7 Mar 2023 23:21:37 +0000 (18:21 -0500)
If a view is defined atop another view, and then CREATE OR REPLACE
VIEW is used to add columns to the lower view, then when the upper
view's referencing RTE is expanded by ApplyRetrieveRule we will have
a subquery RTE with fewer eref->colnames than output columns.  This
confuses various code that assumes those lists are always in sync,
as they are in plain parser output.

We have seen such problems before (cf commit d5b760ecb), and now
I think the time has come to do what was speculated about in that
commit: let's make ApplyRetrieveRule synthesize some column names to
preserve the invariant that holds in parser output.  Otherwise we'll
be chasing this class of bugs indefinitely.  Moreover, it appears from
testing that this actually gives us better results in the test case
d5b760ecb added, and likely in other corner cases that we lack
coverage for.

In HEAD, I replaced d5b760ecb's hack to make expandRTE exit early with
an elog(ERROR) call, since the case is now presumably unreachable.
But it seems like changing that in back branches would bring more risk
than benefit, so there I just updated the comment.

Per bug #17811 from Alexander Lakhin.  Back-patch to all supported
branches.

Discussion: https://postgr.es/m/17811-d31686b78f0dffc9@postgresql.org

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

index 713ec0e521d3b9a863c3b5be161fdc1fa1b90b5c..4193a03ebbaa8b66db0f91387853b484c538d68e 100644 (file)
@@ -2658,12 +2658,17 @@ expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
                    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.
+                    * In a just-parsed subquery RTE, rte->eref->colnames
+                    * should always have exactly as many entries as the
+                    * subquery has non-junk output columns.  However, if the
+                    * subquery RTE was created by expansion of a view,
+                    * perhaps the subquery tlist could now have more entries
+                    * than existed when the outer query was parsed.  Such
+                    * cases should now be prevented because ApplyRetrieveRule
+                    * will extend the colnames list to match.  But out of
+                    * caution, we'll keep the code like this in the back
+                    * branches: just ignore any columns that lack colnames
+                    * entries.
                     */
                    if (!aliasp_item)
                        break;
index 42a6cd465c1e77cefc1dc77c721c8388e4f63574..fa506f9611255346fc9c1deb891596da792f10df 100644 (file)
@@ -26,6 +26,7 @@
 #include "catalog/dependency.h"
 #include "catalog/pg_type.h"
 #include "commands/trigger.h"
+#include "executor/executor.h"
 #include "foreign/fdwapi.h"
 #include "miscadmin.h"
 #include "nodes/makefuncs.h"
@@ -1720,6 +1721,7 @@ ApplyRetrieveRule(Query *parsetree,
    RangeTblEntry *rte,
               *subrte;
    RowMarkClause *rc;
+   int         numCols;
 
    if (list_length(rule->actions) != 1)
        elog(ERROR, "expected just one rule action");
@@ -1879,6 +1881,20 @@ ApplyRetrieveRule(Query *parsetree,
    rte->updatedCols = NULL;
    rte->extraUpdatedCols = NULL;
 
+   /*
+    * Since we allow CREATE OR REPLACE VIEW to add columns to a view, the
+    * rule_action might emit more columns than we expected when the current
+    * query was parsed.  Various places expect rte->eref->colnames to be
+    * consistent with the non-junk output columns of the subquery, so patch
+    * things up if necessary by adding some dummy column names.
+    */
+   numCols = ExecCleanTargetListLength(rule_action->targetList);
+   while (list_length(rte->eref->colnames) < numCols)
+   {
+       rte->eref->colnames = lappend(rte->eref->colnames,
+                                     makeString(pstrdup("?column?")));
+   }
+
    return parsetree;
 }
 
index 2cd9aa4a64e0b9b4b7b904f10029a261f6b6205c..b65389f60c91a740595cb2fa2400062a10b82855 100644 (file)
@@ -2549,22 +2549,51 @@ View definition:
    FROM at_view_1 v1;
 
 explain (verbose, costs off) select * from at_view_2;
-                           QUERY PLAN                           
-----------------------------------------------------------------
+                         QUERY PLAN                          
+-------------------------------------------------------------
  Seq Scan on public.at_base_table bt
-   Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff, NULL))
+   Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff, 4))
 (2 rows)
 
 select * from at_view_2;
- id | stuff  |                   j                    
-----+--------+----------------------------------------
- 23 | skidoo | {"id":23,"stuff":"skidoo","more":null}
+ id | stuff  |                  j                  
+----+--------+-------------------------------------
+ 23 | skidoo | {"id":23,"stuff":"skidoo","more":4}
 (1 row)
 
 drop view at_view_2;
 drop view at_view_1;
 drop table at_base_table;
--- check adding a column not iself requiring a rewrite, together with
+-- related case (bug #17811)
+begin;
+create temp table t1 as select * from int8_tbl;
+create temp view v1 as select 1::int8 as q1;
+create temp view v2 as select * from v1;
+create or replace temp view v1 with (security_barrier = true)
+  as select * from t1;
+create temp table log (q1 int8, q2 int8);
+create rule v1_upd_rule as on update to v1
+  do also insert into log values (new.*);
+update v2 set q1 = q1 + 1 where q1 = 123;
+select * from t1;
+        q1        |        q2         
+------------------+-------------------
+ 4567890123456789 |               123
+ 4567890123456789 |  4567890123456789
+ 4567890123456789 | -4567890123456789
+              124 |               456
+              124 |  4567890123456789
+(5 rows)
+
+select * from log;
+ q1  |        q2        
+-----+------------------
+ 124 |              456
+ 124 | 4567890123456789
+(2 rows)
+
+rollback;
+-- check adding a column not itself requiring a rewrite, together with
 -- a column requiring a default (bug #16038)
 -- ensure that rewrites aren't silently optimized away, removing the
 -- value of the test
index 4e3849da005155e8328b49b9a06d5a67eb083ec4..e848af432a0a05cda6e219ffeb2ba34cf5610f93 100644 (file)
@@ -1636,7 +1636,25 @@ drop view at_view_2;
 drop view at_view_1;
 drop table at_base_table;
 
--- check adding a column not iself requiring a rewrite, together with
+-- related case (bug #17811)
+begin;
+create temp table t1 as select * from int8_tbl;
+create temp view v1 as select 1::int8 as q1;
+create temp view v2 as select * from v1;
+create or replace temp view v1 with (security_barrier = true)
+  as select * from t1;
+
+create temp table log (q1 int8, q2 int8);
+create rule v1_upd_rule as on update to v1
+  do also insert into log values (new.*);
+
+update v2 set q1 = q1 + 1 where q1 = 123;
+
+select * from t1;
+select * from log;
+rollback;
+
+-- check adding a column not itself requiring a rewrite, together with
 -- a column requiring a default (bug #16038)
 
 -- ensure that rewrites aren't silently optimized away, removing the