Subject: [pgjdbc/pgjdbc] issue #3726: BatchInsert + generatedKeys will cause insert block in DB

From: "wufengke (@wufengke)" <[email-masked]>
Date: Sun, 13 Jul 2025 14:32:13 +0000

version: 42.7.4

When I use the executeBatch() for a bulk(infact it's small) data insert and want return the generatedKeys, I find when the batch size is more than 50, it will sometimes block in the DB and does not return any more. I can not find the root cause. 
This issue still exists in the newest version.

The key point I think it related to the retrunGeneratedKeys, more keys returned and more chance it will happen.

If it is a known issure, what is the best practise to workaround if I want to get the  serial generated keys when insert data? Insert  without result key and find them by  a batch query after insert is a best way?

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Sun, 13 Jul 2025 15:44:00 +0000

I suspect this is a duplicate of https://github.com/pgjdbc/pgjdbc/issues/194

---

From: "wufengke (@wufengke)" <[email-masked]>
Date: Sun, 13 Jul 2025 16:04:27 +0000

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Sun, 13 Jul 2025 17:24:09 +0000

Use smaller batches

---

From: "wufengke (@wufengke)" <[email-masked]>
Date: Mon, 14 Jul 2025 13:41:47 +0000

Thanks. But I have another question, does the batchExecute() realy in a batch way? Because I find it's performance is almost the same as application layer loop. I have 5 thousand data.

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Mon, 14 Jul 2025 13:49:06 +0000

There is no concept of executeBatch in postgres. You can try [`rewriteBatchInserts`](https://github.com/pgjdbc/pgjdbc/blob/7b1984b25590171906c96687bc99d7aeaa7a8000/README.md?plain=1#L145) to see if that will help

---

From: "bilalshehata (@bilalshehata)" <[email-masked]>
Date: Wed, 08 Apr 2026 04:47:55 +0000

Hi @davecramer , https://github.com/pgjdbc/pgjdbc/pull/4013 what do you think of this as a potential fix?

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Wed, 08 Apr 2026 09:09:08 +0000

@bilalshehata certainly can't hurt. Let's see if it passes all the tests