Docsity

Transaction & ACID properties, Lecture notes of Database Management Systems (DBMS)

Partial preview of the text

Download Transaction & ACID properties and more Lecture notes Database Management Systems (DBMS) in PDF only on Docsity!

Transactions & ACID Properties

Advanced Databases

M.Sc. Tran Quang Toan

Faculty of Data Science and Artificial Intelligence

Bases and coordinates

1. Transactions

2. Transaction Lifecycle

3. Deep Dive into ACID Properties

4. Case Study & Banking

5. Lab Session & Practice

Transactions

The ”Horror Story”: Life Without Transactions

  • (^) Scenario: Alice transfers $100 to Bob via a Banking App.
  • (^) Backend Operations:
    1. UPDATE accounts SET balance = balance - 100 WHERE user = ’Alice’;
  1. – System Crash / Power Failure / Network Timeout –
  2. UPDATE accounts SET balance = balance + 100 WHERE user = ’Bob’;
  • (^) The Result:
    • (^) Alice loses $100.
    • (^) Bob receives nothing.
    • (^) Money vanishes from the system.

Critical Question

How do we guarantee that either both steps succeed, or neither happens?

Transaction Lifecycle

Controlling Transactions in Postgres

In PostgreSQL, we use specific commands to define the boundaries of our work:

  • (^) BEGIN: Starts a new transaction block. From this point on, changes are ”private” and temporary.
  • (^) COMMIT: Makes all changes made since BEGIN permanent and visible to others.
  • (^) ROLLBACK: Aborts the transaction and undoes every change made within that block. 1 BEGIN; 2 UPDATE i n v e n t o r y SET s t o c k = s t o c k - 1 WHERE item_id = 1 0 1 ; 3 INSERT INTO o r d e r s ( item_id , s t a t u s ) VALUES ( 1 0 1 , 'PAID' ) ; 4 COMMIT; - - A l l or nothing happens h e r e

Success Pattern

Implicit vs. Explicit Transactions

Autocommit Mode (Implicit)

By default, PostgreSQL operates in autocommit mode. Every single SQL statement is treated as a transaction that commits automatically if it succeeds.

Explicit Transactions

You manually group multiple statements using BEGIN...COMMIT.

Why go Explicit?

  • (^) Business Logic: Ensuring consistency across multiple tables (e.g., Order + Payment).
  • (^) Performance: One large COMMIT is significantly faster than 1,000 small ones (less disk I/O overhead).

Postgres-Specific: The ”In Failed Transaction” State

In Postgres, if one command fails, the transaction is ”poisoned.” No further

commands are accepted until you ROLLBACK.

1 BEGIN; 2 - - This v a l i d update w i l l be LOST 3 UPDATE f i l m SET r e n t a l _ r a t e = 0. 9 9 WHERE r a t i n g = 'G' ; 4 - - ERROR: Language ID 999 does not e x i s t (FK v i o l a t i o n ) 5 UPDATE f i l m SET language_id = 999 WHERE t i t l e = 'ACADEMY DINOSAUR' ; 6 - - This v a l i d update w i l l be IGNORED 7 UPDATE f i l m SET r e n t a l _ r a t e = 4. 9 9 WHERE r a t i n g = 'R' ; 8 COMMIT;

Result:

  • (^) Because step 2 failed, the entire block is aborted.
  • (^) Verification: Run SELECT rental_rate FROM film WHERE rating = ’G’;
  • (^) You will see the old prices. Postgres prioritizes Atomicity over partial success. (^) 9/

Deep Dive into ACID Properties

The ACID Properties

To guarantee reliability, every DBMS transaction must adhere to four properties:

  • (^) A - Atomicity: ”All or nothing.”
  • (^) C - Consistency: ”From one valid state to another.”
  • (^) I - Isolation: ”Concurrent transactions don’t interfere.”
  • (^) D - Durability: ”Once committed, it stays committed.”

Why ACID matters for AI?

If your AI training pipeline or agent logs results to a database that isn’t ACID-compliant, a system crash could corrupt your model’s history or metadata.

Atomicity in Crashes

What if the server loses power during a transaction?

  • (^) Upon restart, Postgres performs Crash Recovery.
  • (^) It looks at the logs: Any transaction that didn’t reach the COMMIT state is automatically rolled back.
  • (^) Result: No ”half-finished” data ever exists in your tables.

Consistency: Maintaining the Rules

Consistency ensures that a transaction takes the database from one valid state to another, according to all defined rules. Database Rules include:

  • (^) Constraints: Primary Keys, Unique, Foreign Keys.
  • (^) Check Constraints: e.g., balance >= 0.
  • (^) Triggers: Custom business logic code.

If a transaction violates any of these, the DBMS will reject the change and maintain consistency.

Isolation: Working in Private

In a real-world system, thousands of transactions run at the same time. Definition:

Isolation ensures that the execution of one transaction is not visible to other concurrent transactions until it is committed.

  • (^) Without Isolation, we get ”Dirty Reads” (reading uncommitted, ”fake” data).
  • (^) AI Impact: Parallel data processing scripts might read inconsistent data, leading to incorrect feature engineering.

The 4 Standard Isolation Levels

According to the SQL Standard, there are levels of strictness:

  1. Read Uncommitted: (Not supported by Postgres - it defaults to Read Committed).
  2. Read Committed: You only see data that was committed before your query started.
  3. Repeatable Read: You see a ”snapshot” of the data from when your transaction started.
  4. Serializable: The strictest. Transactions behave as if they ran one after another.