org.postgresql.replication
Interface PGReplicationStream

All Known Implementing Classes:
V3PGReplicationStream

public interface PGReplicationStream

Not tread safe replication stream. After complete streaming should be close, for free resource on backend. Periodical status update work only when use read() method. It means that process wal record should be fast as possible, because during process wal record lead to disconnect by timeout from server.


Method Summary
 void close()
          Stop replication changes from server and free resources.
 void forceUpdateStatus()
          Force send to backend status about last received, flushed and applied LSN.
 LogSequenceNumber getLastAppliedLSN()
          Last applied lsn send in update message to backed.
 LogSequenceNumber getLastFlushedLSN()
          Last flushed lsn send in update message to backend.
 LogSequenceNumber getLastReceiveLSN()
          Parameter updates by execute read() method.
 boolean isClosed()
           
 ByteBuffer read()
          Read next wal record from backend.
 ByteBuffer readPending()
          Read next wal record from backend.
 void setAppliedLSN(LogSequenceNumber applied)
          Parameter used only physical replication and define which lsn already was apply on standby.
 void setFlushedLSN(LogSequenceNumber flushed)
          Set flushed LSN.
 

Method Detail

read

ByteBuffer read()
                throws SQLException

Read next wal record from backend. It method can be block until new message will not get from server.

A single WAL record is never split across two XLogData messages. When a WAL record crosses a WAL page boundary, and is therefore already split using continuation records, it can be split at the page boundary. In other words, the first main WAL record and its continuation records can be sent in different XLogData messages.

Returns:
not null byte array received by replication protocol, return ByteBuffer wrap around received byte array with use offset, so, use ByteBuffer.array() carefully
Throws:
SQLException - when some internal exception occurs during read from stream

readPending

ByteBuffer readPending()
                       throws SQLException

Read next wal record from backend. It method can't be block and in contrast to read(). If message from backend absent return null. It allow periodically check message in stream and if they absent sleep some time, but it time should be less than CommonOptions.getStatusInterval() to avoid disconnect from the server.

A single WAL record is never split across two XLogData messages. When a WAL record crosses a WAL page boundary, and is therefore already split using continuation records, it can be split at the page boundary. In other words, the first main WAL record and its continuation records can be sent in different XLogData messages.

Returns:
byte array received by replication protocol or null if pending message from server absent. Returns ByteBuffer wrap around received byte array with use offset, so, use ByteBuffer.array() carefully.
Throws:
SQLException - when some internal exception occurs during read from stream

getLastReceiveLSN

LogSequenceNumber getLastReceiveLSN()
Parameter updates by execute read() method.

Returns:
not null LSN position that was receive last time via read() method

getLastFlushedLSN

LogSequenceNumber getLastFlushedLSN()
Last flushed lsn send in update message to backend. Parameter updates only via setFlushedLSN(LogSequenceNumber)

Returns:
not null location of the last WAL flushed to disk in the standby.

getLastAppliedLSN

LogSequenceNumber getLastAppliedLSN()
Last applied lsn send in update message to backed. Parameter updates only via setAppliedLSN(LogSequenceNumber)

Returns:
not null location of the last WAL applied in the standby.

setFlushedLSN

void setFlushedLSN(LogSequenceNumber flushed)
Set flushed LSN. It parameter will be send to backend on next update status iteration. Flushed LSN position help backend define which wal can be recycle.

Parameters:
flushed - not null location of the last WAL flushed to disk in the standby.
See Also:
forceUpdateStatus()

setAppliedLSN

void setAppliedLSN(LogSequenceNumber applied)
Parameter used only physical replication and define which lsn already was apply on standby. Feedback will send to backend on next update status iteration.

Parameters:
applied - not null location of the last WAL applied in the standby.
See Also:
forceUpdateStatus()

forceUpdateStatus

void forceUpdateStatus()
                       throws SQLException
Force send to backend status about last received, flushed and applied LSN. You can not use it method explicit, because PGReplicationStream send status to backend periodical by configured interval via CommonOptions.getStatusInterval()

Throws:
SQLException - when some internal exception occurs during read from stream
See Also:
CommonOptions.getStatusInterval()

isClosed

boolean isClosed()
Returns:
true if replication stream was already close, otherwise return false

close

void close()
           throws SQLException

Stop replication changes from server and free resources. After that connection can be reuse to another queries. Also after close current stream they cannot be used anymore.

Note: This method can spend much time for logical replication stream on postgresql version 9.6 and lower, because postgresql have bug - during decode big transaction to logical form and during wait new changes postgresql ignore messages from client. As workaround you can close replication connection instead of close replication stream. For more information about it problem see mailing list thread Stopping logical replication protocol

Throws:
SQLException - when some internal exception occurs during end streaming


Copyright © 2017 PostgreSQL Global Development Group. All rights reserved.