Skip to content

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

ClickHouse 4 min read Case study hardMaster

Memory safety for Postgres extensions in C/C++

Postgres's palloc/MemoryContext allocation and setjmp/longjmp-based error handling clash with C++ new/delete, destructors and exceptions: longjmp skips destructors (undefined behavior), exceptions bypass PG_CATCH, and an uncaught exception aborts the process and forces cluster-wide crash recovery. Four ClickHouse extensions each take a different approach. pg_clickhouse replaces C++ entirely with a new C library (clickhouse-c), and pg_re2 confines C++ to a wrapper that never calls Postgres. pg_chdb moves libchdb into a separate forked helper executable so its crashes don't trigger recovery. pg_stat_ch routes std::terminate through ereport(FATAL) in a background worker, which is a mitigation rather than true isolation, and plans to move to the helper-process model.

Related posts

  • What else runs on your Postgres server, and how do we stop it from taking the database down?

    ClickHouse mediumAdept

    Supporting processes on a managed Postgres VM (PgBouncer, WAL-G, Prometheus, exporters, log collector, watchdogs) share memory, CPU and disk with the database, so a leak or CPU burst in any of them can take it down. ClickHouse Managed Postgres confines them to a cgroup v2 slice using systemd MemoryHigh/MemoryMax, with per-service GOMEMLIMIT values summing below memory.high. Go's GC reacts first, the kernel throttles at memory.high, and any OOM at memory.max is scoped away from Postgres, which runs in a separate cgroup. Backups get reduced CPU weight, WAL-G buffers and log-collector memory are bounded, exported metrics are allowlisted, and a disk-full watchdog kills application sessions while exempting replication and monitoring roles.

  • Why we rewrote WAL-G for Postgres backups in Rust: Meet WAL-RUS

    ClickHouse mediumAdept

    WAL-RUS is an open-source Rust rewrite of WAL-G's Postgres backup and WAL archival tooling, built because Go's garbage-collected runtime made virtual memory hard to predict in resource-constrained managed Postgres environments where overcommit is disabled. It uses bounded worker pools, a persistent daemon with long-lived object storage connections, and a low-copy streaming read-compress-upload pipeline, while keeping WAL-G's WALG_ config variables and two-way archive compatibility. In a WAL-heavy benchmark with four workers archiving to S3, WAL-RUS peaked below 1 GB of virtual memory versus nearly 2.8 GB for WAL-G (over 70% less), with comparable throughput and CPU. pgBackRest also had tight memory but built a larger backlog under heavy WAL activity. WAL-RUS adds incremental backups based on Postgres 17 WAL summaries and is slated to become the default for ClickHouse Cloud's managed Postgres.

  • A Year of Rust in ClickHouse

    ClickHouse mediumAdept

    ClickHouse added Rust to its C++ codebase as an option for new components rather than a rewrite, starting with BLAKE3, then skim for clickhouse-client history, PRQL as a switchable query dialect, and Databricks' delta-kernel-rs replacing a sketchy C++ Delta Lake implementation. The integration needed hermetic builds with vendored crates and careful C++/Rust wrappers. Panics had to be kept from crashing a multi-tenant server, and MSan required instrumenting Rust with a pinned nightly toolchain. Further friction came from cross-compilation, static OpenSSL linking, enormous Rust symbol names, library conventions for memory accounting and connection pools that don't compose with ClickHouse's, and missing build caching, while CI fuzzing caught crashes in the Rust libraries just as it does in C libraries.

  • Can your Postgres survive a bad query?

    ClickHouse hardMaster

    Postgres's `work_mem` limits memory per plan node per process, not per query, so parallel workers and `hash_mem_multiplier` can push a simple aggregation past 8x `work_mem`. Some structures never spill: the deduplication hashtable behind `WITH RECURSIVE ... UNION` stays in memory for the whole query and can grow unbounded. A benchmark ran 7-23 concurrent copies of such a query (~1 GiB each) against ClickHouse Managed Postgres, Cloud SQL, PlanetScale and RDS. With overcommit disabled, ClickHouse returned clean SQLSTATE 53200 errors and kept the cluster up in every run, while RDS hit the OOM killer and crash recovery from 19 connections and the supervisor-based providers terminated sessions or crashed under load.

  • Why strict memory overcommit matters for Postgres

    ClickHouse mediumAdept

    Under Linux's default overcommit policy, an OOM-killed Postgres backend forces the postmaster to terminate all backends and run crash recovery. Strict overcommit (vm.overcommit_memory=2) instead makes malloc return ENOMEM, so only the offending query fails. ClickHouse Managed Postgres sets an explicit vm.overcommit_kbytes of 80% of non-huge-page memory plus 2GB, roughly 60% of RAM plus 2GB, which leaves memory free to handle the error. On an m7i.2xlarge, the default policy dropped all 20 sessions with 30.2s of downtime, strict overcommit caused one query error and no dropped connections, and pgbench showed no measurable throughput cost.

The quest for software engineering knowledge.

Look:
Layout: