Re: Fix overflow of nbatch

Chao Li <li.evan.chao@gmail.com>

From: Chao Li <li.evan.chao@gmail.com>
To: Vaibhav Jain <jainva@google.com>
Cc: pgsql-hackers@postgresql.org, Madhukar <madhukarprasad@google.com>, Sangeetha Seshadri <sangsesh@google.com>
Date: 2025-09-22T23:20:42Z
Lists: pgsql-hackers

> On Sep 22, 2025, at 21:20, Vaibhav Jain <jainva@google.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/




Commits

  1. Fix hashjoin memory balancing logic