{"id":1835,"date":"2026-11-25T03:26:32","date_gmt":"2026-11-25T03:26:32","guid":{"rendered":"https:\/\/virtualserversvps.com\/blog\/?p=1835"},"modified":"2026-11-25T03:26:32","modified_gmt":"2026-11-25T03:26:32","slug":"postgres-idle-in-transaction-lock-timeouts","status":"publish","type":"post","link":"https:\/\/virtualserversvps.com\/blog\/postgres-idle-in-transaction-lock-timeouts\/","title":{"rendered":"PostgreSQL Idle-in-Transaction Sessions on a Small VPS: Timeouts, Lock Waits and Bloat"},"content":{"rendered":"<div style=\"padding:20px\">\n<h2>Find the Idle Sessions First<\/h2>\n<p>An <code>idle in transaction<\/code> session holds its snapshot open and blocks vacuum from cleaning rows it might still see. On a small VPS this is the most common cause of a table that grows without ever shrinking. Find them with <code>SELECT pid, state, xact_start, now()-xact_start AS age, query FROM pg_stat_activity WHERE state='idle in transaction' ORDER BY age DESC;<\/code> and look at the top row before doing anything else.<\/p>\n<p>Anything past a few seconds is suspicious; anything past a few minutes on a production box is a bug in the application, usually a connection handed out by a pool and never committed. Note the <code>application_name<\/code> and <code>client_addr<\/code> columns to identify which service is responsible, because killing the session fixes the symptom for a day and not the cause.<\/p>\n<h2>Terminate Carefully, Not Blindly<\/h2>\n<p>For a genuinely stuck backend, <code>SELECT pg_terminate_backend(pid)<\/code> ends it and rolls back the transaction. Use <code>pg_cancel_backend(pid)<\/code> first if the session is mid-query \u2014 it requests cancellation and lets the server clean up gracefully. Reserve termination for idle-in-transaction sessions where cancellation does nothing because no query is running.<\/p>\n<p>Before killing anything, check <code>pg_stat_activity<\/code> for <code>wait_event_type='Lock'<\/code> across all sessions. If twenty sessions are waiting on one idle-in-transaction blocker, terminating that single PID releases all twenty at once. Termination is a scalpel here: one PID clears a queue, and it is worth reading the whole table before picking it.<\/p>\n<h2>Use statement_timeout to Cap Runaway Queries<\/h2>\n<p><code>statement_timeout<\/code> defaults to 0, which means no limit at all. A single unindexed query on a large table can run for minutes, hold locks and pin memory. Set a sane ceiling per role or per database: <code>ALTER ROLE app_user SET statement_timeout = '30s';<\/code> and let the application see a clear error rather than a hung request.<\/p>\n<p>Set it per role rather than globally so that migrations, backups and admin work are not caught by the same limit. For <code>pg_dump<\/code>, <code>CREATE INDEX CONCURRENTLY<\/code> and maintenance scripts, either use a superuser that inherits no role limit or wrap the statement in <code>SET LOCAL statement_timeout = 0;<\/code> inside a transaction. A blanket global timeout will eventually break a legitimate maintenance job.<\/p>\n<h2>Add idle_in_transaction_session_timeout<\/h2>\n<p>The direct fix for the idle-in-transaction problem is a server-side timeout. <code>ALTER DATABASE appdb SET idle_in_transaction_session_timeout = '60s';<\/code> makes PostgreSQL terminate any session that sits idle inside an open transaction for over a minute. This is a blunt instrument, but it is exactly the right bluntness for a pool with a leak.<\/p>\n<p>Watch <code>pg_stat_database.xact_rollback<\/code> before and after enabling it. If the rollback count jumps once per deploy or per scheduled job, your timeout is too tight for that workload. Sixty seconds is generous for a web request and far too tight for a batch job, so scope it to the database the web application uses, not to the whole cluster.<\/p>\n<h2>Read Lock Waits From pg_stat_activity<\/h2>\n<p>The query that identifies blockers in one shot joins <code>pg_stat_activity<\/code> to <code>pg_locks<\/code>: select the waiting PID, the blocking PID and the relation name via <code>pg_class<\/code>. When <code>granted = false<\/code> appears in <code>pg_locks<\/code>, that row is a waiter, and <code>pg_blocking_pids(pid)<\/code> returns the PID holding the lock. That function is the fastest way to walk the chain on a live server.<\/p>\n<p>Persistent lock waits on a small VPS usually trace back to two patterns: an ALTER TABLE or <code>CREATE INDEX<\/code> without <code>CONCURRENTLY<\/code>, and a long-running analytics query that reads a table the application wants to write. Both are solvable with schedule changes and concurrent DDL, not with more RAM. Track how often <code>pg_stat_database.deadlocks<\/code> increments as a rough indicator.<\/p>\n<h2>Connect the Symptom to Table Bloat<\/h2>\n<p>Idle transactions are not only a locking problem; they delay vacuum. PostgreSQL cannot remove a dead tuple that a live transaction can still see, so a long snapshot pins dead rows in the table and in every index covering it. Monitor with <code>SELECT relname, n_dead_tup, n_live_tup, last_autovacuum FROM pg_stat_user_tables ORDER BY n_dead_tup DESC LIMIT 10;<\/code> and alert when <code>n_dead_tup<\/code> passes a few percent of <code>n_live_tup<\/code>.<\/p>\n<p>When bloat has already accumulated, autovacuum will not shrink the file on disk \u2014 it reclaims space for reuse, not returns it to the OS. Confirm with <code>pgstattuple<\/code> if the extension is available, or compare <code>pg_relation_size<\/code> against the summed live row estimate. A <code>VACUUM FULL<\/code> fixes it but takes an exclusive lock, so schedule it in a window or use <code>pg_repack<\/code> instead. On a <a href=\"https:\/\/virtualserversvps.com\/\">virtualserversvps.com<\/a> instance with local NVMe, that rewrite finishes in minutes rather than hours.<\/p>\n<h2>Make the Fix Permanent<\/h2>\n<p>Timeouts and vacuum settings are useless if nobody knows a transaction is stuck. Add a monitoring check that counts sessions with <code>state='idle in transaction'<\/code> and alerts above a threshold, and another that flags <code>xact_start<\/code> older than five minutes. Both are cheap queries and both fire before users notice the slowdown.<\/p>\n<p>Set the timeouts in <code>postgresql.conf<\/code> or per-role so they survive a failover, and record the change in your runbook next to the autovacuum settings. On a 1 GB or 2 GB instance the margin for lock queues is thin \u2014 a dedicated <a href=\"https:\/\/virtualserversvps.com\/\">virtualserversvps.com<\/a> box with predictable I\/O makes the difference between autovacuum keeping up and falling behind, which is the real root cause behind most tables that never shrink.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>An idle in transaction session holds its snapshot open and blocks vacuum from cleaning rows it might still see. On a small VPS this is the most common cause of a table that grows&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1835","post","type-post","status-publish","format-standard","hentry","category-vps-guides-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.1 (Yoast SEO v26.1) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PostgreSQL Idle-in-Transaction Sessions on a Small VPS: Timeouts, Lock Waits and Bloat - Virtual Servers VPS Blog<\/title>\n<meta name=\"description\" content=\"An idle in transaction session holds its snapshot open and blocks vacuum from cleaning rows it might still see. On a small VPS this is the most common cause of a table that grows...\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/virtualserversvps.com\/blog\/postgres-idle-in-transaction-lock-timeouts\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL Idle-in-Transaction Sessions on a Small VPS: Timeouts, Lock Waits and Bloat\" \/>\n<meta property=\"og:description\" content=\"PostgreSQL Idle-in-Transaction Sessions on a Small VPS: Timeouts, Lock Waits and Bloat\" \/>\n<meta property=\"og:url\" content=\"https:\/\/virtualserversvps.com\/blog\/postgres-idle-in-transaction-lock-timeouts\/\" \/>\n<meta property=\"og:site_name\" content=\"Virtual Servers VPS Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-25T03:26:32+00:00\" \/>\n<meta name=\"author\" content=\"Virtual-Servers-Vps-Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Virtual-Servers-Vps-Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/postgres-idle-in-transaction-lock-timeouts\/\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/postgres-idle-in-transaction-lock-timeouts\/\",\"name\":\"PostgreSQL Idle-in-Transaction Sessions on a Small VPS: Timeouts, Lock Waits and Bloat - Virtual Servers VPS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\"},\"datePublished\":\"2026-11-25T03:26:32+00:00\",\"author\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\"},\"description\":\"An idle in transaction session holds its snapshot open and blocks vacuum from cleaning rows it might still see. On a small VPS this is the most common cause of a table that grows...\",\"breadcrumb\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/postgres-idle-in-transaction-lock-timeouts\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/virtualserversvps.com\/blog\/postgres-idle-in-transaction-lock-timeouts\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/postgres-idle-in-transaction-lock-timeouts\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/virtualserversvps.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Idle-in-Transaction Sessions on a Small VPS: Timeouts, Lock Waits and Bloat\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/\",\"name\":\"Virtual Servers VPS Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\",\"name\":\"Virtual-Servers-Vps-Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"caption\":\"Virtual-Servers-Vps-Editor\"},\"sameAs\":[\"https:\/\/virtualserversvps.com\/blog\"],\"url\":\"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"PostgreSQL Idle-in-Transaction Sessions on a Small VPS: Timeouts, Lock Waits and Bloat - Virtual Servers VPS Blog","description":"An idle in transaction session holds its snapshot open and blocks vacuum from cleaning rows it might still see. On a small VPS this is the most common cause of a table that grows...","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/virtualserversvps.com\/blog\/postgres-idle-in-transaction-lock-timeouts\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL Idle-in-Transaction Sessions on a Small VPS: Timeouts, Lock Waits and Bloat","og_description":"PostgreSQL Idle-in-Transaction Sessions on a Small VPS: Timeouts, Lock Waits and Bloat","og_url":"https:\/\/virtualserversvps.com\/blog\/postgres-idle-in-transaction-lock-timeouts\/","og_site_name":"Virtual Servers VPS Blog","article_published_time":"2026-11-25T03:26:32+00:00","author":"Virtual-Servers-Vps-Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Virtual-Servers-Vps-Editor","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/virtualserversvps.com\/blog\/postgres-idle-in-transaction-lock-timeouts\/","url":"https:\/\/virtualserversvps.com\/blog\/postgres-idle-in-transaction-lock-timeouts\/","name":"PostgreSQL Idle-in-Transaction Sessions on a Small VPS: Timeouts, Lock Waits and Bloat - Virtual Servers VPS Blog","isPartOf":{"@id":"https:\/\/virtualserversvps.com\/blog\/#website"},"datePublished":"2026-11-25T03:26:32+00:00","author":{"@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0"},"description":"An idle in transaction session holds its snapshot open and blocks vacuum from cleaning rows it might still see. On a small VPS this is the most common cause of a table that grows...","breadcrumb":{"@id":"https:\/\/virtualserversvps.com\/blog\/postgres-idle-in-transaction-lock-timeouts\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/virtualserversvps.com\/blog\/postgres-idle-in-transaction-lock-timeouts\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/virtualserversvps.com\/blog\/postgres-idle-in-transaction-lock-timeouts\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/virtualserversvps.com\/blog\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Idle-in-Transaction Sessions on a Small VPS: Timeouts, Lock Waits and Bloat"}]},{"@type":"WebSite","@id":"https:\/\/virtualserversvps.com\/blog\/#website","url":"https:\/\/virtualserversvps.com\/blog\/","name":"Virtual Servers VPS Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0","name":"Virtual-Servers-Vps-Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","caption":"Virtual-Servers-Vps-Editor"},"sameAs":["https:\/\/virtualserversvps.com\/blog"],"url":"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/"}]}},"_links":{"self":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1835","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/comments?post=1835"}],"version-history":[{"count":1,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1835\/revisions"}],"predecessor-version":[{"id":1836,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1835\/revisions\/1836"}],"wp:attachment":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/media?parent=1835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/categories?post=1835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/tags?post=1835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}