PostgreSQL Performance

Query optimization and performance tuning

EXPLAIN

EXPLAIN SELECT * FROM users WHERE email = '[email protected]'; # query plan
EXPLAIN ANALYZE SELECT * FROM users; # with execution time

VACUUM

VACUUM; # reclaim storage
VACUUM ANALYZE users; # vacuum and update statistics
VACUUM FULL; # more aggressive (locks table)

ANALYZE

ANALYZE users; # update table statistics
ANALYZE; # update all tables

Connection Pool

SHOW max_connections; # max connections
SELECT count(*) FROM pg_stat_activity; # active connections

Query Performance

SELECT query, calls, total_time, mean_time
FROM pg_stat_statements
ORDER BY total_time DESC LIMIT 10; # slowest queries