Fix crash in postgres_fdw for provably-empty remote UPDATE/DELETE.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 7 Jul 2021 19:21:25 +0000 (15:21 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 7 Jul 2021 19:21:25 +0000 (15:21 -0400)
In 86dc90056, I'd written find_modifytable_subplan with the assumption
that if the immediate child of a ModifyTable is a Result, it must be
a projecting Result with a subplan.  However, if the UPDATE or DELETE
has a provably-constant-false WHERE clause, that's not so: we'll
generate a dummy subplan with a childless Result.  Add the missing
null-check so we don't crash on such cases.

Per report from Alexander Pyhalov.

Discussion: https://postgr.es/m/b9a6f53549456b2f3e2fd150dcd79d72@postgrespro.ru

contrib/postgres_fdw/expected/postgres_fdw.out
contrib/postgres_fdw/postgres_fdw.c
contrib/postgres_fdw/sql/postgres_fdw.sql

index b510322c4ea8e723e586735c8fc58eb61b93a7ae..b8561d6a3c49e47042f9fb994b5d0929e6441a77 100644 (file)
@@ -6853,6 +6853,26 @@ DROP TRIGGER trig_row_before ON rem1;
 DROP TRIGGER trig_row_after ON rem1;
 DROP TRIGGER trig_local_before ON loc1;
 -- Test direct foreign table modification functionality
+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)
+
+EXPLAIN (verbose, costs off)
+DELETE FROM rem1 WHERE false;     -- currently can't be pushed down
+                      QUERY PLAN                       
+-------------------------------------------------------
+ Delete on public.rem1
+   Remote SQL: DELETE FROM public.loc1 WHERE ctid = $1
+   ->  Result
+         Output: ctid
+         One-Time Filter: false
+(5 rows)
+
 -- Test with statement-level triggers
 CREATE TRIGGER trig_stmt_before
    BEFORE DELETE OR INSERT OR UPDATE ON rem1
index 5cf10402a2023e1406f72a9f48a41072187009e3..f15c97ad7a482908ba6441419a624354f00e855a 100644 (file)
@@ -2370,7 +2370,9 @@ find_modifytable_subplan(PlannerInfo *root,
        if (subplan_index < list_length(appendplan->appendplans))
            subplan = (Plan *) list_nth(appendplan->appendplans, subplan_index);
    }
-   else if (IsA(subplan, Result) && IsA(outerPlan(subplan), Append))
+   else if (IsA(subplan, Result) &&
+            outerPlan(subplan) != NULL &&
+            IsA(outerPlan(subplan), Append))
    {
        Append     *appendplan = (Append *) outerPlan(subplan);
 
index 911f171d812e743be23fa49c0c5cef39a5cba27b..c283e747154fb8758db37aa517f2f16ee4757c99 100644 (file)
@@ -1738,6 +1738,10 @@ DROP TRIGGER trig_local_before ON loc1;
 
 
 -- Test direct foreign table modification functionality
+EXPLAIN (verbose, costs off)
+DELETE FROM rem1;                 -- can be pushed down
+EXPLAIN (verbose, costs off)
+DELETE FROM rem1 WHERE false;     -- currently can't be pushed down
 
 -- Test with statement-level triggers
 CREATE TRIGGER trig_stmt_before