Lecture 26#
Announcements#
This is our last class!
SQL Isolation Levels#
read uncommitted;
Not supported in Postgresql
read committed;
Does not allow read of uncommitted values, but the transaction may read the same value twice and get different results
repeatable read ;
Does not allow the same read producing different results, but it allows phantom updates (changes that are not directly read but impact the query results)
serializable ;
Does not allow phantom updates either.
-- T1:
begin transaction ;
set transaction isolation level read uncommitted;
--not implemented
set transaction isolation level read committed;
set transaction isolation level repeatable read ;
set transaction isolation level serializable ;
commit ;
end;
--- T2
begin transaction ;
commit ;
end;
Implementing serializability#
Two main approaches:
Locking mechanism: 2 Phase locking
Optimistic mechanism: Multi-version control (check at commit time if a change should be allowed)
Two phase locking#
Lock any item before a read or write operation
If the lock is available, xact can get it and continue processing
If the lock is not available, xact must wait until it becomes available
Only a single transaction can lock an item at any point in time
A transaction can get new locks if it is in the growing phase
Transactions cannot release locks in the growing phase
A transaction can release locks if it is in the shrinking phase
Transactions cannot get any more locks in the shrinking phase
As soon as a transaction in the growing phase releases a lock, it enters the shrinking phase.
Two phase locking guarantees serializability!
If a transaction management system uses 2PL (two phase locking), then all the schedules produced by this system are guaranteed to be serializable.
Example:
r1(x) r2(x) w1(x)
Conflicts:
r1(x) w2(x)
r2(x) w1(x)
Not serializable, there is a cycle.
If I were using 2PL, this schedule is not possible.
lock1_(x) r1(x) unlock1(x) lock2(x) r2(x) unlock2(x) XXXXXw1(x)
^T1 enters Not possible
shrinking phase T1 in shrinking
and has no lock on x
2PL guarantees serializability
Problems may occur with deadlocks and lock granularity (for higher concurrency)
DB Tuning with indexing#
Queries and their cost ( Cost(Q) )
Target queries that are very costly or very frequntly asked or both
Workload: Sum over all queries ( Cost(Q) * Frequency(Q) )