Follow a row version
An UPDATE preserves the primary key and creates a new stored row version. Follow that difference before explaining vacuum.
You will learn to distinguish logical identity from tuple location, connect updates to visibility, and explain when PostgreSQL can skip writing new entries in ordinary indexes. Prerequisite: transactions and indexes.
The page loads a fresh order dataset automatically. Select Run SQL in each numbered editor in order. The editors share one database, so later experiments use changes from earlier ones.
Sections on this page
Separate identity from location
PostgreSQL's multiversion concurrency control, or MVCC, lets transactions see appropriate row
versions. The ctid system column locates a tuple version by page and item position. It is not a
stable application identifier.
xmin identifies the inserting transaction of a tuple version. xmax participates in deletion,
update, and locking information and needs interpretation with tuple status flags; treating every
nonzero value as a committed delete is wrong. Neither is a permanent audit field.
Observe the update
Predict whether the primary key changes, whether the tuple location changes, and whether a normal SELECT returns both old and new versions.
Compare id, ctid, and balance in SQL 01 and SQL 02. The key remains the same while the
visible version's location changes. An ordinary query applies
transaction visibility rules; it is not a listing of all physical versions. The old version may still
occupy storage until no relevant snapshot needs it. A later update, vacuum, or rewrite can move or
remove tuples, so save the primary key rather than ctid in application references.
Give HOT an opportunity
A heap-only tuple update, or HOT update, can avoid new entries in ordinary indexes when relevant indexed values do not change and the new tuple fits on the same page. Fillfactor leaves room on pages during insertion; it does not guarantee every future update will qualify.
The first update changes a non-indexed value. The later balance update changes an indexed value, so it cannot use the same ordinary HOT opportunity. Statistics counters are asynchronous estimates and may not update immediately in this embedded runtime; a zero counter is not proof no update ran. Use the resulting values and documented HOT conditions here. A server offers richer cumulative statistics, page inspection, and independent sessions for deeper verification.
Clean up without deleting logical rows
Predict whether vacuum changes the visible row count or reverses the balance update.
Vacuum can reclaim obsolete versions when visibility permits. It does not undo committed business updates. Page space can become reusable without shrinking the relation file.
Explain the evidence
Which column identifies the account after both updates? What do the changed ctid and the
final visible_rows tell you about physical versions and logical rows?
A hint for explaining the results
A primary key identifies the logical entity. A tuple address identifies a particular stored version. Normal SELECT output is filtered by visibility, not by whether bytes still exist.
Worked explanation
The update preserved ID 1 but produced a new visible tuple location and value. Only one version per logical row is visible to the ordinary statements used here. HOT depends on indexed attributes and same-page space; it is an optimization within the versioning system. Vacuum removes eligible old versions and leaves the current logical rows intact. These observations do not reproduce two concurrent snapshots or measure production bloat.
Transfer the idea
After the original experiments, replace SQL 03 with the following and select Run SQL. The two queries read the same row before and after a change to the newly indexed value.
CREATE INDEX version_note ON version_demo (note);
SELECT id, note, ctid FROM version_demo WHERE id = 1;
UPDATE version_demo SET note = 'changed with a note index' WHERE id = 1;
SELECT id, note, ctid FROM version_demo WHERE id = 1;
Compare id, note, and ctid. What HOT condition did the new index and value change remove?
The change in ctid identifies a new tuple version. The asynchronous statistics in the original
example are optional evidence and have no expected counter value.
Next: reclaim space with vacuum.
Primary sources: System columns, MVCC, and HOT.