PostgreSQL database queries are a common performance bottleneck for web apps. Before you resort to more complex optimization techniques like caching or read replicas, you should double-check if your database engine is correctly tuned and queries are not underperforming.
In this blog post, I present a step by step guide on using PG Extras library to spot and resolve common PostgreSQL database performance issues.
How to start using PG Extras
Please refer to READMEs of respective implementations for installation details. API is almost identical for all the versions. Let’s compare the invocation of the
In this blog post, I’ll be using examples from the Ruby version.
Some of the PG Extras methods depend on the
pg_stat_statements extension. If you are using a default Heroku PostgreSQL plugin or AWS RDS, you should be good to go without making any changes.
To check if the extension is already enabled you can use PG Extras itself:
pg_stat_statements is not listed you should check out these docs for info on installing it.
If the extension is available but not enabled you have to run this SQL command in your database:
Now that you’ve set up the library in the language of your choice let’s start checking our database’s health.
[Important] Make certain to run all the checks on a warmed up production database. Under the hood, PG Extras performs a lightweight queries on PostgreSQL metadata tables. It will not impact your production workload.
1) Validate your database specs with cache hit ratios
In theory, the simplest solution to optimize the underperforming database is to scale it up vertically. Before you start throwing money at your performance issues, it’s good to check if it will actually help.
PostgreSQL tracks access patterns of your data and keeps frequently read chunks in a memory cache. A reliable indicator that a database should be scaled up is an invalid cache hit ratio.
You can check index and table cache hit ratios using the following code:
If you want to drill down into each individual’s table and index cache hit ratios, you can use
The rule of the thumb is that values should be above 99%. If your database cache hit ratios are lower, it’s either not correctly configured or should be scaled up to increase the performance.
Heroku PostgreSQL ships with already optimized settings and does not allow you to change them. If you see low cache hit ratios, your best bet is to provision a more powerful database instance.
Amazon RDS is notorious for shipping the database instances with incorrect default settings. If you’re using it, make sure to tweak them before deciding to scale up the instance. PGTune is the best tool to help you tweak the most important Postgres buttons and dials to the correct values.
2) Remove unused indexes
Overusing indexes is a recipe for a sluggish web app.
The more indexes you add, the more write operations have to be performed on each data update. Misconfigured indexes also tend to unecessarily bloat the size of a database, slowing down the backup/restore/upgrade operations.
It’s entirely possible that some of your indexes and not used and can be safely removed.
unused_indexes method can help you spot them:
index_scans on an index that has been around for a while means that it should be removed. If the index is large, remember to use the
CONCURRENTLY option when dropping it, to avoid exclusively blocking the whole related table.
index_size method can give you a quick overview of how much space your database indexes are taking:
3) Add missing indexes
Now that we’ve removed unused indexes, let’s add some new ones. We don’t want them to share the fate of their recently deprovisioned cousins. Let’s look at PG Extras
calls methods before deciding on what should be indexed.
A sequential scan is an action that Postgres performs if it cannot find an index necessary to fulfill the query condition. For the following query:
EXPLAIN ANALYZE query output will show
Seq scan on users Filter: AGE = 18 or
Index Scan using users_age_index Index Cond: AGE = 18 depending on whether the index on
age column is present or not.
seq_scans method displays the number of
Seq Scan operations for each table:
Now that we know which tables are often read inefficiently, we can use
outliers methods to list the most often executed and most time-consuming queries.
Both of those methods let you extract the raw query string. You can use it to perform
EXPLAIN ANALYZE and check if the query planner does
Seq scan on one of the tables.
By correlating all those sources of data, you should be able to spot queries that are consuming a lot of your database resources and are potentially missing an index.
Watch out to avoid premature optimization by adding unnecessary indexes. PostgreSQL will often fallback to
Seq Scan instead of
Index Scan on small tables, for which using the index would be less efficient than reading the whole table row by row.
4) Identify deadlocks
PostgreSQL uses locks to ensure data consistency in multithreaded environments. There are different kinds of locks, but we’ll focus on
RowExclusiveLock. A healthy web app should never lock for more than a couple of hundred of miliseconds.
Deadlock is two more or database locks blocking each other and not able to continue execution. An implementation error that results in a deadlock might have disastrous consequences for your application. The queue of requests not able to proceed could start piling up and eventually crash your servers.
Common reasons for deadlocks and locks that are granted for too long:
- too broad database transaction scope
- adding or removing index without using the
- updating lots of rows at once
- adding a new column with the default value (before PostgreSQL 12)
How to detect locks and deadlocks
You can use
locks method to see all the currently obtained locks together with the source query:
The mere presence of locks does not mean that something is wrong with your database. Only locks that are granted for too long are potentially problematic. You can use the following Ruby snippet integrated into the background job to alert you if this happens:
If you notice extended locks, you can use the
blocking method to check which SQL statements cannot continue execution because of a lock:
If your app is crashing because of deadlocks, you can use the
kill_all to terminate all the database processes before you manage to resolve the underlying cause.
5) Get rid of unnecessary bloat
The way PostgreSQL works is that it never updates or removes the data in place but instead marks each row as visible or not for transactions using two meta columns
xmax. Rows no longer visible for any of the currently active transactions are called dead rows or bloat.
Dead rows are regularly garbage collected by a process called AUTOVACUUM, and space they previously occupied can be reclaimed for new data. If autovacuuming is misconfigured, it might result in your table consisting of mostly dead rows that are blocking the disk space and slowing down queries.
To check if some of your tables are overly bloated, you can use the
If bloat ratio (i.e. size of bloat related to the size of the whole table) is close to the value of 10 you should have a closer look at your vacuum settings.
vacuum_stats method can help you with that:
On frequently updated tables the bloat could be significant but autovacumming should be getting rid of it on a regular basis.
If you see that your bloated table has not been autovacuumed for a while and autovacuum is not expected, it means that something might be misconfigured in your autovacuum settings. You should check the PostgreSQL docs on automatic vacuuming for more info on config settings that could cause that.
VACUUM FULL or not
If your table has bloat, although
vacuum_stats indicate that it has recently been vacuumed, it means that table has a lot of free disk space that has been previously restored from the vacuumed rows.
It is not necessarily a bad thing. PostgreSQL will reuse that disk space for new data. If you’d like to reclaim this space for the operating system, you need to perform the
VACUUM FULL. Contrary to
VACUUM FULL grants an exclusive table lock, so doing it on a live production database could cause a disaster.
VACUUM FULL without locking the underlying table, you could use pg_repack.
Generally, it is not recommended to run
VACUUM FULL on a regular basis. You should consider it only in some cases, i.e., you’ve removed most of the data from a huge table.
6) Remove unneeded objects
It might seem like obvious advice, but I have a feeling that it is often getting overlooked. Do you really need to keep all the data of a user who signed up two years ago and has not visited ever since? Regularly purging unnecessary data will keep both your database and GDPR inspectors happy.
table_size method can help you get a quick overview of how big your tables actually are:
E.g., in my project Abot I regularly remove all the data of teams who deactivated the Slack integration. This is both performance and GDPR friendly practice.
I hope that this checklist will help you keep your Postgres databases running at their best possible performance.
I understand that manually checking all those stats is a tedious and repetitive task. I’m currently working on a new API for PG Extras that will allow you to automate the database health checks and alert when the numbers are off. You can follow me on Twitter to get updated when it’s ready.