Subject: [pgjdbc/pgjdbc] issue #3743: Retrieving the result set from the multi-dimensional array will get an error in binary mode.

From: "sly461 (@sly461)" <[email-masked]>
Date: Mon, 28 Jul 2025 13:30:03 +0000

Please read https://stackoverflow.com/help/minimal-reproducible-example 

**Describe the issue**
I'm trying to use a three-dimensional array. However, I found that I can retrieve the result set normally in regular mode, but an error occurs **in binary mode**.

**Driver Version?**
42.7.7

**Java Version?**
java 21

**OS Version?**
Windows 10

**PostgreSQL Version?**
PostgreSQL 14.18

**To Reproduce**
Steps to reproduce the behaviour:
```
import java.sql.*;
import java.util.Properties;

public class TestMultiDimensionalArrayRetrieveResultSets {
  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");
    // forceBinary
    props.setProperty("prepareThreshold", "-1");
    Connection conn = DriverManager.getConnection(url, props);
    Statement stmt = conn.createStatement();
    stmt.execute("DROP TABLE IF EXISTS TestArray");
    stmt.execute("CREATE TABLE TestArray(intarr int[])");
    stmt.execute("INSERT INTO TestArray VALUES ('{{{0},{1}},{{2},{3}},{{4},{5}}}')");
    ResultSet rs = stmt.executeQuery("SELECT intarr FROM TestArray");
    rs.next();

    // {{{0},{1}},{{2},{3}},{{4},{5}}}
    Array arr = rs.getArray(1);
    ResultSet arrrs = arr.getResultSet();
    arrrs.next();
    // {{0},{1}}
    Array arr1 =  arrrs.getArray(2);
    ResultSet arr1rs = arr1.getResultSet();
    arr1rs.next();
    // {0}
    Array arr11 =  arr1rs.getArray(2);
    ResultSet arr11rs = arr11.getResultSet();
    arr11rs.next();
    System.out.println(arr11rs.getInt(2));
    arr11rs.next();
    arr11rs.close();
    arr1rs.next();
    // {1}
    Array arr12 =  arr1rs.getArray(2);
    ResultSet arr12rs = arr12.getResultSet();
    arr12rs.next();
    System.out.println(arr12rs.getInt(2));
    arr12rs.next();
    arr12rs.close();
    arr1rs.next();
    arr1rs.close();
  }
}
```

**Expected behaviour**
I expected to be able to retrieve the values in a three-dimensional array, but it actually got an error **in binary mode**:
```
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: arraycopy: last source index 33554459 out of bounds for byte[84]
    at java.base/java.lang.System.arraycopy(Native Method)
    at org.postgresql.jdbc.PgArray.storeValues(PgArray.java:275)
    at org.postgresql.jdbc.PgArray.readBinaryResultSet(PgArray.java:205)
    at org.postgresql.jdbc.PgArray.getResultSetImpl(PgArray.java:374)
    at org.postgresql.jdbc.PgArray.getResultSet(PgArray.java:336)
    at org.postgresql.test.TestMultiDimensionalArrayRetrieveResultSets.main(TestMultiDimensionalArrayRetrieveResultSets.java:25)
```

---

From: "sly461 (@sly461)" <[email-masked]>
Date: Tue, 29 Jul 2025 08:25:41 +0000

Hi @davecramer  @vlsi 
I've found the bug and fixed it in #3746 
Please review the code, thx :-)

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Tue, 29 Jul 2025 10:32:44 +0000

why 2 PR's ?

---

From: "sly461 (@sly461)" <[email-masked]>
Date: Tue, 29 Jul 2025 10:46:52 +0000

> why 2 PR's ?

@davecramer Sorry, I accidentally closed the first PR, please refer to the second PR #3746

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Tue, 29 Jul 2025 11:02:15 +0000

You can re-open them. No worries