{"id":1207,"date":"2016-12-06T18:27:33","date_gmt":"2016-12-06T18:27:33","guid":{"rendered":"http:\/\/nenadnoveljic.com\/blog\/?p=1207"},"modified":"2018-08-21T19:04:21","modified_gmt":"2018-08-21T19:04:21","slug":"rescuing-data-corrupted-tables","status":"publish","type":"post","link":"https:\/\/nenadnoveljic.com\/blog\/rescuing-data-corrupted-tables\/","title":{"rendered":"Rescuing Data from Corrupted Tables"},"content":{"rendered":"<p>This blog post describes how to partially rescue data from corrupted table segments if you\u00a0find yourself in a miserable\u00a0position\u00a0of not having\u00a0a valid backup at your disposal. At the risk of sounding trivial, you can use <em>alter index rebuild online<\/em> command for recovering from an index block corruption.<\/p>\n<p><strong>This\u00a0method\u00a0inevitably\u00a0leaves you\u00a0with a data loss. Therefore, don&#8217;t\u00a0rely on it\u00a0in production environments. Instead, you should always\u00a0use well documented and regularly tested RMAN backup &amp; restore\u00a0scripts\u00a0to recover from data corruption.<\/strong><\/p>\n<p>I developed the procedure\u00a0while dealing with\u00a0an OS\u00a0block\u00a0corruption on\u00a0less important\u00a0test\u00a0databases which didn&#8217;t have any backup &amp; restore requirements. In this particular case, it was enough to discard corrupted data from the table.<\/p>\n<p>If you happen to hit such a problem, it is always important to do\u00a0the root-cause analysis and resolve the underlying issue on the OS level. In this case, the corruption was caused by an OS bug which\u00a0reared it&#8217;s ugly\u00a0head\u00a0while\u00a0Unix administrators tested a migration procedure.<\/p>\n<p>These are the error messages I got when trying to retrieve the corrupted data:<\/p>\n<pre><code>set autotrace traceonly\r\nselect * from APP_TABLE ;\r\nERROR:\r\nORA-01115: IO error reading block from file  (block # )\r\nORA-01115: IO error reading block from file 7 (block # 735744)\r\nORA-27072: File I\/O error\r\nSolaris-AMD64 Error: 5: I\/O error\r\nAdditional information: 4\r\nAdditional information: 735744\r\nAdditional information: 4294967295<\/code><\/pre>\n<p>The error messages above tell us that\u00a0a block couldn&#8217;t be retrieved from the file system.<\/p>\n<p>If you see\u00a0a similar\u00a0error message in an RMAN log, you can use the following query to\u00a0obtain the\u00a0information about the\u00a0affected\u00a0object:<\/p>\n<pre><code>select * from dba_extents \r\n  where file_id = 7 \r\n    and 735747 between block_id and block_id + blocks -1 ;\r\n<\/code><\/pre>\n<p>The\u00a0anonymous PL\/SQL block below\u00a0is based on the fact that all the <em>rowids<\/em> can be retrieved from a non-corrupted index. The <em>INDEX<\/em> hint\u00a0is used to avoid\u00a0the full table scan which would stumble over the corrupted blocks. The <em>rowids<\/em>\u00a0obtained by index scan\u00a0are\u00a0consequently used\u00a0to query the rows from the table, one at a time. The row\u00a0will be\u00a0stored in <em>good_rows<\/em> table if it can be retrieved. Otherwise,\u00a0its index key\u00a0will be\u00a0stored in the table <em>corrupted_keys. <\/em>Preferably use the index on the primary key. By doing so you will be able to unambiguously identify discarded\u00a0rows.\u00a0\u00a0I explicitly defined an exception for the OS read error (<em>ORA-1115<\/em>). Other exceptions are deliberately\u00a0left unhandled.<\/p>\n<pre><code>create table good_rows as select * from APP_TABLE where rownum=0 ;\r\n \r\ncreate table corrupted_keys as\r\n  select KEY from APP_TABLE where rownum=0 ;\r\n \r\ndeclare\r\n  read_file_error EXCEPTION ;\r\n  pragma exception_init(read_file_error,-1115) ;\r\nbegin\r\n  for r_row_id in ( select \/*+ INDEX(t INDEX_NAME) *\/ rowid from APP_TABLE t )\r\n  loop\r\n    begin\r\n      insert into good_rows\r\n        select * from APP_TABLE where rowid=r_row_id.rowid  ;\r\n    exception when read_file_error then\r\n      insert into corrupted_keys\r\n        select \/*+ INDEX(t INDEX_NAME) *\/ KEY\r\n          from APP_TABLE t where rowid=r_row_id.rowid ;\r\n    end ;  \r\n  end loop ;\r\nend ; <\/code><\/pre>\n<p>Replace the following\u00a0for your particular case:<\/p>\n<ul>\n<li>APP_TABLE &#8211; corrupted table name<\/li>\n<li>INDEX_NAME &#8211; use preferably the primary key.<\/li>\n<li>KEY &#8211; column(s) in the index<\/li>\n<\/ul>\n<p>If\u00a0there are some\u00a0referential integrity constraints defined, you will have to take care of the dependencies too.<\/p>\n<p>On condition that everything\u00a0went well, you can finally copy the data from the <em>good_rows<\/em> table into the original table:<\/p>\n<pre><code>truncate table APP_TABLE ;\r\n\r\ninsert into APP_TABLE select * from good_rows ;\r\ncommit ;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This blog post describes how to partially rescue data from corrupted table segments if you find yourself in a miserable position of not having a valid backup at your disposal. <a href=\"https:\/\/nenadnoveljic.com\/blog\/rescuing-data-corrupted-tables\/\" class=\"more-link\">Continue Reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[25,5],"tags":[],"class_list":["post-1207","post","type-post","status-publish","format-standard","hentry","category-corruption","category-oracle"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Rescuing Data from Corrupted Tables - All-round Database Topics<\/title>\n<meta name=\"description\" content=\"This blog post describes how to partially rescue data from corrupted table segments if you find yourself in a miserable position of not having a valid backup at your disposal.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nenadnoveljic.com\/blog\/rescuing-data-corrupted-tables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Rescuing Data from Corrupted Tables - All-round Database Topics\" \/>\n<meta property=\"og:description\" content=\"This blog post describes how to partially rescue data from corrupted table segments if you find yourself in a miserable position of not having a valid backup at your disposal.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nenadnoveljic.com\/blog\/rescuing-data-corrupted-tables\/\" \/>\n<meta property=\"og:site_name\" content=\"All-round Database Topics\" \/>\n<meta property=\"article:published_time\" content=\"2016-12-06T18:27:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-08-21T19:04:21+00:00\" \/>\n<meta name=\"author\" content=\"Nenad Noveljic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@NenadNoveljic\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nenad Noveljic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/rescuing-data-corrupted-tables\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/rescuing-data-corrupted-tables\\\/\"},\"author\":{\"name\":\"Nenad Noveljic\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"headline\":\"Rescuing Data from Corrupted Tables\",\"datePublished\":\"2016-12-06T18:27:33+00:00\",\"dateModified\":\"2018-08-21T19:04:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/rescuing-data-corrupted-tables\\\/\"},\"wordCount\":429,\"commentCount\":0,\"articleSection\":[\"corruption\",\"Oracle\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/rescuing-data-corrupted-tables\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/rescuing-data-corrupted-tables\\\/\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/rescuing-data-corrupted-tables\\\/\",\"name\":\"Rescuing Data from Corrupted Tables - All-round Database Topics\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\"},\"datePublished\":\"2016-12-06T18:27:33+00:00\",\"dateModified\":\"2018-08-21T19:04:21+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"description\":\"This blog post describes how to partially rescue data from corrupted table segments if you find yourself in a miserable position of not having a valid backup at your disposal.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/rescuing-data-corrupted-tables\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/rescuing-data-corrupted-tables\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/rescuing-data-corrupted-tables\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Rescuing Data from Corrupted Tables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\",\"name\":\"All-round Database Topics\",\"description\":\"Nenad Noveljic\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\",\"name\":\"Nenad Noveljic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"caption\":\"Nenad Noveljic\"},\"sameAs\":[\"nenad-noveljic-9b746a6\",\"https:\\\/\\\/x.com\\\/NenadNoveljic\"],\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/author\\\/nenad\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Rescuing Data from Corrupted Tables - All-round Database Topics","description":"This blog post describes how to partially rescue data from corrupted table segments if you find yourself in a miserable position of not having a valid backup at your disposal.","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:\/\/nenadnoveljic.com\/blog\/rescuing-data-corrupted-tables\/","og_locale":"en_US","og_type":"article","og_title":"Rescuing Data from Corrupted Tables - All-round Database Topics","og_description":"This blog post describes how to partially rescue data from corrupted table segments if you find yourself in a miserable position of not having a valid backup at your disposal.","og_url":"https:\/\/nenadnoveljic.com\/blog\/rescuing-data-corrupted-tables\/","og_site_name":"All-round Database Topics","article_published_time":"2016-12-06T18:27:33+00:00","article_modified_time":"2018-08-21T19:04:21+00:00","author":"Nenad Noveljic","twitter_card":"summary_large_image","twitter_creator":"@NenadNoveljic","twitter_misc":{"Written by":"Nenad Noveljic","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/nenadnoveljic.com\/blog\/rescuing-data-corrupted-tables\/#article","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/rescuing-data-corrupted-tables\/"},"author":{"name":"Nenad Noveljic","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"headline":"Rescuing Data from Corrupted Tables","datePublished":"2016-12-06T18:27:33+00:00","dateModified":"2018-08-21T19:04:21+00:00","mainEntityOfPage":{"@id":"https:\/\/nenadnoveljic.com\/blog\/rescuing-data-corrupted-tables\/"},"wordCount":429,"commentCount":0,"articleSection":["corruption","Oracle"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nenadnoveljic.com\/blog\/rescuing-data-corrupted-tables\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nenadnoveljic.com\/blog\/rescuing-data-corrupted-tables\/","url":"https:\/\/nenadnoveljic.com\/blog\/rescuing-data-corrupted-tables\/","name":"Rescuing Data from Corrupted Tables - All-round Database Topics","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#website"},"datePublished":"2016-12-06T18:27:33+00:00","dateModified":"2018-08-21T19:04:21+00:00","author":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"description":"This blog post describes how to partially rescue data from corrupted table segments if you find yourself in a miserable position of not having a valid backup at your disposal.","breadcrumb":{"@id":"https:\/\/nenadnoveljic.com\/blog\/rescuing-data-corrupted-tables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nenadnoveljic.com\/blog\/rescuing-data-corrupted-tables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/nenadnoveljic.com\/blog\/rescuing-data-corrupted-tables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nenadnoveljic.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Rescuing Data from Corrupted Tables"}]},{"@type":"WebSite","@id":"https:\/\/nenadnoveljic.com\/blog\/#website","url":"https:\/\/nenadnoveljic.com\/blog\/","name":"All-round Database Topics","description":"Nenad Noveljic","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nenadnoveljic.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa","name":"Nenad Noveljic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","caption":"Nenad Noveljic"},"sameAs":["nenad-noveljic-9b746a6","https:\/\/x.com\/NenadNoveljic"],"url":"https:\/\/nenadnoveljic.com\/blog\/author\/nenad\/"}]}},"_links":{"self":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/1207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/comments?post=1207"}],"version-history":[{"count":1,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/1207\/revisions"}],"predecessor-version":[{"id":1258,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/1207\/revisions\/1258"}],"wp:attachment":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/media?parent=1207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/categories?post=1207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/tags?post=1207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}