connected.
sql>
sql> -- repair table
sql>
sql> declare
2 begin
3 -- create repair table
4 dbms_repair.admin_tables (
5 -- table_name => 'repair_table',
6 table_type => dbms_repair.repair_table,
7 action => dbms_repair.create_action,
8 tablespace => 'users'); -- 如果是使用sys用户的缺省表空间,该项就不用指定
9 end;
10 /
pl/sql procedure successfully completed.
我们查询dba_objects,可以看到如下结果:
sql> select owner, object_name, object_type
2 from dba_objects
3 where object_name like '%repair_table';
owner object_name object_type
------------------------------------------------------------------
sys dba_repair_table view
sys repair_table table
sql>
sql> -- orphan key table
sql>
sql> declare
2 begin
3 -- create orphan key table
4 dbms_repair.admin_tables (
5 table_type => dbms_repair.orphan_table,
6 action => dbms_repair.create_action,
7 tablespace => 'users'); -- 如果是使用sys用户的缺省表空间,该项就不用指定
8 end;
9 /
pl/sql procedure successfully completed.
我们查询dba_objects,可以看到如下结果:
sql> select owner, object_name, object_type
2 from dba_objects
3 where object_name like '%orphan_key_table';
owner object_name object_type
------------------------------------------------------------------
sys dba_orphan_key_table view
sys orphan_key_table table
二、使用dbms_repair.check_object进行检测
check_object procedure检查指定的object,并且将关于损坏和修补的指导信息装入repair table。它将效验指定object中所有块的一致性。而在此之前已标识的块就会被跳过。
sql> @checkobject
sql> set serveroutput on
sql>
sql> declare
2 rpr_count int;
3 begin
4 rpr_count := 0;
5 dbms_repair.check_object (
6 schema_name => 'system',
7 object_name => 't1',
8 repair_table_name => 'repair_table',
9 corrupt_count => rpr_count);
10 dbms_output.put_line('repair count: ' || to_char(rpr_count));
11 end;
12 /
repair count: 1
pl/sql procedure successfully completed.
repair_table的结构如下:
sql> desc repair_table
name null? type
----------------------------------------- -------- ----------------------------
object_id not null number
tablespace_id not null number
relative_file_id not null number
block_id not null number
corrupt_type not null number
schema_name not null varchar2(30)
object_name not null varchar2(30)
baseobject_name varchar2(30)
partition_name varchar2(30)
corrupt_description varchar2(2000)
repair_description varchar2(200)
marked_corrupt not null varchar2(10)
check_timestamp not null date
fix_timestamp date
reformat_timestamp date
我们可以从repair_table中查询坏块的情况:
sql> select object_name, block_id, corrupt_type, marked_corrupt,
2 corrupt_description, repair_description
3 from repair_table;
object_name block_id corrupt_type marked_cor
------------------------------ ---------- ------------ ----------
corrupt_description
--------------------------------------------------------------------------------
repair_description
--------------------------------------------------------------------------------
t1 3 1 false
kdbchk: row locked by non-existent transaction
table=0 slot=0
lockid=32 ktbbhitc=1
mark block software corrupt
三、从坏块中进行数据抽取
从repair_table中可以知道file 6的block 3 坏了,但注意此时这个块还没有被标识为坏块,因此要在这个时候将任何有意义的数据赶快抽取出来。一旦该块被标识为坏块,整个块就会被跳过。
1、 通过alter system dump或trace中来获取块中包含的记录数 (nrows = 3).
2、 查询损坏的object,尽量抽取尽可能多的信息。
下面的查询可以用来从坏块中抢救数据。
建立一个临时表(temp_t1)以方便数据的插入:
sql> create table temp_t1 as
2 select * from system.t1
3 where dbms_rowid.rowid_block_number(rowid) = 3
4 and dbms_rowid.rowid_to_absolute_fno (rowid, 'system','t1') = 6;
table created.
sql> select col1 from temp_t1;
col1
----------
2
3
四、使用dbms_repair.fix_corrupt_blocks来标识坏块
fix_corrupt_blocks procedure用来根据repair table中的信息修正指定objects中的坏块。当这个块被标识为坏了以后,做全表扫描将引起ora-1578错。
sql> declare
2 fix_count int;
3 begin
4 fix_count := 0;
5 dbms_repair.fix_corrupt_blocks (
6 schema_name => 'system',
7 object_name => 't1',
8 object_type => dbms_repair.table_object,
9 repair_table_name => 'repair_table',
10 fix_count => fix_count);
11 dbms_output.put_line('fix count: ' || to_char(fix_count));
12 end;
13 /
fix count: 1
pl/sql procedure successfully completed.
查询repair_table可以看到block 3已经被标识:
sql> select object_name, block_id, marked_corrupt
2 from repair_table;
object_name block_id marked_cor
------------------------------ ---------- ----------
t1 3 true
这时再对table t1做全表扫描,ora-1578将会出现。
sql> select * from system.t1;
select * from system.t1
*
error at line 1:
ora-01578: oracle data block corrupted (file # 6, block # 3)
ora-01110: data file 6: '/tmp/ts_corrupt.dbf'
五、使用dbms_repair.dump_orphan_keys来修补相关的index
dump_orphan_keys将会显示指向数据坏块中记录的index entries
下列查询显示与坏块相关的index。
sql> select index_name from dba_indexes
2 where table_name in (select distinct object_name from repair_table);
index_name
------------------------------
t1_pk
sql> @dumporphankeys
sql> set serveroutput on
sql>
sql> declare
2 key_count int;
3 begin
4 key_count := 0;
5 dbms_repair.dump_orphan_keys (
6 schema_name => 'system',
7 object_name => 't1_pk',
8 object_type => dbms_repair.index_object,
9 repair_table_name => 'repair_table',
10 orphan_table_name => 'orphan_key_table',
11 key_count => key_count);
12 dbms_output.put_line('orphan key count: ' || to_char(key_count));
13 end;
14 /
orphan key count: 3
pl/sql procedure successfully completed.
orphan_key_table的结构如下:
sql> desc orphan_key_table
name null? type
----------------------------------------- -------- ----------------------------
schema_name not null varchar2(30)
index_name not null varchar2(30)
ipart_name varchar2(30)
index_id not null number
table_name not null varchar2(30)
part_name varchar2(30)
table_id not null number
keyrowid not null rowid
key not null rowid
dump_timestamp not null date
下列查询显示t1_pk index中有3个index entries与坏块有关:
sql> select index_name, count(*) from orphan_key_table
2 group by index_name;
index_name count(*)
------------------------------ ----------
t1_pk 3
在orphan_key_table 中的index entry意味着该index应该重建,以保证一个table的指针和它的index指针返回同样的结果集合。
六、使用dbms_repair.skip_corrupt_blocks来跳过坏块
skip_corrupt_blocks用来决定在对指定object的index和table做搜索时是否跳过坏块。
如果index和table不同步,那么一个‘set transaction read only'的transaction可能会出现不一致的情况,例如一个查询仅仅指向index,而它的子查询却同时指向index和table。 如果table的block已经被标识为坏了,那么这两个查询将会返回不同的结果。
建议:如果skip_corrupt_blocks被enable,那么必须重建orphan_key_table中确定的所有indexes(或所有与该object相关的indexes,如果在dump_orphan_keys被忽略的情况下)。
sql> @skipcorruptblocks
sql> declare
2 begin
3 dbms_repair.skip_corrupt_blocks (
4 schema_name => 'system',
5 object_name => 't1',
6 object_type => dbms_repair.table_object,
7 flags => dbms_repair.skip_flag);
8 end;
9 /
pl/sql procedure successfully completed.
下列查询显示跳过坏块已经enable。
sql> select table_name, skip_corrupt from dba_tables
2 where table_name = 't1';
table_name skip_cor
------------------------------ --------
t1 enabled
坏块中的记录被跳过后,全表扫描不再显示错误。
sql> select * from system.t1;
col1 col2
--------------------------------------------
4 dddd
5 eeee
注意此时pk index还没有被修正。不能往t1 table中插入数据
sql> insert into system.t1 values (1,'aaaa');
insert into system.t1 values (1,'aaaa')
*
sql> select * from system.t1 where col1 = 1;
no rows selected
七、使用dbms_repair.rebuild_freelists重建freelists
rebuild_freelists重建指定object的freelists。
sql> declare
2 begin
3 dbms_repair.rebuild_freelists (
4 schema_name => 'system',
5 object_name => 't1',
6 object_type => dbms_repair.table_object);
7 end;
8 /
pl/sql procedure successfully completed.
八、重建index
在orphan_key_table中确定的每一个index都必须重建,以保证查询结果的一致。
sql> alter index system.t1_pk rebuild online;
index altered.
重建index后,就能往t1 table中插入数据。
sql> insert into system.t1 values (1, 'aaaa');
1 row created.
sql> select * from system.t1;
col1 col2
--------------------------------------------
4 dddd
5 eeee
1 aaaa
以上的insert语句只是提供一个简单的例子。如果我们真能知道丢失的数据内容,那当然是最好的。临时表(temp_t1)应该用来存放所有从坏块中抽取的记录。
到此时table t1已经可以被再用,但同时数据也有丢失。一般来说,在使用dbms_repair package之前,应认真考虑数据的丢失问题,因为从index segment和table dump中采集信息非常复杂,同时逻辑上的不一致也可能被引入。要记住:在dbms_repair package 初始版本中,“修补坏块”的功能仅仅是“将块标识为由软件引起的坏块”而已。
在进行上述操作前,必须先阅读〈oracle8i administrator's guide〉中“detecting and repairing data block corruption"章节,同时对风险进行评估。所有操作最好是在oracle工程师的指导下进行。