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

Commit695b34a

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 parent4a94cbd commit695b34a

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

25792579
/*
2580-
* In scenarios where columns have been added to a view
2581-
* since the outer query was originally parsed, there can
2582-
* be more items in the subquery tlist than the outer
2583-
* query expects. We should ignore such extra column(s)
2584-
* --- compare the behavior for composite-returning
2585-
* functions, in the RTE_FUNCTION case below.
2580+
* In a just-parsed subquery RTE, rte->eref->colnames
2581+
* should always have exactly as many entries as the
2582+
* subquery has non-junk output columns. However, if the
2583+
* subquery RTE was created by expansion of a view,
2584+
* perhaps the subquery tlist could now have more entries
2585+
* than existed when the outer query was parsed. Such
2586+
* cases should now be prevented because ApplyRetrieveRule
2587+
* will extend the colnames list to match. But out of
2588+
* caution, we'll keep the code like this in the back
2589+
* branches: just ignore any columns that lack colnames
2590+
* entries.
25862591
*/
25872592
if (!aliasp_item)
25882593
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"
@@ -1668,6 +1669,7 @@ ApplyRetrieveRule(Query *parsetree,
16681669
RangeTblEntry*rte,
16691670
*subrte;
16701671
RowMarkClause*rc;
1672+
intnumCols;
16711673

16721674
if (list_length(rule->actions)!=1)
16731675
elog(ERROR,"expected just one rule action");
@@ -1827,6 +1829,20 @@ ApplyRetrieveRule(Query *parsetree,
18271829
rte->updatedCols=NULL;
18281830
rte->extraUpdatedCols=NULL;
18291831

1832+
/*
1833+
* Since we allow CREATE OR REPLACE VIEW to add columns to a view, the
1834+
* rule_action might emit more columns than we expected when the current
1835+
* query was parsed. Various places expect rte->eref->colnames to be
1836+
* consistent with the non-junk output columns of the subquery, so patch
1837+
* things up if necessary by adding some dummy column names.
1838+
*/
1839+
numCols=ExecCleanTargetListLength(rule_action->targetList);
1840+
while (list_length(rte->eref->colnames)<numCols)
1841+
{
1842+
rte->eref->colnames=lappend(rte->eref->colnames,
1843+
makeString(pstrdup("?column?")));
1844+
}
1845+
18301846
returnparsetree;
18311847
}
18321848

‎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
dropview at_view_1;
16371637
droptable 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 t1asselect*from int8_tbl;
1642+
create temp view v1asselect1::int8as q1;
1643+
create temp view v2asselect*from v1;
1644+
createor replace temp view v1 with (security_barrier= true)
1645+
asselect*from t1;
1646+
1647+
create temp table log (q1 int8, q2 int8);
1648+
createrulev1_upd_ruleasonupdate to v1
1649+
do alsoinsert into logvalues (new.*);
1650+
1651+
update v2set q1= q1+1where 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)

[8]ページ先頭

©2009-2025 Movatter.jp