Skip to content

Commit 9f1e51b

Browse files
committed
Fix more bugs caused by adding columns to the end of a view.
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 d5b760e), 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 d5b760e added, and likely in other corner cases that we lack coverage for. In HEAD, I replaced d5b760e'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
1 parent 1e05ea5 commit 9f1e51b

File tree

4 files changed

+82
-14
lines changed

4 files changed

+82
-14
lines changed

src/backend/parser/parse_relation.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2658,12 +2658,17 @@ expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
26582658
Assert(varattno == te->resno);
26592659

26602660
/*
2661-
* In scenarios where columns have been added to a view
2662-
* since the outer query was originally parsed, there can
2663-
* be more items in the subquery tlist than the outer
2664-
* query expects. We should ignore such extra column(s)
2665-
* --- compare the behavior for composite-returning
2666-
* functions, in the RTE_FUNCTION case below.
2661+
* In a just-parsed subquery RTE, rte->eref->colnames
2662+
* should always have exactly as many entries as the
2663+
* subquery has non-junk output columns. However, if the
2664+
* subquery RTE was created by expansion of a view,
2665+
* perhaps the subquery tlist could now have more entries
2666+
* than existed when the outer query was parsed. Such
2667+
* cases should now be prevented because ApplyRetrieveRule
2668+
* will extend the colnames list to match. But out of
2669+
* caution, we'll keep the code like this in the back
2670+
* branches: just ignore any columns that lack colnames
2671+
* entries.
26672672
*/
26682673
if (!aliasp_item)
26692674
break;

src/backend/rewrite/rewriteHandler.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "catalog/dependency.h"
2727
#include "catalog/pg_type.h"
2828
#include "commands/trigger.h"
29+
#include "executor/executor.h"
2930
#include "foreign/fdwapi.h"
3031
#include "miscadmin.h"
3132
#include "nodes/makefuncs.h"
@@ -1720,6 +1721,7 @@ ApplyRetrieveRule(Query *parsetree,
17201721
RangeTblEntry *rte,
17211722
*subrte;
17221723
RowMarkClause *rc;
1724+
int numCols;
17231725

17241726
if (list_length(rule->actions) != 1)
17251727
elog(ERROR, "expected just one rule action");
@@ -1879,6 +1881,20 @@ ApplyRetrieveRule(Query *parsetree,
18791881
rte->updatedCols = NULL;
18801882
rte->extraUpdatedCols = NULL;
18811883

1884+
/*
1885+
* Since we allow CREATE OR REPLACE VIEW to add columns to a view, the
1886+
* rule_action might emit more columns than we expected when the current
1887+
* query was parsed. Various places expect rte->eref->colnames to be
1888+
* consistent with the non-junk output columns of the subquery, so patch
1889+
* things up if necessary by adding some dummy column names.
1890+
*/
1891+
numCols = ExecCleanTargetListLength(rule_action->targetList);
1892+
while (list_length(rte->eref->colnames) < numCols)
1893+
{
1894+
rte->eref->colnames = lappend(rte->eref->colnames,
1895+
makeString(pstrdup("?column?")));
1896+
}
1897+
18821898
return parsetree;
18831899
}
18841900

src/test/regress/expected/alter_table.out

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2549,22 +2549,51 @@ View definition:
25492549
FROM at_view_1 v1;
25502550

25512551
explain (verbose, costs off) select * from at_view_2;
2552-
QUERY PLAN
2553-
----------------------------------------------------------------
2552+
QUERY PLAN
2553+
-------------------------------------------------------------
25542554
Seq Scan on public.at_base_table bt
2555-
Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff, NULL))
2555+
Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff, 4))
25562556
(2 rows)
25572557

25582558
select * from at_view_2;
2559-
id | stuff | j
2560-
----+--------+----------------------------------------
2561-
23 | skidoo | {"id":23,"stuff":"skidoo","more":null}
2559+
id | stuff | j
2560+
----+--------+-------------------------------------
2561+
23 | skidoo | {"id":23,"stuff":"skidoo","more":4}
25622562
(1 row)
25632563

25642564
drop view at_view_2;
25652565
drop view at_view_1;
25662566
drop table at_base_table;
2567-
-- check adding a column not iself requiring a rewrite, together with
2567+
-- related case (bug #17811)
2568+
begin;
2569+
create temp table t1 as select * from int8_tbl;
2570+
create temp view v1 as select 1::int8 as q1;
2571+
create temp view v2 as select * from v1;
2572+
create or replace temp view v1 with (security_barrier = true)
2573+
as select * from t1;
2574+
create temp table log (q1 int8, q2 int8);
2575+
create rule v1_upd_rule as on update to v1
2576+
do also insert into log values (new.*);
2577+
update v2 set q1 = q1 + 1 where q1 = 123;
2578+
select * from t1;
2579+
q1 | q2
2580+
------------------+-------------------
2581+
4567890123456789 | 123
2582+
4567890123456789 | 4567890123456789
2583+
4567890123456789 | -4567890123456789
2584+
124 | 456
2585+
124 | 4567890123456789
2586+
(5 rows)
2587+
2588+
select * from log;
2589+
q1 | q2
2590+
-----+------------------
2591+
124 | 456
2592+
124 | 4567890123456789
2593+
(2 rows)
2594+
2595+
rollback;
2596+
-- check adding a column not itself requiring a rewrite, together with
25682597
-- a column requiring a default (bug #16038)
25692598
-- ensure that rewrites aren't silently optimized away, removing the
25702599
-- value of the test

src/test/regress/sql/alter_table.sql

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,25 @@ drop view at_view_2;
16361636
drop view at_view_1;
16371637
drop table at_base_table;
16381638

1639-
-- check adding a column not iself requiring a rewrite, together with
1639+
-- related case (bug #17811)
1640+
begin;
1641+
create temp table t1 as select * from int8_tbl;
1642+
create temp view v1 as select 1::int8 as q1;
1643+
create temp view v2 as select * from v1;
1644+
create or replace temp view v1 with (security_barrier = true)
1645+
as select * from t1;
1646+
1647+
create temp table log (q1 int8, q2 int8);
1648+
create rule v1_upd_rule as on update to v1
1649+
do also insert into log values (new.*);
1650+
1651+
update v2 set q1 = q1 + 1 where q1 = 123;
1652+
1653+
select * from t1;
1654+
select * from log;
1655+
rollback;
1656+
1657+
-- check adding a column not itself requiring a rewrite, together with
16401658
-- a column requiring a default (bug #16038)
16411659

16421660
-- ensure that rewrites aren't silently optimized away, removing the

0 commit comments

Comments
 (0)