Asynchronously replicating data from PostgreSQL to ClickHouse®
You can migrate a database from PostgreSQL to ClickHouse® using Yandex Data Transfer. To do this:
- Set up the transfer.
- Activate the transfer.
- Test the replication process.
- Select the data from the target.
If you no longer need the resources you created, delete them.
Getting started
For clarity, we will create all required resources in Yandex Cloud. Prepare the infrastructure:
-
Create a source Managed Service for PostgreSQL cluster in any applicable configuration with publicly available hosts and the following settings:
- DB name:
db1
- Username:
pg-user
- Password:
<source_password>
- DB name:
-
Create a Managed Service for ClickHouse® target cluster in any applicable configuration with publicly available hosts and the following settings:
- Number of ClickHouse® hosts: At least two, which is required to enable replication in the cluster.
- DB name:
db1
- Username:
ch-user
- Password:
<target_password>
-
If you are using security groups in clusters, make sure they are set up correctly and allow connecting to the clusters:
-
Grant the
mdb_replication
role topg-user
in the Managed Service for PostgreSQL cluster.
-
If you do not have Terraform yet, install it.
-
Get the authentication credentials. You can add them to environment variables or specify them later in the provider configuration file.
-
Configure and initialize a provider. There is no need to create a provider configuration file manually, you can download it
. -
Place the configuration file in a separate working directory and specify the parameter values. If you did not add the authentication credentials to environment variables, specify them in the configuration file.
-
Download the postgresql-to-clickhouse.tf
configuration file to the same working directory.This file describes:
- Networks.
- Subnets.
- Security groups for making cluster connections.
- Managed Service for PostgreSQL source cluster.
- Managed Service for ClickHouse® target cluster.
- Source endpoint.
- Target endpoint.
- Transfer.
-
In the
postgresql-to-clickhouse.tf
configuration file, specify the PostgreSQL and ClickHouse® administrator passwords. -
Make sure the Terraform configuration files are correct using this command:
terraform validate
If there are any errors in the configuration files, Terraform will point them out.
-
Create the required infrastructure:
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
All the required resources will be created in the specified folder. You can check resource availability and their settings in the management console
. -
Set up the transfer
-
In the
db1
database, create a table namedx_tab
, and populate it with data:CREATE TABLE x_tab ( id NUMERIC PRIMARY KEY, name CHAR(5) ); CREATE INDEX ON x_tab (id); INSERT INTO x_tab (id, name) VALUES (40, 'User1'), (41, 'User2'), (42, 'User3'), (43, 'User4'), (44, 'User5');
-
Create a transfer:
ManuallyTerraform-
Create a source endpoint of the
PostgreSQL
type and specify the cluster connection parameters in it:- Installation type:
Managed Service for PostgreSQL cluster
- Managed Service for PostgreSQL cluster:
<name_of_PostgreSQL_source_cluster>
from the drop-down list - Database:
db1
- User:
pg-user
- Password:
<user_password>
- Installation type:
-
Create a target endpoint of the
ClickHouse
type and specify the cluster connection parameters in it:- Connection type:
Managed cluster
- Managed cluster:
<name_of_ClickHouse®_target_cluster>
from the drop-down list - Database:
db1
- User:
ch-user
- Password:
<user_password>
- Cleanup policy:
DROP
- Connection type:
-
Create a transfer of the Snapshot and replication type that will use the created endpoints.
-
Set the
transfer_enabled
variable to1
in thepostgresql-to-clickhouse.tf
file. -
Make sure the Terraform configuration files are correct using this command:
terraform validate
If there are any errors in the configuration files, Terraform will point them out.
-
Create the required infrastructure:
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
-
-
Activate the transfer
-
Activate the transfer and wait for its status to change to Replicating.
-
To check that the transfer has moved the replicated data to the target, connect to the target Yandex Managed Service for ClickHouse® cluster and make sure that the
x_tab
table indb1
includes the same columns as thex_tab
table in the source database, as well as the timestamp columns:__data_transfer_commit_time
and__data_transfer_delete_time
:SELECT * FROM db1.x_tab WHERE id = 41;
┌─id─┬──name──┬─── __data-transfer_commit_time─┬───__data-transfer-delete_time─┐ │ 41 │ User2 │ 1633417594957267000 │ 0 │ └────┴────────┴────────────────────────────────┴───────────────────────────────┘
Test the replication process
-
Connect to the source cluster.
-
In the
x_tab
table of the PostgreSQL source database, delete the row where ID is41
and update the one where ID is42
:DELETE FROM db1.public.x_tab WHERE id = 41; UPDATE db1.public.x_tab SET name = 'Key3' WHERE id = 42;
-
Make sure that you see the changes in the
x_tab
table on the ClickHouse® target:SELECT * FROM db1.x_tab WHERE (id >= 41) AND (id <= 42);
┌─id─┬──name──┬─── __data-transfer_commit_time─┬───__data-transfer-delete_time─┐ │ 41 │ User2 │ 1633417594957267000 │ 1675417594957267000 │ │ 42 │ Key3 │ 1675417594957267000 │ 0 │ │ 42 │ User3 │ 1633417594957268000 │ 1675417594957267000 │ └────┴────────┴────────────────────────────────┴───────────────────────────────┘
Select the data from ClickHouse®
For table recovery, the ClickHouse® target with replication enabled uses the ReplicatedReplacingMergeTree
-
__data_transfer_commit_time
: Time for the row to change to this value, inTIMESTAMP
format. -
__data_transfer_delete_time
: Time for deleting the row, inTIMESTAMP
format, if the row was deleted in the source. If the row was not deleted, the value is set to0
.The
__data_transfer_commit_time
column is required for the ReplicatedReplacedMergeTree engine. If a record is deleted or updated, a new row is inserted with a value in this column. When running a query by a single primary key, it will return multiple records with different values from the__data_transfer_commit_time
column.
With the Replicating transfer status, the source data can be added or deleted. To ensure standard SQL commands behavior when a primary key points to a single record, add a construction filtering data by the __data_transfer_delete_time
column to your queries to the tables moved to ClickHouse®. For example, for the x_tab
table:
SELECT * FROM x_tab FINAL
WHERE __data_transfer_delete_time = 0;
To simplify the SELECT
queries, create a view with filtering by the __data_transfer_delete_time
column and use it for querying. For example, for the x_tab
table:
CREATE VIEW x_tab_view AS SELECT * FROM x_tab FINAL
WHERE __data_transfer_delete_time == 0;
Note
The FINAL
keyword noticeably decreases the query efficiency. Avoid it when working with large tables whenever possible.
Delete the resources you created
Some resources are not free of charge. To avoid paying for them, delete the resources you no longer need:
-
Make sure the transfer has the Completed status and delete it.
-
Delete the endpoints and clusters:
ManuallyTerraformIf you created your resources using Terraform:
-
In the terminal window, go to the directory containing the infrastructure plan.
-
Delete the
postgresql-to-clickhouse.tf
configuration file. -
Make sure the Terraform configuration files are correct using this command:
terraform validate
If there are any errors in the configuration files, Terraform will point them out.
-
Confirm updating the resources.
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
All the resources described in the
postgresql-to-clickhouse.tf
configuration file will be deleted. -
-
ClickHouse® is a registered trademark of ClickHouse, Inc