Message-ID: From: "leandrokemp (@leandrokemp)" To: "pgjdbc/pgjdbc" Date: Thu, 18 Sep 2025 21:28:15 +0000 Subject: [pgjdbc/pgjdbc] issue #3811: PreparedStatement batch performance issue in a loop on v42.7.6 or later List-Id: X-GitHub-Author-Id: 41006452 X-GitHub-Author-Login: leandrokemp X-GitHub-Issue: 3811 X-GitHub-Repo: pgjdbc/pgjdbc X-GitHub-State: closed X-GitHub-Type: issue X-GitHub-Url: https://github.com/pgjdbc/pgjdbc/issues/3811 Content-Type: text/plain; charset=utf-8 **Describe the issue** The app I work on run a loop of inserts and add those inserts to a PreparedStatement batch. There are more than 100 thousand inserts, which are added to the batch in the loop. On postgresql 42.7.5 and earlier versions the loop takes a few seconds to add all inserts to the ps batch. From version 42.7.6, the loop hangs and never finishes. I've tested it for more than 30 minutes and it didn't finish. Nothing has changed in my app, except the postgresql library upgrade. **Driver Version?** 42.7.6 **Java Version?** 21 **OS Version?** MacOS 26 and AWS Lambda container **PostgreSQL Version?** 15 **To Reproduce** ``` import java.sql.*; import java.util.List; import java.util.Properties; import java.util.stream.Collectors; import java.util.stream.IntStream; public class TestBatchLoop { public static void main(String []args) throws Exception { String url = "jdbc:postgresql://localhost:5432/test"; Properties props = new Properties(); props.setProperty("user", "test"); props.setProperty("password", "test"); // generate a list with 100 thousand values List values = IntStream.rangeClosed(1, 100_000) .mapToObj(i -> "value" + i) .collect(Collectors.toList()); try ( Connection conn = DriverManager.getConnection(url, props) ){ try ( PreparedStatement ps = conn.prepareStatement("insert into table(column) values(?)") ) { // on version 42.7.5 and earlier this finishes in a few seconds // on version 42.7.6 and later this didn't finish until I stopped it after about 30 minutes for(String value : values) { ps.setString(1, value); String stm = ps.toString(); ps.addBatch(); } ps.executeBatch(); conn.commit(); } } } } ``` **Expected behaviour** It should complete the loop in a few seconds **Logs** There are no logs. It just hangs in the loop