0Pricing
PostgreSQL Performance & Query Optimization · บทเรียน

ทำความเข้าใจ MVCC และ VACUUM

สำรวจการควบคุมภาวะพร้อมกันหลายเวอร์ชัน (MVCC) และบทบาทสำคัญของ VACUUM ในการป้องกันตารางบวม

ทำความเข้าใจ MVCC และ VACUUM เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Meet MVCC: Concurrency's Friend

Welcome to understanding PostgreSQL's core! Today, we dive into Multi-Version Concurrency Control (MVCC). It's a fancy term for a simple, powerful idea.

MVCC is how PostgreSQL allows many users or applications to access and modify data at the same time without interfering with each other. Think of it as a traffic controller for your database.

Why MVCC Matters for Speed

Imagine a database without MVCC. If one user is reading a row, another user trying to update that same row would have to wait. This is called locking, and too much of it can make your database painfully slow.

MVCC solves this by ensuring that readers don't block writers, and writers don't block readers. Everyone gets their own consistent view of the data.

Rows Have Many Lives

The magic of MVCC lies in how it handles changes. When you UPDATE or DELETE a row in PostgreSQL, the database doesn't immediately overwrite or remove the original data.

Instead, it creates a new version of the row (for updates) or simply marks the existing row as 'deleted' without physically removing it. The old version remains, temporarily.

Seeing the Right Data

How does PostgreSQL know which version of a row to show you? Each transaction gets a unique ID. When a row is created, it gets an xmin (creation transaction ID). When it's 'deleted', it gets an xmax (deletion transaction ID).

  • Your transaction only sees rows committed *before* it started.
  • It ignores rows deleted *after* it started.

This ensures you always see a consistent snapshot of the data.

The Aftermath of an UPDATE

Let's see a simple example of how an UPDATE creates new row versions:

First, we create a table and insert a product:

CREATE TABLE products (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100),
  price DECIMAL(10, 2)
);

INSERT INTO products (name, price) VALUES ('Laptop', 1200.00);

Updates Create Dead Tuples

Now, when we update the price, PostgreSQL doesn't change the existing row. Instead, it marks the old row version as 'dead' and inserts a brand new row version with the updated price.

The old version is now a 'dead tuple' – it's no longer visible to new transactions but still occupies disk space.

UPDATE products SET price = 1250.00 WHERE id = 1;

The Hidden Mess: Table Bloat

Over time, with many UPDATEs and DELETEs, tables can accumulate a lot of these 'dead tuples'. This leads to table bloat.

Table bloat means your database files are larger than they need to be, consuming more disk space and potentially slowing down queries because more data needs to be read from disk.

Enter VACUUM!

This is where the VACUUM command comes in! Its primary job is to clean up these dead tuples. It's like a janitor for your database, tidying up the old, unused versions of data.

VACUUM marks the space occupied by dead tuples as reusable, making it available for new data to be inserted into the table. This prevents continuous table growth and improves performance.

How VACUUM Cleans Up

When you run VACUUM, PostgreSQL scans the table, identifies dead tuples, and adds their locations to a 'free space map'. This doesn't immediately shrink the table file on disk, but it ensures that future INSERTs or UPDATEs can reuse that space.

Here's how you'd run a basic VACUUM:

-- Clean up the 'products' table
VACUUM products;

MVCC & VACUUM Check

Let's test your understanding of MVCC and VACUUM's roles.

MVCC & VACUUM: Key Takeaways

You've just learned about two critical PostgreSQL concepts!

  • MVCC enables high concurrency by allowing multiple versions of data.
  • UPDATEs and DELETEs create dead tuples.
  • Table bloat occurs when these dead tuples accumulate, wasting space.
  • The VACUUM command cleans up dead tuples, making their space reusable and preventing bloat.

Understanding these is key to maintaining a healthy and performant PostgreSQL database!

คำถามที่พบบ่อย

บทเรียน “ทำความเข้าใจ MVCC และ VACUUM” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ทำความเข้าใจ MVCC และ VACUUM” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ทำความเข้าใจ MVCC และ VACUUM”

สำรวจการควบคุมภาวะพร้อมกันหลายเวอร์ชัน (MVCC) และบทบาทสำคัญของ VACUUM ในการป้องกันตารางบวม คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “ทำความเข้าใจ MVCC และ VACUUM” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม

ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ทำความเข้าใจ MVCC และ VACUUM
  2. การกำหนดค่าและปรับแต่ง Autovacuum
  3. ผลกระทบของระดับการแยกธุรกรรม
  4. การป้องกันการวนรอบของรหัสธุรกรรม
← กลับไปที่ PostgreSQL Performance & Query Optimization