Files

218 lines
16 KiB
Plaintext

---
id: 'clickhouse-destination'
title: 'ClickHouse destination'
description: 'Configure ClickHouse as a Supabase Pipelines destination.'
subtitle: 'Replicate Supabase Postgres changes to ClickHouse.'
sidebar_label: 'ClickHouse'
---
<$Partial path="pipelines-public-alpha.mdx" />
[ClickHouse](https://clickhouse.com/) is a database for analytics. Supabase Pipelines replicates Postgres tables to ClickHouse as either current-state tables or an append-only history of changes.
To replicate data to ClickHouse:
1. [Choose a table engine](#choose-a-table-engine) and check the [source table requirements](#source-table-requirements).
2. [Prepare a database, user, and HTTPS endpoint](#prepare-clickhouse-resources) in ClickHouse.
3. [Configure the ClickHouse destination](#configure-clickhouse-as-a-destination) in the Dashboard.
4. [Query the replicated data](#query-replicated-data) in ClickHouse.
## Source table requirements
The source table requirements depend on the selected table engine and the operations in the Postgres publication. `ReplacingMergeTree` requires a source primary key. `MergeTree` can replicate insert-only tables without one. If a table has a primary key, include all its key columns in the publication.
Source primary keys must be nondeferrable for either engine.
Check the [replica-identity and array requirements](#replica-identity-and-arrays) for your source tables.
### Choose a table engine
The table engine determines how ClickHouse stores and queries replicated changes. Choose one engine for the entire destination:
| Engine | Data model and query pattern |
| -------------------- | ------------------------------------------------------------------------------------------------ |
| `ReplacingMergeTree` | Current-state tables. Requires a primary key. Query the generated `__current` view. |
| `MergeTree` | Append-only CDC history. A primary key is optional for insert-only tables. Query the base table. |
Choose the engine before creating the pipeline. Changing **Table engine** later does not convert existing destination tables. Writes fail if their engine differs from the configured one. Restore the previous setting to resume writing to those tables.
With `ReplacingMergeTree`, changing a source primary-key value removes the old key from the current-state view and writes the row under its new key. Changing which columns make up the primary key is a separate [schema change](#schema-change-support).
## Prepare ClickHouse resources
Managed Pipelines run in AWS `eu-central-1` (Frankfurt). When creating your ClickHouse service, choose a nearby region to [reduce replication latency](/docs/guides/database/replication/pipelines#region). Then prepare these resources in ClickHouse before creating the destination:
1. Create an empty database for the replicated tables. In ClickHouse Cloud, open your service's **SQL console** and run `create database if not exists pipelines;`, replacing `pipelines` if you prefer another name. Pipelines creates and manages the tables and, for `ReplacingMergeTree`, the current-state views. Do not create or alter these objects yourself.
2. Create a dedicated database user for Pipelines and grant it access to that database. In ClickHouse Cloud, create this user in the **SQL console**; the organization's **Users and roles** page manages console members. The database user must be able to:
- Query `system.databases`, `system.tables`, and `system.columns`
- Create, alter, truncate, and drop tables
- Create and drop views when using `ReplacingMergeTree`
- Insert rows into managed tables
3. Copy the public HTTPS endpoint for your ClickHouse server. In ClickHouse Cloud, click **Connect** and select **HTTPS** to find it. Include the port if your endpoint requires one. Pipelines cannot connect to HTTP endpoints or private and internal hostnames.
For `ReplacingMergeTree`, the ClickHouse server must run version 23.5 or later. `MergeTree` does not have this minimum-version requirement.
## Configure ClickHouse as a destination
Follow [Set up Pipelines](/docs/guides/database/replication/pipelines#setup-overview). When prompted to choose a destination, select **ClickHouse** and enter the following settings:
| Field | Value |
| ------------------ | ----------------------------------------------------------------------- |
| **HTTPS endpoint** | Public HTTPS endpoint, including its port when required |
| **User** | Your dedicated database user, such as `pipelines_user` |
| **Password** | That user's password, if set |
| **Database** | Destination database, such as `pipelines` |
| **Table engine** | `ReplacingMergeTree` for current state or `MergeTree` for event history |
Complete validation and review the initial sync estimate before confirming creation.
## Query replicated data
### How table names are mapped
Pipelines maps each Postgres schema and table pair to one ClickHouse table. For names without leading or trailing underscores, existing underscores are doubled and the names are joined with one underscore:
| Postgres table | ClickHouse table |
| ---------------- | ----------------- |
| `public.orders` | `public_orders` |
| `my_schema.logs` | `my__schema_logs` |
<$Partial path="pipelines-table-names.mdx" />
With `ReplacingMergeTree`, destination table names ending in `__current` are reserved for generated views. Rename source tables that would map to these names, or choose `MergeTree`.
### ReplacingMergeTree
Use the generated `__current` view when you need the latest version of each source row. `ReplacingMergeTree` is the default engine. It uses the source primary key to identify rows and the generated view resolves row versions and removes deleted rows.
The `_etl_version` and `_etl_deleted` names are reserved and can't be used by source columns.
Query the generated view for the current state:
```sql
select *
from pipelines."public_orders__current";
```
ClickHouse background merges combine older row versions over time. Before a merge, the base table can contain multiple versions of the same source row. The generated view uses `FINAL` to return the current version and excludes deleted rows.
Pipelines uses at-least-once ingestion into ClickHouse, so recovery can append the same change again. The generated view provides query-time deduplication. Querying the base table without `FINAL` can count repeated versions. See [delivery guarantees](/docs/guides/database/replication/pipelines-faq#can-data-be-processed-more-than-once) for ordering and recovery limits.
Removing deletion markers can make deleted rows reappear when older changes replay. Pipelines does not perform tombstone cleanup. For manual cleanup, follow the [ETL ClickHouse guide](https://github.com/supabase/etl/blob/main/crates/etl-destinations/src/clickhouse/README.md#replacingmergetree-default) to verify that no older changes can replay.
### MergeTree
Query the base table when you need the history of inserts, updates, and deletes. `MergeTree` stores each replicated change as an append-only event. Pipelines adds:
- `cdc_operation`, containing `INSERT`, `UPDATE`, or `DELETE`
- `cdc_lsn`, containing the Postgres commit LSN for the change
- `cdc_tx_ordinal`, containing the change's position within that transaction
The `cdc_operation`, `cdc_lsn`, and `cdc_tx_ordinal` names are reserved and can't be used by source columns.
Inserts and updates append the complete new row. A primary-key value update also appends a `DELETE` for the old key. Deletes append the old row when the source uses `REPLICA IDENTITY FULL`. With primary-key identity, a delete contains the key values; other fields use `NULL` for nullable scalars or placeholders such as zero, empty strings, and empty arrays. Those placeholders are not the deleted row's original values.
Order source changes by `cdc_lsn` and then `cdc_tx_ordinal`. The old-key delete and new-key update from one primary-key value change share both values, so this pair is not a unique destination-row ID.
Retries can append repeated events. ClickHouse's insert-block deduplication can suppress some retries, but replay can group events into different blocks. `MergeTree` history therefore requires deduplication in your queries or downstream processing when each change must count once. Include the operation and row key when distinguishing a primary-key change's delete and update.
Initial-sync rows share baseline ordering metadata. It does not uniquely identify copied rows. Treat the initial snapshot separately from ongoing changes when deduplicating history.
### Truncates and table resets [#truncates-and-table-restarts]
A source `TRUNCATE` truncates the ClickHouse table for either engine, then ongoing replication continues. It does not start a new initial sync.
Resetting a table drops and recreates its table and, for `ReplacingMergeTree`, its generated view. A table reset erases the accumulated destination data and copies existing source rows only if the table is [selected for initial sync](/docs/guides/database/replication/pipelines#choosing-which-tables-to-copy).
## Replica identity and arrays
| Published operation | Required replica identity |
| ------------------------------------------ | --------------------------------------------------------------------------------------- |
| Insert | None; `ReplacingMergeTree` still requires a primary key |
| Update | Primary-key identity or `REPLICA IDENTITY FULL`; updates must contain complete new rows |
| Delete | Primary-key identity or `REPLICA IDENTITY FULL` |
| Delete with `REPLICA IDENTITY USING INDEX` | The index must contain exactly the source primary-key columns |
`REPLICA IDENTITY NOTHING` cannot support updates or deletes.
Array elements can be `NULL`. A whole-array SQL `NULL` is stored as an empty array, so these two source values become indistinguishable.
### Recover from partial updates
Use `REPLICA IDENTITY FULL` for tables whose updates can omit unchanged out-of-line TOAST values. It lets Pipelines reconstruct the complete new row.
Before setting `REPLICA IDENTITY FULL`, check the [column lists](/docs/guides/database/replication/pipelines#selecting-specific-columns) in every publication that sends updates or deletes for this table. Full replica identity requires omitting the list entirely. Remove a list only if all columns can be replicated to the publication's subscribers.
Run the following SQL using the source project's [SQL Editor](/dashboard/project/_/sql) in the Supabase Dashboard or with `psql` over a direct connection. To connect with `psql`, see [Direct connection](/docs/guides/database/connecting-to-postgres#direct-connection).
```sql
alter table public.your_table replica identity full;
```
For a partitioned table, run the statement on the parent and each partition. Postgres applies replica identity per table.
If replication has already failed on a partial update, use [**Reset table**](/docs/guides/database/replication/pipelines-monitoring#restarting-tables) on the affected table. Changing replica identity applies only to new write-ahead log (WAL) records. The reset lets the table resume from a new replication position past the incompatible update.
<Admonition type="danger">
Resetting a table deletes its existing ClickHouse data when replication resumes, including any append-only history in a `MergeTree` table. Include the table in **Initial sync** before resetting if you want to copy its current source rows again.
</Admonition>
## Type mapping
Pipelines creates ClickHouse columns with these mappings:
| Postgres type | ClickHouse type |
| ----------------------------- | ---------------------------- |
| `boolean` | `Bool` |
| `smallint` | `Int16` |
| `integer` | `Int32` |
| `bigint` | `Int64` |
| `real` | `Float32` |
| `double precision` | `Float64` |
| `date` | `Date32` |
| `timestamp without time zone` | `DateTime64(6, 'UTC')` |
| `timestamp with time zone` | `DateTime64(6, 'UTC')` |
| `uuid` | `UUID` |
| `oid` | `UInt32` |
| Other scalar and custom types | `String` |
| Arrays | `Array(Nullable(<element>))` |
Nullable scalar columns are wrapped in `Nullable(...)`. Character, text, `numeric`, `money`, JSON, `time`, `interval`, binary, bit-string, enum, and unsupported custom values are serialized into `String` columns rather than stored as native ClickHouse types.
Postgres `timestamp` values keep their source wall-clock value in the UTC column. Infinite dates and timestamps are rejected. Keep finite dates and timestamps within ClickHouse's supported range, from 1900 through 2299; values outside it can read back at the range limits.
## Schema change support
Pipelines supports:
- Adding columns
- Renaming or dropping columns; nested subcolumns must stay under the same parent when renamed
- Dropping `NOT NULL` from an existing scalar column
- Adding, changing, or removing supported column defaults
- Adding or removing published columns on tracked tables, subject to the same restrictions
With `ReplacingMergeTree`, renaming or dropping a primary-key column or changing the key's columns or their order is rejected.
New scalar columns are made nullable when ClickHouse needs a value for historical rows and the source default cannot be represented safely. Adding `NOT NULL` keeps an existing destination column nullable. Defaults that cannot be translated are skipped; Postgres still supplies the source values through replication.
A change that requires a different ClickHouse type or switches between `timestamp` and `timestamptz` stops replication for the whole pipeline. Other changes that keep the same ClickHouse column type can continue. Use [**Reset table**](/docs/guides/database/replication/pipelines-monitoring#restarting-tables) to rebuild the affected table.
## Troubleshooting
| Issue | Resolution |
| ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------- |
| URL or connection fails | Use a reachable public HTTPS endpoint with the correct port and credentials. Private endpoints are unsupported. |
| Validation passes but setup or writes fail | Check [permissions and resource requirements](#prepare-clickhouse-resources), including table ownership and the server version. |
| Updates, deletes, or arrays fail | Check the [source table requirements](#source-table-requirements). |
| A schema change fails | Follow the shared [schema-change behavior and recovery](/docs/guides/database/replication/pipelines#schema-change-support). |
Use [pipeline monitoring](/docs/guides/database/replication/pipelines-monitoring) to inspect errors and include the pipeline ID when [contacting support](/dashboard/support/new).
## Additional resources
- [ClickHouse documentation](https://clickhouse.com/docs)
- [ReplacingMergeTree](https://clickhouse.com/docs/reference/engines/table-engines/mergetree-family/replacingmergetree)
- [Monitor pipeline status](/docs/guides/database/replication/pipelines-monitoring)