From 8b8623a8a61b8b61360218eff83b9288e747fc97 Mon Sep 17 00:00:00 2001
From: Rafia Sabih <rafia.sabih@cybertec.at>
Date: Mon, 1 Jun 2026 13:49:09 +0200
Subject: [PATCH] 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            | 10 +++++++++-
 contrib/postgres_fdw/sql/postgres_fdw.sql      | 15 +++++++++++++++
 3 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out
index e90289e4ab1..e935c6dd547 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 );
+CREATE FOREIGN TABLE ftable ( x int )
+	SERVER loopback
+	OPTIONS (table_name 'batch_table', batch_size '65536');
+INSERT INTO ftable VALUES (1);
+DEBUG:  postgres_fdw: batch_size reduced from 65536 to 65535 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..b7934f036f6 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -2249,7 +2249,7 @@ postgresExecForeignBatchInsert(EState *estate,
 static int
 postgresGetForeignModifyBatchSize(ResultRelInfo *resultRelInfo)
 {
-	int			batch_size;
+	int			batch_size, configured;
 	PgFdwModifyState *fmstate = (PgFdwModifyState *) resultRelInfo->ri_FdwState;
 
 	/* should be called only once */
@@ -2304,7 +2304,15 @@ postgresGetForeignModifyBatchSize(ResultRelInfo *resultRelInfo)
 	 * don't exceed this limit by using the maximum batch_size possible.
 	 */
 	if (fmstate && fmstate->p_nums > 0)
+	{
+		configured = batch_size;
 		batch_size = Min(batch_size, PQ_QUERY_PARAM_MAX_LIMIT / fmstate->p_nums);
+		if (batch_size < configured)
+			ereport(DEBUG1,
+					(errmsg("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..92cf36fbe18 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 );
+CREATE FOREIGN TABLE ftable ( x int )
+	SERVER loopback
+	OPTIONS (table_name 'batch_table', batch_size '65536');
+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)

