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

Commit 971faef

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 3a07ba1 commit 971faef

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

contrib/postgres_fdw/expected/postgres_fdw.out

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4653,6 +4653,66 @@ INSERT INTO ft1(c1, c2) VALUES(1111, 2);
46534653
UPDATE ft1 SET c2 = c2 + 1 WHERE c1 = 1;
46544654
ALTER FOREIGN TABLE ft1 DROP CONSTRAINT ft1_c2negative;
46554655
-- ===================================================================
4656+
-- test WITH CHECK OPTION constraints
4657+
-- ===================================================================
4658+
CREATE TABLE base_tbl (a int, b int);
4659+
CREATE FOREIGN TABLE foreign_tbl (a int, b int)
4660+
SERVER loopback OPTIONS(table_name 'base_tbl');
4661+
CREATE VIEW rw_view AS SELECT * FROM foreign_tbl
4662+
WHERE a < b WITH CHECK OPTION;
4663+
\d+ rw_view
4664+
View "public.rw_view"
4665+
Column | Type | Modifiers | Storage | Description
4666+
--------+---------+-----------+---------+-------------
4667+
a | integer | | plain |
4668+
b | integer | | plain |
4669+
View definition:
4670+
SELECT foreign_tbl.a,
4671+
foreign_tbl.b
4672+
FROM foreign_tbl
4673+
WHERE foreign_tbl.a < foreign_tbl.b;
4674+
Options: check_option=cascaded
4675+
4676+
INSERT INTO rw_view VALUES (0, 10); -- ok
4677+
INSERT INTO rw_view VALUES (10, 0); -- should fail
4678+
ERROR: new row violates check option for view "rw_view"
4679+
DETAIL: Failing row contains (10, 0).
4680+
EXPLAIN (VERBOSE, COSTS OFF)
4681+
UPDATE rw_view SET b = 20 WHERE a = 0; -- not pushed down
4682+
QUERY PLAN
4683+
--------------------------------------------------------------------------------------------------
4684+
Update on public.foreign_tbl
4685+
Remote SQL: UPDATE public.base_tbl SET b = $2 WHERE ctid = $1
4686+
-> Foreign Scan on public.foreign_tbl
4687+
Output: foreign_tbl.a, 20, foreign_tbl.ctid
4688+
Remote SQL: SELECT a, ctid FROM public.base_tbl WHERE ((a < b)) AND ((a = 0)) FOR UPDATE
4689+
(5 rows)
4690+
4691+
UPDATE rw_view SET b = 20 WHERE a = 0; -- ok
4692+
EXPLAIN (VERBOSE, COSTS OFF)
4693+
UPDATE rw_view SET b = -20 WHERE a = 0; -- not pushed down
4694+
QUERY PLAN
4695+
--------------------------------------------------------------------------------------------------
4696+
Update on public.foreign_tbl
4697+
Remote SQL: UPDATE public.base_tbl SET b = $2 WHERE ctid = $1
4698+
-> Foreign Scan on public.foreign_tbl
4699+
Output: foreign_tbl.a, '-20'::integer, foreign_tbl.ctid
4700+
Remote SQL: SELECT a, ctid FROM public.base_tbl WHERE ((a < b)) AND ((a = 0)) FOR UPDATE
4701+
(5 rows)
4702+
4703+
UPDATE rw_view SET b = -20 WHERE a = 0; -- should fail
4704+
ERROR: new row violates check option for view "rw_view"
4705+
DETAIL: Failing row contains (0, -20).
4706+
SELECT * FROM foreign_tbl;
4707+
a | b
4708+
---+----
4709+
0 | 20
4710+
(1 row)
4711+
4712+
DROP FOREIGN TABLE foreign_tbl CASCADE;
4713+
NOTICE: drop cascades to view rw_view
4714+
DROP TABLE base_tbl;
4715+
-- ===================================================================
46564716
-- test serial columns (ie, sequence-based defaults)
46574717
-- ===================================================================
46584718
create table loc1 (f1 serial, f2 text);

contrib/postgres_fdw/sql/postgres_fdw.sql

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,30 @@ INSERT INTO ft1(c1, c2) VALUES(1111, 2);
842842
UPDATE ft1 SET c2 = c2 + 1 WHERE c1 = 1;
843843
ALTER FOREIGN TABLE ft1 DROP CONSTRAINT ft1_c2negative;
844844

845+
-- ===================================================================
846+
-- test WITH CHECK OPTION constraints
847+
-- ===================================================================
848+
849+
CREATE TABLE base_tbl (a int, b int);
850+
CREATE FOREIGN TABLE foreign_tbl (a int, b int)
851+
SERVER loopback OPTIONS(table_name 'base_tbl');
852+
CREATE VIEW rw_view AS SELECT * FROM foreign_tbl
853+
WHERE a < b WITH CHECK OPTION;
854+
\d+ rw_view
855+
856+
INSERT INTO rw_view VALUES (0, 10); -- ok
857+
INSERT INTO rw_view VALUES (10, 0); -- should fail
858+
EXPLAIN (VERBOSE, COSTS OFF)
859+
UPDATE rw_view SET b = 20 WHERE a = 0; -- not pushed down
860+
UPDATE rw_view SET b = 20 WHERE a = 0; -- ok
861+
EXPLAIN (VERBOSE, COSTS OFF)
862+
UPDATE rw_view SET b = -20 WHERE a = 0; -- not pushed down
863+
UPDATE rw_view SET b = -20 WHERE a = 0; -- should fail
864+
SELECT * FROM foreign_tbl;
865+
866+
DROP FOREIGN TABLE foreign_tbl CASCADE;
867+
DROP TABLE base_tbl;
868+
845869
-- ===================================================================
846870
-- test serial columns (ie, sequence-based defaults)
847871
-- ===================================================================

doc/src/sgml/postgres-fdw.sgml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,10 @@
488488
<filename>postgres_fdw</> attempts to optimize the query execution by
489489
sending the whole query to the remote server if there are no query
490490
<literal>WHERE</> clauses that cannot be sent to the remote server,
491-
no local joins for the query, and no row-level local <literal>BEFORE</> or
492-
<literal>AFTER</> triggers on the target table. In <command>UPDATE</>,
491+
no local joins for the query, no row-level local <literal>BEFORE</> or
492+
<literal>AFTER</> triggers on the target table, and no
493+
<literal>CHECK OPTION</> constraints from parent views.
494+
In <command>UPDATE</>,
493495
expressions to assign to target columns must use only built-in data types,
494496
<literal>IMMUTABLE</> operators, or <literal>IMMUTABLE</> functions,
495497
to reduce the risk of misexecution of the query.

src/backend/optimizer/plan/createplan.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6145,15 +6145,18 @@ make_modifytable(PlannerInfo *root,
61456145
}
61466146

61476147
/*
6148-
* If the target foreign table has any row-level triggers, we can't
6149-
* modify the foreign table directly.
6148+
* Try to modify the foreign table directly if (1) the FDW provides
6149+
* callback functions needed for that, (2) there are no row-level
6150+
* triggers on the foreign table, and (3) there are no WITH CHECK
6151+
* OPTIONs from parent views.
61506152
*/
61516153
direct_modify = false;
61526154
if (fdwroutine != NULL &&
61536155
fdwroutine->PlanDirectModify != NULL &&
61546156
fdwroutine->BeginDirectModify != NULL &&
61556157
fdwroutine->IterateDirectModify != NULL &&
61566158
fdwroutine->EndDirectModify != NULL &&
6159+
withCheckOptionLists == NIL &&
61576160
!has_row_triggers(root, rti, operation))
61586161
direct_modify = fdwroutine->PlanDirectModify(root, node, rti, i);
61596162
if (direct_modify)

0 commit comments

Comments
 (0)