Class PostgresConnection
- java.lang.Object
-
- io.debezium.jdbc.JdbcConnection
-
- io.debezium.connector.postgresql.connection.PostgresConnection
-
- All Implemented Interfaces:
AutoCloseable
public class PostgresConnection extends JdbcConnection
JdbcConnectionconnection extension used for connecting to Postgres instances.- Author:
- Horia Chiorean
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfacePostgresConnection.PostgresValueConverterBuilder-
Nested 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.StatementPreparer
-
-
Field Summary
Fields Modifier and Type Field Description private PostgresDefaultValueConverterdefaultValueConverterprotected static JdbcConnection.ConnectionFactoryFACTORYprivate static org.slf4j.LoggerLOGGERprivate static intMAX_ATTEMPTS_FOR_OBTAINING_REPLICATION_SLOTObtaining a replication slot may fail if there's a pending transaction.private static DurationPAUSE_BETWEEN_REPLICATION_SLOT_RETRIEVAL_ATTEMPTSprivate TypeRegistrytypeRegistryprivate static StringURL_PATTERN
-
Constructor Summary
Constructors Constructor Description PostgresConnection(Configuration config)Creates a Postgres connection using the supplied configuration.PostgresConnection(Configuration config, PostgresConnection.PostgresValueConverterBuilder valueConverterBuilder)Creates a Postgres connection using the supplied configuration.PostgresConnection(PostgresConnectorConfig config, TypeRegistry typeRegistry)Create a Postgres connection using the supplied configuration andTypeRegistry
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()StringconnectionString()Returns a JDBC connection string for the current configuration.LongcurrentTransactionId()Returns the PG id of the current active transactionlongcurrentXLogLocation()Returns the current position in the server tx log.protected static voiddefaultSettings(Configuration.Builder builder)private Optional<ColumnEditor>doReadTableColumn(ResultSet columnMetadata, TableId tableId, Tables.ColumnNameFilter columnFilter)booleandropPublication(String publicationName)Drops the debezium publication that was created.booleandropReplicationSlot(String slotName)Drops a replication slot that was created on the DBprivate ServerInfo.ReplicationSlotfetchReplicationSlotInfo(String slotName, String pluginName)Fetches the state of a replication stage given a slot name and plugin name<T extends DatabaseSchema<TableId>>
ObjectgetColumnValue(ResultSet rs, int columnIndex, Column column, Table table, T schema)CharsetgetDatabaseCharset()protected Optional<Object>getDefaultValue(Column column, String defaultValue)SlotStategetReplicationSlotState(String slotName, String pluginName)Returns the current state of the replication slotorg.postgresql.jdbc.TimestampUtilsgetTimestampUtils()TypeRegistrygetTypeRegistry()private LsnparseConfirmedFlushLsn(String slotName, String pluginName, String database, ResultSet rs)Obtains the LSN to resume streaming from.private LsnparseRestartLsn(String slotName, String pluginName, String database, ResultSet rs)protected ServerInfo.ReplicationSlotqueryForSlot(String slotName, String database, String pluginName, JdbcConnection.ResultSetMapper<ServerInfo.ReplicationSlot> map)StringquotedColumnIdString(String columnName)Optional<Column>readColumnForDecoder(ResultSet columnMetadata, TableId tableId, Tables.ColumnNameFilter columnNameFilter)ServerInfo.ReplicaIdentityreadReplicaIdentityInfo(TableId tableId)Prints out information about the REPLICA IDENTITY status of a table.(package private) ServerInfo.ReplicationSlotreadReplicationSlotInfo(String slotName, String pluginName)Fetches a replication slot, repeating the query until either the slot is created or until the max number of attempts has been reached To fetch the slot without the retries, use thefetchReplicationSlotInfo(java.lang.String, java.lang.String)callprotected Optional<ColumnEditor>readTableColumn(ResultSet columnMetadata, TableId tableId, Tables.ColumnNameFilter columnFilter)protected intresolveJdbcType(int metadataJdbcType, int nativeType)protected intresolveNativeType(String typeName)ServerInfoserverInfo()Returns information about the PG server to which this instance is connected.private LsntryParseLsn(String slotName, String pluginName, String database, ResultSet rs, String column)private static voidvalidateServerVersion(Statement statement)-
Methods inherited from class io.debezium.jdbc.JdbcConnection
buildSelectWithRowLimits, call, commit, config, connect, connection, connection, connectionString, database, execute, execute, executeWithoutCommitting, isConnected, isNullable, isTableUniqueIndexIncluded, isValid, parseSqlStatementString, patternBasedFactory, patternBasedFactory, prepareQuery, prepareQuery, prepareQuery, prepareQuery, prepareQuery, prepareQueryAndMap, prepareQueryWithBlockingConsumer, prepareUpdate, print, print, query, query, queryAndMap, queryAndMap, querySingleValue, queryWithBlockingConsumer, quotedTableIdString, readAllCatalogNames, readAllSchemaNames, readAllTableNames, readPrimaryKeyNames, readPrimaryKeyOrUniqueIndexNames, readSchema, readTableNames, readTablePreparedStatement, readTableStatement, readTableUniqueIndices, rollback, rowToArray, setAutoCommit, singleResultMapper, tableTypes, username
-
-
-
-
Field Detail
-
LOGGER
private static org.slf4j.Logger LOGGER
-
URL_PATTERN
private static final String URL_PATTERN
-
FACTORY
protected static final JdbcConnection.ConnectionFactory FACTORY
-
MAX_ATTEMPTS_FOR_OBTAINING_REPLICATION_SLOT
private static final int MAX_ATTEMPTS_FOR_OBTAINING_REPLICATION_SLOT
Obtaining a replication slot may fail if there's a pending transaction. We're retrying to get a slot for 30 min.- See Also:
- Constant Field Values
-
PAUSE_BETWEEN_REPLICATION_SLOT_RETRIEVAL_ATTEMPTS
private static final Duration PAUSE_BETWEEN_REPLICATION_SLOT_RETRIEVAL_ATTEMPTS
-
typeRegistry
private final TypeRegistry typeRegistry
-
defaultValueConverter
private final PostgresDefaultValueConverter defaultValueConverter
-
-
Constructor Detail
-
PostgresConnection
public PostgresConnection(Configuration config, PostgresConnection.PostgresValueConverterBuilder valueConverterBuilder)
Creates a Postgres connection using the supplied configuration. If necessary this connection is able to resolve data type mappings. Such a connection requires aPostgresValueConverter, and will provide its ownTypeRegistry. Usually only one such connection per connector is needed.- Parameters:
config-Configurationinstance, may not be null.valueConverterBuilder- supplies a configuredPostgresValueConverterfor a givenTypeRegistry
-
PostgresConnection
public PostgresConnection(PostgresConnectorConfig config, TypeRegistry typeRegistry)
Create a Postgres connection using the supplied configuration andTypeRegistry- Parameters:
config-Configurationinstance, may not be null.typeRegistry- an existing/already-primedTypeRegistryinstance
-
PostgresConnection
public PostgresConnection(Configuration config)
Creates a Postgres connection using the supplied configuration. The connector is the regular one without datatype resolution capabilities.- Parameters:
config-Configurationinstance, may not be null.
-
-
Method Detail
-
connectionString
public String connectionString()
Returns a JDBC connection string for the current configuration.- Returns:
- a
Stringwhere the variables inurlPatternare replaced with values from the configuration
-
readReplicaIdentityInfo
public ServerInfo.ReplicaIdentity readReplicaIdentityInfo(TableId tableId) throws SQLException
Prints out information about the REPLICA IDENTITY status of a table. This in turn determines how much information is available for UPDATE and DELETE operations for logical replication.- Parameters:
tableId- the identifier of the table- Returns:
- the replica identity information; never null
- Throws:
SQLException- if there is a problem obtaining the replica identity information for the given table
-
getReplicationSlotState
public SlotState getReplicationSlotState(String slotName, String pluginName) throws SQLException
Returns the current state of the replication slot- Parameters:
slotName- the name of the slotpluginName- the name of the plugin used for the desired slot- Returns:
- the
SlotStateor null, if no slot state is found - Throws:
SQLException
-
fetchReplicationSlotInfo
private ServerInfo.ReplicationSlot fetchReplicationSlotInfo(String slotName, String pluginName) throws SQLException
Fetches the state of a replication stage given a slot name and plugin name- Parameters:
slotName- the name of the slotpluginName- the name of the plugin used for the desired slot- Returns:
- the
ServerInfo.ReplicationSlotobject or aServerInfo.ReplicationSlot.INVALIDif the slot is not valid - Throws:
SQLException- is thrown by the underlying JDBC
-
readReplicationSlotInfo
@VisibleForTesting ServerInfo.ReplicationSlot readReplicationSlotInfo(String slotName, String pluginName) throws SQLException, InterruptedException
Fetches a replication slot, repeating the query until either the slot is created or until the max number of attempts has been reached To fetch the slot without the retries, use thefetchReplicationSlotInfo(java.lang.String, java.lang.String)call- Parameters:
slotName- the slot namepluginName- the name of the plugin- Returns:
- the
ServerInfo.ReplicationSlotobject or aServerInfo.ReplicationSlot.INVALIDif the slot is not valid - Throws:
SQLException- is thrown by the underyling jdbc driverInterruptedException- is thrown if we don't return an answer within the set number of retries
-
queryForSlot
protected ServerInfo.ReplicationSlot queryForSlot(String slotName, String database, String pluginName, JdbcConnection.ResultSetMapper<ServerInfo.ReplicationSlot> map) throws SQLException
- Throws:
SQLException
-
parseConfirmedFlushLsn
private Lsn parseConfirmedFlushLsn(String slotName, String pluginName, String database, ResultSet rs)
Obtains the LSN to resume streaming from. On PG 9.5 there is no confirmed_flushed_lsn yet, so restart_lsn will be read instead. This may result in more records to be re-read after a restart.
-
parseRestartLsn
private Lsn parseRestartLsn(String slotName, String pluginName, String database, ResultSet rs)
-
tryParseLsn
private Lsn tryParseLsn(String slotName, String pluginName, String database, ResultSet rs, String column) throws org.apache.kafka.connect.errors.ConnectException, SQLException
- Throws:
org.apache.kafka.connect.errors.ConnectExceptionSQLException
-
dropReplicationSlot
public boolean dropReplicationSlot(String slotName)
Drops a replication slot that was created on the DB- Parameters:
slotName- the name of the replication slot, may not be null- Returns:
trueif the slot was dropped,falseotherwise
-
dropPublication
public boolean dropPublication(String publicationName)
Drops the debezium publication that was created.- Parameters:
publicationName- the publication name, may not be null- Returns:
trueif the publication was dropped,falseotherwise
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Overrides:
closein classJdbcConnection
-
currentTransactionId
public Long currentTransactionId() throws SQLException
Returns the PG id of the current active transaction- Returns:
- a PG transaction identifier, or null if no tx is active
- Throws:
SQLException- if anything fails.
-
currentXLogLocation
public long currentXLogLocation() throws SQLExceptionReturns the current position in the server tx log.- Returns:
- a long value, never negative
- Throws:
SQLException- if anything unexpected fails.
-
serverInfo
public ServerInfo serverInfo() throws SQLException
Returns information about the PG server to which this instance is connected.- Returns:
- a
ServerInfoinstance, nevernull - Throws:
SQLException- if anything fails
-
getDatabaseCharset
public Charset getDatabaseCharset()
-
getTimestampUtils
public org.postgresql.jdbc.TimestampUtils getTimestampUtils()
-
defaultSettings
protected static void defaultSettings(Configuration.Builder builder)
-
validateServerVersion
private static void validateServerVersion(Statement statement) throws SQLException
- Throws:
SQLException
-
quotedColumnIdString
public String quotedColumnIdString(String columnName)
- Overrides:
quotedColumnIdStringin classJdbcConnection
-
resolveNativeType
protected int resolveNativeType(String typeName)
- Overrides:
resolveNativeTypein classJdbcConnection
-
resolveJdbcType
protected int resolveJdbcType(int metadataJdbcType, int nativeType)- Overrides:
resolveJdbcTypein classJdbcConnection
-
readTableColumn
protected Optional<ColumnEditor> readTableColumn(ResultSet columnMetadata, TableId tableId, Tables.ColumnNameFilter columnFilter) throws SQLException
- Overrides:
readTableColumnin classJdbcConnection- Throws:
SQLException
-
readColumnForDecoder
public Optional<Column> readColumnForDecoder(ResultSet columnMetadata, TableId tableId, Tables.ColumnNameFilter columnNameFilter) throws SQLException
- Throws:
SQLException
-
doReadTableColumn
private Optional<ColumnEditor> doReadTableColumn(ResultSet columnMetadata, TableId tableId, Tables.ColumnNameFilter columnFilter) throws SQLException
- Throws:
SQLException
-
getDefaultValue
protected Optional<Object> getDefaultValue(Column column, String defaultValue)
- Overrides:
getDefaultValuein classJdbcConnection
-
getTypeRegistry
public TypeRegistry getTypeRegistry()
-
getColumnValue
public <T extends DatabaseSchema<TableId>> Object getColumnValue(ResultSet rs, int columnIndex, Column column, Table table, T schema) throws SQLException
- Overrides:
getColumnValuein classJdbcConnection- Throws:
SQLException
-
-