The Postgres CDC source table, that is, Postgres streaming source table, is used to read the full snapshot data and changed data of the PostgreSQL database in sequence. The exactly-once processing semantics is used to ensure data accuracy even if a failure occurs.
ALTER TABLE test.cdc_order REPLICA IDENTITY FULL
SELECT name FROM pg_available_extensions;
If the default plug-in decoderbufs is not available, you need to set the decoding.plugin.name parameter to specify an existing plug-in in PostgreSQL when creating the PostgreSQL CDC source table.
create table postgresCdcSource ( attr_name attr_type (',' attr_name attr_type)* (','PRIMARY KEY (attr_name, ...) NOT ENFORCED) ) with ( 'connector' = 'postgres-cdc', 'hostname' = 'PostgresHostname', 'username' = 'PostgresUsername', 'password' = 'PostgresPassword', 'database-name' = 'PostgresDatabaseName', 'schema-name' = 'PostgresSchemaName', 'table-name' = 'PostgresTableName' );
Parameter |
Mandatory |
Default Value |
Data Type |
Description |
---|---|---|---|---|
connector |
Yes |
None |
String |
Connector to be used. Set this parameter to postgres-cdc. |
hostname |
Yes |
None |
String |
IP address or hostname of the Postgres database. |
username |
Yes |
None |
String |
Username of the Postgres database. |
password |
Yes |
None |
String |
Password of the Postgres database. |
database-name |
Yes |
None |
String |
Database name. |
schema-name |
Yes |
None |
String |
Postgres schema name. The schema name supports regular expressions to read data from multiple schemas. For example, test(.)* indicates all schema names starting with test. |
table-name |
Yes |
None |
String |
Postgres table name. The table name supports regular expressions to read data from multiple tables. For example, cdc_order(.)* indicates all table names starting with cdc_order. |
port |
No |
5432 |
Integer |
Port number of the Postgres database. |
decoding.plugin.name |
No |
decoderbufs |
String |
Determined based on the plug-in that is installed in the PostgreSQL database. The value can be:
|
debezium.* |
No |
None |
String |
Fine-grained control over the behavior of Debezium clients, for example, 'debezium.snapshot.mode' = 'never'. You are advised to set the debezium.slot.name parameter for each table to avoid the following error: "PSQLException: ERROR: replication slot "debezium" is active for PID 974" |
In this example, Postgres-CDC is used to read data from RDS for PostgreSQL in real time and write the data to the Print result table. The procedure is as follows (PostgreSQL 11.11 is used in this example):
create table test.cdc_order( order_id VARCHAR, order_channel VARCHAR, order_time VARCHAR, pay_amount FLOAT8, real_pay FLOAT8, pay_time VARCHAR, user_id VARCHAR, user_name VARCHAR, area_id VARCHAR, primary key(order_id) );
ALTER TABLE test.cdc_order REPLICA IDENTITY FULL
create table postgresCdcSource( order_id string, order_channel string, order_time string, pay_amount double, real_pay double, pay_time string, user_id string, user_name string, area_id STRING, primary key (order_id) not enforced ) with ( 'connector' = 'postgres-cdc', 'hostname' = 'PostgresHostname', 'username' = 'PostgresUsername', 'password' = 'PostgresPassword', 'database-name' = 'flink', 'schema-name' = 'test', 'table-name' = 'cdc_order' ); create table printSink( order_id string, order_channel string, order_time string, pay_amount double, real_pay double, pay_time string, user_id string, user_name string, area_id STRING, primary key(order_id) not enforced ) with ( 'connector' = 'print' ); insert into printSink select * from postgresCdcSource;
insert into test.cdc_order (order_id, order_channel, order_time, pay_amount, real_pay, pay_time, user_id, user_name, area_id) values ('202103241000000001', 'webShop', '2021-03-24 10:00:00', '100.00', '100.00', '2021-03-24 10:02:03', '0001', 'Alice', '330106'), ('202103251202020001', 'miniAppShop', '2021-03-25 12:02:02', '60.00', '60.00', '2021-03-25 12:03:00', '0002', 'Bob', '330110'); update test.cdc_order set order_channel = 'webShop' where order_id = '202103251202020001'; delete from test.cdc_order where order_id = '202103241000000001';
The data result is as follows:
+I(202103241000000001,webShop,2021-03-24 10:00:00,100.0,100.0,2021-03-24 10:02:03,0001,Alice,330106) +I(202103251202020001,miniAppShop,2021-03-25 12:02:02,60.0,60.0,2021-03-25 12:03:00,0002,Bob,330110) -U(202103251202020001,miniAppShop,2021-03-25 12:02:02,60.0,60.0,2021-03-25 12:03:00,0002,Bob,330110) +U(202103251202020001,webShop,2021-03-25 12:02:02,60.0,60.0,2021-03-25 12:03:00,0002,Bob,330110) -D(202103241000000001,webShop,2021-03-24 10:00:00,100.0,100.0,2021-03-24 10:02:03,0001,Alice,330106)
org.postgresql.util.PSQLException: ERROR: logical decoding requires wal_level >= logical
After modifying the PostgreSQL parameter, restart the RDS PostgreSQL instance for the modification to take effect.
java.lang.IllegalStateException: The "before" field of UPDATE/DELETE message is null, please check the Postgres table has been set REPLICA IDENTITY to FULL level. You can update the setting by running the command in Postgres 'ALTER TABLE test.cdc_order REPLICA IDENTITY FULL'.
A: If a similar error is reported in the run log, run the ALTER TABLE test.cdc_order REPLICA IDENTITY FULL statement in PostgreSQL.