Re: Fix overflow of nbatch

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Vaibhav Jain <jainva(at)google(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org, Madhukar <madhukarprasad(at)google(dot)com>, Sangeetha Seshadri <sangsesh(at)google(dot)com>
Subject: Re: Fix overflow of nbatch
Date: 2025-09-22 23:20:42
Message-ID: EB65FD9A-5A32-4841-A2FC-09F39D6C8532@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Sep 22, 2025, at 21:20, Vaibhav Jain <jainva(at)google(dot)com> wrote:
>
> Hi Everyone,
>
> With a1b4f28, to compute current_space, nbatch is being multiplied
> by BLCKSZ. nbatch is int and when multiplied with BLCKSZ, it can
> easily overflow the int limit.To keep the calculation safe for
> current_space, convert nbatch to size_t.
>
> Please find a patch for the same.
>
> Thanks,
> Vaibhav
> <0001-Fix-overflow-of-nbatch.patch>

I guess that because earlier in the function, nbatch is always clamped with:

nbatch = pg_nextpower2_32(Max(2, minbatch));
So, in practice, nbatch won’t grow to very big. But yes, if nbatch reaches to, say 1 million, it will overflow.

A simple program proves that changing nbatch to size_t will prevent from overflowing:

```
#include <stdio.h>

int main(){
size_t nbatch = 1000000; // 1 million
int BLCKSZ = 8192;
size_t result = 2 * nbatch * BLCKSZ;
printf("%zu\n", result); // will output 16384000000
return 0;
}
```

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David Rowley 2025-09-22 23:35:54 Re: Fix overflow of nbatch
Previous Message Chao Li 2025-09-22 22:43:50 Re: Trivial fix for comment of function table_tuple_lock