Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

ClickHouse 9 min read How-to mediumAdept

Postgres to ClickHouse: Data Modeling Tips

Postgres tables replicated to ClickHouse via PeerDB land in ReplacingMergeTree tables where updates and deletes arrive as versioned inserts (_peerdb_version, _peerdb_is_deleted), so duplicates persist until background merges run. Deduplicate at query time with FINAL, argMax, ROW_NUMBER window functions or views wrapping them, and hide deleted rows with row policies. Pick ordering keys from frequently filtered columns in ascending cardinality, keep PRIMARY KEY a prefix of ORDER BY, and remember ClickHouse's primary key is a non-unique sparse index rather than a B-tree. Columns need explicit Nullable types, and ordering keys can be changed via materialized views or by predefining and swapping target tables.

Assumes: PostgreSQL basicsSQLchange data capture concepts

Related posts

  • Postgres to ClickHouse: Data Modeling Tips V2

    ClickHouse mediumAdept

    Postgres CDC into ClickHouse (via PeerDB or ClickPipes) uses logical decoding and maps tables to ReplacingMergeTree. Updates and deletes arrive as versioned inserts, so queries can see duplicate or deleted rows until background merges run. The guide shows several ways to get Postgres-consistent results: the FINAL modifier or setting with _peerdb_is_deleted filters, row policies, views, refreshable materialized views, and tuning force-merge settings. It also covers custom ordering keys, recommending immutable columns like tenant_id and setting REPLICA IDENTITY to include them, plus JOIN optimization and denormalization.

  • Change Data Capture (CDC) with PostgreSQL and ClickHouse - Part 2

    ClickHouse mediumAdept

    A Postgres-to-ClickHouse CDC pipeline is built with Debezium, Kafka Connect and a ClickHouse materialized view, keeping a ReplacingMergeTree table in sync with the 28M-row UK property price dataset under a mix of inserts, updates and deletes. Key configuration choices include REPLICA IDENTITY FULL (required for delete support), the pgoutput logical decoding plugin, per-row ordering via a single or hash-partitioned Kafka topic, and version/deleted columns in the ReplacingMergeTree engine. The ORDER BY is chosen for analytical access patterns, with the Postgres id appended last for uniqueness. The initial load uses the postgresql table function via INSERT INTO SELECT, which loads all 28M rows in about 80 seconds without tuning but requires pausing writes; Debezium's consistent snapshot is the alternative.

  • Change Data Capture (CDC) with PostgreSQL and ClickHouse - Part 1

    ClickHouse mediumAdept

    Postgres-to-ClickHouse CDC can be built by streaming row changes from Postgres logical decoding (pgoutput plugin, replication slots, LSN ordering) through Debezium and Kafka, then applying them in ClickHouse with the ReplacingMergeTree engine. Updates and deletes become immutable inserts carrying a version column and a deleted flag, and background merges collapse duplicates. Correct results before a merge require FINAL at query time. Consumers must tolerate duplicate events after restarts, and deleted rows are only purged when clean_deleted_rows is set to Always, a setting with a known bug as of 23.5. The post also compares pull-based and push-based CDC and explains why a Kafka queue protects the Postgres WAL from growing when ClickHouse is unavailable.

  • PostgreSQL + ClickHouse as the Open Source unified data stack

    ClickHouse easyNovice

    An open source stack pairs PostgreSQL as the transactional system of record with ClickHouse for analytics, connected by PeerDB for change data capture and the pg_clickhouse extension, which transparently offloads analytical queries from Postgres to ClickHouse. Two patterns are described: CDC replication for operational analytics on application data, and split-writes for high-volume logs, metrics and events that never need to live in Postgres. Adopting it involves creating a ClickHouse database, replicating via PeerDB, configuring pg_clickhouse and pointing queries at its tables, so application changes stay minimal. A managed version in ClickHouse Cloud swaps PeerDB for ClickPipes and preinstalls the extension.

  • Postgres CDC in ClickHouse, A year in review

    ClickHouse mediumAdept

    A year after PeerDB's acquisition, Postgres CDC into ClickHouse via ClickPipes has grown to 400+ companies replicating over 200 TB per month, driven by real-time customer-facing analytics and data warehousing workloads. Key engineering changes include keeping logical replication connections open, because reconnects re-read WAL from restart_lsn and spiked lag. CTID block-based partitioning cut initial-load partition generation from 7+ hours to under a second, and 50+ pre-flight checks plus bucketed user-facing alerts cut on-call load by orders of magnitude. The biggest remaining gap is data modeling overhead from ReplacingMergeTree's asynchronous deduplication, which ClickHouse plans to address with lightweight UPDATEs and unique indexes.

The quest for software engineering knowledge.

Look:
Layout: