1.4 When a Record Is Updated

There are two main ways to change the value of a record stored in a page. One is to overwrite the bytes where the record sits directly with the new value. The other is to leave the original record in place, mark it as "deleted", and store a new record holding the new value in the page's free space. PostgreSQL uses the second method by default, and MS SQL Server uses the first.

As an example, take the following query that changes the phone number of customer 1002, Kim Cheol-su, and compare what the two databases do inside the page.

UPDATE customer SET phone = '010-9999-0000' WHERE id = 1002;

PostgreSQL: Always Inserts a New Row

PostgreSQL does not overwrite the existing row. Instead, it adds a new row (a new version) holding the changed value to the page's free space and creates a new slot pointing to its location. The original row is not erased; only a mark in the row header records that "this transaction deleted this row". The old version's row is also linked to the new version's row, so a lookup that arrives through the old row can still find the new one.

Why go to this trouble? Because someone else may be reading the same row while the value is being changed. With the old row left in place, a query that started before the change was committed can see the old value, and a query that started after it can see the new value. This principle is covered in detail under MVCC in Chapter 7.

This method is costly. A record marked as deleted keeps occupying page space even after nobody can see it anymore. Such a row is called a dead tuple. VACUUM, which runs periodically, finds them and reclaims the space. If updates are too frequent for VACUUM to keep up, the table grows far larger than the actual data. This is called bloat, meaning the pages keep swelling.

SQL Server: Modifies in Place

If the new value fits in the original spot, SQL Server changes the bytes of the row that slot 2 points to directly, in place. The number of rows in the page and the slot numbers stay the same. The old value, 010-1111-2222, is recorded in the transaction log instead. If the change must be cancelled, the log is used to restore the original value.

What if someone else needs to read the old value at the same time? With default settings, SQL Server makes the reader wait until the change finishes. With snapshot isolation (such as RCSI) turned on, the old row is copied to the version store just before the overwrite, and readers see that copy. The old version is kept outside the data page, so dead rows do not accumulate in the original page. Azure SQL Database has this snapshot mode on by default.

Figure 1-4. How UPDATE is handled inside a page: PostgreSQL and SQL Server

block-beta
  columns 1
  block:pg
    columns 4
    pgt["PostgreSQL"]:4
    pb["before UPDATE"]:4
    p1["slot 1"] p2["row 1"]:3
    p3["slot 2"] p4["row 2 · 010-1111-2222"]:3
    pa["after UPDATE"]:4
    p5["slot 1"] p6["row 1"]:3
    p7["slot 2"] p8["row 2 · 010-1111-2222 (deleted mark)"]:3
    p9["slot 3"] p10["row 3 · 010-9999-0000"]:3
    pn["rows in page: 2 -> 3"]:4
  end
  block:ms
    columns 4
    mst["SQL Server"]:4
    mb["before UPDATE"]:4
    m1["slot 1"] m2["row 1"]:3
    m3["slot 2"] m4["row 2 · 010-1111-2222"]:3
    ma["after UPDATE"]:4
    m5["slot 1"] m6["row 1"]:3
    m7["slot 2"] m8["row 2 · 010-9999-0000 (overwritten in place)"]:3
    ml["transaction log"] mo["old value 010-1111-2222"]:3
    mn["rows in page: 2 -> 2"]:4
  end
  p8 -- "link to new row" --> p10
  class pgt,mst meta
  class pb,pa,mb,ma,pn,mn plain
  class p1,p3,p5,p7,p9,m1,m3,m5,m7 slot
  class p2,p4,p6,m2,m4,m6 data
  class p8 muted
  class p10,m8 hot
  class ml,mo meta

The figure places the page before (before UPDATE) and after (after UPDATE) running UPDATE customer SET phone = '010-9999-0000' WHERE id = 1002; side by side, PostgreSQL on the left and SQL Server on the right. Both pages start with two slots, and slot 2 (slot 2) points to Kim Cheol-su's row (row 2, phone number 010-1111-2222).

  • PostgreSQL (left). After the UPDATE, slot 2 and the old row remain, and the old row only gets a deleted mark (deleted mark). A new row (row 3) holding the changed value 010-9999-0000 is added to the page's free space, and a new slot (slot 3) points to it. The arrow from the old row to the new row (link to new row) is the link that lets a lookup arriving through the old row find the new one. As a result, the number of rows in the page grew from 2 to 3 (rows in page: 2 -> 3).
  • SQL Server (right). The bytes of the row that slot 2 points to are changed to 010-9999-0000 in place (overwritten in place). Neither the slot numbers nor the row count change (rows in page: 2 -> 2). The old value 010-1111-2222 is recorded not in the page but in the transaction log (transaction log), and is used to restore the original value when the change is cancelled.

When the Row Grows and No Longer Fits in the Original Page

If the new value is longer than the old one and the page lacks free space, both databases must use another page.

  • PostgreSQL puts the new row in a different page. In this case, the indexes that pointed to the row must also get the new location, which raises the cost. Conversely, if the new row fits in the same page and no indexed column changed, the update is handled as a HOT (Heap Only Tuple) update that leaves the indexes untouched, which is much lighter.
  • SQL Server depends on the table structure. For a heap table, the row is moved to another page and a forwarding pointer saying "the new address is here" is left at the original spot. For a clustered index table, rows must be placed in key order, so a page split occurs that divides the page in two (Chapter 3).

For this reason, both databases offer a setting that leaves part of each page empty instead of filling it completely from the start. It is fillfactor in PostgreSQL and FILL FACTOR in SQL Server, and the more frequently a table is updated, the more free space you leave, which reduces moves to other pages.

ItemPostgreSQLSQL Server / Azure SQL
Default methodAdds a new row and marks the old row as deletedOverwrites the value in the same place
Where the old value livesIn the same table page (dead tuple)Transaction log; version store under snapshot isolation
Number of rows in the pageIncreasesUnchanged
CleanupVACUUM reclaims dead rowsThe version store is cleaned up separately
When the page runs out of spaceNew row in another page, indexes updated tooForwarding pointer for heaps, page split for clustered indexes
Free space settingfillfactorFILL FACTOR

When operating PostgreSQL, monitor the VACUUM status and bloat of frequently updated tables. In SQL Server, monitor forwarding pointers, page splits, and the size of the version store when snapshot isolation is on.