of 41
8/18/2019 Transaction Internals
1/41
1
TransactionInternals
Julian Dyke
Independent Consultant
Web Version
© 2007 Julian Dyke
8/18/2019 Transaction Internals
2/41
2
© 2007 Julian Dyke
Agenda
Transactions
edo
!ndo
ollback
ead Consistency
!ndo"based #eatures
$A%$W&C'
#las(back
8/18/2019 Transaction Internals
3/41
3
© 2007 Julian Dyke
)*a+ples All e*a+ples in t(is presentation are based on cricket
T)A, VAC-A2./0!'& '!,)
WIC)T& '!,)
&C$)
T(e table (as no inde*es
T(e 3ollo4ing table (as been used in all e*a+ples in t(ispresentation
8/18/2019 Transaction Internals
4/41
4
© 2007 Julian Dyke
Transactions A transaction is a set o3 D,5 state+ents e*ecuted
se6uentially by a session
&tarts 4it( t(e 3irst o3 t(e 3ollo4ing state+ents e*ecuted byt(e session
I'&)T !8DAT)
D)5)T)
,)9)
&)5)CT #$ !8DAT) 5$C TA5)
)nds 4it( eit(er a C$,,IT or $55AC
8/18/2019 Transaction Internals
5/41
5
© 2007 Julian Dyke
Transactions ACID properties
Ato+icity " all c(anges +ade by t(e transaction are eit(erco++itted or rolled back
Consistency " t(e database is trans3or+ed 3ro+ one :alidstate to anot(er
Isolation " results o3 t(e transaction are in:isible to ot(er
transactions until t(e transaction is co+plete Durability " once t(e transaction co+pletes; t(e results o3
t(e transaction are per+anent
In $racle transactions can also be
recursi:e
audit
autono+ous
8/18/2019 Transaction Internals
6/41
6
© 2007 Julian Dyke
edo All database c(anges generate redo
ecords c(anges +ade toData and inde* seg+ents
!ndo seg+ents
Data dictionary
Control 3iles .indirectly
edo is used
During reco:ery o3 database
Instance reco:ery
,edia reco:ery
8/18/2019 Transaction Internals
7/417
© 2007 Julian Dyke
!ndo )nsures ACID properties are +aintained 3or eac( transaction
Contains c(anges re6uired to re:erse redo including
c(anges to data and inde* blocks
c(anges to transaction lists
c(anges to undo blocks
All undo operations generate redo
'ot all redo operations generate undo
I+ple+ented using undo seg+ents ,anually"+anaged .rollback seg+ents
&yste+"+anaged .$racle
8/18/2019 Transaction Internals
8/418 © 2007 Julian Dyke
!ndo !sed to rollback unco++itted transactions
y session issuing $55AC state+ent y 8,$' on be(al3 o3 3ailed session
During instance reco:ery
During +edia reco:ery
!sed to i+ple+ent read"consistency
!nco++itted c(anges cannot be seen by ot(er sessions
!sed to i+ple+ent 3las(back
$racle
8/18/2019 Transaction Internals
9/41
8/18/2019 Transaction Internals
10/41
8/18/2019 Transaction Internals
11/411 © 2007 Julian Dyke
!ndo &eg+ent -eader !ndo seg+ents are allocated at instance startup
!ndo seg+ents can be added dyna+ically
)ac( undo seg+ent (eader contains
8ool o3 3ree undo e*tents
&et o3 undo slots
$ne undo slot is allocated to eac( transaction
!ndo slot contains list o3 undo e*tents
)*tents can +igrate 3ro+ one undo seg+ent to anot(er
!ndo slots are used cyclically re+ain in (eader as long as possible
reduces probability o3 $A"01??? &naps(ot too old
8/18/2019 Transaction Internals
12/411 © 2007 Julian Dyke
!ndo &eg+ent -eader &tructure
lock -eader
)*tent Control -eader
)*tent ,ap
etention Table
lock Tail
#ree lock 8ool
Transaction Table
T! &,! -)AD) 5$C
8/18/2019 Transaction Internals
13/411 © 2007 Julian Dyke
Transaction ID .EID ):ery transaction (as a uni6ue ID based on
!ndo seg+ent nu+ber !ndo seg+ent slot nu+ber
!ndo seg+ent se6uence nu+ber .4rap
A transaction ID .EID is allocated to eac( transaction during
t(e 3irst D,5 state+ent= #or e*a+ple 0002=02B=00000DA
Details about transaction can be 3ound in VFTA'&ACTI$'
EID!&' &eg+ent nu+ber EID&5$T &lot nu+ber
EID&G' &e6uence nu+ber
8/18/2019 Transaction Internals
14/411 © 2007 Julian Dyke
Transaction ID .EID
!ndo&eg+ent
1
!ndo&eg+ent
2
!ndo&eg+ent
/
!ndo&eg+ent
&ession1
!8DAT) bo4ling&)T o:ers >
W-)) bo4ler > 7
!8DAT) batting&)T runs > 2?
W-)) bats+an > B
!8DAT) score&)T runs > B0
W-)) tea+ > @A!&@
!ndo &eg+ent /
&lot &tatus WrapH &C' DA
01 10 D0 1/ B00ea
02 10 C# 1/7 B00e10/ < D0 1/B B00e3
0 < CB 127 B003b
0? < C# 12B B003d?
0 < C) 12< B003cB
07 < C# 1/0 B003d0
0B < C# 1/1 B003d1
0 10 D0 1/< B0030
EID 000/=00=00000d0 EID 000=007=00000
8/18/2019 Transaction Internals
15/411 © 2007 Julian Dyke
!ndo )*tents )ac( undo e*tent contains contiguous set o3 undo blocks
)ac( undo block can only be allocated to one transaction
!ndo blocks contain
!ndo block (eader
!ndo records
8/18/2019 Transaction Internals
16/411 © 2007 Julian Dyke
!ndo lock &tructure
lock -eader
!ndo lock -eader
!ndo ecord Inde*
!nused &pace
!ndo ecords
lock Tail
T! !'D$ 5$C
8/18/2019 Transaction Internals
17/411 © 2007 Julian Dyke
!ndo lock !ndo lock -eader contains
Transaction ID .EID 3or current last transaction to useblock
&e6uence nu+ber o3 undo block
'u+ber o3 undo records in undo block
'ot necessarily in current transaction
!ndo records are c(ained toget(er
Allo4 transaction to be rolled back
!ndo records are also used cyclically re+ain in block 3or as long as possible
reduces probability o3 $A"01??? &naps(ot too old
8/18/2019 Transaction Internals
18/41
8/18/2019 Transaction Internals
19/411 © 2007 Julian Dyke
!ndo C(ange Vectors " Data locks #or data blocks
$8 ?=1 .11=1Delete o4 8iece " D8
&lot
$8 11=2Insert o4 8iece " I8
&lot
c0 @A!&@c1 100c2
$8 ?=1 .11=1!pdate o4 8iece " !8
&lot
c1 100c2
$8 11=?
!pdate o4 8iece " !8
&lot
c1 10c2 ?
$8 ?=1 .11=1Insert o4 8iece " I8
&lot
c0 @A!&@c1 10c2 ?
$8 11=/Delete o4 8iece " D8
&lot
I'&)T I'T$ score.tea+; runs; 4icketsVA5!)&.@A!&@;100;
I'&)T!8DAT) score&)T
runs > 10;4ickets > ?
W-)) tea+ > @A!&@
!8DAT)D)5)T) #$, scoreW-)) tea+ > @A!&@
D)5)T)
!ndo
edo
&T$8
8/18/2019 Transaction Internals
20/41
2 © 2007 Julian Dyke
!ndo C(ange Vectors " Inde* locks Assu+e uni6ue inde* on &C$) .T)A,
$8 ?=1 .10=22
8urge 5ea3 o4key @A!&@
$8 10=2Insert 5ea3 o4key @A!&@data 01 00 20 7 00 00
$8 ?=1 .10=22
estore lea3 ro4key @A!&@data 01 00 20 7 00 00
$8 10=Delete lea3 ro4slot 0
I'&)T I'T$ score.tea+; runs; 4icketsVA5!)&.@A!&@;100;
I'&)T!8DAT) score&)T tea+ > @)'9@W-)) tea+ > @A!&@
!8DAT)D)5)T) #$, scoreW-)) tea+ > @)'9@
D)5)T)
!ndo
edo
$8 ?=1 .10=228urge lea3 ro4key @)'9@
$8 10=2Insert 5ea3 o4key @)'9@data 01 00 20 7 00 00
$8 ?=1 .10=22
estore lea3 ro4key @)'9@data 01 00 20 7 00 00
$8 10=Delete lea3 ro4slot 0
&T$8
8/18/2019 Transaction Internals
21/41
2 © 2007 Julian Dyke
&)5)CT #$ !8DAT) edo and !ndo 9eneration
$8 ?=1 .11=15ock o4 " 5
&lot
$8 11=5ock o4 " 5
&lot
&)5)CT runs; 4ickets#$, scoreW-)) tea+ > @A!&@#$ !8DAT)
!ndo
edo
&T$8
8/18/2019 Transaction Internals
22/41
2 © 2007 Julian Dyke
&)5)CT #$ !8DAT) &)5)CT #$ !8DAT) is bad 3or so +any reasons=====
o4s are locked pessi+istically,ore c(ance o3 contention
o4s could be locked opti+istically by any subse6uent
!8DAT) state+entApplication logic +ay need +odi3ication
&)5)CT #$ !8DAT) generates!ndo " +ore space in bu33er cac(e; $A01??? etc
edo " increased p(ysical I$
&)5)CT #$ !8DAT) state+ents cannot be batc(ed)ac( re6uires a separate pair o3 c(ange :ectors
8/18/2019 Transaction Internals
23/41
2 © 2007 Julian Dyke
!8DAT) &tate+ents edo and !ndo 9eneration
$8 ?=1 .11=15ock o4 " 5
&lot
$8 11=5ock o4 " 5
&lot
&)5)CT runs; 4ickets#$, scoreW-)) tea+ > b1#$ !8DAT)
!ndo
edo
C)AT) $ )85AC) 8$C)D!) update%runs.p%tea+ VAC-A2;p%runs '!,)
I&l%runs '!,)l%4ickets '!,)
)9I'
&)5)CT runs; 4icketsI'T$ l%runs; l%4ickets#$, scoreW-)) tea+ > p%tea+#$ !8DAT)
!8DAT) test/&)T
runs > l%runs;4ickets > l%4icketsW-)) tea+ > p%tea+
)'D
$8 ?=1 .11=1!pdate o4 8iece " !8
&lot
c1 100c2
$8 11=?!pdate o4 8iece " !8
&lot
c1 10c2
!8DAT) score&)T
runs > b/;4ickets > b2
W-)) tea+ > b1
!8DAT)
&)5)CT#$ !8DAT)
&T$8
8/18/2019 Transaction Internals
24/41
2 © 2007 Julian Dyke
!8DAT) &tate+ents !8DAT) state+ents t(at include unc(anged colu+ns
Ad:antages
educe parse o:er(ead
9ood on single instance; e:en better on AC
educe space re6uired in library cac(e
5ess c(ance cursors 4ill be aged out
Disad:antages
Increase p(ysical I$ to online redo logs
Increase nu+ber o3 undo blocks in bu33er cac(e Increase probability o3 $A"01???
8/18/2019 Transaction Internals
25/41
2 © 2007 Julian Dyke
Data lock &tructure
lock -eader
Data -eader
Interested Transaction 5ist
Table Inde*
o4 Inde*
!nused &pace
Data
lock Tail
8/18/2019 Transaction Internals
26/41
2 © 2007 Julian Dyke
Interested Transaction 5ist )ac( datainde* block (as an Interested Transaction 5ist
list o3 transactions currently acti:e on block stored 4it(in block (eader
)ac( datainde* ro4 (eader contains a lock byte 5ock byte records current slot in IT5
)ac( ro4 can only be associated 4it( one transaction I3 a second transaction atte+pts to update a ro4 it 4ill
e*perience a ro4 lock 4aits until 3irst transaction co++itsrolls back
Initially t4o IT5 entries are reser:ed in block (eader IT5 list can gro4 dyna+ically according to de+and IT5 list cannot s(rink again
8/18/2019 Transaction Internals
27/41
2 © 2007 Julian Dyke
Interested Transaction 5ist IT5 entry includes
Transaction ID .EID !ndo byte address .!A
&yste+ C(ange 'u+ber .&C'
IT5 entry is o:er4ritten by eac( c(ange to t(e block by t(e
current transaction
8re:ious c(ange is recorded in undo block
During rollback; c(anges are restored to IT5 3ro+ undo block
8/18/2019 Transaction Internals
28/41
2 © 2007 Julian Dyke
ead Consistency e6uired to +aintain ACID properties o3 transaction
Transactions +ust al4ays see consistent :ersions o3blocks +odi3ied by ot(er transactions
Can be applied at
&tate+ent le:el .de3ault
Transaction le:el
!nco++itted block updates are rolled back 4(en block isread
Consistent reads
,ore speci3ically undo is applied to return block toconsistent state
!ndo +ust still be a:ailable in undo seg+ent
I3 undo (as been o:er4ritten; 6uerying session 4illrecei:e $A"01??? &naps(ot too old
8/18/2019 Transaction Internals
29/41
8/18/2019 Transaction Internals
30/41
3 © 2007 Julian Dyke
&)T TA'&ACTI$' Deter+ines le:el at 4(ic( read"consistency is applied
Can be &)T TA'&ACTI$' )AD WIT)
establis(es state+ent"le:el read consistency
subse6uent state+ents see any c(anges co++ittedbe3ore t(at state+ent started
de3ault be(a:iour &)T TA'&ACTI$' )AD $'5K
establis(es transaction"le:el read consistency
all subse6uent state+ents only see c(anges co++itted
be3ore transaction startednot supported 3or &K& user
&)T TA'&ACTI$' state+ent +ust be 3irst state+ent intransaction
8/18/2019 Transaction Internals
31/41
3 © 2007 Julian Dyke
&)T TA'&ACTI$' #or e*a+ple
&ession 1 &ession 2 &ession /
&)5)CT runs#$, scoreW-)) tea+ > @)'9@
uns127
!8DAT) tea+&)T runs > 1/1W-)) tea+ > @)'9@
C$,,IT
&)T TA'&ACTI$')AD WIT) &)T TA'&ACTI$')AD $'5K
&)5)CT runs#$, scoreW-)) tea+ > @)'9@
uns1/1
&)5)CT runs#$, scoreW-)) tea+ > @)'9@
uns127
8/18/2019 Transaction Internals
32/41
3 © 2007 Julian Dyke
$A%$W&C' 8seudocolu+n eturns conser:ati:e upper"bound &C' 3or +ost recent
c(ange in ro4
!ses &C' stored 3or transaction in IT5
&(o4s last ti+e a ro4 in sa+e block 4as updated
,ay s(o4 +ore accurate in3or+ation 3or an indi:idual ro4
'ot supported 4it( 3las(back 6uery
To con:ert $A%$W&C' to an appro*i+ate ti+esta+p uset(e &C'%T$%TI,)&TA,8 built"in 3unction e=g=
&)5)CT $A%$W&C';&C'%T$%TI,)&TA,8 .$A%$W&C'#$, score
8/18/2019 Transaction Internals
33/41
3 © 2007 Julian Dyke
$A%$W&C' 8seudocolu+n #or e*a+ple " no ro4 dependencies .de3ault
&C'#&C 0000=00/?BBbaIT52
EID 000 W-)) tea+ > @)'9@
C$,,IT
&)5)CT $A%$W&C'; tea+s; runs; 4ickets #$, score
$A%$W&C' Tea+s uns Wickets/?0B1/ )'9 0/?0B1/ A!& 0 0
o4 0 lb 2
o4 1 lb 0
0*/?BBbd >/?0B1/
#lag C""" 5ck 0
0*/?BBba >/?0B10
8/18/2019 Transaction Internals
34/41
3 © 2007 Julian Dyke
$A%$W&C' 8seudocolu+n #or e*a+ple .ro4 dependencies
IT51
#lag "!""" 5ck 2
IT52
&C'#&C 0000=00/?BBba#lag ""!" 5ck 1
&C'#&C 0000=00/?Bc30
dscn 0000=00/?Bced
o4 1 lb 1
col 0 )'9
col 1 0
col 2 0
col 0 A!&
col 1 0
col 2 0
&C'#&C 0000=00/?Bced
o4 0 lb 1
dscn 0000=00/?Bced
C)AT) TA5) score.tea+ '!,); runs '!,); 4ickets '!,)$WD)8)'D)'CI)&
&)5)CT ora%ro4scn; tea+s; runs; 4ickets #$, score
$A%$W&C' Tea+s uns Wickets/?0
8/18/2019 Transaction Internals
35/41
3 © 2007 Julian Dyke
#las(back Guery )*a+ple
&ession 1 &ession 2
!8DAT) tea+&)T runs > 11W-)) tea+ > @)'9@
C$,,IT
&)5)CT runs#$, scoreW-)) tea+ > @)'9@
uns1/7
&)5)CT db+s%3las(back=get%syste+%c(ange%nu+ber #$, dual
&C'/
8/18/2019 Transaction Internals
36/41
3 © 2007 Julian Dyke
#las(back Guery Can speci3y A& $# clause
eturns single"ro4
&ynta* is
&)5)CT tea+; runs; 4ickets#$, score A& $# &C' /?0/1W-)) tea+ > @)'9@
#or e*a+ple
A& $# L &C' MscnN O TI,)&TA,8 Mti+esta+pN P
8/18/2019 Transaction Internals
37/41
3 © 2007 Julian Dyke
#las(back Guery Can also speci3y V)&I$'& clause
eturns +ultiple ro4s
&ynta* is
&)5)CT tea+; runs; 4ickets#$, score V)&I$'& )TW))' &C' /?0/?11 A'D /?0/?2
W-)) tea+ > @)'9@
V)&I$'& )TW))' &C' L MscnN O ,I'VA5!) PA'D L MscnN O ,AEVA5!)
V)&I$'& )TW))' TI,)&TA,8 L Mti+esta+pN O ,I'VA5!) PA'D L Mti+esta+pN O ,AEVA5!)
#or e*a+ple
8/18/2019 Transaction Internals
38/41
3 © 2007 Julian Dyke
Version Guery 8seudocolu+ns Valid only 3or #las(back Version Guery= Values can be
V)&I$'&%&TATTI,) ti+esta+p o3 3irst :ersion o3 ro4s returned by 6uery
V)&I$'&%)'DTI,) ti+esta+p o3 last :ersion o3 ro4s returned by 6uery
V)&I$'&%&TAT&C'
&C' o3 3irst :ersion o3 ro4s returned by 6uery V)&I$'&%)'D&C'
&C' o3 last :ersion o3 ro4s returned by 6uery V)&I$'&%EID
#or eac( ro4 returns transaction ID o3 transaction
creating t(at ro4 :ersion V)&I$'&%$8)ATI$'
#or eac( ro4 returns operation creating t(at ro4:ersion= Can be I.nsert !.pdate or D.elete
8/18/2019 Transaction Internals
39/41
3 © 2007 Julian Dyke
Version Guery 8seudocolu+ns )*a+ple
&ession 1 &ession 2
!8DAT) tea+&)T runs > 1?W-)) tea+ > @)'9@
C$,,IT
&)5)CT runs#$, scoreW-)) tea+ > @)'9@
uns11
!8DAT) tea+&)T runs > 1?1W-)) tea+ > @)'9@
C$,,IT
!8DAT) tea+&)T runs > 1?/W-)) tea+ > @)'9@
C$,,IT
&)5)CT db+s%3las(back=get%syste+%c(ange%nu+ber #$, dual
&C'/?0/1/
&)5)CT db+s%3las(back=get%syste+%c(ange%nu+ber #$, dual
&C'/?0/1/
8/18/2019 Transaction Internals
40/41
4 © 2007 Julian Dyke
Version Guery 8seudocolu+ns )*a+ple .continued
&ession 1 &ession 2
&)5)CTV)&I$'&%&TAT&C' Q&tartQ;V)&I$'&%)'D&C' Q)ndQ;V)&I$'&%EID QEIDQ;V)&I$'&%$8)ATI$' Q$pQ;score=tea+ QTea+Q;score=runs QunsQ;
score=4ickets QWicketsQ#$, score V)&I$'& )TW))' &C' /?0/1/ A'D /?0/1/W-)) tea+ > @)'9@
&tart )nd EID $p Tea+ uns Wickets/?0/12 0B000A00#C00000 ! )'9 1?/ 1/?0/1/< /?0/12 07001A00#00000 ! )'9 1?1 1
/?0/1/ /?0/1/< 0002C00)A00000 ! )'9 1? 1/?0/1/ )'9 11 1
EID > 00=02C=00000)A.Arc(itecture > EB
Can be I.nsert; !.pdateor D.elete
8/18/2019 Transaction Internals
41/41