---
title: "Can your Postgres survive a bad query?"
description: "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`."
url: "https://swe.quest/articles/can-your-postgres-survive-a-bad-query-744ea9a22b65710c"
canonical: "https://clickhouse.com/blog/can-your-postgres-survive-a-bad-query"
source: "ClickHouse"
published: "2026-09-28"
difficulty: "hard"
kind: "Research"
reading_minutes: 13
tags: ["postgres","memory-management","resilience","query-engines","linux"]
---

# Can your Postgres survive a bad query?

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2026-09-28, 13 min read, Research. Original: [https://clickhouse.com/blog/can-your-postgres-survive-a-bad-query](https://clickhouse.com/blog/can-your-postgres-survive-a-bad-query)

## Summary

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.

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 management](https://swe.quest/tags/memory-management.md) (Compilers & runtimes)
- [Resilience](https://swe.quest/tags/resilience.md) (Distributed systems)
- [Query engines](https://swe.quest/tags/query-engines.md) (Data & databases)
- [Linux](https://swe.quest/tags/linux.md) (Infrastructure)

## Prerequisites

- Postgres EXPLAIN plans ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- work\_mem tuning
- recursive CTEs
- Linux OOM killer ([easy posts on Linux](https://swe.quest/tags/linux.md?difficulty=easy))

## Keywords

PostgreSQL, work\_mem, hash\_mem\_multiplier, ClickHouse Managed Postgres, Amazon RDS, Google Cloud SQL, PlanetScale, OOM killer

## 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.
- [Benchmarking NVMe-backed Managed Postgres: PlanetScale and ClickHouse](https://swe.quest/articles/benchmarking-nvme-backed-managed-postgres-planetscale-and-clickhouse-9c60dc3e275ce910.md): ClickHouse · 2026-07-30 · Medium · 4 min read · PostgreSQL, Performance engineering, Databases, Cloud platforms
  On identical AWS r8gd instances with local NVMe and quorum synchronous replication, ClickHouse Managed Postgres outperformed PlanetScale Metal in a pgbench TPC-B-like workload.
- [PostgresBench: A Reproducible Benchmark for Postgres Services](https://swe.quest/articles/postgresbench-a-reproducible-benchmark-for-postgres-services-3a30213211ea6b71.md): ClickHouse · 2026-04-02 · Medium · 9 min read · PostgreSQL, Performance engineering, Storage systems, Cloud platforms
  PostgresBench is an open, reproducible benchmark for managed Postgres services built on pgbench's TPC-B-like workload (256 clients, 16 threads, 10-minute runs, three runs each) at ~100 GB and ~500 GB scale factors, with default configs, HA disabled and all services in us-east-2.
- [Introducing Postgres Query Insights in ClickHouse Cloud](https://swe.quest/articles/introducing-postgres-query-insights-in-clickhouse-cloud-a9697a827409b984.md): ClickHouse · 2026-05-07 · Medium · 7 min read · PostgreSQL, Observability, Performance engineering, Analytics & OLAP
  Postgres Query Insights, now in preview for ClickHouse Cloud Managed Postgres, ranks every normalized query pattern by impact and aggregates per-execution counters (CPU, cache vs disk reads, temp spills, unlaunched parallel workers, WAL) to explain why a pattern is slow.
- [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)
