You can load data into tables only as a user who has the INSERT privilege on those tables. If you do not have the INSERT privilege, follow the instructions provided in GRANT to grant the INSERT privilege to the user that you use to connect to your cluster. The syntax is
GRANT INSERT ON TABLE <table_name> IN DATABASE <database_name> TO { ROLE <role_name> | USER <user_identity>}.
How it works
A Flink CDC pipeline is a YAML file with a source (PostgreSQL), a sink (the database), and optional route and transform rules. You submit the file to a Flink cluster, and it runs as a Flink job:- The PostgreSQL source takes a snapshot of each matching table, then reads changes from the PostgreSQL write-ahead log (WAL) through a logical replication slot.
- The database sink creates a Primary Key table for each source table that does not already exist in the database, then loads the rows with Stream Load. Because every table has a primary key, updates overwrite the existing row and deletes remove it.
Before you begin
You need:- PostgreSQL 10 or later. This topic uses the built-in
pgoutputlogical decoding plugin, so no server extension is required. - A cluster.
- A Flink 1.20 cluster, and Java 11 or 17 to run it.
- Network access from every Flink TaskManager to PostgreSQL, on its port (default
5432), and to the database, as described in Connect to the database. - A primary key on every PostgreSQL table you synchronize. The database creates the destination table with the same primary key.
Step 1: Prepare PostgreSQL
Run these steps as a PostgreSQL superuser, or as the owner of the tables. The examples synchronize the tables in thepublic schema of a database named shop.
-
Enable logical replication. Set
wal_leveltologicalinpostgresql.conf:Changingwal_levelrequires a restart of PostgreSQL. For a managed service such as Amazon RDS or Cloud SQL, set the equivalent parameter (for example,rds.logical_replication = 1) in the instance’s parameter group instead. Check the setting after the restart: -
Create a user for Flink CDC. It needs the
REPLICATIONattribute and read access to the tables:Ifpg_hba.confrestricts replication connections, allow this user to connect from the Flink TaskManager hosts. For example: -
Create a publication for the tables to synchronize. It must list every table that the pipeline reads (the
tablesoption in Step 4):Tip Create the publication yourself. If none exists, the connector tries to runCREATE PUBLICATION ... FOR ALL TABLES, which only a superuser can do. For any other user that fails, and Flink retries until the error it reports is aboutmax_wal_sendersrather than the missing publication. -
Set the replica identity of each table to
FULL, so that the WAL records the complete previous row for every UPDATE and DELETE:Do this before you start the pipeline. With the default replica identity, the first UPDATE or DELETE fails the Flink job with aNullPointerException, and the job keeps failing on that change after every restart.
Step 2: Prepare the database
Create the destination database and a user for the pipeline:Step 3: Install Flink CDC
- Download and start Flink 1.20. See First steps in the Flink documentation.
-
Turn on checkpointing by adding this line to the Flink configuration file,
conf/config.yaml, and then restart the cluster:The PostgreSQL source reports its position in the WAL back to PostgreSQL as checkpoints complete. Without checkpoints, the replication slot keeps every WAL segment since the pipeline started, and the PostgreSQL disk fills up. -
Download the Flink CDC release that matches your Flink version from the Flink CDC downloads page, and extract it. Starting with Flink CDC 3.6, each release is built for one Flink version, so for Flink 1.20 download
flink-cdc-3.6.0-1.20-bin.tar.gz, not the-2.2package. -
Download the PostgreSQL and the database pipeline connectors from Maven Central into the
libdirectory of Flink CDC. Use the same version as the Flink CDC release:
Step 4: Define the pipeline
Create a file namedpostgres-to-starrocks.yaml:
tablesmust match the publication. The snapshot copies every table thattablesselects, but PostgreSQL streams changes only for the tables in the publication. A table outside the publication is copied once and then never updated, and the job reports no error.tablesnames tables as<database>.<schema>.<table>. Inside the pipeline, however, a PostgreSQL table is identified only by<schema>.<table>, sorouteandtransformrules matchpublic.orders, notshop.public.orders.- Without the
routeblock, the sink writes each table to a database named after the PostgreSQL schema, so the tables inpublicland in a database calledpublic. sink.buffer-flush.interval-msdefaults to 5 minutes. With the default, a new pipeline looks idle for several minutes after it starts.- To set properties on the tables that the sink creates, add
table.create.properties.<property>to thesinksection, for exampletable.create.properties.replication_num: 1.
Connect to the database
Set the database connection options to addresses of the FE:jdbc-url:jdbc:mysql://<fe_host>:<fe_query_port>. The query port defaults to9030.load-url:<fe_host>:<fe_http_port>. The HTTP port defaults to8030.
<fe_host>, use an address that stays the same when an FE restarts or is replaced, such as a DNS name or a load balancer in front of the FEs, not the IP address of an FE. If the address stops leading to an FE, the Flink job can no longer load data.
The Flink TaskManagers must be able to reach these FE ports, and also the HTTP port (default 8040) of every BE or CN, because the FE redirects each load request to a BE or CN.
Columns with a function as the default value
The sink copies each column’s default value from PostgreSQL into the database CREATE TABLE statement. A default that is a function call, such asDEFAULT now(), is not valid in the database, and the job fails with an error like this one:
transform rule that recomputes the column. A computed column has no default value:
projection. A column that is not listed is not synchronized.
Step 5: Start the pipeline
SetFLINK_HOME to your Flink installation, and submit the pipeline from the Flink CDC directory:
http://<jobmanager_host>:8081). Its status should stay RUNNING. If it changes to RESTARTING, open the job’s Exceptions tab, and see Troubleshooting.
Step 6: Verify the synchronization
-
After the snapshot finishes, the existing rows are in the database:
-
Change some data in PostgreSQL:
- Within a few seconds (the flush interval plus the load time), run the query in the database again. It returns the same rows as the same query in PostgreSQL.
Manage the pipeline
Monitor the replication slot
A replication slot keeps WAL on the PostgreSQL server until the pipeline confirms that it has read it. While the pipeline is stopped, or if it falls behind, WAL builds up. Check how much WAL the slot is holding:Change a table’s schema
The pipeline does not pick up schema changes from PostgreSQL. If you add a column in PostgreSQL, the job keeps running, but it does not write the new column, even after you add the column to the table. To synchronize a table after a schema change:- Make the same change to the table, for example with ALTER TABLE.
- Take a new snapshot.
Take a new snapshot
To copy the tables again from the current state of PostgreSQL and then resume streaming:- Cancel the Flink job.
- Drop the replication slot, as shown in Monitor the replication slot.
-
Truncate every table that the pipeline writes to:
- Submit the pipeline again. The pipeline takes a new snapshot of every table and then continues streaming changes.
Troubleshooting
When the job isRESTARTING, the Exceptions tab of the Flink web UI shows the cause. Look for the innermost Caused by line.