---
title: "Memory safety for Postgres extensions in C/C++"
description: "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."
url: "https://swe.quest/articles/memory-safety-for-postgres-extensions-in-c-c-b6883b8eb3b1e33d"
canonical: "https://clickhouse.com/blog/memory-safety-postgres-extensions-c-cpp"
source: "ClickHouse"
published: "2026-09-30"
difficulty: "hard"
kind: "Case study"
reading_minutes: 4
tags: ["postgres","memory-safety","c-cpp","memory-management"]
---

# Memory safety for Postgres extensions in C/C++

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2026-09-30, 4 min read, Case study. Original: [https://clickhouse.com/blog/memory-safety-postgres-extensions-c-cpp](https://clickhouse.com/blog/memory-safety-postgres-extensions-c-cpp)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Memory safety](https://swe.quest/tags/memory-safety.md) (Security)
- [C & C++](https://swe.quest/tags/c-cpp.md) (Languages)
- [Memory management](https://swe.quest/tags/memory-management.md) (Compilers & runtimes)

## Prerequisites

- Postgres extension development ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- C++ exceptions and RAII ([easy posts on C & C++](https://swe.quest/tags/c-cpp.md?difficulty=easy))
- setjmp/longjmp

## Keywords

pg\_clickhouse, pg\_re2, pg\_chdb, pg\_stat\_ch, MemoryContext, clickhouse-c, libchdb, RE2

## Related posts

- [What else runs on your Postgres server, and how do we stop it from taking the database down?](https://swe.quest/articles/what-else-runs-on-your-postgres-server-and-how-do-we-stop-it-from-taking-the-79e9559393b71ab0.md): ClickHouse · 2026-08-21 · Medium · 4 min read · PostgreSQL, Linux, Memory management, Resilience, Multi-tenancy
  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.
- [Why we rewrote WAL-G for Postgres backups in Rust: Meet WAL-RUS](https://swe.quest/articles/why-we-rewrote-wal-g-for-postgres-backups-in-rust-meet-wal-rus-a99cc2a7ede61ae3.md): ClickHouse · 2026-06-25 · Medium · 4 min read · PostgreSQL, Rust, Memory management, Storage systems, Go
  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.
- [A Year of Rust in ClickHouse](https://swe.quest/articles/a-year-of-rust-in-clickhouse-fadc1c7501c80398.md): ClickHouse · 2025-04-01 · Medium · 12 min read · Rust, C & C++, Build systems, Testing, Supply chain security
  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.
- [Can your Postgres survive a bad query?](https://swe.quest/articles/can-your-postgres-survive-a-bad-query-744ea9a22b65710c.md): ClickHouse · 2026-09-28 · Hard · 13 min read · PostgreSQL, Memory management, Resilience, Query engines, Linux
  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\`.
- [Why strict memory overcommit matters for Postgres](https://swe.quest/articles/why-strict-memory-overcommit-matters-for-postgres-b9d0f67bf7c921e4.md): ClickHouse · 2026-07-28 · Medium · 7 min read · PostgreSQL, Linux, Memory management, Resilience
  Under Linux's default overcommit policy, an OOM-killed Postgres backend forces the postmaster to terminate all backends and run crash recovery.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
