Interface ReplicationConnection

All Superinterfaces:
AutoCloseable
All Known Implementing Classes:
PostgresReplicationConnection

@NotThreadSafe public interface ReplicationConnection extends AutoCloseable
A Postgres logical streaming replication connection. Replication connections are established for a slot and a given plugin and WAL offset tracking is specific to the [database name, plugin name, slot name] triple.
Author:
Horia Chiorean (hchiorea@redhat.com)
  • Method Details

    • startStreaming

      Opens a stream for reading logical replication changes from the last known position of the slot for which the connection was opened. The last known position is based on the server's confirmed_flush_lsn value from the pg_replication_slots table.

      If there is no "last known value" (i.e. the connection is for a newly created slot) then the Postgres server will start streaming changes from that last position reported during this connection's creation.

      Returns:
      a PGReplicationStream from which data is read; never null
      Throws:
      SQLException - if there is a problem obtaining the replication stream
      InterruptedException
    • startStreaming

      ReplicationStream startStreaming(Lsn offset, WalPositionLocator walPosition) throws SQLException, InterruptedException
      Opens a stream for reading logical replication changes from a given LSN position.

      Note that it is possible for a server to have recycled old WAL segments (see the wal_keep_segments setting). If that is the case, then even though a LSN number may be valid, the server will not stream back any changes because they are not available.

      Parameters:
      offset - a value representing the WAL sequence number where replication should start from; if the value is null or negative, this behaves exactly like
      invalid reference
      #startStreaming()
      .
      Returns:
      a PGReplicationStream from which data is read; never null
      Throws:
      SQLException - if anything fails
      InterruptedException
      See Also:
      • LogSequenceNumber
    • createReplicationSlot

      Optional<SlotCreationResult> createReplicationSlot() throws SQLException
      Creates a new replication slot with the given option and returns the result of the command, which may contain results (depending on postgres versions)
      Throws:
      SQLException - if anything fails
    • initConnection

      void initConnection() throws SQLException, InterruptedException
      Forces the connection to be created, is called by startStreaming, but can be called manually in cases where we want the connection but not to to start streaming yet. Can be called multiple times
      Throws:
      SQLException
      InterruptedException
    • isConnected

      boolean isConnected() throws SQLException
      Checks whether this connection is open or not
      Returns:
      true if this connection is open, false otherwise
      Throws:
      SQLException - if anything unexpected fails
    • builder

      Creates a new ReplicationConnection.Builder instance which can be used for creating replication connections.
      Parameters:
      config - a PostgresConnectorConfig instance; may not be null
      Returns:
      a builder, never null
    • reconnect

      void reconnect() throws SQLException
      Throws:
      SQLException