PostgreSQL Memory Tuning & PgBouncer Pool Calculator

Interactive PostgreSQL Memory Tuning & PgBouncer Pool Calculator

PostgreSQL 16 docs · work_mem: engineering heuristic
SCENARIO PRESETS:
?Physical RAM of the database server, in GB. Read it from the instance type or from free -g on Linux. Default: 64 GB.
?CPU cores available to PostgreSQL. Read it from nproc on Linux or from the instance type. Use physical cores, not hyperthreads: the PostgreSQL wiki formula counts cores and says not to include HT threads. If your instance reports vCPUs, check whether they are hyperthreads and enter the core count. Range: 2 to 128, step 2.
?max_connections value you plan to set in postgresql.conf. Keep it low and pool application connections through PgBouncer. With PgBouncer in front, enter the server-side max_connections: work_mem is divided by that number. Range: 20 to 2,000, step 20.
Web / SaaS OLTP (High Concurrency)
?OLTP for short transactions from many concurrent users; DW for long analytical queries with large sorts and hash joins. Default: oltp.
SHARED_BUFFERS & EFFECTIVE_CACHE
shared_buffers = 16 GB · effective_cache_size = 48 GB
WORK_MEM STARTING ESTIMATE, DIRECT CONNECTIONS
work_mem = 20.5 MB per sort/hash node (starting estimate; the real worst case is larger, see the assumptions)
MAINTENANCE_WORK_MEM (INDEX BUILDS; VACUUM USES AT MOST 1 GB)
maintenance_work_mem = 2048 MB (values above 1,024 MB only help index builds)
STARTING PGBOUNCER TRANSACTION POOL
default_pool_size = 32 active server connections (spindles = 0, active set assumed cached)
POSTGRESQL.CONF MEMORY SETTINGS, DIRECT CONNECTIONS (NO POOLER)
max_connections = 200 shared_buffers = 16GB effective_cache_size = 48GB work_mem = 20972kB maintenance_work_mem = 2GB
POSTGRESQL.CONF MEMORY SETTINGS BEHIND PGBOUNCER (MAX_CONNECTIONS AND WORK_MEM FROM THE POOL)
# behind PgBouncer: pool size + 3 superuser slots max_connections = 35 shared_buffers = 16GB effective_cache_size = 48GB work_mem = 119837kB maintenance_work_mem = 2GB
PGBOUNCER.INI BLOCK, READY TO PASTE
[pgbouncer] pool_mode = transaction default_pool_size = 32 ; default_pool_size is per user/database pair: server max_connections must cover pairs x pool max_client_conn = 200
CALC38 · PostgreSQL Memory Tuning…2048321620.5
Computed in your browser

Use the PostgreSQL Memory Tuning & PgBouncer Pool Calculator to evaluate shared_buffers = 0.25 · RAM | work_mem = ((RAM − shared_buffers) · 0.25) / (N_conn · m), with m = 3 OLTP and 1.5 DW for AWS RDS / Aurora PostgreSQL instances, Supabase dedicated databases. With the defaults (Total Server RAM = 64 GB RAM, Allocated CPU Cores = 16 vCPUs, Client Connections = 200) it returns SHARED_BUFFERS & EFFECTIVE_CACHE: shared_buffers = 16 GB · effective_cache_size = 48 GB. Change any input and the results update from the same equation.

Governing equationshared_buffers = 0.25 · RAM | work_mem = ((RAM − shared_buffers) · 0.25) / (N_conn · m), with m = 3 OLTP and 1.5 DW (PostgreSQL 16 resource docs (shared_buffers 25 %, maintenance_work_mem default 64 MB, effective_cache_size default 4 GB); work_mem split is an engineering heuristic)

Model assumptions

work_mem applies per sort or hash node: hash nodes get work_mem × hash_mem_multiplier (default 2.0), and each parallel worker has its own allowance, so the real worst case is larger than the reserve drawn. Enter the client connections your applications open. Above 100 (a planning threshold) a second postgresql.conf block sets max_connections to the pool plus 3 superuser slots and divides work_mem by that number. maintenance_work_mem is capped at 2,048 MB here for index builds; VACUUM uses at most 1 GB of it, so values above 1,024 MB only help index builds. The two blocks reuse the values above. pool_mode = transaction breaks session features such as SET, LISTEN and session advisory locks, and max_client_conn stays at the PgBouncer default of 100 because the documentation gives no sizing rule for it.

Computed in your browser. The calculator, the tables and the worked examples run the same equations.

Why Default PostgreSQL Settings Waste Large Servers

PostgreSQL ships with conservative defaults intended for small machines, so a large server needs explicit settings. The planner estimates how much disk cache a query can use from effective_cache_size, whose default is 4 GB, and that parameter allocates no memory (postgresql.org/docs/16/runtime-config-query.html, checked November 2026). Sorts and hashes larger than work_mem spill to temporary files, and oversized work_mem values across many connections can exhaust RAM.

shared_buffers is allocated once at startup. The PostgreSQL 16 documentation suggests 25 % of memory as a reasonable start on a dedicated server and notes that more than 40 % rarely helps. work_mem is allocated per sort or hash operation, and the documentation warns that total memory use can be many times work_mem because several operations and sessions run at once.

PostgreSQL 16 Memory Configuration Matrix Across Server RAM Tiers (200 Connections, OLTP) — Computed via shared_buffers = 0.25 · RAM | work_mem = ((RAM − shared_buffers) · 0.25) / (N_conn · m), with m = 3 OLTP and 1.5 DW (PostgreSQL 16 resource docs (shared_buffers 25 %, maintenance_work_mem default 64 MB, effective_cache_size default 4 GB); work_mem split is an engineering heuristic) · Sources checked November 2026
Server RAMvCPUsshared_bufferseffective_cache_sizework_mem estimatemaintenance_work_mem
8 GB2 vCPU2.0 GB6.0 GB2.6 MB410 MB
16 GB4 vCPU4.0 GB12.0 GB5.1 MB819 MB
32 GB8 vCPU8.0 GB24.0 GB10.2 MB1638 MB
64 GB16 vCPU16.0 GB48.0 GB20.5 MB2048 MB
128 GB32 vCPU32.0 GB96.0 GB41.0 MB2048 MB
256 GB64 vCPU64.0 GB192.0 GB81.9 MB2048 MB

The Calculator's Memory Rules and Worked Values

The calculator sets shared_buffers = 0.25 x RAM and maintenance_work_mem = min(2,048 MB, 0.05 x RAM), against the documented default of 64 MB. It sets effective_cache_size = 0.75 x RAM by convention; this is a planner estimate, not a PostgreSQL default. Work memory is work_mem = (RAM - shared_buffers) x 0.25 / (max_connections x m), where m is 3 for OLTP and 1.5 for DW. The PostgreSQL documentation sets the work_mem default at 4 MB and gives no formula for choosing it from RAM or connection count, so the multiplier and the 0.25 share are the calculator's own engineering heuristics: a starting estimate that reserves part of the remaining RAM for concurrent sorts, not a documented safe value. The result is not clamped, so on a small server with many connections it can fall below the 4 MB default, which is more conservative than the default. For maintenance_work_mem, VACUUM uses at most 1 GB (postgresql.org/docs/16/runtime-config-resource.html).

At the default 64 GB, 200 connections and OLTP, the calculator returns shared_buffers 16.0 GB, effective_cache_size 48.0 GB, work_mem 20.5 MB ((64 - 16) x 1024 x 0.25 / (200 x 3)) and maintenance_work_mem 2,048 MB. Across all connections that is a reserve of 20.5 x 200 x 3 / 1,024 = 12.0 GB. At 500 connections work_mem falls to 8.2 MB, and on a 4 GB server with 200 connections to 1.3 MB: connection count matters more than most settings. The reserve is a floor, not a ceiling: hash nodes and parallel workers can use more (see the assumptions).

work_mem Starting Estimate (MB) on a 64 GB Server vs Client Connections (per sort/hash node; the real worst case is larger) — Computed via shared_buffers = 0.25 · RAM | work_mem = ((RAM − shared_buffers) · 0.25) / (N_conn · m), with m = 3 OLTP and 1.5 DW (PostgreSQL 16 resource docs (shared_buffers 25 %, maintenance_work_mem default 64 MB, effective_cache_size default 4 GB); work_mem split is an engineering heuristic) · Sources checked November 2026
max_connectionsOLTP work_mem (MB)Analytics DW work_mem (MB)Recommended Architecture
50 conns81.9 MB163.8 MBDirect OK
100 conns41.0 MB81.9 MBDirect OK
200 conns20.5 MB41.0 MBUse PgBouncer Transaction Pool
400 conns10.2 MB20.5 MBUse PgBouncer Transaction Pool
800 conns5.1 MB10.2 MBUse PgBouncer Transaction Pool
1500 conns2.7 MB5.5 MBUse PgBouncer Transaction Pool

Worked Examples: Two Presets Calculated with shared_buffers = 0.25 · RAM | work_mem = ((RAM − shared_buffers) · 0.25) / (N_conn · m), with m = 3 OLTP and 1.5 DW

Worked Example 1 (Small SaaS, 16 GB, 4 vCPU, 100 connections): This preset sets Total Server RAM = 16 GB RAM, Allocated CPU Cores = 4 vCPUs, Client Connections = 100, Primary Database Workload Profile = Web / SaaS OLTP. Evaluating shared_buffers = 0.25 · RAM | work_mem = ((RAM − shared_buffers) · 0.25) / (N_conn · m), with m = 3 OLTP and 1.5 DW gives: shared_buffers = 4.0 GB, effective_cache_size = 12.0 GB, work_mem = 10.2 MB, maintenance_work_mem = 819.0 MB, PgBouncer default_pool_size = 8. Related in the Cloud, API, LLM & Automation Hub: Webhook Queue & Worker Concurrency Calculator (Little's Law) and Redis & Valkey Memory Sizing & Overhead Calculator.

Worked Example 2 (Analytics warehouse, 128 GB, 32 vCPU, 100 connections): This preset shifts the operating inputs to Total Server RAM = 128 GB RAM, Allocated CPU Cores = 32 vCPUs, Client Connections = 100, Primary Database Workload Profile = Analytics / Data Warehouse to size an analytics warehouse: each sort or hash node may use 163.8 MB. For contrast, a hypothetical server with 500 connections and 128 MB of work_mem could commit 500 x 128 MB = 62.5 GiB with a single sort node per connection and risk the Linux OOM killer; that is not this preset (100 connections, 163.8 MB). The calculator returns: shared_buffers = 32.0 GB, effective_cache_size = 96.0 GB, work_mem = 163.8 MB, maintenance_work_mem = 2048.0 MB, PgBouncer default_pool_size = 64. You can compare these limits with other calculators in the Engineering Studio.

How to Evaluate Applying the Settings in Production?

PgBouncer in transaction mode multiplexes many client connections onto fewer server connections. The PostgreSQL wiki suggests about (core_count x 2) + effective_spindle_count active connections (wiki.postgresql.org, Number of Database Connections). The calculator sets the spindle count to 0, which assumes the active data is fully cached, so 16 vCPUs give 32 server connections; count physical cores, not hyperthreads. The pool cannot exceed the connection slots left after the default superuser_reserved_connections of 3 (postgresql.org/docs/16/runtime-config-connection.html, checked November 2026), so 128 vCPUs with max_connections = 200 give 197, not 256, and the result says the cap applied. The paste-ready blocks reuse these values: postgresql.conf gets the memory settings (above 100 clients a second block behind PgBouncer sets max_connections to the pool plus 3 and divides work_mem by it: 117 MB at the default, against 20.5 MB with 200 direct connections) and pgbouncer.ini gets pool_mode = transaction and default_pool_size (pgbouncer.org/config.html). Transaction mode breaks session features such as SET, LISTEN and session-level advisory locks (pgbouncer.org/features.html). max_client_conn has no sizing rule in the documentation (default 100), so the block repeats the client count you entered.

Sources

  1. PostgreSQL 16 · Resource consumptionshared_buffers: about 25 % of RAM as a starting point on a dedicated server; above 40 % rarely helps. work_mem: default 4 MB, and total use can be many times work_mem; the page gives no formula for choosing it from RAM or connection count, so the work_mem split in the calculator is an engineering heuristic. maintenance_work_mem defaults to 64 MB, and VACUUM can use at most 1 GB of it for the collection of dead tuple identifiers. hash_mem_multiplier defaults to 2.0, so hash operations may use work_mem times that value, and each parallel worker has its own work_mem allowance. Basis of the shared_buffers and maintenance rules and of the 4 MB reference for work_mem.
  2. PostgreSQL 16 · Query planning (effective_cache_size)effective_cache_size is the planner's estimate of disk cache available to one query; default 4 GB; it allocates no memory. Basis of the note that the 75 % value is a convention.
  3. PostgreSQL Wiki · Number of database connectionsActive connections near (core_count × 2) + effective_spindle_count. Basis of the PgBouncer pool formula with spindle count 0 (fully cached data). The page says the core count should not include HT threads, even if hyperthreading is enabled.
  4. PgBouncer · Configurationdefault_pool_size (default 20) is the maximum number of server connections per user/database pair. pool_mode (session by default, transaction, statement): in transaction mode the server connection returns to the pool when the transaction ends. max_client_conn defaults to 100 and the page gives no rule for choosing it, so the calculator does not compute it. Basis of what the pool output means and of the pgbouncer.ini block.
  5. PostgreSQL 16 · Connections and authentication (superuser_reserved_connections)superuser_reserved_connections defaults to three connection slots held back for superusers; ordinary roles can use at most max_connections minus that reserve. Basis of the cap on the PgBouncer pool size (checked November 2026).
  6. PgBouncer · Features (pooling modes)Transaction pooling assigns a server connection to a client only during a transaction and breaks session-based features: SET/RESET, LISTEN, WITH HOLD cursors, SQL-level PREPARE / DEALLOCATE and session-level advisory locks. It can be used only when the application avoids them. Basis of the pool_mode note.
  7. PostgreSQL 16 · Setting parameters (memory units)Valid memory units are B, kB, MB, GB and TB with a multiplier of 1024; parameters are written one per line as name = value, and # starts a comment. Basis of the units in the postgresql.conf block.

Method and test suite: Calculation Methodology.

Frequently Asked Questions: PostgreSQL Memory Tuning & PgBouncer Pool Calculator

Why not set shared_buffers to 40 % of RAM?

The PostgreSQL documentation says that more than 40 % rarely helps, and it suggests about 25 % as the starting point on a dedicated server.

Why does the calculator set the spindle count to zero?

The spindle term in the wiki formula reflects disk waits. With the active data fully cached it drops out. On disk-bound servers, add the effective spindle count to the pool size.

Why does the DW profile get a higher work_mem?

The multiplier m is an assumption of this model, not a PostgreSQL standard or a documented value. It stands for the sort or hash nodes one query may run at the same time, and the model uses 3 for OLTP and 1.5 for DW. The DW profile divides the same memory pool by a smaller m, so each node gets a larger limit, which is why the DW preset (128 GB, 100 connections) shows 163.8 MB. Check the real node count of your heaviest queries with EXPLAIN before relying on it.

How do I raise work_mem for one reporting query?

Use SET LOCAL work_mem inside that query's transaction, so the global setting stays conservative. Check the result against your own memory limits before enabling it.