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

Commit76d2177

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 REPLACEVIEW is used to add columns to the lower view, then when the upperview's referencing RTE is expanded by ApplyRetrieveRule we will havea subquery RTE with fewer eref->colnames than output columns. Thisconfuses various code that assumes those lists are always in sync,as they are in plain parser output.We have seen such problems before (cf commitd5b760e), and nowI think the time has come to do what was speculated about in thatcommit: let's make ApplyRetrieveRule synthesize some column names topreserve the invariant that holds in parser output. Otherwise we'llbe chasing this class of bugs indefinitely. Moreover, it appears fromtesting that this actually gives us better results in the test cased5b760e added, and likely in other corner cases that we lackcoverage for.In HEAD, I replacedd5b760e's hack to make expandRTE exit early withan elog(ERROR) call, since the case is now presumably unreachable.But it seems like changing that in back branches would bring more riskthan benefit, so there I just updated the comment.Per bug #17811 from Alexander Lakhin. Back-patch to all supportedbranches.Discussion:https://postgr.es/m/17811-d31686b78f0dffc9@postgresql.org
1 parentae48601 commit76d2177

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
@@ -2672,12 +2672,17 @@ expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
26722672
Assert(varattno==te->resno);
26732673

26742674
/*
2675-
* In scenarios where columns have been added to a view
2676-
* since the outer query was originally parsed, there can
2677-
* be more items in the subquery tlist than the outer
2678-
* query expects. We should ignore such extra column(s)
2679-
* --- compare the behavior for composite-returning
2680-
* functions, in the RTE_FUNCTION case below.
2675+
* In a just-parsed subquery RTE, rte->eref->colnames
2676+
* should always have exactly as many entries as the
2677+
* subquery has non-junk output columns. However, if the
2678+
* subquery RTE was created by expansion of a view,
2679+
* perhaps the subquery tlist could now have more entries
2680+
* than existed when the outer query was parsed. Such
2681+
* cases should now be prevented because ApplyRetrieveRule
2682+
* will extend the colnames list to match. But out of
2683+
* caution, we'll keep the code like this in the back
2684+
* branches: just ignore any columns that lack colnames
2685+
* entries.
26812686
*/
26822687
if (!aliasp_item)
26832688
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"
@@ -1724,6 +1725,7 @@ ApplyRetrieveRule(Query *parsetree,
17241725
RangeTblEntry*rte,
17251726
*subrte;
17261727
RowMarkClause*rc;
1728+
intnumCols;
17271729

17281730
if (list_length(rule->actions)!=1)
17291731
elog(ERROR,"expected just one rule action");
@@ -1883,6 +1885,20 @@ ApplyRetrieveRule(Query *parsetree,
18831885
rte->updatedCols=NULL;
18841886
rte->extraUpdatedCols=NULL;
18851887

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

‎src/test/regress/expected/alter_table.out

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

25492549
explain (verbose, costs off) select * from at_view_2;
2550-
QUERY PLAN
2551-
----------------------------------------------------------------
2550+
QUERY PLAN
2551+
-------------------------------------------------------------
25522552
Seq Scan on public.at_base_table bt
2553-
Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff,NULL))
2553+
Output: bt.id, bt.stuff, to_json(ROW(bt.id, bt.stuff,4))
25542554
(2 rows)
25552555

25562556
select * from at_view_2;
2557-
id | stuff | j
2558-
----+--------+----------------------------------------
2559-
23 | skidoo | {"id":23,"stuff":"skidoo","more":null}
2557+
id | stuff |j
2558+
----+--------+-------------------------------------
2559+
23 | skidoo | {"id":23,"stuff":"skidoo","more":4}
25602560
(1 row)
25612561

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

1635-
-- check adding a column not iself requiring a rewrite, together with
1635+
-- related case (bug #17811)
1636+
begin;
1637+
create temp table t1asselect*from int8_tbl;
1638+
create temp view v1asselect1::int8as q1;
1639+
create temp view v2asselect*from v1;
1640+
createor replace temp view v1 with (security_barrier= true)
1641+
asselect*from t1;
1642+
1643+
create temp table log (q1 int8, q2 int8);
1644+
createrulev1_upd_ruleasonupdate to v1
1645+
do alsoinsert into logvalues (new.*);
1646+
1647+
update v2set q1= q1+1where q1=123;
1648+
1649+
select*from t1;
1650+
select*from log;
1651+
rollback;
1652+
1653+
-- check adding a column not itself requiring a rewrite, together with
16361654
-- a column requiring a default (bug #16038)
16371655

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp