From 562d82a1ff758aebf9f4d9fc4b40d88a9a007104 Mon Sep 17 00:00:00 2001 From: Rafia Sabih Date: Thu, 16 Jul 2026 13:17:05 +0530 Subject: [PATCH v6] 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. Also emit a warning message when batch_size is set to a value over the limit. --- .../postgres_fdw/expected/postgres_fdw.out | 44 +++++++++++++++++++ contrib/postgres_fdw/postgres_fdw.c | 12 +++++ contrib/postgres_fdw/sql/postgres_fdw.sql | 44 +++++++++++++++++++ 3 files changed, 100 insertions(+) diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out index 991501245f9..7e2ee3043b7 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -11684,6 +11684,50 @@ 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. +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(x) VALUES (1); +DEBUG: postgres_fdw: batch_size reduced from 33000 to 32767 to stay within the libpq 65535-parameter limit +-- Clean up +DROP FOREIGN TABLE ftable; +DROP TABLE batch_table; +-- Verify that a WARNING is emitted when batch_size is set to or above the +-- libpq 65535-parameter limit. +CREATE TABLE batch_warn_table ( x int ); +CREATE FOREIGN TABLE ftable_warn ( x int ) + SERVER loopback + OPTIONS (table_name 'batch_warn_table', batch_size '65536'); +INSERT INTO ftable_warn VALUES (1); +WARNING: batch_size of 65536 exceeds protocol limit of 65535 +DETAIL: The batch_size for a query will be reduced to protocol limit divided by the number of columns in the query. +DEBUG: postgres_fdw: batch_size reduced from 65536 to 65535 to stay within the libpq 65535-parameter limit +-- Clean up +DROP FOREIGN TABLE ftable_warn; +DROP TABLE batch_warn_table; +-- Verify that no WARNING is emitted when batch_size is within the +-- libpq 65535-parameter limit. +CREATE TABLE batch_warn_table ( x int ); +CREATE FOREIGN TABLE ftable_no_warn ( x int ) + SERVER loopback + OPTIONS (table_name 'batch_warn_table', batch_size '65535'); +INSERT INTO ftable_no_warn VALUES (1); +-- Clean up +DROP FOREIGN TABLE ftable_no_warn; +DROP TABLE batch_warn_table; +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 '32767'); +INSERT INTO ftable(x) VALUES (1); +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 70de942de12..2a99510f548 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -2182,7 +2182,15 @@ 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; } @@ -8760,6 +8768,10 @@ get_batch_size_option(Relation rel) if (strcmp(def->defname, "batch_size") == 0) { (void) parse_int(defGetString(def), &batch_size, 0, NULL); + if (batch_size > PQ_QUERY_PARAM_MAX_LIMIT) + ereport(WARNING, + errmsg("%s of %d exceeds protocol limit of %d", "batch_size", batch_size, PQ_QUERY_PARAM_MAX_LIMIT), + errdetail("The %s for a query will be reduced to protocol limit divided by the number of columns in the query.", "batch_size")); break; } } diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql index 454deb03d69..ba12ac4dca0 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -4027,6 +4027,50 @@ 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. +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(x) VALUES (1); + +-- Clean up +DROP FOREIGN TABLE ftable; +DROP TABLE batch_table; + +-- Verify that a WARNING is emitted when batch_size is set to or above the +-- libpq 65535-parameter limit. +CREATE TABLE batch_warn_table ( x int ); +CREATE FOREIGN TABLE ftable_warn ( x int ) + SERVER loopback + OPTIONS (table_name 'batch_warn_table', batch_size '65536'); +INSERT INTO ftable_warn VALUES (1); +-- Clean up +DROP FOREIGN TABLE ftable_warn; +DROP TABLE batch_warn_table; + +-- Verify that no WARNING is emitted when batch_size is within the +-- libpq 65535-parameter limit. +CREATE TABLE batch_warn_table ( x int ); +CREATE FOREIGN TABLE ftable_no_warn ( x int ) + SERVER loopback + OPTIONS (table_name 'batch_warn_table', batch_size '65535'); +INSERT INTO ftable_no_warn VALUES (1); +-- Clean up +DROP FOREIGN TABLE ftable_no_warn; +DROP TABLE batch_warn_table; + +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 '32767'); +INSERT INTO ftable(x) 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)