Loading data from PostgreSQL to a ClickHouse® data mart
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
file, specify the passwords of the PostgreSQL and ClickHouse® admin user. -
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
-
Create a table named
x_tab
in thedb1
database 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:
<source_PostgreSQL_cluster_name>
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:
<target_ClickHouse®_cluster_name>
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.
-
In the
postgresql-to-clickhouse.tf
file, set thetransfer_enabled
parameter to1
. -
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
has 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 source PostgreSQL database, delete the row with the41
ID and update the one with the42
ID:DELETE FROM db1.public.x_tab WHERE id = 41; UPDATE db1.public.x_tab SET name = 'Key3' WHERE id = 42;
-
Check 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 when the was row updated to this value, inTIMESTAMP
format. -
__data_transfer_delete_time
: Row deletion time, 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 to work. If a record is deleted or updated, a new row is inserted with a value in this column. A query by a single primary key returns multiple records with different__data_transfer_commit_time
values.
With the Replicating transfer status, the source data can be added or deleted. To ensure standard behavior of SQL commands when a primary key points to a single record, provide a clause to filter data by __data_transfer_delete_time
when querying tables transferred to ClickHouse®. Here is an example of a query to 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 __data_transfer_delete_time
and use it for querying. Here is an example of a query to the x_tab
table:
CREATE VIEW x_tab_view AS SELECT * FROM x_tab FINAL
WHERE __data_transfer_delete_time == 0;
Note
Using the FINAL
keyword in queries makes them much less efficient. 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