2021-04-09 14:07:18 +00:00
---
toc_priority: 30
2021-06-27 19:09:17 +00:00
toc_title: MaterializedPostgreSQL
2021-04-09 14:07:18 +00:00
---
2021-06-27 19:09:17 +00:00
# MaterializedPostgreSQL {#materialize-postgresql}
2021-04-09 14:07:18 +00:00
## Creating a Database {#creating-a-database}
2021-05-04 10:43:21 +00:00
``` sql
CREATE DATABASE test_database
2021-06-27 19:09:17 +00:00
ENGINE = MaterializedPostgreSQL('postgres1:5432', 'postgres_database', 'postgres_user', 'postgres_password'
2021-05-04 10:43:21 +00:00
SELECT * FROM test_database.postgres_table;
```
## Settings {#settings}
2021-06-27 19:09:17 +00:00
1. `materialized_postgresql_max_block_size` - Number of rows collected before flushing data into table. Default: `65536` .
2021-05-04 10:43:21 +00:00
2021-06-27 19:09:17 +00:00
2. `materialized_postgresql_tables_list` - List of tables for MaterializedPostgreSQL database engine. Default: `whole database` .
2021-05-04 10:43:21 +00:00
2021-06-27 19:09:17 +00:00
3. `materialized_postgresql_allow_automatic_update` - Allow to reload table in the background, when schema changes are detected. Default: `0` (`false`).
2021-05-04 10:43:21 +00:00
``` sql
CREATE DATABASE test_database
2021-06-27 19:09:17 +00:00
ENGINE = MaterializedPostgreSQL('postgres1:5432', 'postgres_database', 'postgres_user', 'postgres_password'
SETTINGS materialized_postgresql_max_block_size = 65536,
materialized_postgresql_tables_list = 'table1,table2,table3';
2021-05-04 10:43:21 +00:00
SELECT * FROM test_database.table1;
```
2021-04-11 19:58:33 +00:00
## Requirements {#requirements}
2021-04-09 14:07:18 +00:00
2021-05-04 10:43:21 +00:00
- Setting `wal_level` to `logical` and `max_replication_slots` to at least `2` in the postgresql config file.
2021-04-11 19:58:33 +00:00
- Each replicated table must have one of the following **replica identity** :
2021-04-09 14:07:18 +00:00
1. **default** (primary key)
2. **index**
2021-04-10 14:42:45 +00:00
``` bash
2021-04-09 14:07:18 +00:00
postgres# CREATE TABLE postgres_table (a Integer NOT NULL, b Integer, c Integer NOT NULL, d Integer, e Integer NOT NULL);
postgres# CREATE unique INDEX postgres_table_index on postgres_table(a, c, e);
postgres# ALTER TABLE postgres_table REPLICA IDENTITY USING INDEX postgres_table_index;
```
2021-04-10 14:42:45 +00:00
Primary key is always checked first. If it is absent, then index, defined as replica identity index, is checked.
If index is used as replica identity, there has to be only one such index in a table.
2021-04-09 14:07:18 +00:00
You can check what type is used for a specific table with the following command:
2021-04-10 14:42:45 +00:00
``` bash
2021-04-09 14:07:18 +00:00
postgres# SELECT CASE relreplident
WHEN 'd' THEN 'default'
WHEN 'n' THEN 'nothing'
WHEN 'f' THEN 'full'
WHEN 'i' THEN 'index'
END AS replica_identity
FROM pg_class
WHERE oid = 'postgres_table'::regclass;
```
2021-06-27 19:09:17 +00:00
2021-07-01 08:20:13 +00:00
## Warning {#warning}
2021-06-27 19:09:17 +00:00
2021-07-01 08:20:13 +00:00
1. **TOAST** values convertion is not supported. Default value for the data type will be used.