pg.ddx.io GitHub: pgjdbc/pgjdbc (issues + PRs as email threads)
help / color / mirror / Atom feed[pgjdbc/pgjdbc] issue #4328: Logical replication breaks on idle slot since 42.7.11 -> Tried to write to an inactive copy operation
6+ messages / 3 participants
[nested] [flat]
* [pgjdbc/pgjdbc] issue #4328: Logical replication breaks on idle slot since 42.7.11 -> Tried to write to an inactive copy operation
@ 2026-07-29 10:08 "reynoldregan (@reynoldregan)" <noreply+reynoldregan@github.com>
0 siblings, 0 replies; 6+ messages in thread
From: reynoldregan (@reynoldregan) @ 2026-07-29 10:08 UTC (permalink / raw)
To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>
# Logical replication breaks on idle slot since 42.7.11: "Tried to write to an inactive copy operation"
## Summary
Since **42.7.11**, a logical replication consumer that uses the blocking
`PGReplicationStream.read()` API fails as soon as the slot is idle for one
status-interval, with:
```
org.postgresql.util.PSQLException: Tried to write to an inactive copy operation
```
The same code works correctly on **42.7.8 and earlier**. The regression is
**still present in 42.7.13**.
The root cause is a change in `QueryExecutorImpl.readFromCopy(...)` /
`writeToCopy(...)` that now calls `unlock(op)` inside the `IOException` catch
block. During logical replication the driver deliberately uses a socket read
timeout (`SocketTimeoutException`, an `IOException`) as its normal wake-up
mechanism to send periodic standby status updates. That routine timeout now
**deactivates the COPY**, and the very next status update fails.
## Environment
| | |
|---|---|
| pgjdbc version | **42.7.11** (also reproduced on 42.7.13; last working: 42.7.8) |
| JDK | 21 (also seen on 17) |
| PostgreSQL server | 14+ with `wal_level = logical` |
| Output plugin | `wal2json` (plugin-agnostic — same issue applies to any logical plugin) |
| OS | macOS / Linux |
## Steps to reproduce
1. Create a logical replication slot:
```sql
SELECT pg_create_logical_replication_slot('test_slot', 'wal2json');
```
2. Consume it with the blocking replication API, configuring a finite socket
read timeout (either a connection `socketTimeout`, or simply a
`withStatusInterval`, which the driver folds into the socket timeout — see
note below):
```java
Properties props = new Properties();
PGProperty.USER.set(props, USER);
PGProperty.PASSWORD.set(props, PASSWORD);
PGProperty.ASSUME_MIN_SERVER_VERSION.set(props, "9.4");
PGProperty.REPLICATION.set(props, "database");
PGProperty.PREFER_QUERY_MODE.set(props, "simple");
// Reproduces with or without this; it just shortens the socket timeout:
PGProperty.SOCKET_TIMEOUT.set(props, 30);
Connection conn = DriverManager.getConnection(url, props);
PGConnection repl = conn.unwrap(PGConnection.class);
PGReplicationStream stream = repl.getReplicationAPI()
.replicationStream()
.logical()
.withSlotName("test_slot")
.withStatusInterval(3000, TimeUnit.MILLISECONDS)
.start();
while (true) {
ByteBuffer msg = stream.read(); // blocking
// ... process msg ...
LogSequenceNumber lsn = stream.getLastReceiveLSN();
stream.setFlushedLSN(lsn);
stream.setAppliedLSN(lsn);
}
```
3. Leave the slot **idle** (no changes on the source) for longer than the
effective socket timeout (here 3 s).
**Result:** `read()` throws `PSQLException: Tried to write to an inactive copy
operation` on the first idle period. On 42.7.8 the reader stays connected
indefinitely across idle periods.
## Stack trace
```
org.postgresql.util.PSQLException: Tried to write to an inactive copy operation
at org.postgresql.core.v3.QueryExecutorImpl.writeToCopy(QueryExecutorImpl.java:1244)
at org.postgresql.core.v3.CopyDualImpl.writeToCopy(CopyDualImpl.java:23)
at org.postgresql.core.v3.replication.V3PGReplicationStream.updateStatusInternal(V3PGReplicationStream.java:199)
at org.postgresql.core.v3.replication.V3PGReplicationStream.timeUpdateStatus(V3PGReplicationStream.java:191)
at org.postgresql.core.v3.replication.V3PGReplicationStream.readInternal(V3PGReplicationStream.java:133)
at org.postgresql.core.v3.replication.V3PGReplicationStream.read(V3PGReplicationStream.java:78)
at <application>.processRecords(...)
```
## Root cause
The failure is a sequence of interactions between the replication read loop and
a change to the COPY error handling:
1. `V3ReplicationProtocol.configureSocketTimeout(...)` sets the socket
`SO_TIMEOUT` for the blocking read to
`min(connectionSocketTimeout, statusInterval)` (or `statusInterval` when no
connection `socketTimeout` is set). This is by design: the blocking read must
wake up periodically to send standby status updates.
2. When the slot is idle, that blocking read times out and raises a
`java.net.SocketTimeoutException` (a subclass of `IOException`).
3. **The regression:** in 42.7.11, `QueryExecutorImpl.readFromCopy(...)` and
`writeToCopy(...)` were changed so their `IOException` catch blocks now call
`unlock(op)` before wrapping/rethrowing:
```java
} catch (IOException ioe) {
if (hasLock(op)) {
unlock(op); // <-- NEW in 42.7.11; not present in 42.7.8
}
throw new PSQLException(GT.tr("Database connection failed when reading from copy"),
PSQLState.CONNECTION_FAILURE, ioe);
}
```
This deactivates the COPY on **every** socket read timeout — including the
expected, benign replication idle timeout.
4. `V3PGReplicationStream.receiveNextData(...)` treats a `PSQLException` whose
cause is a `SocketTimeoutException` as normal ("no data yet") and returns
`null` — correct and unchanged behavior:
```java
} catch (PSQLException e) {
if (e.getCause() instanceof SocketTimeoutException) {
return null;
}
throw e;
}
```
5. Back in `readInternal(...)`, before the `null` check, the code sends the
periodic status update: `isTimeUpdate()` → `timeUpdateStatus()` →
`updateStatusInternal()` → `writeToCopy(op, ...)`.
6. `writeToCopy` calls `hasLock(op)`, which is now `false` (unlocked in step 3),
and throws **`Tried to write to an inactive copy operation`**.
In 42.7.8 step 3 did not unlock the COPY, so the idle timeout left the stream
active and the status update in step 5 succeeded — the reader survived idle
periods indefinitely.
## Expected behavior
A socket read timeout during logical replication is a normal idle signal, not a
connection failure. It must **not** deactivate the COPY. `read()` should return
after sending the periodic status update and continue streaming, as it did in
42.7.8.
## Suggested fix
In `QueryExecutorImpl.readFromCopy(...)` and `writeToCopy(...)`, do not
`unlock(op)` when the `IOException` is a `SocketTimeoutException` (a read
timeout is recoverable and expected for a replication COPY); only release the
COPY on a genuine connection failure. For example:
```java
} catch (IOException ioe) {
if (!(ioe instanceof SocketTimeoutException) && hasLock(op)) {
unlock(op);
}
throw new PSQLException(GT.tr("Database connection failed when reading from copy"),
PSQLState.CONNECTION_FAILURE, ioe);
}
```
(Alternatively, `receiveNextData` / `readInternal` could re-validate the COPY
state after a `SocketTimeoutException` before attempting the status update, but
not unlocking on a benign timeout is the smaller, more correct change.)
^ permalink raw [nested|flat] 6+ messages in thread
* Re: [pgjdbc/pgjdbc] issue #4328: Logical replication breaks on idle slot since 42.7.11 -> Tried to write to an inactive copy operation
@ 2026-07-29 15:20 ` "davecramer (@davecramer)" <noreply+davecramer@github.com>
4 siblings, 0 replies; 6+ messages in thread
From: davecramer (@davecramer) @ 2026-07-29 15:20 UTC (permalink / raw)
To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>
Perhaps you can provide a PR?
^ permalink raw [nested|flat] 6+ messages in thread
* Re: [pgjdbc/pgjdbc] issue #4328: Logical replication breaks on idle slot since 42.7.11 -> Tried to write to an inactive copy operation
@ 2026-07-29 17:38 ` "vlsi (@vlsi)" <noreply+vlsi@github.com>
4 siblings, 0 replies; 6+ messages in thread
From: vlsi (@vlsi) @ 2026-07-29 17:38 UTC (permalink / raw)
To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>
@vlsi assigned @vlsi.
^ permalink raw [nested|flat] 6+ messages in thread
* Re: [pgjdbc/pgjdbc] issue #4328: Logical replication breaks on idle slot since 42.7.11 -> Tried to write to an inactive copy operation
@ 2026-07-30 11:38 ` "reynoldregan (@reynoldregan)" <noreply+reynoldregan@github.com>
4 siblings, 0 replies; 6+ messages in thread
From: reynoldregan (@reynoldregan) @ 2026-07-30 11:38 UTC (permalink / raw)
To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>
PR is open here -> https://github.com/pgjdbc/pgjdbc/pull/4330
^ permalink raw [nested|flat] 6+ messages in thread
* Re: [pgjdbc/pgjdbc] issue #4328: Logical replication breaks on idle slot since 42.7.11 -> Tried to write to an inactive copy operation
@ 2026-11-05 10:19 ` "reynoldregan (@reynoldregan)" <noreply+reynoldregan@github.com>
4 siblings, 0 replies; 6+ messages in thread
From: reynoldregan (@reynoldregan) @ 2026-11-05 10:19 UTC (permalink / raw)
To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>
@vlsi , @davecramer - We are blocked on upgrading the PG JDBC Driver to the latest due to this issue. Could you please help on the timeline
^ permalink raw [nested|flat] 6+ messages in thread
* Re: [pgjdbc/pgjdbc] issue #4328: Logical replication breaks on idle slot since 42.7.11 -> Tried to write to an inactive copy operation
@ 2026-11-05 11:30 ` "vlsi (@vlsi)" <noreply+vlsi@github.com>
4 siblings, 0 replies; 6+ messages in thread
From: vlsi (@vlsi) @ 2026-11-05 11:30 UTC (permalink / raw)
To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>
@reynoldregan , thanks for the ping. It does signal the feature is needed for external parties which does increase the priority (I can't speak for everybody, so speaking for myself only).
The PR #4354 is a bit stale, and it does require polishing before review/merge. I can rebase it, however, I do not think our `master` branch is releasable: it breaks backward compatibility since #4336 broke it.
I would prefer rebasing #4354 on top of the recent "security fix" version.
@davecramer , any thoughts?
^ permalink raw [nested|flat] 6+ messages in thread
end of thread, other threads:[~2026-11-05 11:30 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-07-29 10:08 [pgjdbc/pgjdbc] issue #4328: Logical replication breaks on idle slot since 42.7.11 -> Tried to write to an inactive copy operation "reynoldregan (@reynoldregan)" <noreply+reynoldregan@github.com>
2026-07-29 15:20 ` "davecramer (@davecramer)" <noreply+davecramer@github.com>
2026-07-29 17:38 ` "vlsi (@vlsi)" <noreply+vlsi@github.com>
2026-07-30 11:38 ` "reynoldregan (@reynoldregan)" <noreply+reynoldregan@github.com>
2026-11-05 10:19 ` "reynoldregan (@reynoldregan)" <noreply+reynoldregan@github.com>
2026-11-05 11:30 ` "vlsi (@vlsi)" <noreply+vlsi@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