【大膽的實驗】Oracle的不一致性,未commit資料變成實際資料並被其他回話查詢

PiscesCanon發表於2017-07-19
作業系統版本:
  1. [oracle@oracle zkm]$ uname -a
  2. Linux oracle.example.com 2.6.32-431.el6.x86_64 #1 SMP Sun Nov 10 22:19:54 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
  3. [oracle@oracle zkm]$ lsb_release -a
  4. LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
  5. Distributor ID: RedHatEnterpriseServer
  6. Description:    Red Hat Enterprise Linux Server release 6.5 (Santiago)
  7. Release:        6.5
  8. Codename:       Santiago

資料庫版本:
  1. SYS@proc> select * from v$version where rownum=1;

  2. BANNER
  3. --------------------------------------------------------------------------------
  4. Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

環境構造:
  1. SYS@proc> drop table tt purge;

  2. Table dropped.

  3. SYS@proc> create table tt (id int,name varchar2(2000)) tablespace users;

  4. Table created.

  5. SYS@proc> begin
  6.               for i in 1..10000 loop
  7.                   insert into tt values(i,rpad('a',2000,'+'));
  8.               end loop;
  9.               commit;
  10.           end;
  11.           /

  12. PL/SQL procedure successfully completed.

實驗過程:
  1. SYS@proc> begin
  2.               for i in 1..10000 loop
  3.                   update tt set name=rpad('b',2000,'-') where id=i;
  4.               end loop;
  5.           end;
  6.           /

  7. PL/SQL procedure successfully completed.

  8. SYS@procshutd abort;
  9. ORACLE instance shut down.
  10. SYS@proc> exit
  11. Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
  12. With the Partitioning, OLAP, Data Mining and Real Application Testing options
  13. [oracle@oracle ~]$ ll un*
  14. -rw-r-----. 1 oracle oinstall 104865792 Jul 19 11:19 undotbs1.dbf
  15. [oracle@oracle ~]$ rm -fr undotbs1.dbf
  16. [oracle@oracle ~]$ sqlplus / as sysdba

  17. SQL*Plus: Release 11.2.0.4.0 Production on Wed Jul 19 11:20:17 2017

  18. Copyright (c) 1982, 2013, Oracle. All rights reserved.

  19. Connected to an idle instance.

  20. SYS@proc> startup;
  21. ORACLE instance started.

  22. Total System Global Area 521936896 bytes
  23. Fixed Size                 2254824 bytes
  24. Variable Size            306186264 bytes
  25. Database Buffers         209715200 bytes
  26. Redo Buffers               3780608 bytes
  27. Database mounted.
  28. ORA-01157: cannot identify/lock data file 3 - see DBWR trace file
  29. ORA-01110: data file 3: '/home/oracle/undotbs1.dbf'


  30. SYS@proc> alter database datafile 3 offline drop;

  31. Database altered.

  32. SYS@proc> alter database open;

  33. Database altered.

  34. SYS@proc> create undo tablespace undotbs2 datafile '/home/oracle/undotbs2.dbf' size 100m autoextend on;

  35. Tablespace created.

  36. SYS@proc> alter system set undo_tablespace=undotbs2;

  37. System altered.

  38. SYS@proc> show parameter undo_tablespace

  39. NAME                 TYPE     VALUE
  40. ------------------------------------ ----------- ------------------------------
  41. undo_tablespace          string     UNDOTBS2
  42. SYS@proc> select count(*) from tt;
  43. select count(*) from tt
  44.                      *
  45. ERROR at line 1:
  46. ORA-00376: file 3 cannot be read at this time
  47. ORA-01110: data file 3: '/home/oracle/undotbs1.dbf'


  48. SYS@proc> drop tablespace undotbs1 including contents and datafiles;
  49. drop tablespace undotbs1 including contents and datafiles
  50. *
  51. ERROR at line 1:
  52. ORA-01548: active rollback segment '_SYSSMU7_1076253125$' found, terminate dropping tablespace


  53. SYS@proc> --查詢回滾段資訊
  54.     select rownum,
  55.         sys.dba_rollback_segs.segment_name Name,
  56.         v$rollstat.extents Extents,
  57.         v$rollstat.rssize Size_in_Bytes,
  58.         v$rollstat.xacts XActs,
  59.         v$rollstat.gets Gets,
  60.         v$rollstat.waits Waits,
  61.         v$rollstat.writes Writes,
  62.         sys.dba_rollback_segs.status status
  63.    from v$rollstat, sys.dba_rollback_segs, v$rollname
  64.   where v$rollname.name(+) = sys.dba_rollback_segs.segment_name
  65.     and v$rollstat.usn(+) = v$rollname.usn
  66.   order by rownum;

  67.     ROWNUM NAME                              EXTENTS SIZE_IN_BYTES      XACTS       GETS      WAITS     WRITES STATUS
  68. ---------- ------------------------------ ---------- ------------- ---------- ---------- ---------- ---------- ----------------
  69.          1 SYSTEM                                  6        385024          0        293          0      39796 ONLINE
  70.          2 _SYSSMU17_2390902301$                   2        122880          0          2          0          0 ONLINE
  71.          3 _SYSSMU18_1669837494$                   2        122880          0          2          0          0 ONLINE
  72.          4 _SYSSMU39_929646464$                    2        122880          0          4          0        138 ONLINE
  73.          5 _SYSSMU40_3450336478$                   2        122880          0          4          0        430 ONLINE
  74.          6 _SYSSMU41_3787558531$                   2        122880          0          4          0        232 ONLINE
  75.          7 _SYSSMU42_2640506269$                   2        122880          0          4          0        214 ONLINE
  76.          8 _SYSSMU43_391677854$                    2        122880          0          4          0        158 ONLINE
  77.          9 _SYSSMU44_2493446996$                   2        122880          0          7          0        900 ONLINE
  78.         10 _SYSSMU45_2356950286$                   2        122880          0          4          0       3482 ONLINE
  79.         11 _SYSSMU46_275930757$                    2        122880          0          4          0        114 ONLINE

  80.     ROWNUM NAME                           EXTENTS    SIZE_IN_BYTES      XACTS       GETS      WAITS     WRITES STATUS
  81. ---------- ------------------------------ ---------- ------------- ---------- ---------- ---------- ---------- ----------------
  82.         12 _SYSSMU12_1668912051$                                                                               NEEDS RECOVERY
  83.         13 _SYSSMU7_1076253125$                                                                                NEEDS RECOVERY
  84.         14 _SYSSMU16_2690255195$                                                                               NEEDS RECOVERY
  85.         15 _SYSSMU11_3130810267$                                                                               NEEDS RECOVERY
  86.         16 _SYSSMU13_492329380$                                                                                NEEDS RECOVERY
  87.         17 _SYSSMU9_2756025465$                                                                                NEEDS RECOVERY
  88.         18 _SYSSMU15_3752757294$                                                                               NEEDS RECOVERY
  89.         19 _SYSSMU10_146814648$                                                                                NEEDS RECOVERY
  90.         20 _SYSSMU8_4280284895$                                                                                NEEDS RECOVERY
  91.         21 _SYSSMU14_3551377741$                                                                               NEEDS RECOVERY

  92. 21 rows selected.

  93. SYS@proc> shutdown immediate;
  94. Database closed.
  95. Database dismounted.
  96. ORACLE instance shut down.
  97. SYS@proc> create pfile from spfile;

  98. File created.


  99. [oracle@oracle dbs]$ cat initproc.ora | grep offline_rollback_segments
  100. *._offline_rollback_segments='_SYSSMU12_1668912051$','_SYSSMU7_1076253125$','_SYSSMU16_2690255195$','_SYSSMU11_3130810267$','_SYSSMU13_492329380$','_SYSSMU9_2756025465$','_SYSSMU15_3752757294$','_SYSSMU10_146814648$','_SYSSMU8_4280284895$','_SYSSMU14_3551377741$'

  101. SYS@proc> create spfile from pfile;

  102. File created.

  103. SYS@proc> startup;
  104. ORACLE instance started.

  105. Total System Global Area 521936896 bytes
  106. Fixed Size                 2254824 bytes
  107. Variable Size            306186264 bytes
  108. Database Buffers         209715200 bytes
  109. Redo Buffers               3780608 bytes
  110. Database mounted.
  111. Database opened.
  112. SYS@proc> show parameter offline_roll

  113. NAME                                 TYPE        VALUE
  114. ------------------------------------ ----------- ------------------------------
  115. _offline_rollback_segments           string     _SYSSMU12_1668912051$, _SYSSMU
  116.                                                 7_1076253125$, _SYSSMU16_26902
  117.                                                 55195$, _SYSSMU11_3130810267$,
  118.                                                  _SYSSMU13_492329380$, _SYSSMU
  119.                                                 9_2756025465$, _SYSSMU15_37527
  120.                                                 57294$, _SYSSMU10_146814648$,
  121.                                                 _SYSSMU8_4280284895$, _SYSSMU1
  122.                                                 4_3551377741$
  123. SYS@proc> select count(*) from tt;
  124. select count(*) from tt
  125.                      *
  126. ERROR at line 1:
  127. ORA-00376: file 3 cannot be read at this time
  128. ORA-01110: data file 3: '/home/oracle/undotbs1.dbf'

  129. SYS@proc> drop tablespace undotbs1 including contents and datafiles;

  130. Tablespace dropped.

  131. SYS@proc> select count(*) from tt;

  132.   COUNT(*)
  133. ----------
  134.      10000

  135. SYS@proc>
  1. SYS@proc> alter system reset "_offline_rollback_segments";

  2. System altered.

  3. SYS@proc> startup force;
  4. ORACLE instance started.

  5. Total System Global Area 521936896 bytes
  6. Fixed Size                 2254824 bytes
  7. Variable Size            306186264 bytes
  8. Database Buffers         209715200 bytes
  9. Redo Buffers               3780608 bytes
  10. Database mounted.
  11. Database opened.
  12. SYS@proc> show parameter _offline_rollback_segments
  13. SYS@proc>

查詢結果:
select * from tt;(在PLSQL Developer 12 (64 bit)工具中執行)


總結:
需要注意的是,不是每次結果都能看到有a和b結果的混合,很大機率只能看到都是b---..的情況,不過b--..都是未commit的資料,已經達到目的。
至於為什麼會這樣,這涉及到oracle例項恢復原理,讀一致性原理,ITL等等知識,這裡就不詳述了。




來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30174570/viewspace-2142281/,如需轉載,請註明出處,否則將追究法律責任。

相關文章