Class PostgresConnection
- java.lang.Object
-
- io.debezium.jdbc.JdbcConnection
-
- io.debezium.connector.postgresql.connection.PostgresConnection
-
- All Implemented Interfaces:
AutoCloseable
public class PostgresConnection extends io.debezium.jdbc.JdbcConnectionJdbcConnectionconnection extension used for connecting to Postgres instances.- Author:
- Horia Chiorean
Copied from Debezium 1.9.8-Final with the following modifications:
- Constructor PostgresConnection( Configuration config, PostgresValueConverterBuilder valueConverterBuilder, ConnectionFactory factory) to allow passing a custom ConnectionFactory
- override connection() to return a unwrapped PgConnection (otherwise, it will complain about HikariProxyConnection cannot be cast to class org.postgresql.core.BaseConnection)
- override isTableUniqueIndexIncluded: Copied DBZ-5398 from Debezium 2.0.0.Final to fix https://github.com/ververica/flink-cdc-connectors/issues/2710. Remove this comment after bumping debezium version to 2.0.0.Final.
- FLINK-38965: Modified doReadTableColumn to filter out columns from other tables that might be returned due to PostgreSQL LIKE wildcard matching. The underscore '_' matches any single character, and '%' matches any sequence of characters. For example, when querying table 'user_sink', the LIKE pattern may also match 'userbsink' (due to '_'); when querying table 'user%data' (where % is a literal character in the table name), the LIKE pattern may also match 'user_test_data' (due to '%'). See also: https://github.com/debezium/debezium/blob/main/debezium-core/src/main/java/io/debezium/jdbc/JdbcConnection.java#L1327
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfacePostgresConnection.PostgresValueConverterBuilder-
Nested classes/interfaces inherited from class io.debezium.jdbc.JdbcConnection
io.debezium.jdbc.JdbcConnection.BlockingMultiResultSetConsumer, io.debezium.jdbc.JdbcConnection.BlockingResultSetConsumer, io.debezium.jdbc.JdbcConnection.CallPreparer, io.debezium.jdbc.JdbcConnection.ConnectionFactory, io.debezium.jdbc.JdbcConnection.MultiResultSetConsumer, io.debezium.jdbc.JdbcConnection.Operations, io.debezium.jdbc.JdbcConnection.ParameterResultSetConsumer, io.debezium.jdbc.JdbcConnection.ResultSetConsumer, io.debezium.jdbc.JdbcConnection.ResultSetExtractor<T extends Object>, io.debezium.jdbc.JdbcConnection.ResultSetMapper<T extends Object>, io.debezium.jdbc.JdbcConnection.StatementFactory, io.debezium.jdbc.JdbcConnection.StatementPreparer
-
-
Field Summary
Fields Modifier and Type Field Description static StringCONNECTION_DROP_SLOTstatic StringCONNECTION_GENERALstatic StringCONNECTION_HEARTBEATstatic StringCONNECTION_SLOT_INFOstatic StringCONNECTION_STREAMINGstatic StringCONNECTION_VALIDATE_CONNECTIONprotected static io.debezium.jdbc.JdbcConnection.ConnectionFactoryFACTORY
-
Constructor Summary
Constructors Constructor Description PostgresConnection(io.debezium.connector.postgresql.PostgresConnectorConfig config, io.debezium.connector.postgresql.TypeRegistry typeRegistry, String connectionUsage)Create a Postgres connection using the supplied configuration andTypeRegistryPostgresConnection(io.debezium.jdbc.JdbcConfiguration config, PostgresConnection.PostgresValueConverterBuilder valueConverterBuilder, String connectionUsage)Creates a Postgres connection using the supplied configuration.PostgresConnection(io.debezium.jdbc.JdbcConfiguration config, PostgresConnection.PostgresValueConverterBuilder valueConverterBuilder, String connectionUsage, io.debezium.jdbc.JdbcConnection.ConnectionFactory factory)Creates a Postgres connection using the supplied configuration.PostgresConnection(io.debezium.jdbc.JdbcConfiguration config, String connectionUsage)Creates a Postgres connection using the supplied configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Connectionconnection()Return an unwrapped PgConnection instead of HikariProxyConnectionStringconnectionString()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.booleandropPublication(String publicationName)Drops the debezium publication that was created.booleandropReplicationSlot(String slotName)Drops a replication slot that was created on the DBSet<io.debezium.relational.TableId>getAllTableIds(String catalogName)Retrieves allTableIds in a given database catalog, including partitioned tables.<T extends io.debezium.schema.DatabaseSchema<io.debezium.relational.TableId>>
ObjectgetColumnValue(ResultSet rs, int columnIndex, io.debezium.relational.Column column, io.debezium.relational.Table table, T schema)CharsetgetDatabaseCharset()io.debezium.connector.postgresql.connection.PostgresDefaultValueConvertergetDefaultValueConverter()io.debezium.connector.postgresql.spi.SlotStategetReplicationSlotState(String slotName, String pluginName)Returns the current state of the replication slotorg.postgresql.jdbc.TimestampUtilsgetTimestampUtils()io.debezium.connector.postgresql.TypeRegistrygetTypeRegistry()protected booleanisTableType(String tableType)protected booleanisTableUniqueIndexIncluded(String indexName, String columnName)protected io.debezium.connector.postgresql.connection.ServerInfo.ReplicationSlotqueryForSlot(String slotName, String database, String pluginName, io.debezium.jdbc.JdbcConnection.ResultSetMapper<io.debezium.connector.postgresql.connection.ServerInfo.ReplicationSlot> map)StringquotedColumnIdString(String columnName)Optional<io.debezium.relational.Column>readColumnForDecoder(ResultSet columnMetadata, io.debezium.relational.TableId tableId, io.debezium.relational.Tables.ColumnNameFilter columnNameFilter)io.debezium.connector.postgresql.connection.ServerInfo.ReplicaIdentityreadReplicaIdentityInfo(io.debezium.relational.TableId tableId)Prints out information about the REPLICA IDENTITY status of a table.protected Optional<io.debezium.relational.ColumnEditor>readTableColumn(ResultSet columnMetadata, io.debezium.relational.TableId tableId, io.debezium.relational.Tables.ColumnNameFilter columnFilter)protected intresolveJdbcType(int metadataJdbcType, int nativeType)protected intresolveNativeType(String typeName)io.debezium.connector.postgresql.connection.ServerInfoserverInfo()Returns information about the PG server to which this instance is connected.protected String[]supportedTableTypes()-
Methods inherited from class io.debezium.jdbc.JdbcConnection
buildSelectWithRowLimits, call, commit, config, connect, connection, connectionString, database, execute, execute, executeWithoutCommitting, getColumnsDetails, getCurrentTimestamp, isConnected, isNullable, isValid, loadKeyStore, overrideColumn, 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, resolveCatalogName, rollback, rowToArray, setAutoCommit, singleResultMapper, tableTypes, username
-
-
-
-
Field Detail
-
CONNECTION_STREAMING
public static final String CONNECTION_STREAMING
- See Also:
- Constant Field Values
-
CONNECTION_SLOT_INFO
public static final String CONNECTION_SLOT_INFO
- See Also:
- Constant Field Values
-
CONNECTION_DROP_SLOT
public static final String CONNECTION_DROP_SLOT
- See Also:
- Constant Field Values
-
CONNECTION_VALIDATE_CONNECTION
public static final String CONNECTION_VALIDATE_CONNECTION
- See Also:
- Constant Field Values
-
CONNECTION_HEARTBEAT
public static final String CONNECTION_HEARTBEAT
- See Also:
- Constant Field Values
-
CONNECTION_GENERAL
public static final String CONNECTION_GENERAL
- See Also:
- Constant Field Values
-
FACTORY
protected static final io.debezium.jdbc.JdbcConnection.ConnectionFactory FACTORY
-
-
Constructor Detail
-
PostgresConnection
public PostgresConnection(io.debezium.jdbc.JdbcConfiguration config, PostgresConnection.PostgresValueConverterBuilder valueConverterBuilder, String connectionUsage)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 givenTypeRegistryconnectionUsage- a symbolic name of the connection to be tracked in monitoring tools
-
PostgresConnection
public PostgresConnection(io.debezium.jdbc.JdbcConfiguration config, PostgresConnection.PostgresValueConverterBuilder valueConverterBuilder, String connectionUsage, io.debezium.jdbc.JdbcConnection.ConnectionFactory factory)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 givenTypeRegistryconnectionUsage- a symbolic name of the connection to be tracked in monitoring tools
-
PostgresConnection
public PostgresConnection(io.debezium.connector.postgresql.PostgresConnectorConfig config, io.debezium.connector.postgresql.TypeRegistry typeRegistry, String connectionUsage)Create a Postgres connection using the supplied configuration andTypeRegistry- Parameters:
config-Configurationinstance, may not be null.typeRegistry- an existing/already-primedTypeRegistryinstanceconnectionUsage- a symbolic name of the connection to be tracked in monitoring tools
-
PostgresConnection
public PostgresConnection(io.debezium.jdbc.JdbcConfiguration config, String connectionUsage)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.connectionUsage- a symbolic name of the connection to be tracked in monitoring tools
-
-
Method Detail
-
connection
public Connection connection() throws SQLException
Return an unwrapped PgConnection instead of HikariProxyConnection- Overrides:
connectionin classio.debezium.jdbc.JdbcConnection- Throws:
SQLException
-
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 io.debezium.connector.postgresql.connection.ServerInfo.ReplicaIdentity readReplicaIdentityInfo(io.debezium.relational.TableId tableId) throws SQLExceptionPrints 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 io.debezium.connector.postgresql.spi.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
-
queryForSlot
protected io.debezium.connector.postgresql.connection.ServerInfo.ReplicationSlot queryForSlot(String slotName, String database, String pluginName, io.debezium.jdbc.JdbcConnection.ResultSetMapper<io.debezium.connector.postgresql.connection.ServerInfo.ReplicationSlot> map) throws SQLException
- Throws:
SQLException
-
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 classio.debezium.jdbc.JdbcConnection
-
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 io.debezium.connector.postgresql.connection.ServerInfo serverInfo() throws SQLExceptionReturns 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()
-
quotedColumnIdString
public String quotedColumnIdString(String columnName)
- Overrides:
quotedColumnIdStringin classio.debezium.jdbc.JdbcConnection
-
resolveNativeType
protected int resolveNativeType(String typeName)
- Overrides:
resolveNativeTypein classio.debezium.jdbc.JdbcConnection
-
resolveJdbcType
protected int resolveJdbcType(int metadataJdbcType, int nativeType)- Overrides:
resolveJdbcTypein classio.debezium.jdbc.JdbcConnection
-
readTableColumn
protected Optional<io.debezium.relational.ColumnEditor> readTableColumn(ResultSet columnMetadata, io.debezium.relational.TableId tableId, io.debezium.relational.Tables.ColumnNameFilter columnFilter) throws SQLException
- Overrides:
readTableColumnin classio.debezium.jdbc.JdbcConnection- Throws:
SQLException
-
readColumnForDecoder
public Optional<io.debezium.relational.Column> readColumnForDecoder(ResultSet columnMetadata, io.debezium.relational.TableId tableId, io.debezium.relational.Tables.ColumnNameFilter columnNameFilter) throws SQLException
- Throws:
SQLException
-
getDefaultValueConverter
public io.debezium.connector.postgresql.connection.PostgresDefaultValueConverter getDefaultValueConverter()
-
getTypeRegistry
public io.debezium.connector.postgresql.TypeRegistry getTypeRegistry()
-
getColumnValue
public <T extends io.debezium.schema.DatabaseSchema<io.debezium.relational.TableId>> Object getColumnValue(ResultSet rs, int columnIndex, io.debezium.relational.Column column, io.debezium.relational.Table table, T schema) throws SQLException
- Overrides:
getColumnValuein classio.debezium.jdbc.JdbcConnection- Throws:
SQLException
-
supportedTableTypes
protected String[] supportedTableTypes()
- Overrides:
supportedTableTypesin classio.debezium.jdbc.JdbcConnection
-
isTableType
protected boolean isTableType(String tableType)
- Overrides:
isTableTypein classio.debezium.jdbc.JdbcConnection
-
isTableUniqueIndexIncluded
protected boolean isTableUniqueIndexIncluded(String indexName, String columnName)
- Overrides:
isTableUniqueIndexIncludedin classio.debezium.jdbc.JdbcConnection
-
getAllTableIds
public Set<io.debezium.relational.TableId> getAllTableIds(String catalogName) throws SQLException
Retrieves allTableIds in a given database catalog, including partitioned tables.- Parameters:
catalogName- the catalog/database name- Returns:
- set of all table ids for existing table objects
- Throws:
SQLException- if a database exception occurred
-
-