From c70eaef4a134245b88921f60bfbe0d94b35b8f68 Mon Sep 17 00:00:00 2001
From: Suraj Kharage <suraj.kharage@enterprisedb.com>
Date: Fri, 4 Dec 2026 10:51:32 +0530
Subject: [PATCH v1] postgres_fdw: Check shippability of ordered-set aggregate
 direct args

foreign_expr_walker() only walked Aggref.args, but the direct arguments
of an ordered-set aggregate live in aggdirectargs, and deparseAggref()
sends them to the remote server.  A non-shippable function there did not
stop the aggregate from being pushed down.  Walk them too, fix the
comment, and add regression tests.
---
 contrib/postgres_fdw/deparse.c                | 20 +++++-
 .../postgres_fdw/expected/postgres_fdw.out    | 70 +++++++++++++++++++
 contrib/postgres_fdw/sql/postgres_fdw.sql     | 22 ++++++
 3 files changed, 109 insertions(+), 3 deletions(-)

diff --git a/contrib/postgres_fdw/deparse.c b/contrib/postgres_fdw/deparse.c
index ff9fe0f87e4..57be77c2f5d 100644
--- a/contrib/postgres_fdw/deparse.c
+++ b/contrib/postgres_fdw/deparse.c
@@ -977,9 +977,23 @@ foreign_expr_walker(Node *node,
 					return false;
 
 				/*
-				 * Recurse to input args. aggdirectargs, aggorder and
-				 * aggdistinct are all present in args, so no need to check
-				 * their shippability explicitly.
+				 * Recurse to the direct args of an ordered-set aggregate.
+				 * These are kept in aggdirectargs, separately from args, and
+				 * deparseAggref() sends them to the remote server, so they
+				 * must be checked here.  (They are plain expressions, not
+				 * TargetEntry nodes.)
+				 */
+				foreach(lc, agg->aggdirectargs)
+				{
+					if (!foreign_expr_walker((Node *) lfirst(lc),
+											 glob_cxt, &inner_cxt, case_arg_cxt))
+						return false;
+				}
+
+				/*
+				 * Recurse to input args.  aggorder and aggdistinct refer to
+				 * entries of args, so no need to check their shippability
+				 * explicitly.
 				 */
 				foreach(lc, agg->args)
 				{
diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out
index aea1ddda308..ca4cdbb00cb 100644
--- a/contrib/postgres_fdw/expected/postgres_fdw.out
+++ b/contrib/postgres_fdw/expected/postgres_fdw.out
@@ -4208,6 +4208,76 @@ select c2, rank('10'::varchar) within group (order by c6), percentile_cont(c2/10
   4 |    1 |             400
 (5 rows)
 
+-- Direct arguments of an ordered-set aggregate must be checked for
+-- shippability, just like the aggregated arguments.  A local-only function
+-- there must keep the aggregate from being pushed down.  (percentile_disc is
+-- used because it returns an input value, so the output does not depend on
+-- floating-point rounding.)
+create function f_dirarg_local(numeric) returns numeric
+  language plpgsql immutable as $$ begin return $1; end $$;
+explain (verbose, costs off)
+select c2, percentile_disc(f_dirarg_local(c2/10::numeric)) within group (order by c1) from ft1 where c2 < 10 group by c2 order by c2;
+                                                          QUERY PLAN                                                           
+-------------------------------------------------------------------------------------------------------------------------------
+ GroupAggregate
+   Output: c2, percentile_disc((f_dirarg_local(((c2)::numeric / '10'::numeric)))::double precision) WITHIN GROUP (ORDER BY c1)
+   Group Key: ft1.c2
+   ->  Foreign Scan on public.ft1
+         Output: c2, c1
+         Remote SQL: SELECT "C 1", c2 FROM "S 1"."T 1" WHERE ((c2 < 10)) ORDER BY c2 ASC NULLS LAST
+(6 rows)
+
+select c2, percentile_disc(f_dirarg_local(c2/10::numeric)) within group (order by c1) from ft1 where c2 < 10 group by c2 order by c2;
+ c2 | percentile_disc 
+----+-----------------
+  0 |              10
+  1 |              91
+  2 |             192
+  3 |             293
+  4 |             394
+  5 |             495
+  6 |             596
+  7 |             697
+  8 |             798
+  9 |             899
+(10 rows)
+
+-- A function from a shippable extension in a direct argument is still pushed
+-- down.
+create function f_dirarg_ext(numeric) returns numeric
+  language plpgsql immutable as $$ begin return $1; end $$;
+alter extension postgres_fdw add function f_dirarg_ext(numeric);
+explain (verbose, costs off)
+select c2, percentile_disc(f_dirarg_ext(c2/10::numeric)) within group (order by c1) from ft1 where c2 < 10 group by c2 order by c2;
+                                                                                     QUERY PLAN                                                                                     
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Sort
+   Output: c2, (percentile_disc((f_dirarg_ext(((c2)::numeric / '10'::numeric)))::double precision) WITHIN GROUP (ORDER BY c1))
+   Sort Key: ft1.c2
+   ->  Foreign Scan
+         Output: c2, (percentile_disc((f_dirarg_ext(((c2)::numeric / '10'::numeric)))::double precision) WITHIN GROUP (ORDER BY c1))
+         Relations: Aggregate on (public.ft1)
+         Remote SQL: SELECT c2, percentile_disc(public.f_dirarg_ext((c2 / 10::numeric))) WITHIN GROUP (ORDER BY "C 1" ASC NULLS LAST) FROM "S 1"."T 1" WHERE ((c2 < 10)) GROUP BY 1
+(7 rows)
+
+select c2, percentile_disc(f_dirarg_ext(c2/10::numeric)) within group (order by c1) from ft1 where c2 < 10 group by c2 order by c2;
+ c2 | percentile_disc 
+----+-----------------
+  0 |              10
+  1 |              91
+  2 |             192
+  3 |             293
+  4 |             394
+  5 |             495
+  6 |             596
+  7 |             697
+  8 |             798
+  9 |             899
+(10 rows)
+
+alter extension postgres_fdw drop function f_dirarg_ext(numeric);
+drop function f_dirarg_ext(numeric);
+drop function f_dirarg_local(numeric);
 -- Using multiple arguments within aggregates
 explain (verbose, costs off)
 select c1, rank(c1, c2) within group (order by c1, c2) from ft1 group by c1, c2 having c1 = 6 order by 1;
diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql
index b71771ab8df..ade97037da5 100644
--- a/contrib/postgres_fdw/sql/postgres_fdw.sql
+++ b/contrib/postgres_fdw/sql/postgres_fdw.sql
@@ -1261,6 +1261,28 @@ explain (verbose, costs off)
 select c2, rank('10'::varchar) within group (order by c6), percentile_cont(c2/10::numeric) within group (order by c1) from ft1 where c2 < 10 group by c2 having percentile_cont(c2/10::numeric) within group (order by c1) < 500 order by c2;
 select c2, rank('10'::varchar) within group (order by c6), percentile_cont(c2/10::numeric) within group (order by c1) from ft1 where c2 < 10 group by c2 having percentile_cont(c2/10::numeric) within group (order by c1) < 500 order by c2;
 
+-- Direct arguments of an ordered-set aggregate must be checked for
+-- shippability, just like the aggregated arguments.  A local-only function
+-- there must keep the aggregate from being pushed down.  (percentile_disc is
+-- used because it returns an input value, so the output does not depend on
+-- floating-point rounding.)
+create function f_dirarg_local(numeric) returns numeric
+  language plpgsql immutable as $$ begin return $1; end $$;
+explain (verbose, costs off)
+select c2, percentile_disc(f_dirarg_local(c2/10::numeric)) within group (order by c1) from ft1 where c2 < 10 group by c2 order by c2;
+select c2, percentile_disc(f_dirarg_local(c2/10::numeric)) within group (order by c1) from ft1 where c2 < 10 group by c2 order by c2;
+-- A function from a shippable extension in a direct argument is still pushed
+-- down.
+create function f_dirarg_ext(numeric) returns numeric
+  language plpgsql immutable as $$ begin return $1; end $$;
+alter extension postgres_fdw add function f_dirarg_ext(numeric);
+explain (verbose, costs off)
+select c2, percentile_disc(f_dirarg_ext(c2/10::numeric)) within group (order by c1) from ft1 where c2 < 10 group by c2 order by c2;
+select c2, percentile_disc(f_dirarg_ext(c2/10::numeric)) within group (order by c1) from ft1 where c2 < 10 group by c2 order by c2;
+alter extension postgres_fdw drop function f_dirarg_ext(numeric);
+drop function f_dirarg_ext(numeric);
+drop function f_dirarg_local(numeric);
+
 -- Using multiple arguments within aggregates
 explain (verbose, costs off)
 select c1, rank(c1, c2) within group (order by c1, c2) from ft1 group by c1, c2 having c1 = 6 order by 1;
-- 
2.47.3

