pg.ddx.io  GitHub: pgjdbc/pgjdbc (issues + PRs as email threads)  
help / color / mirror / Atom feed
[pgjdbc/pgjdbc] issue #4530: `getString()` / `getObject()` return `null` for non-null `json`, `jsonb`, `inet`, … values with `binaryTransferEnable`; `StackOverflowError` for `xml`
2+ messages / 1 participants
[nested] [flat]

* [pgjdbc/pgjdbc] issue #4530: `getString()` / `getObject()` return `null` for non-null `json`, `jsonb`, `inet`, … values with `binaryTransferEnable`; `StackOverflowError` for `xml`
@ 2026-11-13 14:52  "lukaseder (@lukaseder)" <noreply+lukaseder@github.com>
  0 siblings, 0 replies; 2+ messages in thread

From: lukaseder (@lukaseder) @ 2026-11-13 14:52 UTC (permalink / raw)
  To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>

Related issue:

- https://github.com/pgjdbc/pgjdbc/issues/1635

**Describe the issue**

When binary transfer is enabled through `binaryTransferEnable` for a type that pgjdbc has no binary decoder for, `ResultSet.getString()` and `ResultSet.getObject()` silently return `null` for non-null values. `wasNull()` returns `false`, and `getBytes()` returns the data. For `xml`, both `getString()` and `getObject()` throw a `StackOverflowError`.

Types I tested:

| Type | `getString()` / `getObject()` with `binaryTransferEnable=<type>` |
|---|---|
| `json`, `jsonb` | `null` (`getObject()` is a `PGobject` whose value is `null`) |
| `inet`, `macaddr`, `tsvector` | `null` (same) |
| `xml` | `StackOverflowError` |

Presumably any type listed in `binaryTransferEnable` that has no `PGBinaryObject` implementation or dedicated decoder is affected. Without `binaryTransferEnable`, everything works.

Silently returning `null` for a non-null value is the main problem: applications can't tell it apart from SQL `NULL`.

**Driver Version?** `master` at 9c6eeaab40b429bd5bc8030973ba49280a50f41c

**Java Version?** OpenJDK 21.0.12

**OS Version?** Linux

**PostgreSQL Version?** 16.15

**To Reproduce**

Single-file reproducer, run with `java -cp postgresql-42.x.jar BinaryTransferNullRepro.java <url> <user> <password>`:

```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class BinaryTransferNullRepro {

    public static void main(String[] args) throws Exception {
        String url = args.length > 0 ? args[0] : "jdbc:postgresql://localhost:5432/postgres";
        String user = args.length > 1 ? args[1] : "postgres";
        String password = args.length > 2 ? args[2] : "";

        String[][] cases = {
            { "jsonb",    "'{\"a\": 1}'" },
            { "json",     "'{\"a\": 1}'" },
            { "inet",     "'127.0.0.1'" },
            { "macaddr",  "'08:00:2b:01:02:03'" },
            { "tsvector", "'a b'" },
            { "xml",      "'<a/>'" },
        };

        for (boolean binary : new boolean[] { false, true }) {
            System.out.println(binary ? "With binaryTransferEnable=<type>:" : "Default (text transfer):");

            for (String[] c : cases)
                run(url, user, password, c[0], c[1], binary);

            System.out.println();
        }
    }

    static void run(String url, String user, String password, String type, String literal, boolean binary) throws Exception {

        // prepareThreshold=-1 forces a server-prepared statement on the first
        // execution, so binary transfer applies immediately
        String fullUrl = url + (url.contains("?") ? "&" : "?")
            + "prepareThreshold=-1" + (binary ? "&binaryTransferEnable=" + type : "");

        try (Connection conn = DriverManager.getConnection(fullUrl, user, password);
             PreparedStatement ps = conn.prepareStatement("select cast(" + literal + " as " + type + ")");
             ResultSet rs = ps.executeQuery()) {

            rs.next();
            byte[] bytes = rs.getBytes(1);
            String string;

            try {
                string = rs.getString(1);
            }
            catch (StackOverflowError e) {
                string = "<StackOverflowError>";
            }

            Object object;
            try {
                object = rs.getObject(1);
            }
            catch (StackOverflowError e) {
                object = "<StackOverflowError>";
            }

            System.out.printf("  %-9s wasNull=%-5s getBytes().length=%-3d getString()=%-22s getObject()=%s%n",
                type, rs.wasNull(), bytes.length, string, object);
        }
    }
}
```

Output:

```
Default (text transfer):
  jsonb     wasNull=false getBytes().length=8   getString()={"a": 1}               getObject()={"a": 1}
  json      wasNull=false getBytes().length=8   getString()={"a": 1}               getObject()={"a": 1}
  inet      wasNull=false getBytes().length=9   getString()=127.0.0.1              getObject()=127.0.0.1
  macaddr   wasNull=false getBytes().length=17  getString()=08:00:2b:01:02:03      getObject()=08:00:2b:01:02:03
  tsvector  wasNull=false getBytes().length=7   getString()='a' 'b'                getObject()='a' 'b'
  xml       wasNull=false getBytes().length=4   getString()=<a/>                   getObject()=org.postgresql.jdbc.PgSQLXML@49912c99

With binaryTransferEnable=<type>:
  jsonb     wasNull=false getBytes().length=9   getString()=null                   getObject()=null
  json      wasNull=false getBytes().length=8   getString()=null                   getObject()=null
  inet      wasNull=false getBytes().length=8   getString()=null                   getObject()=null
  macaddr   wasNull=false getBytes().length=6   getString()=null                   getObject()=null
  tsvector  wasNull=false getBytes().length=12  getString()=null                   getObject()=null
  xml       wasNull=false getBytes().length=4   getString()=<StackOverflowError>   getObject()=<StackOverflowError>
```

(The `getObject()` values print as `null` because `PGobject.toString()` returns its `null` value; the `PGobject` itself is not `null`.)

**Expected behaviour**

`getString()` and `getObject()` return the same values as with text transfer. If pgjdbc can't decode a type's binary format, it should either not request binary transfer for that type, or throw an exception, but never return `null` for a non-null value.

**Analysis**

`null` case:

1. [`PgResultSet.getString(int)`](https://github.com/pgjdbc/pgjdbc/blob/9c6eeaab40b429bd5bc8030973ba49280a50f41c/pgjdbc/src/main/java/...): for binary non-`VARCHAR` columns, it calls `internalGetObject()`, which returns `null` for these types. It then falls back to `getObject(columnIndex).toString()`.
2. [`PgResultSet.getObject(int)`](https://github.com/pgjdbc/pgjdbc/blob/9c6eeaab40b429bd5bc8030973ba49280a50f41c/pgjdbc/src/main/java/...): for binary columns, it calls `connection.getObject(type, null, value)`, passing the bytes but no string value.
3. [`PgConnection.getObject(String, String, byte[])`](https://github.com/pgjdbc/pgjdbc/blob/9c6eeaab40b429bd5bc8030973ba49280a50f41c/pgjdbc/src/main/java/...): unless the `PGobject` class is a `PGBinaryObject`, it calls `obj.setValue(value)` with that `null`, and the bytes are discarded. `PGobject.toString()` then returns `null`.

`xml` case: `getString()` → `internalGetObject()` (`Types.SQLXML`) → [`getSQLXML()`](https://github.com/pgjdbc/pgjdbc/blob/9c6eeaab40b429bd5bc8030973ba49280a50f41c/pgjdbc/src/main/java/...) → `getString()` → … recurses forever. This looks similar to #1635, which was the same kind of recursion for binary `NUMERIC`.

**Possible fixes**

- `json` and `jsonb` are easy to decode: the binary format of `json` is its text, and `jsonb`'s binary format is a version byte `0x01` followed by the text. Both are encoded in `client_encoding`, like text transfer.
- For types without a binary decoder (`inet`, `macaddr`, `tsvector`, user-defined types, …), `PgConnection.getObject()` should not silently drop `byteValue`. Either reject or ignore those OIDs in `binaryTransferEnable` (with a warning), or throw an exception, instead of returning a `PGobject` with a `null` value.
- `xml`: `getSQLXML()` should not go through `getString()` for binary values.

**Context**

I found this while working on reading `json` / `jsonb` via `getBytes()` in jOOQ, to avoid an intermediate `String` (https://github.com/jOOQ/jOOQ/issues/20504). That works with both text and binary transfer, but `getString()` breaks as soon as binary transfer is enabled for `jsonb`.


^ permalink  raw  [nested|flat] 2+ messages in thread

* Re: [pgjdbc/pgjdbc] issue #4530: `getString()` / `getObject()` return `null` for non-null `json`, `jsonb`, `inet`, … values with `binaryTransferEnable`; `StackOverflowError` for `xml`
@ 2026-11-13 15:04  "lukaseder (@lukaseder)" <noreply+lukaseder@github.com>
  0 siblings, 0 replies; 2+ messages in thread

From: lukaseder (@lukaseder) @ 2026-11-13 15:04 UTC (permalink / raw)
  To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>

Here's a PR with a fix generated by Claude AI:

- https://github.com/pgjdbc/pgjdbc/pull/4531

^ permalink  raw  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2026-11-13 15:04 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-13 14:52 [pgjdbc/pgjdbc] issue #4530: `getString()` / `getObject()` return `null` for non-null `json`, `jsonb`, `inet`, … values with `binaryTransferEnable`; `StackOverflowError` for `xml` "lukaseder (@lukaseder)" <noreply+lukaseder@github.com>
2026-11-13 15:04 Re: [pgjdbc/pgjdbc] issue #4530: `getString()` / `getObject()` return `null` for non-null `json`, `jsonb`, `inet`, … values with `binaryTransferEnable`; `StackOverflowError` for `xml` "lukaseder (@lukaseder)" <noreply+lukaseder@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox