[experimental] MaterializedPostgreSQL

Creates ClickHouse database with an initial data dump of PostgreSQL database tables and starts replication process, i.e. executes background job to apply new changes as they happen on PostgreSQL database tables in the remote PostgreSQL database.

ClickHouse server works as PostgreSQL replica. It reads WAL and performs DML queries. DDL is not replicated, but can be handled (described below).

Creating a Database

  1. CREATE DATABASE [IF NOT EXISTS] db_name [ON CLUSTER cluster]
  2. ENGINE = MaterializedPostgreSQL('host:port', ['database' | database], 'user', 'password') [SETTINGS ...]

Engine Parameters

  • host:port — PostgreSQL server endpoint.
  • database — PostgreSQL database name.
  • user — PostgreSQL user.
  • password — User password.

Dynamically adding new tables to replication

  1. ATTACH TABLE postgres_database.new_table;

It will work as well if there is a setting materialized_postgresql_tables_list.

Dynamically removing tables from replication

  1. DETACH TABLE postgres_database.table_to_remove;

Settings

  1. CREATE DATABASE database1
  2. ENGINE = MaterializedPostgreSQL('postgres1:5432', 'postgres_database', 'postgres_user', 'postgres_password')
  3. SETTINGS materialized_postgresql_max_block_size = 65536,
  4. materialized_postgresql_tables_list = 'table1,table2,table3';
  5. SELECT * FROM database1.table1;

It is also possible to change settings at run time.

  1. ALTER DATABASE postgres_database MODIFY SETTING materialized_postgresql_max_block_size = <new_size>;

Requirements

  1. The wal_level setting must have a value logical and max_replication_slots parameter must have a value at least 2 in the PostgreSQL config file.

  2. Each replicated table must have one of the following replica identity:

  • primary key (by default)

  • index

  1. postgres# CREATE TABLE postgres_table (a Integer NOT NULL, b Integer, c Integer NOT NULL, d Integer, e Integer NOT NULL);
  2. postgres# CREATE unique INDEX postgres_table_index on postgres_table(a, c, e);
  3. postgres# ALTER TABLE postgres_table REPLICA IDENTITY USING INDEX postgres_table_index;

The primary key is always checked first. If it is absent, then the index, defined as replica identity index, is checked.
If the index is used as a replica identity, there has to be only one such index in a table.
You can check what type is used for a specific table with the following command:

  1. postgres# SELECT CASE relreplident
  2. WHEN 'd' THEN 'default'
  3. WHEN 'n' THEN 'nothing'
  4. WHEN 'f' THEN 'full'
  5. WHEN 'i' THEN 'index'
  6. END AS replica_identity
  7. FROM pg_class
  8. WHERE oid = 'postgres_table'::regclass;

Warning

Replication of TOAST values is not supported. The default value for the data type will be used.

Example of Use

  1. CREATE DATABASE postgresql_db
  2. ENGINE = MaterializedPostgreSQL('postgres1:5432', 'postgres_database', 'postgres_user', 'postgres_password');
  3. SELECT * FROM postgresql_db.postgres_table;

Notes

  • Failover of the logical replication slot.

Logical Replication Slots which exist on the primary are not available on standby replicas.
So if there is a failover, new primary (the old physical standby) won’t be aware of any slots which were existing with old primary. This will lead to a broken replication from PostgreSQL.
A solution to this is to manage replication slots yourself and define a permanent replication slot (some information can be found here). You’ll need to pass slot name via materialized_postgresql_replication_slot setting, and it has to be exported with EXPORT SNAPSHOT option. The snapshot identifier needs to be passed via materialized_postgresql_snapshot setting.