【赛迪网-it技术报道】这篇论坛文章(赛迪网技术社区)主要介绍了使用dbms_repair检测和修补数据坏块的具体方法,详细内容请参考下文。
oracle提供了许多方法检测和修补数据库中的数据坏块,而dbms_repair package就是其中之一。
对任何可能导致数据丢失的损坏,我们都要仔细的分析,以求理解所要涉及的数据。就修补坏块本身来说, 它可能会丢失数据,也可能会导致数据在逻辑上不一致;因此在进行修补坏块之前,必须仔细权衡使用dbms_repair的得失。
dbms_repair package 仅仅对transaction层和data层的坏块(即通常所说的由软件引起的坏块)起作用,对物理上损坏的块,在它被读到缓冲区中时就已被标识出来了,而dbms_repair会忽略所有被标识为坏了的块。
在dbms_repair package 初始版本中,“修补坏块”的功能仅仅是“将块标识为由软件引起的坏块”
使用dbms_repair package的注意事项:
1、 db_block_checking和db_block_checksum要设置为false.
2、 在使用dbms_repair之前,有坏块的文件应做一个备份。
下面我们就通过一个例子来说明dbms_repair package是如何检测和修补坏块的。
例如,table t1(结构如下)中存在一个坏块:
sql> desc t1
name null? type
------------------------ -------- --------------------
col1 not null number(38)
col2 char(512)
用analyze命令对table t1进行分析后,会得到如下错误提示:
sql> analyze table t1 validate structure;
analyze table t1 validate structure
*
error at line 1:
ora-01498: block check failure
在analyze产生的trace文件中,可以知道坏块中包含3条记录的数据(nrows = 3),
trace文件中主要的内容如下:
dump file /export/home/oracle/product/8.1.5
/admin/v815/udump/v815_ora_2835.trc
oracle8 enterprise edition release 8.1.5.0.0
with the partitioning option
*** 1998.12.16.15.53.02.000
*** session id:(7.6) 1998.12.16.15.53.02.000
kdbchk: row locked by non-existent transaction
table=0 slot=0
lockid=32 ktbbhitc=1
block header dump: 0x01800003
object id on block? y
seg/obj: 0xb6d csc: 0x00.1cf5f itc: 1 flg: - typ: 1 - data
fsl: 0 fnx: 0x0 ver: 0x01
itl xid uba flag lck scn/fsc
0x01 xid: 0x0002.011.00000121 uba: 0x008018fb.0345.0d --u- 3 fsc
0x0000.0001cf60
data_block_dump
===============
tsiz: 0x7b8
hsiz: 0x18
pbl: 0x28088044
bdba: 0x01800003
flag=-----------
ntab=1
nrow=3
frre=-1
fsbo=0x18
fseo=0x19d
avsp=0x185
tosp=0x185
0xe:pti[0] nrow=3 offs=0
0x12:pri[0] offs=0x5ff
0x14:pri[1] offs=0x3a6
0x16:pri[2] offs=0x19d
block_row_dump:
(注:其余的省略)
end_of_block_dump
一、首先使用dbms_repair.admin_tables来建立repair table和orphan key table,
并且为repair table和orphan key tables提供管理功能
sql> @admincreate
sql> connect sys/
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工程师的指导下进行。
闽公网安备 35060202000074号