ผลกระทบของระดับการแยกธุรกรรม
ทำความเข้าใจว่าระดับการแยกธุรกรรมต่าง ๆ ส่งผลต่อการทำงานพร้อมกันและความสอดคล้องของข้อมูลอย่างไร
ผลกระทบของระดับการแยกธุรกรรม เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Welcome to Transactions!
Imagine managing money in a bank. When you transfer funds, you don't want the money to disappear or duplicate. This is where transactions come in!
A transaction is a sequence of operations performed as a single logical unit of work. It either completely succeeds (commits) or completely fails (rolls back).
Transactions ensure database reliability through ACID properties:
- Atomicity: All or nothing.
- Consistency: Valid state before and after.
- Isolation: Concurrent transactions don't interfere.
- Durability: Committed changes are permanent.
Concurrency Challenges
When multiple users or applications access the database at the same time, strange things can happen without proper control. These are called concurrency anomalies:
- Dirty Read: Reading uncommitted data from another transaction.
- Non-Repeatable Read: Reading the same row twice in one transaction, but getting different values because another transaction committed a change in between.
- Phantom Read: Rerunning a query and seeing new rows (or missing rows) that another transaction committed.
Isolation levels help prevent these issues.
SQL Isolation Levels
SQL databases define different isolation levels to control how much one transaction is affected by others running concurrently.
These levels are a trade-off: higher isolation means fewer concurrency anomalies but often comes with increased overhead or reduced concurrency, as transactions might wait for each other.
PostgreSQL supports three standard isolation levels: READ COMMITTED, REPEATABLE READ, and SERIALIZABLE. (It also technically has READ UNCOMMITTED, but it behaves like READ COMMITTED).
PostgreSQL's Default: READ COMMITTED
READ COMMITTED is PostgreSQL's default and most commonly used isolation level. It's a good balance between concurrency and consistency for many applications.
With READ COMMITTED:
- You cannot see uncommitted changes from other transactions (prevents Dirty Reads).
- You can see changes committed by other transactions *after* your current statement began.
This means if you run the same SELECT query multiple times within a transaction, you might get different results if another transaction commits changes in between your SELECTs.
READ COMMITTED in Action
Let's visualize READ COMMITTED with two sessions:
- Session A starts a transaction.
- Session A reads
balance = 100. - Session B starts, updates
balanceto90, and commits. - Session A reads
balanceagain. Because Session B committed, Session A now seesbalance = 90.
This behavior is generally acceptable for many applications, as you always see committed data, even if it changes during your transaction.
Experiment with READ COMMITTED
Try running these commands in a PostgreSQL client. Pay attention to the output of the two SELECT statements.
If you were to run UPDATE products SET price = 950 WHERE id = 1; COMMIT; in a separate client session *between* the two SELECT statements below, the second SELECT would show the updated price (950), while the first would show the original (1000).
-- Setup: Create a table and insert data
DROP TABLE IF EXISTS products;
CREATE TABLE products (id INT PRIMARY KEY, name TEXT, price INT);
INSERT INTO products VALUES (1, 'Laptop', 1000);
-- Start a transaction with READ COMMITTED
BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED;
-- First SELECT within the transaction
SELECT 'First SELECT:' AS stage, id, name, price FROM products WHERE id = 1;
-- Second SELECT within the transaction
SELECT 'Second SELECT:' AS stage, id, name, price FROM products WHERE id = 1;
-- End the transaction
COMMIT;
-- Cleanup
DROP TABLE products;Moving Up: REPEATABLE READ
The REPEATABLE READ isolation level offers stronger guarantees than READ COMMITTED. It ensures that any data a transaction reads will remain unchanged for the duration of that transaction.
With REPEATABLE READ:
- You cannot see uncommitted changes (prevents Dirty Reads).
- You cannot see committed changes from other transactions *after* your transaction started (prevents Non-Repeatable Reads).
This means if you run the same SELECT query multiple times, you are guaranteed to get the same result set, even if other transactions commit changes to those rows.
The Strongest: SERIALIZABLE
SERIALIZABLE is the highest isolation level. It guarantees that the outcome of concurrently executing transactions is the same as if they had executed one after another, serially.
With SERIALIZABLE:
- It prevents all concurrency anomalies (Dirty, Non-Repeatable, and Phantom Reads).
- It provides the strongest data consistency.
However, this comes at a cost. Transactions might be forced to wait or even be rolled back (a serialization failure) if they conflict with another transaction, leading to higher overhead and potential retries.
Choosing Your Level
Selecting the right isolation level is crucial:
- READ COMMITTED: Good for most applications where high concurrency is needed and slight data changes within a transaction are acceptable. It's PostgreSQL's default for a reason.
- REPEATABLE READ: Use when you need to ensure that data you've read doesn't change during your transaction, like for complex reports or data analysis where consistency of a snapshot is vital.
- SERIALIZABLE: Reserve for critical operations requiring absolute data consistency, such as financial transactions or inventory systems, where any anomaly is unacceptable. Be prepared to handle serialization failures in your application logic.
Quick Check: Isolation Levels
Which PostgreSQL isolation level prevents Non-Repeatable Reads but still allows Phantom Reads?
Recap: Transaction Isolation
Great job! In this lesson, we explored PostgreSQL's transaction isolation levels.
- We learned about concurrency anomalies like Dirty Reads, Non-Repeatable Reads, and Phantom Reads.
- We understood how READ COMMITTED (the default) prevents dirty reads but allows others.
- We saw that REPEATABLE READ adds protection against non-repeatable reads.
- Finally, SERIALIZABLE offers the strongest guarantee, preventing all anomalies at the cost of potential serialization failures.
Choosing the right isolation level is key to balancing data consistency with application performance and concurrency.
เรียนรู้ SQL ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 22
- บทเรียน
- 88
คำถามที่พบบ่อย
บทเรียน “ผลกระทบของระดับการแยกธุรกรรม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ผลกระทบของระดับการแยกธุรกรรม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ผลกระทบของระดับการแยกธุรกรรม”
ทำความเข้าใจว่าระดับการแยกธุรกรรมต่าง ๆ ส่งผลต่อการทำงานพร้อมกันและความสอดคล้องของข้อมูลอย่างไร คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ผลกระทบของระดับการแยกธุรกรรม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม
ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำความเข้าใจ MVCC และ VACUUM
- การกำหนดค่าและปรับแต่ง Autovacuum
- ผลกระทบของระดับการแยกธุรกรรม
- การป้องกันการวนรอบของรหัสธุรกรรม