Why strict memory overcommit matters for Postgres
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.
Related posts
- 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.
- How we configure huge pages in ClickHouse Managed Postgres
ClickHouse mediumAdept
ClickHouse Managed Postgres runs every server's shared_buffers on 2MB huge pages, which avoids per-backend page-table bloat and improves TLB reach. The setup has three parts. A quarter of RAM is reserved as huge pages before Postgres starts, after dropping caches and compacting memory. Postgres is pinned to huge_pages='on' so it fails at boot instead of silently falling back to 4KB pages. shared_buffers is sized in two passes using `postgres -C shared_memory_size` so the full segment, including roughly 750MB of overhead, fits the pool. On an r7i.4xlarge with 32GB shared_buffers, page tables grew 31MB per connection with 4KB pages (6.1GB at 200 connections) versus 111MB total with huge pages, and select-only pgbench throughput rose 12%.
- 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.
- Incident Report: May 4th, 2024
Railway mediumAdeptA single self-service customer workload deployed multiple memory-hungry replicas into Railway's smallest region, Asia-Southeast, and exhausted its memory. The kernel OOM reaper then killed the Dataplane instance manager on every compute node, and a later redeploy packed all replicas onto one node, locking up the hypervisor. The root cause was overcommitment: the sum of per-container limits exceeded physical RAM, so system memory pressure hit 99% without any container breaching its own limit and triggering eviction. Fixes include reprioritizing the OOM killer to evict user workloads before system daemons, evicting or migrating workloads on node-level memory pressure, scheduling on historical usage, adding capacity, and committing to status updates at least every 30 minutes.
- What is WAL backpressure, and why does ClickHouse Managed Postgres need it?
ClickHouse hardMaster
ClickHouse Managed Postgres prevents WAL-full PANICs, which happen when writes outpace archiving to object storage, by throttling client write bandwidth with the cgroup v2 I/O controller. A systemd timer counts pending WAL segments every 15 seconds and applies tiered write caps of 80%, 50% and 20% of provisioned disk throughput at 100, 500 and 1,000 segments. Processes are sorted by process title into an immune cgroup (archiver, checkpointer, WAL writer, logger) and a throttled one (client backends), and reads are never capped. In a pgbench test with a rate-limited archive_command, the backlog peaked at 3,433 segments (53 GiB), the disk never passed 33% full, and the backlog drained in under four minutes once storage recovered. One caveat: WAL-heavy workloads see throughput cut much more than WAL production, which fell only about 10%.