Class PostgresReplicationConnection
java.lang.Object
io.debezium.jdbc.JdbcConnection
io.debezium.connector.postgresql.connection.PostgresReplicationConnection
- All Implemented Interfaces:
ReplicationConnection,AutoCloseable
Implementation of a
ReplicationConnection for Postgresql. Note that replication connections in PG cannot execute
regular statements but only a limited number of replication-related commands.- Author:
- Horia Chiorean (hchiorea@redhat.com)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classNested classes/interfaces inherited from class io.debezium.jdbc.JdbcConnection
JdbcConnection.BlockingMultiResultSetConsumer, JdbcConnection.BlockingResultSetConsumer, JdbcConnection.CallPreparer, JdbcConnection.ConnectionFactory, JdbcConnection.MultiResultSetConsumer, JdbcConnection.Operations, JdbcConnection.ParameterResultSetConsumer, JdbcConnection.ResultSetConsumer, JdbcConnection.ResultSetExtractor<T extends Object>, JdbcConnection.ResultSetMapper<T extends Object>, JdbcConnection.StatementFactory, JdbcConnection.StatementPreparerNested classes/interfaces inherited from interface io.debezium.connector.postgresql.connection.ReplicationConnection
ReplicationConnection.Builder -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final PostgresConnectorConfigprivate Lsnprivate final booleanprivate booleanprivate final PostgresConnectionprivate static org.slf4j.Loggerprivate final MessageDecoderprivate final PostgresConnectorConfig.LogicalDecoderprivate final PostgresConnectorConfig.AutoCreateModeprivate final Stringprivate Optional<ReplicaIdentityMapper>private SlotCreationResultprivate final Stringprivate static final Stringprivate final Durationprivate final Propertiesprivate final RelationalTableFiltersprivate final TypeRegistry -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePostgresReplicationConnection(PostgresConnectorConfig config, String slotName, String publicationName, RelationalTableFilters tableFilter, PostgresConnectorConfig.AutoCreateMode publicationAutocreateMode, PostgresConnectorConfig.LogicalDecoder plugin, boolean dropSlotOnClose, Duration statusUpdateInterval, PostgresConnection jdbcConnection, TypeRegistry typeRegistry, Properties streamParams, PostgresSchema schema) Creates a new replication connection with the given params. -
Method Summary
Modifier and TypeMethodDescriptionprivate static JdbcConfigurationaddDefaultSettings(JdbcConfiguration configuration) voidclose()voidclose(boolean dropSlot) private voidcreateOrUpdatePublicationModeFilterted(Statement stmt, boolean isUpdate) Creates a new replication slot with the given option and returns the result of the command, which may contain results (depending on postgres versions)private ReplicationStreamcreateReplicationStream(Lsn startLsn, WalPositionLocator walPosition) private ServerInfo.ReplicationSlotprivate BooleanhasMinimumVersion(int version) voidForces 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.protected voidprivate voidCheck all tables captured by the connector, contained inSet<TableId>fromdetermineCapturedTables().protected voidprivate SlotCreationResultprotected org.postgresql.core.BaseConnectionvoidprivate org.postgresql.replication.PGReplicationStreamstartPgReplicationStream(Lsn lsn, BiFunction<org.postgresql.replication.fluent.logical.ChainedLogicalStreamBuilder, Function<Integer, Boolean>, org.postgresql.replication.fluent.logical.ChainedLogicalStreamBuilder> configurator) startStreaming(Lsn offset, WalPositionLocator walPosition) Opens a stream for reading logical replication changes from a given LSN position.startStreaming(WalPositionLocator walPosition) creating a replication connection and starting to stream involves a few steps: 1.private booleanprotected voidvalidateSlotIsInExpectedState(WalPositionLocator walPosition) Methods inherited from class io.debezium.jdbc.JdbcConnection
buildSelectWithRowLimits, call, commit, config, connect, connection, connection, connectionString, database, escapeEscapeSequence, execute, execute, executeWithoutCommitting, getAttributeDetails, getColumnsDetails, getColumnValue, getCurrentTimestamp, isConnected, isNullable, isTableType, isTableUniqueIndexIncluded, isValid, loadKeyStore, overrideColumn, parseSqlStatementString, patternBasedFactory, patternBasedFactory, prepareQuery, prepareQuery, prepareQuery, prepareQuery, prepareQuery, prepareQueryAndMap, prepareQueryWithBlockingConsumer, prepareUpdate, print, print, query, query, queryAndMap, queryAndMap, querySingleValue, queryWithBlockingConsumer, quotedColumnIdString, quotedTableIdString, readAllCatalogNames, readAllSchemaNames, readAllTableNames, readPrimaryKeyNames, readPrimaryKeyOrUniqueIndexNames, readSchema, readTableColumn, readTableNames, readTablePreparedStatement, readTableStatement, readTableUniqueIndices, resolveCatalogName, resolveJdbcType, resolveNativeType, rollback, rowToArray, setAutoCommit, setQueryColumnValue, singleResultMapper, supportedTableTypes, tableTypes, usernameMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.debezium.connector.postgresql.connection.ReplicationConnection
isConnected
-
Field Details
-
SQL_STATE_INSUFFICIENT_PRIVILEGE
- See Also:
-
LOGGER
private static org.slf4j.Logger LOGGER -
slotName
-
publicationName
-
tableFilter
-
publicationAutocreateMode
-
plugin
-
dropSlotOnClose
private final boolean dropSlotOnClose -
connectorConfig
-
statusUpdateInterval
-
messageDecoder
-
jdbcConnection
-
typeRegistry
-
streamParams
-
defaultStartingPos
-
slotCreationInfo
-
hasInitedSlot
private boolean hasInitedSlot -
replicaIdentityMapper
-
-
Constructor Details
-
PostgresReplicationConnection
private PostgresReplicationConnection(PostgresConnectorConfig config, String slotName, String publicationName, RelationalTableFilters tableFilter, PostgresConnectorConfig.AutoCreateMode publicationAutocreateMode, PostgresConnectorConfig.LogicalDecoder plugin, boolean dropSlotOnClose, Duration statusUpdateInterval, PostgresConnection jdbcConnection, TypeRegistry typeRegistry, Properties streamParams, PostgresSchema schema) Creates a new replication connection with the given params.- Parameters:
config- the JDBC configuration for the connection; may not be nullslotName- the name of the DB slot for logical replication; may not be nullpublicationName- the name of the DB publication for logical replication; may not be nulltableFilter- the tables to watch of the DB publication for logical replication; may not be nullpublicationAutocreateMode- the mode for publication autocreation; may not be nullplugin- decoder matching the server side plug-in used for streaming changes; may not be nulldropSlotOnClose- whether the replication slot should be dropped once the connection is closedstatusUpdateInterval- the interval at which the replication connection should periodically send statusjdbcConnection- general PostgreSQL JDBC connectiontypeRegistry- registry with PostgreSQL typesstreamParams- additional parameters to pass to the replication streamschema- the schema; must not be null
-
-
Method Details
-
addDefaultSettings
-
getSlotInfo
- Throws:
SQLExceptionInterruptedException
-
initPublication
protected void initPublication() -
createOrUpdatePublicationModeFilterted
-
initReplicaIdentity
private void initReplicaIdentity()Check all tables captured by the connector, contained inSet<TableId>fromdetermineCapturedTables(). Updating Replica Identity in PostgreSQL database based onPostgresConnectorConfig.REPLICA_IDENTITY_AUTOSET_VALUESconfiguration parameter for eachTableId- Throws:
Exception
-
determineCapturedTables
- Throws:
Exception
-
initReplicationSlot
- Throws:
SQLExceptionInterruptedException
-
useTemporarySlot
- Throws:
SQLException
-
startStreaming
public ReplicationStream startStreaming(WalPositionLocator walPosition) throws SQLException, InterruptedException creating a replication connection and starting to stream involves a few steps: 1. we create the connection and ensure that a. the slot exists b. the slot isn't currently being used 2. we query to get our potential start position in the slot (lsn) 3. we try and start streaming, depending on our options this may fail, which can result in the connection being killed and we need to start the process over if we are using a temporary slot 4. actually start the streamerThis method takes care of all of these and this method queries for a default starting position If you know where you are starting from you should call
startStreaming(Lsn, WalPositionLocator), this method delegates to that method- Specified by:
startStreamingin interfaceReplicationConnection- Returns:
- Throws:
SQLExceptionInterruptedException
-
startStreaming
public ReplicationStream startStreaming(Lsn offset, WalPositionLocator walPosition) throws SQLException, InterruptedException Description copied from interface:ReplicationConnectionOpens 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_segmentssetting). 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.- Specified by:
startStreamingin interfaceReplicationConnection- Parameters:
offset- a value representing the WAL sequence number where replication should start from; if the value isnullor negative, this behaves exactly like#startStreaming().- Returns:
- a
PGReplicationStreamfrom which data is read; never null - Throws:
SQLException- if anything failsInterruptedException- See Also:
-
LogSequenceNumber
-
validateSlotIsInExpectedState
- Throws:
SQLException
-
initConnection
Description copied from interface:ReplicationConnectionForces 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- Specified by:
initConnectionin interfaceReplicationConnection- Throws:
SQLExceptionInterruptedException
-
createReplicationSlot
Description copied from interface:ReplicationConnectionCreates a new replication slot with the given option and returns the result of the command, which may contain results (depending on postgres versions)- Specified by:
createReplicationSlotin interfaceReplicationConnection- Throws:
SQLException- if anything fails
-
pgConnection
- Throws:
SQLException
-
parseSlotCreation
-
createReplicationStream
private ReplicationStream createReplicationStream(Lsn startLsn, WalPositionLocator walPosition) throws SQLException, InterruptedException - Throws:
SQLExceptionInterruptedException
-
startPgReplicationStream
private org.postgresql.replication.PGReplicationStream startPgReplicationStream(Lsn lsn, BiFunction<org.postgresql.replication.fluent.logical.ChainedLogicalStreamBuilder, Function<Integer, throws SQLExceptionBoolean>, org.postgresql.replication.fluent.logical.ChainedLogicalStreamBuilder> configurator) - Throws:
SQLException
-
hasMinimumVersion
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Overrides:
closein classJdbcConnection
-
close
public void close(boolean dropSlot) -
reconnect
- Specified by:
reconnectin interfaceReplicationConnection- Throws:
SQLException
-