Escolar Documentos
Profissional Documentos
Cultura Documentos
Internals
Julian Dyke
Independent Consultant
Web Version
1
Agenda
Transactions
Redo
Undo
Rollback
Read Consistency
Undo-based Features
ORA_ROWSCN
Flashback
Examples
TEAM
VARCHAR2(30)
RUNS
NUMBER
WICKETS
NUMBER
Transactions
INSERT
UPDATE
DELETE
MERGE
SELECT FOR UPDATE
LOCK TABLE
Transactions
ACID properties
Atomicity - all changes made by the transaction are either
committed or rolled back
Consistency - the database is transformed from one valid
state to another
Isolation - results of the transaction are invisible to other
transactions until the transaction is complete
Durability - once the transaction completes, the results of
the transaction are permanent
Redo
Redo is used:
During recovery of database
Instance recovery
Media recovery
Undo
Undo
5.1
11.5
slot 22: 10
block 42 slot 1
col1: 74
col1: 75
block 42 slot 1
col2: 6
block 42 slot 1
col2: 7
col2: 75
col1:
col3: 7
col2:
block 42 slot 0
Undo Header
col0: ENG
col1: 841
col2: 3
5.1
block 42 slot 1
col0: AUS
5.1
col1: 77
74
75
col2: 8
6
7
Data Block
block 42 slot 1
11.5
slot 22: 10
9
block 42 slot 1
block 42 slot 1
2
5.1
COMMIT;
UPDATE score
SET runs
wickets
= 77,
75
= 7wickets = 8
WHERE team = 'AUS';
block 42 slot 1
col1: 74
block 42 slot 1
col2: 6
block 42 slot 1
2
5.1
col2: 75
col1:
col3: 7
col2:
col1: 77
col2: 8
5.4
slot 22: 9
Log Buffer
STOP
Undo Block
5.1
Rollback
5.2
5.1
11.5
5.1
11.5
11.5
5.6
11.5
slot 22: 10
block 42 slot 1
col2: 8
ROLLBACK;
UPDATE score
SET wickets = 9
10
WHERE team = 'AUS';
slot 22: 10
9
block 42 slot 1
col2: 9
block 42 slot 1
col2: 9
block 42 slot 1
col2: 10
block 42 slot 1
col2: 9
slot 22: 10
block 42 slot 1
block 42 slot 0
Undo Header
col0: ENG
col1: 841
col2: 3
5.1
block 42 slot 1
col0: AUS
5.1
block 42 slot 2
col3: 8
block 42 slot 2
col3: 9
col1: 77
col2: 8
10
9
Data Block
col2: 8
slot 22: 9
Log Buffer
STOP
Undo Block
Retention Table
Transaction Table
Block Tail
KTU SMU HEADER BLOCK
Transaction ID (XID)
Transaction ID (XID)
Undo
Segment
1
STOP
Slot Status
01 10
Undo02 10
03 9
Segment
9
2 04 10
05 9
06 9
07 9
08 9
Undo Segment 3
Wrap#
SCN
4D0
134
4CF Undo 137
4D0Segment
138
4C8
127
3 139
4D0
4CF
128
4CE
129
4CF
130
4CF
131
DBA
8004ea
8004e1
Undo
8004ef
Segment
800fb4 4
8004f0
800fd5
800fc8
800fd0
800fd1
Session
1
UPDATE bowling
SET overs = 4
WHERE bowler = 7
Session
2
UPDATE batting
SET runs = 25
WHERE batsman = 8
Session
3
UPDATE score
SET runs = 80
WHERE team = 'AUS';
XID 0003.004.000004d0
XID 0004.007.00000498
XID 0001.023.000004C8
Undo Extents
Undo Records
Block Tail
KTU UNDO BLOCK
Undo Block
Contains
DBA of undo block
Sequence number of undo block
Record number in undo block
Undo
Redo
STOP
DELETE
UPDATE score
SET
runs = 104,
wickets = 5
WHERE team = 'AUS';
OP 5.1 (11.1)
Delete Row Piece - DRP
OP 5.1 (11.1)
Update Row Piece - URP
OP 5.1 (11.1)
Insert Row Piece - IRP
Slot 4:
Slot 4:
Slot 4:
OP 11.2
Insert Row Piece - IRP
c1: 100
c2: 4
Slot 4:
OP 11.5
Update Row Piece - URP
c0: 'AUS'
c1: 104
c2: 5
c0: 'AUS'
c1: 100
c2: 4
UPDATE
Slot 4:
c1: 104
c2: 5
OP 11.3
Delete Row Piece - DRP
Slot 4:
Undo
Redo
OP 5.1 (10.22)
Purge Leaf Row
key: 'AUS'
OP 10.2
Insert Leaf Row
key: 'AUS'
data: 01 00 20 47 00 00
UPDATE
UPDATE score
SET team = 'ENG'
WHERE team = 'AUS';
OP 5.1 (10.22)
Restore leaf row
key: 'AUS'
data: 01 00 20 47 00 00
OP 5.1 (10.22)
Restore leaf row
key: 'ENG'
data: 01 00 20 47 00 00
OP 10.4
Delete leaf row
slot: 0
OP 10.4
Delete leaf row
slot: 0
OP 5.1 (10.22)
Purge leaf row
key: 'ENG'
OP 10.2
Insert Leaf Row
key: 'ENG'
data: 01 00 20 47 00 00
STOP
DELETE
Undo
OP 5.1 (11.1)
Lock Row - LKR
Slot 4:
Redo
OP 11.4
Lock Row - LKR
Slot 4:
STOP
UPDATE Statements
SELECT
FOR UPDATE
BEGIN
SELECT runs, wickets
INTO l_runs, l_wickets
FROM score
WHERE team = p_team
FOR UPDATE;
UPDATE test3
SET
runs = l_runs,
wickets = l_wickets
WHERE team = p_team;
END;
/
Undo
OP 5.1 (11.1)
Lock Row - LKR
Slot 4:
Redo
OP 11.4
Lock Row - LKR
Slot 4:
STOP
UPDATE
UPDATE score
SET
runs = :b3,
wickets = :b2
WHERE team = :b1;
OP 5.1 (11.1)
Update Row Piece - URP
Slot 4:
c1: 100
c2: 4
OP 11.5
Update Row Piece - URP
Slot 4:
c1: 104
c2: 4
UPDATE Statements
Advantages
Reduce parse overhead
Good on single instance, even better on RAC
Reduce space required in library cache
Less chance cursors will be aged out
Disadvantages
Increase physical I/O to online redo logs
Increase number of undo blocks in buffer cache
Increase probability of ORA-01555
Data
Block Tail
Read Consistency
Read Consistency
Sessionscore
2
Session
1
Australia
score
version
Australia
2 A current (dirty)
Session
2 wants
to check
segment 4 slot 22:
another
4
single
runs
of
block
42
is
already
in
runs.
The
score
the
current
Australia
Update
state:
10undo header
SELECT runs, wickets
UPDATE score
Update
undo
header
The
must
score
be updated
must be the buffer
cache,
score
to point4D9
at last
undo
wrap#:
SET
runs so
= 89
88;
84;
torecord
point at
last undo
Undo
is applied based on
updated
a team
copy = 'AUS';
(16)
dba: 008002DA
FROM
score again session1 makes
WHERE
Session
1
has
not
yet
record (18)
(17)
uba records in ITL slot
WHERE team = 'AUS'; into a free buffer
committed so Session 2
Undo Header
First apply undo record 18
must
perform
a (4)
Get next
undo
segment
consistent
seq:
052C
irb:record
15
16
17
18
ITL 1:
ITLReserve
1:
next slot read
(22 = 0x16)
Apply
undo
16
Get
undo block (0x008002DA) xid: 0004.016.000004D9
xid: 0004.016.000004D9
xid: next
0004.016.000004D9
Wrap number denotes current
uba: 008002DA.052C.18
-008002DA.052C.16
008002DA.052C.17
uba: 008002DA.052C.18
008002DA.052C.16
008002DA.052C.17
version
ofITL.
undo header
Update
Update
uba
uba in
in
ITL
16
uba: Get first
to
End
undo
of
record
uba
list
17
16
Apply undo record 17
available
ITL
in
block 42 slot 2
Set
ITL
blockRead
42 slot
1 lock
0 block
block 42 slot 1 lock
0undo block into5.1
data
data
block
Read
buffer
transaction
to
col2: 82
Set
ITL
ITL uba
ubaID
to
tocol1:
address
address
of
col1:42
ENG
ENGof
from disk Set
into
cache
current
XID
second
third
first undo
undo
undo
record
record
record
col2: 841
col2:
841 dba Set current XID
17 in header
uba: 008002DA.052C.16
0x008002DA
=
undo
block
0x0004 is undo segment
# undo record 18
buffer
Apply
col3: 3
3 seq#
block 42 slot 2
0x052C = undocol3:
block
0x016Undo
is slot
#
(22)
5.1
record 16
16 = undo record#
17
18
is wrap
block 42 slot 2 lock 1
block 42 slot 2 lock0x000004D9
0 First undo
1
in TX#socol2: 84
Sequence
(0x52C)
nonumber
previous
uba008002DA.052C.17
col1: AUS
col1: AUS
18
uba:
denotes current version
of
89
col2: 89
82Undo record 18 undo block
block 42 slot 2
col2: 82
88
84
84
88
Undo5.1
record 17
col2:
88
col3: 9
col3:
9Third
TX
Set row
to undo
Second
in TX
Apply
Apply
undo
undoundo
to
to in
Update
runslock
Previous
uba is 17 ITL# (1)
uba(dba
is 16008002DA)
runs
runs
column
column
Data Block (42 copy)
Data
Block (42) column Previous
Undo Block
STOP
SET TRANSACTION
SET TRANSACTION
For example:
Session 2
Session 1
Session 3
SELECT runs
FROM score
WHERE team = 'ENG';
Runs
127
SET TRANSACTION
READ WRITE;
SET TRANSACTION
READ ONLY;
SELECT runs
FROM score
WHERE team = 'ENG';
SELECT runs
FROM score
WHERE team = 'ENG';
Runs
131
Runs
127
UPDATE team
SET runs = 131
WHERE team = 'ENG';
COMMIT;
ORA_ROWSCN Pseudocolumn
ORA_ROWSCN Pseudocolumn
0x3588ba =
3508410
ITL1:
CREATE TABLE score
(team NUMBER, runs NUMBER, wickets NUMBER);
XID: 0008.012.000004FA
Flag: C---U--
Lck: 0
SCN/FSC: 0000.003588ba
COMMIT;
SCN/FSC: 0000.003588ba
ITL2:
XID: 0009.008.00000502
ORA_ROWSCN
3508410
3508410
Flag: --U-
Teams
ENG
AUS
Runs
0
0
Wickets
0
0
Lck: 1
SCN/FSC: 0000.003588bd
Row 0: lb: 2
1
UPDATE score
SET runs = 4
WHERE team = 'ENG';
col 0: ENG
COMMIT;
col 2: 0
col 1: 4
Teams
ENG
AUS
Runs
4
0
Wickets
0
0
Row 1: lb: 0
1
col 0: AUS
col 1: 0
col 2: 0
0x3588bd =
3508413
ORA_ROWSCN Pseudocolumn
0x358ced =
3509485
ITL1:
CREATE TABLE score
(team NUMBER, runs NUMBER, wickets NUMBER)
ROWDEPENDENCIES;
INSERT INTO score (team, runs, wickets) VALUES ('ENG',0,0);
INSERT INTO score (teams,runs,wickets) VALUES ('AUS',0,0);
2
Lck: 0
SCN/FSC: 0000.00358ced
ITL2:
SCN/FSC:
Flag: --U- 0000.003588ba
Lck: 1
COMMIT;
SCN/FSC: 0000.00358cf0
Row 0: lb: 2
1
ORA_ROWSCN
3509485
3509485
dscn: 0000.00358cf0
0000.00358ced
Teams
ENG
AUS
Runs
0
0
Wickets
0
0
col 2: 0
Row 1: lb: 0
1
COMMIT;
dscn: 0000.00358ced
col 0: ENG
col 1: 4
0
UPDATE score
SET runs = 4
WHERE team = 'ENG';
-U--Flag: C---
Teams
ENG
AUS
Runs
4
0
Wickets
0
0
col 0: AUS
col 1: 0
col 2: 0
0x358cf0 =
3509488
Flashback Query
Example
Session 2
Session 1
SELECT runs
FROM score
WHERE team = 'ENG';
Runs
137
UPDATE team
SET runs = 141
WHERE team = 'ENG';
COMMIT;
SELECT dbms_flashback.get_system_change_number FROM dual;
SCN
3494833
SELECT team, runs, wickets FROM score
WHERE team = 'ENG';
Team
ENG
Runs
141
Wickets
1
Runs
137
Wickets
1
Flashback Query
For example:
SELECT team, runs, wickets
FROM score AS OF SCN 3506431
WHERE team = 'ENG';
Flashback Query
For example:
SELECT team, runs, wickets
FROM score VERSIONS BETWEEN SCN 3503511 AND 3503524
WHERE team = 'ENG';
Example:
Session 1
SELECT runs
FROM score
WHERE team = 'ENG';
Session 2
SELECT dbms_flashback.get_system_change_number FROM dual;
SCN
3503136
Runs
141
UPDATE team
SET runs = 145
WHERE team = 'ENG';
COMMIT;
UPDATE team
SET runs = 151
WHERE team = 'ENG';
COMMIT;
UPDATE team
SET runs = 153
WHERE team = 'ENG';
COMMIT;
SELECT dbms_flashback.get_system_change_number FROM dual;
SCN
3503143
Example (continued):
Session 2
Session 1
SELECT
VERSIONS_STARTSCN "Start",
VERSIONS_ENDSCN "End",
VERSIONS_XID "XID",
VERSIONS_OPERATION "Op",
score.team "Team",
score.runs "Runs",
score.wickets "Wickets"
FROM score VERSIONS BETWEEN SCN 3503136 AND 3503143
WHERE team = 'ENG';
Start
3503142
3503139
3503136
End
3503142
3503139
3503136
XID
08000A00FC040000
07001A00F6040000
06002C00EA040000
XID = 0066.02C.000004EA
(Architecture = X86)
Op
U
U
U
Team
ENG
ENG
ENG
ENG
Runs
153
151
145
141
Wickets
1
1
1
1