[20190226]测试使用bbed恢复索引.txt

--//上午做tab$删除恢复测试时发现,tab$的索引i_tab1很小.可以尝试使用bbed解决这个问题.
--//首先在普通表上做一个测试看看.

1.环境:
SCOTT@book> @ ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

SCOTT@book> create table empy as select * from emp ;
Table created.

SCOTT@book> create index i_empy_empno on empy(empno);
Index created.

SCOTT@book> select rowid,empy.* from empy where rownum=1;
ROWID                   EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
------------------ ---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
AAAWPZAAEAAAAILAAA       7369 SMITH      CLERK           7902 1980-12-17 00:00:00        800                    20

SCOTT@book> @ rowid AAAWPZAAEAAAAILAAA
    OBJECT       FILE      BLOCK        ROW ROWID_DBA            DBA                  TEXT
---------- ---------- ---------- ---------- -------------------- -------------------- ----------------------------------------
     91097          4        523          0  0x100020B           4,523                alter system dump datafile 4 block 523 ;

SCOTT@book> select HEADER_FILE,HEADER_BLOCK,segment_name from dba_segments where owner=user and segment_name='I_EMPY_EMPNO';
HEADER_FILE HEADER_BLOCK SEGMENT_NAME
----------- ------------ --------------------
          4          530 I_EMPY_EMPNO
--//索引的roor节点在dba=4,531.

2.删除记录看看:

SCOTT@book> delete from empy where empno not in (7369);
13 rows deleted.

SCOTT@book> commit ;
Commit complete.

--//仅仅保留1条.并且这条是empy表最小的empno号.

SCOTT@book> alter system checkpoint ;
System altered.

3.先恢复表:
BBED> set dba 4,523
        DBA             0x0100020b (16777739 4,523)

BBED> x /rnccntnnn *kdbr[0]
rowdata[529]                                @8150
------------
flag@8150: 0x2c (KDRHFL, KDRHFF, KDRHFH)
lock@8151: 0x00
cols@8152:    8

col    0[3] @8153: 7369
col    1[5] @8157: SMITH
col    2[5] @8163: CLERK
col    3[3] @8169: 7902
col    4[7] @8173: 1980-12-17 00:00:00
col    5[2] @8181: 800
col    6[0] @8184: *NULL*
col    7[2] @8185: 20
--//这条记录保留.

BBED> x /rnccntnnn *kdbr[1]
rowdata[486]                                @8107
------------
flag@8107: 0x3c (KDRHFL, KDRHFF, KDRHFD, KDRHFH)
lock@8108: 0x02
cols@8109:    0

--//执行如下生成bbed脚本:
$ seq 1 13 | xargs -I{} echo 'x /rnccntnnn dba 4,523 *kdbr[{}]' | rlbbed | grep -B1 "^lock@" | grep "^flag@.*=0x3c" | cut -d: -f1| cut -d"@" -f2 | xargs -I{} echo assign dba 4,523 offset {}=0x2c
assign dba 4,523 offset 8107=0x2c
assign dba 4,523 offset 8064=0x2c
assign dba 4,523 offset 8023=0x2c
assign dba 4,523 offset 7978=0x2c
assign dba 4,523 offset 7937=0x2c
assign dba 4,523 offset 7896=0x2c
assign dba 4,523 offset 7856=0x2c
assign dba 4,523 offset 7818=0x2c
assign dba 4,523 offset 7775=0x2c
assign dba 4,523 offset 7737=0x2c
assign dba 4,523 offset 7699=0x2c
assign dba 4,523 offset 7660=0x2c
assign dba 4,523 offset 7621=0x2c

--//执行如上脚本,去除删除标识.

BBED> sum apply
Check value for File 4, Block 523:
current = 0x8c05, required = 0x8c05

BBED> verify
DBVERIFY - Verification starting
FILE = /mnt/ramdisk/book/users01.dbf
BLOCK = 523

Block Checking: DBA = 16777739, Block Type = KTB-managed data block
data header at 0x7f6d56b9427c
kdbchk: the amount of space used is not equal to block size
        used=613 fsc=503 avsp=7451 dtl=8064
Block 523 failed with check code 6110

--//dtl-used-fsc = avsp

BBED> p ktbbh.ktbbhitl[1]._ktbitun._ktbitfsc
sb2 _ktbitfsc                               @86       503

BBED> assign ktbbh.ktbbhitl[1]._ktbitun._ktbitfsc=0
sb2 _ktbitfsc                               @86       0

BBED> sum apply
Check value for File 4, Block 523:
current = 0x8df2, required = 0x8df2

BBED> verify
DBVERIFY - Verification starting
FILE = /mnt/ramdisk/book/users01.dbf
BLOCK = 523

Block Checking: DBA = 16777739, Block Type = KTB-managed data block
data header at 0xe9427c
kdbchk: space available on commit is incorrect
        tosp=7980 fsc=0 stb=0 avsp=7451
Block 523 failed with check code 6111

--//avsp+fsc+stb=tops.

BBED> assign kdbh.kdbhtosp=kdbh.kdbhavsp
sb2 kdbhtosp                                @136      7451

BBED> sum apply
Check value for File 4, Block 523:
current = 0x8fc5, required = 0x8fc5

BBED> verify
DBVERIFY - Verification starting
FILE = /mnt/ramdisk/book/users01.dbf
BLOCK = 523

--//OK, 现在对应块已经修复.

SCOTT@book> alter system flush buffer_cache;
System altered.

SCOTT@book> select * from empy ;
     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 1980-12-17 00:00:00        800                    20
      7499 ALLEN      SALESMAN        7698 1981-02-20 00:00:00       1600        300         30
      7521 WARD       SALESMAN        7698 1981-02-22 00:00:00       1250        500         30
      7566 JONES      MANAGER         7839 1981-04-02 00:00:00       2975                    20
      7654 MARTIN     SALESMAN        7698 1981-09-28 00:00:00       1250       1400         30
      7698 BLAKE      MANAGER         7839 1981-05-01 00:00:00       2850                    30
      7782 CLARK      MANAGER         7839 1981-06-09 00:00:00       2450                    10
      7788 SCOTT      ANALYST         7566 1987-04-19 00:00:00       3000                    20
      7839 KING       PRESIDENT            1981-11-17 00:00:00       5000                    10
      7844 TURNER     SALESMAN        7698 1981-09-08 00:00:00       1500          0         30
      7876 ADAMS      CLERK           7788 1987-05-23 00:00:00       1100                    20
      7900 JAMES      CLERK           7698 1981-12-03 00:00:00        950                    30
      7902 FORD       ANALYST         7566 1981-12-03 00:00:00       3000                    20
      7934 MILLER     CLERK           7782 1982-01-23 00:00:00       1300                    10
14 rows selected.

SCOTT@book> select * from empy where empno in(7499,7369);
     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 1980-12-17 00:00:00        800                    20

--//可以发现走索引仅仅找到1条.

SCOTT@book> ANALYZE TABLE empy VALIDATE STRUCTURE CASCADE;
ANALYZE TABLE empy VALIDATE STRUCTURE CASCADE
*
ERROR at line 1:
ORA-01499: table/index cross reference failure - see trace file
--//一般普通表的索引不用恢复,根据表重建就ok了.但是对于sys.tab$的索引不行,这也是做这个测试的原因.

4.恢复索引看看.
BBED> set dba 4,531
        DBA             0x01000213 (16777747 4,531)

BBED> p kd_off
sb2 kd_off[0]                               @132      8032
sb2 kd_off[1]                               @134      0
sb2 kd_off[2]                               @136      8019
sb2 kd_off[3]                               @138      8006
sb2 kd_off[4]                               @140      7993
sb2 kd_off[5]                               @142      7980
sb2 kd_off[6]                               @144      7967
sb2 kd_off[7]                               @146      7954
sb2 kd_off[8]                               @148      7941
sb2 kd_off[9]                               @150      7928
sb2 kd_off[10]                              @152      7915
sb2 kd_off[11]                              @154      7902
sb2 kd_off[12]                              @156      7889
sb2 kd_off[13]                              @158      7877

--//可以发现bbed查看索引有问题,kd_off[0],kd_off[1]看到的偏移是不对的.实际上kd_off偏移从136开始kd_off[2].

BBED> x /rnx *kd_off[2]
rowdata[172]                                @8119
------------
flag@8119:     0x00 (NONE)
lock@8120:     0x00
data key:
col    0[3] @8122: 7369
col    1[6] @8126:  0x01  0x00  0x02  0x0b  0x00  0x00

--//这个表的第一条记录(最小值),我没有删除.empno=7369.

BBED> x /rnx *kd_off[3]
rowdata[159]                                @8106
------------
flag@8106:     0x01 (KDXRDEL)
lock@8107:     0x02
data key:
col    0[3] @8109: 7499
col    1[6] @8113:  0x01  0x00  0x02  0x0b  0x00  0x01
--//对比删除与不删除的可以看出.flag不同,删除的flag=0x01.

$ seq 2 13 | xargs -I{} echo 'x /rnx dba 4,531 *kd_off[{}]' | rlbbed | grep -B1 "^lock@.*:*0x02$" | grep "flag@.*0x01" | cut -d: -f1 | cut -d@ -f2 |xargs -I{} echo assign dba 4,531 offset {}=0x00
assign dba 4,531 offset 8106=0x00
assign dba 4,531 offset 8093=0x00
assign dba 4,531 offset 8080=0x00
assign dba 4,531 offset 8067=0x00
assign dba 4,531 offset 8054=0x00
assign dba 4,531 offset 8041=0x00
assign dba 4,531 offset 8028=0x00
assign dba 4,531 offset 8015=0x00
assign dba 4,531 offset 8002=0x00
assign dba 4,531 offset 7989=0x00
assign dba 4,531 offset 7977=0x00
--//仅仅修改11条,还有2条需要修改标识.

BBED> dump  offset 160 count 4
 File: /mnt/ramdisk/book/users01.dbf (4)
 Block: 531             Offsets:  160 to  163   Dba:0x01000213
---------------------------------------------------------------
 b81eab1e

<64 bytes per line>

--// 说明: kd_off[13]的偏移量记录在偏移158,下面2条记录的偏移记录从160算起.
--// b81e 颠倒过来就是  1eb8=7864 , 记录相对偏移从kdxle算起(当前是100), 7864+100 = 7964
--// ab1e 颠倒过来就是  1eab=7851 , 记录相对偏移从kdxle算起(当前是100), 7851+100  = 7951

BBED> x /rnx offset 7964
rowdata[17]                                 @7964
-----------
flag@7964:     0x01 (KDXRDEL)
lock@7965:     0x02
data key:
col    0[3] @7967: 7902
col    1[6] @7971:  0x01  0x00  0x02  0x0b  0x00  0x0c

BBED> x /rnx offset 7951
rowdata[4]                                  @7951
----------
flag@7951:     0x01 (KDXRDEL)
lock@7952:     0x02
data key:
col    0[3] @7954: 7934
col    1[6] @7958:  0x01  0x00  0x02  0x0b  0x00  0x0d

--//也就是要补充执行如下:
assign dba 4,531 offset 7964=0x00
assign dba 4,531 offset 7951=0x00

--//执行如下脚本:
--//补充如果你仔细看就可以发现键值是按照行目录排序的.如果有记录插入,oracle是通过2分法定位插入行目录的位置,
--//你可以发现后面的偏移不是有序的,这也就是索引的块内无序,块间有序,但是如果索引分裂,oracle会重新排序.

assign dba 4,531 offset 8106=0x00
assign dba 4,531 offset 8093=0x00
assign dba 4,531 offset 8080=0x00
assign dba 4,531 offset 8067=0x00
assign dba 4,531 offset 8054=0x00
assign dba 4,531 offset 8041=0x00
assign dba 4,531 offset 8028=0x00
assign dba 4,531 offset 8015=0x00
assign dba 4,531 offset 8002=0x00
assign dba 4,531 offset 7989=0x00
assign dba 4,531 offset 7977=0x00
assign dba 4,531 offset 7964=0x00
assign dba 4,531 offset 7951=0x00

BBED> sum apply
Check value for File 4, Block 531:
current = 0x91b5, required = 0x91b5

BBED> verify
DBVERIFY - Verification starting
FILE = /mnt/ramdisk/book/users01.dbf
BLOCK = 531

Block Checking: DBA = 16777747, Block Type = KTB-managed data block
**** actual free space = 7593 < kdxcoavs = 7787
**** actual rows marked deleted = 0 != kdxlende = 13
---- end index block validation
Block 531 failed with check code 6401

BBED> p kdxle.kdxlende
sb2 kdxlende                                @118      13

BBED> assign  kdxle.kdxlende=0
sb2 kdxlende                                @118      0

BBED> sum apply
Check value for File 4, Block 531:
current = 0x91b8, required = 0x91b8

BBED> verify
DBVERIFY - Verification starting
FILE = /mnt/ramdisk/book/users01.dbf
BLOCK = 531

Block Checking: DBA = 16777747, Block Type = KTB-managed data block
**** actual free space = 7593 < kdxcoavs = 7787
---- end index block validation
Block 531 failed with check code 6401

BBED> p kdxle.kdxlexco.kdxcoavs
sb2 kdxcoavs                                @114      7787

BBED> assign kdxle.kdxlexco.kdxcoavs=7593
sb2 kdxcoavs                                @114      7593

BBED> sum apply
Check value for File 4, Block 531:
current = 0x927a, required = 0x927a

BBED> verify
DBVERIFY - Verification starting
FILE = /mnt/ramdisk/book/users01.dbf
BLOCK = 531

--//OK,现在完全修复了.

3.验证看看:
SCOTT@book> alter system flush buffer_cache;
System altered.

SCOTT@book> select * from empy where empno in(7499,7369);
     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 1980-12-17 00:00:00        800                    20
      7499 ALLEN      SALESMAN        7698 1981-02-20 00:00:00       1600        300         30

--//通过索引能定位行记录.
SCOTT@book> ANALYZE TABLE empy VALIDATE STRUCTURE CASCADE;
Table analyzed.

--//ANALYZE TABLE empy VALIDATE STRUCTURE CASCADE;一切ok,证明修复没有任何问题.

总结:
--//使用bbed修复索引有点繁琐,主要kd_off记录的偏移不对,从kd_off[2]算起.并且遗漏2条键值记录.

04-19 20:17