{"id":171,"date":"2012-05-22T16:03:25","date_gmt":"2012-05-22T14:03:25","guid":{"rendered":"http:\/\/blog.gocept.com\/?p=171"},"modified":"2012-12-03T15:07:38","modified_gmt":"2012-12-03T14:07:38","slug":"dont-stop-postgresqls-autovacuum-with-your-application","status":"publish","type":"post","link":"https:\/\/blog.gocept.com\/2012\/05\/22\/dont-stop-postgresqls-autovacuum-with-your-application\/","title":{"rendered":"Don’t stop PostgreSQL’s autovacuum with your application"},"content":{"rendered":"
Some weeks ago, we received a complaint from a customer about bad PostgreSQL performance for a specific application. I took a look into the database and found strange things going on: the query planner was executing “interesting” query plans, tables were bloated with lots of dead rows (one was 6 times as big as it should be), and so on.<\/p>\n
The cause revealed itself when looking at pg_stat_user_tables<\/em>:<\/p>\n Despite of heavy write activity on the database, no table had ever seen autovacuum or autoanalyze. But why?<\/p>\n As I delved into it, I noticed that PostgreSQL’s autovacuum\/autoanalyze was practically stopped in two ways by the application. I’d like to share our findings to help other programmers not to get trapped in situations like this.<\/p>\n<\/div>\n It turned out that the application had one component which connected to the database and opened a transaction right after startup, but never finished that transaction:<\/p>\n Note that the database server was started about 11 \u00be hours ago in this example. Vacuuming (whether automatic or manual) stops at the oldest transaction id that is still in use. Otherwise it would be vacuuming active transactions, which is not sensible at all. In our example, vacuuming is stopped right away since the oldest running transaction is only one minute older than the running server instance. At least this is easy to resolve: we got the developers to fix the application. Now it finishes every transaction in a sensible amount of time with either COMMIT<\/em> or ABORT<\/em>.<\/p>\n<\/div>\n Unfortunately, this was not all of it: autovacuum was working now but quite sporadically. A little bit of research revealed that autovacuum will abort if it is not able to obtain a table lock within one second<\/a>\u00a0– and guess what:\u00a0the application made quite heavy use of table locks. We found a hint that something suspicious is going on in the PostgreSQL log:<\/p>\n Searching the application source brought up several places where table locks were used. Example:<\/p>\n The textindex code was particularly problematic as it dealt often with large documents. Statements like the one above could easily place load on the database server high enough to cause frequent autovacuum aborts.<\/p>\n The developers said that they have introduced the locks because of concurrency issues. As we could not get rid of them, I have installed a nightly cron job to force-vacuum the database. PostgreSQL has shown much improved query responses since then. Some queries’ completion times even improved by a factor of 10. I’ve been told that in the meantime they have found a way to remove the locks so the cron job is not necessary anymore.<\/p>\n<\/div>\n PostgreSQL shows good auto-tuning and is a pretty low-maintenance database server if<\/em> you allow it to perform its autovacuum\/autoanalyze tasks regularly. We have seen that application programs may put autovacuum effectively out of business. In this particular case, unfinished transactions and extensive use of table locks were the show-stoppers. After we have identified and removed these causes, our PostgreSQL database is running smoothly again.<\/p>\n We are currently in the process of integrating some of the most obvious signs of trouble into the standard database monitoring on our managed hosting platform<\/a> to catch those problems quickly as they show up.<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"abc-hans=# SELECT relname, last_vacuum, last_autovacuum, last_analyze, last_autoanalyze\r\nFROM pg_stat_user_tables;\r\n relname | last_vacuum | last_autovacuum | last_analyze | last_autoanalyze\r\n-----------------------+-------------+-----------------+--------------+------------------\r\n object_ref | | | |\r\n archived_current | | | |\r\n archived_item | | | |\r\n pgtextindex | | | |\r\n archived_container | | | |\r\n archived_chunk | | | |\r\n commit_lock | | | |\r\n archived_blob_info | | | |\r\n archived_state | | | |\r\n archived_blob_link | | | |\r\n archived_item_deleted | | | |\r\n pack_object | | | |\r\n archived_class | | | |\r\n archived_object | | | |\r\n object_state | | | |\r\n object_refs_added | | | |\r\n blob_chunk | | | |<\/pre>\n
Unfinished transactions<\/h2>\n
abc-hans=# SELECT procpid, current_timestamp - xact_start AS xact_runtime, current_query\r\nFROM pg_stat_activity ORDER BY xact_start;\r\n procpid | xact_runtime | current_query\r\n\r\n---------+-----------------+-----------------------\r\n 18915 | 11:46:20.8783 | <IDLE> in transaction\r\n 21289 | 11:18:20.07042 | <IDLE> in transaction<\/pre>\n
Exclusive table locks<\/h2>\n
postgres[13251]: [40-1] user=,db= ERROR: canceling autovacuum task<\/pre>\n
stmt = \"\"\"\r\nLOCK %(table)s IN EXCLUSIVE MODE;\r\nDELETE FROM %(table)s WHERE docid = %%s;\r\nINSERT INTO %(table)s (docid, coefficient, marker, text_vector)\r\nVALUES (%%s, %%s, %%s, %(clause)s)\r\n\"\"\" % {'table': self.table, 'clause': clause}<\/pre>\n
Summary<\/h2>\n