As traffic goes up, even your PostgreSQL may become a bottleneck.
In this cases it is important to analyze the traffic and understand the usage pattern. The way you will be able to tune the system to meet the challenge.
Understand Usage Pattern at Peak Time
Use PostreSQL top project to get key usage patterns in real time:
- Current active SQL statements running
- Query plans
- User tables and indexes statistics
To get a broad insight of PostgreSQL behavior use pgFouine. This tool analyzes the PostgreSQL logs and provides detailed usage patternsreports such as leading queries, duration, queries by type and queries patterns.
You can get some of these metrics by querying the pg_catalog schema (such as pg_stat_user_tables and pg_stat_user_indexes), and use log_statement to analyze all queries.
Enable Slow Queries
- Add pg_stat_statements to shared_preload_libraries statement @ postresql.conf
- Restart PostgreSQL daemon.
- use the pg_stat_statements view to pinpoint the bottlenecks:
SELECT query, calls, total_time, rows, 100.0 * shared_blks_hit /
nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent
FROM pg_stat_statements ORDER BY total_time DESC LIMIT 5;
Explain the Execution Plans
Use the Explain statement to analyze slow queries execution paths and eliminate them:
EXPLAIN (FORMAT JSON) SELECT * FROM table_name;
Using these great tools, you can boost your PostgreSQL and meet the business challenges