Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7086be6

Browse files
committed
When WCOs are present, disable direct foreign table modification.
If the user modifies a view that has CHECK OPTIONs and this gets translated into a modification to an underlying relation which happens to be a foreign table, the check options should be enforced. In the normal code path, that was happening properly, but it was not working properly for "direct" modification because the whole operation gets pushed to the remote side in that case and we never have an option to enforce the constraint against individual tuples. Fix by disabling direct modification when there is a need to enforce CHECK OPTIONs. Etsuro Fujita, reviewed by Kyotaro Horiguchi and by me. Discussion: http://postgr.es/m/f8a48f54-6f02-9c8a-5250-9791603171ee@lab.ntt.co.jp
1 parent b4af9e3 commit 7086be6

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

contrib/postgres_fdw/expected/postgres_fdw.out

+60
Original file line numberDiff line numberDiff line change
@@ -5894,6 +5894,66 @@ INSERT INTO ft1(c1, c2) VALUES(1111, 2);
58945894
UPDATE ft1 SET c2 = c2 + 1 WHERE c1 = 1;
58955895
ALTER FOREIGN TABLE ft1 DROP CONSTRAINT ft1_c2negative;
58965896
-- ===================================================================
5897+
-- test WITH CHECK OPTION constraints
5898+
-- ===================================================================
5899+
CREATE TABLE base_tbl (a int, b int);
5900+
CREATE FOREIGN TABLE foreign_tbl (a int, b int)
5901+
SERVER loopback OPTIONS(table_name 'base_tbl');
5902+
CREATE VIEW rw_view AS SELECT * FROM foreign_tbl
5903+
WHERE a < b WITH CHECK OPTION;
5904+
\d+ rw_view
5905+
View "public.rw_view"
5906+
Column | Type | Collation | Nullable | Default | Storage | Description
5907+
--------+---------+-----------+----------+---------+---------+-------------
5908+
a | integer | | | | plain |
5909+
b | integer | | | | plain |
5910+
View definition:
5911+
SELECT foreign_tbl.a,
5912+
foreign_tbl.b
5913+
FROM foreign_tbl
5914+
WHERE foreign_tbl.a < foreign_tbl.b;
5915+
Options: check_option=cascaded
5916+
5917+
INSERT INTO rw_view VALUES (0, 10); -- ok
5918+
INSERT INTO rw_view VALUES (10, 0); -- should fail
5919+
ERROR: new row violates check option for view "rw_view"
5920+
DETAIL: Failing row contains (10, 0).
5921+
EXPLAIN (VERBOSE, COSTS OFF)
5922+
UPDATE rw_view SET b = 20 WHERE a = 0; -- not pushed down
5923+
QUERY PLAN
5924+
--------------------------------------------------------------------------------------------------
5925+
Update on public.foreign_tbl
5926+
Remote SQL: UPDATE public.base_tbl SET b = $2 WHERE ctid = $1
5927+
-> Foreign Scan on public.foreign_tbl
5928+
Output: foreign_tbl.a, 20, foreign_tbl.ctid
5929+
Remote SQL: SELECT a, ctid FROM public.base_tbl WHERE ((a < b)) AND ((a = 0)) FOR UPDATE
5930+
(5 rows)
5931+
5932+
UPDATE rw_view SET b = 20 WHERE a = 0; -- ok
5933+
EXPLAIN (VERBOSE, COSTS OFF)
5934+
UPDATE rw_view SET b = -20 WHERE a = 0; -- not pushed down
5935+
QUERY PLAN
5936+
--------------------------------------------------------------------------------------------------
5937+
Update on public.foreign_tbl
5938+
Remote SQL: UPDATE public.base_tbl SET b = $2 WHERE ctid = $1
5939+
-> Foreign Scan on public.foreign_tbl
5940+
Output: foreign_tbl.a, '-20'::integer, foreign_tbl.ctid
5941+
Remote SQL: SELECT a, ctid FROM public.base_tbl WHERE ((a < b)) AND ((a = 0)) FOR UPDATE
5942+
(5 rows)
5943+
5944+
UPDATE rw_view SET b = -20 WHERE a = 0; -- should fail
5945+
ERROR: new row violates check option for view "rw_view"
5946+
DETAIL: Failing row contains (0, -20).
5947+
SELECT * FROM foreign_tbl;
5948+
a | b
5949+
---+----
5950+
0 | 20
5951+
(1 row)
5952+
5953+
DROP FOREIGN TABLE foreign_tbl CASCADE;
5954+
NOTICE: drop cascades to view rw_view
5955+
DROP TABLE base_tbl;
5956+
-- ===================================================================
58975957
-- test serial columns (ie, sequence-based defaults)
58985958
-- ===================================================================
58995959
create table loc1 (f1 serial, f2 text);

contrib/postgres_fdw/sql/postgres_fdw.sql

+24
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,30 @@ INSERT INTO ft1(c1, c2) VALUES(1111, 2);
11801180
UPDATE ft1 SET c2 = c2 + 1 WHERE c1 = 1;
11811181
ALTER FOREIGN TABLE ft1 DROP CONSTRAINT ft1_c2negative;
11821182

1183+
-- ===================================================================
1184+
-- test WITH CHECK OPTION constraints
1185+
-- ===================================================================
1186+
1187+
CREATE TABLE base_tbl (a int, b int);
1188+
CREATE FOREIGN TABLE foreign_tbl (a int, b int)
1189+
SERVER loopback OPTIONS(table_name 'base_tbl');
1190+
CREATE VIEW rw_view AS SELECT * FROM foreign_tbl
1191+
WHERE a < b WITH CHECK OPTION;
1192+
\d+ rw_view
1193+
1194+
INSERT INTO rw_view VALUES (0, 10); -- ok
1195+
INSERT INTO rw_view VALUES (10, 0); -- should fail
1196+
EXPLAIN (VERBOSE, COSTS OFF)
1197+
UPDATE rw_view SET b = 20 WHERE a = 0; -- not pushed down
1198+
UPDATE rw_view SET b = 20 WHERE a = 0; -- ok
1199+
EXPLAIN (VERBOSE, COSTS OFF)
1200+
UPDATE rw_view SET b = -20 WHERE a = 0; -- not pushed down
1201+
UPDATE rw_view SET b = -20 WHERE a = 0; -- should fail
1202+
SELECT * FROM foreign_tbl;
1203+
1204+
DROP FOREIGN TABLE foreign_tbl CASCADE;
1205+
DROP TABLE base_tbl;
1206+
11831207
-- ===================================================================
11841208
-- test serial columns (ie, sequence-based defaults)
11851209
-- ===================================================================

doc/src/sgml/postgres-fdw.sgml

+4-2
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,10 @@
498498
<filename>postgres_fdw</> attempts to optimize the query execution by
499499
sending the whole query to the remote server if there are no query
500500
<literal>WHERE</> clauses that cannot be sent to the remote server,
501-
no local joins for the query, and no row-level local <literal>BEFORE</> or
502-
<literal>AFTER</> triggers on the target table. In <command>UPDATE</>,
501+
no local joins for the query, no row-level local <literal>BEFORE</> or
502+
<literal>AFTER</> triggers on the target table, and no
503+
<literal>CHECK OPTION</> constraints from parent views.
504+
In <command>UPDATE</>,
503505
expressions to assign to target columns must use only built-in data types,
504506
<literal>IMMUTABLE</> operators, or <literal>IMMUTABLE</> functions,
505507
to reduce the risk of misexecution of the query.

src/backend/optimizer/plan/createplan.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -6500,15 +6500,18 @@ make_modifytable(PlannerInfo *root,
65006500
}
65016501

65026502
/*
6503-
* If the target foreign table has any row-level triggers, we can't
6504-
* modify the foreign table directly.
6503+
* Try to modify the foreign table directly if (1) the FDW provides
6504+
* callback functions needed for that, (2) there are no row-level
6505+
* triggers on the foreign table, and (3) there are no WITH CHECK
6506+
* OPTIONs from parent views.
65056507
*/
65066508
direct_modify = false;
65076509
if (fdwroutine != NULL &&
65086510
fdwroutine->PlanDirectModify != NULL &&
65096511
fdwroutine->BeginDirectModify != NULL &&
65106512
fdwroutine->IterateDirectModify != NULL &&
65116513
fdwroutine->EndDirectModify != NULL &&
6514+
withCheckOptionLists == NIL &&
65126515
!has_row_triggers(root, rti, operation))
65136516
direct_modify = fdwroutine->PlanDirectModify(root, node, rti, i);
65146517
if (direct_modify)

0 commit comments

Comments
 (0)