From 52bc9be01d21a3774ee76d37446a4a43e70847f3 Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <postgres@jeltef.nl>
Date: Wed, 25 Nov 2026 22:28:08 +0100
Subject: [PATCH v4 4/8] postgres_fdw: Add test for costing pushed down joins

This adds a new test for the follow on commit. The reason it is separate
is so it's easy to see the plan change in the next commit. When actually
committing this should probably be squashed into a single commit.
---
 .../postgres_fdw/expected/postgres_fdw.out    | 22 +++++++++++++++++++
 contrib/postgres_fdw/sql/postgres_fdw.sql     |  8 +++++++
 2 files changed, 30 insertions(+)

diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out
index 886ce3a6e4c..5f35f42c86e 100644
--- a/contrib/postgres_fdw/expected/postgres_fdw.out
+++ b/contrib/postgres_fdw/expected/postgres_fdw.out
@@ -326,6 +326,28 @@ ORDER BY c2;
 
 DROP FUNCTION local_filter(int);
 DROP FUNCTION local_project(int);
+-- Without remote estimates we don't know what join strategy the foreign
+-- server is going to use for a pushed down join.  With an equality join
+-- clause it can use a hash join though, just like we could.  So pushing the
+-- join down should not look more expensive than fetching both sides and
+-- joining them locally.
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft1 t2 ON (t1.c1 = t2.c1);
+                       QUERY PLAN                        
+---------------------------------------------------------
+ Hash Join
+   Output: t1.c1, t2.c1
+   Hash Cond: (t1.c1 = t2.c1)
+   ->  Foreign Scan on public.ft1 t1
+         Output: t1.c1
+         Remote SQL: SELECT "C 1" FROM "S 1"."T 1"
+   ->  Hash
+         Output: t2.c1
+         ->  Foreign Scan on public.ft1 t2
+               Output: t2.c1
+               Remote SQL: SELECT "C 1" FROM "S 1"."T 1"
+(11 rows)
+
 -- ===================================================================
 -- test subscription
 -- ===================================================================
diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql
index 3055129808c..da6e79546be 100644
--- a/contrib/postgres_fdw/sql/postgres_fdw.sql
+++ b/contrib/postgres_fdw/sql/postgres_fdw.sql
@@ -284,6 +284,14 @@ ORDER BY c2;
 DROP FUNCTION local_filter(int);
 DROP FUNCTION local_project(int);
 
+-- Without remote estimates we don't know what join strategy the foreign
+-- server is going to use for a pushed down join.  With an equality join
+-- clause it can use a hash join though, just like we could.  So pushing the
+-- join down should not look more expensive than fetching both sides and
+-- joining them locally.
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft1 t2 ON (t1.c1 = t2.c1);
+
 -- ===================================================================
 -- test subscription
 -- ===================================================================
-- 
2.56.1

