{"id":1181,"date":"2007-09-24T00:35:31","date_gmt":"2007-09-24T00:35:31","guid":{"rendered":"\/blogs\/paul\/post\/Search-Engine-QA-1-Running-out-of-transaction-log-space.aspx"},"modified":"2017-04-13T09:52:08","modified_gmt":"2017-04-13T16:52:08","slug":"search-engine-qa-1-running-out-of-transaction-log-space","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-1-running-out-of-transaction-log-space\/","title":{"rendered":"Search Engine Q&#038;A #1: Running out of transaction log space"},"content":{"rendered":"<p>One of the great things about the blog engine we use is that it shows all the search engine queries that led to someone clicking through to the site. I&#8217;ve been looking through the logs to see what kind of problems people are having that end up here. In this occasional series, I&#8217;m going to pick a search engine query and blog about it &#8211; hopefully helping out people who have the problem in future.<\/p>\n<p>First up is running out of transaction log space. This happens when the transaction log fills up to the point where it has to grow but either autogrow is not turned on or the volume on which the transaction log is placed has no more space for the file to grow. So what causes the transaction log to fill up in the first place? It could be a number of different things. The two most common ones I&#8217;ve seen are:<\/p>\n<ul>\n<li>The database is in full recovery mode with normal processing, a full database backup has been taken but no log backups have been taken. This commonly happens when an application goes into production and someone decides to take a backup, without realizing the consequences. Log truncation is not automatic in such a case &#8211; a log backup is required to allow the log to truncate.<\/li>\n<li>The database is in any recovery mode but there&#8217;s a very long-running, uncommitted transaction that prevents log truncation. (Even in full recovery mode with regular log backups, all the log records for the long-running transaction are required in case it rolls-back &#8211; and SQL Server can&#8217;t selectively truncate log records from the log for some transactions but not others.)<\/li>\n<\/ul>\n<p>Let&#8217;s have a look at both of these in SQL Server 2005 and example solutions.<\/p>\n<p><strong>The log backup case&#8230;<\/strong><\/p>\n<p>First thing to do is create a database with a very small transaction log that we can easily fill up.<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nUSE &#x5B;master];\r\nGO\r\n\r\nIF DATABASEPROPERTY (N'tinylogtest', 'Version') &gt; 0 DROP DATABASE &#x5B;tinylogtest];\r\nGO\r\n\r\nCREATE DATABASE &#x5B;tinylogtest] ON (\r\n    NAME = N'fgt_mdf',\r\n    FILENAME = N'c:\\tinylogtest\\tinylogtest.mdf',\r\n    SIZE= 2MB)\r\nLOG ON (\r\n    NAME = N'fgt_log',\r\n    FILENAME = N'c:\\tinylogtest\\tinylogtest.ldf',\r\n    SIZE = 512KB,\r\n    FILEGROWTH = 0);\r\nGO\r\n<\/pre>\n<p>Notice that I&#8217;ve specifically turned auto-growth of the log file off by setting the file growth to zero. Now let&#8217;s set the database into full recovery mode and take a full database backup.<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nALTER DATABASE &#x5B;tinylogtest] SET RECOVERY FULL;\r\nGO\r\n\r\nBACKUP DATABASE &#x5B;tinylogtest] TO DISK = N'C:\\tinylogtest\\dummybackup.bck';\r\nGO\r\n<\/pre>\n<p>And then create a simple table and fill up the transaction log.<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nSET NOCOUNT ON;\r\nGO\r\n\r\nCREATE TABLE &#x5B;tinylogtest]..&#x5B;testtable] (&#x5B;c1] INT, &#x5B;c2] CHAR (3000));\r\nGO\r\n\r\nWHILE (1=1)\r\nBEGIN\r\n    INSERT INTO &#x5B;tinylogtest]..&#x5B;testtable] VALUES (1, 'a')\r\nEND;\r\nGO\r\n<\/pre>\n<p>And we get the following error:<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nMsg 9002, Level 17, State 2, Line 4\r\nThe transaction log for database 'tinylogtest' is full. To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.databases\r\nThis is much better than you get on SQL Server 2000:\r\nMsg 9002, Level 17, State 2, Line 4\r\nThe\u00a0log file\u00a0for database 'tinylogtest' is full.\r\n<\/pre>\n<p>This has no helpful information at all. So, looking at the 2005 message, it&#8217;s telling us to look in the <em>sys.databases<\/em> table for more info. Let&#8217;s do that:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nSELECT &#x5B;log_reuse_wait_desc] FROM sys.databases WHERE &#x5B;name] = N'tinylogtest';\r\nGO\r\n<\/pre>\n<p>And the result is:<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nLOG_BACKUP\r\n<\/pre>\n<p>That&#8217;s pretty clear &#8211; take a log backup! Here are the various values this can take (at the time of writing), from the Books Online entry for <em>sys.databases<\/em>:<\/p>\n<ul>\n<li>NOTHING<\/li>\n<li>CHECKPOINT<\/li>\n<li>LOG_BACKUP<\/li>\n<li>ACTIVE_BACKUP_OR_RESTORE<\/li>\n<li>ACTIVE_TRANSACTION<\/li>\n<li>DATABASE_MIRRORING<\/li>\n<li>REPLICATION<\/li>\n<li>DATABASE_SNAPSHOT_CREATION<\/li>\n<li>LOG_SCAN<\/li>\n<li>OTHER_TRANSIENT<\/li>\n<\/ul>\n<p>Their meanings are explained in the Books Online entry <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms345414(v=sql.105).aspx\">Factors That Can Delay Log Truncation<\/a>. Note that the value returned is what was stopping log truncation when it was last attempted.<\/p>\n<p>Now if we take a log backup, the log can be truncated.<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nBACKUP LOG &#x5B;tinylogtest] TO DISK = N'C:\\tinylogtest\\dummybackup.bck';\r\nGO\r\n<\/pre>\n<p>And checking the state in <em>sys.databases<\/em> again returns:<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nNOTHING\r\n<\/pre>\n<p>Simple &#8211; but it&#8217;s amazing how many times I see this problem on newsgroups and forums.<\/p>\n<p><strong>The long-running transaction case&#8230;<\/strong><\/p>\n<p>For this test, assume I&#8217;ve run the T-SQL above to create the database, put it into full recovery mode, take the first full backup, and then create the table. Now in a second connection, I&#8217;ll create a long-running transaction:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nBEGIN TRANSACTION PAULSTRAN;\r\nGO\r\n\r\nINSERT INTO &#x5B;tinylogtest]..&#x5B;testtable] VALUES (1,'a');\r\nGO\r\n<\/pre>\n<p>Note that the transaction hasn&#8217;t been committed or rolled back. In the original connection, I&#8217;ll execute the loop to fill up the log again. When we check sys.databases we get back:<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nLOG_BACKUP\r\n<\/pre>\n<p>So we take a log backup and check the state again. This time we get:<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nACTIVE_TRANSACTION\r\n<\/pre>\n<p>We need to use the\u00a0<em>DBCC OPENTRAN<\/em> command to find out what the long running transaction is:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nDBCC OPENTRAN (N'tinylogtest');\r\nGO\r\n<\/pre>\n<p>And we get back:<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nTransaction information for database 'tinylogtest'.\r\n\r\nOldest active transaction:\r\nSPID (server process ID): 54\r\nUID (user ID) : -1\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : PAULSTRAN\r\nLSN\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : (18:98:1)\r\nStart time\u00a0\u00a0\u00a0\u00a0 \u00a0: Sep 23 2007\u00a0 5:49:53:077PM\r\nSID\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 0x010500000000000515000000ae4da6eadcd59cf661d6bb58ed030000\r\nDBCC execution completed. If DBCC printed error messages, contact your system administrator.\r\n<\/pre>\n<p>From this info we can track down the transaction and commit it or roll it back. In our example, once we roll back the PAULSTRAN transaction, there&#8217;s enough log space to insert one more record into the table, but then we run out of log space again. What&#8217;s going on? Because the PAULSTRAN transaction we very close to the start of the transaction log, all of the log it was effectively holding hostage was backed up but couldn&#8217;t be cleared. In this case we need to perform another log backup to allow the log to be cleared. This makes sense and is documented behavior.<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>Of course, there are other causes of the transaction log filling up and other things you can do apart from taking log backups or identifying long-running transactions &#8211; such as growing the log file or moving the log file to a volume with more free space. Here are some good resources you can use for further reading:<\/p>\n<ul>\n<li><a href=\"https:\/\/support.microsoft.com\/kb\/317375\">KB 317375 &#8211; A transaction log grows unexpectedly or becomes full on a computer that is running SQL Server<\/a><\/li>\n<li><a href=\"https:\/\/support.microsoft.com\/kb\/873235\">KB 873325 &#8211; How to stop the transaction log of a SQL Server database from growing unexpectedly<\/a><\/li>\n<li><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms175495.aspx\">Books Online entry for Troubleshooting a Full Transaction Log<\/a>\u00a0&#8211; this has a bunch of links to other BOL topics in this space<\/li>\n<\/ul>\n<p>Hope this helps!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the great things about the blog engine we use is that it shows all the search engine queries that led to someone clicking through to the site. I&#8217;ve been looking through the logs to see what kind of problems people are having that end up here. In this occasional series, I&#8217;m going to [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,78,98],"tags":[],"class_list":["post-1181","post","type-post","status-publish","format-standard","hentry","category-example-scripts","category-search-engine-q-and-a","category-transaction-log"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Search Engine Q&amp;A #1: Running out of transaction log space - Paul S. Randal<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-1-running-out-of-transaction-log-space\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Search Engine Q&amp;A #1: Running out of transaction log space - Paul S. Randal\" \/>\n<meta property=\"og:description\" content=\"One of the great things about the blog engine we use is that it shows all the search engine queries that led to someone clicking through to the site. I&#8217;ve been looking through the logs to see what kind of problems people are having that end up here. In this occasional series, I&#8217;m going to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-1-running-out-of-transaction-log-space\/\" \/>\n<meta property=\"og:site_name\" content=\"Paul S. Randal\" \/>\n<meta property=\"article:published_time\" content=\"2007-09-24T00:35:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-04-13T16:52:08+00:00\" \/>\n<meta name=\"author\" content=\"Paul Randal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Paul Randal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-1-running-out-of-transaction-log-space\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-1-running-out-of-transaction-log-space\/\",\"name\":\"Search Engine Q&A #1: Running out of transaction log space - Paul S. Randal\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\"},\"datePublished\":\"2007-09-24T00:35:31+00:00\",\"dateModified\":\"2017-04-13T16:52:08+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-1-running-out-of-transaction-log-space\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-1-running-out-of-transaction-log-space\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-1-running-out-of-transaction-log-space\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Search Engine Q&#038;A #1: Running out of transaction log space\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\",\"name\":\"Paul S. Randal\",\"description\":\"In Recovery...\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\",\"name\":\"Paul Randal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"caption\":\"Paul Randal\"},\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/paul\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Search Engine Q&A #1: Running out of transaction log space - Paul S. Randal","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:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-1-running-out-of-transaction-log-space\/","og_locale":"en_US","og_type":"article","og_title":"Search Engine Q&A #1: Running out of transaction log space - Paul S. Randal","og_description":"One of the great things about the blog engine we use is that it shows all the search engine queries that led to someone clicking through to the site. I&#8217;ve been looking through the logs to see what kind of problems people are having that end up here. In this occasional series, I&#8217;m going to [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-1-running-out-of-transaction-log-space\/","og_site_name":"Paul S. Randal","article_published_time":"2007-09-24T00:35:31+00:00","article_modified_time":"2017-04-13T16:52:08+00:00","author":"Paul Randal","twitter_misc":{"Written by":"Paul Randal","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-1-running-out-of-transaction-log-space\/","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-1-running-out-of-transaction-log-space\/","name":"Search Engine Q&A #1: Running out of transaction log space - Paul S. Randal","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website"},"datePublished":"2007-09-24T00:35:31+00:00","dateModified":"2017-04-13T16:52:08+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-1-running-out-of-transaction-log-space\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-1-running-out-of-transaction-log-space\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-1-running-out-of-transaction-log-space\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/paul\/"},{"@type":"ListItem","position":2,"name":"Search Engine Q&#038;A #1: Running out of transaction log space"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/","name":"Paul S. Randal","description":"In Recovery...","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce","name":"Paul Randal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","caption":"Paul Randal"},"sameAs":["http:\/\/3.209.169.194\/blogs\/paul"],"url":"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/1181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/comments?post=1181"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/1181\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/media?parent=1181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/categories?post=1181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/tags?post=1181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}