*** a/contrib/postgres_fdw/deparse.c
--- b/contrib/postgres_fdw/deparse.c
***************
*** 189,198 **** is_foreign_expr(PlannerInfo *root,
  	if (!foreign_expr_walker((Node *) expr, &glob_cxt, &loc_cxt))
  		return false;
  
- 	/* Expressions examined here should be boolean, ie noncollatable */
- 	Assert(loc_cxt.collation == InvalidOid);
- 	Assert(loc_cxt.state == FDW_COLLATE_NONE);
- 
  	/*
  	 * An expression which includes any mutable functions can't be sent over
  	 * because its result is not stable.  For example, sending now() remote
--- 189,194 ----
***************
*** 788,794 **** deparseTargetList(StringInfo buf,
   *
   * If params is not NULL, it receives a list of Params and other-relation Vars
   * used in the clauses; these values must be transmitted to the remote server
!  * as parameter values.
   *
   * If params is NULL, we're generating the query for EXPLAIN purposes,
   * so Params and other-relation Vars should be replaced by dummy values.
--- 784,791 ----
   *
   * If params is not NULL, it receives a list of Params and other-relation Vars
   * used in the clauses; these values must be transmitted to the remote server
!  * as parameter values.  Caller is responsible for initializing the result list
!  * to empty if necessary.
   *
   * If params is NULL, we're generating the query for EXPLAIN purposes,
   * so Params and other-relation Vars should be replaced by dummy values.
***************
*** 805,813 **** appendWhereClause(StringInfo buf,
  	int			nestlevel;
  	ListCell   *lc;
  
- 	if (params)
- 		*params = NIL;			/* initialize result list to empty */
- 
  	/* Set up context struct for recursion */
  	context.root = root;
  	context.foreignrel = baserel;
--- 802,807 ----
***************
*** 940,945 **** deparseUpdateSql(StringInfo buf, PlannerInfo *root,
--- 934,996 ----
  }
  
  /*
+  * deparse remote UPDATE statement
+  *
+  * The statement text is appended to buf, and we also create an integer List
+  * of the columns being retrieved by RETURNING (if any), which is returned
+  * to *retrieved_attrs.
+  */
+ void
+ deparsePushedDownUpdateSql(StringInfo buf, PlannerInfo *root,
+ 						   Index rtindex, Relation rel,
+ 						   List	*targetlist,
+ 						   List *targetAttrs,
+ 						   List	*remote_conds,
+ 						   List **params_list,
+ 						   List *returningList,
+ 						   List **retrieved_attrs)
+ {
+ 	RelOptInfo *baserel = root->simple_rel_array[rtindex];
+ 	deparse_expr_cxt context;
+ 	bool		first;
+ 	ListCell   *lc;
+ 
+ 	if (params_list)
+ 		*params_list = NIL;		/* initialize result list to empty */
+ 
+ 	/* Set up context struct for recursion */
+ 	context.root = root;
+ 	context.foreignrel = baserel;
+ 	context.buf = buf;
+ 	context.params_list = params_list;
+ 
+ 	appendStringInfoString(buf, "UPDATE ");
+ 	deparseRelation(buf, rel);
+ 	appendStringInfoString(buf, " SET ");
+ 
+ 	first = true;
+ 	foreach(lc, targetAttrs)
+ 	{
+ 		int			attnum = lfirst_int(lc);
+ 		TargetEntry *tle = get_tle_by_resno(targetlist, attnum);
+ 
+ 		if (!first)
+ 			appendStringInfoString(buf, ", ");
+ 		first = false;
+ 
+ 		deparseColumnRef(buf, rtindex, attnum, root);
+ 		appendStringInfo(buf, " = ");
+ 		deparseExpr((Expr *) tle->expr, &context);
+ 	}
+ 	if (remote_conds)
+ 		appendWhereClause(buf, root, baserel, remote_conds,
+ 						  true, params_list);
+ 
+ 	deparseReturningList(buf, root, rtindex, rel, false,
+ 						 returningList, retrieved_attrs);
+ }
+ 
+ /*
   * deparse remote DELETE statement
   *
   * The statement text is appended to buf, and we also create an integer List
***************
*** 962,967 **** deparseDeleteSql(StringInfo buf, PlannerInfo *root,
--- 1013,1048 ----
  }
  
  /*
+  * deparse remote DELETE statement
+  *
+  * The statement text is appended to buf, and we also create an integer List
+  * of the columns being retrieved by RETURNING (if any), which is returned
+  * to *retrieved_attrs.
+  */
+ void
+ deparsePushedDownDeleteSql(StringInfo buf, PlannerInfo *root,
+ 						   Index rtindex, Relation rel,
+ 						   List	*remote_conds,
+ 						   List	**params_list,
+ 						   List *returningList,
+ 						   List **retrieved_attrs)
+ {
+ 	RelOptInfo *baserel = root->simple_rel_array[rtindex];
+ 
+ 	if (params_list)
+ 		*params_list = NIL;		/* initialize result list to empty */
+ 
+ 	appendStringInfoString(buf, "DELETE FROM ");
+ 	deparseRelation(buf, rel);
+ 	if (remote_conds)
+ 		appendWhereClause(buf, root, baserel, remote_conds,
+ 						  true, params_list);
+ 
+ 	deparseReturningList(buf, root, rtindex, rel, false,
+ 						 returningList, retrieved_attrs);
+ }
+ 
+ /*
   * Add a RETURNING clause, if needed, to an INSERT/UPDATE/DELETE.
   */
  static void
*** a/contrib/postgres_fdw/expected/postgres_fdw.out
--- b/contrib/postgres_fdw/expected/postgres_fdw.out
***************
*** 1099,1105 **** INSERT INTO ft2 (c1,c2,c3)
--- 1099,1124 ----
  (3 rows)
  
  INSERT INTO ft2 (c1,c2,c3) VALUES (1104,204,'ddd'), (1105,205,'eee');
+ EXPLAIN (verbose, costs off)
+ UPDATE ft2 SET c2 = c2 + 300, c3 = c3 || '_update3' WHERE c1 % 10 = 3;              -- can be pushed down
+                                                       QUERY PLAN                                                      
+ ----------------------------------------------------------------------------------------------------------------------
+  Update on public.ft2
+    ->  Foreign Update on public.ft2
+          Remote SQL: UPDATE "S 1"."T 1" SET c2 = (c2 + 300), c3 = (c3 || '_update3'::text) WHERE ((("C 1" % 10) = 3))
+ (3 rows)
+ 
  UPDATE ft2 SET c2 = c2 + 300, c3 = c3 || '_update3' WHERE c1 % 10 = 3;
+ EXPLAIN (verbose, costs off)
+ UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7' WHERE c1 % 10 = 7 RETURNING *;  -- can be pushed down
+                                                                             QUERY PLAN                                                                            
+ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
+  Update on public.ft2
+    Output: c1, c2, c3, c4, c5, c6, c7, c8
+    ->  Foreign Update on public.ft2
+          Remote SQL: UPDATE "S 1"."T 1" SET c2 = (c2 + 400), c3 = (c3 || '_update7'::text) WHERE ((("C 1" % 10) = 7)) RETURNING "C 1", c2, c3, c4, c5, c6, c7, c8
+ (4 rows)
+ 
  UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7' WHERE c1 % 10 = 7 RETURNING *;
    c1  | c2  |         c3         |              c4              |            c5            | c6 |     c7     | c8  
  ------+-----+--------------------+------------------------------+--------------------------+----+------------+-----
***************
*** 1209,1215 **** UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7' WHERE c1 % 10 = 7 RETURNING
  
  EXPLAIN (verbose, costs off)
  UPDATE ft2 SET c2 = ft2.c2 + 500, c3 = ft2.c3 || '_update9', c7 = DEFAULT
!   FROM ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 9;
                                                                              QUERY PLAN                                                                             
  -------------------------------------------------------------------------------------------------------------------------------------------------------------------
   Update on public.ft2
--- 1228,1234 ----
  
  EXPLAIN (verbose, costs off)
  UPDATE ft2 SET c2 = ft2.c2 + 500, c3 = ft2.c3 || '_update9', c7 = DEFAULT
!   FROM ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 9;                               -- can't be pushed down
                                                                              QUERY PLAN                                                                             
  -------------------------------------------------------------------------------------------------------------------------------------------------------------------
   Update on public.ft2
***************
*** 1230,1245 **** UPDATE ft2 SET c2 = ft2.c2 + 500, c3 = ft2.c3 || '_update9', c7 = DEFAULT
  UPDATE ft2 SET c2 = ft2.c2 + 500, c3 = ft2.c3 || '_update9', c7 = DEFAULT
    FROM ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 9;
  EXPLAIN (verbose, costs off)
!   DELETE FROM ft2 WHERE c1 % 10 = 5 RETURNING c1, c4;
!                                        QUERY PLAN                                       
! ----------------------------------------------------------------------------------------
   Delete on public.ft2
     Output: c1, c4
!    Remote SQL: DELETE FROM "S 1"."T 1" WHERE ctid = $1 RETURNING "C 1", c4
!    ->  Foreign Scan on public.ft2
!          Output: ctid
!          Remote SQL: SELECT ctid FROM "S 1"."T 1" WHERE ((("C 1" % 10) = 5)) FOR UPDATE
! (6 rows)
  
  DELETE FROM ft2 WHERE c1 % 10 = 5 RETURNING c1, c4;
    c1  |              c4              
--- 1249,1262 ----
  UPDATE ft2 SET c2 = ft2.c2 + 500, c3 = ft2.c3 || '_update9', c7 = DEFAULT
    FROM ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 9;
  EXPLAIN (verbose, costs off)
!   DELETE FROM ft2 WHERE c1 % 10 = 5 RETURNING c1, c4;                               -- can be pushed down
!                                          QUERY PLAN                                         
! --------------------------------------------------------------------------------------------
   Delete on public.ft2
     Output: c1, c4
!    ->  Foreign Delete on public.ft2
!          Remote SQL: DELETE FROM "S 1"."T 1" WHERE ((("C 1" % 10) = 5)) RETURNING "C 1", c4
! (4 rows)
  
  DELETE FROM ft2 WHERE c1 % 10 = 5 RETURNING c1, c4;
    c1  |              c4              
***************
*** 1350,1356 **** DELETE FROM ft2 WHERE c1 % 10 = 5 RETURNING c1, c4;
  (103 rows)
  
  EXPLAIN (verbose, costs off)
! DELETE FROM ft2 USING ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 2;
                                                        QUERY PLAN                                                      
  ----------------------------------------------------------------------------------------------------------------------
   Delete on public.ft2
--- 1367,1373 ----
  (103 rows)
  
  EXPLAIN (verbose, costs off)
! DELETE FROM ft2 USING ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 2;                -- can't be pushed down
                                                        QUERY PLAN                                                      
  ----------------------------------------------------------------------------------------------------------------------
   Delete on public.ft2
***************
*** 2193,2198 **** SELECT c1,c2,c3,c4 FROM ft2 ORDER BY c1;
--- 2210,2228 ----
   1104 | 204 | ddd                | 
  (819 rows)
  
+ EXPLAIN (verbose, costs off)
+ UPDATE ft2 SET c7 = DEFAULT WHERE c1 % 10 = 0 AND date(c4) = '1970-01-01'::date;    -- can't be pushed down
+                                                       QUERY PLAN                                                       
+ -----------------------------------------------------------------------------------------------------------------------
+  Update on public.ft2
+    Remote SQL: UPDATE "S 1"."T 1" SET c7 = $2 WHERE ctid = $1
+    ->  Foreign Scan on public.ft2
+          Output: c1, c2, NULL::integer, c3, c4, c5, c6, 'ft2       '::character(10), c8, ctid
+          Filter: (date(ft2.c4) = '01-01-1970'::date)
+          Remote SQL: SELECT "C 1", c2, c3, c4, c5, c6, c8, ctid FROM "S 1"."T 1" WHERE ((("C 1" % 10) = 0)) FOR UPDATE
+ (6 rows)
+ 
+ UPDATE ft2 SET c7 = DEFAULT WHERE c1 % 10 = 0 AND date(c4) = '1970-01-01'::date;
  -- Test that trigger on remote table works as expected
  CREATE OR REPLACE FUNCTION "S 1".F_BRTRIG() RETURNS trigger AS $$
  BEGIN
***************
*** 2334,2340 **** CONTEXT:  Remote SQL command: INSERT INTO "S 1"."T 1"("C 1", c2, c3, c4, c5, c6,
  UPDATE ft1 SET c2 = -c2 WHERE c1 = 1;  -- c2positive
  ERROR:  new row for relation "T 1" violates check constraint "c2positive"
  DETAIL:  Failing row contains (1, -1, 00001_trig_update, 1970-01-02 08:00:00+00, 1970-01-02 00:00:00, 1, 1         , foo).
! CONTEXT:  Remote SQL command: UPDATE "S 1"."T 1" SET c2 = $2 WHERE ctid = $1
  -- Test savepoint/rollback behavior
  select c2, count(*) from ft2 where c2 < 500 group by 1 order by 1;
   c2  | count 
--- 2364,2370 ----
  UPDATE ft1 SET c2 = -c2 WHERE c1 = 1;  -- c2positive
  ERROR:  new row for relation "T 1" violates check constraint "c2positive"
  DETAIL:  Failing row contains (1, -1, 00001_trig_update, 1970-01-02 08:00:00+00, 1970-01-02 00:00:00, 1, 1         , foo).
! CONTEXT:  Remote SQL command: UPDATE "S 1"."T 1" SET c2 = (- c2) WHERE (("C 1" = 1))
  -- Test savepoint/rollback behavior
  select c2, count(*) from ft2 where c2 < 500 group by 1 order by 1;
   c2  | count 
***************
*** 2493,2499 **** savepoint s3;
  update ft2 set c2 = -2 where c2 = 42 and c1 = 10; -- fail on remote side
  ERROR:  new row for relation "T 1" violates check constraint "c2positive"
  DETAIL:  Failing row contains (10, -2, 00010_trig_update_trig_update, 1970-01-11 08:00:00+00, 1970-01-11 00:00:00, 0, 0         , foo).
! CONTEXT:  Remote SQL command: UPDATE "S 1"."T 1" SET c2 = $2 WHERE ctid = $1
  rollback to savepoint s3;
  select c2, count(*) from ft2 where c2 < 500 group by 1 order by 1;
   c2  | count 
--- 2523,2529 ----
  update ft2 set c2 = -2 where c2 = 42 and c1 = 10; -- fail on remote side
  ERROR:  new row for relation "T 1" violates check constraint "c2positive"
  DETAIL:  Failing row contains (10, -2, 00010_trig_update_trig_update, 1970-01-11 08:00:00+00, 1970-01-11 00:00:00, 0, 0         , foo).
! CONTEXT:  Remote SQL command: UPDATE "S 1"."T 1" SET c2 = (-2) WHERE ((c2 = 42)) AND (("C 1" = 10))
  rollback to savepoint s3;
  select c2, count(*) from ft2 where c2 < 500 group by 1 order by 1;
   c2  | count 
***************
*** 2633,2639 **** CONTEXT:  Remote SQL command: INSERT INTO "S 1"."T 1"("C 1", c2, c3, c4, c5, c6,
  UPDATE ft1 SET c2 = -c2 WHERE c1 = 1;  -- c2positive
  ERROR:  new row for relation "T 1" violates check constraint "c2positive"
  DETAIL:  Failing row contains (1, -1, 00001_trig_update, 1970-01-02 08:00:00+00, 1970-01-02 00:00:00, 1, 1         , foo).
! CONTEXT:  Remote SQL command: UPDATE "S 1"."T 1" SET c2 = $2 WHERE ctid = $1
  ALTER FOREIGN TABLE ft1 DROP CONSTRAINT ft1_c2positive;
  -- But inconsistent check constraints provide inconsistent results
  ALTER FOREIGN TABLE ft1 ADD CONSTRAINT ft1_c2negative CHECK (c2 < 0);
--- 2663,2669 ----
  UPDATE ft1 SET c2 = -c2 WHERE c1 = 1;  -- c2positive
  ERROR:  new row for relation "T 1" violates check constraint "c2positive"
  DETAIL:  Failing row contains (1, -1, 00001_trig_update, 1970-01-02 08:00:00+00, 1970-01-02 00:00:00, 1, 1         , foo).
! CONTEXT:  Remote SQL command: UPDATE "S 1"."T 1" SET c2 = (- c2) WHERE (("C 1" = 1))
  ALTER FOREIGN TABLE ft1 DROP CONSTRAINT ft1_c2positive;
  -- But inconsistent check constraints provide inconsistent results
  ALTER FOREIGN TABLE ft1 ADD CONSTRAINT ft1_c2negative CHECK (c2 < 0);
***************
*** 3026,3031 **** NOTICE:  NEW: (13,"test triggered !")
--- 3056,3254 ----
   (0,27)
  (1 row)
  
+ -- cleanup
+ DROP TRIGGER trig_row_before ON rem1;
+ DROP TRIGGER trig_row_after ON rem1;
+ DROP TRIGGER trig_local_before ON loc1;
+ -- Test update-pushdown functionality
+ -- Test with statement-level triggers
+ CREATE TRIGGER trig_stmt_before
+ 	BEFORE DELETE OR INSERT OR UPDATE ON rem1
+ 	FOR EACH STATEMENT EXECUTE PROCEDURE trigger_func();
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can be pushed down
+                         QUERY PLAN                        
+ ----------------------------------------------------------
+  Update on public.rem1
+    ->  Foreign Update on public.rem1
+          Remote SQL: UPDATE public.loc1 SET f2 = ''::text
+ (3 rows)
+ 
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can be pushed down
+                  QUERY PLAN                  
+ ---------------------------------------------
+  Delete on public.rem1
+    ->  Foreign Delete on public.rem1
+          Remote SQL: DELETE FROM public.loc1
+ (3 rows)
+ 
+ DROP TRIGGER trig_stmt_before ON rem1;
+ CREATE TRIGGER trig_stmt_after
+ 	AFTER DELETE OR INSERT OR UPDATE ON rem1
+ 	FOR EACH STATEMENT EXECUTE PROCEDURE trigger_func();
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can be pushed down
+                         QUERY PLAN                        
+ ----------------------------------------------------------
+  Update on public.rem1
+    ->  Foreign Update on public.rem1
+          Remote SQL: UPDATE public.loc1 SET f2 = ''::text
+ (3 rows)
+ 
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can be pushed down
+                  QUERY PLAN                  
+ ---------------------------------------------
+  Delete on public.rem1
+    ->  Foreign Delete on public.rem1
+          Remote SQL: DELETE FROM public.loc1
+ (3 rows)
+ 
+ DROP TRIGGER trig_stmt_after ON rem1;
+ -- Test with row-level ON INSERT triggers
+ CREATE TRIGGER trig_row_before_insert
+ BEFORE INSERT ON rem1
+ FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can be pushed down
+                         QUERY PLAN                        
+ ----------------------------------------------------------
+  Update on public.rem1
+    ->  Foreign Update on public.rem1
+          Remote SQL: UPDATE public.loc1 SET f2 = ''::text
+ (3 rows)
+ 
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can be pushed down
+                  QUERY PLAN                  
+ ---------------------------------------------
+  Delete on public.rem1
+    ->  Foreign Delete on public.rem1
+          Remote SQL: DELETE FROM public.loc1
+ (3 rows)
+ 
+ DROP TRIGGER trig_row_before_insert ON rem1;
+ CREATE TRIGGER trig_row_after_insert
+ AFTER INSERT ON rem1
+ FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can be pushed down
+                         QUERY PLAN                        
+ ----------------------------------------------------------
+  Update on public.rem1
+    ->  Foreign Update on public.rem1
+          Remote SQL: UPDATE public.loc1 SET f2 = ''::text
+ (3 rows)
+ 
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can be pushed down
+                  QUERY PLAN                  
+ ---------------------------------------------
+  Delete on public.rem1
+    ->  Foreign Delete on public.rem1
+          Remote SQL: DELETE FROM public.loc1
+ (3 rows)
+ 
+ DROP TRIGGER trig_row_after_insert ON rem1;
+ -- Test with row-level ON UPDATE triggers
+ CREATE TRIGGER trig_row_before_update
+ BEFORE UPDATE ON rem1
+ FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can't be pushed down
+                              QUERY PLAN                              
+ ---------------------------------------------------------------------
+  Update on public.rem1
+    Remote SQL: UPDATE public.loc1 SET f2 = $2 WHERE ctid = $1
+    ->  Foreign Scan on public.rem1
+          Output: f1, ''::text, ctid, rem1.*
+          Remote SQL: SELECT f1, f2, ctid FROM public.loc1 FOR UPDATE
+ (5 rows)
+ 
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can be pushed down
+                  QUERY PLAN                  
+ ---------------------------------------------
+  Delete on public.rem1
+    ->  Foreign Delete on public.rem1
+          Remote SQL: DELETE FROM public.loc1
+ (3 rows)
+ 
+ DROP TRIGGER trig_row_before_update ON rem1;
+ CREATE TRIGGER trig_row_after_update
+ AFTER UPDATE ON rem1
+ FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can't be pushed down
+                                   QUERY PLAN                                   
+ -------------------------------------------------------------------------------
+  Update on public.rem1
+    Remote SQL: UPDATE public.loc1 SET f2 = $2 WHERE ctid = $1 RETURNING f1, f2
+    ->  Foreign Scan on public.rem1
+          Output: f1, ''::text, ctid, rem1.*
+          Remote SQL: SELECT f1, f2, ctid FROM public.loc1 FOR UPDATE
+ (5 rows)
+ 
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can be pushed down
+                  QUERY PLAN                  
+ ---------------------------------------------
+  Delete on public.rem1
+    ->  Foreign Delete on public.rem1
+          Remote SQL: DELETE FROM public.loc1
+ (3 rows)
+ 
+ DROP TRIGGER trig_row_after_update ON rem1;
+ -- Test with row-level ON DELETE triggers
+ CREATE TRIGGER trig_row_before_delete
+ BEFORE DELETE ON rem1
+ FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can be pushed down
+                         QUERY PLAN                        
+ ----------------------------------------------------------
+  Update on public.rem1
+    ->  Foreign Update on public.rem1
+          Remote SQL: UPDATE public.loc1 SET f2 = ''::text
+ (3 rows)
+ 
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can't be pushed down
+                              QUERY PLAN                              
+ ---------------------------------------------------------------------
+  Delete on public.rem1
+    Remote SQL: DELETE FROM public.loc1 WHERE ctid = $1
+    ->  Foreign Scan on public.rem1
+          Output: ctid, rem1.*
+          Remote SQL: SELECT f1, f2, ctid FROM public.loc1 FOR UPDATE
+ (5 rows)
+ 
+ DROP TRIGGER trig_row_before_delete ON rem1;
+ CREATE TRIGGER trig_row_after_delete
+ AFTER DELETE ON rem1
+ FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can be pushed down
+                         QUERY PLAN                        
+ ----------------------------------------------------------
+  Update on public.rem1
+    ->  Foreign Update on public.rem1
+          Remote SQL: UPDATE public.loc1 SET f2 = ''::text
+ (3 rows)
+ 
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can't be pushed down
+                                QUERY PLAN                               
+ ------------------------------------------------------------------------
+  Delete on public.rem1
+    Remote SQL: DELETE FROM public.loc1 WHERE ctid = $1 RETURNING f1, f2
+    ->  Foreign Scan on public.rem1
+          Output: ctid, rem1.*
+          Remote SQL: SELECT f1, f2, ctid FROM public.loc1 FOR UPDATE
+ (5 rows)
+ 
+ DROP TRIGGER trig_row_after_delete ON rem1;
  -- ===================================================================
  -- test IMPORT FOREIGN SCHEMA
  -- ===================================================================
*** a/contrib/postgres_fdw/postgres_fdw.c
--- b/contrib/postgres_fdw/postgres_fdw.c
***************
*** 87,93 **** typedef struct PgFdwRelationInfo
   * planner to executor.  Currently we store:
   *
   * 1) SELECT statement text to be sent to the remote server
!  * 2) Integer list of attribute numbers retrieved by the SELECT
   *
   * These items are indexed with the enum FdwScanPrivateIndex, so an item
   * can be fetched with list_nth().  For example, to get the SELECT statement:
--- 87,99 ----
   * planner to executor.  Currently we store:
   *
   * 1) SELECT statement text to be sent to the remote server
!  * 2) List of restriction clauses that can be executed remotely
!  * 3) Integer list of attribute numbers retrieved by the SELECT
!  * 4) Boolean flag showing if an UPDATE/DELETE is pushed down
!  * 5) UPDATE/DELETE statement text to be sent to the remote server
!  * 6) Boolean flag showing if we set the command es_processed
!  * 7) Boolean flag showing if the remote query has a RETURNING clause
!  * 8) Integer list of attribute numbers retrieved by RETURNING, if any
   *
   * These items are indexed with the enum FdwScanPrivateIndex, so an item
   * can be fetched with list_nth().  For example, to get the SELECT statement:
***************
*** 97,104 **** enum FdwScanPrivateIndex
  {
  	/* SQL statement to execute remotely (as a String node) */
  	FdwScanPrivateSelectSql,
! 	/* Integer list of attribute numbers retrieved by the SELECT */
! 	FdwScanPrivateRetrievedAttrs
  };
  
  /*
--- 103,122 ----
  {
  	/* SQL statement to execute remotely (as a String node) */
  	FdwScanPrivateSelectSql,
! 	/* List of restriction clauses that can be executed remotely */
! 	FdwScanPrivateRemoteConds,
! 	/* Integer list of attribute numbers retrieved by SELECT */
! 	FdwScanPrivateRetrievedAttrsBySelect,
! 	/* UPDATE/DELETE-pushdown flag (as an integer Value node) */
! 	FdwScanPrivateUpdatePushedDown,
! 	/* UPDATE/DELETE statement to execute remotely (as a String node) */
! 	FdwScanPrivateUpdateSql,
! 	/* set-processed flag (as an integer Value node) */
! 	FdwScanPrivateSetProcessed,
! 	/* has-returning flag (as an integer Value node) */
! 	FdwScanPrivateHasReturning,
! 	/* Integer list of attribute numbers retrieved by RETURNING */
! 	FdwScanPrivateRetrievedAttrsByReturning
  };
  
  /*
***************
*** 132,139 **** typedef struct PgFdwScanState
  	AttInMetadata *attinmeta;	/* attribute datatype conversion metadata */
  
  	/* extracted fdw_private data */
! 	char	   *query;			/* text of SELECT command */
  	List	   *retrieved_attrs;	/* list of retrieved attribute numbers */
  
  	/* for remote query execution */
  	PGconn	   *conn;			/* connection for the scan */
--- 150,159 ----
  	AttInMetadata *attinmeta;	/* attribute datatype conversion metadata */
  
  	/* extracted fdw_private data */
! 	char	   *query;			/* text of SELECT or UPDATE/DELETE command */
  	List	   *retrieved_attrs;	/* list of retrieved attribute numbers */
+ 	bool		set_processed;	/* do we set the command es_processed? */
+ 	bool		has_returning;	/* is there a RETURNING clause? */
  
  	/* for remote query execution */
  	PGconn	   *conn;			/* connection for the scan */
***************
*** 153,158 **** typedef struct PgFdwScanState
--- 173,183 ----
  	int			fetch_ct_2;		/* Min(# of fetches done, 2) */
  	bool		eof_reached;	/* true if last fetch reached EOF */
  
+ 	/* for update pushdown */
+ 	bool		update_pushed_down;	/* is an UPDATE/DELETE pushed down? */
+ 	PGresult   *result;			/* result of an UPDATE/DELETE query */
+ 	TupleTableSlot *rslot;		/* slot containing the result tuple */
+ 
  	/* working memory contexts */
  	MemoryContext batch_cxt;	/* context holding current batch of tuples */
  	MemoryContext temp_cxt;		/* context for per-tuple temporary data */
***************
*** 181,186 **** typedef struct PgFdwModifyState
--- 206,215 ----
  	int			p_nums;			/* number of parameters to transmit */
  	FmgrInfo   *p_flinfo;		/* output conversion functions for them */
  
+ 	/* for update pushdown */
+ 	bool		update_pushed_down;	/* is an UPDATE/DELETE pushed down? */
+ 	PgFdwScanState *fsstate;	/* execution state of a foreign scan */
+ 
  	/* working memory context */
  	MemoryContext temp_cxt;		/* context for per-tuple temporary data */
  } PgFdwModifyState;
***************
*** 309,320 **** static bool ec_member_matches_foreign(PlannerInfo *root, RelOptInfo *rel,
  static void create_cursor(ForeignScanState *node);
  static void fetch_more_data(ForeignScanState *node);
  static void close_cursor(PGconn *conn, unsigned int cursor_number);
  static void prepare_foreign_modify(PgFdwModifyState *fmstate);
  static const char **convert_prep_stmt_params(PgFdwModifyState *fmstate,
  						 ItemPointer tupleid,
  						 TupleTableSlot *slot);
! static void store_returning_result(PgFdwModifyState *fmstate,
! 					   TupleTableSlot *slot, PGresult *res);
  static int postgresAcquireSampleRowsFunc(Relation relation, int elevel,
  							  HeapTuple *rows, int targrows,
  							  double *totalrows,
--- 338,370 ----
  static void create_cursor(ForeignScanState *node);
  static void fetch_more_data(ForeignScanState *node);
  static void close_cursor(PGconn *conn, unsigned int cursor_number);
+ static void execute_pushed_down_command(ForeignScanState *node);
+ static TupleTableSlot *get_next_returning_result(ForeignScanState *node);
+ static bool update_is_pushdown_safe(PlannerInfo *root,
+ 									ModifyTable *plan,
+ 									Index resultRelation,
+ 									int subplan_index,
+ 									Relation rel,
+ 									List *targetAttrs);
+ static List *push_update_down(PlannerInfo *root,
+ 							  ModifyTable *plan,
+ 							  Index resultRelation,
+ 							  int subplan_index,
+ 							  Relation rel,
+ 							  List *targetAttrs);
+ static List *rewrite_targetlist(Index resultRelation, Relation rel,
+ 								List *targetlist, List *targetAttrs);
  static void prepare_foreign_modify(PgFdwModifyState *fmstate);
  static const char **convert_prep_stmt_params(PgFdwModifyState *fmstate,
  						 ItemPointer tupleid,
  						 TupleTableSlot *slot);
! static void store_returning_result(TupleTableSlot *slot,
! 					   PGresult *res,
! 					   int row,
! 					   Relation rel,
! 					   AttInMetadata *attinmeta,
! 					   List *retrieved_attrs,
! 					   MemoryContext temp_context);
  static int postgresAcquireSampleRowsFunc(Relation relation, int elevel,
  							  HeapTuple *rows, int targrows,
  							  double *totalrows,
***************
*** 853,860 **** postgresGetForeignPlan(PlannerInfo *root,
  	 * Build the fdw_private list that will be available to the executor.
  	 * Items in the list must match enum FdwScanPrivateIndex, above.
  	 */
! 	fdw_private = list_make2(makeString(sql.data),
! 							 retrieved_attrs);
  
  	/*
  	 * Create the ForeignScan node from target list, local filtering
--- 903,912 ----
  	 * Build the fdw_private list that will be available to the executor.
  	 * Items in the list must match enum FdwScanPrivateIndex, above.
  	 */
! 	fdw_private = list_make4(makeString(sql.data),
! 							 remote_conds,
! 							 retrieved_attrs,
! 							 makeInteger(false));
  
  	/*
  	 * Create the ForeignScan node from target list, local filtering
***************
*** 903,908 **** postgresBeginForeignScan(ForeignScanState *node, int eflags)
--- 955,966 ----
  	node->fdw_state = (void *) fsstate;
  
  	/*
+ 	 * Decide whether we push an UPDATE/DELETE command down.
+ 	 */
+ 	fsstate->update_pushed_down = intVal(list_nth(fsplan->fdw_private,
+ 												  FdwScanPrivateUpdatePushedDown));
+ 
+ 	/*
  	 * Identify which user to do the remote access as.  This should match what
  	 * ExecCheckRTEPerms() does.
  	 */
***************
*** 921,935 **** postgresBeginForeignScan(ForeignScanState *node, int eflags)
  	 */
  	fsstate->conn = GetConnection(server, user, false);
  
! 	/* Assign a unique ID for my cursor */
! 	fsstate->cursor_number = GetCursorNumber(fsstate->conn);
! 	fsstate->cursor_exists = false;
  
! 	/* Get private info created by planner functions. */
! 	fsstate->query = strVal(list_nth(fsplan->fdw_private,
! 									 FdwScanPrivateSelectSql));
! 	fsstate->retrieved_attrs = (List *) list_nth(fsplan->fdw_private,
! 											   FdwScanPrivateRetrievedAttrs);
  
  	/* Create contexts for batches of tuples and per-tuple temp workspace. */
  	fsstate->batch_cxt = AllocSetContextCreate(estate->es_query_cxt,
--- 979,1011 ----
  	 */
  	fsstate->conn = GetConnection(server, user, false);
  
! 	if (fsstate->update_pushed_down)
! 	{
! 		/* Initialize state variable */
! 		fsstate->num_tuples = -1;		/* -1 means not set yet */
  
! 		/* Get private info created by planner functions. */
! 		fsstate->query = strVal(list_nth(fsplan->fdw_private,
! 										 FdwScanPrivateUpdateSql));
! 		fsstate->set_processed = intVal(list_nth(fsplan->fdw_private,
! 												 FdwScanPrivateSetProcessed));
! 		fsstate->has_returning = intVal(list_nth(fsplan->fdw_private,
! 												 FdwScanPrivateHasReturning));
! 		fsstate->retrieved_attrs = (List *) list_nth(fsplan->fdw_private,
! 									  FdwScanPrivateRetrievedAttrsByReturning);
! 	}
! 	else
! 	{
! 		/* Assign a unique ID for my cursor */
! 		fsstate->cursor_number = GetCursorNumber(fsstate->conn);
! 		fsstate->cursor_exists = false;
! 
! 		/* Get private info created by planner functions. */
! 		fsstate->query = strVal(list_nth(fsplan->fdw_private,
! 										 FdwScanPrivateSelectSql));
! 		fsstate->retrieved_attrs = (List *) list_nth(fsplan->fdw_private,
! 										 FdwScanPrivateRetrievedAttrsBySelect);
! 	}
  
  	/* Create contexts for batches of tuples and per-tuple temp workspace. */
  	fsstate->batch_cxt = AllocSetContextCreate(estate->es_query_cxt,
***************
*** 996,1001 **** postgresIterateForeignScan(ForeignScanState *node)
--- 1072,1094 ----
  	TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;
  
  	/*
+ 	 * Get the result of an UPDATE/DELETE RETURNING, if pushing the command down.
+ 	 */
+ 	if (fsstate->update_pushed_down)
+ 	{
+ 		/*
+ 		 * If this is the first call after Begin, execute the command.
+ 		 */
+ 		if (fsstate->num_tuples == -1)
+ 			execute_pushed_down_command(node);
+ 
+ 		/*
+ 		 * Get the next returning result.
+ 		 */
+ 		return get_next_returning_result(node);
+ 	}
+ 
+ 	/*
  	 * If this is the first call after Begin or ReScan, we need to create the
  	 * cursor on the remote side.
  	 */
***************
*** 1037,1042 **** postgresReScanForeignScan(ForeignScanState *node)
--- 1130,1138 ----
  	char		sql[64];
  	PGresult   *res;
  
+ 	/* This shouldn't be called in update pushdown case. */
+ 	Assert(fsstate->update_pushed_down == false);
+ 
  	/* If we haven't created the cursor yet, nothing to do. */
  	if (!fsstate->cursor_exists)
  		return;
***************
*** 1095,1103 **** postgresEndForeignScan(ForeignScanState *node)
  	if (fsstate == NULL)
  		return;
  
! 	/* Close the cursor if open, to prevent accumulation of cursors */
! 	if (fsstate->cursor_exists)
! 		close_cursor(fsstate->conn, fsstate->cursor_number);
  
  	/* Release remote connection */
  	ReleaseConnection(fsstate->conn);
--- 1191,1208 ----
  	if (fsstate == NULL)
  		return;
  
! 	if (fsstate->update_pushed_down)
! 	{
! 		/* Clean up */
! 		if (fsstate->result)
! 			PQclear(fsstate->result);
! 	}
! 	else
! 	{
! 		/* Close the cursor if open, to prevent accumulation of cursors */
! 		if (fsstate->cursor_exists)
! 			close_cursor(fsstate->conn, fsstate->cursor_number);
! 	}
  
  	/* Release remote connection */
  	ReleaseConnection(fsstate->conn);
***************
*** 1146,1158 **** postgresAddForeignUpdateTargets(Query *parsetree,
  /*
   * postgresPlanForeignModify
   *		Plan an insert/update/delete operation on a foreign table
-  *
-  * Note: currently, the plan tree generated for UPDATE/DELETE will always
-  * include a ForeignScan that retrieves ctids (using SELECT FOR UPDATE)
-  * and then the ModifyTable node will have to execute individual remote
-  * UPDATE/DELETE commands.  If there are no local conditions or joins
-  * needed, it'd be better to let the scan node do UPDATE/DELETE RETURNING
-  * and then do nothing at ModifyTable.  Room for future optimization ...
   */
  static List *
  postgresPlanForeignModify(PlannerInfo *root,
--- 1251,1256 ----
***************
*** 1168,1175 **** postgresPlanForeignModify(PlannerInfo *root,
  	List	   *returningList = NIL;
  	List	   *retrieved_attrs = NIL;
  
- 	initStringInfo(&sql);
- 
  	/*
  	 * Core code already has some lock on each rel being planned, so we can
  	 * use NoLock here.
--- 1266,1271 ----
***************
*** 1213,1218 **** postgresPlanForeignModify(PlannerInfo *root,
--- 1309,1337 ----
  	}
  
  	/*
+ 	 * For UPDATE/DELETE, try to push the command down into the remote.
+ 	 */
+ 	if (operation == CMD_UPDATE || operation == CMD_DELETE)
+ 	{
+ 		/* Check to see whether it's safe to push the command down. */
+ 		if (update_is_pushdown_safe(root, plan,
+ 									resultRelation,
+ 									subplan_index,
+ 									rel, targetAttrs))
+ 		{
+ 			List	   *fdw_private;
+ 
+ 			/* OK, modify plan so as to push the command down. */
+ 			fdw_private = push_update_down(root, plan,
+ 										   resultRelation,
+ 										   subplan_index,
+ 										   rel, targetAttrs);
+ 			heap_close(rel, NoLock);
+ 			return fdw_private;
+ 		}
+ 	}
+ 
+ 	/*
  	 * Extract the relevant RETURNING list if any.
  	 */
  	if (plan->returningLists)
***************
*** 1221,1226 **** postgresPlanForeignModify(PlannerInfo *root,
--- 1340,1346 ----
  	/*
  	 * Construct the SQL command string.
  	 */
+ 	initStringInfo(&sql);
  	switch (operation)
  	{
  		case CMD_INSERT:
***************
*** 1291,1296 **** postgresBeginForeignModify(ModifyTableState *mtstate,
--- 1411,1452 ----
  	fmstate = (PgFdwModifyState *) palloc0(sizeof(PgFdwModifyState));
  	fmstate->rel = rel;
  
+ 	/* Deconstruct fdw_private data. */
+ 	fmstate->query = strVal(list_nth(fdw_private,
+ 									 FdwModifyPrivateUpdateSql));
+ 	fmstate->target_attrs = (List *) list_nth(fdw_private,
+ 											  FdwModifyPrivateTargetAttnums);
+ 	fmstate->has_returning = intVal(list_nth(fdw_private,
+ 											 FdwModifyPrivateHasReturning));
+ 	fmstate->retrieved_attrs = (List *) list_nth(fdw_private,
+ 											 FdwModifyPrivateRetrievedAttrs);
+ 
+ 	/*
+ 	 * if query is NULL, we are in update pushdown case.
+ 	 */
+ 	if (fmstate->query == NULL)
+ 	{
+ 		PlanState	   *node = mtstate->mt_plans[subplan_index];
+ 		PgFdwScanState *fsstate;
+ 
+ 		Assert(fmstate->target_attrs == NIL);
+ 		Assert(fmstate->has_returning == false);
+ 		Assert(fmstate->retrieved_attrs == NIL);
+ 
+ 		Assert(nodeTag(node) == T_ForeignScanState);
+ 		fsstate = (PgFdwScanState *) ((ForeignScanState *) node)->fdw_state;
+ 		Assert(fsstate->update_pushed_down);
+ 
+ 		fmstate->update_pushed_down = true;
+ 		if (fsstate->has_returning)
+ 		{
+ 			fmstate->has_returning = true;
+ 			fmstate->fsstate = fsstate;
+ 		}
+ 		resultRelInfo->ri_FdwState = fmstate;
+ 		return;
+ 	}
+ 
  	/*
  	 * Identify which user to do the remote access as.  This should match what
  	 * ExecCheckRTEPerms() does.
***************
*** 1307,1322 **** postgresBeginForeignModify(ModifyTableState *mtstate,
  	fmstate->conn = GetConnection(server, user, true);
  	fmstate->p_name = NULL;		/* prepared statement not made yet */
  
- 	/* Deconstruct fdw_private data. */
- 	fmstate->query = strVal(list_nth(fdw_private,
- 									 FdwModifyPrivateUpdateSql));
- 	fmstate->target_attrs = (List *) list_nth(fdw_private,
- 											  FdwModifyPrivateTargetAttnums);
- 	fmstate->has_returning = intVal(list_nth(fdw_private,
- 											 FdwModifyPrivateHasReturning));
- 	fmstate->retrieved_attrs = (List *) list_nth(fdw_private,
- 											 FdwModifyPrivateRetrievedAttrs);
- 
  	/* Create context for per-tuple temp workspace. */
  	fmstate->temp_cxt = AllocSetContextCreate(estate->es_query_cxt,
  											  "postgres_fdw temporary data",
--- 1463,1468 ----
***************
*** 1414,1420 **** postgresExecForeignInsert(EState *estate,
  	{
  		n_rows = PQntuples(res);
  		if (n_rows > 0)
! 			store_returning_result(fmstate, slot, res);
  	}
  	else
  		n_rows = atoi(PQcmdTuples(res));
--- 1560,1570 ----
  	{
  		n_rows = PQntuples(res);
  		if (n_rows > 0)
! 			store_returning_result(slot, res, 0,
! 								   fmstate->rel,
! 								   fmstate->attinmeta,
! 								   fmstate->retrieved_attrs,
! 								   fmstate->temp_cxt);
  	}
  	else
  		n_rows = atoi(PQcmdTuples(res));
***************
*** 1445,1450 **** postgresExecForeignUpdate(EState *estate,
--- 1595,1608 ----
  	PGresult   *res;
  	int			n_rows;
  
+ 	/* Just return the slot of the ForeignScan node, if pushing update down */
+ 	if (fmstate->update_pushed_down)
+ 	{
+ 		Assert(fmstate->has_returning);
+ 		Assert(fmstate->fsstate->rslot);
+ 		return fmstate->fsstate->rslot;
+ 	}
+ 
  	/* Set up the prepared statement on the remote server, if we didn't yet */
  	if (!fmstate->p_name)
  		prepare_foreign_modify(fmstate);
***************
*** 1484,1490 **** postgresExecForeignUpdate(EState *estate,
  	{
  		n_rows = PQntuples(res);
  		if (n_rows > 0)
! 			store_returning_result(fmstate, slot, res);
  	}
  	else
  		n_rows = atoi(PQcmdTuples(res));
--- 1642,1652 ----
  	{
  		n_rows = PQntuples(res);
  		if (n_rows > 0)
! 			store_returning_result(slot, res, 0,
! 								   fmstate->rel,
! 								   fmstate->attinmeta,
! 								   fmstate->retrieved_attrs,
! 								   fmstate->temp_cxt);
  	}
  	else
  		n_rows = atoi(PQcmdTuples(res));
***************
*** 1515,1520 **** postgresExecForeignDelete(EState *estate,
--- 1677,1690 ----
  	PGresult   *res;
  	int			n_rows;
  
+ 	/* Just return the slot of the ForeignScan node, if pushing update down */
+ 	if (fmstate->update_pushed_down)
+ 	{
+ 		Assert(fmstate->has_returning);
+ 		Assert(fmstate->fsstate->rslot);
+ 		return fmstate->fsstate->rslot;
+ 	}
+ 
  	/* Set up the prepared statement on the remote server, if we didn't yet */
  	if (!fmstate->p_name)
  		prepare_foreign_modify(fmstate);
***************
*** 1554,1560 **** postgresExecForeignDelete(EState *estate,
  	{
  		n_rows = PQntuples(res);
  		if (n_rows > 0)
! 			store_returning_result(fmstate, slot, res);
  	}
  	else
  		n_rows = atoi(PQcmdTuples(res));
--- 1724,1734 ----
  	{
  		n_rows = PQntuples(res);
  		if (n_rows > 0)
! 			store_returning_result(slot, res, 0,
! 								   fmstate->rel,
! 								   fmstate->attinmeta,
! 								   fmstate->retrieved_attrs,
! 								   fmstate->temp_cxt);
  	}
  	else
  		n_rows = atoi(PQcmdTuples(res));
***************
*** 1582,1587 **** postgresEndForeignModify(EState *estate,
--- 1756,1765 ----
  	if (fmstate == NULL)
  		return;
  
+ 	/* If pushing update down, nothing to do */
+ 	if (fmstate->update_pushed_down)
+ 		return;
+ 
  	/* If we created a prepared statement, destroy it */
  	if (fmstate->p_name)
  	{
***************
*** 1660,1670 **** postgresExplainForeignScan(ForeignScanState *node, ExplainState *es)
  {
  	List	   *fdw_private;
  	char	   *sql;
  
  	if (es->verbose)
  	{
  		fdw_private = ((ForeignScan *) node->ss.ps.plan)->fdw_private;
! 		sql = strVal(list_nth(fdw_private, FdwScanPrivateSelectSql));
  		ExplainPropertyText("Remote SQL", sql, es);
  	}
  }
--- 1838,1854 ----
  {
  	List	   *fdw_private;
  	char	   *sql;
+ 	bool		update_pushed_down;
  
  	if (es->verbose)
  	{
  		fdw_private = ((ForeignScan *) node->ss.ps.plan)->fdw_private;
! 		update_pushed_down = intVal(list_nth(fdw_private,
! 											 FdwScanPrivateUpdatePushedDown));
! 		if (update_pushed_down)
! 			sql = strVal(list_nth(fdw_private, FdwScanPrivateUpdateSql));
! 		else
! 			sql = strVal(list_nth(fdw_private, FdwScanPrivateSelectSql));
  		ExplainPropertyText("Remote SQL", sql, es);
  	}
  }
***************
*** 1685,1691 **** postgresExplainForeignModify(ModifyTableState *mtstate,
  		char	   *sql = strVal(list_nth(fdw_private,
  										  FdwModifyPrivateUpdateSql));
  
! 		ExplainPropertyText("Remote SQL", sql, es);
  	}
  }
  
--- 1869,1876 ----
  		char	   *sql = strVal(list_nth(fdw_private,
  										  FdwModifyPrivateUpdateSql));
  
! 		if (sql != NULL)
! 			ExplainPropertyText("Remote SQL", sql, es);
  	}
  }
  
***************
*** 1914,1919 **** ec_member_matches_foreign(PlannerInfo *root, RelOptInfo *rel,
--- 2099,2344 ----
  }
  
  /*
+  * Check to see whether it's safe to push an UPDATE/DELETE command down.
+  *
+  * Conditions checked here:
+  *
+  * 1. If the target relation has any row-level local BEFORE/AFTER triggers, we
+  * must not push the command down, since that breaks execution of the triggers.
+  *
+  * 2. If there are any local joins needed, we mustn't push the command down,
+  * because that breaks execution of the joins.
+  *
+  * 3. If there are any quals that can't be evaluated remotely, we mustn't push
+  * the command down, because that breaks evaluation of the quals.
+  *
+  * 4. In UPDATE, if it is unsafe to evaluate any expressions to assign to the
+  * target columns on the remote server, we must not push the command down.
+  */
+ static bool
+ update_is_pushdown_safe(PlannerInfo *root,
+ 						ModifyTable *plan,
+ 						Index resultRelation,
+ 						int subplan_index,
+ 						Relation rel,
+ 						List *targetAttrs)
+ {
+ 	CmdType		operation = plan->operation;
+ 	RelOptInfo *baserel = root->simple_rel_array[resultRelation];
+ 	Plan	   *subplan = (Plan *) list_nth(plan->plans, subplan_index);
+ 	ListCell   *lc;
+ 
+ 	/* Check point 1 */
+ 	if (rel->trigdesc &&
+ 		((operation == CMD_UPDATE &&
+ 		  (rel->trigdesc->trig_update_after_row ||
+ 		   rel->trigdesc->trig_update_before_row)) ||
+ 		 (operation == CMD_DELETE &&
+ 		  (rel->trigdesc->trig_delete_after_row ||
+ 		   rel->trigdesc->trig_delete_before_row))))
+ 		return false;
+ 
+ 	/* Check point 2 */
+ 	if (nodeTag(subplan) != T_ForeignScan)
+ 		return false;
+ 
+ 	/* Check point 3 */
+ 	if (subplan->qual != NIL)
+ 		return false;
+ 
+ 	/* Check point 4 */
+ 	foreach(lc, targetAttrs)
+ 	{
+ 		int			attnum = lfirst_int(lc);
+ 		TargetEntry *tle = get_tle_by_resno(subplan->targetlist,
+ 											attnum);
+ 
+ 		if (!is_foreign_expr(root, baserel, (Expr *) tle->expr))
+ 			return false;
+ 	}
+ 
+ 	return true;
+ }
+ 
+ /*
+  * Modify a plan so as to push an UPDATE/DELETE command down.
+  */
+ static List *
+ push_update_down(PlannerInfo *root,
+ 				 ModifyTable *plan,
+ 				 Index resultRelation,
+ 				 int subplan_index,
+ 				 Relation rel,
+ 				 List *targetAttrs)
+ {
+ 	CmdType		operation = plan->operation;
+ 	bool		canSetTag = plan->canSetTag;
+ 	Plan	   *subplan = (Plan *) list_nth(plan->plans, subplan_index);
+ 	ForeignScan *fscan = (ForeignScan *) subplan;
+ 	StringInfoData sql;
+ 	List	   *remote_conds;
+ 	List	   *params_list = NIL;
+ 	List	   *returningList = NIL;
+ 	List	   *retrieved_attrs = NIL;
+ 	List	   *fdw_private;
+ 	ListCell   *lc;
+ 
+ 	Assert(operation == CMD_UPDATE || operation == CMD_DELETE);
+ 
+ 	initStringInfo(&sql);
+ 
+ 	/*
+ 	 * Extract the baserestrictinfo clauses that can be evaluated remotely.
+ 	 */
+ 	remote_conds = (List *) list_nth(fscan->fdw_private,
+ 									 FdwScanPrivateRemoteConds);
+ 
+ 	/*
+ 	 * Extract the relevant RETURNING list if any.
+ 	 */
+ 	if (plan->returningLists)
+ 		returningList = (List *) list_nth(plan->returningLists, subplan_index);
+ 
+ 	/*
+ 	 * Construct the SQL command string.
+ 	 */
+ 	if (operation == CMD_UPDATE)
+ 	{
+ 		List	   *targetlist = subplan->targetlist;
+ 
+ 		deparsePushedDownUpdateSql(&sql, root, resultRelation, rel,
+ 								   targetlist,
+ 								   targetAttrs,
+ 								   remote_conds,
+ 								   &params_list,
+ 								   returningList,
+ 								   &retrieved_attrs);
+ 
+ 		/*
+ 		 * Rrewrite targetlist for safety of ExecProject.
+ 		 */
+ 		subplan->targetlist = rewrite_targetlist(resultRelation,
+ 												 rel,
+ 												 targetlist,
+ 												 targetAttrs);
+ 	}
+ 	else
+ 	{
+ 		Assert(operation == CMD_DELETE);
+ 
+ 		deparsePushedDownDeleteSql(&sql, root, resultRelation, rel,
+ 								   remote_conds,
+ 								   &params_list,
+ 								   returningList,
+ 								   &retrieved_attrs);
+ 	}
+ 
+ 	/*
+ 	 * Update the operation info.
+ 	 */
+ 	fscan->operation = operation;
+ 
+ 	/*
+ 	 * Update the fdw_exprs list that will be available to the executor.
+ 	 */
+ 	fscan->fdw_exprs = params_list;
+ 
+ 	/*
+ 	 * Update the fdw_private list that will be available to the executor.
+ 	 * Items in the list must match enum FdwScanPrivateIndex, above.
+ 	 */
+ 	lc = list_nth_cell(fscan->fdw_private, FdwScanPrivateUpdatePushedDown);
+ 	lfirst(lc) = makeInteger(true);
+ 
+ 	fscan->fdw_private = lappend(fscan->fdw_private, makeString(sql.data));
+ 	fscan->fdw_private = lappend(fscan->fdw_private, makeInteger(canSetTag));
+ 	fscan->fdw_private = lappend(fscan->fdw_private,
+ 								 makeInteger((retrieved_attrs != NIL)));
+ 	fscan->fdw_private = lappend(fscan->fdw_private, retrieved_attrs);
+ 
+ 	/*
+ 	 * Build the fdw_private list that will be available to the executor.
+ 	 * Items in the list must match enum FdwModifyPrivateIndex, above.
+ 	 */
+ 	fdw_private = list_make4(makeString(NULL), NIL, makeInteger(false), NIL);
+ 
+ 	return fdw_private;
+ }
+ 
+ /*
+  * Rrewrite the targetlist of an UPDATE.
+  *
+  * This is called for safety of ExecProject if pushing the command down.
+  */
+ static List *
+ rewrite_targetlist(Index resultRelation, Relation rel,
+ 				   List *targetlist, List *targetAttrs)
+ {
+ 	List	   *new_tlist = NIL;
+ 	int			numattrs = RelationGetNumberOfAttributes(rel);
+ 	int			attrno = 1;
+ 	ListCell   *lc;
+ 
+ 	foreach(lc, targetlist)
+ 	{
+ 		TargetEntry *tle = (TargetEntry *) lfirst(lc);
+ 
+ 		if (tle->resjunk)
+ 		{
+ 			new_tlist = lappend(new_tlist, tle);
+ 			continue;
+ 		}
+ 
+ 		if (attrno > numattrs)
+ 			ereport(ERROR,
+ 					(errcode(ERRCODE_DATATYPE_MISMATCH),
+ 					 errmsg("table row type and query-specified row type do not match"),
+ 					 errdetail("Query has too many columns.")));
+ 
+ 		if (!list_member_int(targetAttrs, attrno))
+ 			new_tlist = lappend(new_tlist, tle);
+ 		else
+ 		{
+ 			Form_pg_attribute attr = rel->rd_att->attrs[attrno - 1];
+ 			Oid			atttype;
+ 			int32		atttypmod;
+ 			Oid			attcollation;
+ 			Node	   *new_expr;
+ 			TargetEntry *new_tle;
+ 
+ 			Assert(!attr->attisdropped);
+ 			atttype = attr->atttypid;
+ 			atttypmod = attr->atttypmod;
+ 			attcollation = attr->attcollation;
+ 
+ 			new_expr = (Node *) makeVar(resultRelation,
+ 										attrno,
+ 										atttype,
+ 										atttypmod,
+ 										attcollation,
+ 										0);
+ 
+ 			new_tle = makeTargetEntry((Expr *) new_expr,
+ 									  attrno,
+ 									  pstrdup(NameStr(attr->attname)),
+ 									  false);
+ 
+ 			new_tlist = lappend(new_tlist, new_tle);
+ 		}
+ 
+ 		attrno++;
+ 	}
+ 
+ 	if (attrno != numattrs + 1)
+ 		ereport(ERROR,
+ 				(errcode(ERRCODE_DATATYPE_MISMATCH),
+ 				 errmsg("table row type and query-specified row type do not match"),
+ 				 errdetail("Query has too few columns.")));
+ 
+ 	return new_tlist;
+ }
+ 
+ /*
   * Create cursor for node's query with current parameter values.
   */
  static void
***************
*** 2150,2155 **** close_cursor(PGconn *conn, unsigned int cursor_number)
--- 2575,2729 ----
  }
  
  /*
+  * Execute a pushed-down UPDATE/DELETE command.
+  */
+ static void
+ execute_pushed_down_command(ForeignScanState *node)
+ {
+ 	PgFdwScanState *fsstate = (PgFdwScanState *) node->fdw_state;
+ 	ExprContext *econtext = node->ss.ps.ps_ExprContext;
+ 	int			numParams = fsstate->numParams;
+ 	const char **values = fsstate->param_values;
+ 
+ 	/*
+ 	 * Construct array of query parameter values in text format.  We do the
+ 	 * conversions in the short-lived per-tuple context, so as not to cause a
+ 	 * memory leak over repeated scans.
+ 	 */
+ 	if (numParams > 0)
+ 	{
+ 		int			nestlevel;
+ 		MemoryContext oldcontext;
+ 		int			i;
+ 		ListCell   *lc;
+ 
+ 		oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
+ 
+ 		nestlevel = set_transmission_modes();
+ 
+ 		i = 0;
+ 		foreach(lc, fsstate->param_exprs)
+ 		{
+ 			ExprState  *expr_state = (ExprState *) lfirst(lc);
+ 			Datum		expr_value;
+ 			bool		isNull;
+ 
+ 			/* Evaluate the parameter expression */
+ 			expr_value = ExecEvalExpr(expr_state, econtext, &isNull, NULL);
+ 
+ 			/*
+ 			 * Get string representation of each parameter value by invoking
+ 			 * type-specific output function, unless the value is null.
+ 			 */
+ 			if (isNull)
+ 				values[i] = NULL;
+ 			else
+ 				values[i] = OutputFunctionCall(&fsstate->param_flinfo[i],
+ 											   expr_value);
+ 			i++;
+ 		}
+ 
+ 		reset_transmission_modes(nestlevel);
+ 
+ 		MemoryContextSwitchTo(oldcontext);
+ 	}
+ 
+ 	/*
+ 	 * Notice that we pass NULL for paramTypes, thus forcing the remote server
+ 	 * to infer types for all parameters.  Since we explicitly cast every
+ 	 * parameter (see deparse.c), the "inference" is trivial and will produce
+ 	 * the desired result.  This allows us to avoid assuming that the remote
+ 	 * server has the same OIDs we do for the parameters' types.
+ 	 *
+ 	 * We don't use a PG_TRY block here, so be careful not to throw error
+ 	 * without releasing the PGresult.
+ 	 */
+ 	fsstate->result = PQexecParams(fsstate->conn, fsstate->query,
+ 								   numParams, NULL, values, NULL, NULL, 0);
+ 	if (PQresultStatus(fsstate->result) !=
+ 		(fsstate->has_returning ? PGRES_TUPLES_OK : PGRES_COMMAND_OK))
+ 		pgfdw_report_error(ERROR, fsstate->result, fsstate->conn, true,
+ 						   fsstate->query);
+ 
+ 	/* Get the number of rows affected. */
+ 	if (fsstate->has_returning)
+ 		fsstate->num_tuples = PQntuples(fsstate->result);
+ 	else
+ 		fsstate->num_tuples = atoi(PQcmdTuples(fsstate->result));
+ }
+ 
+ /*
+  * Get the result of an UPDATE/DELETE RETURNING.
+  */
+ static TupleTableSlot *
+ get_next_returning_result(ForeignScanState *node)
+ {
+ 	PgFdwScanState *fsstate = (PgFdwScanState *) node->fdw_state;
+ 	TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;
+ 	MemoryContext oldcontext;
+ 
+ 	/*
+ 	 * If the update query doesn't have a RETURNING clause, then there is
+ 	 * nothing to do, so we just return an empty slot.
+ 	 */
+ 	if (!fsstate->has_returning)
+ 	{
+ 		/*
+ 		 * Increment the command es_processed count if necessary.
+ 		 * (Note: ModifyTable cannot do that by itself in this case.)
+ 		 */
+ 		if (fsstate->set_processed)
+ 		{
+ 			EState	   *estate = node->ss.ps.state;
+ 
+ 			estate->es_processed += fsstate->num_tuples;
+ 		}
+ 
+ 		/*
+ 		 * Increment the tuple count for EXPLAIN ANALYZE if necessary.
+ 		 * (Note: EXPLAIN ANALYZE cannot do that by itself in this case.)
+ 		 */
+ 		if (node->ss.ps.instrument)
+ 		{
+ 			Instrumentation *instr = node->ss.ps.instrument;
+ 
+ 			instr->ntuples += fsstate->num_tuples;
+ 		}
+ 
+ 		return ExecClearTuple(slot);
+ 	}
+ 
+ 	/* If we didn't get any tuples, must be end of data. */
+ 	if (fsstate->next_tuple >= fsstate->num_tuples)
+ 		return ExecClearTuple(slot);
+ 
+ 	/* OK, we'll store RETURNING tuples in the batch_cxt. */
+ 	oldcontext = MemoryContextSwitchTo(fsstate->batch_cxt);
+ 
+ 	/* Fetch the next tuple. */
+ 	store_returning_result(slot,
+ 						   fsstate->result,
+ 						   fsstate->next_tuple,
+ 						   fsstate->rel,
+ 						   fsstate->attinmeta,
+ 						   fsstate->retrieved_attrs,
+ 						   fsstate->temp_cxt);
+ 	fsstate->rslot = slot;
+ 	fsstate->next_tuple++;
+ 
+ 	MemoryContextSwitchTo(oldcontext);
+ 
+ 	/*
+ 	 * Return slot.  Note that this is safe because we can avoid applying
+ 	 * ExecQual to the tuple due to no local quals (see the comment for
+ 	 * update_is_pushdown_safe) and because the tuple can be safely
+ 	 * projected by ExecProject (see push_update_down) and would then be
+ 	 * ignored by postgresExecForeignUpdate or postgresExecForeignDelete.
+ 	 */
+ 	return slot;
+ }
+ 
+ /*
   * prepare_foreign_modify
   *		Establish a prepared statement for execution of INSERT/UPDATE/DELETE
   */
***************
*** 2261,2278 **** convert_prep_stmt_params(PgFdwModifyState *fmstate,
   * have PG_TRY blocks to ensure this happens.
   */
  static void
! store_returning_result(PgFdwModifyState *fmstate,
! 					   TupleTableSlot *slot, PGresult *res)
  {
  	PG_TRY();
  	{
  		HeapTuple	newtup;
  
! 		newtup = make_tuple_from_result_row(res, 0,
! 											fmstate->rel,
! 											fmstate->attinmeta,
! 											fmstate->retrieved_attrs,
! 											fmstate->temp_cxt);
  		/* tuple will be deleted when it is cleared from the slot */
  		ExecStoreTuple(newtup, slot, InvalidBuffer, true);
  	}
--- 2835,2857 ----
   * have PG_TRY blocks to ensure this happens.
   */
  static void
! store_returning_result(TupleTableSlot *slot,
! 					   PGresult *res,
! 					   int row,
! 					   Relation rel,
! 					   AttInMetadata *attinmeta,
! 					   List *retrieved_attrs,
! 					   MemoryContext temp_context)
  {
  	PG_TRY();
  	{
  		HeapTuple	newtup;
  
! 		newtup = make_tuple_from_result_row(res, row,
! 											rel,
! 											attinmeta,
! 											retrieved_attrs,
! 											temp_context);
  		/* tuple will be deleted when it is cleared from the slot */
  		ExecStoreTuple(newtup, slot, InvalidBuffer, true);
  	}
*** a/contrib/postgres_fdw/postgres_fdw.h
--- b/contrib/postgres_fdw/postgres_fdw.h
***************
*** 66,75 **** extern void deparseUpdateSql(StringInfo buf, PlannerInfo *root,
--- 66,89 ----
  				 Index rtindex, Relation rel,
  				 List *targetAttrs, List *returningList,
  				 List **retrieved_attrs);
+ extern void deparsePushedDownUpdateSql(StringInfo buf, PlannerInfo *root,
+ 									   Index rtindex, Relation rel,
+ 									   List	*targetlist,
+ 									   List *targetAttrs,
+ 									   List	*remote_conds,
+ 									   List **params_list,
+ 									   List *returningList,
+ 									   List **retrieved_attrs);
  extern void deparseDeleteSql(StringInfo buf, PlannerInfo *root,
  				 Index rtindex, Relation rel,
  				 List *returningList,
  				 List **retrieved_attrs);
+ extern void deparsePushedDownDeleteSql(StringInfo buf, PlannerInfo *root,
+ 									   Index rtindex, Relation rel,
+ 									   List	*remote_conds,
+ 									   List **params_list,
+ 									   List *returningList,
+ 									   List **retrieved_attrs);
  extern void deparseAnalyzeSizeSql(StringInfo buf, Relation rel);
  extern void deparseAnalyzeSql(StringInfo buf, Relation rel,
  				  List **retrieved_attrs);
*** a/contrib/postgres_fdw/sql/postgres_fdw.sql
--- b/contrib/postgres_fdw/sql/postgres_fdw.sql
***************
*** 339,358 **** INSERT INTO ft2 (c1,c2,c3) SELECT c1+1000,c2+100, c3 || c3 FROM ft2 LIMIT 20;
  INSERT INTO ft2 (c1,c2,c3)
    VALUES (1101,201,'aaa'), (1102,202,'bbb'), (1103,203,'ccc') RETURNING *;
  INSERT INTO ft2 (c1,c2,c3) VALUES (1104,204,'ddd'), (1105,205,'eee');
  UPDATE ft2 SET c2 = c2 + 300, c3 = c3 || '_update3' WHERE c1 % 10 = 3;
  UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7' WHERE c1 % 10 = 7 RETURNING *;
  EXPLAIN (verbose, costs off)
  UPDATE ft2 SET c2 = ft2.c2 + 500, c3 = ft2.c3 || '_update9', c7 = DEFAULT
!   FROM ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 9;
  UPDATE ft2 SET c2 = ft2.c2 + 500, c3 = ft2.c3 || '_update9', c7 = DEFAULT
    FROM ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 9;
  EXPLAIN (verbose, costs off)
!   DELETE FROM ft2 WHERE c1 % 10 = 5 RETURNING c1, c4;
  DELETE FROM ft2 WHERE c1 % 10 = 5 RETURNING c1, c4;
  EXPLAIN (verbose, costs off)
! DELETE FROM ft2 USING ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 2;
  DELETE FROM ft2 USING ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 2;
  SELECT c1,c2,c3,c4 FROM ft2 ORDER BY c1;
  
  -- Test that trigger on remote table works as expected
  CREATE OR REPLACE FUNCTION "S 1".F_BRTRIG() RETURNS trigger AS $$
--- 339,365 ----
  INSERT INTO ft2 (c1,c2,c3)
    VALUES (1101,201,'aaa'), (1102,202,'bbb'), (1103,203,'ccc') RETURNING *;
  INSERT INTO ft2 (c1,c2,c3) VALUES (1104,204,'ddd'), (1105,205,'eee');
+ EXPLAIN (verbose, costs off)
+ UPDATE ft2 SET c2 = c2 + 300, c3 = c3 || '_update3' WHERE c1 % 10 = 3;              -- can be pushed down
  UPDATE ft2 SET c2 = c2 + 300, c3 = c3 || '_update3' WHERE c1 % 10 = 3;
+ EXPLAIN (verbose, costs off)
+ UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7' WHERE c1 % 10 = 7 RETURNING *;  -- can be pushed down
  UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7' WHERE c1 % 10 = 7 RETURNING *;
  EXPLAIN (verbose, costs off)
  UPDATE ft2 SET c2 = ft2.c2 + 500, c3 = ft2.c3 || '_update9', c7 = DEFAULT
!   FROM ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 9;                               -- can't be pushed down
  UPDATE ft2 SET c2 = ft2.c2 + 500, c3 = ft2.c3 || '_update9', c7 = DEFAULT
    FROM ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 9;
  EXPLAIN (verbose, costs off)
!   DELETE FROM ft2 WHERE c1 % 10 = 5 RETURNING c1, c4;                               -- can be pushed down
  DELETE FROM ft2 WHERE c1 % 10 = 5 RETURNING c1, c4;
  EXPLAIN (verbose, costs off)
! DELETE FROM ft2 USING ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 2;                -- can't be pushed down
  DELETE FROM ft2 USING ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 2;
  SELECT c1,c2,c3,c4 FROM ft2 ORDER BY c1;
+ EXPLAIN (verbose, costs off)
+ UPDATE ft2 SET c7 = DEFAULT WHERE c1 % 10 = 0 AND date(c4) = '1970-01-01'::date;    -- can't be pushed down
+ UPDATE ft2 SET c7 = DEFAULT WHERE c1 % 10 = 0 AND date(c4) = '1970-01-01'::date;
  
  -- Test that trigger on remote table works as expected
  CREATE OR REPLACE FUNCTION "S 1".F_BRTRIG() RETURNS trigger AS $$
***************
*** 665,670 **** UPDATE rem1 SET f2 = 'testo';
--- 672,761 ----
  -- Test returning a system attribute
  INSERT INTO rem1(f2) VALUES ('test') RETURNING ctid;
  
+ -- cleanup
+ DROP TRIGGER trig_row_before ON rem1;
+ DROP TRIGGER trig_row_after ON rem1;
+ DROP TRIGGER trig_local_before ON loc1;
+ 
+ 
+ -- Test update-pushdown functionality
+ 
+ -- Test with statement-level triggers
+ CREATE TRIGGER trig_stmt_before
+ 	BEFORE DELETE OR INSERT OR UPDATE ON rem1
+ 	FOR EACH STATEMENT EXECUTE PROCEDURE trigger_func();
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can be pushed down
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can be pushed down
+ DROP TRIGGER trig_stmt_before ON rem1;
+ 
+ CREATE TRIGGER trig_stmt_after
+ 	AFTER DELETE OR INSERT OR UPDATE ON rem1
+ 	FOR EACH STATEMENT EXECUTE PROCEDURE trigger_func();
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can be pushed down
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can be pushed down
+ DROP TRIGGER trig_stmt_after ON rem1;
+ 
+ -- Test with row-level ON INSERT triggers
+ CREATE TRIGGER trig_row_before_insert
+ BEFORE INSERT ON rem1
+ FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can be pushed down
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can be pushed down
+ DROP TRIGGER trig_row_before_insert ON rem1;
+ 
+ CREATE TRIGGER trig_row_after_insert
+ AFTER INSERT ON rem1
+ FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can be pushed down
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can be pushed down
+ DROP TRIGGER trig_row_after_insert ON rem1;
+ 
+ -- Test with row-level ON UPDATE triggers
+ CREATE TRIGGER trig_row_before_update
+ BEFORE UPDATE ON rem1
+ FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can't be pushed down
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can be pushed down
+ DROP TRIGGER trig_row_before_update ON rem1;
+ 
+ CREATE TRIGGER trig_row_after_update
+ AFTER UPDATE ON rem1
+ FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can't be pushed down
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can be pushed down
+ DROP TRIGGER trig_row_after_update ON rem1;
+ 
+ -- Test with row-level ON DELETE triggers
+ CREATE TRIGGER trig_row_before_delete
+ BEFORE DELETE ON rem1
+ FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can be pushed down
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can't be pushed down
+ DROP TRIGGER trig_row_before_delete ON rem1;
+ 
+ CREATE TRIGGER trig_row_after_delete
+ AFTER DELETE ON rem1
+ FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+ EXPLAIN (verbose, costs off)
+ UPDATE rem1 set f2 = '';          -- can be pushed down
+ EXPLAIN (verbose, costs off)
+ DELETE FROM rem1;                 -- can't be pushed down
+ DROP TRIGGER trig_row_after_delete ON rem1;
+ 
  -- ===================================================================
  -- test IMPORT FOREIGN SCHEMA
  -- ===================================================================
*** a/doc/src/sgml/postgres-fdw.sgml
--- b/doc/src/sgml/postgres-fdw.sgml
***************
*** 414,419 ****
--- 414,428 ----
     <literal>WHERE</> clauses are not sent to the remote server unless they use
     only built-in data types, operators, and functions.  Operators and
     functions in the clauses must be <literal>IMMUTABLE</> as well.
+    For an <command>UPDATE</> or <command>DELETE</> query,
+    <filename>postgres_fdw</> attempts to optimize the query execution by
+    sending the whole query to the remote server if there are no query
+    <literal>WHERE</> clauses that cannot be sent to the remote server,
+    no local joins for the query, or no row-level local <literal>BEFORE</> or
+    <literal>AFTER</> triggers on the target table.  In <command>UPDATE</>,
+    expressions to assign to target columns must use only built-in data types,
+    <literal>IMMUTABLE</> operators, and <literal>IMMUTABLE</> functions,
+    to reduce the risk of misexecution of the query.
    </para>
  
    <para>
*** a/src/backend/commands/explain.c
--- b/src/backend/commands/explain.c
***************
*** 947,953 **** ExplainNode(PlanState *planstate, List *ancestors,
  			pname = sname = "WorkTable Scan";
  			break;
  		case T_ForeignScan:
! 			pname = sname = "Foreign Scan";
  			break;
  		case T_CustomScan:
  			sname = "Custom Scan";
--- 947,971 ----
  			pname = sname = "WorkTable Scan";
  			break;
  		case T_ForeignScan:
! 			sname = "Foreign Scan";
! 			switch (((ForeignScan *) plan)->operation)
! 			{
! 				case CMD_SELECT:
! 					pname = "Foreign Scan";
! 					operation = "Select";
! 					break;
! 				case CMD_UPDATE:
! 					pname = "Foreign Update";
! 					operation = "Update";
! 					break;
! 				case CMD_DELETE:
! 					pname = "Foreign Delete";
! 					operation = "Delete";
! 					break;
! 				default:
! 					pname = "???";
! 					break;
! 			}
  			break;
  		case T_CustomScan:
  			sname = "Custom Scan";
***************
*** 1683,1688 **** show_plan_tlist(PlanState *planstate, List *ancestors, ExplainState *es)
--- 1701,1711 ----
  		return;
  	if (IsA(plan, RecursiveUnion))
  		return;
+ 	/* Likewise for ForeignScan in case of pushed-down UPDATE/DELETE */
+ 	if (IsA(plan, ForeignScan) &&
+ 		(((ForeignScan *) plan)->operation == CMD_UPDATE ||
+ 		 ((ForeignScan *) plan)->operation == CMD_DELETE))
+ 		return;
  
  	/* Set up deparsing context */
  	context = set_deparse_context_planstate(es->deparse_cxt,
*** a/src/backend/nodes/copyfuncs.c
--- b/src/backend/nodes/copyfuncs.c
***************
*** 592,597 **** _copyForeignScan(const ForeignScan *from)
--- 592,598 ----
  	/*
  	 * copy remainder of node
  	 */
+ 	COPY_SCALAR_FIELD(operation);
  	COPY_NODE_FIELD(fdw_exprs);
  	COPY_NODE_FIELD(fdw_private);
  	COPY_SCALAR_FIELD(fsSystemCol);
*** a/src/backend/nodes/outfuncs.c
--- b/src/backend/nodes/outfuncs.c
***************
*** 558,563 **** _outForeignScan(StringInfo str, const ForeignScan *node)
--- 558,564 ----
  
  	_outScanInfo(str, (const Scan *) node);
  
+ 	WRITE_ENUM_FIELD(operation, CmdType);
  	WRITE_NODE_FIELD(fdw_exprs);
  	WRITE_NODE_FIELD(fdw_private);
  	WRITE_BOOL_FIELD(fsSystemCol);
*** a/src/backend/optimizer/plan/createplan.c
--- b/src/backend/optimizer/plan/createplan.c
***************
*** 3569,3574 **** make_foreignscan(List *qptlist,
--- 3569,3575 ----
  	plan->lefttree = NULL;
  	plan->righttree = NULL;
  	node->scan.scanrelid = scanrelid;
+ 	node->operation = CMD_SELECT;
  	node->fdw_exprs = fdw_exprs;
  	node->fdw_private = fdw_private;
  	/* fsSystemCol will be filled in by create_foreignscan_plan */
*** a/src/include/nodes/plannodes.h
--- b/src/include/nodes/plannodes.h
***************
*** 480,485 **** typedef struct WorkTableScan
--- 480,486 ----
  typedef struct ForeignScan
  {
  	Scan		scan;
+ 	CmdType		operation;		/* SELECT, UPDATE, or DELETE */
  	List	   *fdw_exprs;		/* expressions that FDW may evaluate */
  	List	   *fdw_private;	/* private data for FDW */
  	bool		fsSystemCol;	/* true if any "system column" is needed */
