Skip to content

Tune PostgreSQL Performance

Key idea:

First four levers: shared_buffers = 25 % RAM, effective_cache_size = 75 % RAM, work_mem = RAM / (max_connections × 3), maintenance_work_mem = 1-2 GB. Then enable pg_stat_statements, pick the top-10 by total_exec_time, add indexes guided by EXPLAIN ANALYZE. Watch autovacuum (pg_stat_user_tables.last_autovacuum): always lagging → bump autovacuum_max_workers.

Below: details, example, related terms, FAQ.

Check your site →

Details

  • shared_buffers defaults to 128 MB — set 25 % RAM (4 GB RAM → 1 GB)
  • work_mem affects JOIN/ORDER — too low = disk sort, too high = OOM
  • pg_stat_statements: CREATE EXTENSION + 20 MB RAM, stats for every query
  • pgbouncer in front of Postgres: transaction pool, 10k clients → 100 connections
  • EXPLAIN (ANALYZE, BUFFERS) shows the real plan + disk reads

Example

-- pgbench benchmark before/after tuning
$ pgbench -c 10 -T 60 mydb

-- top-10 slowest queries
SELECT query, calls, total_exec_time, mean_exec_time
FROM pg_stat_statements
ORDER BY total_exec_time DESC LIMIT 10;

-- Index + verify
CREATE INDEX CONCURRENTLY ON orders (user_id, created_at DESC);
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM orders WHERE user_id = 1 LIMIT 20;

Related

Understanding PostgreSQL Configuration Parameters

PostgreSQL performance tuning begins with a solid understanding of its configuration parameters. These parameters dictate how the database engine manages memory, disk I/O, and user connections. Below are some key parameters that you should focus on:

  • shared_buffers: This parameter determines how much memory PostgreSQL uses for caching data. A value of approximately 25% of your system's RAM is recommended for optimal performance.
  • effective_cache_size: This setting provides the query planner with a rough estimate of how much memory is available for caching data. Setting it to about 75% of your total RAM allows PostgreSQL to make better decisions about query plans.
  • work_mem: This parameter controls the amount of memory allocated for operations like sorting and hashing. A good starting point is to set it to RAM / (max_connections × 3), ensuring efficient memory use across multiple connections.
  • maintenance_work_mem: This parameter is used for maintenance operations such as VACUUM and CREATE INDEX. Allocating between 1-2 GB can significantly speed up these operations, especially in larger databases.

Adjusting these parameters can lead to improved query performance and overall system efficiency. Always monitor the effects of changes to ensure that they yield the desired outcomes.

Monitoring and Analyzing PostgreSQL Performance

To effectively tune PostgreSQL performance, monitoring and analysis are crucial. Utilize built-in tools and extensions to gather insights into database performance:

  • pg_stat_statements: This extension tracks execution statistics of all SQL statements executed. Enable it by adding shared_preload_libraries = 'pg_stat_statements' in your postgresql.conf file. After restarting PostgreSQL, you can query pg_stat_statements to identify slow queries.
  • EXPLAIN ANALYZE: This command provides a detailed execution plan for a query, allowing you to see how the database engine plans to execute it. Use it to identify bottlenecks and understand the impact of added indexes.
  • pg_stat_user_tables: Monitor autovacuum activity through this view. Pay attention to the last_autovacuum column to ensure that autovacuum processes are running as expected. If you notice consistent delays, consider increasing autovacuum_max_workers.

Regularly monitoring these metrics allows you to make informed decisions about performance tuning and ensures that your PostgreSQL instance is running optimally.

Practical Commands for PostgreSQL Tuning

Implementing performance tuning in PostgreSQL involves executing a series of commands and configuring parameters appropriately. Below are practical commands and configurations you can use:

  • Set shared_buffers: To set shared_buffers to 25% of your RAM, first calculate the value based on your system's total memory. For example, if your server has 16 GB of RAM, use:
  • ALTER SYSTEM SET shared_buffers = '4GB';
  • Set effective_cache_size: Similarly, adjust effective_cache_size:
  • ALTER SYSTEM SET effective_cache_size = '12GB';
  • Adjust work_mem: To calculate work_mem based on your max connections:
  • ALTER SYSTEM SET work_mem = '256MB';
  • Enable pg_stat_statements: Add to your postgresql.conf:
  • shared_preload_libraries = 'pg_stat_statements'
  • Check slow queries: After enabling pg_stat_statements, run:
  • SELECT * FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 10;
  • Monitor autovacuum: To check the last autovacuum time:
  • SELECT relname, last_autovacuum FROM pg_stat_user_tables;

By executing these commands and monitoring their effects, you can significantly enhance the performance of your PostgreSQL database.

Score 0–100Unified site health score
SSL + SecuritySecurity and certificate status
PerformanceResponse speed and caching
SEO Signalsrobots.txt, sitemap, canonicals

Why teams trust us

100
point scale
SSL
SSL + HTTP headers
10+
scoring criteria
Free
no signup

How it works

1

Enter site URL

2

Analyse 10+ factors

3

Get overall score

What is Health Score?

Health Score is a comprehensive assessment of site technical health across 20+ parameters: SSL, security headers, response speed, SEO technical factors, and availability.

Comprehensive Score

20+ parameters in one number: SSL, headers, speed, SEO technical factors.

Detailed Breakdown

Each parameter with explanation — what is checked, what was found, how to fix.

Score Trends

Compare Health Score at different dates — see progress or regression.

Health Monitoring

Set up automated Health Score checks and get notified when the score drops.

Who uses this

Developers

quick pre-release audit

SEO

technical baseline score

Marketers

client site check

Security

header security audit

Common Mistakes

Ignoring red parametersA red parameter is a critical issue. Start fixing those, not the yellow ones.
Only checking homepageIssues may exist on subpages. Check key sections and landing pages.
Not re-checking after fixAfter each fix, rerun the check and verify the score improved.
Treating 80+ as good enoughAim for 95+. Every red item is a risk to SEO or security.

Best Practices

Fix by priorityRed > yellow > blue. Critical issues first.
Check regularlyWeekly Health Score check helps catch degradation before it affects SEO.
Use monitoringConnect an automated HTTP monitor — it will be the first to notice downtime.
Compare with competitorsCheck the Health Score of your nearest competitor — a great benchmark for prioritization.

Get more with a free account

Health Score check history and real-time site health monitoring.

Sign up free

Learn more

Frequently Asked Questions

Can I trust PgTune?

Yes, as a starting point. pgtune.leopard.in.ua generates a base config. From there, tune to your workload (OLTP vs OLAP).

When do I need partitions?

Tables > 100 GB or purging old data. For < 10 GB — unnecessary complexity.

Is a connection pooler critical?

At > 200 concurrent — yes, Postgres per-connection backend eats 10 MB RAM + context switches.

Try the live tool that powered this guide

Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.