{"id":3000,"date":"2016-01-19T00:26:23","date_gmt":"2016-01-19T00:26:23","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3000"},"modified":"2016-01-19T00:26:23","modified_gmt":"2016-01-19T00:26:23","slug":"postgres-log-message-regex-locks","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/postgres-log-message-regex-locks\/","title":{"rendered":"Regular Expression for Postgres log messages warning of locks"},"content":{"rendered":"<p>To match locks in Postgres logs you can use the following regular expressions. The first matches messages that show tables being locked, and the second shows the application loading share locks.<\/p>\n<pre lang=\"regex\">\n.*user=(?P&lt;lock_user_name&gt;\\w+),db=(?P&lt;lock_database&gt;\\w+) \nLOG:\\s+process (?P&lt;lock_process_id&gt;\\d+) \nacquired (?P&lt;lock_type&gt;\\w+) on (?&lt;lock_on&gt;\\w+) \n(?P&lt;lock_tuple&gt;[\\(\\)0-9,]+) of (?P&lt;lock_object_type&gt;\\w+) \n(?P&lt;lock_object_oid&gt;\\d+) of database (?P&lt;lock_db_oid&gt;\\d+) \nafter (?P&lt;lock_wait_time&gt;[0-9.]+).*\t\n<\/pre>\n<pre lang=\"regex\">\n.*user=(?P&lt;lock_user_name&gt;\\w+),db=(?P&lt;lock_database&gt;\\w+) \nLOG:\\s+process (?P&lt;lock_process_id&gt;\\d+) acquired \n(?P&lt;lock_type&gt;\\w+) on transaction (?&lt;lock_transaction_id&gt;\\d+) \nafter (?P&lt;lock_wait_time&gt;[0-9.]+) .*\t\n<\/pre>\n<p>The useful thing about this is it allows you to see which queries wait on locks, and for how long.<\/p>\n<p>To make these entries show up in the log, you need this in postgresql.conf:<\/p>\n<pre>\nlog_lock_waits = on\n<\/pre>\n<p>Locks on tables are particularly interesting, because they will list both the rows and tables that are locked. However, they do this using internal identifiers.<\/p>\n<p>You can look up the tables like so:<\/p>\n<pre lang=\"sql\">\nselect pg_class.oid, nspname, relname\nfrom pg_class, pg_namespace\nwhere pg_class.relnamespace = pg_namespace.oid\n  and pg_class.oid = 1234\n<\/pre>\n<p>The log output will give you row IDs (representing where the row is stored). You can query this like so:<\/p>\n<pre lang=\"sql\">\nselect *\nfrom mytable\nwhere ctid::text = '(0,1)'\n<\/pre>\n<p>If you are experiencing a lot of locks and are concerned, it is also worth trying to get the server name of the offending transaction, so that you can determine whether multiple servers in a farm are causing you issues, or several processes on the same server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To match locks in Postgres logs you can use the following regular expressions. The first matches messages that show tables being locked, and the second shows the application loading share locks. .*user=(?P&lt;lock_user_name&gt;\\w+),db=(?P&lt;lock_database&gt;\\w+) LOG:\\s+process (?P&lt;lock_process_id&gt;\\d+) acquired (?P&lt;lock_type&gt;\\w+) on (?&lt;lock_on&gt;\\w+) (?P&lt;lock_tuple&gt;[\\(\\)0-9,]+) of (?P&lt;lock_object_type&gt;\\w+) (?P&lt;lock_object_oid&gt;\\d+) of database (?P&lt;lock_db_oid&gt;\\d+) after (?P&lt;lock_wait_time&gt;[0-9.]+).* .*user=(?P&lt;lock_user_name&gt;\\w+),db=(?P&lt;lock_database&gt;\\w+) LOG:\\s+process (?P&lt;lock_process_id&gt;\\d+) acquired (?P&lt;lock_type&gt;\\w+) on transaction (?&lt;lock_transaction_id&gt;\\d+) &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/postgres-log-message-regex-locks\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Regular Expression for Postgres log messages warning of locks&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[186,345,347,370,437,458,522],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3000"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3000"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3000\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}