The Debezium PolarDBO connector is compatible with PolarDB for PostgreSQL (Compatible with Oracle) and captures row-level changes in PolarDB for PostgreSQL (Compatible with Oracle) databases, generates data change event records, and streams them to Kafka topics. For more information about its features and usage, see the community Debezium PostgreSQL connector.
Since PolarDB for PostgreSQL (Compatible with Oracle) and community PostgreSQL differ only in how they handle a few data types and built-in objects, this topic describes how to build a Debezium connector that supports PolarDB for PostgreSQL (Compatible with Oracle) by adapting the community's Debezium PostgreSQL connector with minimal code changes.
Build the Debezium PolarDBO connector
The Debezium PolarDBO connector is adapted from the community Debezium connector for PostgreSQL. No service-level agreement (SLA) is provided for the Debezium PolarDBO connector, whether you build it yourself or use the JAR package provided in this topic.
Prerequisites
-
Set up the Java environment
All Debezium versions require Java 11 or later. Ensure that you have Java 11 configured before you build and run the connector.
-
Determine the Debezium version
Select a Debezium version that is compatible with your Kafka, Kafka Connect, and PolarDB for PostgreSQL (Compatible with Oracle) versions. For compatibility details, see the Debezium Releases Overview.
Note-
For the Debezium code repository, see Debezium.
-
The following table maps PolarDB for PostgreSQL (Compatible with Oracle) versions to their compatible community PostgreSQL versions.
-
Oracle compatibility 2.0 corresponds to community PostgreSQL 14.
-
Oracle compatibility 1.0 corresponds to community PostgreSQL 11.
-
-
-
Identify the PgJDBC version
In the
pom.xmlfile of the selected Debezium version, search forversion.postgresql.driverto identify the PgJDBC version.NoteFor the PgJDBC code repository, see PgJDBC.
Procedure
Debezium community edition 2.6.2.Final supports Kafka Connect 2.x and 3.x, and PostgreSQL versions 10, 11, 12, 13, 14, 15, and 16.
The following steps show how to build the connector based on Debezium 2.6.2.Final.
-
Clone the Debezium and PgJDBC repositories for the required versions.
git clone -b v2.6.2.Final --depth=1 https://github.com/debezium/debezium.git git clone -b REL42.6.1 --depth=1 https://github.com/pgjdbc/pgjdbc.git -
Copy the required PgJDBC files into the Debezium directory.
mkdir -p debezium/debezium-connector-postgres/src/main/java/org/postgresql/core/v3 mkdir -p debezium/debezium-connector-postgres/src/main/java/org/postgresql/jdbc cp pgjdbc/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java debezium/debezium-connector-postgres/src/main/java/org/postgresql/core/v3 cp pgjdbc/pgjdbc/src/main/java/org/postgresql/core/v3/QueryExecutorImpl.java debezium/debezium-connector-postgres/src/main/java/org/postgresql/core/v3 cp pgjdbc/pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java debezium/debezium-connector-postgres/src/main/java/org/postgresql/jdbc -
Apply the patch file to adapt the connector for PolarDB for PostgreSQL (Compatible with Oracle).
git apply v2.6.2.Final-support-polardbo-v1.patchNote-
Download the compatibility patch file for the Debezium PolarDBO connector: v2.6.2.Final-support-polardbo-v1.patch.
-
By default, this patch packages the debezium-api, debezium-core, PgJDBC, and protobuf-java dependencies into the JAR. To exclude these dependencies, remove them from the pom.xml file.
-
-
Use Maven to build the Debezium PolarDBO connector.
mvn clean package -pl :debezium-connector-postgres -DskipITs -Dquick # After the build is complete, you can find the JAR package in the debezium-connector-postgres/target directory.A JAR package for the Debezium PolarDBO connector, built with JDK 11, is also available for download: debezium-connector-postgres-polardbo-v1.0-2.6.2.Final.jar.
Usage
The Debezium PolarDBO connector reads incremental changes from a PolarDB for PostgreSQL (Compatible with Oracle) database using logical replication. The following conditions must be met before you can use the connector:
-
The
wal_levelcluster parameter must be set tological. This setting adds the information required for logical replication to the write-ahead logging (WAL).NoteYou can set the
wal_levelcluster parameter in the console. For more information, see Configure cluster parameters. Changing this parameter restarts the cluster. Plan this operation carefully according to your business needs. -
Run the
ALTER TABLE schema.table REPLICA IDENTITY FULL;command to set theREPLICA IDENTITYof each subscribed table toFULL. This ensures that insert and update events contain the previous values for all columns, which guarantees data consistency.Note-
REPLICA IDENTITY is a table-level setting specific to PostgreSQL. It determines whether the logical decoding plug-in includes the previous values of the involved table columns during INSERT and UPDATE events. For more information about the values for REPLICA IDENTITY, see REPLICA IDENTITY.
-
Setting the
REPLICA IDENTITYof a subscribed table toFULLmay require a table lock, which can affect your services. Plan this operation according to your business needs. You can run the following command to check if the current setting isFULL:SELECT relreplident = 'f' FROM pg_class WHERE relname = 'tablename';
-
-
The
max_wal_sendersandmax_replication_slotsparameter values must be greater than the sum of replication slots currently in use and those required by your Kafka jobs. -
Use a privileged account or a standard account that has both LOGIN and REPLICATION permissions. The account must also have the SELECT permission on all subscribed tables for the initial snapshot.
-
Connect only to the
primary endpointof the PolarDB cluster. Logical replication is not supported on thecluster endpoint. -
Set the
connector.classparameter toio.debezium.connector.postgresql.PolarDBOConnector. -
We recommend that you set the
plugin.nameparameter topgoutput. Otherwise, incremental parsing may produce garbled text for databases that do not use UTF-8 encoding. For more information, see the community documentation.
Example
The following example describes how to use the Debezium PolarDBO connector to synchronize the t1 and t2 tables from the dbz_db database in a PolarDB for PostgreSQL Oracle compatibility 2.0 cluster to a Kafka message queue.
Prerequisites
-
Set up Kafka
-
Deploy a Kafka
instanceand ensure it is accessible from the Kafka Connecthost. You can also use ApsaraMQ for Kafka. For more information, see Quick start. -
Create a topic named
pg_dbz_eventin the Kafka instance to receive messages.NoteFor testing, you can create a single-
partitiontopic for easier viewing. For production environments, create a multi-partition topic.
-
-
Start Kafka Connect locally in
distributed modeon port 8083.-
Copy the Debezium PolarDBO connector JAR package to the
plugin.pathdirectory of Kafka Connect.# Replace ${plugin.path} with the actual path. mkdir ${plugin.path}/debezium-connector-polardbo cp debezium-connector-postgres-polardbo-v1.0-2.6.2.Final.jar ${plugin.path}/debezium-connector-polardbo
-
-
Set up PolarDB for PostgreSQL (Compatible with Oracle)
-
On the PolarDB cluster purchase page, purchase a PolarDB for PostgreSQL (Compatible with Oracle) 2.0 cluster.
-
Configure the PolarDB cluster to meet all the prerequisites listed in the Usage section.
-
Create a privileged account. For more information, see Create an account.
-
Obtain the cluster
primary endpoint. For more information, see View connection endpoints. If the PolarDB cluster and the Kafka Connect instance are in the sameavailability zone, you can use theprivate endpoint. Otherwise, you must request apublic endpoint. Add the address of the Kafka Connectinstanceto the PolarDBcluster whitelist. For more information, see Configure a cluster whitelist. -
In the console, create a database named
dbz_db. For more information, see Create a database. -
Run the following statements to create the
t1andt2tables in thedbz_dbdatabase and insert data.CREATE TABLE public.t1 (a int PRIMARY KEY, b text, c TIMESTAMP); ALTER TABLE public.t1 REPLICA IDENTITY FULL; INSERT INTO public.t1(a, b, c) VALUES(1, 'a', now()); CREATE TABLE public.t2 (a int PRIMARY KEY, b text, c DATE); ALTER TABLE public.t2 REPLICA IDENTITY FULL; INSERT INTO public.t2(a, b, c) VALUES(1, 'a', now());
-
Test
-
Create a configuration file named
config/postgresql-connector.json. For parameter descriptions, see the community documentation.{ "name": "dbz-polardb", "config": { "connector.class": "io.debezium.connector.postgresql.PolarDBOConnector", "database.hostname": "<yourHostname>", "database.port": "<yourPort>", "database.user": "<yourUserName>", "database.password": "<yourPassWord>", "database.dbname" : "dbz_db", "plugin.name": "pgoutput", "slot.name": "dbz_polardb", "table.include.list": "public.t1,public.t2", "topic.prefix": "polardb" "transforms": "Combine", "transforms.Combine.type": "io.debezium.transforms.ByLogicalTableRouter", "transforms.Combine.topic.regex": "(.*)", "transforms.Combine.topic.replacement": "pg_dbz_event" } }NoteBy default, Debezium creates one topic per table. This configuration routes all change events to a single topic.
-
Add the connector.
curl -i -X POST -H "Accept:application/json" -H "Content-Type:application/json" 'http://localhost:8083/connectors' -d @config/postgresql-connector.jsonAfter the connector is added, the full data becomes available in the Kafka topic.
On the message query tab of your Kafka instance, set the query method to query by offset, select partition
0, and set the start offset to0. Then, click Query. The results show two Debezium CDC messages (at offsets 0 and 1). The keys contain__dbz__physicalTableIdentifiervalues ofpolardb.public.t1andpolardb.public.t2, respectively. This confirms that the full data snapshot has been synchronized to Kafka. -
Run the following DML statements in the
dbz_dbdatabase of the PolarDB cluster:INSERT INTO public.t1(a, b, c) VALUES(2, 'b', now()); UPDATE public.t1 SET b = 'c' WHERE a = 1; DELETE FROM public.t1 WHERE a = 2; INSERT INTO public.t1(a, b, c) VALUES(4, 'd', now()); INSERT INTO public.t2(a, b, c) VALUES(2, 'b', now()); UPDATE public.t2 SET b = 'c' WHERE a = 1; DELETE FROM public.t2 WHERE a = 2; INSERT INTO public.t2(a, b, c) VALUES(4, 'd', now());The incremental data is now available in the Kafka topic.
On the message query page, set the query method to query by offset, select the target partition and start offset, and then click Query. The results show that Debezium captured the DML operations on the t1 and t2 tables as CDC messages. The key of each message includes the table identifier (such as
polardb.public.t1) and the primary key value. The message value contains the Debezium-formatted change event, includingbeforeandafterfields. For a DELETE operation, the message value is 0 bytes.