Sub topic 5
What INSERT, UPDATE and DELETE do to row versions
Assume you know basic SQL and transactions, including BEGIN, COMMIT, and ROLLBACK. You should also be able to open two psql sessions against the same PostgreSQL database.
PostgreSQL stores table rows as heap tuples. A tuple is one physical row version. When a transaction changes a row, PostgreSQL normally creates or marks tuple versions instead of overwriting the existing version immediately. This lets a query that started earlier continue reading the version allowed by its snapshot.
INSERT creates a new version
An INSERT creates a tuple whose xmin records the transaction ID (XID) that inserted it. Its xmax is normally zero, meaning that no transaction has deleted or replaced it.
DROP TABLE IF EXISTS accounts;
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance integer NOT NULL
);
BEGIN;
INSERT INTO accounts VALUES (1, 100);
SELECT xmin, xmax, ctid, id, balance
FROM accounts
WHERE id = 1;
COMMIT;The ctid identifies the tuple's current physical location as a page number and item number, such as (0,1). It is useful for investigation, but it is not a permanent row identifier. An update can give the replacement version a different ctid.
Until the inserting transaction commits, other transactions generally cannot see this new version. If the transaction rolls back, the tuple remains as storage for a while, but visibility checks treat it as aborted.
UPDATE replaces a version
An UPDATE creates a new tuple version and marks the old version as replaced by the updating transaction. Conceptually, PostgreSQL changes the old tuple's xmax and stores the new tuple with the updating transaction's xmin.
The old tuple also points toward the new tuple through its tuple chain. PostgreSQL follows this chain when it needs to determine which version is visible.
BEGIN;
SELECT xmin, xmax, ctid, id, balance
FROM accounts
WHERE id = 1;
UPDATE accounts
SET balance = balance - 10
WHERE id = 1;
SELECT xmin, xmax, ctid, id, balance
FROM accounts
WHERE id = 1;
COMMIT;The new version may have a different ctid. PostgreSQL can sometimes perform a HOT update, which avoids changing indexes when indexed columns remain unchanged and page space permits. The logical result is still a new row version.
DELETE marks a version as removed
A DELETE does not immediately erase the tuple. It records the deleting transaction's XID in the old tuple's xmax. A visibility check then hides that version from snapshots for which the delete has committed.
BEGIN;
DELETE FROM accounts WHERE id = 1;
SELECT xmin, xmax, ctid, id, balance
FROM accounts
WHERE id = 1;
COMMIT;After commit, a new query does not see the deleted row. The old tuple's space becomes reusable only after PostgreSQL determines that no still-running transaction could need the old version. VACUUM performs that cleanup.
Exercise
After a committed UPDATE, why can PostgreSQL still need the old tuple?
Show answer
A transaction with an older snapshot may still be allowed to see the old version. VACUUM can reclaim it only after no such snapshot remains.