






















































































Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!























































































1 Introduction W
Exercise: We have these 2 relations:
Graduated(gcode, mark, school) School(scode, city)
But instead we can write:
Π푐푖푡푖푒푠
What is the difference? Prove that they are equivalent.
Suppose we have Graduated(gcode, mark, school) and School(scode, city). We need to prove that these 2 queries are equivalent:
(a) Π푐푖푡푖푒푠
and
(b) Π푐푖푡푖푒푠
Selection operation is commutative, so: 휎퐴휎퐵(푅) = 휎퐵 휎퐴(푅). A selection whose condition is a con- junction is equivalent to a sequence of selections with those same individual conditions: 휎퐴∧퐵(푅) = 휎 1 (휎퐵(푅)) = 휎퐵(휎퐴(푅)). Let 휎퐴 be: 휎퐴 = 휎퐵∧퐶 = 휎푚푎푟 푘 = 100 ︸ ︷︷ ︸ 퐵
퐶
. From the properties of
selection we have that: 휎퐴 = 휎퐵∧퐶 = 휎퐵(휎퐶 (푅)) = 휎퐶 (휎퐵(푅)) Let 푅 be (퐺푟푎푑푢푎푡푒푑 × 푆푐ℎ표표푙), so if I first apply 휎퐶 to 푅 and then 휎퐵 it is the same as applying 휎퐵 first and then 휎퐶.
The sequences of the two operations are:
First:
Graduated School
Second:
Graduated
School
The second method is better on performance because we compute a join after some tuple and the circled nodes have been removed.
Data Management Notes
2 Architecture of a DBMS W
Databases are a set of database files where each file is constituted by a set of pages, stored in physical blocks. If we want to use a page we need to bring it in main memory. The size of a block (and of a page) is the size of portion of storage that can be transferred to main memory and vice versa. The buffer pool is a non-persistent memory space dedicated to the DBMS to process pages. Th buffer management is responsible of the transfer of the pages between ram and disk. The buffer pool is used by the system /recovery manager) and shared by all transactions.
Data Management Notes
2.1.1 Buffer Pool
Organized in frames, that are main memory pages, whose size is that of a block. Buffer pool is in RAM so its management is more efficient with respect to secondary storage. Is managed with the same principles of the cache (locality, block replacement, ...).
2.1.2 Buffer Manager
It uses ”page table” data structure that associates to each page an ID (PID) appearing in the buffer. It uses 5 primitives: FIX, UNFIX, USE, FORCE and FLUSH.
External module issues a ”Fix” in order to ask the buffer manager to load a specific page into a frame of the buffer. For each frame, the BM maintains:
What happens when a Fix is issued:
1 BM checks if a frame already contains the page with requested PID 2 If YES, PIN-COUNT ++ (of that fram) 3 If NOT, then
3.1 BM chooses a frame 퐹′^ according to certain replacement policy. 3.2 If the dirty-bit of 퐹′^ is TRUE, then the page contained in F’ is written back to secondary storage.
3.3 Page 푃 is read from secondary storage and loaded into 퐹′. Then PIN-COUNT of 퐹′^ = 0 and dirty-bit = FALSE
4 BM returns the address of 퐹′^ to the module issuer.
2.1.4 Replacement Policy
The victim is chosen among those with pin-count=0. If no frame has pin-count = 0, the request is put in a queue or the transaction is aborted. Otherwise, two policies can be used: LRU & Clock Replacement.
1 LRU: Least Recent Used, done with a queue containing the frames with pin = 0. 2 Clock Replacement: Using a pointer called current, we cycle through the frames. Each frame has a bit called referenced that says if the page contained in the frame has been used recently. Referenced is set to TRUE when pin-count = 0. If the pointed frame has pin-count = 0, we split that (page used often). If the frame has pin-count = 0 and referenced = TRUE, we set it to FALSE because time has passed since the last used of the page contained in the frame, then we split the frame. Finally, if pin-count = 0 and referenced = FALSE, the frame is chosen as victim.
2.1.5 Unfix
The transaction certifies that is does not need to the content of a page anymore pin-counter decremented.
2.1.6 Use
The transaction modifies the content of a frame dirty-bit set to TRUE
2.1.7 Force
Synchronous (i.e. the transaction waits for the successful competition of the operation) transfer to secondary storage of the page contained in the frame.
Data Management Notes
Transaction is an abstraction of execution from a software that wants to access data (read/write). Syntactically, it starts with ”BEGIN”, ends with ”END” and confirms what it has done with ”COMMIT”. Concur- rency is a fundamental concept, we mus unsure it among transactions (for throughput purposes).
.................................................. .Example................................................... Example of concurrency problem: APP 1
FindSeat
BookSeat
FindSeat finds the same seat so it will be a problem for APP2 to book it after APP1. This is because transactions are not isolated.
3.1.1 Isolation
Isolation of transactions means that it is executed like it was the only one in the system, i.e. without concurrent transactions
3.1.2 ACID properties
Properties of transactions are called ACID properties.
1 Atomicity: Either all or none of the transactions actions have their effect.
2 Consistency: Each transaction execution brings the DB to a correct state.
3 Isolation: Each transaction is independent from the others executed concurrently. 4 Durability: If a transaction execution succeed, then its effect is permanent stored in DB.
3.1.3 Schedules
Suppose we have a set of transactions {푇 1 , ..., 푇푁 }. A sequence 푆 of execution of actions of such transaction respecting the order within each transactions is called schedule. A schedule can be partial if does not contain all the actions of the transactions. A schedule is serial if the actions of different transactions in 푆 do not interleave.
3.1.4 Serializability
A schedule 푆 that is not serial is correct with respect to concurrency if it is serializable, i.e. there is a serial sched- ule 푆′^ constituted by the same transactions of 푆 s.t. ∀ initial state 퐼, the outcome of 푆 is the same as the outcome of 푆′.
”A schedule 푆 on {푇 1 , 푇푁 } is serializable if there exists a serial schedule on {푇 1 , ..., 푇푁 } that is equivalent to 푆”. What does equivalent mean? ∀ input → produces the same output.
Two finite states automatas are equivalent if ∀ input they produce the same output.
But if the input is infinite (all the possible inputs) → then it does not exist an algorithm that can check if two automatas are equivalent. The problem is called the ”Halting” problem and it is an undecidable problem. So the Serializable’s definition cannot be applied in pracrice because the equivalence cannot be checked. → The solution is to ”Relax” the notion of equivalence.
Data Management Notes
Very often, software’s architecture is made as Tier III:
Presentation Layer
App. Layer
Actions
Transaction Manager
Transactions (푇 1 , 푇 2 , ..., 푇 3 ) are created in App. Layer.
User 1 wants to buy a ticket User 2 .. .. .. .. .. .. .. User 1 wants to give a score to the service of buying a ticket. If transaction manager receives transaction 푇 1 , 푇 2 , 푇 3 , it thinks they are correct because the app layer gave them to it. So it should be the App. Layer to think if there are problems with the order of the transactions, not the data layer. Inside the transaction manager there is the Scheduler.
3.1.5 Scheduler
1 It assigns an ID to the transactions. 2 It abstracts from operations in the local store. 3 It decides if a schedule is acceptable of not.
Defining different notion of serializability to solve the equivalence problem.
Preliminary Definitions
Let 푆 1 and 푆 2 be two complete schedules on the same transactions. Then 푆 1 is View-Equivalent to 푆 2 if 푆 1 and 푆 2 have the same READS-FROM relation and the same FINAL-WRITE set. A complete schedule 푆 is View-Serializable if ∃ a serial schedule 푆′^ on the same transactions of 푆 that is view- equivalent to 푆.
Example: Consider the following schedule
read1(A,t) read2(A,s) s:=100 write2(A,s) t=100 write1(A,t)
Is it serializable? Is it View-Serializable? 푇 1 and 푇 2 do the same thing on the DB, does not matter the initial state, they will write 100, so it is serializable. If we write it without local store:
R1(A) R2(A) W2(A) W1(A)
훼 → 푇 1 푇 2 → R1(A) W1(a) R2(A) W2(A). NO! 훽 → 푇 2 푇 1 → R2(A) W2(A) R1(A) W1(A). NO!
훼 and 훽 both changes the READ-FROM! So it is not View-Serializable. Remember that I need the local-store to check if it is serializable.
Data Management Notes
3 Transaction management W
Theorem: Conflict-Serializability
A schedule 푆 is conflict-serializable iff the prece- dence graph 푃(푆) associated to 푆 is acyclic. To prove the theorem:
Preliminary Lemma
If two schedule 푆 1 and 푆 2 are conflict-equiv ︸ ︷︷ ︸ 훼
then
훽
Lemma’s Proof
I have to prove that 훼 → 훽, so if I show that 훼∧! 훽 is impossible I’ve done (or! 훽 →! 훼) (or 훼 →! 훽 → ... →! 훼).
The green edge appears in 푃(푆 1 ), so 푝푖 (퐴) comes before 푞 (^) 푗 (퐴) because they are conflicting. If this edge does not appear in 푃(푆 2 ) (and remember that the schedules are on the same transactions) then I can assume that 푞 (^) 푗 (퐴) comes before 푝푖 (퐴), negating the assumption on 훼.
Now that the lemma has been proved, we prove the theorem.
Theorem’s Proof
In order to prove the Theorem, in addition to the Lemma’s proof, we need to prove the Exe2 (slide 41) that says: if 푆 is a serial schedule, then the precedence graph 푃(푆) is acyclic. Proof : In a serial schedule on the transaction 푇 1 , ..., 푇푁 , all the actions of each transaction cannot interleave with the action of other transactions. This means that even if there are some conflicting actions from 푇 1 to 푇 2 , the converse is impossible because no action of 푇 2 comes after some action of 푇 2. Theorem’s Proof : Lemma + Exe2: Starting from the definition of conflict-serializable we have demonstrated that if two schedules 푆 1 and 푆 2 are conf-equiv, then 푃(푆 1 ) = 푃(푆 2 ) ︸ ︷︷ ︸ LEMMA
and that
if 푆 is a serial schedule, than 푃(푆) is acyclic ︸ ︷︷ ︸ EXE
Conflict-serializ. says that: A schedule 푆 is conflict-serializable if there exists a serial schedule 푆′^ that is conf.-equiv to it. Let’s suppose we find a serial schedule 푆′^ that is conf-equiv to 푆. (This means that there are no actions 푎푖 of 푇푖 and 푏 (^) 푗 of 푇푗 s.t. 푎푖 and 푏 (^) 푗 are conflicting and the mutual position of the two actions in 푆 1 is different from their mutual position in 푆 2. So, for the first Lemma, 푆 and 푆′^ have the same precedence graph. 푃(푆) = 푃(푆′). And for the Exe2, 푃(푆′) is acyclic. So 푃(푆) too is acyclic.
Data Management Notes
3 Transaction management W
3.3.1 Topological order of a graph
Is a sequence of nodes of 퐺 s.t. if the edge 푇푖 → 푇푗 is in the graph 퐺, then 푇푖 appears before 푇푗 in the sequence 푆. Two propositions:
1 If the graph 퐺 is acyclic, then there exists at least one topological order of 퐺. Algorithm to find it: Pick a node without incoming edges, remove all of its outgoing edges, add the node to the sequence, pick another node and so on.
Apply The Algo
푆 = { 1 , 3 , 2 , 4 } → Top. Order 2 If 푆 is a Top. of 퐺, and there exists a oath from node 푛 1 to node 푛 2 in 퐺, then 푛 1 is before 푛 2 in 푆.
We need to prove the other direction of the Theorem: =⇒ Let 푆 be defined on the transaction 푇 1 , ..., 푇푁. Suppose that 푃(푆) is acyclic. Then, there exists at least one topological order of 푃(푆), i.e. a sequence of its nodes s.t. if 푇푖 → 푇푗 is in 푃(푆), then 푇푖 appears before 푇푗 in the sequence. To such a topological order of 푃(푆), it corresponds the serial schedule 푆′^ on 푇 1 , ..., 푇푁 s.t. if 푇푖 → 푇푗 is in the graph, then all actions of 푇푖 appears immediately before 푇푗 in 푆′. It is easy to see that such a schedule 푆′^ is conflict-equiv. to 푆. Indeed, if 푆′^ is not conf.-equiv. to 푆, then there is a pair of conflicting actions 푎ℎ and 푏푘 s.t. (푎ℎ <푆′^ 푏푘 ) & (푏푘 <푆 푎ℎ ). But (푏푘 <푆 푎ℎ ) means that the path 푇푘 → 푡ℎ is in the graph 푃(푆), and therefore 푇푘 appears before 푇ℎ in every top. order of 푃(푆). However, (푎ℎ <푆′^ 푏푘 ) means that 푇ℎ appears before 푇푘 in 푆′, and this contradicts the fact that 푆′^ corresponds to a top. order of 푃(푆).
3.3.2 Algorithm for Conflict-Serializability
Exercise:
Top. Order = {1, 3, 2, 4} Yes, is conflict-serializable.
Data Management Notes
3 Transaction management W
Example:
푇 3 ︷ ︸︸ ︷ 푊 3 (푦) 퐶 3 푊 1 (푥) ︸︷︷︸ 푇 1
푇 2 ︷ ︸︸ ︷ 푅 2 (푥) 퐶 2 푊 1 (푦) 퐶 1 ︸ ︷︷ ︸ 푇 1
푆 ∈ 푂퐶푆푅 because in 푆′^ = { 3 , 2 , 1 } 푇 3 totally precedes 푇 2.
1 2 3
푆 /∈ COCSR because 푇 1 → 푇 2 but 퐶 2 comes before 퐶 1.
View-serial is not used ove to the complexity, conf-serial is not used ove to the size of 푃(푆) (it can be made of thousand on nodes!). So, what’s the solution? =⇒ Locks.
Rule 1
Every transaction should be well-formed. It’s well-formed if every action 푝푖 (퐴) of 푇푖 is contained in a ”critical section” i.e. in a sequence of actions delimited by a pair on lock-unlock on 퐴.
푇푖 : 퐼푖 (퐴)...푝푖 (퐴)...푢푖 (퐴)
Rule 2
The schedule is legal. 푆 with locks is legal is no transactions in it locks an element 퐴 when a different transaction has granted the lock on 퐴 and has not yet unlocked 퐴.
푆 : ...퐼푖 (퐴) ....... ...... ︸ ︷︷ ︸ No 퐼푗 (퐴)
The schedule manages the so called Lock Table, which contains all the requests coming from other transactions on an object 퐴 while 퐴 is locked by another transaction. Otherwise, if the Schedules would grant the lock on 퐴 while 퐴 is locked, the schedule become illegal. The scheduler ensures that the schedule remains well-formed too.
Are the locks sufficient for serializability? → No, isolation is not ensured (ghost update could appear for example). Solution: 2PL.
Data Management Notes
3 Transaction management W
3.4.1 Two Phase Locking
A schedule 푆 follow the 2PL protocol if each 푇푖 ∈ 푆, all lock operations precede all unlock operations.
푆 : ................ ︸ ︷︷ ︸ No unlock for 푇푖
No lock for 푇푖
This creates a plan divided in two sections over time:
But while 2PL avoid ghost update, it can happen that a deadlock appears.
3.4.2 Exercises
Exercise: 푟 1 (푥) 푤 2 (푥) 푤 3 (푦) 푤 1 (푧) 푤 3 (푧) follows 2PL protocol?
푙 = Lock 푤 = write 푢 = unlock
푙 1 (푥) 푟 1 (푥) 푙 2 (푧) 푢 1 (푥) 푙 2 (푥) 푤 2 (푥) 푢 2 (푥) 푙 3 (푦) 푤 3 (푦) 푤 1 (푧) 푢 1 (푧) 푙 3 (푧) 푤 3 (푧) 푢 3 (푦) 푢 3 (푧)
If to the sequence above we remove the lock and unlock operation, we obtain 푟 1 (푥) 푤 2 (푥) 푤 3 (푦) 푤 1 (푧) 푤 3 (푧) that is equal to the input =⇒ Is 2PL Schedule.
Important Notes
Exercise: 푟 1 (푥) 푤 2 (푥) 푤 3 (푦) 푤 3 (푧) 푤 1 (푧)
푙 1 (푥) 푟 1 (푥) ︸ ︷︷ ︸ 푇 1
푇 2 waits
푇 3
푇 2 continues
Sequence of output actions: 푆′ 2 : 푟 1 (푥) 푤 3 (푦) 푤 3 (푧) 푤 1 (푧) 푤 2 (푥) 6 = 푟 1 (푥) 푤 2 (푥) 푤 3 (푦) 푤 3 (푧) 푤 1 (푧) =⇒ Not in 2PL Class.
Data Management Notes
3 Transaction management W
1 푆 suffers from deadlock −→ 푆 ∈ 2PL. TRUE 2 푆 does not suffer deadlock −→ 푆 ∈2PL. FALSE
We never considered the rollback in the transactions. The notions of serializability that we have considered are not strong enough to guarantee ACID. This is because a new anomaly called ”Dirty-Read”. 푇 1 푇 2 BEGIN BEGIN 푅푒푎푑(퐴, 푥) 푥 = 푥 + 1 푊푟푖푡푒(퐴, 푥) 푅푒푎푑(퐴, 푥) 푥 = 푥 + 1 Rollback 푊푟푖푡푒(퐴, 푥) Commit
Problem: 푇 2 reads a value written by 푇 1 before 푇 1 commits or rollbacks. There- fore, 푇 2 reads a value that is not supposed to be there → Dirty Value!
3.5.1 Cascading Rollback
A rollback of a transaction 푇푖 can trigger the rollback if other transactions. If a transaction 푇푗 read from 푇푖 , we should kill 푇푗 (푇푗 should rollback). If 푇ℎ has read from 푇푗 it should be killed and so on.
3.5.2 Recoverable Schedules
A schedule 푆 is recoverable if no transaction is no transaction in 푆 commits before all other transactions it has ”READ-FROM”, commit.
Recoverability =⇒ No Dirty Reads
Example of recoverable schedule: 푆 : 푤 1 (퐴) 푤 1 (퐵) 푤 2 (퐴) 푟 2 (퐵) 푐 1 푐 2
In this case 푇 2 commit after 푇 1 =⇒ Recoverable.
Example of NON recoverable schedule: 푆 : 푤 1 (퐴) 푤 1 (퐵) 푤 2 (퐴) 푟 2 (퐵) 푟 3 (퐴) 푐 1 푐 3 푐 2
In this case 푇 2 reads from 푇 1 and 푇 3 reads from 푇 1 =⇒ 푇 3 commits after 푇 1 =⇒ Not recoverable.
Beware that recoverable schedules can still suffer from the cascading rollback!
Data Management Notes
3 Transaction management W
A schedule 푆 avoids cascading rollback if every trans. in 푆 read value that are written by transactions that have already committed. Example:
푆 = 푤 2 (퐴) 푤 1 (퐵) 푤 1 (퐴) 푐 1 푟 2 (퐵) 푐 2 → ACR blocks the dirty-read!
3.5.4 Strict Schedules
In a schedule 푆, a transaction 푇푖 writes on 푇푗 if there is a 푤 (^) 푗 (퐴) in 푆 followed by 푤푖 (퐴), and there is no write action on 퐴 in 푆 between these two actions. A schedule 푆 is strict if every transaction reads only values written by transactions that have already committed, and writes only on transactions that have already committed. String → ACR → Recoverable.
3.5.5 Rigorous Schedules
A schedule 푆 is rigorous if for each pair of conflicting actions 푎푖 and 푏 (^) 푗 appearing in 푆, with 푎푖 appearing before 푏 (^) 푗 , the commit command 푐푖 of 푇푖 appears in 푆 between 푎푖 and 푏 (^) 푗.
Rigorous → Strict → ACR → Recoverable.
Prove that every Rigorous Schedules is Serializable
We have a schedule 푆 and a serial schedule 푆′^ coherent with the sequence and the order of the commits of the transactions. 푇 1 푐 1 푇 2 푐 2 푇 3 푐 3 ...푇푁 푐푁. Goal: show that 푆′^ in conf. equiv. to 푆. Suppose that is not true: we have a pair of conflicting actions that appear in different order in the two schedules. 푆 : ...푎푖 ...푏 (^) 푗 ...; 푆′^ : ...푏 (^) 푗 ...푎푖 .... If 푆 is rigorous (our assumption) then, by definition, since 푎푖 and 푏 (^) 푗 are conflicting, we have a commit action in the middle: 푆 : ...푎푖 푐푖 ...푏 (^) 푗 푐 (^) 푗 .... But if we assume that 푆′^ has that configuration shown above, it means that 푇푗 comes before 푇푖. But this is impossible because we have defined 푆′^ to be the serial schedule that is coherent on the commits order. So , 푆′^ conf. equiv. to 푆, therefore 푆 = conf.-serializable → 푆 is serializable.
A schedule S is said to be in the strict 2PL class if
3.6.1 Strict 2PL Protocol
A schedule 푆 follow the strict 2PL protocol if it follows the 2PL protocol, and all exclusive locks of every transaction 푇 are kept by 푇 until 푇 commits or rollbacks. (Every schedule following 푆 2 푃퐿 → Strict).
3.6.2 Strong Strict 2PL Protocol
A schedule 푆 follows the Strong Strict 2PL Protocol if it follows 2PL protocol, and all locks of every transaction 푇 are kept by 푇 until 푇 commits or rollbacks. (Every schedule following SS2PL → Rigorous).
Data Management Notes