WalShadow:从物理 WAL 实现 PostgreSQL 到 ClickHouse 的亚秒级复制
WalShadow: Sub-second Postgres replication to ClickHouse from physical WAL

原始链接: https://clickhouse.com/blog/introducing-walshadow

ClickHouse 推出了 **WalShadow**,这是一个开源引擎,可实现从 PostgreSQL 到 ClickHouse 的高性能实时复制。与依赖资源密集型逻辑复制的传统 CDC 工具不同,WalShadow 直接读取物理预写日志(WAL)。 通过在源数据库外部解码物理 WAL 流并将其转换为 ClickHouse 原生数据块,WalShadow 实现了与 Postgres 物理备机相当的性能。在基准测试中,它维持了每秒 28.9 万行的处理速度,从提交到可见的延迟约为 200 毫秒,较逻辑复制方法有显著提升。 主要功能包括: * **高效性**:无需逻辑复制槽,最大限度地减少了对源 Postgres 实例的开销。 * **弹性**:处理完整生命周期,包括模式演变、重启恢复和并行处理。 * **正确性**:使用 WAL 位置 (`_lsn`) 确保数据完整性,即使在并行插入的情况下也是如此。 WalShadow 现已在 GitHub 上发布。此外,ClickHouse Managed Postgres 也已开放私有预览,该工具已原生集成到其复制层中。此次发布是 ClickHouse 实现为事务和分析工作负载提供统一、低延迟技术栈这一目标的重要一步。

ClickHouse 引入了 **WalShadow**,这是一款通过直接读取物理预写日志(WAL)来实现从 Postgres 到 ClickHouse 次秒级复制的工具。 与依赖 `wal_level = logical` 的传统方法不同,WalShadow 使用的是 `wal_level = replica`。这一架构选择避开了逻辑复制带来的运维负担,例如槽位管理开销、重排序缓冲以及性能下降等问题。开发人员认为,逻辑复制是针对 Postgres 到 Postgres 的使用场景进行了优化,而 WalShadow 则是专门为简化数据导入 ClickHouse 而构建的。 该项目突显了 AI 编程助手在日益重要的角色,它们帮助开发人员更高效地处理复杂的底层协议细节。然而,由于该工具依赖于对物理 WAL 的直接访问,其兼容性受到限制;目前它仅适用于自托管的 Postgres 或 ClickHouse 的托管服务,因为大多数第三方托管服务商不会授予客户访问底层 WAL 文件的必要权限。团队计划与 Postgres 核心社区分享他们的技术成果,以期在未来改善原生的逻辑解码功能。
相关文章

原文

Today, we’re announcing WalShadow, an open-source engine that replicates Postgres data to ClickHouse directly from physical WAL.

In our benchmarks, transactions committed in Postgres became visible in ClickHouse in around 200 ms, while WalShadow sustained 289K rows/sec, effectively keeping pace with the source Postgres instance.

Unlike traditional CDC based systems, WalShadow doesn’t use Postgres logical replication. It consumes the same physical WAL stream used by Postgres replicas, decodes it outside the source database, and writes ClickHouse-native blocks directly into ClickHouse. The result is a replication architecture that gets close to the latency and throughput of a Postgres physical standby, while making the data immediately available for analytics in ClickHouse.

WalShadow supports the complete replication lifecycle, including initial load, continuous replication, schema evolution, restart recovery, and planned source switchovers.

By consuming physical WAL directly, WalShadow eliminates the need for logical replication slots, removes much of the operational overhead associated with logical replication, and significantly reduces resource consumption on the source Postgres instance. It also supports complex schema changes such as ADD COLUMN, RENAME COLUMN, DROP COLUMN, and CREATE TABLE.

WalShadow is fully open source and available today on GitHub.

For a fully managed experience, we’re also launching WalShadow in private preview for ClickHouse Managed Postgres.

Physical WAL is key to WalShadow’s architecture, but most managed Postgres services don’t expose it to customers, making it impossible to use WalShadow. ClickHouse Managed Postgres manages both sides of the stack, allowing us to integrate WalShadow directly into the Postgres replication layer and provide a native path from Postgres WAL to ClickHouse.

Sign up for the private preview of WalShadow on ClickHouse Managed Postgres.

WalShadow Schema Decoder Clickhouse.png

WalShadow takes a new approach to Postgres-to-ClickHouse replication, effectively turning ClickHouse into an analytical physical standby.

WalShadow consumes the same WAL stream Postgres generates for physical replication and recovery. Instead of asking the source database to decode changes into logical events, WalShadow processes the WAL outside the source through four stages:

  1. Track the live schema. WalShadow filters catalog WAL records and replays them into a schema-only shadow Postgres instance. This maintains an up-to-date catalog of tables, columns, and Postgres types as the source schema evolves.
  2. Decode data changes in parallel. Heap records are distributed across a pool of Rust-based decoders without passing through the shadow Postgres instance.
  3. Build ClickHouse-native blocks. A batcher groups decoded rows by table into complete ClickHouse-native blocks, preserving data fidelity without intermediate format conversions.
  4. Insert in parallel. A separate pool of inserters writes multiple blocks to ClickHouse concurrently, allowing decoding and insertion to scale independently.

This creates a direct path from Postgres to ClickHouse: no logical decoding output plugin, no Kafka, and no JSON serialization or separate normalization step.

Because WalShadow processes blocks in parallel, they may arrive in ClickHouse out of order. WalShadow preserves correctness by attaching the source WAL position (_lsn) to every row, allowing ClickHouse to retain the latest version for each key. Operations that require strict ordering, such as schema changes and truncations, introduce barriers that wait for all preceding data to become durable before they are applied.

The source only needs to ship physical WAL, resulting in a load profile similar to a physical standby while allowing changes to reach ClickHouse within a second.

For a detailed overview of architecture, see the architecture documentation. To understand various available tuning settings affecting performance and functionality, see the configuration guide.

We benchmarked WalShadow against PeerDB, our state-of-the-art Postgres-to-ClickHouse CDC tool powered by logical replication which powers ClickPipes.

For me, this comparison is bittersweet. We’re proud that PeerDB, which we created, serves thousands of customers. WalShadow carries that journey forward by reimagining replication directly from physical WAL and moving us closer to a unified Postgres and ClickHouse stack.

The benchmark replicated a continuous stream of changes from a single table, with Postgres, ClickHouse, and each replication tool running in the same region. We used modest c8i.2xlarge instances with 8 vCPUs each. Performance will vary by workload, but these results offer a useful indication of what WalShadow can offer.

Commit-to-visible latency measures the time from a transaction committing in Postgres to its rows becoming visible in ClickHouse. A native Postgres physical replica established a practical baseline of around 50 ms. WalShadow achieved approximately 200 ms, compared with around 10 seconds for PeerDB, about 50x lower latency in this benchmark.

The source Postgres instance sustained approximately 290,000 inserted rows per second, establishing the maximum rate the replication pipeline could process. WalShadow replicated 289,000 rows per second, effectively matching the source without becoming the bottleneck. PeerDB sustained approximately 120,000 rows per second, or around 40% of the source rate.

These results show that low latency does not have to come at the cost of throughput: WalShadow keeps data real-time while operating at nearly the full speed of the source.

At ClickHouse, we have taken several major steps to bring Postgres and ClickHouse closer together: acquisition of PeerDB, launching Postgres CDC in ClickPipes, and introducing ClickHouse Managed Postgres, an enterprise-grade Postgres service built on local NVMe storage for fast OLTP and natively integrated with ClickHouse for fast OLAP.

These efforts share one goal: to give developers a unified data stack that combines Postgres for transactions and ClickHouse for analytics, without added complexity.

WalShadow represents a major milestone toward that vision. By replicating directly from physical WAL into ClickHouse-native blocks, it delivers sub-second analytics, removes much of the operational overhead of logical replication, and supports advanced schema changes that are difficult to handle through conventional logical-decoding pipelines.

Over the coming months, we will work closely with customers to harden WalShadow across real-world workloads. We are already collaborating with an initial group of design partners and are now ready to expand access.

Sign up for the private preview, and we’ll get you access within a day or two.

联系我们 contact @ memedata.com