postgres_fdw: Emit message when batch_size is reduced

Поиск
Список
Период
Сортировка
Искать

postgres_fdw: Emit message when batch_size is reduced

От:
Rafia Sabih <rafia.pghackers@gmail.com>
Дата:
Вложения:
Hello hackers,

I noticed that when configured batch_size is reduced because of libpq limit, in postgresGetForeignModifyBatchSize, there is no message about it that gets to the user. I am thinking that to keep the transparency it makes sense to add a debug message at least, as per the attached patch.

What do you think?

--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Corey Huinker <corey.huinker@gmail.com>
Дата:
On Mon, Jun 1, 2026 at 7:55 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:
Hello hackers,

I noticed that when configured batch_size is reduced because of libpq limit, in postgresGetForeignModifyBatchSize, there is no message about it that gets to the user. I am thinking that to keep the transparency it makes sense to add a debug message at least, as per the attached patch.

What do you think?


I'm iffy on this one.

The desire for instrumentation is generally good, and if a database was having a problem where the tuned parameter wasn't having the desired effect then it's understandable to want to know that.

But I'm also not certain what different action someone would take if they knew that this situation was happening. Do you anticipate some sort of action on the user or DBA's part from this information beyond understanding that's why you're not getting the full batch size?

Thoughts on the code change itself:

- The new variable (configured) should be declared in the new if/then block, which is the narrowest scope possible.

- The ereport() uses the old style with errmsg inside parens, and that's no longer necessary. Existing ereport calls were left unchanged to avoid code churn, but new ones should be in the simpler style.

- This error is debuggy-enough that it might be an elog() rather than an ereport()

- I don't have good guidance on whether it should be DEBUG1 vs DEBUG2 or a higher number. Curious if we have that guidance documented anywhere.

- In the test case, choosing a batch_size > PQ_QUERY_PARAM_MAX_LIMIT seems a bit artificial, in that it's a value that can't work for any query with parameters, and someone might add sanity checks to batch_size in option.c, which would in turn invalidate the test case. A test case with a 2-column table should be able to get the same effect any batch_size greater than half of 65535, yes?

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Rafia Sabih <rafia.pghackers@gmail.com>
Дата:
Вложения:


On Fri, 5 Jun 2026 at 07:43, Corey Huinker <corey.huinker@gmail.com> wrote:
On Mon, Jun 1, 2026 at 7:55 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:
Hello hackers,

I noticed that when configured batch_size is reduced because of libpq limit, in postgresGetForeignModifyBatchSize, there is no message about it that gets to the user. I am thinking that to keep the transparency it makes sense to add a debug message at least, as per the attached patch.

What do you think?


I'm iffy on this one.

The desire for instrumentation is generally good, and if a database was having a problem where the tuned parameter wasn't having the desired effect then it's understandable to want to know that.

But I'm also not certain what different action someone would take if they knew that this situation was happening. Do you anticipate some sort of action on the user or DBA's part from this information beyond understanding that's why you're not getting the full batch size?

Hey Corey,

Thanks for your opinion. As I said, it feels important from the transparency point to know when and why the batch_size changed for a case.
Thoughts on the code change itself:

- The new variable (configured) should be declared in the new if/then block, which is the narrowest scope possible.

- The ereport() uses the old style with errmsg inside parens, and that's no longer necessary. Existing ereport calls were left unchanged to avoid code churn, but new ones should be in the simpler style.

- This error is debuggy-enough that it might be an elog() rather than an ereport()

- I don't have good guidance on whether it should be DEBUG1 vs DEBUG2 or a higher number. Curious if we have that guidance documented anywhere.

- In the test case, choosing a batch_size > PQ_QUERY_PARAM_MAX_LIMIT seems a bit artificial, in that it's a value that can't work for any query with parameters, and someone might add sanity checks to batch_size in option.c, which would in turn invalidate the test case. A test case with a 2-column table should be able to get the same effect any batch_size greater than half of 65535, yes?

I reworked the patch with your suggestions.
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Corey Huinker <corey.huinker@gmail.com>
Дата:

- In the test case, choosing a batch_size > PQ_QUERY_PARAM_MAX_LIMIT seems a bit artificial, in that it's a value that can't work for any query with parameters, and someone might add sanity checks to batch_size in option.c, which would in turn invalidate the test case. A test case with a 2-column table should be able to get the same effect any batch_size greater than half of 65535, yes?




Thinking some more on this part, I think it would be worthwhile to check on assignments to batch_size, and issue a NOTICE or WARNING that the set value exceeds PQ_QUERY_PARAM_MAX_LIMIT, but does not reject the change because this value could theoretically (but very unlikely) get smaller in the future and we don't want to mess up any pg_restores or pg_upgrades.
 

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Rafia Sabih <rafia.pghackers@gmail.com>
Дата:
Вложения:


On Fri, 5 Jun 2026 at 19:44, Corey Huinker <corey.huinker@gmail.com> wrote:


On Fri, Jun 5, 2026 at 7:02 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Fri, 5 Jun 2026 at 07:43, Corey Huinker <corey.huinker@gmail.com> wrote:
On Mon, Jun 1, 2026 at 7:55 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:
Hello hackers,

I noticed that when configured batch_size is reduced because of libpq limit, in postgresGetForeignModifyBatchSize, there is no message about it that gets to the user. I am thinking that to keep the transparency it makes sense to add a debug message at least, as per the attached patch.

What do you think?


I'm iffy on this one.

The desire for instrumentation is generally good, and if a database was having a problem where the tuned parameter wasn't having the desired effect then it's understandable to want to know that.

But I'm also not certain what different action someone would take if they knew that this situation was happening. Do you anticipate some sort of action on the user or DBA's part from this information beyond understanding that's why you're not getting the full batch size?

Hey Corey,

Thanks for your opinion. As I said, it feels important from the transparency point to know when and why the batch_size changed for a case.
Thoughts on the code change itself:

- The new variable (configured) should be declared in the new if/then block, which is the narrowest scope possible.

- The ereport() uses the old style with errmsg inside parens, and that's no longer necessary. Existing ereport calls were left unchanged to avoid code churn, but new ones should be in the simpler style.

- This error is debuggy-enough that it might be an elog() rather than an ereport()

- I don't have good guidance on whether it should be DEBUG1 vs DEBUG2 or a higher number. Curious if we have that guidance documented anywhere.

- In the test case, choosing a batch_size > PQ_QUERY_PARAM_MAX_LIMIT seems a bit artificial, in that it's a value that can't work for any query with parameters, and someone might add sanity checks to batch_size in option.c, which would in turn invalidate the test case. A test case with a 2-column table should be able to get the same effect any batch_size greater than half of 65535, yes?

I reworked the patch with your suggestions.
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

If you can, run pgindent on your changes before submitting a patch.

The lack of a tab after "int" and the lack of a blank line after the var declaration were easy to spot, but just in case I ran pgindent myself and got:

$ git diff
diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index 633451973fb..2e1bc47a737 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -2305,12 +2305,13 @@ postgresGetForeignModifyBatchSize(ResultRelInfo *resultRelInfo)
     */
    if (fmstate && fmstate->p_nums > 0)
    {
-       int configured = batch_size;
+       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);
+           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);
    }

The elog may get changed back into an ereport. If it does, it'll come under our wording guidelines scrutiny, but we'll worry about that later.

Next, the test case still does "INSERT INTO ftable VALUES(1)", which surprised me that it works even though ftable now has 2 columns. I think we should change that to

    INSERT INTO ftable(x) VALUES(1):

As this will remote the cognitive dissonance I experienced when looking at the test case, and it has the bonus of demonstrating that the parameters are generated in the SQL that postgres_fdw generates, not the SQL sent by the user.

As for the "should we even do this" question, the overhead seems pretty small and it will only come up in corner cases, and even then only for those who have signed up for the firehose of DEBUG1 messages, so I'm at least a +0.5 on this now.

Thanks for your inputs. Reworked patch is attached.
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Rafia Sabih <rafia.pghackers@gmail.com>
Дата:


On Fri, 5 Jun 2026 at 19:52, Corey Huinker <corey.huinker@gmail.com> wrote:

- In the test case, choosing a batch_size > PQ_QUERY_PARAM_MAX_LIMIT seems a bit artificial, in that it's a value that can't work for any query with parameters, and someone might add sanity checks to batch_size in option.c, which would in turn invalidate the test case. A test case with a 2-column table should be able to get the same effect any batch_size greater than half of 65535, yes?




Thinking some more on this part, I think it would be worthwhile to check on assignments to batch_size, and issue a NOTICE or WARNING that the set value exceeds PQ_QUERY_PARAM_MAX_LIMIT, but does not reject the change because this value could theoretically (but very unlikely) get smaller in the future and we don't want to mess up any pg_restores or pg_upgrades.
 

I am not able to understand this clearly, are you saying there should be a new check for this NOTICE/WARNING or should the DEBUG1 message (in the patch upthread) be a NOTICE/WARNING?
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Corey Huinker <corey.huinker@gmail.com>
Дата:
On Tue, Jun 9, 2026 at 7:38 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Fri, 5 Jun 2026 at 19:52, Corey Huinker <corey.huinker@gmail.com> wrote:

- In the test case, choosing a batch_size > PQ_QUERY_PARAM_MAX_LIMIT seems a bit artificial, in that it's a value that can't work for any query with parameters, and someone might add sanity checks to batch_size in option.c, which would in turn invalidate the test case. A test case with a 2-column table should be able to get the same effect any batch_size greater than half of 65535, yes?




Thinking some more on this part, I think it would be worthwhile to check on assignments to batch_size, and issue a NOTICE or WARNING that the set value exceeds PQ_QUERY_PARAM_MAX_LIMIT, but does not reject the change because this value could theoretically (but very unlikely) get smaller in the future and we don't want to mess up any pg_restores or pg_upgrades.
 

I am not able to understand this clearly, are you saying there should be a new check for this NOTICE/WARNING or should the DEBUG1 message (in the patch upthread) be a NOTICE/WARNING?
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

I'm saying that we may want a separate check when the batch_size option is SET (via CREATE/ALTER TABLE/SERVER) to test the value against PQ_QUERY_PARAM_MAX_LIMIT and issue a NOTICE/WARNING that the chosen value will always have to be set at or below $PQ_QUERY_PARAM_MAX_LIMIT - but still let them store the values as-is.



 

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Corey Huinker <corey.huinker@gmail.com>
Дата:

Thanks for your inputs. Reworked patch is attached.
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

You've addressed all my concerns, aside from the desire for the check on the set/update of the value. Do you have a commitfest entry? I didn't find one. 

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Rafia Sabih <rafia.pghackers@gmail.com>
Дата:


On Tue, 9 Jun 2026 at 22:22, Corey Huinker <corey.huinker@gmail.com> wrote:

Thanks for your inputs. Reworked patch is attached.
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

You've addressed all my concerns, aside from the desire for the check on the set/update of the value. Do you have a commitfest entry? I didn't find one. 
There is commitfest entry now --> https://commitfest.postgresql.org/patch/6873/

--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Corey Huinker <corey.huinker@gmail.com>
Дата:
On Wed, Jun 10, 2026 at 5:09 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 9 Jun 2026 at 22:22, Corey Huinker <corey.huinker@gmail.com> wrote:

Thanks for your inputs. Reworked patch is attached.
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

You've addressed all my concerns, aside from the desire for the check on the set/update of the value. Do you have a commitfest entry? I didn't find one. 
There is commitfest entry now --> https://commitfest.postgresql.org/patch/6873/



I've added myself as a reviewer. Did you want to try adding the check at time of the option being set? If not, I can make an attempt at that. 

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Rafia Sabih <rafia.pghackers@gmail.com>
Дата:
Вложения:


On Tue, 16 Jun 2026 at 22:20, Corey Huinker <corey.huinker@gmail.com> wrote:
On Wed, Jun 10, 2026 at 5:09 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 9 Jun 2026 at 22:22, Corey Huinker <corey.huinker@gmail.com> wrote:

Thanks for your inputs. Reworked patch is attached.
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

You've addressed all my concerns, aside from the desire for the check on the set/update of the value. Do you have a commitfest entry? I didn't find one. 
There is commitfest entry now --> https://commitfest.postgresql.org/patch/6873/



I've added myself as a reviewer. Did you want to try adding the check at time of the option being set? If not, I can make an attempt at that. 
Please find the attached file for the patch with the warning message at the time of batch_size option addition. Looking forward to your inputs.

--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Corey Huinker <corey.huinker@gmail.com>
Дата:
On Fri, Jun 19, 2026 at 8:38 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 16 Jun 2026 at 22:20, Corey Huinker <corey.huinker@gmail.com> wrote:
On Wed, Jun 10, 2026 at 5:09 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 9 Jun 2026 at 22:22, Corey Huinker <corey.huinker@gmail.com> wrote:

Thanks for your inputs. Reworked patch is attached.
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

You've addressed all my concerns, aside from the desire for the check on the set/update of the value. Do you have a commitfest entry? I didn't find one. 
There is commitfest entry now --> https://commitfest.postgresql.org/patch/6873/



I've added myself as a reviewer. Did you want to try adding the check at time of the option being set? If not, I can make an attempt at that. 
Please find the attached file for the patch with the warning message at the time of batch_size option addition. Looking forward to your inputs.

--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Applies clean, passes.

I think we need to tweak the elog() below:

+ if (batch_size > PQ_QUERY_PARAM_MAX_LIMIT)
+ elog(WARNING, "postgres_fdw: batch_size %d is at or above the libpq "
+ "%d-parameter limit; the effective per-batch ceiling is "
+ "limit / number_of_columns and may be lower",
+ batch_size, PQ_QUERY_PARAM_MAX_LIMIT);

I think this should be an ereport() because it's the sort of error we'd want the caller to see, and that means we need the message to conform the guidelines at https://www.postgresql.org/docs/current/error-style-guide.html, and I'm going to suggest this as a starting point:

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"));

I'd like to hear other people's opinions on what the proper conforming error message would be.

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Rafia Sabih <rafia.pghackers@gmail.com>
Дата:
Вложения:


On Mon, 22 Jun 2026 at 22:40, Corey Huinker <corey.huinker@gmail.com> wrote:
On Fri, Jun 19, 2026 at 8:38 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 16 Jun 2026 at 22:20, Corey Huinker <corey.huinker@gmail.com> wrote:
On Wed, Jun 10, 2026 at 5:09 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 9 Jun 2026 at 22:22, Corey Huinker <corey.huinker@gmail.com> wrote:

Thanks for your inputs. Reworked patch is attached.
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

You've addressed all my concerns, aside from the desire for the check on the set/update of the value. Do you have a commitfest entry? I didn't find one. 
There is commitfest entry now --> https://commitfest.postgresql.org/patch/6873/



I've added myself as a reviewer. Did you want to try adding the check at time of the option being set? If not, I can make an attempt at that. 
Please find the attached file for the patch with the warning message at the time of batch_size option addition. Looking forward to your inputs.

--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Applies clean, passes.

I think we need to tweak the elog() below:

+ if (batch_size > PQ_QUERY_PARAM_MAX_LIMIT)
+ elog(WARNING, "postgres_fdw: batch_size %d is at or above the libpq "
+ "%d-parameter limit; the effective per-batch ceiling is "
+ "limit / number_of_columns and may be lower",
+ batch_size, PQ_QUERY_PARAM_MAX_LIMIT);

I think this should be an ereport() because it's the sort of error we'd want the caller to see, and that means we need the message to conform the guidelines at https://www.postgresql.org/docs/current/error-style-guide.html, and I'm going to suggest this as a starting point:

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"));

Done. 
I'd like to hear other people's opinions on what the proper conforming error message would be.


--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Nurlan Tulemisov <nurlan.tulemisov@gmail.com>
Дата:

Hi,I reviewed v5, applied it to the current master, and ran the postgres_fdw regression tests. The patch applies cleanly and the tests pass.

Would it also make sense to emit a NOTICE or WARNING from postgres_fdw_validator() in option.c when batch_size is set to a value greater than PQ_QUERY_PARAM_MAX_LIMIT?


That would notify the user immediately during CREATE or ALTER, while still accepting the value and preserving the existing behavior.

The existing tests cover cases where the messages are emitted, but I think it may also be useful to cover the exact boundaries and the cases where no message should be emitted.

For a two-parameter foreign insert, batch_size = 32767 should not produce a DEBUG1 message, while batch_size = 32768 should be reduced to 32767. Similarly, for a single parameter, batch_size = 65535 should not produce a warning, while 65536 should.

Regards, 
Nurlan


сб, 11 июл. 2026 г. в 21:13, Rafia Sabih <rafia.pghackers@gmail.com>:


On Mon, 22 Jun 2026 at 22:40, Corey Huinker <corey.huinker@gmail.com> wrote:
On Fri, Jun 19, 2026 at 8:38 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 16 Jun 2026 at 22:20, Corey Huinker <corey.huinker@gmail.com> wrote:
On Wed, Jun 10, 2026 at 5:09 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 9 Jun 2026 at 22:22, Corey Huinker <corey.huinker@gmail.com> wrote:

Thanks for your inputs. Reworked patch is attached.
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

You've addressed all my concerns, aside from the desire for the check on the set/update of the value. Do you have a commitfest entry? I didn't find one. 
There is commitfest entry now --> https://commitfest.postgresql.org/patch/6873/



I've added myself as a reviewer. Did you want to try adding the check at time of the option being set? If not, I can make an attempt at that. 
Please find the attached file for the patch with the warning message at the time of batch_size option addition. Looking forward to your inputs.

--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Applies clean, passes.

I think we need to tweak the elog() below:

+ if (batch_size > PQ_QUERY_PARAM_MAX_LIMIT)
+ elog(WARNING, "postgres_fdw: batch_size %d is at or above the libpq "
+ "%d-parameter limit; the effective per-batch ceiling is "
+ "limit / number_of_columns and may be lower",
+ batch_size, PQ_QUERY_PARAM_MAX_LIMIT);

I think this should be an ereport() because it's the sort of error we'd want the caller to see, and that means we need the message to conform the guidelines at https://www.postgresql.org/docs/current/error-style-guide.html, and I'm going to suggest this as a starting point:

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"));

Done. 
I'd like to hear other people's opinions on what the proper conforming error message would be.


--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH


--
Regards,
Nurlan

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Rafia Sabih <rafia.pghackers@gmail.com>
Дата:
Вложения:


On Sat, 11 Jul 2026 at 23:56, Nurlan Tulemisov <nurlan.tulemisov@gmail.com> wrote:

Hi,I reviewed v5, applied it to the current master, and ran the postgres_fdw regression tests. The patch applies cleanly and the tests pass.

Thank you for the review. 

Would it also make sense to emit a NOTICE or WARNING from postgres_fdw_validator() in option.c when batch_size is set to a value greater than PQ_QUERY_PARAM_MAX_LIMIT?

Wouldn't that give duplicate warnings with the changes already in the patch...or you are suggesting to have it only in postgres_fdw_validator() ?
That would notify the user immediately during CREATE or ALTER, while still accepting the value and preserving the existing behavior.

The existing tests cover cases where the messages are emitted, but I think it may also be useful to cover the exact boundaries and the cases where no message should be emitted.

For a two-parameter foreign insert, batch_size = 32767 should not produce a DEBUG1 message, while batch_size = 32768 should be reduced to 32767. Similarly, for a single parameter, batch_size = 65535 should not produce a warning, while 65536 should.

Done.
Please find the updated patch attached. 

Regards, 
Nurlan


сб, 11 июл. 2026 г. в 21:13, Rafia Sabih <rafia.pghackers@gmail.com>:


On Mon, 22 Jun 2026 at 22:40, Corey Huinker <corey.huinker@gmail.com> wrote:
On Fri, Jun 19, 2026 at 8:38 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 16 Jun 2026 at 22:20, Corey Huinker <corey.huinker@gmail.com> wrote:
On Wed, Jun 10, 2026 at 5:09 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 9 Jun 2026 at 22:22, Corey Huinker <corey.huinker@gmail.com> wrote:

Thanks for your inputs. Reworked patch is attached.
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

You've addressed all my concerns, aside from the desire for the check on the set/update of the value. Do you have a commitfest entry? I didn't find one. 
There is commitfest entry now --> https://commitfest.postgresql.org/patch/6873/



I've added myself as a reviewer. Did you want to try adding the check at time of the option being set? If not, I can make an attempt at that. 
Please find the attached file for the patch with the warning message at the time of batch_size option addition. Looking forward to your inputs.

--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Applies clean, passes.

I think we need to tweak the elog() below:

+ if (batch_size > PQ_QUERY_PARAM_MAX_LIMIT)
+ elog(WARNING, "postgres_fdw: batch_size %d is at or above the libpq "
+ "%d-parameter limit; the effective per-batch ceiling is "
+ "limit / number_of_columns and may be lower",
+ batch_size, PQ_QUERY_PARAM_MAX_LIMIT);

I think this should be an ereport() because it's the sort of error we'd want the caller to see, and that means we need the message to conform the guidelines at https://www.postgresql.org/docs/current/error-style-guide.html, and I'm going to suggest this as a starting point:

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"));

Done. 
I'd like to hear other people's opinions on what the proper conforming error message would be.


--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH


--
Regards,
Nurlan


--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Nurlan Tulemisov <nurlan.tulemisov@gmail.com>
Дата:
 Hi Rafia,

Wouldn't that give duplicate warnings with the changes already in the patch...or you are suggesting to have it only in postgres_fdw_validator() ?
Yes, after thinking about it some more, I think it would be better to emit the WARNING only from postgres_fdw_validator() when the option is SET (CREATE/ALTER), while still allowing values greater than PQ_QUERY_PARAM_MAX_LIMIT to be stored.

This also seems consistent with Corey's earlier suggestion:
I'm saying that we may want a separate check when the batch_size option is
SET (via CREATE/ALTER TABLE/SERVER) to test the value against
PQ_QUERY_PARAM_MAX_LIMIT
and issue a NOTICE/WARNING that the chosen value will always have to be set
at or below $PQ_QUERY_PARAM_MAX_LIMIT - but still let them store the values
as-is.

The WARNING currently emitted during INSERT seems potentially noisy, since it may be repeated every time the foreign table is used.

Nit: since this table is used to verify that no warning is emitted:
+-- Verify that no WARNING is emitted when batch_size is within the
+-- libpq 65535-parameter limit.
+CREATE TABLE batch_warn_table ( x int );
Would batch_no_warn_table be a clearer name? The current name may be slightly confusing in this context.

On Thu, Jul 16, 2026 at 10:51 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Sat, 11 Jul 2026 at 23:56, Nurlan Tulemisov <nurlan.tulemisov@gmail.com> wrote:

Hi,I reviewed v5, applied it to the current master, and ran the postgres_fdw regression tests. The patch applies cleanly and the tests pass.

Thank you for the review. 

Would it also make sense to emit a NOTICE or WARNING from postgres_fdw_validator() in option.c when batch_size is set to a value greater than PQ_QUERY_PARAM_MAX_LIMIT?

Wouldn't that give duplicate warnings with the changes already in the patch...or you are suggesting to have it only in postgres_fdw_validator() ?
That would notify the user immediately during CREATE or ALTER, while still accepting the value and preserving the existing behavior.

The existing tests cover cases where the messages are emitted, but I think it may also be useful to cover the exact boundaries and the cases where no message should be emitted.

For a two-parameter foreign insert, batch_size = 32767 should not produce a DEBUG1 message, while batch_size = 32768 should be reduced to 32767. Similarly, for a single parameter, batch_size = 65535 should not produce a warning, while 65536 should.

Done.
Please find the updated patch attached. 

Regards, 
Nurlan


сб, 11 июл. 2026 г. в 21:13, Rafia Sabih <rafia.pghackers@gmail.com>:


On Mon, 22 Jun 2026 at 22:40, Corey Huinker <corey.huinker@gmail.com> wrote:
On Fri, Jun 19, 2026 at 8:38 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 16 Jun 2026 at 22:20, Corey Huinker <corey.huinker@gmail.com> wrote:
On Wed, Jun 10, 2026 at 5:09 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 9 Jun 2026 at 22:22, Corey Huinker <corey.huinker@gmail.com> wrote:

Thanks for your inputs. Reworked patch is attached.
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

You've addressed all my concerns, aside from the desire for the check on the set/update of the value. Do you have a commitfest entry? I didn't find one. 
There is commitfest entry now --> https://commitfest.postgresql.org/patch/6873/



I've added myself as a reviewer. Did you want to try adding the check at time of the option being set? If not, I can make an attempt at that. 
Please find the attached file for the patch with the warning message at the time of batch_size option addition. Looking forward to your inputs.

--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Applies clean, passes.

I think we need to tweak the elog() below:

+ if (batch_size > PQ_QUERY_PARAM_MAX_LIMIT)
+ elog(WARNING, "postgres_fdw: batch_size %d is at or above the libpq "
+ "%d-parameter limit; the effective per-batch ceiling is "
+ "limit / number_of_columns and may be lower",
+ batch_size, PQ_QUERY_PARAM_MAX_LIMIT);

I think this should be an ereport() because it's the sort of error we'd want the caller to see, and that means we need the message to conform the guidelines at https://www.postgresql.org/docs/current/error-style-guide.html, and I'm going to suggest this as a starting point:

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"));

Done. 
I'd like to hear other people's opinions on what the proper conforming error message would be.


--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH


--
Regards,
Nurlan


--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Rafia Sabih <rafia.pghackers@gmail.com>
Дата:
Вложения:


On Fri, 17 Jul 2026 at 18:39, Nurlan Tulemisov <nurlan.tulemisov@gmail.com> wrote:
 Hi Rafia,

Wouldn't that give duplicate warnings with the changes already in the patch...or you are suggesting to have it only in postgres_fdw_validator() ?
Yes, after thinking about it some more, I think it would be better to emit the WARNING only from postgres_fdw_validator() when the option is SET (CREATE/ALTER), while still allowing values greater than PQ_QUERY_PARAM_MAX_LIMIT to be stored.

This also seems consistent with Corey's earlier suggestion:
I'm saying that we may want a separate check when the batch_size option is
SET (via CREATE/ALTER TABLE/SERVER) to test the value against
PQ_QUERY_PARAM_MAX_LIMIT
and issue a NOTICE/WARNING that the chosen value will always have to be set
at or below $PQ_QUERY_PARAM_MAX_LIMIT - but still let them store the values
as-is.

The WARNING currently emitted during INSERT seems potentially noisy, since it may be repeated every time the foreign table is used.

Nit: since this table is used to verify that no warning is emitted:
+-- Verify that no WARNING is emitted when batch_size is within the
+-- libpq 65535-parameter limit.
+CREATE TABLE batch_warn_table ( x int );
Would batch_no_warn_table be a clearer name? The current name may be slightly confusing in this context.

Thank you for the review, I have made both the changes in the attached version. 
On Thu, Jul 16, 2026 at 10:51 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Sat, 11 Jul 2026 at 23:56, Nurlan Tulemisov <nurlan.tulemisov@gmail.com> wrote:

Hi,I reviewed v5, applied it to the current master, and ran the postgres_fdw regression tests. The patch applies cleanly and the tests pass.

Thank you for the review. 

Would it also make sense to emit a NOTICE or WARNING from postgres_fdw_validator() in option.c when batch_size is set to a value greater than PQ_QUERY_PARAM_MAX_LIMIT?

Wouldn't that give duplicate warnings with the changes already in the patch...or you are suggesting to have it only in postgres_fdw_validator() ?
That would notify the user immediately during CREATE or ALTER, while still accepting the value and preserving the existing behavior.

The existing tests cover cases where the messages are emitted, but I think it may also be useful to cover the exact boundaries and the cases where no message should be emitted.

For a two-parameter foreign insert, batch_size = 32767 should not produce a DEBUG1 message, while batch_size = 32768 should be reduced to 32767. Similarly, for a single parameter, batch_size = 65535 should not produce a warning, while 65536 should.

Done.
Please find the updated patch attached. 

Regards, 
Nurlan


сб, 11 июл. 2026 г. в 21:13, Rafia Sabih <rafia.pghackers@gmail.com>:


On Mon, 22 Jun 2026 at 22:40, Corey Huinker <corey.huinker@gmail.com> wrote:
On Fri, Jun 19, 2026 at 8:38 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 16 Jun 2026 at 22:20, Corey Huinker <corey.huinker@gmail.com> wrote:
On Wed, Jun 10, 2026 at 5:09 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 9 Jun 2026 at 22:22, Corey Huinker <corey.huinker@gmail.com> wrote:

Thanks for your inputs. Reworked patch is attached.
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

You've addressed all my concerns, aside from the desire for the check on the set/update of the value. Do you have a commitfest entry? I didn't find one. 
There is commitfest entry now --> https://commitfest.postgresql.org/patch/6873/



I've added myself as a reviewer. Did you want to try adding the check at time of the option being set? If not, I can make an attempt at that. 
Please find the attached file for the patch with the warning message at the time of batch_size option addition. Looking forward to your inputs.

--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Applies clean, passes.

I think we need to tweak the elog() below:

+ if (batch_size > PQ_QUERY_PARAM_MAX_LIMIT)
+ elog(WARNING, "postgres_fdw: batch_size %d is at or above the libpq "
+ "%d-parameter limit; the effective per-batch ceiling is "
+ "limit / number_of_columns and may be lower",
+ batch_size, PQ_QUERY_PARAM_MAX_LIMIT);

I think this should be an ereport() because it's the sort of error we'd want the caller to see, and that means we need the message to conform the guidelines at https://www.postgresql.org/docs/current/error-style-guide.html, and I'm going to suggest this as a starting point:

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"));

Done. 
I'd like to hear other people's opinions on what the proper conforming error message would be.


--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH


--
Regards,
Nurlan


--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH


--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Rafia Sabih <rafia.pghackers@gmail.com>
Дата:


On Tue, 21 Jul 2026 at 20:47, Nurlan Tulemisov <nurlan.tulemisov@gmail.com> wrote:
Hi Rafia,

I reviewed v7. I have no further concerns with the current version, and the patch looks ready to me.
Thanks for your time on this. Please mark it ready for committer then. 

On Mon, Jul 20, 2026 at 12:16 PM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Fri, 17 Jul 2026 at 18:39, Nurlan Tulemisov <nurlan.tulemisov@gmail.com> wrote:
 Hi Rafia,

Wouldn't that give duplicate warnings with the changes already in the patch...or you are suggesting to have it only in postgres_fdw_validator() ?
Yes, after thinking about it some more, I think it would be better to emit the WARNING only from postgres_fdw_validator() when the option is SET (CREATE/ALTER), while still allowing values greater than PQ_QUERY_PARAM_MAX_LIMIT to be stored.

This also seems consistent with Corey's earlier suggestion:
I'm saying that we may want a separate check when the batch_size option is
SET (via CREATE/ALTER TABLE/SERVER) to test the value against
PQ_QUERY_PARAM_MAX_LIMIT
and issue a NOTICE/WARNING that the chosen value will always have to be set
at or below $PQ_QUERY_PARAM_MAX_LIMIT - but still let them store the values
as-is.

The WARNING currently emitted during INSERT seems potentially noisy, since it may be repeated every time the foreign table is used.

Nit: since this table is used to verify that no warning is emitted:
+-- Verify that no WARNING is emitted when batch_size is within the
+-- libpq 65535-parameter limit.
+CREATE TABLE batch_warn_table ( x int );
Would batch_no_warn_table be a clearer name? The current name may be slightly confusing in this context.

Thank you for the review, I have made both the changes in the attached version. 
On Thu, Jul 16, 2026 at 10:51 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Sat, 11 Jul 2026 at 23:56, Nurlan Tulemisov <nurlan.tulemisov@gmail.com> wrote:

Hi,I reviewed v5, applied it to the current master, and ran the postgres_fdw regression tests. The patch applies cleanly and the tests pass.

Thank you for the review. 

Would it also make sense to emit a NOTICE or WARNING from postgres_fdw_validator() in option.c when batch_size is set to a value greater than PQ_QUERY_PARAM_MAX_LIMIT?

Wouldn't that give duplicate warnings with the changes already in the patch...or you are suggesting to have it only in postgres_fdw_validator() ?
That would notify the user immediately during CREATE or ALTER, while still accepting the value and preserving the existing behavior.

The existing tests cover cases where the messages are emitted, but I think it may also be useful to cover the exact boundaries and the cases where no message should be emitted.

For a two-parameter foreign insert, batch_size = 32767 should not produce a DEBUG1 message, while batch_size = 32768 should be reduced to 32767. Similarly, for a single parameter, batch_size = 65535 should not produce a warning, while 65536 should.

Done.
Please find the updated patch attached. 

Regards, 
Nurlan


сб, 11 июл. 2026 г. в 21:13, Rafia Sabih <rafia.pghackers@gmail.com>:


On Mon, 22 Jun 2026 at 22:40, Corey Huinker <corey.huinker@gmail.com> wrote:
On Fri, Jun 19, 2026 at 8:38 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 16 Jun 2026 at 22:20, Corey Huinker <corey.huinker@gmail.com> wrote:
On Wed, Jun 10, 2026 at 5:09 AM Rafia Sabih <rafia.pghackers@gmail.com> wrote:


On Tue, 9 Jun 2026 at 22:22, Corey Huinker <corey.huinker@gmail.com> wrote:

Thanks for your inputs. Reworked patch is attached.
--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

You've addressed all my concerns, aside from the desire for the check on the set/update of the value. Do you have a commitfest entry? I didn't find one. 
There is commitfest entry now --> https://commitfest.postgresql.org/patch/6873/



I've added myself as a reviewer. Did you want to try adding the check at time of the option being set? If not, I can make an attempt at that. 
Please find the attached file for the patch with the warning message at the time of batch_size option addition. Looking forward to your inputs.

--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Applies clean, passes.

I think we need to tweak the elog() below:

+ if (batch_size > PQ_QUERY_PARAM_MAX_LIMIT)
+ elog(WARNING, "postgres_fdw: batch_size %d is at or above the libpq "
+ "%d-parameter limit; the effective per-batch ceiling is "
+ "limit / number_of_columns and may be lower",
+ batch_size, PQ_QUERY_PARAM_MAX_LIMIT);

I think this should be an ereport() because it's the sort of error we'd want the caller to see, and that means we need the message to conform the guidelines at https://www.postgresql.org/docs/current/error-style-guide.html, and I'm going to suggest this as a starting point:

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"));

Done. 
I'd like to hear other people's opinions on what the proper conforming error message would be.


--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH


--
Regards,
Nurlan


--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH


--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH


--
Regards,
Nurlan


--
Regards,
Rafia Sabih
CYBERTEC PostgreSQL International GmbH

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Corey Huinker <corey.huinker@gmail.com>
Дата:

Overall, the changes look good to me, and I don't have any further comments.

Same here, I'd set it to Ready For Committer status if it weren't already set.

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Robert Haas <robertmhaas@gmail.com>
Дата:
On Tue, Aug 11, 2026 at 3:01 PM Corey Huinker  wrote:
>> Overall, the changes look good to me, and I don't have any further comments.
> Same here, I'd set it to Ready For Committer status if it weren't already set.

I have mixed feelings about this patch. I agree that the way that the
batch size is capped based on the supplied number of parameters could
be confusing to someone. I also think that the message that are
emitted here are not necessarily too verbose. I think it would be
unacceptable to emit a WARNING for every statement, but this only
emits a WARNING at object definition time and a DEBUG message at usage
time, which might be OK. All of that basically speaks in favor of the
patch.

On the flip side, this isn't the only thing in
postgresGetForeignModifyBatchSize that could trip somebody up. The
batch size can be taken from the table level option or the server
level option initially, and then it can be reset to 1 for various
different reasons, and then it can be capped by the limit at issue
here. So I feel like it's fair to wonder how many people would really
be helped by this patch. They'd have to have a confusion about the
batch size, and then they'd have to have the specific confusion that
corresponds to what the patch can tell you about, and then (unless
they have a value so large that it triggers the definition-time
warning) would have to think of setting log_min_messages to a low
enough level to let them see the DEBUG message. I'm not sure how many
people are going to think of that.

I think my instinct if I were having a batch_size problem would be to
check the documentation, which already does mention the fact that the
batch size can be clamped to avoid passing too many libpq parameters.
Interestingly, that's the only thing it mentions: the restrictions on
RETURNING, BEFORE/AFTER ROW INSERT TRIGGERS, and WITH CHECK OPTION
constraints are not documented. If I didn't find something talking
about this in the documentation, personally, my next move would be to
check the source code. I'd probably only find the proposed DEBUG
message after I'd already found the code that explains the behavior I
was seeing.

And all that assumes that there's really a problem here in the first
place. Large batch sizes are obviously nicer, but a batch size of 10
rather than 1 by definition eliminates 90% of the overhead that can
theoretically be eliminated. Is limiting the batch size to a value
that is likely to still be over 1000 really going to be a problem for
people? And if is, to Corey's point upthread, what are they going to
do about it? It's not like the libpq limit can feasibly be raised, so
you just have to live with the batch size that the system can manage.
You might want to understand what's happening, but the existing
documentation seems more useful for that than the proposed messages.

So overall I'm inclined to feel like this isn't really worth it.

-- 
Robert Haas
Databricks


Re: postgres_fdw: Emit message when batch_size is reduced

От:
Corey Huinker <corey.huinker@gmail.com>
Дата:
On Tue, Oct 13, 2026 at 10:25 AM Robert Haas <robertmhaas@gmail.com> wrote:
On Tue, Aug 11, 2026 at 3:01 PM Corey Huinker <corey.huinker@gmail.com> wrote:
>> Overall, the changes look good to me, and I don't have any further comments.
> Same here, I'd set it to Ready For Committer status if it weren't already set.

I have mixed feelings about this patch. I agree that the way that the
batch size is capped based on the supplied number of parameters could
be confusing to someone. I also think that the message that are
emitted here are not necessarily too verbose. I think it would be
unacceptable to emit a WARNING for every statement, but this only
emits a WARNING at object definition time and a DEBUG message at usage
time, which might be OK. All of that basically speaks in favor of the
patch.

On the flip side, this isn't the only thing in
postgresGetForeignModifyBatchSize that could trip somebody up. The
batch size can be taken from the table level option or the server
level option initially, and then it can be reset to 1 for various
different reasons, and then it can be capped by the limit at issue
here. So I feel like it's fair to wonder how many people would really
be helped by this patch. They'd have to have a confusion about the
batch size, and then they'd have to have the specific confusion that
corresponds to what the patch can tell you about, and then (unless
they have a value so large that it triggers the definition-time
warning) would have to think of setting log_min_messages to a low
enough level to let them see the DEBUG message. I'm not sure how many
people are going to think of that.

My interest in the DEBUG messages was giving a curious user an answer to the question "If I'm not getting the batch_size I asked for, what batch_size am I actually getting?", but that should infer-able from logs looking for "FETCH <number> FROM cursor_name", albeit with some additional effort to line up which FETCH commands correspond to which FDW queries, and cross referencing that against the fetch_size that they've set, so it may be that a curious user already has the instrumentation they need.

I think my instinct if I were having a batch_size problem would be to
check the documentation, which already does mention the fact that the
batch size can be clamped to avoid passing too many libpq parameters.

I agree that's the best course of action once a user knows that they have a batch_size problem, but I don't think the user affected by this situation knows that specific detail just yet. They probably know that their FDW queries have slow and/or stair-step stuttering performance (i.e. why does a result set of 1372 rows take so much longer than one with 1371? What's special about that number?), and they might also know that they made some configuration changes to cut down on network chatter for very large result sets, but the root cause still eludes them. When in the discovery phase of problem, it is reasonable to crank up the logging detail and see what's in there. But that same increase in logging detail will surface the FETCH x FROM y queries as well
 
And all that assumes that there's really a problem here in the first
place. Large batch sizes are obviously nicer, but a batch size of 10
rather than 1 by definition eliminates 90% of the overhead that can
theoretically be eliminated. Is limiting the batch size to a value
that is likely to still be over 1000 really going to be a problem for
people? And if is, to Corey's point upthread, what are they going to
do about it? It's not like the libpq limit can feasibly be raised, so
you just have to live with the batch size that the system can manage.
 
I see this as an argument for removing the DEBUG message but keeping the WARNING in option.c - as it immediately tells the well user "the value you specified cannot possibly achieve the thing you think it will do", potentially eliminating a fruitless or at least confusing testing/debug cycle, and possibly making them aware that there is an upper bound to fetch_size, thus initiating the voyage of discovery as outlined by Robert above, as well as providing a strong clue as to why the logs show a fetch_size of 652 despite the number 652 not showing up in the config files anywhere.

So overall I'm inclined to feel like this isn't really worth it.

If the reduction in scope I outlined in my previous paragraph isn't appealing, then I'm fine with us not proceeding on this one.

Re: postgres_fdw: Emit message when batch_size is reduced

От:
Robert Haas <robertmhaas@gmail.com>
Дата:
On Wed, Oct 14, 2026 at 12:22 PM Corey Huinker  wrote:
> My interest in the DEBUG messages was giving a curious user an answer to the question "If I'm not getting the batch_size I asked for, what batch_size am I actually getting?", but that should infer-able from logs looking for "FETCH  FROM cursor_name", albeit with some additional effort to line up which FETCH commands correspond to which FDW queries, and cross referencing that against the fetch_size that they've set, so it may be that a curious user already has the instrumentation they need.

Unless I'm confused, that's fetch_size, and this patch is about
batch_size. So, you're doing a bulk-insert to a foreign table, and the
question is about how many separate INSERT statements we're going to
have to issue in order to handle all the rows.

If the concern is about the difficulty of correlating local and remote
log lines, we could give postgres_fdw an option that causes the
commands it ships to the remote side to be logged locally at a level
of the user's choosing, and that could also involve logging fetch
sizes. I am not sure whether that would be valuable enough to justify
its carrying costs, but I can see the argument for it. You could watch
it issue transaction control commands and stuff, too, which could be
helpful for debugging. But all of that is a separate project from
this.

> I agree that's the best course of action once a user knows that they have a batch_size problem, but I don't think the user affected by this situation knows that specific detail just yet. They probably know that their FDW queries have slow and/or stair-step stuttering performance (i.e. why does a result set of 1372 rows take so much longer than one with 1371? What's special about that number?), and they might also know that they made some configuration changes to cut down on network chatter for very large result sets, but the root cause still eludes them. When in the discovery phase of problem, it is reasonable to crank up the logging detail and see what's in there. But that same increase in logging detail will surface the FETCH x FROM y queries as well

Do you think this is a real effect? I'm guessing that you're supposing
that the fetch size is 1372 here, so that with 1371 rows we fetch all
the rows in one go and see that there are no more and with 1372 rows
we have to issue a second fetch. But it's probably not the second
fetch that is the dominant cost here. In my mind, when you're sad
about the performance in these kinds of scenarios, you're more likely
to be sad that the plan had to use a cursor at all, because that may
for example have precluded the use of parallel query. Whether you have
to do one fetch or two probably matters relatively little by
comparison. That's not to say that the extra fetch is free. It isn't,
and you might be able to see that in some scenarios, like when the
cost of generating each row is very low. But I suspect it's not the
biggest of deals, either.

In the INSERT scenario, which is what I think the patch is targeting,
the issue would be one INSERT with 1372 rows vs. two inserts, one with
1371 rows and a second with 1 row. I don't know how much slower the
second one is, but I bet it isn't much slower. The extra statement has
some overhead, but so does inserting 1372 rows. You could test a case
with a single-row, single-column table with no indexes, so that the
extra network round trip is the biggest chunk of the cost possible,
but that's a pretty artificial scenario.

Honestly, there's room in my mind to question whether very large batch
sizes are even a good idea. Up near 64k parameters, the generated
INSERT INTO ... VALUES (...), (...), (...) ... statement will be
enormous. It has not been my experience that PostgreSQL users
routinely experience regret that they're only sending half a megabyte
of SQL in one shot; indeed, most of the regrets I've seen in this area
have come from query strings that are longer than they really ought to
be.

One more new discovery: apparently, you can already get the post-clamp
batch size by just using EXPLAIN (ANALYZE, VERBOSE) -- or if that's
too tricky in an IRL situation, you could set up auto_explain to do
the same thing for you automatically and dump the results to the logs.
In some ways, that's actually better than the DEBUG message this patch
proposes to add, because it takes into account all the ways that
postgresGetForeignModifyBatchSize can decide to adjust the numbers,
instead of only the last case. Regrettably, it appears that without
ANALYZE, you see the unclamped value, because
postgresGetForeignModifyBatchSize doesn't have an fmstate and skips
clamping the batch size for lack of knowledge. Potentially, that could
be fixed by factoring the p_nums calculation in create_foreign_modify
into a helper function that postgresGetForeignModifyBatchSize uses, or
by a larger refactoring that creates a partial fmstate even in pure
EXPLAIN mode, and just skips connection to the remote side. None of
that seems essential, but it could be done. Also regrettably, EXPLAIN
can only help with INSERT, not COPY FROM, so theoretically a DEBUG
message to cover that case could still have value, but it's aiming at
a very narrow target: the batch size is gigantic, you actually want to
be sending huge SQL statements, you're using COPY FROM, and you don't
have the ability to easily inspect the remote logs.

> I see this as an argument for removing the DEBUG message but keeping the WARNING in option.c - as it immediately tells the well user "the value you specified cannot possibly achieve the thing you think it will do", potentially eliminating a fruitless or at least confusing testing/debug cycle, and possibly making them aware that there is an upper bound to fetch_size, thus initiating the voyage of discovery as outlined by Robert above, as well as providing a strong clue as to why the logs show a fetch_size of 652 despite the number 652 not showing up in the config files anywhere.

Maybe, but keep in mind that (1) this is still about batch_size, not
fetch_size and (2) the two warnings aren't the same. The
postgres_fdw_validator WARNING is only going to fire if you set the
batch size above 64k, whereas the DEBUG message fires if the batch
size is more than 64k divided by the number of columns per row. They
only have the same threshold for a 1-column table, which is probably
not what you've got. Said differently, it's much more likely that you
have a ten-column table and a batch size of 7000 than a one-column
table and a batch size of 70000.

-- 
Robert Haas
Databricks


Re: postgres_fdw: Emit message when batch_size is reduced

От:
Corey Huinker <corey.huinker@gmail.com>
Дата:


On Thu, Oct 15, 2026 at 9:55 AM Robert Haas <robertmhaas@gmail.com> wrote:
On Wed, Oct 14, 2026 at 12:22 PM Corey Huinker <corey.huinker@gmail.com> wrote:
> My interest in the DEBUG messages was giving a curious user an answer to the question "If I'm not getting the batch_size I asked for, what batch_size am I actually getting?", but that should infer-able from logs looking for "FETCH <number> FROM cursor_name", albeit with some additional effort to line up which FETCH commands correspond to which FDW queries, and cross referencing that against the fetch_size that they've set, so it may be that a curious user already has the instrumentation they need.

Unless I'm confused, that's fetch_size, and this patch is about
batch_size. So, you're doing a bulk-insert to a foreign table, and the
question is about how many separate INSERT statements we're going to
have to issue in order to handle all the rows.

Yes, sorry for the confusion.
FAQ