PostgreSQL performance tuning: the settings and checks to start with
Most PostgreSQL tuning is a short list: a handful of memory and WAL settings, autovacuum, connection limits, and then query-level work with pg_stat_statements and EXPLAIN. This page lists each item, what it does, how the docs say to size it, and how to check the result.
45 minutes with an engineer: we review your workload and sketch the design.
- ✓Defaults read from PostgreSQL 19 docs, 4 Dec 2026
- ✓No benchmark figures
Start with measurement, not settings: enable pg_stat_statements, find the queries that use the most total time, and read their EXPLAIN ANALYZE output. Then review shared_buffers, effective_cache_size, work_mem, WAL and checkpoint settings, random_page_cost, autovacuum and max_connections against the PostgreSQL 19 documentation. Defaults and sizing figures below are the docs' own, read 4 Dec 2026.
Customers
Partners
Where should you start when tuning PostgreSQL?
Start by finding what is slow, then change one thing and measure again. Turn on pg_stat_statements to rank queries by total time, run EXPLAIN ANALYZE on the top few, and only then touch server settings. Settings help queries the planner already plans well; they do not rescue a missing index.
The PostgreSQL docs describe EXPLAIN ANALYZE as showing whether the planner's estimates are close to reality, and pg_stat_statements as tracking planning and execution statistics for every statement (EXPLAIN, pg_stat_statements, checked 4 Dec 2026). Those two tools tell you where time goes. The settings below then adjust memory, I/O and background work around that. Change one item at a time and compare the same queries before and after.
Which PostgreSQL settings matter most, and what are their defaults?
Twelve items cover most first reviews: three memory settings, two WAL and checkpoint settings, one planner cost, autovacuum, connections, and the query tools. The table gives each default from the PostgreSQL docs, what it does, and how to check it. Sizing figures appear only where the docs state them.
| Setting or tool | Default | What it does | How to check it |
|---|---|---|---|
| shared_buffers | 128MB | Memory for PostgreSQL's own page cache; set at server start | Cache hit and read counts in pg_stat_database; EXPLAIN (BUFFERS) hit and read |
| effective_cache_size | 4GB | Planner's estimate of cache available to one query; allocates nothing | Plans choosing index or sequential scans as expected |
| work_mem | 4MB | Memory per sort or hash operation before spilling to temporary files | log_temp_files, temp_files in pg_stat_database, temp blocks in EXPLAIN |
| maintenance_work_mem | 64MB | Memory for VACUUM, CREATE INDEX and similar jobs | Duration of index builds and vacuums |
| max_wal_size | 1GB | Soft limit on WAL growth between automatic checkpoints | num_requested vs num_timed in pg_stat_checkpointer |
| checkpoint_timeout | 5min | Maximum time between automatic checkpoints | log_checkpoints, which is on by default |
| random_page_cost | 4.0 | Planner's cost of a non-sequential page fetch | EXPLAIN plans on your real data and storage |
| autovacuum_vacuum_scale_factor | 0.2 | Fraction of the table that must change before autovacuum runs | n_dead_tup and last autovacuum in pg_stat_all_tables |
| max_connections | 100 | Maximum concurrent connections; set at server start | Rows in pg_stat_activity against the limit |
| pg_stat_statements | Off until loaded | Per-statement call counts and time | Query the view, ordered by total_exec_time |
| EXPLAIN (ANALYZE) | Not a setting | Real timings and row counts per plan node | Compare estimated with actual rows |
| Indexes | Not a setting | Fast access paths for filters and joins | idx_scan in pg_stat_all_indexes |
Defaults from the PostgreSQL 19 docs, checked 4 Dec 2026: resource consumption, WAL, query planning, autovacuum, connections, logging and monitoring statistics. Defaults differ by version and by packager, so confirm yours with SHOW.
How much memory should shared_buffers and effective_cache_size get?
The docs give 25% of system memory as a reasonable starting value for shared_buffers on a dedicated server with 1GB or more of RAM, and say more than 40% is unlikely to work better. effective_cache_size is only a planner hint covering shared buffers plus the operating system cache. It does not reserve memory.
The docs add that larger shared_buffers settings usually need a larger max_wal_size, and that PostgreSQL also relies on the operating system cache (resource consumption, checked 4 Dec 2026). For effective_cache_size, they say to consider both shared buffers and the part of the kernel cache used for PostgreSQL data files, and to allow for concurrent queries sharing that space (query planning, checked 4 Dec 2026). To check: run a hot query with EXPLAIN (ANALYZE, BUFFERS) and compare shared hit with shared read. A change that does not move those numbers or the query time did not help.
How do you set work_mem and maintenance_work_mem without running out of memory?
work_mem applies per sort or hash operation, not per connection, so one complex query can use several multiples of it and many sessions can do so at once. Raise it only when queries spill to disk, and prefer raising it for one role or session. maintenance_work_mem can be set much larger than work_mem.
The docs say the total memory used can be many times work_mem, because a query can run several sorts and hashes at once and several sessions can run concurrently. Hash operations use work_mem multiplied by hash_mem_multiplier, which defaults to 2.0 (resource consumption, checked 4 Dec 2026). For maintenance_work_mem the docs call it safe to set significantly larger than work_mem, with the caution that up to autovacuum_max_workers times that amount may be allocated when autovacuum runs, which autovacuum_work_mem can cap separately.
- Find spills: set log_temp_files so temporary files are logged, watch temp_files in pg_stat_database, and read the temp blocks in EXPLAIN (ANALYZE, BUFFERS) output (logging, EXPLAIN, checked 4 Dec 2026).
- Scope the change: ALTER ROLE or SET for the reporting job that needs more, rather than the global default.
- Multiply before you commit: work_mem times operations per query times concurrent sessions must fit in free memory.
How should WAL and checkpoint settings be set?
Watch how checkpoints start before changing anything. If most are requested because WAL reached max_wal_size rather than timed by checkpoint_timeout, the WAL limit is too low for your write rate. Raising either lengthens crash recovery, per the docs, so it is a trade, not a free gain.
The pg_stat_checkpointer view counts scheduled checkpoints (num_timed) and requested ones (num_requested) (monitoring statistics, checked 4 Dec 2026). The docs say max_wal_size is a soft limit with a 1GB default, checkpoint_timeout defaults to five minutes, and increasing either can increase the time needed for crash recovery. checkpoint_completion_target defaults to 0.9, and the docs advise against reducing it (WAL settings, checked 4 Dec 2026). wal_buffers defaults to -1, which sizes it at 1/32nd of shared_buffers, within stated limits. log_checkpoints is on by default, so the log shows buffers written and time spent.
When should you change random_page_cost?
Change it only when plans on your real data pick the wrong path and the cause is the cost model. The default is 4.0 because the docs assume most random reads hit cache. They say lowering it suits databases that fit in memory, and raising it suits storage with a higher random read cost, like magnetic disks.
The docs note that random access to durable storage is normally much more expensive than four times sequential access, but that a lower default is used because most random accesses, such as indexed reads, are assumed to be cached (query planning, checked 4 Dec 2026). They also say network-attached storage latency tends to reduce the relative cost of random access. The docs give no value for any storage type, so we do not either. To test, change random_page_cost in one session, compare the plan and actual time with EXPLAIN ANALYZE, and keep it only if real timings improve across your top queries.
How do you tune autovacuum?
Leave autovacuum on and tune it per table. It defaults to starting a vacuum when 20% of a table plus 50 rows has changed, which is slow to fire on very large tables. The docs allow per-table overrides through storage parameters. Check n_dead_tup and the last autovacuum time before changing the global defaults.
The docs list autovacuum_vacuum_scale_factor at 0.2, autovacuum_vacuum_threshold at 50, autovacuum_max_workers at 3 and autovacuum_vacuum_cost_delay at 2 milliseconds, and say the scale factor can be overridden for individual tables with storage parameters (autovacuum settings, checked 4 Dec 2026). The routine vacuuming chapter says that for many installations it is sufficient to let the autovacuum daemon do the work, and explains why accurate planner statistics from ANALYZE matter (routine vacuuming, checked 4 Dec 2026). To check: query pg_stat_all_tables for n_dead_tup, last_autovacuum and n_mod_since_analyze, and set log_autovacuum_min_duration to log slow runs.
How many connections should PostgreSQL allow, and do you need pooling?
max_connections defaults to 100 and sizes shared memory, so raising it is not free. Each backend is a separate process, and work_mem multiplies by active sessions. If an application opens far more connections than the server can run at once, put a pooler such as PgBouncer in front rather than raising the limit.
The docs say max_connections is set at server start and increasing it leads to higher allocation of resources, including shared memory (connection settings, checked 4 Dec 2026). PgBouncer's documentation lists three pool modes: session, where a server connection is released when the client disconnects (the default), transaction, and statement (PgBouncer config, checked 4 Dec 2026). Transaction pooling reuses server connections between transactions, which changes what session-level features can be relied on, so test the application. Limits, pooler placement and the error messages are covered in PostgreSQL connection limits. To check: count rows in pg_stat_activity by state and see how many are idle.
How do you find slow queries with pg_stat_statements?
Add pg_stat_statements to shared_preload_libraries, restart, create the extension, and order the view by total_exec_time. Total time ranks what costs the server most, whether one slow query or a fast query called millions of times. Compare calls, mean_exec_time and rows before choosing what to fix.
The docs say the module must be loaded through shared_preload_libraries because it needs shared memory, so adding it needs a restart, and that query identifier calculation must be enabled, which compute_query_id set to auto or on does (pg_stat_statements, checked 4 Dec 2026). The view exposes calls, total_exec_time and mean_exec_time per normalised statement, plus buffer counters. For a slow-statement log without the extension, log_min_duration_statement logs any statement at or over the threshold you set (logging, checked 4 Dec 2026). Reset the counters with pg_stat_statements_reset() after a change so the next reading reflects it.
How do you read EXPLAIN ANALYZE?
Read from the innermost, most indented node outward, comparing estimated rows with actual rows. A large gap means the planner worked from bad statistics, so run ANALYZE or raise the column statistics target. Then look for sequential scans on large tables, sorts spilling, and nodes that account for most of the time.
EXPLAIN ANALYZE actually runs the statement, so for INSERT, UPDATE or DELETE wrap it in a transaction you roll back (EXPLAIN, checked 4 Dec 2026). The docs say buffer information is included by default when ANALYZE is used, and the actual time values are in milliseconds while cost estimates are in arbitrary units, so the two are not comparable (using EXPLAIN, checked 4 Dec 2026). default_statistics_target is 100, and larger values make ANALYZE slower but can improve estimates (query planning, checked 4 Dec 2026).
Which indexes help, and how do you check they are used?
B-tree is the default and fits equality and range filters. The docs also list Hash, GiST, SP-GiST, GIN and BRIN, each suited to different operators. Check use with idx_scan in the index statistics view, and build indexes on live tables with CREATE INDEX CONCURRENTLY, which avoids blocking writes but needs two table scans.
The docs say CREATE INDEX makes B-trees by default, and that the planner considers a B-tree whenever an indexed column is used with the comparison operators <, <=, =, >= or > (index types, checked 4 Dec 2026). With CONCURRENTLY, PostgreSQL builds without locks that block inserts, updates or deletes, at the cost of extra total work (CREATE INDEX, checked 4 Dec 2026). In pg_stat_all_indexes, an index with idx_scan at zero over a representative period has not been used since statistics were last reset, which makes it a candidate to review, since every index also slows writes. Confirm before dropping: some serve constraints.
When is tuning a job for an outside team?
When the checklist is done and queries are still slow, or when nobody on your team has time to own it. OSStend runs community, unforked PostgreSQL for customers 24/7 with named engineers, in its cloud, yours, your data center, Kubernetes or OpenShift, or air-gapped. Pricing is quoted per design.
For ongoing operation see managed PostgreSQL. For help with a cluster you keep running yourself there is PostgreSQL support, for a one-off review or design PostgreSQL consulting, and for a wider scope DBA services. The pricing page explains how quotes work.
Who this does not fit
This page suits teams who run PostgreSQL and want a starting checklist from the official docs. It gives no benchmark numbers and no one-size configuration, and it does not replace measuring your own workload.
Contact us if
- ✓You run PostgreSQL yourself and want a short list of what to review, in order.
- ✓You want the defaults and the docs' own sizing guidance with the source beside each.
- ✓You want to know when to stop tuning and ask for help.
Not a fit if
- ✕You want a ready-made postgresql.conf for your hardware. The docs do not give one, and neither do we.
- ✕You use a managed cloud service where the provider locks some settings. Check which parameters your plan lets you change.
- ✕Your problem is application design, such as a query run once per row. Settings will not fix that.
Questions about PostgreSQL performance tuning
What are the best PostgreSQL settings for performance?
There is no universal set. The docs' starting points are shared_buffers at 25% of memory on a dedicated server with 1GB or more of RAM, and defaults elsewhere until measurement says otherwise.
How much RAM should I give shared_buffers?
The docs give 25% of system memory as a reasonable start on a dedicated server with 1GB or more of RAM, and say more than 40% is unlikely to work better (docs, checked 4 Dec 2026).
What should work_mem be set to?
The docs give no number. They warn that total use can be many times work_mem, so raise it for the sessions that spill and measure with log_temp_files and EXPLAIN.
Does PostgreSQL tune itself?
Partly. Autovacuum, checkpoint spreading and the planner run on their own, and wal_buffers sizes itself from shared_buffers. Memory settings keep conservative defaults, so a large server needs review.
Do I need PgBouncer?
If many application connections are mostly idle, a pooler lets fewer server connections serve them. If your app opens few connections, you may not. See PostgreSQL connection limits.
How do I find slow queries in PostgreSQL?
Load pg_stat_statements and order by total_exec_time, and set log_min_duration_statement to log slow statements. Then run EXPLAIN (ANALYZE, BUFFERS) on the worst.
Related pages
- GuidePostgres connection limits, PgBouncer and replicasPostgres connection limits explained: max_connections, memory per connection, PgBouncer modes, RDS Proxy pinning, provider limits, docs read 19 Nov 2026.
- PostgreSQLManaged PostgreSQL service and Postgres hostingManaged PostgreSQL service, compared: how to choose between RDS, Aiven, Crunchy Bridge, Neon and a tailored cluster run 24/7 in your cloud or ours.
- PostgreSQL supportPostgreSQL support, 24/7, on clusters you runPostgreSQL support for community PostgreSQL: 24/7 on-call engineers on clusters you already run, in your cloud, data center or Kubernetes. Checked 4 Dec 2026.
- PostgreSQL consultingPostgreSQL consulting services: design, migrate, tunePostgreSQL consulting from engineers who run it 24/7: architecture reviews, Oracle and MySQL migrations, upgrades, tuning and HA design. Checked 4 Dec 2026.
- DBA servicesDatabase administration services, run 24/7Database administration services for PostgreSQL, MySQL, Redis or Valkey and OpenSearch: monitoring, tuning, backups, upgrades and 24/7 on-call engineers.
- PricingDatabase as a service pricing: dedicated, tailoredDatabase as a service pricing from OSStend: dedicated, tailored clusters priced by engine, size, SLA tier and location. Enterprise only, with no free tier.
Bring your slow database to an engineer
45 minutes: your settings, top queries and recovery targets are reviewed, and a PostgreSQL design is sketched with the checks above.
- ✓A design sketch for your workload, not a plan menu
- ✓A zero-downtime cutover path for what you run today
- ✓A price for that design, after the review





