Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for Postgres Difficulty: Intermediate ClaudeChatGPTCursor

Postgres Memory Sizing (shared_buffers & work_mem) Prompt

Size shared_buffers, work_mem, maintenance_work_mem and effective_cache_size from your actual RAM, connection count, and cache-hit evidence — not the '25% of RAM' rule of thumb that quietly causes OOM or disk spills.

Target user
DBAs and platform engineers sizing Postgres memory for a specific server
Difficulty
Intermediate
Tools
Claude, ChatGPT, Cursor

The prompt

You are a senior PostgreSQL DBA who sizes memory from evidence and arithmetic, not from
the "25% of RAM" meme. You know work_mem is per-operation-per-connection and is the most
common cause of Postgres OOM, and you always leave headroom for the OS page cache.

I will provide:
- Total server RAM and what else runs on the box (dedicated DB or shared): [RAM / TENANCY]
- max_connections and the real peak concurrent active connections (and whether a pooler
  like PgBouncer sits in front): [CONNECTIONS]
- Working-set size / total database size: [DB SIZE]
- Workload type (OLTP, analytics/reporting, mixed): [WORKLOAD]
- Current settings: shared_buffers, work_mem, maintenance_work_mem, effective_cache_size,
  and any cache-hit ratio from pg_statio_user_tables / pg_stat_database: [CURRENT]
- Postgres version: [VERSION]

Work through this in order:

1. **shared_buffers**: recommend a value with reasoning tied to RAM, workload, and whether
   the working set fits. Explain that Postgres relies on the OS page cache too, so you do
   NOT want shared_buffers to consume most of RAM, and how the buffer cache hit ratio
   tells you if it's undersized. Note the double-buffering nuance.

2. **work_mem — the danger knob.** Compute the realistic worst case:
   work_mem x (sorts/hashes per query) x peak concurrent queries x parallel workers. Show
   the arithmetic so I can see how a "generous" work_mem plus high concurrency multiplies
   into more than total RAM. Recommend a conservative global value and per-role or
   per-session overrides for the few heavy analytical queries that need more.

3. **maintenance_work_mem**: size it for VACUUM, CREATE INDEX, and ALTER TABLE — it can be
   much larger than work_mem because few run concurrently — and note autovacuum_work_mem.

4. **effective_cache_size**: set it to the planner's view of OS cache + shared_buffers
   (a hint, not an allocation) so index scans are costed correctly.

5. **Cross-check for OOM risk**: sum the worst-case allocations against RAM with a margin,
   account for the pooler capping real concurrency, and flag if the current or proposed
   settings can OOM under peak load. Give the pg_stat queries to validate cache-hit ratio
   and spill frequency after the change.

Output: (a) a settings table [param | current | proposed | reasoning]; (b) the work_mem
worst-case arithmetic; (c) the OOM cross-check; (d) the validation queries; (e) one risk.

Guardrails: work_mem is allocated per sort/hash node per connection — model the worst case
(nodes x concurrency x parallel workers) against total RAM before raising it; this is the
#1 cause of Postgres OOM. Change one parameter at a time and validate cache-hit ratio and
spill rate on a replica or in a window before applying to prod. Leave RAM for the OS cache.

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Why this prompt works

Postgres memory sizing is dominated by a single dangerous asymmetry: shared_buffers is a fixed shared allocation, but work_mem is charged per sort or hash node, per connection, and per parallel worker. The infamous OOM happens when someone sets a “reasonable” work_mem, then a burst of concurrent analytical queries each open several hash nodes and the total blows past RAM. This prompt forces the multiplication out into the open, so the number is chosen against a worst case rather than a single-query intuition.

It also resists the “25% of RAM for shared_buffers” cargo cult by tying the recommendation to whether the working set fits and to the actual buffer cache hit ratio, while reserving headroom for the OS page cache that Postgres depends on. Sizing maintenance_work_mem and effective_cache_size in the same pass, plus an explicit OOM cross-check and post-change validation queries, turns four interacting knobs into one coherent, evidence-backed plan.

Related prompts

More Postgres prompts & error guides

Browse every Postgres prompt and troubleshooting guide in one place.

Free download · 368-page PDF

Reading prompts? Get all 500 in one free PDF

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.