Class PostgresConnection

  • All Implemented Interfaces:
    AutoCloseable

    public class PostgresConnection
    extends io.debezium.jdbc.JdbcConnection
    JdbcConnection connection 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 interface  PostgresConnection.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
    • 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 and TypeRegistry
      PostgresConnection​(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
      void close()  
      Connection connection()
      Return an unwrapped PgConnection instead of HikariProxyConnection
      String connectionString()
      Returns a JDBC connection string for the current configuration.
      Long currentTransactionId()
      Returns the PG id of the current active transaction
      long currentXLogLocation()
      Returns the current position in the server tx log.
      boolean dropPublication​(String publicationName)
      Drops the debezium publication that was created.
      boolean dropReplicationSlot​(String slotName)
      Drops a replication slot that was created on the DB
      Set<io.debezium.relational.TableId> getAllTableIds​(String catalogName)
      Retrieves all TableIds in a given database catalog, including partitioned tables.
      <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)  
      Charset getDatabaseCharset()  
      io.debezium.connector.postgresql.connection.PostgresDefaultValueConverter getDefaultValueConverter()  
      io.debezium.connector.postgresql.spi.SlotState getReplicationSlotState​(String slotName, String pluginName)
      Returns the current state of the replication slot
      org.postgresql.jdbc.TimestampUtils getTimestampUtils()  
      io.debezium.connector.postgresql.TypeRegistry getTypeRegistry()  
      protected boolean isTableType​(String tableType)  
      protected boolean isTableUniqueIndexIncluded​(String indexName, String columnName)  
      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)  
      String quotedColumnIdString​(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.ReplicaIdentity readReplicaIdentityInfo​(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 int resolveJdbcType​(int metadataJdbcType, int nativeType)  
      protected int resolveNativeType​(String typeName)  
      io.debezium.connector.postgresql.connection.ServerInfo serverInfo()
      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
    • 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 a PostgresValueConverter, and will provide its own TypeRegistry. Usually only one such connection per connector is needed.
        Parameters:
        config - Configuration instance, may not be null.
        valueConverterBuilder - supplies a configured PostgresValueConverter for a given TypeRegistry
        connectionUsage - 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 a PostgresValueConverter, and will provide its own TypeRegistry. Usually only one such connection per connector is needed.
        Parameters:
        config - Configuration instance, may not be null.
        valueConverterBuilder - supplies a configured PostgresValueConverter for a given TypeRegistry
        connectionUsage - 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 and TypeRegistry
        Parameters:
        config - Configuration instance, may not be null.
        typeRegistry - an existing/already-primed TypeRegistry instance
        connectionUsage - 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 - Configuration instance, 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:
        connection in class io.debezium.jdbc.JdbcConnection
        Throws:
        SQLException
      • connectionString

        public String connectionString()
        Returns a JDBC connection string for the current configuration.
        Returns:
        a String where the variables in urlPattern are replaced with values from the configuration
      • readReplicaIdentityInfo

        public io.debezium.connector.postgresql.connection.ServerInfo.ReplicaIdentity readReplicaIdentityInfo​(io.debezium.relational.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 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 slot
        pluginName - the name of the plugin used for the desired slot
        Returns:
        the SlotState or 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:
        true if the slot was dropped, false otherwise
      • dropPublication

        public boolean dropPublication​(String publicationName)
        Drops the debezium publication that was created.
        Parameters:
        publicationName - the publication name, may not be null
        Returns:
        true if the publication was dropped, false otherwise
      • close

        public void close()
        Specified by:
        close in interface AutoCloseable
        Overrides:
        close in class io.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 SQLException
        Returns 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 SQLException
        Returns information about the PG server to which this instance is connected.
        Returns:
        a ServerInfo instance, never null
        Throws:
        SQLException - if anything fails
      • getDatabaseCharset

        public Charset getDatabaseCharset()
      • getTimestampUtils

        public org.postgresql.jdbc.TimestampUtils getTimestampUtils()
      • quotedColumnIdString

        public String quotedColumnIdString​(String columnName)
        Overrides:
        quotedColumnIdString in class io.debezium.jdbc.JdbcConnection
      • resolveNativeType

        protected int resolveNativeType​(String typeName)
        Overrides:
        resolveNativeType in class io.debezium.jdbc.JdbcConnection
      • resolveJdbcType

        protected int resolveJdbcType​(int metadataJdbcType,
                                      int nativeType)
        Overrides:
        resolveJdbcType in class io.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:
        readTableColumn in class io.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:
        getColumnValue in class io.debezium.jdbc.JdbcConnection
        Throws:
        SQLException
      • supportedTableTypes

        protected String[] supportedTableTypes()
        Overrides:
        supportedTableTypes in class io.debezium.jdbc.JdbcConnection
      • isTableType

        protected boolean isTableType​(String tableType)
        Overrides:
        isTableType in class io.debezium.jdbc.JdbcConnection
      • isTableUniqueIndexIncluded

        protected boolean isTableUniqueIndexIncluded​(String indexName,
                                                     String columnName)
        Overrides:
        isTableUniqueIndexIncluded in class io.debezium.jdbc.JdbcConnection
      • getAllTableIds

        public Set<io.debezium.relational.TableId> getAllTableIds​(String catalogName)
                                                           throws SQLException
        Retrieves all TableIds 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