diff -pruN 2.9.2-2/LICENSE 2.9.3-1/LICENSE
--- 2.9.2-2/LICENSE	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/LICENSE	2025-09-30 05:28:24.000000000 +0000
@@ -1,6 +1,6 @@
 MySQL Foreign Data Wrapper for PostgreSQL
 
-Copyright (c) 2011-2024, EnterpriseDB Corporation.
+Copyright (c) 2011-2025, EnterpriseDB Corporation.
 
 Permission to use, copy, modify, and distribute this software and its
 documentation for any purpose, without fee, and without a written agreement is
diff -pruN 2.9.2-2/Makefile 2.9.3-1/Makefile
--- 2.9.2-2/Makefile	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/Makefile	2025-09-30 05:28:24.000000000 +0000
@@ -1,7 +1,7 @@
 # mysql_fdw/Makefile
 #
 # Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
-# Portions Copyright (c) 2004-2024, EnterpriseDB Corporation.
+# Portions Copyright (c) 2004-2025, EnterpriseDB Corporation.
 #
 
 MODULE_big = mysql_fdw
@@ -40,8 +40,8 @@ include $(PGXS)
 ifndef MAJORVERSION
 MAJORVERSION := $(basename $(VERSION))
 endif
-ifeq (,$(findstring $(MAJORVERSION), 12 13 14 15 16 17))
-$(error PostgreSQL 12, 13, 14, 15, 16, or 17 is required to compile this extension)
+ifeq (,$(findstring $(MAJORVERSION), 13 14 15 16 17 18))
+$(error PostgreSQL 13, 14, 15, 16, 17, or 18 is required to compile this extension)
 endif
 
 else
diff -pruN 2.9.2-2/README.md 2.9.3-1/README.md
--- 2.9.2-2/README.md	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/README.md	2025-09-30 05:28:24.000000000 +0000
@@ -5,7 +5,7 @@ This is a foreign data wrapper (FDW) to
 to [MySQL][1].
 
 Please note that this version of mysql_fdw works with PostgreSQL and EDB
-Postgres Advanced Server 12, 13, 14, 15, 16, and 17.
+Postgres Advanced Server 13, 14, 15, 16, 17, and 18.
 
 Contents
 --------
@@ -504,7 +504,7 @@ Reference FDW implementation, `postgres_
 
 License
 -------
-Copyright (c) 2011-2024, EnterpriseDB Corporation.
+Copyright (c) 2011-2025, EnterpriseDB Corporation.
 
 Permission to use, copy, modify, and distribute this software and its
 documentation for any purpose, without fee, and without a written
diff -pruN 2.9.2-2/connection.c 2.9.3-1/connection.c
--- 2.9.2-2/connection.c	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/connection.c	2025-09-30 05:28:24.000000000 +0000
@@ -4,7 +4,7 @@
  * 		Connection management functions for mysql_fdw
  *
  * Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
- * Portions Copyright (c) 2004-2024, EnterpriseDB Corporation.
+ * Portions Copyright (c) 2004-2025, EnterpriseDB Corporation.
  *
  * IDENTIFICATION
  * 		connection.c
@@ -14,9 +14,7 @@
 
 #include "postgres.h"
 
-#if PG_VERSION_NUM >= 130000
 #include "common/hashfn.h"
-#endif
 #include "mysql_fdw.h"
 #include "utils/hsearch.h"
 #include "utils/inval.h"
diff -pruN 2.9.2-2/debian/changelog 2.9.3-1/debian/changelog
--- 2.9.2-2/debian/changelog	2024-09-14 21:28:54.000000000 +0000
+++ 2.9.3-1/debian/changelog	2025-10-08 17:35:55.000000000 +0000
@@ -1,3 +1,13 @@
+postgresql-mysql-fdw (2.9.3-1) unstable; urgency=medium
+
+  * New upstream version 2.9.3.
+  * Upload for PostgreSQL 18.
+  * debian/pgversions: Bump to 13+.
+  * Add ${postgresql:Breaks}.
+  * debian/tests: Depend on postgresql-common-dev instead of 'make'.
+
+ -- Christoph Berg <myon@debian.org>  Wed, 08 Oct 2025 19:35:55 +0200
+
 postgresql-mysql-fdw (2.9.2-2) unstable; urgency=medium
 
   * Upload for PostgreSQL 17.
diff -pruN 2.9.2-2/debian/control 2.9.3-1/debian/control
--- 2.9.2-2/debian/control	2024-09-14 21:28:54.000000000 +0000
+++ 2.9.3-1/debian/control	2025-10-08 17:35:55.000000000 +0000
@@ -6,25 +6,26 @@ Uploaders:
  Markus Wanner <markus.wanner@datahouse.ch>,
  Christoph Berg <myon@debian.org>,
 Build-Depends:
- architecture-is-64-bit <!pkg.postgresql.32-bit>,
  debhelper-compat (= 13),
+ architecture-is-64-bit <!pkg.postgresql.32-bit>,
  default-libmysqlclient-dev | libmysqlclient-dev,
  postgresql-server-dev-all (>= 217~),
-Standards-Version: 4.7.0
-Rules-Requires-Root: no
+Standards-Version: 4.7.2
 Homepage: https://github.com/EnterpriseDB/mysql_fdw
 Vcs-Git: https://salsa.debian.org/postgresql/postgresql-mysql-fdw.git
 Vcs-Browser: https://salsa.debian.org/postgresql/postgresql-mysql-fdw
 
-Package: postgresql-17-mysql-fdw
+Package: postgresql-18-mysql-fdw
 Architecture: any
 Depends:
  default-libmysqlclient-dev | libmysqlclient-dev,
- ${postgresql:Depends},
  ${misc:Depends},
+ ${postgresql:Depends},
  ${shlibs:Depends},
-Description: Postgres 17 Foreign Data Wrapper for MySQL
+Breaks:
+ ${postgresql:Breaks},
+Description: Postgres 18 Foreign Data Wrapper for MySQL
  postgresql-mysql-fdw is a PostgreSQL extension implementing a Foreign Data
  Wrapper (FDW) to access MySQL data from PostgreSQL.
  .
- This package is for PostgreSQL 17.
+ This package is for PostgreSQL 18.
diff -pruN 2.9.2-2/debian/control.in 2.9.3-1/debian/control.in
--- 2.9.2-2/debian/control.in	2024-09-14 21:28:54.000000000 +0000
+++ 2.9.3-1/debian/control.in	2025-10-08 17:35:55.000000000 +0000
@@ -6,12 +6,11 @@ Uploaders:
  Markus Wanner <markus.wanner@datahouse.ch>,
  Christoph Berg <myon@debian.org>,
 Build-Depends:
- architecture-is-64-bit <!pkg.postgresql.32-bit>,
  debhelper-compat (= 13),
+ architecture-is-64-bit <!pkg.postgresql.32-bit>,
  default-libmysqlclient-dev | libmysqlclient-dev,
  postgresql-server-dev-all (>= 217~),
-Standards-Version: 4.7.0
-Rules-Requires-Root: no
+Standards-Version: 4.7.2
 Homepage: https://github.com/EnterpriseDB/mysql_fdw
 Vcs-Git: https://salsa.debian.org/postgresql/postgresql-mysql-fdw.git
 Vcs-Browser: https://salsa.debian.org/postgresql/postgresql-mysql-fdw
@@ -20,9 +19,11 @@ Package: postgresql-PGVERSION-mysql-fdw
 Architecture: any
 Depends:
  default-libmysqlclient-dev | libmysqlclient-dev,
- ${postgresql:Depends},
  ${misc:Depends},
+ ${postgresql:Depends},
  ${shlibs:Depends},
+Breaks:
+ ${postgresql:Breaks},
 Description: Postgres PGVERSION Foreign Data Wrapper for MySQL
  postgresql-mysql-fdw is a PostgreSQL extension implementing a Foreign Data
  Wrapper (FDW) to access MySQL data from PostgreSQL.
diff -pruN 2.9.2-2/debian/pgversions 2.9.3-1/debian/pgversions
--- 2.9.2-2/debian/pgversions	2024-07-19 17:22:40.000000000 +0000
+++ 2.9.3-1/debian/pgversions	2025-10-08 17:35:55.000000000 +0000
@@ -1 +1 @@
-12+
+13+
diff -pruN 2.9.2-2/debian/tests/control 2.9.3-1/debian/tests/control
--- 2.9.2-2/debian/tests/control	2024-07-19 17:17:49.000000000 +0000
+++ 2.9.3-1/debian/tests/control	2025-10-08 17:35:55.000000000 +0000
@@ -1,7 +1,20 @@
-Depends: @, default-mysql-server | mysql-server, locales-all, make
-Tests: installcheck
-Restrictions: needs-root, allow-stderr, flaky, skippable
+Depends:
+ default-mysql-server | mysql-server,
+ locales-all,
+ postgresql-common-dev,
+ @,
+Tests:
+ installcheck,
+Restrictions:
+ allow-stderr,
+ flaky,
+ needs-root,
+ skippable,
 
-Depends: @
-Tests: create-extension
-Restrictions: allow-stderr, superficial
+Depends:
+ @,
+Tests:
+ create-extension,
+Restrictions:
+ allow-stderr,
+ superficial,
diff -pruN 2.9.2-2/deparse.c 2.9.3-1/deparse.c
--- 2.9.2-2/deparse.c	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/deparse.c	2025-09-30 05:28:24.000000000 +0000
@@ -4,7 +4,7 @@
  * 		Query deparser for mysql_fdw
  *
  * Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
- * Portions Copyright (c) 2004-2024, EnterpriseDB Corporation.
+ * Portions Copyright (c) 2004-2025, EnterpriseDB Corporation.
  *
  * IDENTIFICATION
  * 		deparse.c
@@ -358,20 +358,12 @@ mysql_deparse_select_sql(List *tlist, Li
 		 * Core code already has some lock on each rel being planned, so we
 		 * can use NoLock here.
 		 */
-#if PG_VERSION_NUM < 130000
-		rel = heap_open(rte->relid, NoLock);
-#else
 		rel = table_open(rte->relid, NoLock);
-#endif
 
 		mysql_deparse_target_list(buf, root, foreignrel->relid, rel,
 								  fpinfo->attrs_used, retrieved_attrs);
 
-#if PG_VERSION_NUM < 130000
-		heap_close(rel, NoLock);
-#else
 		table_close(rel, NoLock);
-#endif
 	}
 }
 
@@ -1031,11 +1023,7 @@ mysql_deparse_array_ref(SubscriptingRef
 		{
 			deparseExpr(lfirst(lowlist_item), context);
 			appendStringInfoChar(buf, ':');
-#if PG_VERSION_NUM < 130000
-			lowlist_item = lnext(lowlist_item);
-#else
 			lowlist_item = lnext(node->reflowerindexpr, lowlist_item);
-#endif
 		}
 		deparseExpr(lfirst(uplist_item), context);
 		appendStringInfoChar(buf, ']');
@@ -1544,6 +1532,14 @@ foreign_expr_walker(Node *node, foreign_
 				Param	   *p = (Param *) node;
 
 				/*
+				 * Bail out on planner internal params.  We could perhaps pass
+				 * them to the remote server as regular params, but we don't
+				 * have the machinery to do that at the moment.
+				 */
+				if (p->paramkind != PARAM_EXTERN)
+					return false;
+
+				/*
 				 * Collation rule is same as for Consts and non-foreign Vars.
 				 */
 				collation = p->paramcollid;
@@ -2116,11 +2112,7 @@ mysql_deparse_from_expr_for_rel(StringIn
 		 * Core code already has some lock on each rel being planned, so we
 		 * can use NoLock here.
 		 */
-#if PG_VERSION_NUM < 130000
-		rel = heap_open(rte->relid, NoLock);
-#else
 		rel = table_open(rte->relid, NoLock);
-#endif
 
 		mysql_deparse_relation(buf, rel);
 
@@ -2133,11 +2125,7 @@ mysql_deparse_from_expr_for_rel(StringIn
 			appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX,
 							 foreignrel->relid);
 
-#if PG_VERSION_NUM < 130000
-		heap_close(rel, NoLock);
-#else
 		table_close(rel, NoLock);
-#endif
 	}
 	return;
 }
diff -pruN 2.9.2-2/expected/aggregate_pushdown.out 2.9.3-1/expected/aggregate_pushdown.out
--- 2.9.2-2/expected/aggregate_pushdown.out	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/expected/aggregate_pushdown.out	2025-09-30 05:28:24.000000000 +0000
@@ -608,11 +608,13 @@ SELECT c2, sum FROM fdw132_t1 t1, latera
    ->  Subquery Scan on qry
          Output: qry.sum, t2.c1
          Filter: ((t1.c2 * 2) = qry.sum)
-         ->  Foreign Scan
-               Output: (sum((t2.c1 + t1.c1))), t2.c1
-               Relations: Aggregate on (mysql_fdw_regress.fdw132_t2 t2)
-               Remote query: SELECT sum((`c1` + ?)), `c1` FROM `mysql_fdw_regress`.`test2` GROUP BY 2
-(12 rows)
+         ->  GroupAggregate
+               Output: sum((t2.c1 + t1.c1)), t2.c1
+               Group Key: t2.c1
+               ->  Foreign Scan on public.fdw132_t2 t2
+                     Output: t2.c1, t2.c2, t2.c3, t2.c4
+                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test2` ORDER BY `c1` IS NULL, `c1` ASC
+(14 rows)
 
 -- Check with placeHolderVars
 EXPLAIN (VERBOSE, COSTS OFF)
diff -pruN 2.9.2-2/expected/aggregate_pushdown_1.out 2.9.3-1/expected/aggregate_pushdown_1.out
--- 2.9.2-2/expected/aggregate_pushdown_1.out	1970-01-01 00:00:00.000000000 +0000
+++ 2.9.3-1/expected/aggregate_pushdown_1.out	2025-09-30 05:28:24.000000000 +0000
@@ -0,0 +1,1076 @@
+\set MYSQL_HOST			`echo \'"$MYSQL_HOST"\'`
+\set MYSQL_PORT			`echo \'"$MYSQL_PORT"\'`
+\set MYSQL_USER_NAME	`echo \'"$MYSQL_USER_NAME"\'`
+\set MYSQL_PASS			`echo \'"$MYSQL_PWD"\'`
+-- Before running this file User must create database mysql_fdw_regress on
+-- mysql with all permission for MYSQL_USER_NAME user with MYSQL_PWD password
+-- and ran mysql_init.sh file to create tables.
+\c contrib_regression
+CREATE EXTENSION IF NOT EXISTS mysql_fdw;
+-- FDW-132: Support for aggregate pushdown.
+CREATE SERVER mysql_svr FOREIGN DATA WRAPPER mysql_fdw
+  OPTIONS (host :MYSQL_HOST, port :MYSQL_PORT);
+CREATE USER MAPPING FOR public SERVER mysql_svr
+  OPTIONS (username :MYSQL_USER_NAME, password :MYSQL_PASS);
+CREATE TYPE user_enum AS ENUM ('foo', 'bar', 'buz');
+CREATE FOREIGN TABLE fdw132_t1(c1 int, c2 int, c3 text COLLATE "C", c4 text COLLATE "C")
+  SERVER mysql_svr OPTIONS(dbname 'mysql_fdw_regress', table_name 'test1');
+CREATE FOREIGN TABLE fdw132_t2(c1 int, c2 int, c3 text COLLATE "C", c4 text COLLATE "C")
+  SERVER mysql_svr OPTIONS(dbname 'mysql_fdw_regress', table_name 'test2');
+INSERT INTO fdw132_t1 values(1, 100, 'AAA1', 'foo');
+INSERT INTO fdw132_t1 values(2, 100, 'AAA2', 'bar');
+INSERT INTO fdw132_t1 values(11, 100, 'AAA11', 'foo');
+INSERT INTO fdw132_t2 values(1, 200, 'BBB1', 'foo');
+INSERT INTO fdw132_t2 values(2, 200, 'BBB2', 'bar');
+INSERT INTO fdw132_t2 values(12, 200, 'BBB12', 'foo');
+ALTER FOREIGN TABLE fdw132_t1 ALTER COLUMN c4 type user_enum;
+ALTER FOREIGN TABLE fdw132_t2 ALTER COLUMN c4 type user_enum;
+-- Simple aggregates
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(c1), avg(c1), min(c2), max(c1), sum(c1) * (random() <= 1)::int AS sum2 FROM fdw132_t1 WHERE c2 > 5 GROUP BY c2 ORDER BY 1, 2;
+                                                                                                     QUERY PLAN                                                                                                     
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Foreign Scan
+   Output: (sum(c1)), (avg(c1)), (min(c2)), (max(c1)), ((sum(c1)) * ((random() <= '1'::double precision))::integer), c2
+   Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+   Remote query: SELECT sum(`c1`), avg(`c1`), min(`c2`), max(`c1`), `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 5)) GROUP BY 5 ORDER BY sum(`c1`) IS NULL, sum(`c1`) ASC, avg(`c1`) IS NULL, avg(`c1`) ASC
+(4 rows)
+
+SELECT sum(c1), avg(c1), min(c2), max(c1), sum(c1) * (random() <= 1)::int AS sum2 FROM fdw132_t1 WHERE c2 > 5 GROUP BY c2 ORDER BY 1, 2;
+ sum |  avg   | min | max | sum2 
+-----+--------+-----+-----+------
+  14 | 4.6667 | 100 |  11 |   14
+(1 row)
+
+-- Aggregate is not pushed down as aggregation contains random()
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(c1 * (random() <= 1)::int) AS sum, avg(c1) FROM fdw132_t1;
+                                  QUERY PLAN                                   
+-------------------------------------------------------------------------------
+ Aggregate
+   Output: sum((c1 * ((random() <= '1'::double precision))::integer)), avg(c1)
+   ->  Foreign Scan on public.fdw132_t1
+         Output: c1, c2, c3, c4
+         Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1`
+(5 rows)
+
+SELECT sum(c1 * (random() <= 1)::int) AS sum, avg(c1) FROM fdw132_t1;
+ sum |        avg         
+-----+--------------------
+  14 | 4.6666666666666667
+(1 row)
+
+-- Aggregate over join query
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT count(*), sum(t1.c1), avg(t2.c1) FROM  fdw132_t1 t1 INNER JOIN fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c1 = 2;
+                                                                                           QUERY PLAN                                                                                            
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Foreign Scan
+   Output: (count(*)), (sum(t1.c1)), (avg(t2.c1))
+   Relations: Aggregate on ((mysql_fdw_regress.fdw132_t1 t1) INNER JOIN (mysql_fdw_regress.fdw132_t2 t2))
+   Remote query: SELECT count(*), sum(r1.`c1`), avg(r2.`c1`) FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (TRUE)) WHERE ((r2.`c1` = 2)) AND ((r1.`c1` = 2))
+(4 rows)
+
+SELECT count(*), sum(t1.c1), avg(t2.c1) FROM  fdw132_t1 t1 INNER JOIN fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c1 = 2;
+ count | sum |  avg   
+-------+-----+--------
+     1 |   2 | 2.0000
+(1 row)
+
+-- Not pushed down due to local conditions present in underneath input rel
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(t1.c1), count(t2.c1) FROM fdw132_t1 t1 INNER JOIN fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE ((t1.c1 * t2.c1)/(t1.c1 * t2.c1)) * random() <= 1;
+                                                                        QUERY PLAN                                                                        
+----------------------------------------------------------------------------------------------------------------------------------------------------------
+ Aggregate
+   Output: sum(t1.c1), count(t2.c1)
+   ->  Foreign Scan
+         Output: t1.c1, t2.c1
+         Filter: (((((t1.c1 * t2.c1) / (t1.c1 * t2.c1)))::double precision * random()) <= '1'::double precision)
+         Relations: (mysql_fdw_regress.fdw132_t1 t1) INNER JOIN (mysql_fdw_regress.fdw132_t2 t2)
+         Remote query: SELECT r1.`c1`, r2.`c1` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (((r1.`c1` = r2.`c1`))))
+(7 rows)
+
+SELECT sum(t1.c1), count(t2.c1) FROM fdw132_t1 t1 INNER JOIN fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE ((t1.c1 * t2.c1)/(t1.c1 * t2.c1)) * random() <= 1;
+ sum | count 
+-----+-------
+   3 |     2
+(1 row)
+
+-- GROUP BY clause HAVING expressions
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c2+2, sum(c2) * (c2+2) FROM fdw132_t1 GROUP BY c2+2 ORDER BY c2+2;
+                                                                     QUERY PLAN                                                                      
+-----------------------------------------------------------------------------------------------------------------------------------------------------
+ Foreign Scan
+   Output: ((c2 + 2)), ((sum(c2) * (c2 + 2)))
+   Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+   Remote query: SELECT (`c2` + 2), (sum(`c2`) * (`c2` + 2)) FROM `mysql_fdw_regress`.`test1` GROUP BY 1 ORDER BY (`c2` + 2) IS NULL, (`c2` + 2) ASC
+(4 rows)
+
+SELECT c2+2, sum(c2) * (c2+2) FROM fdw132_t1 GROUP BY c2+2 ORDER BY c2+2;
+ ?column? | ?column? 
+----------+----------
+      102 |    30600
+(1 row)
+
+-- Aggregates in subquery are pushed down.
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT count(x.a), sum(x.a) FROM (SELECT c2 a, sum(c1) b FROM fdw132_t1 GROUP BY c2 ORDER BY 1, 2) x;
+                                                                         QUERY PLAN                                                                         
+------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Aggregate
+   Output: count(fdw132_t1.c2), sum(fdw132_t1.c2)
+   ->  Foreign Scan
+         Output: fdw132_t1.c2, (sum(fdw132_t1.c1))
+         Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+         Remote query: SELECT `c2`, sum(`c1`) FROM `mysql_fdw_regress`.`test1` GROUP BY 1 ORDER BY `c2` IS NULL, `c2` ASC, sum(`c1`) IS NULL, sum(`c1`) ASC
+(6 rows)
+
+SELECT count(x.a), sum(x.a) FROM (SELECT c2 a, sum(c1) b FROM fdw132_t1 GROUP BY c2 ORDER BY 1, 2) x;
+ count | sum 
+-------+-----
+     1 | 100
+(1 row)
+
+-- Aggregate is still pushed down by taking unshippable expression out
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c2 * (random() <= 1)::int AS sum1, sum(c1) * c2 AS sum2 FROM fdw132_t1 GROUP BY c2 ORDER BY 1, 2;
+                                                     QUERY PLAN                                                      
+---------------------------------------------------------------------------------------------------------------------
+ Sort
+   Output: ((c2 * ((random() <= '1'::double precision))::integer)), ((sum(c1) * c2)), c2
+   Sort Key: ((fdw132_t1.c2 * ((random() <= '1'::double precision))::integer)), ((sum(fdw132_t1.c1) * fdw132_t1.c2))
+   ->  Foreign Scan
+         Output: (c2 * ((random() <= '1'::double precision))::integer), ((sum(c1) * c2)), c2
+         Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+         Remote query: SELECT (sum(`c1`) * `c2`), `c2` FROM `mysql_fdw_regress`.`test1` GROUP BY 2
+(7 rows)
+
+SELECT c2 * (random() <= 1)::int AS sum1, sum(c1) * c2 AS sum2 FROM fdw132_t1 GROUP BY c2 ORDER BY 1, 2;
+ sum1 | sum2 
+------+------
+  100 | 1400
+(1 row)
+
+-- Aggregate with unshippable GROUP BY clause are not pushed
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c2 * (random() <= 1)::int AS c2 FROM fdw132_t2 GROUP BY c2 * (random() <= 1)::int ORDER BY 1;
+                                     QUERY PLAN                                     
+------------------------------------------------------------------------------------
+ Sort
+   Output: ((c2 * ((random() <= '1'::double precision))::integer))
+   Sort Key: ((fdw132_t2.c2 * ((random() <= '1'::double precision))::integer))
+   ->  HashAggregate
+         Output: ((c2 * ((random() <= '1'::double precision))::integer))
+         Group Key: (fdw132_t2.c2 * ((random() <= '1'::double precision))::integer)
+         ->  Foreign Scan on public.fdw132_t2
+               Output: (c2 * ((random() <= '1'::double precision))::integer)
+               Remote query: SELECT `c2` FROM `mysql_fdw_regress`.`test2`
+(9 rows)
+
+SELECT c2 * (random() <= 1)::int AS c2 FROM fdw132_t2 GROUP BY c2 * (random() <= 1)::int ORDER BY 1;
+ c2  
+-----
+ 200
+(1 row)
+
+-- GROUP BY clause in various forms, cardinal, alias and constant expression
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT count(c2) w, c2 x, 5 y, 7.0 z FROM fdw132_t1 GROUP BY 2, y, 9.0::int ORDER BY 2;
+                                                              QUERY PLAN                                                               
+---------------------------------------------------------------------------------------------------------------------------------------
+ Foreign Scan
+   Output: (count(c2)), c2, 5, 7.0, 9
+   Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+   Remote query: SELECT count(`c2`), `c2`, 5, 7.0, 9 FROM `mysql_fdw_regress`.`test1` GROUP BY 2, 3, 5 ORDER BY `c2` IS NULL, `c2` ASC
+(4 rows)
+
+SELECT count(c2) w, c2 x, 5 y, 7.0 z FROM fdw132_t1 GROUP BY 2, y, 9.0::int ORDER BY 2;
+ w |  x  | y |  z  
+---+-----+---+-----
+ 3 | 100 | 5 | 7.0
+(1 row)
+
+-- Testing HAVING clause shippability
+SET enable_sort TO ON;
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c2, sum(c1) FROM fdw132_t2 GROUP BY c2 HAVING avg(c1) < 500 and sum(c1) < 49800 ORDER BY c2;
+                                                                               QUERY PLAN                                                                                
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Foreign Scan
+   Output: c2, (sum(c1))
+   Relations: Aggregate on (mysql_fdw_regress.fdw132_t2)
+   Remote query: SELECT `c2`, sum(`c1`) FROM `mysql_fdw_regress`.`test2` GROUP BY 1 HAVING ((avg(`c1`) < 500)) AND ((sum(`c1`) < 49800)) ORDER BY `c2` IS NULL, `c2` ASC
+(4 rows)
+
+SELECT c2, sum(c1) FROM fdw132_t2 GROUP BY c2 HAVING avg(c1) < 500 and sum(c1) < 49800 ORDER BY c2;
+ c2  | sum 
+-----+-----
+ 200 |  15
+(1 row)
+
+-- Using expressions in HAVING clause
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c3, count(c1) FROM fdw132_t1 GROUP BY c3 HAVING exp(max(c1)) = exp(2) ORDER BY 1, 2;
+                                                                                               QUERY PLAN                                                                                                
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Foreign Scan
+   Output: c3, (count(c1))
+   Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+   Remote query: SELECT `c3`, count(`c1`) FROM `mysql_fdw_regress`.`test1` GROUP BY 1 HAVING ((exp(max(`c1`)) = 7.38905609893065)) ORDER BY `c3` IS NULL, `c3` ASC, count(`c1`) IS NULL, count(`c1`) ASC
+(4 rows)
+
+SELECT c3, count(c1) FROM fdw132_t1 GROUP BY c3 HAVING exp(max(c1)) = exp(2) ORDER BY 1, 2;
+  c3  | count 
+------+-------
+ AAA2 |     1
+(1 row)
+
+SET enable_sort TO off;
+-- Unshippable HAVING clause will be evaluated locally, and other qual in HAVING clause is pushed down
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT count(*) FROM (SELECT c3, count(c1) FROM fdw132_t1 GROUP BY c3 HAVING (avg(c1) / avg(c1)) * random() <= 1 and avg(c1) < 500) x;
+                                                        QUERY PLAN                                                         
+---------------------------------------------------------------------------------------------------------------------------
+ Aggregate
+   Output: count(*)
+   ->  Foreign Scan
+         Output: fdw132_t1.c3, NULL::bigint
+         Filter: (((((avg(fdw132_t1.c1)) / (avg(fdw132_t1.c1))))::double precision * random()) <= '1'::double precision)
+         Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+         Remote query: SELECT `c3`, NULL, avg(`c1`) FROM `mysql_fdw_regress`.`test1` GROUP BY 1 HAVING ((avg(`c1`) < 500))
+(7 rows)
+
+SELECT count(*) FROM (SELECT c3, count(c1) FROM fdw132_t1 GROUP BY c3 HAVING (avg(c1) / avg(c1)) * random() <= 1 and avg(c1) < 500) x;
+ count 
+-------
+     3
+(1 row)
+
+-- Aggregate in HAVING clause is not pushable, and thus aggregation is not pushed down
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(c1) FROM fdw132_t1 GROUP BY c2 HAVING avg(c1 * (random() <= 1)::int) > 1 ORDER BY 1;
+                                                   QUERY PLAN                                                   
+----------------------------------------------------------------------------------------------------------------
+ Sort
+   Output: (sum(c1)), c2
+   Sort Key: (sum(fdw132_t1.c1))
+   ->  GroupAggregate
+         Output: sum(c1), c2
+         Group Key: fdw132_t1.c2
+         Filter: (avg((fdw132_t1.c1 * ((random() <= '1'::double precision))::integer)) > '1'::numeric)
+         ->  Foreign Scan on public.fdw132_t1
+               Output: c1, c2, c3, c4
+               Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` ORDER BY `c2` IS NULL, `c2` ASC
+(10 rows)
+
+SELECT sum(c1) FROM fdw132_t1 GROUP BY c2 HAVING avg(c1 * (random() <= 1)::int) > 1 ORDER BY 1;
+ sum 
+-----
+  14
+(1 row)
+
+-- Testing ORDER BY, DISTINCT, FILTER, Ordered-sets and VARIADIC within aggregates
+-- ORDER BY within aggregates (same column used to order) are not pushed
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(c1 ORDER BY c1) FROM fdw132_t1 WHERE c1 < 100 GROUP BY c2 ORDER BY 1;
+                                                                         QUERY PLAN                                                                          
+-------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Sort
+   Output: (sum(c1 ORDER BY c1)), c2
+   Sort Key: (sum(fdw132_t1.c1 ORDER BY fdw132_t1.c1))
+   ->  GroupAggregate
+         Output: sum(c1 ORDER BY c1), c2
+         Group Key: fdw132_t1.c2
+         ->  Foreign Scan on public.fdw132_t1
+               Output: c1, c2, c3, c4
+               Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c1` < 100)) ORDER BY `c2` IS NULL, `c2` ASC, `c1` IS NULL, `c1` ASC
+(9 rows)
+
+SELECT sum(c1 ORDER BY c1) FROM fdw132_t1 WHERE c1 < 100 GROUP BY c2 ORDER BY 1;
+ sum 
+-----
+  14
+(1 row)
+
+-- ORDER BY within aggregate (different column used to order also using DESC)
+-- are not pushed.
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(c2 ORDER BY c1 desc) FROM fdw132_t2 WHERE c1 > 1 and c2 > 50;
+                                                                     QUERY PLAN                                                                     
+----------------------------------------------------------------------------------------------------------------------------------------------------
+ Aggregate
+   Output: sum(c2 ORDER BY c1 DESC)
+   ->  Foreign Scan on public.fdw132_t2
+         Output: c1, c2, c3, c4
+         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test2` WHERE ((`c1` > 1)) AND ((`c2` > 50)) ORDER BY `c1` IS NOT NULL, `c1` DESC
+(5 rows)
+
+SELECT sum(c2 ORDER BY c1 desc) FROM fdw132_t2 WHERE c1 > 1 and c2 > 50;
+ sum 
+-----
+ 400
+(1 row)
+
+-- DISTINCT within aggregate
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(DISTINCT (c1)%5) FROM fdw132_t2 WHERE c2 = 200 and c1 < 50;
+                                                       QUERY PLAN                                                        
+-------------------------------------------------------------------------------------------------------------------------
+ Foreign Scan
+   Output: (sum(DISTINCT (c1 % 5)))
+   Relations: Aggregate on (mysql_fdw_regress.fdw132_t2)
+   Remote query: SELECT sum(DISTINCT (`c1` % 5)) FROM `mysql_fdw_regress`.`test2` WHERE ((`c1` < 50)) AND ((`c2` = 200))
+(4 rows)
+
+SELECT sum(DISTINCT (c1)%5) FROM fdw132_t2 WHERE c2 = 200 and c1 < 50;
+ sum 
+-----
+   3
+(1 row)
+
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(DISTINCT (t1.c1)%5) FROM fdw132_t1 t1 join fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c1 < 20 or (t1.c1 is null and t2.c1 < 5) GROUP BY (t2.c1)%3 ORDER BY 1;
+                                                                                                                                                                                     QUERY PLAN                                                                                                                                                                                      
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Foreign Scan
+   Output: (sum(DISTINCT (t1.c1 % 5))), ((t2.c1 % 3))
+   Relations: Aggregate on ((mysql_fdw_regress.fdw132_t1 t1) INNER JOIN (mysql_fdw_regress.fdw132_t2 t2))
+   Remote query: SELECT sum(DISTINCT (r1.`c1` % 5)), (r2.`c1` % 3) FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON ((((r1.`c1` < 20) OR ((r1.`c1` IS NULL) AND (r2.`c1` < 5)))) AND ((r1.`c1` = r2.`c1`)))) WHERE (((r1.`c1` < 20) OR (r1.`c1` IS NULL))) GROUP BY 2 ORDER BY sum(DISTINCT (r1.`c1` % 5)) IS NULL, sum(DISTINCT (r1.`c1` % 5)) ASC
+(4 rows)
+
+SELECT sum(DISTINCT (t1.c1)%5) FROM fdw132_t1 t1 join fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c1 < 20 or (t1.c1 is null and t2.c1 < 5) GROUP BY (t2.c1)%3 ORDER BY 1;
+ sum 
+-----
+   1
+   2
+(2 rows)
+
+-- DISTINCT with aggregate within aggregate
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(DISTINCT c1) FROM fdw132_t2 WHERE c2 = 200 and c1 < 50;
+                                                    QUERY PLAN                                                     
+-------------------------------------------------------------------------------------------------------------------
+ Foreign Scan
+   Output: (sum(DISTINCT c1))
+   Relations: Aggregate on (mysql_fdw_regress.fdw132_t2)
+   Remote query: SELECT sum(DISTINCT `c1`) FROM `mysql_fdw_regress`.`test2` WHERE ((`c1` < 50)) AND ((`c2` = 200))
+(4 rows)
+
+SELECT sum(DISTINCT c1) FROM fdw132_t2 WHERE c2 = 200 and c1 < 50;
+ sum 
+-----
+  15
+(1 row)
+
+-- DISTINCT combined with ORDER BY within aggregate is not pushed.
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT array_agg(DISTINCT (t1.c1)%5 ORDER BY (t1.c1)%5) FROM fdw132_t1 t1 join fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c1 < 20 or (t1.c1 is null and t2.c1 < 5) GROUP BY (t2.c1)%3 ORDER BY 1;
+                                                                                                                                                                                QUERY PLAN                                                                                                                                                                                
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Sort
+   Output: (array_agg(DISTINCT (t1.c1 % 5) ORDER BY (t1.c1 % 5))), ((t2.c1 % 3))
+   Sort Key: (array_agg(DISTINCT (t1.c1 % 5) ORDER BY (t1.c1 % 5)))
+   ->  GroupAggregate
+         Output: array_agg(DISTINCT (t1.c1 % 5) ORDER BY (t1.c1 % 5)), ((t2.c1 % 3))
+         Group Key: (t2.c1 % 3)
+         ->  Foreign Scan
+               Output: (t2.c1 % 3), t1.c1
+               Relations: (mysql_fdw_regress.fdw132_t1 t1) INNER JOIN (mysql_fdw_regress.fdw132_t2 t2)
+               Remote query: SELECT r2.`c1`, r1.`c1` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON ((((r1.`c1` < 20) OR ((r1.`c1` IS NULL) AND (r2.`c1` < 5)))) AND ((r1.`c1` = r2.`c1`)))) WHERE (((r1.`c1` < 20) OR (r1.`c1` IS NULL))) ORDER BY (r2.`c1` % 3) IS NULL, (r2.`c1` % 3) ASC, (r1.`c1` % 5) IS NULL, (r1.`c1` % 5) ASC
+(10 rows)
+
+SELECT array_agg(DISTINCT (t1.c1)%5 ORDER BY (t1.c1)%5) FROM fdw132_t1 t1 join fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c1 < 20 or (t1.c1 is null and t2.c1 < 5) GROUP BY (t2.c1)%3 ORDER BY 1;
+ array_agg 
+-----------
+ {1}
+ {2}
+(2 rows)
+
+-- DISTINCT, ORDER BY and FILTER within aggregate, not pushed down.
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(c1%3), sum(DISTINCT c1%3 ORDER BY c1%3) filter (WHERE c1%3 < 2), c2 FROM fdw132_t1 WHERE c2 = 100 GROUP BY c2;
+                                             QUERY PLAN                                              
+-----------------------------------------------------------------------------------------------------
+ GroupAggregate
+   Output: sum((c1 % 3)), sum(DISTINCT (c1 % 3) ORDER BY (c1 % 3)) FILTER (WHERE ((c1 % 3) < 2)), c2
+   ->  Foreign Scan on public.fdw132_t1
+         Output: c1, c2, c3, c4
+         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` = 100))
+(5 rows)
+
+SELECT sum(c1%3), sum(DISTINCT c1%3 ORDER BY c1%3) filter (WHERE c1%3 < 2), c2 FROM fdw132_t1 WHERE c2 = 100 GROUP BY c2;
+ sum | sum | c2  
+-----+-----+-----
+   5 |   1 | 100
+(1 row)
+
+-- FILTER within aggregate, not pushed
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(c1) filter (WHERE c1 < 100 and c2 > 5) FROM fdw132_t1 GROUP BY c2 ORDER BY 1 nulls last;
+                                                   QUERY PLAN                                                   
+----------------------------------------------------------------------------------------------------------------
+ Sort
+   Output: (sum(c1) FILTER (WHERE ((c1 < 100) AND (c2 > 5)))), c2
+   Sort Key: (sum(fdw132_t1.c1) FILTER (WHERE ((fdw132_t1.c1 < 100) AND (fdw132_t1.c2 > 5))))
+   ->  GroupAggregate
+         Output: sum(c1) FILTER (WHERE ((c1 < 100) AND (c2 > 5))), c2
+         Group Key: fdw132_t1.c2
+         ->  Foreign Scan on public.fdw132_t1
+               Output: c1, c2, c3, c4
+               Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` ORDER BY `c2` IS NULL, `c2` ASC
+(9 rows)
+
+SELECT sum(c1) filter (WHERE c1 < 100 and c2 > 5) FROM fdw132_t1 GROUP BY c2 ORDER BY 1 nulls last;
+ sum 
+-----
+  14
+(1 row)
+
+-- Outer query is aggregation query
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT DISTINCT (SELECT count(*) filter (WHERE t2.c2 = 200 and t2.c1 < 10) FROM fdw132_t1 t1 WHERE t1.c1 = 2) FROM fdw132_t2 t2 ORDER BY 1;
+                                             QUERY PLAN                                              
+-----------------------------------------------------------------------------------------------------
+ Unique
+   Output: ((SubPlan 1))
+   ->  Sort
+         Output: ((SubPlan 1))
+         Sort Key: ((SubPlan 1))
+         ->  Aggregate
+               Output: (SubPlan 1)
+               ->  Foreign Scan on public.fdw132_t2 t2
+                     Output: t2.c1, t2.c2, t2.c3, t2.c4
+                     Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test2`
+               SubPlan 1
+                 ->  Foreign Scan on public.fdw132_t1 t1
+                       Output: count(*) FILTER (WHERE ((t2.c2 = 200) AND (t2.c1 < 10)))
+                       Remote query: SELECT NULL FROM `mysql_fdw_regress`.`test1` WHERE ((`c1` = 2))
+(14 rows)
+
+SELECT DISTINCT (SELECT count(*) filter (WHERE t2.c2 = 200 and t2.c1 < 10) FROM fdw132_t1 t1 WHERE t1.c1 = 2) FROM fdw132_t2 t2 ORDER BY 1;
+ count 
+-------
+     2
+(1 row)
+
+-- Inner query is aggregation query
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT DISTINCT (SELECT count(t1.c1) filter (WHERE t2.c2 = 200 and t2.c1 < 10) FROM fdw132_t1 t1 WHERE t1.c1 = 2) FROM fdw132_t2 t2 ORDER BY 1;
+                                                QUERY PLAN                                                 
+-----------------------------------------------------------------------------------------------------------
+ Unique
+   Output: ((SubPlan 1))
+   ->  Sort
+         Output: ((SubPlan 1))
+         Sort Key: ((SubPlan 1))
+         ->  Foreign Scan on public.fdw132_t2 t2
+               Output: (SubPlan 1)
+               Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test2`
+               SubPlan 1
+                 ->  Aggregate
+                       Output: count(t1.c1) FILTER (WHERE ((t2.c2 = 200) AND (t2.c1 < 10)))
+                       ->  Foreign Scan on public.fdw132_t1 t1
+                             Output: t1.c1, t1.c2, t1.c3, t1.c4
+                             Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` WHERE ((`c1` = 2))
+(14 rows)
+
+SELECT DISTINCT (SELECT count(t1.c1) filter (WHERE t2.c2 = 200 and t2.c1 < 10) FROM fdw132_t1 t1 WHERE t1.c1 = 2) FROM fdw132_t2 t2 ORDER BY 1;
+ count 
+-------
+     0
+     1
+(2 rows)
+
+-- Ordered-sets within aggregate, not pushed down.
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c2, rank('10'::varchar) within group (ORDER BY c3), percentile_cont(c2/200::numeric) within group (ORDER BY c1) FROM fdw132_t2 GROUP BY c2 HAVING percentile_cont(c2/200::numeric) within group (ORDER BY c1) < 500 ORDER BY c2;
+                                                                                    QUERY PLAN                                                                                     
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ GroupAggregate
+   Output: c2, rank('10'::text) WITHIN GROUP (ORDER BY c3), percentile_cont((((c2)::numeric / '200'::numeric))::double precision) WITHIN GROUP (ORDER BY ((c1)::double precision))
+   Group Key: fdw132_t2.c2
+   Filter: (percentile_cont((((fdw132_t2.c2)::numeric / '200'::numeric))::double precision) WITHIN GROUP (ORDER BY ((fdw132_t2.c1)::double precision)) < '500'::double precision)
+   ->  Foreign Scan on public.fdw132_t2
+         Output: c1, c2, c3, c4
+         Remote query: SELECT `c1`, `c2`, `c3` FROM `mysql_fdw_regress`.`test2` ORDER BY `c2` IS NULL, `c2` ASC
+(7 rows)
+
+SELECT c2, rank('10'::varchar) within group (ORDER BY c3), percentile_cont(c2/200::numeric) within group (ORDER BY c1) FROM fdw132_t2 GROUP BY c2 HAVING percentile_cont(c2/200::numeric) within group (ORDER BY c1) < 500 ORDER BY c2;
+ c2  | rank | percentile_cont 
+-----+------+-----------------
+ 200 |    1 |              12
+(1 row)
+
+-- Using multiple arguments within aggregates
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c1, rank(c1, c2) within group (ORDER BY c1, c2) FROM fdw132_t1 GROUP BY c1, c2 HAVING c1 = 2 ORDER BY 1;
+                                                         QUERY PLAN                                                          
+-----------------------------------------------------------------------------------------------------------------------------
+ GroupAggregate
+   Output: c1, rank(c1, c2) WITHIN GROUP (ORDER BY c1, c2), c2
+   Group Key: fdw132_t1.c2
+   ->  Foreign Scan on public.fdw132_t1
+         Output: c1, c2, c3, c4
+         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c1` = 2)) ORDER BY `c2` IS NULL, `c2` ASC
+(6 rows)
+
+SELECT c1, rank(c1, c2) within group (ORDER BY c1, c2) FROM fdw132_t1 GROUP BY c1, c2 HAVING c1 = 2 ORDER BY 1;
+ c1 | rank 
+----+------
+  2 |    1
+(1 row)
+
+-- Subquery in FROM clause HAVING aggregate
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT count(*), x.b FROM fdw132_t1, (SELECT c1 a, sum(c1) b FROM fdw132_t2 GROUP BY c1) x WHERE fdw132_t1.c1 = x.a GROUP BY x.b ORDER BY 1, 2;
+                                                    QUERY PLAN                                                    
+------------------------------------------------------------------------------------------------------------------
+ Sort
+   Output: (count(*)), x.b
+   Sort Key: (count(*)), x.b
+   ->  HashAggregate
+         Output: count(*), x.b
+         Group Key: x.b
+         ->  Hash Join
+               Output: x.b
+               Inner Unique: true
+               Hash Cond: (fdw132_t1.c1 = x.a)
+               ->  Foreign Scan on public.fdw132_t1
+                     Output: fdw132_t1.c1, fdw132_t1.c2, fdw132_t1.c3, fdw132_t1.c4
+                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` ORDER BY `c1` IS NULL, `c1` ASC
+               ->  Hash
+                     Output: x.b, x.a
+                     ->  Subquery Scan on x
+                           Output: x.b, x.a
+                           ->  Foreign Scan
+                                 Output: fdw132_t2.c1, (sum(fdw132_t2.c1))
+                                 Relations: Aggregate on (mysql_fdw_regress.fdw132_t2)
+                                 Remote query: SELECT `c1`, sum(`c1`) FROM `mysql_fdw_regress`.`test2` GROUP BY 1
+(21 rows)
+
+SELECT count(*), x.b FROM fdw132_t1, (SELECT c1 a, sum(c1) b FROM fdw132_t2 GROUP BY c1) x WHERE fdw132_t1.c1 = x.a GROUP BY x.b ORDER BY 1, 2;
+ count | b 
+-------+---
+     1 | 1
+     1 | 2
+(2 rows)
+
+-- Join with IS NULL check in HAVING
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT avg(t1.c1), sum(t2.c1) FROM fdw132_t1 t1 join fdw132_t2 t2 ON (t1.c1 = t2.c1) GROUP BY t2.c1 HAVING (avg(t1.c1) is null and sum(t2.c1) > 10) or sum(t2.c1) is null ORDER BY 1 nulls last, 2;
+                                                                                                                                                                           QUERY PLAN                                                                                                                                                                           
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Foreign Scan
+   Output: (avg(t1.c1)), (sum(t2.c1)), t2.c1
+   Relations: Aggregate on ((mysql_fdw_regress.fdw132_t1 t1) INNER JOIN (mysql_fdw_regress.fdw132_t2 t2))
+   Remote query: SELECT avg(r1.`c1`), sum(r2.`c1`), r2.`c1` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (((r1.`c1` = r2.`c1`)))) GROUP BY 3 HAVING ((((avg(r1.`c1`) IS NULL) AND (sum(r2.`c1`) > 10)) OR (sum(r2.`c1`) IS NULL))) ORDER BY avg(r1.`c1`) IS NULL, avg(r1.`c1`) ASC, sum(r2.`c1`) IS NULL, sum(r2.`c1`) ASC
+(4 rows)
+
+SELECT avg(t1.c1), sum(t2.c1) FROM fdw132_t1 t1 join fdw132_t2 t2 ON (t1.c1 = t2.c1) GROUP BY t2.c1 HAVING (avg(t1.c1) is null and sum(t2.c1) > 10) or sum(t2.c1) is null ORDER BY 1 nulls last, 2;
+ avg | sum 
+-----+-----
+(0 rows)
+
+-- ORDER BY expression is part of the target list but not pushed down to
+-- foreign server.
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(c2) * (random() <= 1)::int AS sum FROM fdw132_t1 ORDER BY 1;
+                                      QUERY PLAN                                      
+--------------------------------------------------------------------------------------
+ Sort
+   Output: (((sum(c2)) * ((random() <= '1'::double precision))::integer))
+   Sort Key: (((sum(fdw132_t1.c2)) * ((random() <= '1'::double precision))::integer))
+   ->  Foreign Scan
+         Output: ((sum(c2)) * ((random() <= '1'::double precision))::integer)
+         Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+         Remote query: SELECT sum(`c2`) FROM `mysql_fdw_regress`.`test1`
+(7 rows)
+
+SELECT sum(c2) * (random() <= 1)::int AS sum FROM fdw132_t1 ORDER BY 1;
+ sum 
+-----
+ 300
+(1 row)
+
+-- LATERAL join, with parameterization
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c2, sum FROM fdw132_t1 t1, lateral (SELECT sum(t2.c1 + t1.c1) sum FROM fdw132_t2 t2 GROUP BY t2.c1) qry WHERE t1.c2 * 2 = qry.sum and t1.c2 > 10 ORDER BY 1;
+                                                          QUERY PLAN                                                          
+------------------------------------------------------------------------------------------------------------------------------
+ Nested Loop
+   Output: t1.c2, qry.sum
+   ->  Foreign Scan on public.fdw132_t1 t1
+         Output: t1.c1, t1.c2, t1.c3, t1.c4
+         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 10)) ORDER BY `c2` IS NULL, `c2` ASC
+   ->  Subquery Scan on qry
+         Output: qry.sum, t2.c1
+         Filter: ((t1.c2 * 2) = qry.sum)
+         ->  GroupAggregate
+               Output: sum((t2.c1 + t1.c1)), t2.c1
+               Group Key: t2.c1
+               ->  Foreign Scan on public.fdw132_t2 t2
+                     Output: t2.c1, t2.c2, t2.c3, t2.c4
+                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test2` ORDER BY `c1` IS NULL, `c1` ASC
+(14 rows)
+
+-- Check with placeHolderVars
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT q.b, count(fdw132_t1.c1), sum(q.a) FROM fdw132_t1 left join (SELECT min(13), avg(fdw132_t1.c1), sum(fdw132_t2.c1) FROM fdw132_t1 right join fdw132_t2 ON (fdw132_t1.c1 = fdw132_t2.c1) WHERE fdw132_t1.c1 = 12) q(a, b, c) ON (fdw132_t1.c1 = q.b) WHERE fdw132_t1.c1 between 10 and 15 GROUP BY q.b ORDER BY 1 nulls last, 2;
+                                                                                                       QUERY PLAN                                                                                                       
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Sort
+   Output: q.b, (count(fdw132_t1.c1)), (sum(q.a))
+   Sort Key: q.b, (count(fdw132_t1.c1))
+   ->  HashAggregate
+         Output: q.b, count(fdw132_t1.c1), sum(q.a)
+         Group Key: q.b
+         ->  Hash Left Join
+               Output: q.b, fdw132_t1.c1, q.a
+               Inner Unique: true
+               Hash Cond: ((fdw132_t1.c1)::numeric = q.b)
+               ->  Foreign Scan on public.fdw132_t1
+                     Output: fdw132_t1.c1, fdw132_t1.c2, fdw132_t1.c3, fdw132_t1.c4
+                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` WHERE ((`c1` >= 10)) AND ((`c1` <= 15)) ORDER BY `c1` IS NULL, `c1` ASC
+               ->  Hash
+                     Output: q.b, q.a
+                     ->  Subquery Scan on q
+                           Output: q.b, q.a
+                           ->  Foreign Scan
+                                 Output: (min(13)), (avg(fdw132_t1_1.c1)), NULL::bigint
+                                 Relations: Aggregate on ((mysql_fdw_regress.fdw132_t1) INNER JOIN (mysql_fdw_regress.fdw132_t2))
+                                 Remote query: SELECT min(13), avg(r1.`c1`), NULL FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (TRUE)) WHERE ((r2.`c1` = 12)) AND ((r1.`c1` = 12))
+(21 rows)
+
+SELECT q.b, count(fdw132_t1.c1), sum(q.a) FROM fdw132_t1 left join (SELECT min(13), avg(fdw132_t1.c1), sum(fdw132_t2.c1) FROM fdw132_t1 right join fdw132_t2 ON (fdw132_t1.c1 = fdw132_t2.c1) WHERE fdw132_t1.c1 = 12) q(a, b, c) ON (fdw132_t1.c1 = q.b) WHERE fdw132_t1.c1 between 10 and 15 GROUP BY q.b ORDER BY 1 nulls last, 2;
+ b | count | sum 
+---+-------+-----
+   |     1 |    
+(1 row)
+
+-- Not supported cases
+-- Grouping sets
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c2, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY rollup(c2) ORDER BY 1 nulls last;
+                                                         QUERY PLAN                                                          
+-----------------------------------------------------------------------------------------------------------------------------
+ GroupAggregate
+   Output: c2, sum(c1)
+   Group Key: fdw132_t1.c2
+   Group Key: ()
+   ->  Foreign Scan on public.fdw132_t1
+         Output: c1, c2, c3, c4
+         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 3)) ORDER BY `c2` IS NULL, `c2` ASC
+(7 rows)
+
+SELECT c2, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY rollup(c2) ORDER BY 1 nulls last;
+ c2  | sum 
+-----+-----
+ 100 |  14
+     |  14
+(2 rows)
+
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c2, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY cube(c2) ORDER BY 1 nulls last;
+                                                         QUERY PLAN                                                          
+-----------------------------------------------------------------------------------------------------------------------------
+ GroupAggregate
+   Output: c2, sum(c1)
+   Group Key: fdw132_t1.c2
+   Group Key: ()
+   ->  Foreign Scan on public.fdw132_t1
+         Output: c1, c2, c3, c4
+         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 3)) ORDER BY `c2` IS NULL, `c2` ASC
+(7 rows)
+
+SELECT c2, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY cube(c2) ORDER BY 1 nulls last;
+ c2  | sum 
+-----+-----
+ 100 |  14
+     |  14
+(2 rows)
+
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c2, c3, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY grouping sets(c2, c3) ORDER BY 1 nulls last, 2 nulls last;
+                                                               QUERY PLAN                                                                
+-----------------------------------------------------------------------------------------------------------------------------------------
+ Sort
+   Output: c2, c3, (sum(c1))
+   Sort Key: fdw132_t1.c2, fdw132_t1.c3 COLLATE "C"
+   ->  MixedAggregate
+         Output: c2, c3, sum(c1)
+         Hash Key: fdw132_t1.c3
+         Group Key: fdw132_t1.c2
+         ->  Foreign Scan on public.fdw132_t1
+               Output: c1, c2, c3, c4
+               Remote query: SELECT `c1`, `c2`, `c3` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 3)) ORDER BY `c2` IS NULL, `c2` ASC
+(10 rows)
+
+SELECT c2, c3, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY grouping sets(c2, c3) ORDER BY 1 nulls last, 2 nulls last;
+ c2  |  c3   | sum 
+-----+-------+-----
+ 100 |       |  14
+     | AAA1  |   1
+     | AAA11 |  11
+     | AAA2  |   2
+(4 rows)
+
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c2, sum(c1), grouping(c2) FROM fdw132_t1 WHERE c2 > 3 GROUP BY c2 ORDER BY 1 nulls last;
+                                                         QUERY PLAN                                                          
+-----------------------------------------------------------------------------------------------------------------------------
+ GroupAggregate
+   Output: c2, sum(c1), GROUPING(c2)
+   Group Key: fdw132_t1.c2
+   ->  Foreign Scan on public.fdw132_t1
+         Output: c1, c2, c3, c4
+         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 3)) ORDER BY `c2` IS NULL, `c2` ASC
+(6 rows)
+
+SELECT c2, sum(c1), grouping(c2) FROM fdw132_t1 WHERE c2 > 3 GROUP BY c2 ORDER BY 1 nulls last;
+ c2  | sum | grouping 
+-----+-----+----------
+ 100 |  14 |        0
+(1 row)
+
+-- DISTINCT itself is not pushed down, whereas underneath aggregate is pushed
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT DISTINCT sum(c1) s FROM fdw132_t1 WHERE c2 > 6 GROUP BY c2 ORDER BY 1;
+                                                    QUERY PLAN                                                     
+-------------------------------------------------------------------------------------------------------------------
+ Unique
+   Output: (sum(c1)), c2
+   ->  Sort
+         Output: (sum(c1)), c2
+         Sort Key: (sum(fdw132_t1.c1))
+         ->  Foreign Scan
+               Output: (sum(c1)), c2
+               Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+               Remote query: SELECT sum(`c1`), `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 6)) GROUP BY 2
+(9 rows)
+
+SELECT DISTINCT sum(c1) s FROM fdw132_t1 WHERE c2 > 6 GROUP BY c2 ORDER BY 1;
+ s  
+----
+ 14
+(1 row)
+
+-- WindowAgg
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c2, sum(c2), count(c2) over (partition by c2%2) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
+                                                              QUERY PLAN                                                              
+--------------------------------------------------------------------------------------------------------------------------------------
+ Sort
+   Output: c2, (sum(c2)), (count(c2) OVER (?)), ((c2 % 2))
+   Sort Key: fdw132_t1.c2
+   ->  WindowAgg
+         Output: c2, (sum(c2)), count(c2) OVER (?), ((c2 % 2))
+         ->  Sort
+               Output: c2, ((c2 % 2)), (sum(c2))
+               Sort Key: ((fdw132_t1.c2 % 2))
+               ->  Foreign Scan
+                     Output: c2, ((c2 % 2)), (sum(c2))
+                     Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+                     Remote query: SELECT `c2`, (`c2` % 2), sum(`c2`) FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 10)) GROUP BY 1
+(12 rows)
+
+SELECT c2, sum(c2), count(c2) over (partition by c2%2) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
+ c2  | sum | count 
+-----+-----+-------
+ 100 | 300 |     1
+(1 row)
+
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c2, array_agg(c2) over (partition by c2%2 ORDER BY c2 desc) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
+                                                        QUERY PLAN                                                         
+---------------------------------------------------------------------------------------------------------------------------
+ Sort
+   Output: c2, (array_agg(c2) OVER (?)), ((c2 % 2))
+   Sort Key: fdw132_t1.c2
+   ->  WindowAgg
+         Output: c2, array_agg(c2) OVER (?), ((c2 % 2))
+         ->  Sort
+               Output: c2, ((c2 % 2))
+               Sort Key: ((fdw132_t1.c2 % 2)), fdw132_t1.c2 DESC
+               ->  Foreign Scan
+                     Output: c2, ((c2 % 2))
+                     Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+                     Remote query: SELECT `c2`, (`c2` % 2) FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 10)) GROUP BY 1
+(12 rows)
+
+SELECT c2, array_agg(c2) over (partition by c2%2 ORDER BY c2 desc) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
+ c2  | array_agg 
+-----+-----------
+ 100 | {100}
+(1 row)
+
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c2, array_agg(c2) over (partition by c2%2 ORDER BY c2 range between current row and unbounded following) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
+                                                        QUERY PLAN                                                         
+---------------------------------------------------------------------------------------------------------------------------
+ Sort
+   Output: c2, (array_agg(c2) OVER (?)), ((c2 % 2))
+   Sort Key: fdw132_t1.c2
+   ->  WindowAgg
+         Output: c2, array_agg(c2) OVER (?), ((c2 % 2))
+         ->  Sort
+               Output: c2, ((c2 % 2))
+               Sort Key: ((fdw132_t1.c2 % 2)), fdw132_t1.c2
+               ->  Foreign Scan
+                     Output: c2, ((c2 % 2))
+                     Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+                     Remote query: SELECT `c2`, (`c2` % 2) FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 10)) GROUP BY 1
+(12 rows)
+
+SELECT c2, array_agg(c2) over (partition by c2%2 ORDER BY c2 range between current row and unbounded following) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
+ c2  | array_agg 
+-----+-----------
+ 100 | {100}
+(1 row)
+
+-- User defined function for user defined aggregate, VARIADIC
+CREATE FUNCTION least_accum(anyelement, variadic anyarray)
+returns anyelement language sql AS
+  'SELECT least($1, min($2[i])) FROM generate_subscripts($2,2) g(i)';
+CREATE aggregate least_agg(variadic items anyarray) (
+  stype = anyelement, sfunc = least_accum
+);
+-- Not pushed down due to user defined aggregate
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c2, least_agg(c1) FROM fdw132_t1 GROUP BY c2 ORDER BY c2;
+                                                QUERY PLAN                                                
+----------------------------------------------------------------------------------------------------------
+ GroupAggregate
+   Output: c2, least_agg(VARIADIC ARRAY[c1])
+   Group Key: fdw132_t1.c2
+   ->  Foreign Scan on public.fdw132_t1
+         Output: c1, c2, c3, c4
+         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` ORDER BY `c2` IS NULL, `c2` ASC
+(6 rows)
+
+SELECT c2, least_agg(c1) FROM fdw132_t1 GROUP BY c2 ORDER BY c2;
+ c2  | least_agg 
+-----+-----------
+ 100 |          
+(1 row)
+
+-- FDW-129: Limit and offset pushdown with Aggregate pushdown.
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT 1 OFFSET 1;
+                                                             QUERY PLAN                                                              
+-------------------------------------------------------------------------------------------------------------------------------------
+ Foreign Scan
+   Output: (sum(c1)), c1
+   Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+   Remote query: SELECT sum(`c1`), `c1` FROM `mysql_fdw_regress`.`test1` GROUP BY 2 ORDER BY `c1` IS NULL, `c1` ASC LIMIT 1 OFFSET 1
+(4 rows)
+
+SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT 1 OFFSET 1;
+ sum | c1 
+-----+----
+   2 |  2
+(1 row)
+
+-- Limit 0, Offset 0 with aggregates.
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT 0 OFFSET 0;
+                                                             QUERY PLAN                                                              
+-------------------------------------------------------------------------------------------------------------------------------------
+ Foreign Scan
+   Output: (sum(c1)), c1
+   Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+   Remote query: SELECT sum(`c1`), `c1` FROM `mysql_fdw_regress`.`test1` GROUP BY 2 ORDER BY `c1` IS NULL, `c1` ASC LIMIT 0 OFFSET 0
+(4 rows)
+
+SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT 0 OFFSET 0;
+ sum | c1 
+-----+----
+(0 rows)
+
+-- Limit NULL
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT NULL OFFSET 2;
+                                                        QUERY PLAN                                                        
+--------------------------------------------------------------------------------------------------------------------------
+ Limit
+   Output: (sum(c1)), c1
+   ->  Foreign Scan
+         Output: (sum(c1)), c1
+         Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+         Remote query: SELECT sum(`c1`), `c1` FROM `mysql_fdw_regress`.`test1` GROUP BY 2 ORDER BY `c1` IS NULL, `c1` ASC
+(6 rows)
+
+SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT NULL OFFSET 2;
+ sum | c1 
+-----+----
+  11 | 11
+(1 row)
+
+-- Limit ALL
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT ALL OFFSET 2;
+                                                        QUERY PLAN                                                        
+--------------------------------------------------------------------------------------------------------------------------
+ Limit
+   Output: (sum(c1)), c1
+   ->  Foreign Scan
+         Output: (sum(c1)), c1
+         Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
+         Remote query: SELECT sum(`c1`), `c1` FROM `mysql_fdw_regress`.`test1` GROUP BY 2 ORDER BY `c1` IS NULL, `c1` ASC
+(6 rows)
+
+SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT ALL OFFSET 2;
+ sum | c1 
+-----+----
+  11 | 11
+(1 row)
+
+-- Delete existing data and load new data for partition-wise aggregate test
+-- cases.
+DELETE FROM fdw132_t1;
+DELETE FROM fdw132_t2;
+INSERT INTO fdw132_t1 values(1, 1, 'AAA1', 'foo');
+INSERT INTO fdw132_t1 values(2, 2, 'AAA2', 'bar');
+INSERT INTO fdw132_t2 values(3, 3, 'AAA11', 'foo');
+INSERT INTO fdw132_t2 values(4, 4, 'AAA12', 'foo');
+-- Test partition-wise aggregates
+SET enable_partitionwise_aggregate TO on;
+-- Create the partition table.
+CREATE TABLE fprt1 (c1 int, c2 int, c3 varchar, c4 varchar) PARTITION BY RANGE(c1);
+CREATE FOREIGN TABLE ftprt1_p1 PARTITION OF fprt1 FOR VALUES FROM (1) TO (2)
+  SERVER mysql_svr OPTIONS (dbname 'mysql_fdw_regress', table_name 'test1');
+CREATE FOREIGN TABLE ftprt1_p2 PARTITION OF fprt1 FOR VALUES FROM (3) TO (4)
+  SERVER mysql_svr OPTIONS (dbname 'mysql_fdw_regress', TABLE_NAME 'test2');
+-- Plan with partitionwise aggregates is enabled
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c1, sum(c1) FROM fprt1 GROUP BY c1 ORDER BY 2;
+                                           QUERY PLAN                                           
+------------------------------------------------------------------------------------------------
+ Sort
+   Output: fprt1.c1, (sum(fprt1.c1))
+   Sort Key: (sum(fprt1.c1))
+   ->  Append
+         ->  Foreign Scan
+               Output: fprt1.c1, (sum(fprt1.c1))
+               Relations: Aggregate on (mysql_fdw_regress.ftprt1_p1 fprt1)
+               Remote query: SELECT `c1`, sum(`c1`) FROM `mysql_fdw_regress`.`test1` GROUP BY 1
+         ->  Foreign Scan
+               Output: fprt1_1.c1, (sum(fprt1_1.c1))
+               Relations: Aggregate on (mysql_fdw_regress.ftprt1_p2 fprt1)
+               Remote query: SELECT `c1`, sum(`c1`) FROM `mysql_fdw_regress`.`test2` GROUP BY 1
+(12 rows)
+
+SELECT c1, sum(c1) FROM fprt1 GROUP BY c1 ORDER BY 2;
+ c1 | sum 
+----+-----
+  1 |   1
+  2 |   2
+  3 |   3
+  4 |   4
+(4 rows)
+
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c1, sum(c2), min(c2), count(*) FROM fprt1 GROUP BY c1 HAVING avg(c2) < 22 ORDER BY 2;
+                                                                  QUERY PLAN                                                                   
+-----------------------------------------------------------------------------------------------------------------------------------------------
+ Sort
+   Output: fprt1.c1, (sum(fprt1.c2)), (min(fprt1.c2)), (count(*))
+   Sort Key: (sum(fprt1.c2))
+   ->  Append
+         ->  Foreign Scan
+               Output: fprt1.c1, (sum(fprt1.c2)), (min(fprt1.c2)), (count(*))
+               Relations: Aggregate on (mysql_fdw_regress.ftprt1_p1 fprt1)
+               Remote query: SELECT `c1`, sum(`c2`), min(`c2`), count(*) FROM `mysql_fdw_regress`.`test1` GROUP BY 1 HAVING ((avg(`c2`) < 22))
+         ->  Foreign Scan
+               Output: fprt1_1.c1, (sum(fprt1_1.c2)), (min(fprt1_1.c2)), (count(*))
+               Relations: Aggregate on (mysql_fdw_regress.ftprt1_p2 fprt1)
+               Remote query: SELECT `c1`, sum(`c2`), min(`c2`), count(*) FROM `mysql_fdw_regress`.`test2` GROUP BY 1 HAVING ((avg(`c2`) < 22))
+(12 rows)
+
+SELECT c1, sum(c2), min(c2), count(*) FROM fprt1 GROUP BY c1 HAVING avg(c2) < 22 ORDER BY 1;
+ c1 | sum | min | count 
+----+-----+-----+-------
+  1 |   1 |   1 |     1
+  2 |   2 |   2 |     1
+  3 |   3 |   3 |     1
+  4 |   4 |   4 |     1
+(4 rows)
+
+-- Check with whole-row reference
+-- Should have all the columns in the target list for the given relation
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c1, count(t1) FROM fprt1 t1 GROUP BY c1 HAVING avg(c2) < 22 ORDER BY 1;
+                                                         QUERY PLAN                                                         
+----------------------------------------------------------------------------------------------------------------------------
+ GroupAggregate
+   Output: t1.c1, count(((t1.*)::fprt1))
+   Group Key: t1.c1
+   Filter: (avg(t1.c2) < '22'::numeric)
+   ->  Append
+         ->  Foreign Scan on public.ftprt1_p1 t1_1
+               Output: t1_1.c1, t1_1.*, t1_1.c2
+               Remote query: SELECT `c1`, `c2`, `c3`, `c4` FROM `mysql_fdw_regress`.`test1` ORDER BY `c1` IS NULL, `c1` ASC
+         ->  Foreign Scan on public.ftprt1_p2 t1_2
+               Output: t1_2.c1, t1_2.*, t1_2.c2
+               Remote query: SELECT `c1`, `c2`, `c3`, `c4` FROM `mysql_fdw_regress`.`test2` ORDER BY `c1` IS NULL, `c1` ASC
+(11 rows)
+
+SELECT c1, count(t1) FROM fprt1 t1 GROUP BY c1 HAVING avg(c2) < 22 ORDER BY 1;
+ c1 | count 
+----+-------
+  1 |     1
+  2 |     1
+  3 |     1
+  4 |     1
+(4 rows)
+
+-- When GROUP BY clause does not match with PARTITION KEY.
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT c2, avg(c1), max(c1), count(*) FROM fprt1 GROUP BY c2 HAVING sum(c1) < 700 ORDER BY 1;
+                                                          QUERY PLAN                                                           
+-------------------------------------------------------------------------------------------------------------------------------
+ Finalize GroupAggregate
+   Output: fprt1.c2, avg(fprt1.c1), max(fprt1.c1), count(*)
+   Group Key: fprt1.c2
+   Filter: (sum(fprt1.c1) < 700)
+   ->  Merge Append
+         Sort Key: fprt1.c2
+         ->  Partial GroupAggregate
+               Output: fprt1.c2, PARTIAL avg(fprt1.c1), PARTIAL max(fprt1.c1), PARTIAL count(*), PARTIAL sum(fprt1.c1)
+               Group Key: fprt1.c2
+               ->  Foreign Scan on public.ftprt1_p1 fprt1
+                     Output: fprt1.c2, fprt1.c1
+                     Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` ORDER BY `c2` IS NULL, `c2` ASC
+         ->  Partial GroupAggregate
+               Output: fprt1_1.c2, PARTIAL avg(fprt1_1.c1), PARTIAL max(fprt1_1.c1), PARTIAL count(*), PARTIAL sum(fprt1_1.c1)
+               Group Key: fprt1_1.c2
+               ->  Foreign Scan on public.ftprt1_p2 fprt1_1
+                     Output: fprt1_1.c2, fprt1_1.c1
+                     Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test2` ORDER BY `c2` IS NULL, `c2` ASC
+(18 rows)
+
+SELECT c2, avg(c1), max(c1), count(*) FROM fprt1 GROUP BY c2 HAVING sum(c1) < 700 ORDER BY 1;
+ c2 |          avg           | max | count 
+----+------------------------+-----+-------
+  1 | 1.00000000000000000000 |   1 |     1
+  2 |     2.0000000000000000 |   2 |     1
+  3 |     3.0000000000000000 |   3 |     1
+  4 |     4.0000000000000000 |   4 |     1
+(4 rows)
+
+SET enable_partitionwise_aggregate TO off;
+-- Cleanup
+DROP aggregate least_agg(variadic items anyarray);
+DROP FUNCTION least_accum(anyelement, variadic anyarray);
+DELETE FROM fdw132_t1;
+DELETE FROM fdw132_t2;
+DROP FOREIGN TABLE fdw132_t1;
+DROP FOREIGN TABLE fdw132_t2;
+DROP FOREIGN TABLE ftprt1_p1;
+DROP FOREIGN TABLE ftprt1_p2;
+DROP TABLE IF EXISTS fprt1;
+DROP TYPE user_enum;
+DROP USER MAPPING FOR public SERVER mysql_svr;
+DROP SERVER mysql_svr;
+DROP EXTENSION mysql_fdw;
diff -pruN 2.9.2-2/expected/aggregate_pushdown_2.out 2.9.3-1/expected/aggregate_pushdown_2.out
--- 2.9.2-2/expected/aggregate_pushdown_2.out	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/expected/aggregate_pushdown_2.out	2025-09-30 05:28:24.000000000 +0000
@@ -254,6 +254,7 @@ SELECT sum(c1) FROM fdw132_t1 GROUP BY c
                                                    QUERY PLAN                                                   
 ----------------------------------------------------------------------------------------------------------------
  Sort
+   Disabled: true
    Output: (sum(c1)), c2
    Sort Key: (sum(fdw132_t1.c1))
    ->  GroupAggregate
@@ -263,7 +264,7 @@ SELECT sum(c1) FROM fdw132_t1 GROUP BY c
          ->  Foreign Scan on public.fdw132_t1
                Output: c1, c2, c3, c4
                Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` ORDER BY `c2` IS NULL, `c2` ASC
-(10 rows)
+(11 rows)
 
 SELECT sum(c1) FROM fdw132_t1 GROUP BY c2 HAVING avg(c1 * (random() <= 1)::int) > 1 ORDER BY 1;
  sum 
@@ -278,6 +279,7 @@ SELECT sum(c1 ORDER BY c1) FROM fdw132_t
                                                                          QUERY PLAN                                                                          
 -------------------------------------------------------------------------------------------------------------------------------------------------------------
  Sort
+   Disabled: true
    Output: (sum(c1 ORDER BY c1)), c2
    Sort Key: (sum(fdw132_t1.c1 ORDER BY fdw132_t1.c1))
    ->  GroupAggregate
@@ -286,7 +288,7 @@ SELECT sum(c1 ORDER BY c1) FROM fdw132_t
          ->  Foreign Scan on public.fdw132_t1
                Output: c1, c2, c3, c4
                Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c1` < 100)) ORDER BY `c2` IS NULL, `c2` ASC, `c1` IS NULL, `c1` ASC
-(9 rows)
+(10 rows)
 
 SELECT sum(c1 ORDER BY c1) FROM fdw132_t1 WHERE c1 < 100 GROUP BY c2 ORDER BY 1;
  sum 
@@ -370,6 +372,7 @@ SELECT array_agg(DISTINCT (t1.c1)%5 ORDE
                                                                                                                                                                                 QUERY PLAN                                                                                                                                                                                
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Sort
+   Disabled: true
    Output: (array_agg(DISTINCT (t1.c1 % 5) ORDER BY (t1.c1 % 5))), ((t2.c1 % 3))
    Sort Key: (array_agg(DISTINCT (t1.c1 % 5) ORDER BY (t1.c1 % 5)))
    ->  GroupAggregate
@@ -379,7 +382,7 @@ SELECT array_agg(DISTINCT (t1.c1)%5 ORDE
                Output: (t2.c1 % 3), t1.c1
                Relations: (mysql_fdw_regress.fdw132_t1 t1) INNER JOIN (mysql_fdw_regress.fdw132_t2 t2)
                Remote query: SELECT r2.`c1`, r1.`c1` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON ((((r1.`c1` < 20) OR ((r1.`c1` IS NULL) AND (r2.`c1` < 5)))) AND ((r1.`c1` = r2.`c1`)))) WHERE (((r1.`c1` < 20) OR (r1.`c1` IS NULL))) ORDER BY (r2.`c1` % 3) IS NULL, (r2.`c1` % 3) ASC, (r1.`c1` % 5) IS NULL, (r1.`c1` % 5) ASC
-(10 rows)
+(11 rows)
 
 SELECT array_agg(DISTINCT (t1.c1)%5 ORDER BY (t1.c1)%5) FROM fdw132_t1 t1 join fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c1 < 20 or (t1.c1 is null and t2.c1 < 5) GROUP BY (t2.c1)%3 ORDER BY 1;
  array_agg 
@@ -391,13 +394,13 @@ SELECT array_agg(DISTINCT (t1.c1)%5 ORDE
 -- DISTINCT, ORDER BY and FILTER within aggregate, not pushed down.
 EXPLAIN (VERBOSE, COSTS OFF)
 SELECT sum(c1%3), sum(DISTINCT c1%3 ORDER BY c1%3) filter (WHERE c1%3 < 2), c2 FROM fdw132_t1 WHERE c2 = 100 GROUP BY c2;
-                                                                QUERY PLAN                                                                 
--------------------------------------------------------------------------------------------------------------------------------------------
+                                             QUERY PLAN                                              
+-----------------------------------------------------------------------------------------------------
  GroupAggregate
    Output: sum((c1 % 3)), sum(DISTINCT (c1 % 3) ORDER BY (c1 % 3)) FILTER (WHERE ((c1 % 3) < 2)), c2
    ->  Foreign Scan on public.fdw132_t1
          Output: c1, c2, c3, c4
-         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` = 100)) ORDER BY (`c1` % 3) IS NULL, (`c1` % 3) ASC
+         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` = 100))
 (5 rows)
 
 SELECT sum(c1%3), sum(DISTINCT c1%3 ORDER BY c1%3) filter (WHERE c1%3 < 2), c2 FROM fdw132_t1 WHERE c2 = 100 GROUP BY c2;
@@ -412,6 +415,7 @@ SELECT sum(c1) filter (WHERE c1 < 100 an
                                                    QUERY PLAN                                                   
 ----------------------------------------------------------------------------------------------------------------
  Sort
+   Disabled: true
    Output: (sum(c1) FILTER (WHERE ((c1 < 100) AND (c2 > 5)))), c2
    Sort Key: (sum(fdw132_t1.c1) FILTER (WHERE ((fdw132_t1.c1 < 100) AND (fdw132_t1.c2 > 5))))
    ->  GroupAggregate
@@ -420,7 +424,7 @@ SELECT sum(c1) filter (WHERE c1 < 100 an
          ->  Foreign Scan on public.fdw132_t1
                Output: c1, c2, c3, c4
                Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` ORDER BY `c2` IS NULL, `c2` ASC
-(9 rows)
+(10 rows)
 
 SELECT sum(c1) filter (WHERE c1 < 100 and c2 > 5) FROM fdw132_t1 GROUP BY c2 ORDER BY 1 nulls last;
  sum 
@@ -436,6 +440,7 @@ SELECT DISTINCT (SELECT count(*) filter
  Unique
    Output: ((SubPlan 1))
    ->  Sort
+         Disabled: true
          Output: ((SubPlan 1))
          Sort Key: ((SubPlan 1))
          ->  Aggregate
@@ -447,7 +452,7 @@ SELECT DISTINCT (SELECT count(*) filter
                  ->  Foreign Scan on public.fdw132_t1 t1
                        Output: count(*) FILTER (WHERE ((t2.c2 = 200) AND (t2.c1 < 10)))
                        Remote query: SELECT NULL FROM `mysql_fdw_regress`.`test1` WHERE ((`c1` = 2))
-(14 rows)
+(15 rows)
 
 SELECT DISTINCT (SELECT count(*) filter (WHERE t2.c2 = 200 and t2.c1 < 10) FROM fdw132_t1 t1 WHERE t1.c1 = 2) FROM fdw132_t2 t2 ORDER BY 1;
  count 
@@ -463,6 +468,7 @@ SELECT DISTINCT (SELECT count(t1.c1) fil
  Unique
    Output: ((SubPlan 1))
    ->  Sort
+         Disabled: true
          Output: ((SubPlan 1))
          Sort Key: ((SubPlan 1))
          ->  Foreign Scan on public.fdw132_t2 t2
@@ -474,7 +480,7 @@ SELECT DISTINCT (SELECT count(t1.c1) fil
                        ->  Foreign Scan on public.fdw132_t1 t1
                              Output: t1.c1, t1.c2, t1.c3, t1.c4
                              Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` WHERE ((`c1` = 2))
-(14 rows)
+(15 rows)
 
 SELECT DISTINCT (SELECT count(t1.c1) filter (WHERE t2.c2 = 200 and t2.c1 < 10) FROM fdw132_t1 t1 WHERE t1.c1 = 2) FROM fdw132_t2 t2 ORDER BY 1;
  count 
@@ -528,6 +534,7 @@ SELECT count(*), x.b FROM fdw132_t1, (SE
                                                     QUERY PLAN                                                    
 ------------------------------------------------------------------------------------------------------------------
  Sort
+   Disabled: true
    Output: (count(*)), x.b
    Sort Key: (count(*)), x.b
    ->  HashAggregate
@@ -548,7 +555,7 @@ SELECT count(*), x.b FROM fdw132_t1, (SE
                                  Output: fdw132_t2.c1, (sum(fdw132_t2.c1))
                                  Relations: Aggregate on (mysql_fdw_regress.fdw132_t2)
                                  Remote query: SELECT `c1`, sum(`c1`) FROM `mysql_fdw_regress`.`test2` GROUP BY 1
-(21 rows)
+(22 rows)
 
 SELECT count(*), x.b FROM fdw132_t1, (SELECT c1 a, sum(c1) b FROM fdw132_t2 GROUP BY c1) x WHERE fdw132_t1.c1 = x.a GROUP BY x.b ORDER BY 1, 2;
  count | b 
@@ -580,13 +587,14 @@ SELECT sum(c2) * (random() <= 1)::int AS
                                       QUERY PLAN                                      
 --------------------------------------------------------------------------------------
  Sort
+   Disabled: true
    Output: (((sum(c2)) * ((random() <= '1'::double precision))::integer))
    Sort Key: (((sum(fdw132_t1.c2)) * ((random() <= '1'::double precision))::integer))
    ->  Foreign Scan
          Output: ((sum(c2)) * ((random() <= '1'::double precision))::integer)
          Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
          Remote query: SELECT sum(`c2`) FROM `mysql_fdw_regress`.`test1`
-(7 rows)
+(8 rows)
 
 SELECT sum(c2) * (random() <= 1)::int AS sum FROM fdw132_t1 ORDER BY 1;
  sum 
@@ -607,11 +615,13 @@ SELECT c2, sum FROM fdw132_t1 t1, latera
    ->  Subquery Scan on qry
          Output: qry.sum, t2.c1
          Filter: ((t1.c2 * 2) = qry.sum)
-         ->  Foreign Scan
-               Output: (sum((t2.c1 + t1.c1))), t2.c1
-               Relations: Aggregate on (mysql_fdw_regress.fdw132_t2 t2)
-               Remote query: SELECT sum((`c1` + ?)), `c1` FROM `mysql_fdw_regress`.`test2` GROUP BY 2
-(12 rows)
+         ->  GroupAggregate
+               Output: sum((t2.c1 + t1.c1)), t2.c1
+               Group Key: t2.c1
+               ->  Foreign Scan on public.fdw132_t2 t2
+                     Output: t2.c1, t2.c2, t2.c3, t2.c4
+                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test2` ORDER BY `c1` IS NULL, `c1` ASC
+(14 rows)
 
 -- Check with placeHolderVars
 EXPLAIN (VERBOSE, COSTS OFF)
@@ -619,6 +629,7 @@ SELECT q.b, count(fdw132_t1.c1), sum(q.a
                                                                                                        QUERY PLAN                                                                                                       
 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Sort
+   Disabled: true
    Output: q.b, (count(fdw132_t1.c1)), (sum(q.a))
    Sort Key: q.b, (count(fdw132_t1.c1))
    ->  HashAggregate
@@ -639,7 +650,7 @@ SELECT q.b, count(fdw132_t1.c1), sum(q.a
                                  Output: (min(13)), (avg(fdw132_t1_1.c1)), NULL::bigint
                                  Relations: Aggregate on ((mysql_fdw_regress.fdw132_t1) INNER JOIN (mysql_fdw_regress.fdw132_t2))
                                  Remote query: SELECT min(13), avg(r1.`c1`), NULL FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (TRUE)) WHERE ((r2.`c1` = 12)) AND ((r1.`c1` = 12))
-(21 rows)
+(22 rows)
 
 SELECT q.b, count(fdw132_t1.c1), sum(q.a) FROM fdw132_t1 left join (SELECT min(13), avg(fdw132_t1.c1), sum(fdw132_t2.c1) FROM fdw132_t1 right join fdw132_t2 ON (fdw132_t1.c1 = fdw132_t2.c1) WHERE fdw132_t1.c1 = 12) q(a, b, c) ON (fdw132_t1.c1 = q.b) WHERE fdw132_t1.c1 between 10 and 15 GROUP BY q.b ORDER BY 1 nulls last, 2;
  b | count | sum 
@@ -651,16 +662,20 @@ SELECT q.b, count(fdw132_t1.c1), sum(q.a
 -- Grouping sets
 EXPLAIN (VERBOSE, COSTS OFF)
 SELECT c2, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY rollup(c2) ORDER BY 1 nulls last;
-                                                         QUERY PLAN                                                          
------------------------------------------------------------------------------------------------------------------------------
- GroupAggregate
-   Output: c2, sum(c1)
-   Group Key: fdw132_t1.c2
-   Group Key: ()
-   ->  Foreign Scan on public.fdw132_t1
-         Output: c1, c2, c3, c4
-         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 3)) ORDER BY `c2` IS NULL, `c2` ASC
-(7 rows)
+                                                            QUERY PLAN                                                             
+-----------------------------------------------------------------------------------------------------------------------------------
+ Sort
+   Disabled: true
+   Output: c2, (sum(c1))
+   Sort Key: fdw132_t1.c2
+   ->  GroupAggregate
+         Output: c2, sum(c1)
+         Group Key: fdw132_t1.c2
+         Group Key: ()
+         ->  Foreign Scan on public.fdw132_t1
+               Output: c1, c2, c3, c4
+               Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 3)) ORDER BY `c2` IS NULL, `c2` ASC
+(11 rows)
 
 SELECT c2, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY rollup(c2) ORDER BY 1 nulls last;
  c2  | sum 
@@ -671,16 +686,20 @@ SELECT c2, sum(c1) FROM fdw132_t1 WHERE
 
 EXPLAIN (VERBOSE, COSTS OFF)
 SELECT c2, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY cube(c2) ORDER BY 1 nulls last;
-                                                         QUERY PLAN                                                          
------------------------------------------------------------------------------------------------------------------------------
- GroupAggregate
-   Output: c2, sum(c1)
-   Group Key: fdw132_t1.c2
-   Group Key: ()
-   ->  Foreign Scan on public.fdw132_t1
-         Output: c1, c2, c3, c4
-         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 3)) ORDER BY `c2` IS NULL, `c2` ASC
-(7 rows)
+                                                            QUERY PLAN                                                             
+-----------------------------------------------------------------------------------------------------------------------------------
+ Sort
+   Disabled: true
+   Output: c2, (sum(c1))
+   Sort Key: fdw132_t1.c2
+   ->  GroupAggregate
+         Output: c2, sum(c1)
+         Group Key: fdw132_t1.c2
+         Group Key: ()
+         ->  Foreign Scan on public.fdw132_t1
+               Output: c1, c2, c3, c4
+               Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 3)) ORDER BY `c2` IS NULL, `c2` ASC
+(11 rows)
 
 SELECT c2, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY cube(c2) ORDER BY 1 nulls last;
  c2  | sum 
@@ -694,6 +713,7 @@ SELECT c2, c3, sum(c1) FROM fdw132_t1 WH
                                                                QUERY PLAN                                                                
 -----------------------------------------------------------------------------------------------------------------------------------------
  Sort
+   Disabled: true
    Output: c2, c3, (sum(c1))
    Sort Key: fdw132_t1.c2, fdw132_t1.c3 COLLATE "C"
    ->  MixedAggregate
@@ -703,7 +723,7 @@ SELECT c2, c3, sum(c1) FROM fdw132_t1 WH
          ->  Foreign Scan on public.fdw132_t1
                Output: c1, c2, c3, c4
                Remote query: SELECT `c1`, `c2`, `c3` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 3)) ORDER BY `c2` IS NULL, `c2` ASC
-(10 rows)
+(11 rows)
 
 SELECT c2, c3, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY grouping sets(c2, c3) ORDER BY 1 nulls last, 2 nulls last;
  c2  |  c3   | sum 
@@ -740,13 +760,14 @@ SELECT DISTINCT sum(c1) s FROM fdw132_t1
  Unique
    Output: (sum(c1)), c2
    ->  Sort
+         Disabled: true
          Output: (sum(c1)), c2
          Sort Key: (sum(fdw132_t1.c1))
          ->  Foreign Scan
                Output: (sum(c1)), c2
                Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
                Remote query: SELECT sum(`c1`), `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 6)) GROUP BY 2
-(9 rows)
+(10 rows)
 
 SELECT DISTINCT sum(c1) s FROM fdw132_t1 WHERE c2 > 6 GROUP BY c2 ORDER BY 1;
  s  
@@ -760,18 +781,21 @@ SELECT c2, sum(c2), count(c2) over (part
                                                               QUERY PLAN                                                              
 --------------------------------------------------------------------------------------------------------------------------------------
  Sort
-   Output: c2, (sum(c2)), (count(c2) OVER (?)), ((c2 % 2))
+   Disabled: true
+   Output: c2, (sum(c2)), (count(c2) OVER w1), ((c2 % 2))
    Sort Key: fdw132_t1.c2
    ->  WindowAgg
-         Output: c2, (sum(c2)), count(c2) OVER (?), ((c2 % 2))
+         Output: c2, (sum(c2)), count(c2) OVER w1, ((c2 % 2))
+         Window: w1 AS (PARTITION BY ((fdw132_t1.c2 % 2)))
          ->  Sort
+               Disabled: true
                Output: c2, ((c2 % 2)), (sum(c2))
                Sort Key: ((fdw132_t1.c2 % 2))
                ->  Foreign Scan
                      Output: c2, ((c2 % 2)), (sum(c2))
                      Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
                      Remote query: SELECT `c2`, (`c2` % 2), sum(`c2`) FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 10)) GROUP BY 1
-(12 rows)
+(15 rows)
 
 SELECT c2, sum(c2), count(c2) over (partition by c2%2) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
  c2  | sum | count 
@@ -784,18 +808,21 @@ SELECT c2, array_agg(c2) over (partition
                                                         QUERY PLAN                                                         
 ---------------------------------------------------------------------------------------------------------------------------
  Sort
-   Output: c2, (array_agg(c2) OVER (?)), ((c2 % 2))
+   Disabled: true
+   Output: c2, (array_agg(c2) OVER w1), ((c2 % 2))
    Sort Key: fdw132_t1.c2
    ->  WindowAgg
-         Output: c2, array_agg(c2) OVER (?), ((c2 % 2))
+         Output: c2, array_agg(c2) OVER w1, ((c2 % 2))
+         Window: w1 AS (PARTITION BY ((fdw132_t1.c2 % 2)) ORDER BY fdw132_t1.c2)
          ->  Sort
+               Disabled: true
                Output: c2, ((c2 % 2))
                Sort Key: ((fdw132_t1.c2 % 2)), fdw132_t1.c2 DESC
                ->  Foreign Scan
                      Output: c2, ((c2 % 2))
                      Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
                      Remote query: SELECT `c2`, (`c2` % 2) FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 10)) GROUP BY 1
-(12 rows)
+(15 rows)
 
 SELECT c2, array_agg(c2) over (partition by c2%2 ORDER BY c2 desc) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
  c2  | array_agg 
@@ -805,21 +832,24 @@ SELECT c2, array_agg(c2) over (partition
 
 EXPLAIN (VERBOSE, COSTS OFF)
 SELECT c2, array_agg(c2) over (partition by c2%2 ORDER BY c2 range between current row and unbounded following) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
-                                                        QUERY PLAN                                                         
----------------------------------------------------------------------------------------------------------------------------
+                                                            QUERY PLAN                                                             
+-----------------------------------------------------------------------------------------------------------------------------------
  Sort
-   Output: c2, (array_agg(c2) OVER (?)), ((c2 % 2))
+   Disabled: true
+   Output: c2, (array_agg(c2) OVER w1), ((c2 % 2))
    Sort Key: fdw132_t1.c2
    ->  WindowAgg
-         Output: c2, array_agg(c2) OVER (?), ((c2 % 2))
+         Output: c2, array_agg(c2) OVER w1, ((c2 % 2))
+         Window: w1 AS (PARTITION BY ((fdw132_t1.c2 % 2)) ORDER BY fdw132_t1.c2 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
          ->  Sort
+               Disabled: true
                Output: c2, ((c2 % 2))
                Sort Key: ((fdw132_t1.c2 % 2)), fdw132_t1.c2
                ->  Foreign Scan
                      Output: c2, ((c2 % 2))
                      Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
                      Remote query: SELECT `c2`, (`c2` % 2) FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 10)) GROUP BY 1
-(12 rows)
+(15 rows)
 
 SELECT c2, array_agg(c2) over (partition by c2%2 ORDER BY c2 range between current row and unbounded following) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
  c2  | array_agg 
@@ -946,6 +976,7 @@ SELECT c1, sum(c1) FROM fprt1 GROUP BY c
                                            QUERY PLAN                                           
 ------------------------------------------------------------------------------------------------
  Sort
+   Disabled: true
    Output: fprt1.c1, (sum(fprt1.c1))
    Sort Key: (sum(fprt1.c1))
    ->  Append
@@ -957,7 +988,7 @@ SELECT c1, sum(c1) FROM fprt1 GROUP BY c
                Output: fprt1_1.c1, (sum(fprt1_1.c1))
                Relations: Aggregate on (mysql_fdw_regress.ftprt1_p2 fprt1)
                Remote query: SELECT `c1`, sum(`c1`) FROM `mysql_fdw_regress`.`test2` GROUP BY 1
-(12 rows)
+(13 rows)
 
 SELECT c1, sum(c1) FROM fprt1 GROUP BY c1 ORDER BY 2;
  c1 | sum 
@@ -973,6 +1004,7 @@ SELECT c1, sum(c2), min(c2), count(*) FR
                                                                   QUERY PLAN                                                                   
 -----------------------------------------------------------------------------------------------------------------------------------------------
  Sort
+   Disabled: true
    Output: fprt1.c1, (sum(fprt1.c2)), (min(fprt1.c2)), (count(*))
    Sort Key: (sum(fprt1.c2))
    ->  Append
@@ -984,7 +1016,7 @@ SELECT c1, sum(c2), min(c2), count(*) FR
                Output: fprt1_1.c1, (sum(fprt1_1.c2)), (min(fprt1_1.c2)), (count(*))
                Relations: Aggregate on (mysql_fdw_regress.ftprt1_p2 fprt1)
                Remote query: SELECT `c1`, sum(`c2`), min(`c2`), count(*) FROM `mysql_fdw_regress`.`test2` GROUP BY 1 HAVING ((avg(`c2`) < 22))
-(12 rows)
+(13 rows)
 
 SELECT c1, sum(c2), min(c2), count(*) FROM fprt1 GROUP BY c1 HAVING avg(c2) < 22 ORDER BY 1;
  c1 | sum | min | count 
@@ -999,20 +1031,28 @@ SELECT c1, sum(c2), min(c2), count(*) FR
 -- Should have all the columns in the target list for the given relation
 EXPLAIN (VERBOSE, COSTS OFF)
 SELECT c1, count(t1) FROM fprt1 t1 GROUP BY c1 HAVING avg(c2) < 22 ORDER BY 1;
-                                                         QUERY PLAN                                                         
-----------------------------------------------------------------------------------------------------------------------------
- GroupAggregate
-   Output: t1.c1, count(((t1.*)::fprt1))
-   Group Key: t1.c1
-   Filter: (avg(t1.c2) < '22'::numeric)
+                                            QUERY PLAN                                            
+--------------------------------------------------------------------------------------------------
+ Sort
+   Disabled: true
+   Output: t1.c1, (count(((t1.*)::fprt1)))
+   Sort Key: t1.c1
    ->  Append
-         ->  Foreign Scan on public.ftprt1_p1 t1_1
-               Output: t1_1.c1, t1_1.*, t1_1.c2
-               Remote query: SELECT `c1`, `c2`, `c3`, `c4` FROM `mysql_fdw_regress`.`test1` ORDER BY `c1` IS NULL, `c1` ASC
-         ->  Foreign Scan on public.ftprt1_p2 t1_2
-               Output: t1_2.c1, t1_2.*, t1_2.c2
-               Remote query: SELECT `c1`, `c2`, `c3`, `c4` FROM `mysql_fdw_regress`.`test2` ORDER BY `c1` IS NULL, `c1` ASC
-(11 rows)
+         ->  HashAggregate
+               Output: t1.c1, count(((t1.*)::fprt1))
+               Group Key: t1.c1
+               Filter: (avg(t1.c2) < '22'::numeric)
+               ->  Foreign Scan on public.ftprt1_p1 t1
+                     Output: t1.c1, t1.*, t1.c2
+                     Remote query: SELECT `c1`, `c2`, `c3`, `c4` FROM `mysql_fdw_regress`.`test1`
+         ->  HashAggregate
+               Output: t1_1.c1, count(((t1_1.*)::fprt1))
+               Group Key: t1_1.c1
+               Filter: (avg(t1_1.c2) < '22'::numeric)
+               ->  Foreign Scan on public.ftprt1_p2 t1_1
+                     Output: t1_1.c1, t1_1.*, t1_1.c2
+                     Remote query: SELECT `c1`, `c2`, `c3`, `c4` FROM `mysql_fdw_regress`.`test2`
+(19 rows)
 
 SELECT c1, count(t1) FROM fprt1 t1 GROUP BY c1 HAVING avg(c2) < 22 ORDER BY 1;
  c1 | count 
@@ -1026,27 +1066,24 @@ SELECT c1, count(t1) FROM fprt1 t1 GROUP
 -- When GROUP BY clause does not match with PARTITION KEY.
 EXPLAIN (VERBOSE, COSTS OFF)
 SELECT c2, avg(c1), max(c1), count(*) FROM fprt1 GROUP BY c2 HAVING sum(c1) < 700 ORDER BY 1;
-                                                          QUERY PLAN                                                           
--------------------------------------------------------------------------------------------------------------------------------
- Finalize GroupAggregate
-   Output: fprt1.c2, avg(fprt1.c1), max(fprt1.c1), count(*)
-   Group Key: fprt1.c2
-   Filter: (sum(fprt1.c1) < 700)
-   ->  Merge Append
-         Sort Key: fprt1.c2
-         ->  Partial GroupAggregate
-               Output: fprt1.c2, PARTIAL avg(fprt1.c1), PARTIAL max(fprt1.c1), PARTIAL count(*), PARTIAL sum(fprt1.c1)
-               Group Key: fprt1.c2
-               ->  Foreign Scan on public.ftprt1_p1 fprt1
-                     Output: fprt1.c2, fprt1.c1
-                     Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` ORDER BY `c2` IS NULL, `c2` ASC
-         ->  Partial GroupAggregate
-               Output: fprt1_1.c2, PARTIAL avg(fprt1_1.c1), PARTIAL max(fprt1_1.c1), PARTIAL count(*), PARTIAL sum(fprt1_1.c1)
-               Group Key: fprt1_1.c2
-               ->  Foreign Scan on public.ftprt1_p2 fprt1_1
+                                      QUERY PLAN                                      
+--------------------------------------------------------------------------------------
+ Sort
+   Disabled: true
+   Output: fprt1.c2, (avg(fprt1.c1)), (max(fprt1.c1)), (count(*))
+   Sort Key: fprt1.c2
+   ->  HashAggregate
+         Output: fprt1.c2, avg(fprt1.c1), max(fprt1.c1), count(*)
+         Group Key: fprt1.c2
+         Filter: (sum(fprt1.c1) < 700)
+         ->  Append
+               ->  Foreign Scan on public.ftprt1_p1 fprt1_1
                      Output: fprt1_1.c2, fprt1_1.c1
-                     Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test2` ORDER BY `c2` IS NULL, `c2` ASC
-(18 rows)
+                     Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1`
+               ->  Foreign Scan on public.ftprt1_p2 fprt1_2
+                     Output: fprt1_2.c2, fprt1_2.c1
+                     Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test2`
+(15 rows)
 
 SELECT c2, avg(c1), max(c1), count(*) FROM fprt1 GROUP BY c2 HAVING sum(c1) < 700 ORDER BY 1;
  c2 |          avg           | max | count 
diff -pruN 2.9.2-2/expected/aggregate_pushdown_4.out 2.9.3-1/expected/aggregate_pushdown_4.out
--- 2.9.2-2/expected/aggregate_pushdown_4.out	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/expected/aggregate_pushdown_4.out	1970-01-01 00:00:00.000000000 +0000
@@ -1,1075 +0,0 @@
-\set MYSQL_HOST			`echo \'"$MYSQL_HOST"\'`
-\set MYSQL_PORT			`echo \'"$MYSQL_PORT"\'`
-\set MYSQL_USER_NAME	`echo \'"$MYSQL_USER_NAME"\'`
-\set MYSQL_PASS			`echo \'"$MYSQL_PWD"\'`
--- Before running this file User must create database mysql_fdw_regress on
--- mysql with all permission for MYSQL_USER_NAME user with MYSQL_PWD password
--- and ran mysql_init.sh file to create tables.
-\c contrib_regression
-CREATE EXTENSION IF NOT EXISTS mysql_fdw;
--- FDW-132: Support for aggregate pushdown.
-CREATE SERVER mysql_svr FOREIGN DATA WRAPPER mysql_fdw
-  OPTIONS (host :MYSQL_HOST, port :MYSQL_PORT);
-CREATE USER MAPPING FOR public SERVER mysql_svr
-  OPTIONS (username :MYSQL_USER_NAME, password :MYSQL_PASS);
-CREATE TYPE user_enum AS ENUM ('foo', 'bar', 'buz');
-CREATE FOREIGN TABLE fdw132_t1(c1 int, c2 int, c3 text COLLATE "C", c4 text COLLATE "C")
-  SERVER mysql_svr OPTIONS(dbname 'mysql_fdw_regress', table_name 'test1');
-CREATE FOREIGN TABLE fdw132_t2(c1 int, c2 int, c3 text COLLATE "C", c4 text COLLATE "C")
-  SERVER mysql_svr OPTIONS(dbname 'mysql_fdw_regress', table_name 'test2');
-INSERT INTO fdw132_t1 values(1, 100, 'AAA1', 'foo');
-INSERT INTO fdw132_t1 values(2, 100, 'AAA2', 'bar');
-INSERT INTO fdw132_t1 values(11, 100, 'AAA11', 'foo');
-INSERT INTO fdw132_t2 values(1, 200, 'BBB1', 'foo');
-INSERT INTO fdw132_t2 values(2, 200, 'BBB2', 'bar');
-INSERT INTO fdw132_t2 values(12, 200, 'BBB12', 'foo');
-ALTER FOREIGN TABLE fdw132_t1 ALTER COLUMN c4 type user_enum;
-ALTER FOREIGN TABLE fdw132_t2 ALTER COLUMN c4 type user_enum;
--- Simple aggregates
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(c1), avg(c1), min(c2), max(c1), sum(c1) * (random() <= 1)::int AS sum2 FROM fdw132_t1 WHERE c2 > 5 GROUP BY c2 ORDER BY 1, 2;
-                                                                                                     QUERY PLAN                                                                                                     
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Foreign Scan
-   Output: (sum(c1)), (avg(c1)), (min(c2)), (max(c1)), ((sum(c1)) * ((random() <= '1'::double precision))::integer), c2
-   Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-   Remote query: SELECT sum(`c1`), avg(`c1`), min(`c2`), max(`c1`), `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 5)) GROUP BY 5 ORDER BY sum(`c1`) IS NULL, sum(`c1`) ASC, avg(`c1`) IS NULL, avg(`c1`) ASC
-(4 rows)
-
-SELECT sum(c1), avg(c1), min(c2), max(c1), sum(c1) * (random() <= 1)::int AS sum2 FROM fdw132_t1 WHERE c2 > 5 GROUP BY c2 ORDER BY 1, 2;
- sum |  avg   | min | max | sum2 
------+--------+-----+-----+------
-  14 | 4.6667 | 100 |  11 |   14
-(1 row)
-
--- Aggregate is not pushed down as aggregation contains random()
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(c1 * (random() <= 1)::int) AS sum, avg(c1) FROM fdw132_t1;
-                                  QUERY PLAN                                   
--------------------------------------------------------------------------------
- Aggregate
-   Output: sum((c1 * ((random() <= '1'::double precision))::integer)), avg(c1)
-   ->  Foreign Scan on public.fdw132_t1
-         Output: c1, c2, c3, c4
-         Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1`
-(5 rows)
-
-SELECT sum(c1 * (random() <= 1)::int) AS sum, avg(c1) FROM fdw132_t1;
- sum |        avg         
------+--------------------
-  14 | 4.6666666666666667
-(1 row)
-
--- Aggregate over join query
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT count(*), sum(t1.c1), avg(t2.c1) FROM  fdw132_t1 t1 INNER JOIN fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c1 = 2;
-                                                                                           QUERY PLAN                                                                                            
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Foreign Scan
-   Output: (count(*)), (sum(t1.c1)), (avg(t2.c1))
-   Relations: Aggregate on ((mysql_fdw_regress.fdw132_t1 t1) INNER JOIN (mysql_fdw_regress.fdw132_t2 t2))
-   Remote query: SELECT count(*), sum(r1.`c1`), avg(r2.`c1`) FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (TRUE)) WHERE ((r2.`c1` = 2)) AND ((r1.`c1` = 2))
-(4 rows)
-
-SELECT count(*), sum(t1.c1), avg(t2.c1) FROM  fdw132_t1 t1 INNER JOIN fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c1 = 2;
- count | sum |  avg   
--------+-----+--------
-     1 |   2 | 2.0000
-(1 row)
-
--- Not pushed down due to local conditions present in underneath input rel
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(t1.c1), count(t2.c1) FROM fdw132_t1 t1 INNER JOIN fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE ((t1.c1 * t2.c1)/(t1.c1 * t2.c1)) * random() <= 1;
-                                                                        QUERY PLAN                                                                        
-----------------------------------------------------------------------------------------------------------------------------------------------------------
- Aggregate
-   Output: sum(t1.c1), count(t2.c1)
-   ->  Foreign Scan
-         Output: t1.c1, t2.c1
-         Filter: (((((t1.c1 * t2.c1) / (t1.c1 * t2.c1)))::double precision * random()) <= '1'::double precision)
-         Relations: (mysql_fdw_regress.fdw132_t1 t1) INNER JOIN (mysql_fdw_regress.fdw132_t2 t2)
-         Remote query: SELECT r1.`c1`, r2.`c1` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (((r1.`c1` = r2.`c1`))))
-(7 rows)
-
-SELECT sum(t1.c1), count(t2.c1) FROM fdw132_t1 t1 INNER JOIN fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE ((t1.c1 * t2.c1)/(t1.c1 * t2.c1)) * random() <= 1;
- sum | count 
------+-------
-   3 |     2
-(1 row)
-
--- GROUP BY clause HAVING expressions
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c2+2, sum(c2) * (c2+2) FROM fdw132_t1 GROUP BY c2+2 ORDER BY c2+2;
-                                                                     QUERY PLAN                                                                      
------------------------------------------------------------------------------------------------------------------------------------------------------
- Foreign Scan
-   Output: ((c2 + 2)), ((sum(c2) * (c2 + 2)))
-   Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-   Remote query: SELECT (`c2` + 2), (sum(`c2`) * (`c2` + 2)) FROM `mysql_fdw_regress`.`test1` GROUP BY 1 ORDER BY (`c2` + 2) IS NULL, (`c2` + 2) ASC
-(4 rows)
-
-SELECT c2+2, sum(c2) * (c2+2) FROM fdw132_t1 GROUP BY c2+2 ORDER BY c2+2;
- ?column? | ?column? 
-----------+----------
-      102 |    30600
-(1 row)
-
--- Aggregates in subquery are pushed down.
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT count(x.a), sum(x.a) FROM (SELECT c2 a, sum(c1) b FROM fdw132_t1 GROUP BY c2 ORDER BY 1, 2) x;
-                                                                         QUERY PLAN                                                                         
-------------------------------------------------------------------------------------------------------------------------------------------------------------
- Aggregate
-   Output: count(fdw132_t1.c2), sum(fdw132_t1.c2)
-   ->  Foreign Scan
-         Output: fdw132_t1.c2, (sum(fdw132_t1.c1))
-         Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-         Remote query: SELECT `c2`, sum(`c1`) FROM `mysql_fdw_regress`.`test1` GROUP BY 1 ORDER BY `c2` IS NULL, `c2` ASC, sum(`c1`) IS NULL, sum(`c1`) ASC
-(6 rows)
-
-SELECT count(x.a), sum(x.a) FROM (SELECT c2 a, sum(c1) b FROM fdw132_t1 GROUP BY c2 ORDER BY 1, 2) x;
- count | sum 
--------+-----
-     1 | 100
-(1 row)
-
--- Aggregate is still pushed down by taking unshippable expression out
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c2 * (random() <= 1)::int AS sum1, sum(c1) * c2 AS sum2 FROM fdw132_t1 GROUP BY c2 ORDER BY 1, 2;
-                                                     QUERY PLAN                                                      
----------------------------------------------------------------------------------------------------------------------
- Sort
-   Output: ((c2 * ((random() <= '1'::double precision))::integer)), ((sum(c1) * c2)), c2
-   Sort Key: ((fdw132_t1.c2 * ((random() <= '1'::double precision))::integer)), ((sum(fdw132_t1.c1) * fdw132_t1.c2))
-   ->  Foreign Scan
-         Output: (c2 * ((random() <= '1'::double precision))::integer), ((sum(c1) * c2)), c2
-         Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-         Remote query: SELECT (sum(`c1`) * `c2`), `c2` FROM `mysql_fdw_regress`.`test1` GROUP BY 2
-(7 rows)
-
-SELECT c2 * (random() <= 1)::int AS sum1, sum(c1) * c2 AS sum2 FROM fdw132_t1 GROUP BY c2 ORDER BY 1, 2;
- sum1 | sum2 
-------+------
-  100 | 1400
-(1 row)
-
--- Aggregate with unshippable GROUP BY clause are not pushed
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c2 * (random() <= 1)::int AS c2 FROM fdw132_t2 GROUP BY c2 * (random() <= 1)::int ORDER BY 1;
-                                     QUERY PLAN                                     
-------------------------------------------------------------------------------------
- Sort
-   Output: ((c2 * ((random() <= '1'::double precision))::integer))
-   Sort Key: ((fdw132_t2.c2 * ((random() <= '1'::double precision))::integer))
-   ->  HashAggregate
-         Output: ((c2 * ((random() <= '1'::double precision))::integer))
-         Group Key: (fdw132_t2.c2 * ((random() <= '1'::double precision))::integer)
-         ->  Foreign Scan on public.fdw132_t2
-               Output: (c2 * ((random() <= '1'::double precision))::integer)
-               Remote query: SELECT `c2` FROM `mysql_fdw_regress`.`test2`
-(9 rows)
-
-SELECT c2 * (random() <= 1)::int AS c2 FROM fdw132_t2 GROUP BY c2 * (random() <= 1)::int ORDER BY 1;
- c2  
------
- 200
-(1 row)
-
--- GROUP BY clause in various forms, cardinal, alias and constant expression
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT count(c2) w, c2 x, 5 y, 7.0 z FROM fdw132_t1 GROUP BY 2, y, 9.0::int ORDER BY 2;
-                                                              QUERY PLAN                                                               
----------------------------------------------------------------------------------------------------------------------------------------
- Foreign Scan
-   Output: (count(c2)), c2, 5, 7.0, 9
-   Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-   Remote query: SELECT count(`c2`), `c2`, 5, 7.0, 9 FROM `mysql_fdw_regress`.`test1` GROUP BY 2, 3, 5 ORDER BY `c2` IS NULL, `c2` ASC
-(4 rows)
-
-SELECT count(c2) w, c2 x, 5 y, 7.0 z FROM fdw132_t1 GROUP BY 2, y, 9.0::int ORDER BY 2;
- w |  x  | y |  z  
----+-----+---+-----
- 3 | 100 | 5 | 7.0
-(1 row)
-
--- Testing HAVING clause shippability
-SET enable_sort TO ON;
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c2, sum(c1) FROM fdw132_t2 GROUP BY c2 HAVING avg(c1) < 500 and sum(c1) < 49800 ORDER BY c2;
-                                                                               QUERY PLAN                                                                                
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Foreign Scan
-   Output: c2, (sum(c1))
-   Relations: Aggregate on (mysql_fdw_regress.fdw132_t2)
-   Remote query: SELECT `c2`, sum(`c1`) FROM `mysql_fdw_regress`.`test2` GROUP BY 1 HAVING ((avg(`c1`) < 500)) AND ((sum(`c1`) < 49800)) ORDER BY `c2` IS NULL, `c2` ASC
-(4 rows)
-
-SELECT c2, sum(c1) FROM fdw132_t2 GROUP BY c2 HAVING avg(c1) < 500 and sum(c1) < 49800 ORDER BY c2;
- c2  | sum 
------+-----
- 200 |  15
-(1 row)
-
--- Using expressions in HAVING clause
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c3, count(c1) FROM fdw132_t1 GROUP BY c3 HAVING exp(max(c1)) = exp(2) ORDER BY 1, 2;
-                                                                                               QUERY PLAN                                                                                                
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Foreign Scan
-   Output: c3, (count(c1))
-   Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-   Remote query: SELECT `c3`, count(`c1`) FROM `mysql_fdw_regress`.`test1` GROUP BY 1 HAVING ((exp(max(`c1`)) = 7.38905609893065)) ORDER BY `c3` IS NULL, `c3` ASC, count(`c1`) IS NULL, count(`c1`) ASC
-(4 rows)
-
-SELECT c3, count(c1) FROM fdw132_t1 GROUP BY c3 HAVING exp(max(c1)) = exp(2) ORDER BY 1, 2;
-  c3  | count 
-------+-------
- AAA2 |     1
-(1 row)
-
-SET enable_sort TO off;
--- Unshippable HAVING clause will be evaluated locally, and other qual in HAVING clause is pushed down
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT count(*) FROM (SELECT c3, count(c1) FROM fdw132_t1 GROUP BY c3 HAVING (avg(c1) / avg(c1)) * random() <= 1 and avg(c1) < 500) x;
-                                                        QUERY PLAN                                                         
----------------------------------------------------------------------------------------------------------------------------
- Aggregate
-   Output: count(*)
-   ->  Foreign Scan
-         Output: fdw132_t1.c3, NULL::bigint
-         Filter: (((((avg(fdw132_t1.c1)) / (avg(fdw132_t1.c1))))::double precision * random()) <= '1'::double precision)
-         Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-         Remote query: SELECT `c3`, NULL, avg(`c1`) FROM `mysql_fdw_regress`.`test1` GROUP BY 1 HAVING ((avg(`c1`) < 500))
-(7 rows)
-
-SELECT count(*) FROM (SELECT c3, count(c1) FROM fdw132_t1 GROUP BY c3 HAVING (avg(c1) / avg(c1)) * random() <= 1 and avg(c1) < 500) x;
- count 
--------
-     3
-(1 row)
-
--- Aggregate in HAVING clause is not pushable, and thus aggregation is not pushed down
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(c1) FROM fdw132_t1 GROUP BY c2 HAVING avg(c1 * (random() <= 1)::int) > 1 ORDER BY 1;
-                                                   QUERY PLAN                                                   
-----------------------------------------------------------------------------------------------------------------
- Sort
-   Output: (sum(c1)), c2
-   Sort Key: (sum(fdw132_t1.c1))
-   ->  GroupAggregate
-         Output: sum(c1), c2
-         Group Key: fdw132_t1.c2
-         Filter: (avg((fdw132_t1.c1 * ((random() <= '1'::double precision))::integer)) > '1'::numeric)
-         ->  Foreign Scan on public.fdw132_t1
-               Output: c1, c2, c3, c4
-               Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` ORDER BY `c2` IS NULL, `c2` ASC
-(10 rows)
-
-SELECT sum(c1) FROM fdw132_t1 GROUP BY c2 HAVING avg(c1 * (random() <= 1)::int) > 1 ORDER BY 1;
- sum 
------
-  14
-(1 row)
-
--- Testing ORDER BY, DISTINCT, FILTER, Ordered-sets and VARIADIC within aggregates
--- ORDER BY within aggregates (same column used to order) are not pushed
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(c1 ORDER BY c1) FROM fdw132_t1 WHERE c1 < 100 GROUP BY c2 ORDER BY 1;
-                                                             QUERY PLAN                                                              
--------------------------------------------------------------------------------------------------------------------------------------
- Sort
-   Output: (sum(c1 ORDER BY c1)), c2
-   Sort Key: (sum(fdw132_t1.c1 ORDER BY fdw132_t1.c1))
-   ->  GroupAggregate
-         Output: sum(c1 ORDER BY c1), c2
-         Group Key: fdw132_t1.c2
-         ->  Foreign Scan on public.fdw132_t1
-               Output: c1, c2, c3, c4
-               Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c1` < 100)) ORDER BY `c2` IS NULL, `c2` ASC
-(9 rows)
-
-SELECT sum(c1 ORDER BY c1) FROM fdw132_t1 WHERE c1 < 100 GROUP BY c2 ORDER BY 1;
- sum 
------
-  14
-(1 row)
-
--- ORDER BY within aggregate (different column used to order also using DESC)
--- are not pushed.
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(c2 ORDER BY c1 desc) FROM fdw132_t2 WHERE c1 > 1 and c2 > 50;
-                                                  QUERY PLAN                                                   
----------------------------------------------------------------------------------------------------------------
- Aggregate
-   Output: sum(c2 ORDER BY c1 DESC)
-   ->  Foreign Scan on public.fdw132_t2
-         Output: c1, c2, c3, c4
-         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test2` WHERE ((`c1` > 1)) AND ((`c2` > 50))
-(5 rows)
-
-SELECT sum(c2 ORDER BY c1 desc) FROM fdw132_t2 WHERE c1 > 1 and c2 > 50;
- sum 
------
- 400
-(1 row)
-
--- DISTINCT within aggregate
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(DISTINCT (c1)%5) FROM fdw132_t2 WHERE c2 = 200 and c1 < 50;
-                                                       QUERY PLAN                                                        
--------------------------------------------------------------------------------------------------------------------------
- Foreign Scan
-   Output: (sum(DISTINCT (c1 % 5)))
-   Relations: Aggregate on (mysql_fdw_regress.fdw132_t2)
-   Remote query: SELECT sum(DISTINCT (`c1` % 5)) FROM `mysql_fdw_regress`.`test2` WHERE ((`c1` < 50)) AND ((`c2` = 200))
-(4 rows)
-
-SELECT sum(DISTINCT (c1)%5) FROM fdw132_t2 WHERE c2 = 200 and c1 < 50;
- sum 
------
-   3
-(1 row)
-
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(DISTINCT (t1.c1)%5) FROM fdw132_t1 t1 join fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c1 < 20 or (t1.c1 is null and t2.c1 < 5) GROUP BY (t2.c1)%3 ORDER BY 1;
-                                                                                                                                                                                     QUERY PLAN                                                                                                                                                                                      
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Foreign Scan
-   Output: (sum(DISTINCT (t1.c1 % 5))), ((t2.c1 % 3))
-   Relations: Aggregate on ((mysql_fdw_regress.fdw132_t1 t1) INNER JOIN (mysql_fdw_regress.fdw132_t2 t2))
-   Remote query: SELECT sum(DISTINCT (r1.`c1` % 5)), (r2.`c1` % 3) FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON ((((r1.`c1` < 20) OR ((r1.`c1` IS NULL) AND (r2.`c1` < 5)))) AND ((r1.`c1` = r2.`c1`)))) WHERE (((r1.`c1` < 20) OR (r1.`c1` IS NULL))) GROUP BY 2 ORDER BY sum(DISTINCT (r1.`c1` % 5)) IS NULL, sum(DISTINCT (r1.`c1` % 5)) ASC
-(4 rows)
-
-SELECT sum(DISTINCT (t1.c1)%5) FROM fdw132_t1 t1 join fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c1 < 20 or (t1.c1 is null and t2.c1 < 5) GROUP BY (t2.c1)%3 ORDER BY 1;
- sum 
------
-   1
-   2
-(2 rows)
-
--- DISTINCT with aggregate within aggregate
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(DISTINCT c1) FROM fdw132_t2 WHERE c2 = 200 and c1 < 50;
-                                                    QUERY PLAN                                                     
--------------------------------------------------------------------------------------------------------------------
- Foreign Scan
-   Output: (sum(DISTINCT c1))
-   Relations: Aggregate on (mysql_fdw_regress.fdw132_t2)
-   Remote query: SELECT sum(DISTINCT `c1`) FROM `mysql_fdw_regress`.`test2` WHERE ((`c1` < 50)) AND ((`c2` = 200))
-(4 rows)
-
-SELECT sum(DISTINCT c1) FROM fdw132_t2 WHERE c2 = 200 and c1 < 50;
- sum 
------
-  15
-(1 row)
-
--- DISTINCT combined with ORDER BY within aggregate is not pushed.
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT array_agg(DISTINCT (t1.c1)%5 ORDER BY (t1.c1)%5) FROM fdw132_t1 t1 join fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c1 < 20 or (t1.c1 is null and t2.c1 < 5) GROUP BY (t2.c1)%3 ORDER BY 1;
-                                                                                                                                                           QUERY PLAN                                                                                                                                                           
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Sort
-   Output: (array_agg(DISTINCT (t1.c1 % 5) ORDER BY (t1.c1 % 5))), ((t2.c1 % 3))
-   Sort Key: (array_agg(DISTINCT (t1.c1 % 5) ORDER BY (t1.c1 % 5)))
-   ->  GroupAggregate
-         Output: array_agg(DISTINCT (t1.c1 % 5) ORDER BY (t1.c1 % 5)), ((t2.c1 % 3))
-         Group Key: (t2.c1 % 3)
-         ->  Foreign Scan
-               Output: (t2.c1 % 3), t1.c1
-               Relations: (mysql_fdw_regress.fdw132_t1 t1) INNER JOIN (mysql_fdw_regress.fdw132_t2 t2)
-               Remote query: SELECT r2.`c1`, r1.`c1` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON ((((r1.`c1` < 20) OR ((r1.`c1` IS NULL) AND (r2.`c1` < 5)))) AND ((r1.`c1` = r2.`c1`)))) WHERE (((r1.`c1` < 20) OR (r1.`c1` IS NULL))) ORDER BY (r2.`c1` % 3) IS NULL, (r2.`c1` % 3) ASC
-(10 rows)
-
-SELECT array_agg(DISTINCT (t1.c1)%5 ORDER BY (t1.c1)%5) FROM fdw132_t1 t1 join fdw132_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c1 < 20 or (t1.c1 is null and t2.c1 < 5) GROUP BY (t2.c1)%3 ORDER BY 1;
- array_agg 
------------
- {1}
- {2}
-(2 rows)
-
--- DISTINCT, ORDER BY and FILTER within aggregate, not pushed down.
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(c1%3), sum(DISTINCT c1%3 ORDER BY c1%3) filter (WHERE c1%3 < 2), c2 FROM fdw132_t1 WHERE c2 = 100 GROUP BY c2;
-                                             QUERY PLAN                                              
------------------------------------------------------------------------------------------------------
- GroupAggregate
-   Output: sum((c1 % 3)), sum(DISTINCT (c1 % 3) ORDER BY (c1 % 3)) FILTER (WHERE ((c1 % 3) < 2)), c2
-   Group Key: fdw132_t1.c2
-   ->  Foreign Scan on public.fdw132_t1
-         Output: c1, c2, c3, c4
-         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` = 100))
-(6 rows)
-
-SELECT sum(c1%3), sum(DISTINCT c1%3 ORDER BY c1%3) filter (WHERE c1%3 < 2), c2 FROM fdw132_t1 WHERE c2 = 100 GROUP BY c2;
- sum | sum | c2  
------+-----+-----
-   5 |   1 | 100
-(1 row)
-
--- FILTER within aggregate, not pushed
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(c1) filter (WHERE c1 < 100 and c2 > 5) FROM fdw132_t1 GROUP BY c2 ORDER BY 1 nulls last;
-                                                   QUERY PLAN                                                   
-----------------------------------------------------------------------------------------------------------------
- Sort
-   Output: (sum(c1) FILTER (WHERE ((c1 < 100) AND (c2 > 5)))), c2
-   Sort Key: (sum(fdw132_t1.c1) FILTER (WHERE ((fdw132_t1.c1 < 100) AND (fdw132_t1.c2 > 5))))
-   ->  GroupAggregate
-         Output: sum(c1) FILTER (WHERE ((c1 < 100) AND (c2 > 5))), c2
-         Group Key: fdw132_t1.c2
-         ->  Foreign Scan on public.fdw132_t1
-               Output: c1, c2, c3, c4
-               Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` ORDER BY `c2` IS NULL, `c2` ASC
-(9 rows)
-
-SELECT sum(c1) filter (WHERE c1 < 100 and c2 > 5) FROM fdw132_t1 GROUP BY c2 ORDER BY 1 nulls last;
- sum 
------
-  14
-(1 row)
-
--- Outer query is aggregation query
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT DISTINCT (SELECT count(*) filter (WHERE t2.c2 = 200 and t2.c1 < 10) FROM fdw132_t1 t1 WHERE t1.c1 = 2) FROM fdw132_t2 t2 ORDER BY 1;
-                                             QUERY PLAN                                              
------------------------------------------------------------------------------------------------------
- Unique
-   Output: ((SubPlan 1))
-   ->  Sort
-         Output: ((SubPlan 1))
-         Sort Key: ((SubPlan 1))
-         ->  Aggregate
-               Output: (SubPlan 1)
-               ->  Foreign Scan on public.fdw132_t2 t2
-                     Output: t2.c1, t2.c2, t2.c3, t2.c4
-                     Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test2`
-               SubPlan 1
-                 ->  Foreign Scan on public.fdw132_t1 t1
-                       Output: count(*) FILTER (WHERE ((t2.c2 = 200) AND (t2.c1 < 10)))
-                       Remote query: SELECT NULL FROM `mysql_fdw_regress`.`test1` WHERE ((`c1` = 2))
-(14 rows)
-
-SELECT DISTINCT (SELECT count(*) filter (WHERE t2.c2 = 200 and t2.c1 < 10) FROM fdw132_t1 t1 WHERE t1.c1 = 2) FROM fdw132_t2 t2 ORDER BY 1;
- count 
--------
-     2
-(1 row)
-
--- Inner query is aggregation query
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT DISTINCT (SELECT count(t1.c1) filter (WHERE t2.c2 = 200 and t2.c1 < 10) FROM fdw132_t1 t1 WHERE t1.c1 = 2) FROM fdw132_t2 t2 ORDER BY 1;
-                                                QUERY PLAN                                                 
------------------------------------------------------------------------------------------------------------
- Unique
-   Output: ((SubPlan 1))
-   ->  Sort
-         Output: ((SubPlan 1))
-         Sort Key: ((SubPlan 1))
-         ->  Foreign Scan on public.fdw132_t2 t2
-               Output: (SubPlan 1)
-               Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test2`
-               SubPlan 1
-                 ->  Aggregate
-                       Output: count(t1.c1) FILTER (WHERE ((t2.c2 = 200) AND (t2.c1 < 10)))
-                       ->  Foreign Scan on public.fdw132_t1 t1
-                             Output: t1.c1, t1.c2, t1.c3, t1.c4
-                             Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` WHERE ((`c1` = 2))
-(14 rows)
-
-SELECT DISTINCT (SELECT count(t1.c1) filter (WHERE t2.c2 = 200 and t2.c1 < 10) FROM fdw132_t1 t1 WHERE t1.c1 = 2) FROM fdw132_t2 t2 ORDER BY 1;
- count 
--------
-     0
-     1
-(2 rows)
-
--- Ordered-sets within aggregate, not pushed down.
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c2, rank('10'::varchar) within group (ORDER BY c3), percentile_cont(c2/200::numeric) within group (ORDER BY c1) FROM fdw132_t2 GROUP BY c2 HAVING percentile_cont(c2/200::numeric) within group (ORDER BY c1) < 500 ORDER BY c2;
-                                                                                    QUERY PLAN                                                                                     
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- GroupAggregate
-   Output: c2, rank('10'::text) WITHIN GROUP (ORDER BY c3), percentile_cont((((c2)::numeric / '200'::numeric))::double precision) WITHIN GROUP (ORDER BY ((c1)::double precision))
-   Group Key: fdw132_t2.c2
-   Filter: (percentile_cont((((fdw132_t2.c2)::numeric / '200'::numeric))::double precision) WITHIN GROUP (ORDER BY ((fdw132_t2.c1)::double precision)) < '500'::double precision)
-   ->  Foreign Scan on public.fdw132_t2
-         Output: c1, c2, c3, c4
-         Remote query: SELECT `c1`, `c2`, `c3` FROM `mysql_fdw_regress`.`test2` ORDER BY `c2` IS NULL, `c2` ASC
-(7 rows)
-
-SELECT c2, rank('10'::varchar) within group (ORDER BY c3), percentile_cont(c2/200::numeric) within group (ORDER BY c1) FROM fdw132_t2 GROUP BY c2 HAVING percentile_cont(c2/200::numeric) within group (ORDER BY c1) < 500 ORDER BY c2;
- c2  | rank | percentile_cont 
------+------+-----------------
- 200 |    1 |              12
-(1 row)
-
--- Using multiple arguments within aggregates
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c1, rank(c1, c2) within group (ORDER BY c1, c2) FROM fdw132_t1 GROUP BY c1, c2 HAVING c1 = 2 ORDER BY 1;
-                                                         QUERY PLAN                                                          
------------------------------------------------------------------------------------------------------------------------------
- GroupAggregate
-   Output: c1, rank(c1, c2) WITHIN GROUP (ORDER BY c1, c2), c2
-   Group Key: fdw132_t1.c1, fdw132_t1.c2
-   ->  Foreign Scan on public.fdw132_t1
-         Output: c1, c2, c3, c4
-         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c1` = 2)) ORDER BY `c2` IS NULL, `c2` ASC
-(6 rows)
-
-SELECT c1, rank(c1, c2) within group (ORDER BY c1, c2) FROM fdw132_t1 GROUP BY c1, c2 HAVING c1 = 2 ORDER BY 1;
- c1 | rank 
-----+------
-  2 |    1
-(1 row)
-
--- Subquery in FROM clause HAVING aggregate
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT count(*), x.b FROM fdw132_t1, (SELECT c1 a, sum(c1) b FROM fdw132_t2 GROUP BY c1) x WHERE fdw132_t1.c1 = x.a GROUP BY x.b ORDER BY 1, 2;
-                                                    QUERY PLAN                                                    
-------------------------------------------------------------------------------------------------------------------
- Sort
-   Output: (count(*)), x.b
-   Sort Key: (count(*)), x.b
-   ->  HashAggregate
-         Output: count(*), x.b
-         Group Key: x.b
-         ->  Hash Join
-               Output: x.b
-               Inner Unique: true
-               Hash Cond: (fdw132_t1.c1 = x.a)
-               ->  Foreign Scan on public.fdw132_t1
-                     Output: fdw132_t1.c1, fdw132_t1.c2, fdw132_t1.c3, fdw132_t1.c4
-                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` ORDER BY `c1` IS NULL, `c1` ASC
-               ->  Hash
-                     Output: x.b, x.a
-                     ->  Subquery Scan on x
-                           Output: x.b, x.a
-                           ->  Foreign Scan
-                                 Output: fdw132_t2.c1, (sum(fdw132_t2.c1))
-                                 Relations: Aggregate on (mysql_fdw_regress.fdw132_t2)
-                                 Remote query: SELECT `c1`, sum(`c1`) FROM `mysql_fdw_regress`.`test2` GROUP BY 1
-(21 rows)
-
-SELECT count(*), x.b FROM fdw132_t1, (SELECT c1 a, sum(c1) b FROM fdw132_t2 GROUP BY c1) x WHERE fdw132_t1.c1 = x.a GROUP BY x.b ORDER BY 1, 2;
- count | b 
--------+---
-     1 | 1
-     1 | 2
-(2 rows)
-
--- Join with IS NULL check in HAVING
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT avg(t1.c1), sum(t2.c1) FROM fdw132_t1 t1 join fdw132_t2 t2 ON (t1.c1 = t2.c1) GROUP BY t2.c1 HAVING (avg(t1.c1) is null and sum(t2.c1) > 10) or sum(t2.c1) is null ORDER BY 1 nulls last, 2;
-                                                                                                                                                                           QUERY PLAN                                                                                                                                                                           
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Foreign Scan
-   Output: (avg(t1.c1)), (sum(t2.c1)), t2.c1
-   Relations: Aggregate on ((mysql_fdw_regress.fdw132_t1 t1) INNER JOIN (mysql_fdw_regress.fdw132_t2 t2))
-   Remote query: SELECT avg(r1.`c1`), sum(r2.`c1`), r2.`c1` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (((r1.`c1` = r2.`c1`)))) GROUP BY 3 HAVING ((((avg(r1.`c1`) IS NULL) AND (sum(r2.`c1`) > 10)) OR (sum(r2.`c1`) IS NULL))) ORDER BY avg(r1.`c1`) IS NULL, avg(r1.`c1`) ASC, sum(r2.`c1`) IS NULL, sum(r2.`c1`) ASC
-(4 rows)
-
-SELECT avg(t1.c1), sum(t2.c1) FROM fdw132_t1 t1 join fdw132_t2 t2 ON (t1.c1 = t2.c1) GROUP BY t2.c1 HAVING (avg(t1.c1) is null and sum(t2.c1) > 10) or sum(t2.c1) is null ORDER BY 1 nulls last, 2;
- avg | sum 
------+-----
-(0 rows)
-
--- ORDER BY expression is part of the target list but not pushed down to
--- foreign server.
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(c2) * (random() <= 1)::int AS sum FROM fdw132_t1 ORDER BY 1;
-                                      QUERY PLAN                                      
---------------------------------------------------------------------------------------
- Sort
-   Output: (((sum(c2)) * ((random() <= '1'::double precision))::integer))
-   Sort Key: (((sum(fdw132_t1.c2)) * ((random() <= '1'::double precision))::integer))
-   ->  Foreign Scan
-         Output: ((sum(c2)) * ((random() <= '1'::double precision))::integer)
-         Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-         Remote query: SELECT sum(`c2`) FROM `mysql_fdw_regress`.`test1`
-(7 rows)
-
-SELECT sum(c2) * (random() <= 1)::int AS sum FROM fdw132_t1 ORDER BY 1;
- sum 
------
- 300
-(1 row)
-
--- LATERAL join, with parameterization
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c2, sum FROM fdw132_t1 t1, lateral (SELECT sum(t2.c1 + t1.c1) sum FROM fdw132_t2 t2 GROUP BY t2.c1) qry WHERE t1.c2 * 2 = qry.sum and t1.c2 > 10 ORDER BY 1;
-                                                          QUERY PLAN                                                          
-------------------------------------------------------------------------------------------------------------------------------
- Nested Loop
-   Output: t1.c2, qry.sum
-   ->  Foreign Scan on public.fdw132_t1 t1
-         Output: t1.c1, t1.c2, t1.c3, t1.c4
-         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 10)) ORDER BY `c2` IS NULL, `c2` ASC
-   ->  Subquery Scan on qry
-         Output: qry.sum, t2.c1
-         Filter: ((t1.c2 * 2) = qry.sum)
-         ->  Foreign Scan
-               Output: (sum((t2.c1 + t1.c1))), t2.c1
-               Relations: Aggregate on (mysql_fdw_regress.fdw132_t2 t2)
-               Remote query: SELECT sum((`c1` + ?)), `c1` FROM `mysql_fdw_regress`.`test2` GROUP BY 2
-(12 rows)
-
--- Check with placeHolderVars
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT q.b, count(fdw132_t1.c1), sum(q.a) FROM fdw132_t1 left join (SELECT min(13), avg(fdw132_t1.c1), sum(fdw132_t2.c1) FROM fdw132_t1 right join fdw132_t2 ON (fdw132_t1.c1 = fdw132_t2.c1) WHERE fdw132_t1.c1 = 12) q(a, b, c) ON (fdw132_t1.c1 = q.b) WHERE fdw132_t1.c1 between 10 and 15 GROUP BY q.b ORDER BY 1 nulls last, 2;
-                                                                                                       QUERY PLAN                                                                                                       
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Sort
-   Output: q.b, (count(fdw132_t1.c1)), (sum(q.a))
-   Sort Key: q.b, (count(fdw132_t1.c1))
-   ->  HashAggregate
-         Output: q.b, count(fdw132_t1.c1), sum(q.a)
-         Group Key: q.b
-         ->  Hash Left Join
-               Output: q.b, fdw132_t1.c1, q.a
-               Inner Unique: true
-               Hash Cond: ((fdw132_t1.c1)::numeric = q.b)
-               ->  Foreign Scan on public.fdw132_t1
-                     Output: fdw132_t1.c1, fdw132_t1.c2, fdw132_t1.c3, fdw132_t1.c4
-                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` WHERE ((`c1` >= 10)) AND ((`c1` <= 15)) ORDER BY `c1` IS NULL, `c1` ASC
-               ->  Hash
-                     Output: q.b, q.a
-                     ->  Subquery Scan on q
-                           Output: q.b, q.a
-                           ->  Foreign Scan
-                                 Output: (min(13)), (avg(fdw132_t1_1.c1)), NULL::bigint
-                                 Relations: Aggregate on ((mysql_fdw_regress.fdw132_t1) INNER JOIN (mysql_fdw_regress.fdw132_t2))
-                                 Remote query: SELECT min(13), avg(r1.`c1`), NULL FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (TRUE)) WHERE ((r2.`c1` = 12)) AND ((r1.`c1` = 12))
-(21 rows)
-
-SELECT q.b, count(fdw132_t1.c1), sum(q.a) FROM fdw132_t1 left join (SELECT min(13), avg(fdw132_t1.c1), sum(fdw132_t2.c1) FROM fdw132_t1 right join fdw132_t2 ON (fdw132_t1.c1 = fdw132_t2.c1) WHERE fdw132_t1.c1 = 12) q(a, b, c) ON (fdw132_t1.c1 = q.b) WHERE fdw132_t1.c1 between 10 and 15 GROUP BY q.b ORDER BY 1 nulls last, 2;
- b | count | sum 
----+-------+-----
-   |     1 |    
-(1 row)
-
--- Not supported cases
--- Grouping sets
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c2, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY rollup(c2) ORDER BY 1 nulls last;
-                                                         QUERY PLAN                                                          
------------------------------------------------------------------------------------------------------------------------------
- GroupAggregate
-   Output: c2, sum(c1)
-   Group Key: fdw132_t1.c2
-   Group Key: ()
-   ->  Foreign Scan on public.fdw132_t1
-         Output: c1, c2, c3, c4
-         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 3)) ORDER BY `c2` IS NULL, `c2` ASC
-(7 rows)
-
-SELECT c2, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY rollup(c2) ORDER BY 1 nulls last;
- c2  | sum 
------+-----
- 100 |  14
-     |  14
-(2 rows)
-
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c2, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY cube(c2) ORDER BY 1 nulls last;
-                                                         QUERY PLAN                                                          
------------------------------------------------------------------------------------------------------------------------------
- GroupAggregate
-   Output: c2, sum(c1)
-   Group Key: fdw132_t1.c2
-   Group Key: ()
-   ->  Foreign Scan on public.fdw132_t1
-         Output: c1, c2, c3, c4
-         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 3)) ORDER BY `c2` IS NULL, `c2` ASC
-(7 rows)
-
-SELECT c2, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY cube(c2) ORDER BY 1 nulls last;
- c2  | sum 
------+-----
- 100 |  14
-     |  14
-(2 rows)
-
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c2, c3, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY grouping sets(c2, c3) ORDER BY 1 nulls last, 2 nulls last;
-                                                               QUERY PLAN                                                                
------------------------------------------------------------------------------------------------------------------------------------------
- Sort
-   Output: c2, c3, (sum(c1))
-   Sort Key: fdw132_t1.c2, fdw132_t1.c3 COLLATE "C"
-   ->  MixedAggregate
-         Output: c2, c3, sum(c1)
-         Hash Key: fdw132_t1.c3
-         Group Key: fdw132_t1.c2
-         ->  Foreign Scan on public.fdw132_t1
-               Output: c1, c2, c3, c4
-               Remote query: SELECT `c1`, `c2`, `c3` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 3)) ORDER BY `c2` IS NULL, `c2` ASC
-(10 rows)
-
-SELECT c2, c3, sum(c1) FROM fdw132_t1 WHERE c2 > 3 GROUP BY grouping sets(c2, c3) ORDER BY 1 nulls last, 2 nulls last;
- c2  |  c3   | sum 
------+-------+-----
- 100 |       |  14
-     | AAA1  |   1
-     | AAA11 |  11
-     | AAA2  |   2
-(4 rows)
-
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c2, sum(c1), grouping(c2) FROM fdw132_t1 WHERE c2 > 3 GROUP BY c2 ORDER BY 1 nulls last;
-                                                         QUERY PLAN                                                          
------------------------------------------------------------------------------------------------------------------------------
- GroupAggregate
-   Output: c2, sum(c1), GROUPING(c2)
-   Group Key: fdw132_t1.c2
-   ->  Foreign Scan on public.fdw132_t1
-         Output: c1, c2, c3, c4
-         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 3)) ORDER BY `c2` IS NULL, `c2` ASC
-(6 rows)
-
-SELECT c2, sum(c1), grouping(c2) FROM fdw132_t1 WHERE c2 > 3 GROUP BY c2 ORDER BY 1 nulls last;
- c2  | sum | grouping 
------+-----+----------
- 100 |  14 |        0
-(1 row)
-
--- DISTINCT itself is not pushed down, whereas underneath aggregate is pushed
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT DISTINCT sum(c1) s FROM fdw132_t1 WHERE c2 > 6 GROUP BY c2 ORDER BY 1;
-                                                    QUERY PLAN                                                     
--------------------------------------------------------------------------------------------------------------------
- Unique
-   Output: (sum(c1)), c2
-   ->  Sort
-         Output: (sum(c1)), c2
-         Sort Key: (sum(fdw132_t1.c1))
-         ->  Foreign Scan
-               Output: (sum(c1)), c2
-               Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-               Remote query: SELECT sum(`c1`), `c2` FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 6)) GROUP BY 2
-(9 rows)
-
-SELECT DISTINCT sum(c1) s FROM fdw132_t1 WHERE c2 > 6 GROUP BY c2 ORDER BY 1;
- s  
-----
- 14
-(1 row)
-
--- WindowAgg
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c2, sum(c2), count(c2) over (partition by c2%2) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
-                                                              QUERY PLAN                                                              
---------------------------------------------------------------------------------------------------------------------------------------
- Sort
-   Output: c2, (sum(c2)), (count(c2) OVER (?)), ((c2 % 2))
-   Sort Key: fdw132_t1.c2
-   ->  WindowAgg
-         Output: c2, (sum(c2)), count(c2) OVER (?), ((c2 % 2))
-         ->  Sort
-               Output: c2, ((c2 % 2)), (sum(c2))
-               Sort Key: ((fdw132_t1.c2 % 2))
-               ->  Foreign Scan
-                     Output: c2, ((c2 % 2)), (sum(c2))
-                     Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-                     Remote query: SELECT `c2`, (`c2` % 2), sum(`c2`) FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 10)) GROUP BY 1
-(12 rows)
-
-SELECT c2, sum(c2), count(c2) over (partition by c2%2) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
- c2  | sum | count 
------+-----+-------
- 100 | 300 |     1
-(1 row)
-
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c2, array_agg(c2) over (partition by c2%2 ORDER BY c2 desc) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
-                                                        QUERY PLAN                                                         
----------------------------------------------------------------------------------------------------------------------------
- Sort
-   Output: c2, (array_agg(c2) OVER (?)), ((c2 % 2))
-   Sort Key: fdw132_t1.c2
-   ->  WindowAgg
-         Output: c2, array_agg(c2) OVER (?), ((c2 % 2))
-         ->  Sort
-               Output: c2, ((c2 % 2))
-               Sort Key: ((fdw132_t1.c2 % 2)), fdw132_t1.c2 DESC
-               ->  Foreign Scan
-                     Output: c2, ((c2 % 2))
-                     Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-                     Remote query: SELECT `c2`, (`c2` % 2) FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 10)) GROUP BY 1
-(12 rows)
-
-SELECT c2, array_agg(c2) over (partition by c2%2 ORDER BY c2 desc) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
- c2  | array_agg 
------+-----------
- 100 | {100}
-(1 row)
-
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c2, array_agg(c2) over (partition by c2%2 ORDER BY c2 range between current row and unbounded following) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
-                                                        QUERY PLAN                                                         
----------------------------------------------------------------------------------------------------------------------------
- Sort
-   Output: c2, (array_agg(c2) OVER (?)), ((c2 % 2))
-   Sort Key: fdw132_t1.c2
-   ->  WindowAgg
-         Output: c2, array_agg(c2) OVER (?), ((c2 % 2))
-         ->  Sort
-               Output: c2, ((c2 % 2))
-               Sort Key: ((fdw132_t1.c2 % 2)), fdw132_t1.c2
-               ->  Foreign Scan
-                     Output: c2, ((c2 % 2))
-                     Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-                     Remote query: SELECT `c2`, (`c2` % 2) FROM `mysql_fdw_regress`.`test1` WHERE ((`c2` > 10)) GROUP BY 1
-(12 rows)
-
-SELECT c2, array_agg(c2) over (partition by c2%2 ORDER BY c2 range between current row and unbounded following) FROM fdw132_t1 WHERE c2 > 10 GROUP BY c2 ORDER BY 1;
- c2  | array_agg 
------+-----------
- 100 | {100}
-(1 row)
-
--- User defined function for user defined aggregate, VARIADIC
-CREATE FUNCTION least_accum(anyelement, variadic anyarray)
-returns anyelement language sql AS
-  'SELECT least($1, min($2[i])) FROM generate_subscripts($2,2) g(i)';
-CREATE aggregate least_agg(variadic items anyarray) (
-  stype = anyelement, sfunc = least_accum
-);
--- Not pushed down due to user defined aggregate
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c2, least_agg(c1) FROM fdw132_t1 GROUP BY c2 ORDER BY c2;
-                                                QUERY PLAN                                                
-----------------------------------------------------------------------------------------------------------
- GroupAggregate
-   Output: c2, least_agg(VARIADIC ARRAY[c1])
-   Group Key: fdw132_t1.c2
-   ->  Foreign Scan on public.fdw132_t1
-         Output: c1, c2, c3, c4
-         Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` ORDER BY `c2` IS NULL, `c2` ASC
-(6 rows)
-
-SELECT c2, least_agg(c1) FROM fdw132_t1 GROUP BY c2 ORDER BY c2;
- c2  | least_agg 
------+-----------
- 100 |          
-(1 row)
-
--- FDW-129: Limit and offset pushdown with Aggregate pushdown.
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT 1 OFFSET 1;
-                                                             QUERY PLAN                                                              
--------------------------------------------------------------------------------------------------------------------------------------
- Foreign Scan
-   Output: (sum(c1)), c1
-   Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-   Remote query: SELECT sum(`c1`), `c1` FROM `mysql_fdw_regress`.`test1` GROUP BY 2 ORDER BY `c1` IS NULL, `c1` ASC LIMIT 1 OFFSET 1
-(4 rows)
-
-SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT 1 OFFSET 1;
- sum | c1 
------+----
-   2 |  2
-(1 row)
-
--- Limit 0, Offset 0 with aggregates.
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT 0 OFFSET 0;
-                                                             QUERY PLAN                                                              
--------------------------------------------------------------------------------------------------------------------------------------
- Foreign Scan
-   Output: (sum(c1)), c1
-   Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-   Remote query: SELECT sum(`c1`), `c1` FROM `mysql_fdw_regress`.`test1` GROUP BY 2 ORDER BY `c1` IS NULL, `c1` ASC LIMIT 0 OFFSET 0
-(4 rows)
-
-SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT 0 OFFSET 0;
- sum | c1 
------+----
-(0 rows)
-
--- Limit NULL
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT NULL OFFSET 2;
-                                                        QUERY PLAN                                                        
---------------------------------------------------------------------------------------------------------------------------
- Limit
-   Output: (sum(c1)), c1
-   ->  Foreign Scan
-         Output: (sum(c1)), c1
-         Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-         Remote query: SELECT sum(`c1`), `c1` FROM `mysql_fdw_regress`.`test1` GROUP BY 2 ORDER BY `c1` IS NULL, `c1` ASC
-(6 rows)
-
-SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT NULL OFFSET 2;
- sum | c1 
------+----
-  11 | 11
-(1 row)
-
--- Limit ALL
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT ALL OFFSET 2;
-                                                        QUERY PLAN                                                        
---------------------------------------------------------------------------------------------------------------------------
- Limit
-   Output: (sum(c1)), c1
-   ->  Foreign Scan
-         Output: (sum(c1)), c1
-         Relations: Aggregate on (mysql_fdw_regress.fdw132_t1)
-         Remote query: SELECT sum(`c1`), `c1` FROM `mysql_fdw_regress`.`test1` GROUP BY 2 ORDER BY `c1` IS NULL, `c1` ASC
-(6 rows)
-
-SELECT sum(c1), c1 FROM fdw132_t1 GROUP BY c1 ORDER BY c1 LIMIT ALL OFFSET 2;
- sum | c1 
------+----
-  11 | 11
-(1 row)
-
--- Delete existing data and load new data for partition-wise aggregate test
--- cases.
-DELETE FROM fdw132_t1;
-DELETE FROM fdw132_t2;
-INSERT INTO fdw132_t1 values(1, 1, 'AAA1', 'foo');
-INSERT INTO fdw132_t1 values(2, 2, 'AAA2', 'bar');
-INSERT INTO fdw132_t2 values(3, 3, 'AAA11', 'foo');
-INSERT INTO fdw132_t2 values(4, 4, 'AAA12', 'foo');
--- Test partition-wise aggregates
-SET enable_partitionwise_aggregate TO on;
--- Create the partition table.
-CREATE TABLE fprt1 (c1 int, c2 int, c3 varchar, c4 varchar) PARTITION BY RANGE(c1);
-CREATE FOREIGN TABLE ftprt1_p1 PARTITION OF fprt1 FOR VALUES FROM (1) TO (2)
-  SERVER mysql_svr OPTIONS (dbname 'mysql_fdw_regress', table_name 'test1');
-CREATE FOREIGN TABLE ftprt1_p2 PARTITION OF fprt1 FOR VALUES FROM (3) TO (4)
-  SERVER mysql_svr OPTIONS (dbname 'mysql_fdw_regress', TABLE_NAME 'test2');
--- Plan with partitionwise aggregates is enabled
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c1, sum(c1) FROM fprt1 GROUP BY c1 ORDER BY 2;
-                                           QUERY PLAN                                           
-------------------------------------------------------------------------------------------------
- Sort
-   Output: ftprt1_p1.c1, (sum(ftprt1_p1.c1))
-   Sort Key: (sum(ftprt1_p1.c1))
-   ->  Append
-         ->  Foreign Scan
-               Output: ftprt1_p1.c1, (sum(ftprt1_p1.c1))
-               Relations: Aggregate on (mysql_fdw_regress.ftprt1_p1 fprt1)
-               Remote query: SELECT `c1`, sum(`c1`) FROM `mysql_fdw_regress`.`test1` GROUP BY 1
-         ->  Foreign Scan
-               Output: ftprt1_p2.c1, (sum(ftprt1_p2.c1))
-               Relations: Aggregate on (mysql_fdw_regress.ftprt1_p2 fprt1)
-               Remote query: SELECT `c1`, sum(`c1`) FROM `mysql_fdw_regress`.`test2` GROUP BY 1
-(12 rows)
-
-SELECT c1, sum(c1) FROM fprt1 GROUP BY c1 ORDER BY 2;
- c1 | sum 
-----+-----
-  1 |   1
-  2 |   2
-  3 |   3
-  4 |   4
-(4 rows)
-
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c1, sum(c2), min(c2), count(*) FROM fprt1 GROUP BY c1 HAVING avg(c2) < 22 ORDER BY 2;
-                                                                  QUERY PLAN                                                                   
------------------------------------------------------------------------------------------------------------------------------------------------
- Sort
-   Output: ftprt1_p1.c1, (sum(ftprt1_p1.c2)), (min(ftprt1_p1.c2)), (count(*))
-   Sort Key: (sum(ftprt1_p1.c2))
-   ->  Append
-         ->  Foreign Scan
-               Output: ftprt1_p1.c1, (sum(ftprt1_p1.c2)), (min(ftprt1_p1.c2)), (count(*))
-               Relations: Aggregate on (mysql_fdw_regress.ftprt1_p1 fprt1)
-               Remote query: SELECT `c1`, sum(`c2`), min(`c2`), count(*) FROM `mysql_fdw_regress`.`test1` GROUP BY 1 HAVING ((avg(`c2`) < 22))
-         ->  Foreign Scan
-               Output: ftprt1_p2.c1, (sum(ftprt1_p2.c2)), (min(ftprt1_p2.c2)), (count(*))
-               Relations: Aggregate on (mysql_fdw_regress.ftprt1_p2 fprt1)
-               Remote query: SELECT `c1`, sum(`c2`), min(`c2`), count(*) FROM `mysql_fdw_regress`.`test2` GROUP BY 1 HAVING ((avg(`c2`) < 22))
-(12 rows)
-
-SELECT c1, sum(c2), min(c2), count(*) FROM fprt1 GROUP BY c1 HAVING avg(c2) < 22 ORDER BY 1;
- c1 | sum | min | count 
-----+-----+-----+-------
-  1 |   1 |   1 |     1
-  2 |   2 |   2 |     1
-  3 |   3 |   3 |     1
-  4 |   4 |   4 |     1
-(4 rows)
-
--- Check with whole-row reference
--- Should have all the columns in the target list for the given relation
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c1, count(t1) FROM fprt1 t1 GROUP BY c1 HAVING avg(c2) < 22 ORDER BY 1;
-                                                         QUERY PLAN                                                         
-----------------------------------------------------------------------------------------------------------------------------
- GroupAggregate
-   Output: t1.c1, count(((t1.*)::fprt1))
-   Group Key: t1.c1
-   Filter: (avg(t1.c2) < '22'::numeric)
-   ->  Append
-         ->  Foreign Scan on public.ftprt1_p1 t1
-               Output: t1.c1, t1.*, t1.c2
-               Remote query: SELECT `c1`, `c2`, `c3`, `c4` FROM `mysql_fdw_regress`.`test1` ORDER BY `c1` IS NULL, `c1` ASC
-         ->  Foreign Scan on public.ftprt1_p2 t1_1
-               Output: t1_1.c1, t1_1.*, t1_1.c2
-               Remote query: SELECT `c1`, `c2`, `c3`, `c4` FROM `mysql_fdw_regress`.`test2` ORDER BY `c1` IS NULL, `c1` ASC
-(11 rows)
-
-SELECT c1, count(t1) FROM fprt1 t1 GROUP BY c1 HAVING avg(c2) < 22 ORDER BY 1;
- c1 | count 
-----+-------
-  1 |     1
-  2 |     1
-  3 |     1
-  4 |     1
-(4 rows)
-
--- When GROUP BY clause does not match with PARTITION KEY.
-EXPLAIN (VERBOSE, COSTS OFF)
-SELECT c2, avg(c1), max(c1), count(*) FROM fprt1 GROUP BY c2 HAVING sum(c1) < 700 ORDER BY 1;
-                                                              QUERY PLAN                                                               
----------------------------------------------------------------------------------------------------------------------------------------
- Finalize GroupAggregate
-   Output: ftprt1_p1.c2, avg(ftprt1_p1.c1), max(ftprt1_p1.c1), count(*)
-   Group Key: ftprt1_p1.c2
-   Filter: (sum(ftprt1_p1.c1) < 700)
-   ->  Merge Append
-         Sort Key: ftprt1_p1.c2
-         ->  Partial GroupAggregate
-               Output: ftprt1_p1.c2, PARTIAL avg(ftprt1_p1.c1), PARTIAL max(ftprt1_p1.c1), PARTIAL count(*), PARTIAL sum(ftprt1_p1.c1)
-               Group Key: ftprt1_p1.c2
-               ->  Foreign Scan on public.ftprt1_p1
-                     Output: ftprt1_p1.c2, ftprt1_p1.c1
-                     Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test1` ORDER BY `c2` IS NULL, `c2` ASC
-         ->  Partial GroupAggregate
-               Output: ftprt1_p2.c2, PARTIAL avg(ftprt1_p2.c1), PARTIAL max(ftprt1_p2.c1), PARTIAL count(*), PARTIAL sum(ftprt1_p2.c1)
-               Group Key: ftprt1_p2.c2
-               ->  Foreign Scan on public.ftprt1_p2
-                     Output: ftprt1_p2.c2, ftprt1_p2.c1
-                     Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test2` ORDER BY `c2` IS NULL, `c2` ASC
-(18 rows)
-
-SELECT c2, avg(c1), max(c1), count(*) FROM fprt1 GROUP BY c2 HAVING sum(c1) < 700 ORDER BY 1;
- c2 |          avg           | max | count 
-----+------------------------+-----+-------
-  1 | 1.00000000000000000000 |   1 |     1
-  2 |     2.0000000000000000 |   2 |     1
-  3 |     3.0000000000000000 |   3 |     1
-  4 |     4.0000000000000000 |   4 |     1
-(4 rows)
-
-SET enable_partitionwise_aggregate TO off;
--- Cleanup
-DROP aggregate least_agg(variadic items anyarray);
-DROP FUNCTION least_accum(anyelement, variadic anyarray);
-DELETE FROM fdw132_t1;
-DELETE FROM fdw132_t2;
-DROP FOREIGN TABLE fdw132_t1;
-DROP FOREIGN TABLE fdw132_t2;
-DROP FOREIGN TABLE ftprt1_p1;
-DROP FOREIGN TABLE ftprt1_p2;
-DROP TABLE IF EXISTS fprt1;
-DROP TYPE user_enum;
-DROP USER MAPPING FOR public SERVER mysql_svr;
-DROP SERVER mysql_svr;
-DROP EXTENSION mysql_fdw;
diff -pruN 2.9.2-2/expected/join_pushdown.out 2.9.3-1/expected/join_pushdown.out
--- 2.9.2-2/expected/join_pushdown.out	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/expected/join_pushdown.out	2025-09-30 05:28:24.000000000 +0000
@@ -1113,7 +1113,6 @@ SELECT t1.c1,t2.c2,t3.c3
 (4 rows)
 
 -- With whole-row reference; partitionwise join does not apply
--- Table alias in foreign scan is different for v12, v11 and v10.
 EXPLAIN (VERBOSE, COSTS false)
 SELECT t1, t2, t1.c1
   FROM fprt1 t1 JOIN fprt2 t2 ON (t1.c1 = t2.c2)
@@ -1189,7 +1188,6 @@ SELECT t1.c1,t1.c2
 (4 rows)
 
 -- With PHVs, partitionwise join selected but no join pushdown
--- Table alias in foreign scan is different for v12, v11 and v10.
 EXPLAIN (VERBOSE, COSTS OFF)
 SELECT t1.c1, t1.phv, t2.c2, t2.phv
   FROM (SELECT 't1_phv' phv, * FROM fprt1 WHERE c1 % 2 = 0) t1 LEFT JOIN
diff -pruN 2.9.2-2/expected/join_pushdown_1.out 2.9.3-1/expected/join_pushdown_1.out
--- 2.9.2-2/expected/join_pushdown_1.out	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/expected/join_pushdown_1.out	2025-09-30 05:28:24.000000000 +0000
@@ -204,6 +204,7 @@ SELECT t1.c1, t2.c1
                                                    QUERY PLAN                                                   
 ----------------------------------------------------------------------------------------------------------------
  Sort
+   Disabled: true
    Output: t1.c1, t2.c1
    Sort Key: t1.c1, t2.c1
    ->  Nested Loop Left Join
@@ -217,7 +218,7 @@ SELECT t1.c1, t2.c1
                ->  Foreign Scan on public.fdw139_t2 t2
                      Output: t2.c1
                      Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test2` ORDER BY `c1` IS NULL, `c1` ASC
-(14 rows)
+(15 rows)
 
 SELECT t1.c1, t2.c1
   FROM fdw139_t1 t1 LEFT JOIN fdw139_t2 t2 ON (abs(t1.c1) = t2.c1)
@@ -332,9 +333,11 @@ SELECT t1.c1, t2.c1
                                                    QUERY PLAN                                                   
 ----------------------------------------------------------------------------------------------------------------
  Sort
+   Disabled: true
    Output: t1.c1, t2.c1
    Sort Key: t1.c1, t2.c1
    ->  Merge Full Join
+         Disabled: true
          Output: t1.c1, t2.c1
          Merge Cond: (t1.c1 = t2.c1)
          ->  Foreign Scan on public.fdw139_t1 t1
@@ -345,7 +348,7 @@ SELECT t1.c1, t2.c1
                ->  Foreign Scan on public.fdw139_t1 t2
                      Output: t2.c1, t2.c2, t2.c3, t2.c4
                      Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` ORDER BY `c1` IS NULL, `c1` ASC
-(14 rows)
+(16 rows)
 
 SELECT t1.c1, t2.c1
   FROM fdw139_t1 t1 FULL JOIN fdw139_t1 t2 ON (t1.c1 = t2.c1)
@@ -592,6 +595,7 @@ SELECT t1.c1, t2.c1, l1.c1
  Limit
    Output: t1.c1, t2.c1, l1.c1
    ->  Sort
+         Disabled: true
          Output: t1.c1, t2.c1, l1.c1
          Sort Key: t1.c1, t2.c1, l1.c1
          ->  Nested Loop
@@ -602,7 +606,7 @@ SELECT t1.c1, t2.c1, l1.c1
                      Output: t1.c1, t2.c1
                      Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
                      Remote query: SELECT r1.`c1`, r2.`c1` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (TRUE))
-(13 rows)
+(14 rows)
 
 SELECT t1.c1, t2.c1, l1.c1
   FROM fdw139_t1 t1 CROSS JOIN fdw139_t2 t2 CROSS JOIN local_t1 l1
@@ -663,6 +667,7 @@ SELECT t1.c1, t2.c1
                                       QUERY PLAN                                      
 --------------------------------------------------------------------------------------
  Sort
+   Disabled: true
    Output: t1.c1, t2.c1
    Sort Key: t1.c1, t2.c1
    ->  Nested Loop Left Join
@@ -676,7 +681,7 @@ SELECT t1.c1, t2.c1
                ->  Foreign Scan on public.fdw139_t2 t2
                      Output: t2.c1, t2.c4
                      Remote query: SELECT `c1`, `c4` FROM `mysql_fdw_regress`.`test2`
-(14 rows)
+(15 rows)
 
 SELECT t1.c1, t2.c1
   FROM fdw139_t1 t1 LEFT JOIN fdw139_t2 t2 ON (t1.c4 = t2.c4)
@@ -697,9 +702,10 @@ SELECT t1.c1, t2.c1
   ORDER BY t1.c1, t2.c1 NULLS LAST;
                                                    QUERY PLAN                                                   
 ----------------------------------------------------------------------------------------------------------------
- Sort
+ Incremental Sort
    Output: t1.c1, t2.c1
    Sort Key: t1.c1, t2.c1
+   Presorted Key: t1.c1
    ->  Nested Loop Left Join
          Output: t1.c1, t2.c1
          Join Filter: (t1.c1 = t2.c1)
@@ -712,7 +718,7 @@ SELECT t1.c1, t2.c1
                ->  Foreign Scan on public.fdw139_t2 t2
                      Output: t2.c1
                      Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test2` ORDER BY `c1` IS NULL, `c1` ASC
-(15 rows)
+(16 rows)
 
 SELECT t1.c1, t2.c1
   FROM fdw139_t1 t1 LEFT JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c4 = 'foo'
@@ -765,9 +771,10 @@ SELECT t1.c1, t2.c2
   ORDER BY t1.c1, t2.c1, t2.c2 NULLS LAST;  -- not pushed down, different view owners
                                                       QUERY PLAN                                                      
 ----------------------------------------------------------------------------------------------------------------------
- Sort
+ Incremental Sort
    Output: fdw139_t1.c1, fdw139_t2.c2, fdw139_t2.c1
    Sort Key: fdw139_t1.c1, fdw139_t2.c1, fdw139_t2.c2
+   Presorted Key: fdw139_t1.c1
    ->  Nested Loop Left Join
          Output: fdw139_t1.c1, fdw139_t2.c2, fdw139_t2.c1
          Join Filter: (fdw139_t1.c1 = fdw139_t2.c1)
@@ -779,7 +786,7 @@ SELECT t1.c1, t2.c2
                ->  Foreign Scan on public.fdw139_t2
                      Output: fdw139_t2.c2, fdw139_t2.c1
                      Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test2` ORDER BY `c1` IS NULL, `c1` ASC
-(14 rows)
+(15 rows)
 
 SELECT t1.c1, t2.c2
   FROM v1 t1 LEFT JOIN v2 t2 ON (t1.c1 = t2.c1)
@@ -801,7 +808,7 @@ SELECT t1.c1, t2.c2
  Foreign Scan
    Output: fdw139_t1.c1, fdw139_t2.c2, fdw139_t2.c1
    Relations: (mysql_fdw_regress.fdw139_t1) LEFT JOIN (mysql_fdw_regress.fdw139_t2)
-   Remote query: SELECT r6.`c1`, r9.`c2`, r9.`c1` FROM (`mysql_fdw_regress`.`test1` r6 LEFT JOIN `mysql_fdw_regress`.`test2` r9 ON (((r6.`c1` = r9.`c1`)))) ORDER BY r6.`c1` IS NULL, r6.`c1` ASC, r9.`c1` IS NULL, r9.`c1` ASC, r9.`c2` IS NULL, r9.`c2` ASC
+   Remote query: SELECT r4.`c1`, r5.`c2`, r5.`c1` FROM (`mysql_fdw_regress`.`test1` r4 LEFT JOIN `mysql_fdw_regress`.`test2` r5 ON (((r4.`c1` = r5.`c1`)))) ORDER BY r4.`c1` IS NULL, r4.`c1` ASC, r5.`c1` IS NULL, r5.`c1` ASC, r5.`c2` IS NULL, r5.`c2` ASC
 (4 rows)
 
 SELECT t1.c1, t2.c2
@@ -820,9 +827,10 @@ SELECT t1.c1, t2.c2
   ORDER BY t1.c1, t2.c1, t2.c2 NULLS LAST;  -- not pushed down, view owner not current user
                                                       QUERY PLAN                                                      
 ----------------------------------------------------------------------------------------------------------------------
- Sort
+ Incremental Sort
    Output: fdw139_t1.c1, t2.c2, t2.c1
    Sort Key: fdw139_t1.c1, t2.c1, t2.c2
+   Presorted Key: fdw139_t1.c1
    ->  Nested Loop Left Join
          Output: fdw139_t1.c1, t2.c2, t2.c1
          Join Filter: (fdw139_t1.c1 = t2.c1)
@@ -834,7 +842,7 @@ SELECT t1.c1, t2.c2
                ->  Foreign Scan on public.fdw139_t2 t2
                      Output: t2.c2, t2.c1
                      Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test2` ORDER BY `c1` IS NULL, `c1` ASC
-(14 rows)
+(15 rows)
 
 SELECT t1.c1, t2.c2
   FROM v1 t1 LEFT JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
@@ -856,7 +864,7 @@ SELECT t1.c1, t2.c2
  Foreign Scan
    Output: fdw139_t1.c1, t2.c2, t2.c1
    Relations: (mysql_fdw_regress.fdw139_t1) LEFT JOIN (mysql_fdw_regress.fdw139_t2 t2)
-   Remote query: SELECT r6.`c1`, r2.`c2`, r2.`c1` FROM (`mysql_fdw_regress`.`test1` r6 LEFT JOIN `mysql_fdw_regress`.`test2` r2 ON (((r6.`c1` = r2.`c1`)))) ORDER BY r6.`c1` IS NULL, r6.`c1` ASC, r2.`c1` IS NULL, r2.`c1` ASC, r2.`c2` IS NULL, r2.`c2` ASC
+   Remote query: SELECT r4.`c1`, r2.`c2`, r2.`c1` FROM (`mysql_fdw_regress`.`test1` r4 LEFT JOIN `mysql_fdw_regress`.`test2` r2 ON (((r4.`c1` = r2.`c1`)))) ORDER BY r4.`c1` IS NULL, r4.`c1` ASC, r2.`c1` IS NULL, r2.`c1` ASC, r2.`c2` IS NULL, r2.`c2` ASC
 (4 rows)
 
 SELECT t1.c1, t2.c2
@@ -1084,19 +1092,22 @@ EXPLAIN (VERBOSE, COSTS OFF)
 SELECT t1.c1,t2.c2,t3.c3
   FROM fprt1 t1 INNER JOIN fprt2 t2 ON (t1.c1 = t2.c2) INNER JOIN fprt1 t3 ON (t2.c2 = t3.c1)
   WHERE t1.c1 % 2 =0 ORDER BY 1,2,3;
-                                                                                                                                                                  QUERY PLAN                                                                                                                                                                   
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Merge Append
+                                                                                                                                  QUERY PLAN                                                                                                                                   
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Sort
+   Disabled: true
+   Output: t1.c1, t2.c2, t3.c3
    Sort Key: t1.c1, t3.c3
-   ->  Foreign Scan
-         Output: t1.c1, t2.c2, t3.c3
-         Relations: ((mysql_fdw_regress.ftprt1_p1 t1) INNER JOIN (mysql_fdw_regress.ftprt2_p1 t2)) INNER JOIN (mysql_fdw_regress.ftprt1_p1 t3)
-         Remote query: SELECT r6.`c1`, r8.`c2`, r10.`c3` FROM ((`mysql_fdw_regress`.`test1` r6 INNER JOIN `mysql_fdw_regress`.`test3` r8 ON (((r6.`c1` = r8.`c2`)))) INNER JOIN `mysql_fdw_regress`.`test1` r10 ON (((r6.`c1` = r10.`c1`)))) WHERE (((r6.`c1` % 2) = 0)) ORDER BY r6.`c1` IS NULL, r6.`c1` ASC, r10.`c3` IS NULL, r10.`c3` ASC
-   ->  Foreign Scan
-         Output: t1_1.c1, t2_1.c2, t3_1.c3
-         Relations: ((mysql_fdw_regress.ftprt1_p2 t1) INNER JOIN (mysql_fdw_regress.ftprt2_p2 t2)) INNER JOIN (mysql_fdw_regress.ftprt1_p2 t3)
-         Remote query: SELECT r7.`c1`, r9.`c2`, r11.`c3` FROM ((`mysql_fdw_regress`.`test2` r7 INNER JOIN `mysql_fdw_regress`.`test4` r9 ON (((r7.`c1` = r9.`c2`)))) INNER JOIN `mysql_fdw_regress`.`test2` r11 ON (((r7.`c1` = r11.`c1`)))) WHERE (((r7.`c1` % 2) = 0)) ORDER BY r7.`c1` IS NULL, r7.`c1` ASC, r11.`c3` IS NULL, r11.`c3` ASC
-(10 rows)
+   ->  Append
+         ->  Foreign Scan
+               Output: t1_1.c1, t2_1.c2, t3_1.c3
+               Relations: ((mysql_fdw_regress.ftprt1_p1 t1) INNER JOIN (mysql_fdw_regress.ftprt2_p1 t2)) INNER JOIN (mysql_fdw_regress.ftprt1_p1 t3)
+               Remote query: SELECT r6.`c1`, r8.`c2`, r10.`c3` FROM ((`mysql_fdw_regress`.`test1` r6 INNER JOIN `mysql_fdw_regress`.`test3` r8 ON (((r6.`c1` = r8.`c2`)))) INNER JOIN `mysql_fdw_regress`.`test1` r10 ON (((r6.`c1` = r10.`c1`)))) WHERE (((r6.`c1` % 2) = 0))
+         ->  Foreign Scan
+               Output: t1_2.c1, t2_2.c2, t3_2.c3
+               Relations: ((mysql_fdw_regress.ftprt1_p2 t1) INNER JOIN (mysql_fdw_regress.ftprt2_p2 t2)) INNER JOIN (mysql_fdw_regress.ftprt1_p2 t3)
+               Remote query: SELECT r7.`c1`, r9.`c2`, r11.`c3` FROM ((`mysql_fdw_regress`.`test2` r7 INNER JOIN `mysql_fdw_regress`.`test4` r9 ON (((r7.`c1` = r9.`c2`)))) INNER JOIN `mysql_fdw_regress`.`test2` r11 ON (((r7.`c1` = r11.`c1`)))) WHERE (((r7.`c1` % 2) = 0))
+(13 rows)
 
 SELECT t1.c1,t2.c2,t3.c3
   FROM fprt1 t1 INNER JOIN fprt2 t2 ON (t1.c1 = t2.c2) INNER JOIN fprt1 t3 ON (t2.c2 = t3.c1)
@@ -1110,34 +1121,36 @@ SELECT t1.c1,t2.c2,t3.c3
 (4 rows)
 
 -- With whole-row reference; partitionwise join does not apply
--- Table alias in foreign scan is different for v12, v11 and v10.
 EXPLAIN (VERBOSE, COSTS false)
 SELECT t1, t2, t1.c1
   FROM fprt1 t1 JOIN fprt2 t2 ON (t1.c1 = t2.c2)
   ORDER BY t1.c3, t1.c1;
-                                                                     QUERY PLAN                                                                     
-----------------------------------------------------------------------------------------------------------------------------------------------------
- Nested Loop
+                                            QUERY PLAN                                            
+--------------------------------------------------------------------------------------------------
+ Sort
+   Disabled: true
    Output: ((t1.*)::fprt1), ((t2.*)::fprt2), t1.c1, t1.c3
-   Join Filter: (t1.c1 = t2.c2)
-   ->  Merge Append
-         Sort Key: t1.c3, t1.c1
-         ->  Foreign Scan on public.ftprt1_p1 t1
-               Output: t1.*, t1.c1, t1.c3
-               Remote query: SELECT `c1`, `c2`, `c3`, `c4` FROM `mysql_fdw_regress`.`test1` ORDER BY `c3` IS NULL, `c3` ASC, `c1` IS NULL, `c1` ASC
-         ->  Foreign Scan on public.ftprt1_p2 t1_1
-               Output: t1_1.*, t1_1.c1, t1_1.c3
-               Remote query: SELECT `c1`, `c2`, `c3`, `c4` FROM `mysql_fdw_regress`.`test2` ORDER BY `c3` IS NULL, `c3` ASC, `c1` IS NULL, `c1` ASC
-   ->  Materialize
-         Output: ((t2.*)::fprt2), t2.c2
+   Sort Key: t1.c3, t1.c1
+   ->  Nested Loop
+         Output: ((t1.*)::fprt1), ((t2.*)::fprt2), t1.c1, t1.c3
+         Join Filter: (t1.c1 = t2.c2)
          ->  Append
-               ->  Foreign Scan on public.ftprt2_p1 t2
-                     Output: t2.*, t2.c2
-                     Remote query: SELECT `c1`, `c2`, `c3` FROM `mysql_fdw_regress`.`test3` ORDER BY `c2` IS NULL, `c2` ASC
-               ->  Foreign Scan on public.ftprt2_p2 t2_1
-                     Output: t2_1.*, t2_1.c2
-                     Remote query: SELECT `c1`, `c2`, `c3` FROM `mysql_fdw_regress`.`test4` ORDER BY `c2` IS NULL, `c2` ASC
-(20 rows)
+               ->  Foreign Scan on public.ftprt1_p1 t1_1
+                     Output: t1_1.*, t1_1.c1, t1_1.c3
+                     Remote query: SELECT `c1`, `c2`, `c3`, `c4` FROM `mysql_fdw_regress`.`test1`
+               ->  Foreign Scan on public.ftprt1_p2 t1_2
+                     Output: t1_2.*, t1_2.c1, t1_2.c3
+                     Remote query: SELECT `c1`, `c2`, `c3`, `c4` FROM `mysql_fdw_regress`.`test2`
+         ->  Materialize
+               Output: ((t2.*)::fprt2), t2.c2
+               ->  Append
+                     ->  Foreign Scan on public.ftprt2_p1 t2_1
+                           Output: t2_1.*, t2_1.c2
+                           Remote query: SELECT `c1`, `c2`, `c3` FROM `mysql_fdw_regress`.`test3`
+                     ->  Foreign Scan on public.ftprt2_p2 t2_2
+                           Output: t2_2.*, t2_2.c2
+                           Remote query: SELECT `c1`, `c2`, `c3` FROM `mysql_fdw_regress`.`test4`
+(23 rows)
 
 SELECT t1, t2, t1.c1
   FROM fprt1 t1 JOIN fprt2 t2 ON (t1.c1 = t2.c2)
@@ -1160,19 +1173,22 @@ EXPLAIN (VERBOSE, COSTS OFF)
 SELECT t1.c1,t1.c2
   FROM fprt1 t1, LATERAL (SELECT t2.c1, t2.c2 FROM fprt2 t2
   WHERE t1.c1 = t2.c2 AND t1.c2 = t2.c1) q WHERE t1.c1 % 2 = 0 ORDER BY 1,2;
-                                                                                                                                     QUERY PLAN                                                                                                                                     
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Merge Append
+                                                                                                      QUERY PLAN                                                                                                      
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Sort
+   Disabled: true
+   Output: t1.c1, t1.c2
    Sort Key: t1.c1, t1.c2
-   ->  Foreign Scan
-         Output: t1.c1, t1.c2
-         Relations: (mysql_fdw_regress.ftprt1_p1 t1) INNER JOIN (mysql_fdw_regress.ftprt2_p1 t2)
-         Remote query: SELECT r4.`c1`, r4.`c2` FROM (`mysql_fdw_regress`.`test1` r4 INNER JOIN `mysql_fdw_regress`.`test3` r6 ON (((r4.`c1` = r6.`c2`)) AND ((r4.`c2` = r6.`c1`)))) WHERE (((r4.`c1` % 2) = 0)) ORDER BY r4.`c1` IS NULL, r4.`c1` ASC, r4.`c2` IS NULL, r4.`c2` ASC
-   ->  Foreign Scan
-         Output: t1_1.c1, t1_1.c2
-         Relations: (mysql_fdw_regress.ftprt1_p2 t1) INNER JOIN (mysql_fdw_regress.ftprt2_p2 t2)
-         Remote query: SELECT r5.`c1`, r5.`c2` FROM (`mysql_fdw_regress`.`test2` r5 INNER JOIN `mysql_fdw_regress`.`test4` r7 ON (((r5.`c1` = r7.`c2`)) AND ((r5.`c2` = r7.`c1`)))) WHERE (((r5.`c1` % 2) = 0)) ORDER BY r5.`c1` IS NULL, r5.`c1` ASC, r5.`c2` IS NULL, r5.`c2` ASC
-(10 rows)
+   ->  Append
+         ->  Foreign Scan
+               Output: t1_1.c1, t1_1.c2
+               Relations: (mysql_fdw_regress.ftprt1_p1 t1) INNER JOIN (mysql_fdw_regress.ftprt2_p1 t2)
+               Remote query: SELECT r4.`c1`, r4.`c2` FROM (`mysql_fdw_regress`.`test1` r4 INNER JOIN `mysql_fdw_regress`.`test3` r6 ON (((r4.`c1` = r6.`c2`)) AND ((r4.`c2` = r6.`c1`)))) WHERE (((r4.`c1` % 2) = 0))
+         ->  Foreign Scan
+               Output: t1_2.c1, t1_2.c2
+               Relations: (mysql_fdw_regress.ftprt1_p2 t1) INNER JOIN (mysql_fdw_regress.ftprt2_p2 t2)
+               Remote query: SELECT r5.`c1`, r5.`c2` FROM (`mysql_fdw_regress`.`test2` r5 INNER JOIN `mysql_fdw_regress`.`test4` r7 ON (((r5.`c1` = r7.`c2`)) AND ((r5.`c2` = r7.`c1`)))) WHERE (((r5.`c1` % 2) = 0))
+(13 rows)
 
 SELECT t1.c1,t1.c2
   FROM fprt1 t1, LATERAL (SELECT t2.c1, t2.c2 FROM fprt2 t2
@@ -1186,41 +1202,41 @@ SELECT t1.c1,t1.c2
 (4 rows)
 
 -- With PHVs, partitionwise join selected but no join pushdown
--- Table alias in foreign scan is different for v12, v11 and v10.
 EXPLAIN (VERBOSE, COSTS OFF)
 SELECT t1.c1, t1.phv, t2.c2, t2.phv
   FROM (SELECT 't1_phv' phv, * FROM fprt1 WHERE c1 % 2 = 0) t1 LEFT JOIN
     (SELECT 't2_phv' phv, * FROM fprt2 WHERE c2 % 2 = 0) t2 ON (t1.c1 = t2.c2)
   ORDER BY t1.c1, t2.c2;
-                                                                  QUERY PLAN                                                                   
------------------------------------------------------------------------------------------------------------------------------------------------
+                                                  QUERY PLAN                                                   
+---------------------------------------------------------------------------------------------------------------
  Sort
-   Output: ftprt1_p1.c1, 't1_phv'::text, ftprt2_p1.c2, ('t2_phv'::text)
-   Sort Key: ftprt1_p1.c1, ftprt2_p1.c2
+   Disabled: true
+   Output: fprt1.c1, 't1_phv'::text, fprt2.c2, ('t2_phv'::text)
+   Sort Key: fprt1.c1, fprt2.c2
    ->  Append
          ->  Nested Loop Left Join
-               Output: ftprt1_p1.c1, 't1_phv'::text, ftprt2_p1.c2, ('t2_phv'::text)
-               Join Filter: (ftprt1_p1.c1 = ftprt2_p1.c2)
-               ->  Foreign Scan on public.ftprt1_p1
-                     Output: ftprt1_p1.c1
-                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` WHERE (((`c1` % 2) = 0)) ORDER BY `c1` IS NULL, `c1` ASC
+               Output: fprt1_1.c1, 't1_phv'::text, fprt2_1.c2, ('t2_phv'::text)
+               Join Filter: (fprt1_1.c1 = fprt2_1.c2)
+               ->  Foreign Scan on public.ftprt1_p1 fprt1_1
+                     Output: fprt1_1.c1
+                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` WHERE (((`c1` % 2) = 0))
                ->  Materialize
-                     Output: ftprt2_p1.c2, ('t2_phv'::text)
-                     ->  Foreign Scan on public.ftprt2_p1
-                           Output: ftprt2_p1.c2, 't2_phv'::text
-                           Remote query: SELECT `c2` FROM `mysql_fdw_regress`.`test3` WHERE (((`c2` % 2) = 0)) ORDER BY `c2` IS NULL, `c2` ASC
+                     Output: fprt2_1.c2, ('t2_phv'::text)
+                     ->  Foreign Scan on public.ftprt2_p1 fprt2_1
+                           Output: fprt2_1.c2, 't2_phv'::text
+                           Remote query: SELECT `c2` FROM `mysql_fdw_regress`.`test3` WHERE (((`c2` % 2) = 0))
          ->  Nested Loop Left Join
-               Output: ftprt1_p2.c1, 't1_phv'::text, ftprt2_p2.c2, ('t2_phv'::text)
-               Join Filter: (ftprt1_p2.c1 = ftprt2_p2.c2)
-               ->  Foreign Scan on public.ftprt1_p2
-                     Output: ftprt1_p2.c1
-                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test2` WHERE (((`c1` % 2) = 0)) ORDER BY `c1` IS NULL, `c1` ASC
+               Output: fprt1_2.c1, 't1_phv'::text, fprt2_2.c2, ('t2_phv'::text)
+               Join Filter: (fprt1_2.c1 = fprt2_2.c2)
+               ->  Foreign Scan on public.ftprt1_p2 fprt1_2
+                     Output: fprt1_2.c1
+                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test2` WHERE (((`c1` % 2) = 0))
                ->  Materialize
-                     Output: ftprt2_p2.c2, ('t2_phv'::text)
-                     ->  Foreign Scan on public.ftprt2_p2
-                           Output: ftprt2_p2.c2, 't2_phv'::text
-                           Remote query: SELECT `c2` FROM `mysql_fdw_regress`.`test4` WHERE (((`c2` % 2) = 0)) ORDER BY `c2` IS NULL, `c2` ASC
-(26 rows)
+                     Output: fprt2_2.c2, ('t2_phv'::text)
+                     ->  Foreign Scan on public.ftprt2_p2 fprt2_2
+                           Output: fprt2_2.c2, 't2_phv'::text
+                           Remote query: SELECT `c2` FROM `mysql_fdw_regress`.`test4` WHERE (((`c2` % 2) = 0))
+(27 rows)
 
 SELECT t1.c1, t1.phv, t2.c2, t2.phv
   FROM (SELECT 't1_phv' phv, * FROM fprt1 WHERE c1 % 2 = 0) t1 LEFT JOIN
@@ -1244,13 +1260,14 @@ SELECT t3 FROM fdw139_t1 t1
                                                                                                                    QUERY PLAN                                                                                                                    
 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Sort
+   Disabled: true
    Output: t3.*
    Sort Key: t3.*
    ->  Foreign Scan
          Output: t3.*
          Relations: ((mysql_fdw_regress.fdw139_t1 t1) LEFT JOIN (mysql_fdw_regress.fdw139_t1 t2)) LEFT JOIN (mysql_fdw_regress.fdw139_t1 t3)
          Remote query: SELECT r4.`c1`, r4.`c2`, r4.`c3`, r4.`c4` FROM ((`mysql_fdw_regress`.`test1` r1 LEFT JOIN `mysql_fdw_regress`.`test1` r2 ON (((r1.`c1` = r2.`c1`)))) LEFT JOIN `mysql_fdw_regress`.`test1` r4 ON (((r4.`c1` = r2.`c1`))))
-(7 rows)
+(8 rows)
 
 SELECT t3 FROM fdw139_t1 t1
     LEFT JOIN fdw139_t1 t2 ON (t1.c1 = t2.c1)
diff -pruN 2.9.2-2/expected/join_pushdown_3.out 2.9.3-1/expected/join_pushdown_3.out
--- 2.9.2-2/expected/join_pushdown_3.out	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/expected/join_pushdown_3.out	2025-09-30 05:28:24.000000000 +0000
@@ -1113,7 +1113,6 @@ SELECT t1.c1,t2.c2,t3.c3
 (4 rows)
 
 -- With whole-row reference; partitionwise join does not apply
--- Table alias in foreign scan is different for v12, v11 and v10.
 EXPLAIN (VERBOSE, COSTS false)
 SELECT t1, t2, t1.c1
   FROM fprt1 t1 JOIN fprt2 t2 ON (t1.c1 = t2.c2)
@@ -1189,7 +1188,6 @@ SELECT t1.c1,t1.c2
 (4 rows)
 
 -- With PHVs, partitionwise join selected but no join pushdown
--- Table alias in foreign scan is different for v12, v11 and v10.
 EXPLAIN (VERBOSE, COSTS OFF)
 SELECT t1.c1, t1.phv, t2.c2, t2.phv
   FROM (SELECT 't1_phv' phv, * FROM fprt1 WHERE c1 % 2 = 0) t1 LEFT JOIN
diff -pruN 2.9.2-2/expected/select.out 2.9.3-1/expected/select.out
--- 2.9.2-2/expected/select.out	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/expected/select.out	2025-09-30 05:28:24.000000000 +0000
@@ -15,7 +15,7 @@ CREATE USER MAPPING FOR PUBLIC SERVER my
 SELECT mysql_fdw_version();
  mysql_fdw_version 
 -------------------
-             20902
+             20903
 (1 row)
 
 -- Create foreign tables
@@ -1202,19 +1202,21 @@ EXPLAIN (VERBOSE, COSTS OFF)
 SELECT c1, c2 FROM f_test_tbl1
   WHERE c8 = (SELECT c1 FROM f_test_tbl2 WHERE c1 = (SELECT 20))
   ORDER BY c1;
-                                                        QUERY PLAN                                                         
----------------------------------------------------------------------------------------------------------------------------
+                                                  QUERY PLAN                                                  
+--------------------------------------------------------------------------------------------------------------
  Foreign Scan on public.f_test_tbl1
    Output: f_test_tbl1.c1, f_test_tbl1.c2
-   Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test_tbl1` WHERE ((`c8` = ?)) ORDER BY `c1` IS NULL, `c1` ASC
+   Filter: (f_test_tbl1.c8 = $1)
+   Remote query: SELECT `c1`, `c2`, `c8` FROM `mysql_fdw_regress`.`test_tbl1` ORDER BY `c1` IS NULL, `c1` ASC
    InitPlan 2 (returns $1)
      ->  Foreign Scan on public.f_test_tbl2
            Output: f_test_tbl2.c1
-           Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test_tbl2` WHERE ((`c1` = ?))
+           Filter: (f_test_tbl2.c1 = $0)
+           Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test_tbl2`
            InitPlan 1 (returns $0)
              ->  Result
                    Output: 20
-(10 rows)
+(12 rows)
 
 SELECT c1, c2 FROM f_test_tbl1
   WHERE c8 = (SELECT c1 FROM f_test_tbl2 WHERE c1 = (SELECT 20))
@@ -2186,6 +2188,19 @@ SELECT c1, c2, c6, c8 FROM f_test_tbl1 e
  600 | EMP6 | 2850.00000 | 30
 (1 row)
 
+-- FDW-704: Self join queries with CTE should work correctly.
+WITH
+  tbl1 (a, b) AS MATERIALIZED (VALUES (1,1))
+SELECT
+  (SELECT t.a FROM f_mysql_test "t"
+    LEFT JOIN f_mysql_test "s" ON s.a = t.a
+    WHERE t.a = tbl1.a AND t.b = tbl1.b) "val"
+  FROM tbl1;
+ val 
+-----
+   1
+(1 row)
+
 -- Cleanup
 DROP TABLE l_test_tbl1;
 DROP TABLE l_test_tbl2;
diff -pruN 2.9.2-2/expected/select_1.out 2.9.3-1/expected/select_1.out
--- 2.9.2-2/expected/select_1.out	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/expected/select_1.out	2025-09-30 05:28:24.000000000 +0000
@@ -15,7 +15,7 @@ CREATE USER MAPPING FOR PUBLIC SERVER my
 SELECT mysql_fdw_version();
  mysql_fdw_version 
 -------------------
-             20902
+             20903
 (1 row)
 
 -- Create foreign tables
@@ -1202,19 +1202,21 @@ EXPLAIN (VERBOSE, COSTS OFF)
 SELECT c1, c2 FROM f_test_tbl1
   WHERE c8 = (SELECT c1 FROM f_test_tbl2 WHERE c1 = (SELECT 20))
   ORDER BY c1;
-                                                        QUERY PLAN                                                         
----------------------------------------------------------------------------------------------------------------------------
+                                                  QUERY PLAN                                                  
+--------------------------------------------------------------------------------------------------------------
  Foreign Scan on public.f_test_tbl1
    Output: f_test_tbl1.c1, f_test_tbl1.c2
-   Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test_tbl1` WHERE ((`c8` = ?)) ORDER BY `c1` IS NULL, `c1` ASC
+   Filter: (f_test_tbl1.c8 = (InitPlan 2).col1)
+   Remote query: SELECT `c1`, `c2`, `c8` FROM `mysql_fdw_regress`.`test_tbl1` ORDER BY `c1` IS NULL, `c1` ASC
    InitPlan 2
      ->  Foreign Scan on public.f_test_tbl2
            Output: f_test_tbl2.c1
-           Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test_tbl2` WHERE ((`c1` = ?))
+           Filter: (f_test_tbl2.c1 = (InitPlan 1).col1)
+           Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test_tbl2`
            InitPlan 1
              ->  Result
                    Output: 20
-(10 rows)
+(12 rows)
 
 SELECT c1, c2 FROM f_test_tbl1
   WHERE c8 = (SELECT c1 FROM f_test_tbl2 WHERE c1 = (SELECT 20))
@@ -2186,6 +2188,19 @@ SELECT c1, c2, c6, c8 FROM f_test_tbl1 e
  600 | EMP6 | 2850.00000 | 30
 (1 row)
 
+-- FDW-704: Self join queries with CTE should work correctly.
+WITH
+  tbl1 (a, b) AS MATERIALIZED (VALUES (1,1))
+SELECT
+  (SELECT t.a FROM f_mysql_test "t"
+    LEFT JOIN f_mysql_test "s" ON s.a = t.a
+    WHERE t.a = tbl1.a AND t.b = tbl1.b) "val"
+  FROM tbl1;
+ val 
+-----
+   1
+(1 row)
+
 -- Cleanup
 DROP TABLE l_test_tbl1;
 DROP TABLE l_test_tbl2;
diff -pruN 2.9.2-2/mysql_fdw--1.0.sql 2.9.3-1/mysql_fdw--1.0.sql
--- 2.9.2-2/mysql_fdw--1.0.sql	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/mysql_fdw--1.0.sql	2025-09-30 05:28:24.000000000 +0000
@@ -4,7 +4,7 @@
  * 			Foreign-data wrapper for remote MySQL servers
  *
  * Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
- * Portions Copyright (c) 2004-2024, EnterpriseDB Corporation.
+ * Portions Copyright (c) 2004-2025, EnterpriseDB Corporation.
  *
  * IDENTIFICATION
  * 			mysql_fdw--1.0.sql
diff -pruN 2.9.2-2/mysql_fdw--1.1.sql 2.9.3-1/mysql_fdw--1.1.sql
--- 2.9.2-2/mysql_fdw--1.1.sql	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/mysql_fdw--1.1.sql	2025-09-30 05:28:24.000000000 +0000
@@ -4,7 +4,7 @@
  * 			Foreign-data wrapper for remote MySQL servers
  *
  * Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
- * Portions Copyright (c) 2004-2024, EnterpriseDB Corporation.
+ * Portions Copyright (c) 2004-2025, EnterpriseDB Corporation.
  *
  * IDENTIFICATION
  * 			mysql_fdw--1.1.sql
diff -pruN 2.9.2-2/mysql_fdw--1.2.sql 2.9.3-1/mysql_fdw--1.2.sql
--- 2.9.2-2/mysql_fdw--1.2.sql	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/mysql_fdw--1.2.sql	2025-09-30 05:28:24.000000000 +0000
@@ -3,7 +3,7 @@
  * mysql_fdw--1.2.sql
  * 			Foreign-data wrapper for remote MySQL servers
  *
- * Portions Copyright (c) 2022-2024, EnterpriseDB Corporation.
+ * Portions Copyright (c) 2022-2025, EnterpriseDB Corporation.
  *
  * IDENTIFICATION
  * 			mysql_fdw--1.2.sql
diff -pruN 2.9.2-2/mysql_fdw.c 2.9.3-1/mysql_fdw.c
--- 2.9.2-2/mysql_fdw.c	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/mysql_fdw.c	2025-09-30 05:28:24.000000000 +0000
@@ -4,7 +4,7 @@
  * 		Foreign-data wrapper for remote MySQL servers
  *
  * Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
- * Portions Copyright (c) 2004-2024, EnterpriseDB Corporation.
+ * Portions Copyright (c) 2004-2025, EnterpriseDB Corporation.
  *
  * IDENTIFICATION
  * 		mysql_fdw.c
@@ -32,6 +32,10 @@
 #include "access/table.h"
 #include "commands/defrem.h"
 #include "commands/explain.h"
+#if PG_VERSION_NUM >= 180000
+#include "commands/explain_format.h"
+#include "commands/explain_state.h"
+#endif
 #include "catalog/heap.h"
 #include "catalog/pg_type.h"
 #include "foreign/fdwapi.h"
@@ -115,9 +119,9 @@ unsigned int ((mysql_num_rows) (MYSQL_RE
 
 /*
  * In PG 9.5.1 the number will be 90501,
- * our version is 2.9.2 so number will be 20902
+ * our version is 2.9.3 so number will be 20903
  */
-#define CODE_VERSION   20902
+#define CODE_VERSION   20903
 
 /*
  * The number of rows in a foreign relation are estimated to be so less that
@@ -347,7 +351,7 @@ static TargetEntry *mysql_tlist_member_m
 static List *mysql_varlist_append_unique_var(List *varlist, Var *var);
 #endif
 
-void *mysql_dll_handle = NULL;
+static void *mysql_dll_handle = NULL;
 static int wait_timeout = WAIT_TIMEOUT;
 static int interactive_timeout = INTERACTIVE_TIMEOUT;
 static void mysql_error_print(MYSQL *conn);
@@ -1224,7 +1228,20 @@ mysqlGetForeignPaths(PlannerInfo *root,
 					   foreigntableid);
 
 	/* Create a ForeignPath node and add it as only possible path */
-#if PG_VERSION_NUM >= 170000
+#if PG_VERSION_NUM >= 180000
+	add_path(baserel, (Path *)
+			 create_foreignscan_path(root, baserel,
+									 NULL,	/* default pathtarget */
+									 baserel->rows,
+									 0,
+									 startup_cost,
+									 total_cost,
+									 NIL,	/* no pathkeys */
+									 baserel->lateral_relids,
+									 NULL,	/* no extra plan */
+									 NIL, /* no fdw_restrictinfo list */
+									 NIL));	/* no fdw_private data */
+#elif PG_VERSION_NUM >= 170000
 	add_path(baserel, (Path *)
 			 create_foreignscan_path(root, baserel,
 									 NULL,	/* default pathtarget */
@@ -1599,11 +1616,7 @@ mysqlPlanForeignModify(PlannerInfo *root
 	 * Core code already has some lock on each rel being planned, so we can
 	 * use NoLock here.
 	 */
-#if PG_VERSION_NUM < 130000
-	rel = heap_open(rte->relid, NoLock);
-#else
 	rel = table_open(rte->relid, NoLock);
-#endif
 
 	foreignTableId = RelationGetRelid(rel);
 
@@ -1695,11 +1708,7 @@ mysqlPlanForeignModify(PlannerInfo *root
 				(errcode(ERRCODE_FDW_UNABLE_TO_CREATE_EXECUTION),
 				 errmsg("RETURNING is not supported by this FDW")));
 
-#if PG_VERSION_NUM < 130000
-	heap_close(rel, NoLock);
-#else
 	table_close(rel, NoLock);
-#endif
 
 	return list_make2(makeString(sql.data), targetAttrs);
 }
@@ -2546,18 +2555,18 @@ process_query_params(ExprContext *econte
 	{
 		ExprState  *expr_state = (ExprState *) lfirst(lc);
 		Datum		expr_value;
-		bool		isNull;
+		bool		*isNull = (bool *) palloc(sizeof(bool));
 
 		/* Evaluate the parameter expression */
-		expr_value = ExecEvalExpr(expr_state, econtext, &isNull);
+		expr_value = ExecEvalExpr(expr_state, econtext, isNull);
 		mysql_bind_sql_var(param_types[i], i, expr_value, *mysql_bind_buf,
-						   &isNull);
+						   isNull);
 
 		/*
 		 * Get string representation of each parameter value by invoking
 		 * type-specific output function, unless the value is null.
 		 */
-		if (isNull)
+		if (*isNull)
 			param_values[i] = NULL;
 		else
 			param_values[i] = OutputFunctionCall(&param_flinfo[i], expr_value);
@@ -2841,7 +2850,20 @@ mysqlGetForeignJoinPaths(PlannerInfo *ro
 	 * Create a new join path and add it to the joinrel which represents a
 	 * join between foreign tables.
 	 */
-#if PG_VERSION_NUM >= 170000
+#if PG_VERSION_NUM >= 180000
+	joinpath = create_foreign_join_path(root,
+										joinrel,
+										NULL,	/* default pathtarget */
+										joinrel->rows,
+										0,
+										startup_cost,
+										total_cost,
+										NIL,	/* no pathkeys */
+										joinrel->lateral_relids,
+										epq_path,
+										extra->restrictlist,
+										NIL);	/* no fdw_private */
+#elif PG_VERSION_NUM >= 170000
 	joinpath = create_foreign_join_path(root,
 										joinrel,
 										NULL,	/* default pathtarget */
@@ -3272,11 +3294,7 @@ mysql_build_scan_list_for_baserel(Oid re
 	*retrieved_attrs = NIL;
 
 	/* Planner must have taken a lock, so request no lock here */
-#if PG_VERSION_NUM < 130000
-	relation = heap_open(relid, NoLock);
-#else
 	relation = table_open(relid, NoLock);
-#endif
 
 	tupdesc = RelationGetDescr(relation);
 
@@ -3309,11 +3327,7 @@ mysql_build_scan_list_for_baserel(Oid re
 		}
 	}
 
-#if PG_VERSION_NUM < 130000
-	heap_close(relation, NoLock);
-#else
 	table_close(relation, NoLock);
-#endif
 
 	return tlist;
 }
@@ -3878,7 +3892,19 @@ mysql_add_foreign_grouping_paths(Planner
 #endif
 
 	/* Create and add foreign path to the grouping relation. */
-#if PG_VERSION_NUM >= 170000
+#if PG_VERSION_NUM >= 180000
+	grouppath = create_foreign_upper_path(root,
+										  grouped_rel,
+										  grouped_rel->reltarget,
+										  num_groups,
+										  0,
+										  startup_cost,
+										  total_cost,
+										  NIL,	/* no pathkeys */
+										  NULL,
+										  NIL,	/* no fdw_restrictinfo list */
+										  NIL);	/* no fdw_private */
+#elif PG_VERSION_NUM >= 170000
 	grouppath = create_foreign_upper_path(root,
 										  grouped_rel,
 										  grouped_rel->reltarget,
@@ -4099,10 +4125,17 @@ mysql_get_useful_pathkeys_for_relation(P
 			continue;
 
 		/* Looks like we can generate a pathkey, so let's do it. */
+#if PG_VERSION_NUM >= 180000
+		pathkey = make_canonical_pathkey(root, cur_ec,
+										 linitial_oid(cur_ec->ec_opfamilies),
+										 COMPARE_LT,
+										 false);
+#else
 		pathkey = make_canonical_pathkey(root, cur_ec,
 										 linitial_oid(cur_ec->ec_opfamilies),
 										 BTLessStrategyNumber,
 										 false);
+#endif
 
 		/* Check for sort operator pushability. */
 		if (mysql_get_sortby_direction_string(em, pathkey) == NULL)
@@ -4167,7 +4200,20 @@ mysql_add_paths_with_pathkeys(PlannerInf
 								 -1.0);
 
 		if (IS_SIMPLE_REL(rel))
-#if PG_VERSION_NUM >= 170000
+#if PG_VERSION_NUM >= 180000
+			add_path(rel, (Path *)
+					 create_foreignscan_path(root, rel,
+											 NULL,
+											 rel->rows,
+											 0,
+											 startup_cost,
+											 total_cost,
+											 useful_pathkeys,
+											 rel->lateral_relids,
+											 sorted_epq_path,
+											 NIL,	/* no fdw_restrictinfo list */
+											 NIL));	/* no fdw_private list */
+#elif PG_VERSION_NUM >= 170000
 			add_path(rel, (Path *)
 					 create_foreignscan_path(root, rel,
 											 NULL,
@@ -4192,7 +4238,20 @@ mysql_add_paths_with_pathkeys(PlannerInf
 											 NIL));	/* no fdw_private list */
 #endif
 		else
-#if PG_VERSION_NUM >= 170000
+#if PG_VERSION_NUM >= 180000
+			add_path(rel, (Path *)
+					 create_foreign_join_path(root, rel,
+											  NULL,
+											  rel->rows,
+											  0,
+											  startup_cost,
+											  total_cost,
+											  useful_pathkeys,
+											  rel->lateral_relids,
+											  sorted_epq_path,
+											  restrictlist,
+											  NIL));	/* no fdw_private */
+#elif PG_VERSION_NUM >= 170000
 			add_path(rel, (Path *)
 					 create_foreign_join_path(root, rel,
 											  NULL,
@@ -4351,7 +4410,19 @@ mysql_add_foreign_ordered_paths(PlannerI
 	fdw_private = list_make2(makeInteger(true), makeInteger(false));
 
 	/* Create foreign ordering path */
-#if PG_VERSION_NUM >= 170000
+#if PG_VERSION_NUM >= 180000
+	ordered_path = create_foreign_upper_path(root,
+											 input_rel,
+											 root->upper_targets[UPPERREL_ORDERED],
+											 rows,
+											 0,
+											 startup_cost,
+											 total_cost,
+											 root->sort_pathkeys,
+											 NULL,	/* no extra plan */
+											 NIL,	/* no fdw_restrictinfo list */
+											 fdw_private);
+#elif PG_VERSION_NUM >= 170000
 	ordered_path = create_foreign_upper_path(root,
 											 input_rel,
 											 root->upper_targets[UPPERREL_ORDERED],
@@ -4544,7 +4615,19 @@ mysql_add_foreign_final_paths(PlannerInf
 				 * no-longer-needed outer plan (if any), which makes the
 				 * EXPLAIN output look cleaner
 				 */
-#if PG_VERSION_NUM >= 170000
+#if PG_VERSION_NUM >= 180000
+				final_path = create_foreign_upper_path(root,
+													   path->parent,
+													   path->pathtarget,
+													   path->rows,
+													   0,
+													   path->startup_cost,
+													   path->total_cost,
+													   path->pathkeys,
+													   NULL,	/* no extra plan */
+													   NIL,		/* no fdw_restrictinfo list */
+													   NIL);	/* no fdw_private */
+#elif PG_VERSION_NUM >= 170000
 				final_path = create_foreign_upper_path(root,
 													   path->parent,
 													   path->pathtarget,
@@ -4670,7 +4753,19 @@ mysql_add_foreign_final_paths(PlannerInf
 	 * Create foreign final path; this gets rid of a no-longer-needed outer
 	 * plan (if any), which makes the EXPLAIN output look cleaner
 	 */
-#if PG_VERSION_NUM >= 170000
+#if PG_VERSION_NUM >= 180000
+	final_path = create_foreign_upper_path(root,
+										   input_rel,
+										   root->upper_targets[UPPERREL_FINAL],
+										   rows,
+										   0,
+										   startup_cost,
+										   total_cost,
+										   pathkeys,
+										   NULL,	/* no extra plan */
+										   NIL,		/* no fdw_restrictinfo list */
+										   fdw_private);
+#elif PG_VERSION_NUM >= 170000
 	final_path = create_foreign_upper_path(root,
 										   input_rel,
 										   root->upper_targets[UPPERREL_FINAL],
@@ -4962,13 +5057,26 @@ mysql_get_sortby_direction_string(Equiva
 	if (!mysql_is_builtin(pathkey->pk_opfamily))
 		return NULL;
 
+#if PG_VERSION_NUM >= 180000
+	oprid = get_opfamily_member_for_cmptype(pathkey->pk_opfamily,
+											em->em_datatype,
+											em->em_datatype,
+											pathkey->pk_cmptype);
+#else
 	oprid = get_opfamily_member(pathkey->pk_opfamily, em->em_datatype,
 								em->em_datatype, pathkey->pk_strategy);
+#endif
 
 	if (!OidIsValid(oprid))
+#if PG_VERSION_NUM >= 180000
+		elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
+			 pathkey->pk_cmptype, em->em_datatype, em->em_datatype,
+			 pathkey->pk_opfamily);
+#else
 		elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
 			 pathkey->pk_strategy, em->em_datatype, em->em_datatype,
 			 pathkey->pk_opfamily);
+#endif
 
 	/* Can't push down the sort if the operator is not shippable. */
 	if (!mysql_check_remote_pushability(oprid))
diff -pruN 2.9.2-2/mysql_fdw.control 2.9.3-1/mysql_fdw.control
--- 2.9.2-2/mysql_fdw.control	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/mysql_fdw.control	2025-09-30 05:28:24.000000000 +0000
@@ -4,7 +4,7 @@
 # 			Foreign-data wrapper for remote MySQL servers
 #
 # Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
-# Portions Copyright (c) 2004-2024, EnterpriseDB Corporation.
+# Portions Copyright (c) 2004-2025, EnterpriseDB Corporation.
 #
 # IDENTIFICATION
 # 			mysql_fdw.control
diff -pruN 2.9.2-2/mysql_fdw.h 2.9.3-1/mysql_fdw.h
--- 2.9.2-2/mysql_fdw.h	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/mysql_fdw.h	2025-09-30 05:28:24.000000000 +0000
@@ -4,7 +4,7 @@
  * 		Foreign-data wrapper for remote MySQL servers
  *
  * Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
- * Portions Copyright (c) 2004-2024, EnterpriseDB Corporation.
+ * Portions Copyright (c) 2004-2025, EnterpriseDB Corporation.
  *
  * IDENTIFICATION
  * 		mysql_fdw.h
@@ -73,11 +73,7 @@
 #define mysql_num_rows (*_mysql_num_rows)
 
 /* Macro for list API backporting. */
-#if PG_VERSION_NUM < 130000
-#define mysql_list_concat(l1, l2) list_concat(l1, list_copy(l2))
-#else
 #define mysql_list_concat(l1, l2) list_concat((l1), (l2))
-#endif
 
 /*
  * Options structure to store the MySQL
diff -pruN 2.9.2-2/mysql_pushability.c 2.9.3-1/mysql_pushability.c
--- 2.9.2-2/mysql_pushability.c	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/mysql_pushability.c	2025-09-30 05:28:24.000000000 +0000
@@ -3,7 +3,7 @@
  * mysql_pushability.c
  *		routines for FDW pushability
  *
- * Portions Copyright (c) 2022-2024, EnterpriseDB Corporation.
+ * Portions Copyright (c) 2022-2025, EnterpriseDB Corporation.
  *
  * IDENTIFICATION
  *		mysql_pushability.c
diff -pruN 2.9.2-2/mysql_pushability.h 2.9.3-1/mysql_pushability.h
--- 2.9.2-2/mysql_pushability.h	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/mysql_pushability.h	2025-09-30 05:28:24.000000000 +0000
@@ -3,7 +3,7 @@
  * mysql_pushability.h
  *		prototypes for mysql_pushability.c
  *
- * Portions Copyright (c) 2022-2024, EnterpriseDB Corporation.
+ * Portions Copyright (c) 2022-2025, EnterpriseDB Corporation.
  *
  * IDENTIFICATION
  *		mysql_pushability.h
diff -pruN 2.9.2-2/mysql_query.c 2.9.3-1/mysql_query.c
--- 2.9.2-2/mysql_query.c	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/mysql_query.c	2025-09-30 05:28:24.000000000 +0000
@@ -4,7 +4,7 @@
  * 		Type handling for remote MySQL servers
  *
  * Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
- * Portions Copyright (c) 2004-2024, EnterpriseDB Corporation.
+ * Portions Copyright (c) 2004-2025, EnterpriseDB Corporation.
  *
  * IDENTIFICATION
  * 		mysql_query.c
diff -pruN 2.9.2-2/mysql_query.h 2.9.3-1/mysql_query.h
--- 2.9.2-2/mysql_query.h	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/mysql_query.h	2025-09-30 05:28:24.000000000 +0000
@@ -4,7 +4,7 @@
  * 		Foreign-data wrapper for remote MySQL servers
  *
  * Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
- * Portions Copyright (c) 2004-2024, EnterpriseDB Corporation.
+ * Portions Copyright (c) 2004-2025, EnterpriseDB Corporation.
  *
  * IDENTIFICATION
  * 		mysql_query.h
diff -pruN 2.9.2-2/option.c 2.9.3-1/option.c
--- 2.9.2-2/option.c	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/option.c	2025-09-30 05:28:24.000000000 +0000
@@ -4,7 +4,7 @@
  * 		FDW option handling for mysql_fdw
  *
  * Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
- * Portions Copyright (c) 2004-2024, EnterpriseDB Corporation.
+ * Portions Copyright (c) 2004-2025, EnterpriseDB Corporation.
  *
  * IDENTIFICATION
  * 		option.c
diff -pruN 2.9.2-2/sql/join_pushdown.sql 2.9.3-1/sql/join_pushdown.sql
--- 2.9.2-2/sql/join_pushdown.sql	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/sql/join_pushdown.sql	2025-09-30 05:28:24.000000000 +0000
@@ -484,7 +484,6 @@ SELECT t1.c1,t2.c2,t3.c3
   WHERE t1.c1 % 2 =0 ORDER BY 1,2,3;
 
 -- With whole-row reference; partitionwise join does not apply
--- Table alias in foreign scan is different for v12, v11 and v10.
 EXPLAIN (VERBOSE, COSTS false)
 SELECT t1, t2, t1.c1
   FROM fprt1 t1 JOIN fprt2 t2 ON (t1.c1 = t2.c2)
@@ -504,7 +503,6 @@ SELECT t1.c1,t1.c2
   WHERE t1.c1 = t2.c2 AND t1.c2 = t2.c1) q WHERE t1.c1 % 2 = 0 ORDER BY 1,2;
 
 -- With PHVs, partitionwise join selected but no join pushdown
--- Table alias in foreign scan is different for v12, v11 and v10.
 EXPLAIN (VERBOSE, COSTS OFF)
 SELECT t1.c1, t1.phv, t2.c2, t2.phv
   FROM (SELECT 't1_phv' phv, * FROM fprt1 WHERE c1 % 2 = 0) t1 LEFT JOIN
diff -pruN 2.9.2-2/sql/select.sql 2.9.3-1/sql/select.sql
--- 2.9.2-2/sql/select.sql	2024-07-11 05:24:25.000000000 +0000
+++ 2.9.3-1/sql/select.sql	2025-09-30 05:28:24.000000000 +0000
@@ -678,6 +678,15 @@ SELECT c1, c2, c6, c8 FROM f_test_tbl1 e
   WHERE c6 = ANY (ARRAY[400, 2850.00000])
   ORDER BY c1;
 
+-- FDW-704: Self join queries with CTE should work correctly.
+WITH
+  tbl1 (a, b) AS MATERIALIZED (VALUES (1,1))
+SELECT
+  (SELECT t.a FROM f_mysql_test "t"
+    LEFT JOIN f_mysql_test "s" ON s.a = t.a
+    WHERE t.a = tbl1.a AND t.b = tbl1.b) "val"
+  FROM tbl1;
+
 -- Cleanup
 DROP TABLE l_test_tbl1;
 DROP TABLE l_test_tbl2;
