Why Postgres Replication Stream doesn't work when used in separate function? Why Postgres Replication Stream doesn't work when used in separate function? postgresql postgresql

Why Postgres Replication Stream doesn't work when used in separate function?


I believe your problem with with Connection. It gets out of scope once your function returns and it is upto garbage collector to collect it and finalize. in finalize, connection is closed and probably then your program fails. Try moving connection and other required intermediate variables in a scope that is available in your main method and try again.


There is some moderately bogus behavior with respect to closing a connection while doing a COPY. I wonder if that could be involved?

If memory serves, the issue was that a Terminate message (sent on Connection.close() to indicate that the client wants to end theprotocol connection) is not synchronized relative to COPY protocol operations. This means that theoretically, your Terminate messagecould be shimmed right into the middle of a CopyData message. In practice, I've only ever seen an error from the server complainingabout an unexpected message type (Terminate) during COPY, since the client is supposed to send CopyDone or CopyFail before a Terminatewhile a COPY is in effect. Still, I think it might be possible to get message contents munged.

I think your code might be susceptible to that since you kick off a bunch of COPY operations in a separate function and there might be a possibility that connection might get garbage collected.

As I see in the logs that socket connection is closed while copy protocol is still reading the data.

org.postgresql.util.PSQLException: Database connection failed when reading from copyat org.postgresql.core.v3.QueryExecutorImpl.readFromCopy(QueryExecutorImpl.java:1028)at org.postgresql.core.v3.CopyDualImpl.readFromCopy(CopyDualImpl.java:41)at org.postgresql.core.v3.replication.V3PGReplicationStream.receiveNextData(V3PGReplicationStream.java:155)at org.postgresql.core.v3.replication.V3PGReplicationStream.readInternal(V3PGReplicationStream.java:124)at org.postgresql.core.v3.replication.V3PGReplicationStream.readPending(V3PGReplicationStream.java:78)at Server.main(Server.java:47)Caused by: java.net.SocketException: Socket closedat java.net.SocketInputStream.socketRead0(Native Method)at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)at java.net.SocketInputStream.read(SocketInputStream.java:171)at java.net.SocketInputStream.read(SocketInputStream.java:141)at org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:140)at org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:109)