From 65807192ef8d4a325ca04480391e0a0719632249 Mon Sep 17 00:00:00 2001
From: Rafia Sabih <rafia.sabih@cybertec.at>
Date: Fri, 5 Jun 2026 12:54:11 +0200
Subject: [PATCH v2] Emit debug message for batch_size reduced

In case batch_size is reduced to keep the parameter within
limits of libpq, emit a debug message.
---
 contrib/postgres_fdw/expected/postgres_fdw.out | 15 +++++++++++++++
 contrib/postgres_fdw/postgres_fdw.c            |  7 +++++++
 contrib/postgres_fdw/sql/postgres_fdw.sql      | 15 +++++++++++++++
 3 files changed, 37 insertions(+)

diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out
index e90289e4ab1..6fe477dc8c3 100644
--- a/contrib/postgres_fdw/expected/postgres_fdw.out
+++ b/contrib/postgres_fdw/expected/postgres_fdw.out
@@ -11509,6 +11509,21 @@ DROP TRIGGER ftable_rowcount_trigger ON ltable;
 DROP TABLE ltable;
 DROP TABLE parent;
 DROP FUNCTION ftable_rowcount_trigf;
+-- Verify that DEBUG1 is emitted when batch_size is reduced to stay within
+-- the libpq 65535-parameter limit.  A single-column table has p_nums = 1,
+-- so the effective ceiling is 65535.  Setting batch_size to 65536 forces a
+-- one-step reduction.
+SET client_min_messages = DEBUG1;
+CREATE TABLE batch_table ( x int, y int );
+CREATE FOREIGN TABLE ftable ( x int, y int )
+	SERVER loopback
+	OPTIONS (table_name 'batch_table', batch_size '33000');
+INSERT INTO ftable VALUES (1);
+DEBUG:  postgres_fdw: batch_size reduced from 33000 to 32767 to stay within the libpq 65535-parameter limit
+RESET client_min_messages;
+-- Clean up
+DROP FOREIGN TABLE ftable;
+DROP TABLE batch_table;
 -- ===================================================================
 -- test asynchronous execution
 -- ===================================================================
diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index 0a589f8db74..633451973fb 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -2304,7 +2304,14 @@ postgresGetForeignModifyBatchSize(ResultRelInfo *resultRelInfo)
 	 * don't exceed this limit by using the maximum batch_size possible.
 	 */
 	if (fmstate && fmstate->p_nums > 0)
+	{
+		int configured = batch_size;
 		batch_size = Min(batch_size, PQ_QUERY_PARAM_MAX_LIMIT / fmstate->p_nums);
+		if (batch_size < configured)
+			elog(DEBUG1,"postgres_fdw: batch_size reduced from %d to %d "
+							"to stay within the libpq %d-parameter limit",
+							configured, batch_size, PQ_QUERY_PARAM_MAX_LIMIT);
+	}
 
 	return batch_size;
 }
diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql
index dfc58beb0d2..23159e8dc6e 100644
--- a/contrib/postgres_fdw/sql/postgres_fdw.sql
+++ b/contrib/postgres_fdw/sql/postgres_fdw.sql
@@ -3934,6 +3934,21 @@ DROP TABLE ltable;
 DROP TABLE parent;
 DROP FUNCTION ftable_rowcount_trigf;
 
+-- Verify that DEBUG1 is emitted when batch_size is reduced to stay within
+-- the libpq 65535-parameter limit.  A single-column table has p_nums = 1,
+-- so the effective ceiling is 65535.  Setting batch_size to 65536 forces a
+-- one-step reduction.
+SET client_min_messages = DEBUG1;
+CREATE TABLE batch_table ( x int, y int );
+CREATE FOREIGN TABLE ftable ( x int, y int )
+	SERVER loopback
+	OPTIONS (table_name 'batch_table', batch_size '33000');
+INSERT INTO ftable VALUES (1);
+RESET client_min_messages;
+-- Clean up
+DROP FOREIGN TABLE ftable;
+DROP TABLE batch_table;
+
 -- ===================================================================
 -- test asynchronous execution
 -- ===================================================================
-- 
2.39.5 (Apple Git-154)

