1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

postgres_fdw: Account for triggers in non-direct remote UPDATE planning.

Previously, in postgresPlanForeignModify, we planned an UPDATE operation
on a foreign table so that we transmit only columns that were explicitly
targets of the UPDATE, so as to avoid unnecessary data transmission, but
if there were BEFORE ROW UPDATE triggers on the foreign table, those
triggers might change values for non-target columns, in which case we
would miss sending changed values for those columns.  Prevent optimizing
away transmitting all columns if there are BEFORE ROW UPDATE triggers on
the foreign table.

This is an oversight in commit 7cbe57c34 which added triggers on foreign
tables, so apply the patch all the way back to 9.4 where that came in.

Author: Shohei Mochizuki
Reviewed-by: Amit Langote
Discussion: https://postgr.es/m/201905270152.x4R1q3qi014550@toshiba.co.jp
This commit is contained in:
Etsuro Fujita
2019-06-13 17:59:17 +09:00
parent df88137a39
commit 1a3d9f62c1
3 changed files with 37 additions and 6 deletions

View File

@ -3063,6 +3063,25 @@ SELECT * from loc1;
2 | skidoo triggered ! 2 | skidoo triggered !
(2 rows) (2 rows)
EXPLAIN (verbose, costs off)
UPDATE rem1 set f1 = 10; -- all columns should be transmitted
QUERY PLAN
-----------------------------------------------------------------------
Update on public.rem1
Remote SQL: UPDATE public.loc1 SET f1 = $2, f2 = $3 WHERE ctid = $1
-> Foreign Scan on public.rem1
Output: 10, f2, ctid, rem1.*
Remote SQL: SELECT f1, f2, ctid FROM public.loc1 FOR UPDATE
(5 rows)
UPDATE rem1 set f1 = 10;
SELECT * from loc1;
f1 | f2
----+--------------------------------
10 | skidoo triggered ! triggered !
10 | skidoo triggered ! triggered !
(2 rows)
DELETE FROM rem1; DELETE FROM rem1;
-- Add a second trigger, to check that the changes are propagated correctly -- Add a second trigger, to check that the changes are propagated correctly
-- from trigger to trigger -- from trigger to trigger
@ -3175,6 +3194,6 @@ NOTICE: trig_row_after(23, skidoo) AFTER ROW INSERT ON rem1
NOTICE: NEW: (13,"test triggered !") NOTICE: NEW: (13,"test triggered !")
ctid ctid
-------- --------
(0,27) (0,29)
(1 row) (1 row)

View File

@ -1176,12 +1176,19 @@ postgresPlanForeignModify(PlannerInfo *root,
/* /*
* In an INSERT, we transmit all columns that are defined in the foreign * In an INSERT, we transmit all columns that are defined in the foreign
* table. In an UPDATE, we transmit only columns that were explicitly * table. In an UPDATE, if there are BEFORE ROW UPDATE triggers on the
* targets of the UPDATE, so as to avoid unnecessary data transmission. * foreign table, we transmit all columns like INSERT; else we transmit
* (We can't do that for INSERT since we would miss sending default values * only columns that were explicitly targets of the UPDATE, so as to avoid
* for columns not listed in the source statement.) * unnecessary data transmission. (We can't do that for INSERT since we
* would miss sending default values for columns not listed in the source
* statement, and for UPDATE if there are BEFORE ROW UPDATE triggers since
* those triggers might change values for non-target columns, in which
* case we would miss sending changed values for those columns.)
*/ */
if (operation == CMD_INSERT) if (operation == CMD_INSERT ||
(operation == CMD_UPDATE &&
rel->trigdesc &&
rel->trigdesc->trig_update_before_row))
{ {
TupleDesc tupdesc = RelationGetDescr(rel); TupleDesc tupdesc = RelationGetDescr(rel);
int attnum; int attnum;

View File

@ -638,6 +638,11 @@ SELECT * from loc1;
UPDATE rem1 set f2 = 'skidoo' RETURNING f2; UPDATE rem1 set f2 = 'skidoo' RETURNING f2;
SELECT * from loc1; SELECT * from loc1;
EXPLAIN (verbose, costs off)
UPDATE rem1 set f1 = 10; -- all columns should be transmitted
UPDATE rem1 set f1 = 10;
SELECT * from loc1;
DELETE FROM rem1; DELETE FROM rem1;
-- Add a second trigger, to check that the changes are propagated correctly -- Add a second trigger, to check that the changes are propagated correctly