{"id":1117,"date":"2007-12-05T19:37:28","date_gmt":"2007-12-05T19:37:28","guid":{"rendered":"\/blogs\/paul\/post\/Search-Engine-QA-10-When-are-pages-from-a-truncated-table-reused.aspx"},"modified":"2013-01-19T10:08:48","modified_gmt":"2013-01-19T18:08:48","slug":"search-engine-qa-10-when-are-pages-from-a-truncated-table-reused","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-10-when-are-pages-from-a-truncated-table-reused\/","title":{"rendered":"Search Engine Q&#038;A #10: When are pages from a truncated table reused?"},"content":{"rendered":"<p>This is a question I was sent a week or so ago &#8211; if a table is truncated inside a transaction, what protects the integrity of the table&#8217;s pages in case the transaction rolls back? Let&#8217;s find out.<\/p>\n<p>First off I&#8217;ll create a simple table to experiment with:<\/p>\n<pre class=\"brush: sql; gutter: true; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nCREATE TABLE &#x5B;TruncateTest] (\r\n    &#x5B;c1] INT IDENTITY,\r\n    &#x5B;c2] CHAR (8000) DEFAULT 'A');\r\nGO\r\n\r\nSET NOCOUNT ON;\r\nGO\r\n\r\nDECLARE @a INT;\r\nSELECT @a = 1;\r\nWHILE (@a &lt; 20)\r\nBEGIN\r\n    INSERT INTO &#x5B;TruncateTest] DEFAULT VALUES;\r\n    SELECT @a = @a + 1;\r\nEND;\r\nGO\r\n<\/pre>\n<p>We can see what pages and extents are allocated to the table using the undocumented <em>DBCC IND<\/em> command:<\/p>\n<pre class=\"brush: sql; gutter: true; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nDBCC IND (&#x5B;test], &#x5B;TruncateTest], 0);\r\nGO\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nPageFID PagePID\r\n------- -------\r\n1       193\r\n1       192\r\n1       194\r\n1       195\r\n1       196\r\n1       197\r\n1       198\r\n1       199\r\n1       200\r\n1       224\r\n1       225\r\n1       226\r\n1       227\r\n1       228\r\n1       229\r\n1       230\r\n1       231\r\n1       232\r\n1       233\r\n1       234\r\n\r\nDBCC execution completed. If DBCC printed error messages, contact your system administrator.\r\n<\/pre>\n<p>I&#8217;ve edited the output to just the page IDs and we can see that there are\u00a04 extents used by this table (starting on pages <em>(1:192)<\/em>, <em>(1:200)<\/em>, <em>(1:224)<\/em>, and <em>(1:232)<\/em>). Now if we truncate the table in a transaction, what will <em>DBCC IND<\/em> show?<\/p>\n<pre class=\"brush: sql; gutter: true; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nBEGIN TRAN;\r\nGO\r\n\r\nTRUNCATE TABLE &#x5B;TruncateTest];\r\nGO\r\n\r\nDBCC IND (&#x5B;test], &#x5B;TruncateTest], 0);\r\nGO\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nDBCC execution completed. If DBCC printed error messages, contact your system administrator.\r\n<\/pre>\n<p>Looks like there are no pages allocated to the table. So where are they? Let&#8217;s check what locks there are. Instead of using <em>sp_lock<\/em>, I&#8217;m going to use it&#8217;s replacement DMV, <em>sys.dm_tran_locks<\/em>:<\/p>\n<pre class=\"brush: sql; gutter: true; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nSELECT\r\n    &#x5B;resource_type],\r\n    &#x5B;resource_description],\r\n    &#x5B;request_mode]\r\nFROM\r\n    sys.dm_tran_locks\r\nWHERE\r\n    &#x5B;resource_type] IN (N'EXTENT', N'PAGE');\r\nGO\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nresource_type  resource_description  request_mode\r\n-------------- --------------------- -------------\r\nEXTENT         1:200                 X\r\nPAGE           1:198                 X\r\nPAGE           1:199                 X\r\nPAGE           1:196                 X\r\nPAGE           1:197                 X\r\nPAGE           1:194                 X\r\nPAGE           1:195                 X\r\nPAGE           1:192                 X\r\nPAGE           1:193                 X\r\nEXTENT         1:192                 X\r\nPAGE           1:200                 X\r\nEXTENT         1:232                 X\r\nEXTENT         1:224                 X\r\n<\/pre>\n<p>Ah &#8211; all the pages and extents are locked. The table doesn&#8217;t show them as allocated any more but because they&#8217;re exclusively locked, the allocation subsystem can&#8217;t <em>really<\/em> deallocate them until the locks are dropped (when the transaction commits). That&#8217;s the answer &#8211; they can&#8217;t be reused until they&#8217;re really deallocated. If a transaction rollback happens, the pages are just marked as allocated again.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a question I was sent a week or so ago &#8211; if a table is truncated inside a transaction, what protects the integrity of the table&#8217;s pages in case the transaction rolls back? Let&#8217;s find out. First off I&#8217;ll create a simple table to experiment with: CREATE TABLE &#x5B;TruncateTest] ( &#x5B;c1] INT IDENTITY, [&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,48,57,62],"tags":[],"class_list":["post-1117","post","type-post","status-publish","format-standard","hentry","category-example-scripts","category-inside-the-storage-engine","category-locking","category-on-disk-structures"],"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 #10: When are pages from a truncated table reused? - 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-10-when-are-pages-from-a-truncated-table-reused\/\" \/>\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 #10: When are pages from a truncated table reused? - Paul S. Randal\" \/>\n<meta property=\"og:description\" content=\"This is a question I was sent a week or so ago &#8211; if a table is truncated inside a transaction, what protects the integrity of the table&#8217;s pages in case the transaction rolls back? Let&#8217;s find out. First off I&#8217;ll create a simple table to experiment with: CREATE TABLE &#x5B;TruncateTest] ( &#x5B;c1] INT IDENTITY, [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-10-when-are-pages-from-a-truncated-table-reused\/\" \/>\n<meta property=\"og:site_name\" content=\"Paul S. Randal\" \/>\n<meta property=\"article:published_time\" content=\"2007-12-05T19:37:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-01-19T18:08:48+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=\"2 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-10-when-are-pages-from-a-truncated-table-reused\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-10-when-are-pages-from-a-truncated-table-reused\/\",\"name\":\"Search Engine Q&A #10: When are pages from a truncated table reused? - Paul S. Randal\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\"},\"datePublished\":\"2007-12-05T19:37:28+00:00\",\"dateModified\":\"2013-01-19T18:08:48+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-10-when-are-pages-from-a-truncated-table-reused\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-10-when-are-pages-from-a-truncated-table-reused\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-10-when-are-pages-from-a-truncated-table-reused\/#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 #10: When are pages from a truncated table reused?\"}]},{\"@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 #10: When are pages from a truncated table reused? - 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-10-when-are-pages-from-a-truncated-table-reused\/","og_locale":"en_US","og_type":"article","og_title":"Search Engine Q&A #10: When are pages from a truncated table reused? - Paul S. Randal","og_description":"This is a question I was sent a week or so ago &#8211; if a table is truncated inside a transaction, what protects the integrity of the table&#8217;s pages in case the transaction rolls back? Let&#8217;s find out. First off I&#8217;ll create a simple table to experiment with: CREATE TABLE &#x5B;TruncateTest] ( &#x5B;c1] INT IDENTITY, [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-10-when-are-pages-from-a-truncated-table-reused\/","og_site_name":"Paul S. Randal","article_published_time":"2007-12-05T19:37:28+00:00","article_modified_time":"2013-01-19T18:08:48+00:00","author":"Paul Randal","twitter_misc":{"Written by":"Paul Randal","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-10-when-are-pages-from-a-truncated-table-reused\/","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-10-when-are-pages-from-a-truncated-table-reused\/","name":"Search Engine Q&A #10: When are pages from a truncated table reused? - Paul S. Randal","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website"},"datePublished":"2007-12-05T19:37:28+00:00","dateModified":"2013-01-19T18:08:48+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-10-when-are-pages-from-a-truncated-table-reused\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-10-when-are-pages-from-a-truncated-table-reused\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/search-engine-qa-10-when-are-pages-from-a-truncated-table-reused\/#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 #10: When are pages from a truncated table reused?"}]},{"@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\/1117","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=1117"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/1117\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/media?parent=1117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/categories?post=1117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/tags?post=1117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}