72
1 © IBSurgeon Firebird Conference 2014, Prague 1 How Firebird transactions work Dmitry Kuzmenko www.IBSurgeon.com

How Firebird transactions work

Embed Size (px)

Citation preview

Page 1: How Firebird transactions work

1

© IBSurgeonFirebird Conference 2014, Prague1

How Firebird transactions work

Dmitry Kuzmenkowww.IBSurgeon.com

Page 2: How Firebird transactions work

2

© IBSurgeonFirebird Conference 2014, Prague2

Thank you!

Page 3: How Firebird transactions work

3

© IBSurgeonFirebird Conference 2014, Prague3

• Tools and consulting• Platinum Sponsor of Firebird

Foundation• Founded in 2002: 12 years of

Firebird and InterBase recoveries and consulting

• Based in Moscow, Russia

Page 4: How Firebird transactions work

4

© IBSurgeonFirebird Conference 2014, Prague4

AgendaWhat is transaction? Why we need it?

How we will present about transactions

Records and versions

Transactions and record versions

Transaction Inventory

Record visibility in transactions

Transaction Markers and their evaluation

Some conclusions

Page 5: How Firebird transactions work

5

© IBSurgeonFirebird Conference 2014, Prague5

• Transaction as a general concept of any dynamic system• “Classic” example

• begin• -- move money from account1 to account2• Decrease account1• Increase account2

• end – commit/rollback

• Transaction Managers

What is transaction?

Page 6: How Firebird transactions work

6

© IBSurgeonFirebird Conference 2014, Prague6

Database transaction definition

• a unit of work performed against a database, and treated in a coherent and reliable way independent of other transactions.

• A database transaction, by definition, must be Atomic, Consistent, Isolated and Durable

Page 7: How Firebird transactions work

7

© IBSurgeonFirebird Conference 2014, Prague7

In ideal world

only serial operations

Insert into T1(i1) values (100);

SELECT i1 FROM T1

Insert into T1(i1) values (200);

Page 8: How Firebird transactions work

8

© IBSurgeonFirebird Conference 2014, Prague8

Page 9: How Firebird transactions work

9

© IBSurgeonFirebird Conference 2014, Prague9

Page 10: How Firebird transactions work

10

© IBSurgeonFirebird Conference 2014, Prague10

In real world

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

Tx11

INSERT T1

Tx14commit

UPDATE T1

nowait

commit

Tx20

UPDATE T1

rollback

UPDATE T1

Page 11: How Firebird transactions work

11

© IBSurgeonFirebird Conference 2014, Prague11

The ultimate purpose of transaction:

• Concurrent execution of operations should lead to the exactly the same result as sequental execution of operations.

For each [snapshot] transaction Firebird engine should maintain a stable view of the database.

In simple words: each transaction should run as the only transaction.

Page 12: How Firebird transactions work

12

© IBSurgeonFirebird Conference 2014, Prague12

Page 13: How Firebird transactions work

13

© IBSurgeonFirebird Conference 2014, Prague13

How Firebird does implement stable view for each transactions?

Page 14: How Firebird transactions work

14

© IBSurgeonFirebird Conference 2014, Prague14

How we will present about transactions

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

Tx 11

Transaction’s number

Start End

Page 15: How Firebird transactions work

15

© IBSurgeonFirebird Conference 2014, Prague15

How we will present about transactions

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

Tx 11commit

Tx 12rollback

Transaction’s result

Transaction’s result

Page 16: How Firebird transactions work

16

© IBSurgeonFirebird Conference 2014, Prague16

How we will present about transactions

Tx 11commit

snapshot

Transaction’s parameters

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

Page 17: How Firebird transactions work

17

© IBSurgeonFirebird Conference 2014, Prague17

How we will present about transactions

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

Tx 11commit

Insert into T1(i1) values (100);

snapshot

Operation in the frames of transaction

Page 18: How Firebird transactions work

18

© IBSurgeonFirebird Conference 2014, Prague18

How we will present about transactions

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

Tx 11commit

Insert into T1(i1) values (100);

SELECT i1 FROM T1

snapshot

i1100

Result of operation

Page 19: How Firebird transactions work

19

© IBSurgeonFirebird Conference 2014, Prague19

Now let's start...

Basics your [probably] know:

- Everything in the database is done within transaction

- Each transaction get it’s own incremented number1, 2, 3, … etc

- Firebird is a multi-version engine (each record in Firebird can have versions)

Page 20: How Firebird transactions work

20

© IBSurgeonFirebird Conference 2014, Prague20

Record versions is a key thing for understanding transactions' work in Firebird.

Page 21: How Firebird transactions work

21

© IBSurgeonFirebird Conference 2014, Prague21

How record versions appear

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

Tx10 commit

Insert into T1(i1) values

(100);

Page 22: How Firebird transactions work

22

© IBSurgeonFirebird Conference 2014, Prague22

How record versions appear

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

Tx10 commit

Insert into T1(i1) values

(100);

Tx50commit

SELECT i1FROM T1

i1100

Page 23: How Firebird transactions work

23

© IBSurgeonFirebird Conference 2014, Prague23

How record versions appear

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

Tx10 commit

Insert into T1(i1) values

(100);

Tx50commit

SELECT i1FROM T1

i1100

Tx60commit

UPDATE T1SET i1=200

new version!

Page 24: How Firebird transactions work

24

© IBSurgeonFirebird Conference 2014, Prague24

How record versions appear

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

Tx10 commit

Insert into T1(i1) values

(100);

Tx50commit

SELECT i1FROM T1

i1100

SELECT i1FROM T1

Tx60commit

UPDATE T1SET i1=200

SELECT i1FROM T1

i1100

i1200

Page 25: How Firebird transactions work

25

© IBSurgeonFirebird Conference 2014, Prague25

How it works?

Page 26: How Firebird transactions work

26

© IBSurgeonFirebird Conference 2014, Prague26

Each record version has transaction #

N on page Transaction number Datafield1, datafield2

1 50 100

Page 27: How Firebird transactions work

27

© IBSurgeonFirebird Conference 2014, Prague27

TR50read

N Tx Data

1 10 100

...

Page 28: How Firebird transactions work

28

© IBSurgeonFirebird Conference 2014, Prague28

N Tx Data

1 10 100

...

TR50 TR60writeread

Page 29: How Firebird transactions work

29

© IBSurgeonFirebird Conference 2014, Prague29

N Tx Data

1 10 100

60 200

...

TR50 TR60read

write

Page 30: How Firebird transactions work

30

© IBSurgeonFirebird Conference 2014, Prague30

TR50 TR60

read

N Tx Data

1 10 100

60 200

...

readwrite

Page 31: How Firebird transactions work

31

© IBSurgeonFirebird Conference 2014, Prague31

Some intermediate conclusions

1. No “locks” are placed on the record

2. There can be a lot of committed versions for one record

3. Versions may be needed or not. If not, they can be considered as “garbage”.

4. Only one non-committed version can exist for the record(2 active transactions can’t update the same record)

Page 32: How Firebird transactions work

32

© IBSurgeonFirebird Conference 2014, Prague32

How server knows about transactions states? Is transaction Active or not?

• TIP – Transaction Inventory Pages• Linear list of transaction states, from 1 to last transaction number

• Stored in the database• Limitation — 2 billions of transactions

Page 33: How Firebird transactions work

33

© IBSurgeonFirebird Conference 2014, Prague33

Transaction states

• Each transaction is represented in Transactions Inventory by it’s state• 00 – Active• 01 – Committed• 10 – Rolled back• 11 – Limbo (distributed 2-phase

transactions)

TIP contents

Tx № Tx state

10 committed

11 committed

12 committed

13 rolled back

14 committed

15 committed

16 committed

17 rolled back

18 active

19 committed

20 active

Page 34: How Firebird transactions work

34

© IBSurgeonFirebird Conference 2014, Prague34

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

Tx10 commit

Insert into T1(i1) values

(100);

Tx50commit

SELECT i1FROM T1

i1100

SELECT i1FROM T1

Tx60commit

UPDATE T1SET i1=200

SELECT i1FROM T1

i1100

i1200

TIP

Tx State

10 Commited

Tx State

10 Commited

50 Active

60 Active

Tx State

10 Commited

50 Commited

60 Commited

Page 35: How Firebird transactions work

35

© IBSurgeonFirebird Conference 2014, Prague35

Transaction isolation levels

Page 36: How Firebird transactions work

36

© IBSurgeonFirebird Conference 2014, Prague36

Isolation levels in Firebird

READ COMMITED

SNAPSHOT

SNAPSHOT WITH TABLE STABILITY

Isolation levels in FirebirdIsolation levels in Firebird

Page 37: How Firebird transactions work

37

© IBSurgeonFirebird Conference 2014, Prague37

Snapshot

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

Tx 52commit

Insert into T1(i1) values (100);

Tx 51rollback

Insert into T1(i1) values (200);

Tx 10commit

SELECT FROM T1 SELECT FROM T1

snapshot

i1

Page 38: How Firebird transactions work

38

© IBSurgeonFirebird Conference 2014, Prague38

Read Commited

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

Tx 15commit

Insert into T1(i1) values (100);

Tx 10commit

SELECT i1 FROM T1

SELECT i1FROM T1

read commited

i1100

i1

Page 39: How Firebird transactions work

39

© IBSurgeonFirebird Conference 2014, Prague39

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10

Read Committed transactions “see” global TIP. That’s why they can read committed changes of other transactions

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10

Snapshot copies TIP on it’s start. It does not see any changes made byother committed transactions after snapshot start

Read Commited and Snapshot

Page 40: How Firebird transactions work

40

© IBSurgeonFirebird Conference 2014, Prague40

TIP for Read Commited

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

Tx 15commit

Insert into T1(i1) values (100);

Tx 10commit

SELECT i1 FROM T1

SELECT i1FROM T1

read commited

i1100

i1

Tx State

10 Active

Tx State

10 Active

15 Active

Tx State

10 Active

15 Commited

Page 41: How Firebird transactions work

41

© IBSurgeonFirebird Conference 2014, Prague41

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

Tx 52commit

Insert into T1(i1) values (100);

Tx 51rollback

Insert into T1(i1) values (200);

Tx 10commit

SELECT FROM T1 SELECT FROM T1

snapshot

i1

Tx State

10 Active

Tx State

10 Active

51 Active

52 Active

Tx State

10 Active

51 Rollback

52 Commited

Tx State

10 Active

TIP for snapshot

Page 42: How Firebird transactions work

42

© IBSurgeonFirebird Conference 2014, Prague42

Each transaction can see:

• Own created records and versions• Insert, Update, Delete

• If it is Read Committed, it can see every changes that was made by committed transactions, because it looks into global TIP

• If it is Snapshot, it can see own changes and record versions commited to the moment of its start, because it looks into it’s own copy of TIP

Page 43: How Firebird transactions work

43

© IBSurgeonFirebird Conference 2014, Prague43

Record versions visibility

Page 44: How Firebird transactions work

44

© IBSurgeonFirebird Conference 2014, Prague44

How we will present about records

Each record can have versions, created by different transactions

Record 10 Tx 10 100 Tx 20 200 Tx 30 555

Page 45: How Firebird transactions work

45

© IBSurgeonFirebird Conference 2014, Prague45

Record 10 Tx 10 100 Tx 20 200 Tx 30 555

R10 Tx 10 Tx 20 Tx 30

Compact representation

How we will present about records

Page 46: How Firebird transactions work

46

© IBSurgeonFirebird Conference 2014, Prague46

3 rules of record visibilty

1) For each snapshot transaction engine maintains stable view of database

2) Transaction can not see record versions created by another active transaction

3) Transaction should walk backversions chain looking for commited backversion

Page 47: How Firebird transactions work

47

© IBSurgeonFirebird Conference 2014, Prague47

Ex: record versions visibility for Tx20

Snapshot isolation, copy of TIP for Tx20

Page 48: How Firebird transactions work

48

© IBSurgeonFirebird Conference 2014, Prague48

• In order to figure out which record version is visible, every transaction must read TIP

• TIP can contain up to 2 Billion transactions• So each transaction should read up to 2 billions

of transactions! - Damn, that's why Firebird is slow! (it's a joke)

Page 49: How Firebird transactions work

49

© IBSurgeonFirebird Conference 2014, Prague49

TIP (example)

We need a way to separate old, not interestingtransactions from currently active part of TIP

● For this purpose engine maintains Oldest Interesting Transaction marker, or OIT

Page 50: How Firebird transactions work

50

© IBSurgeonFirebird Conference 2014, Prague50

TIP (example)

Page 51: How Firebird transactions work

51

© IBSurgeonFirebird Conference 2014, Prague51

Page 52: How Firebird transactions work

52

© IBSurgeonFirebird Conference 2014, Prague52

firebird>gstat -h A.FDBDatabase header page information:Flags 0Generation 6System Change Number 0Page size 4096ODS version 12.0Oldest transaction 1Oldest active 2Oldest snapshot 2Next transaction 3Sequence number 0Next attachment ID 3

Transaction markers

Page 53: How Firebird transactions work

53

© IBSurgeonFirebird Conference 2014, Prague53

4 markers

• Transaction markers are key characterstics of TIP and transaction mechanism

– Let's see what they mean and how they evaluated:• NEXT — next transaction• OAT — Oldest Active • OST — Oldest Snapshot• OIT — Oldest Interesting

Page 54: How Firebird transactions work

54

© IBSurgeonFirebird Conference 2014, Prague54

NEXT

• NEXT is the simplest — it's the most recent transaction

• NEXT number is written on header page

Page 55: How Firebird transactions work

55

© IBSurgeonFirebird Conference 2014, Prague55

OAT is the first transaction in TIP which state is “active”

Evaluation:

● Scan TIP starting from current OAT value looking

for “active” transaction

● Save found value in transaction's lock data

● Save found value as new OAT marker

OAT is really an oldest active transaction

OAT - Oldest Active Transaction

Page 56: How Firebird transactions work

56

© IBSurgeonFirebird Conference 2014, Prague56

OAT evaluation example

Page 57: How Firebird transactions work

57

© IBSurgeonFirebird Conference 2014, Prague57

Problems indicated by OAT

● Where to look?● NEXT — OAT > (number of connections * number of

transaction)● What it means?

● Long running transaction which makes Firebird to think that record versions are still needed

Page 58: How Firebird transactions work

58

© IBSurgeonFirebird Conference 2014, Prague58

Page 59: How Firebird transactions work

59

© IBSurgeonFirebird Conference 2014, Prague59

Page 60: How Firebird transactions work

60

© IBSurgeonFirebird Conference 2014, Prague60

Page 61: How Firebird transactions work

61

© IBSurgeonFirebird Conference 2014, Prague61

OST and Read Commited transactions

Page 62: How Firebird transactions work

62

© IBSurgeonFirebird Conference 2014, Prague62

Page 63: How Firebird transactions work

63

© IBSurgeonFirebird Conference 2014, Prague63

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

UPDATE T1

UPDATE T1

UPDATE T1

UPDATE T1

UPDATE T1

N Tx Data

1 4 ccc

2 1 aaa

3 2 bbb

4 3 bbbb

5

6

7

...

Select * from rdb$databasewrite

The longer transaction lasts, the higher chance to create potentially useless (potential garbage) versions

Page 64: How Firebird transactions work

64

© IBSurgeonFirebird Conference 2014, Prague64

Where to look (OST-OIT) > sweep interval

What it means– Autosweep does not work (if sweep interval >0)– Some records need garbage collection

Problems indicated by OST

Page 65: How Firebird transactions work

65

© IBSurgeonFirebird Conference 2014, Prague65

• Direct • Loss of performance due to more record versions: i.e., queries

become slower• More indexed reads • More data page reads

• 1.5mln versions ~30mb per record

• Indirect• After transaction’s end its versions become garbage, and garbage

collection mechanism tries to gather it• Due to long transaction OST stuck, so autosweep (if it is not

disabled) tries to start at unpredictable moment (and ends without success)• GC and sweep can consume a lot of resources• Unpredictable moment can occur at high load time

Problems caused by long running transactions

Page 66: How Firebird transactions work

66

© IBSurgeonFirebird Conference 2014, Prague66

Oldest Interesting Transaction

Page 67: How Firebird transactions work

67

© IBSurgeonFirebird Conference 2014, Prague67

TIP size

• TIP to be copied is NEXT - OIT

• Size of active part of the TIP in bytes is (Next – OIT) / 4

Page 68: How Firebird transactions work

68

© IBSurgeonFirebird Conference 2014, Prague68

Page 69: How Firebird transactions work

69

© IBSurgeonFirebird Conference 2014, Prague69

Problems indicated by OIT

Where to lookOIT- OST

ProblemBig size of TIP

— Global, and, specifically copies of TIP for snapshots

Page 70: How Firebird transactions work

70

© IBSurgeonFirebird Conference 2014, Prague70

Ideal transactions flow

t0 t1 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12

TxNN

TxNN

TxNN

TxNN

TxNN

TxNN

TxNN

TxNN Short transactions does not stuck OIT or OAT or OST, and avoid problems related with it.

Oldest transaction X-1Oldest active XOldest snapshot XNext transaction X+1

Page 71: How Firebird transactions work

71

© IBSurgeonFirebird Conference 2014, Prague71

Summary

• Make write (for INSERT/UPDATE/DELETE) transactions as short as possible

• Use Read Commited Read-Only transactions for SELECTs

Page 72: How Firebird transactions work

72

© IBSurgeonFirebird Conference 2014, Prague72

Thank you!

• Questions? [email protected]