PostgreSQL 18 Internals - Phase 0 Lab 1: Counting 811 WAL Records in 100 Inserts

2026-12-01来源 dev.toEnglish

As part of a deep-dive into PostgreSQL 18 internals, I’m running a series of labs to understand what happens under the hood when we write data.

In this first lab, I loaded a subset of the NYC Yellow Taxi dataset (100 rows, 20 columns) and analysed the Write-Ahead Log (WAL) to understand the cost of inserts, foreign keys, and index maintenance.

The Experiment

I used Python & Alembic Migration Scripts to insert 100 rows into PG 18. I ran the insert in 3 rounds:

Round 1: Fresh DDL (tables created from scratch).
Round 2 & 3: Truncate (tables reused).
I used pg_waldump to count every record type in the resulting WAL segments.

  1. Setup NYC Taxi dataset has 20 columns

Settings :

  • postgresql v.18

  • pg_stat_statements loaded

  • track_io_timing = on, track_wal_io_timing = on, autovacuum = off

  • synchronous_commit = on, fsync = on

  • data_checksums = on, wal_log_hints = on, full_page_writes = on

  • CREATE EXTENSION pg_walinspect (superuser or pg_read_server_files)

  • wal_buffers = 4 MB, checkpoint_timeout = 5 min, checkpoint_completion_target = 0.9

2. Identical across all rounds

Metric Value
relation_size 16 kB
pages 2
rows 100
writes 100
fsyncs 100
stmt_calls 100
xact_commits 110
write_time_sec 0.001
total_exec_time_sec 0.01
shared_blks_read 1
ckpt_timed 0
ckpt_req 0

For 100 inserts, xact_commits is 110 - this is because I ran 10 SELECT statements as a part of capturing snapshots of metrics. When I checked heap_blks_read, idx_blks_read from pg_statio_user_tables for shared_blks_read=1, the answer ended up being the trips_pkey.

Different for each round (Non-WAL metrics)

Metric Round 1 Round 2 Round 3
time_sec 0.384 0.353 0.349
rows_per_sec 260 283 286
fsync_time_sec 0.342 0.314 0.308
shared_blks_dirtied 15 12 12

The 3 more shared_blks_dirtied turned out to be pg_attrdef, pg_index, pg_trigger. This is because the first round was triggered right after creating the tables.

The different WAL record types identical across all rounds: (from pg_waldump)

Record Type Count Size (bytes)
Heap/LOCK 495 26730
Btree/INSERT_LEAF 100 6400
Transaction/COMMIT 100 3400
Heap/INSERT 98 14798
Sequence/LOG 4 396
Heap/INSERT+INIT 2 302
Btree/NEWROOT 1 90

There 4 lookup tables and 5 FKs:

  • vendor_id (vendor_lookup)
  • ratecode_id (ratecode_lookup)
  • payment_type (payment_type_lookup)
  • pu_location_id (taxi_zones_lookup)
  • do_location_id (taxi_zones_lookup)

So for Heap/LOCK count, it should rows x FKs i.e., 500 but I got 495. Upon checking if there are location IDs which both location FK shared in common (pu_location_id == do_location_id) for any of the rows, 5 tuples were identified matching the criteria.

FSM block 2 also got dirtied

rmgr: XLOG len (rec/tot): 49/ 8241, tx: 740341, lsn: 0/498FF980, prev 0/498FF958, desc: FPI_FOR_HINT , blkref #0: rel 1663/16388/25339 fork fsm blk 2 FPW 

WAL metrics different for each round are:

(from pg_waldump)

XLOG/FPI_FOR_HINT Round 1 Round 2 Round 3
Count 13 10 10
Record size
(49 bytes header)
637 490 490
FPI size 60320 44828 45520
Combined size 60957 45318 46010

BUT FPI count is different when fetched from pg_stat_wal

Metric Round 1 Round 2 Round 3
wal_records 811 810 810
wal_bytes 102095 85956 86016
wal_fpi 11 8 8

3 Questions for the Community

I am new to PostgreSQL internals and would appreciate help explaining these three observations:

  1. Why is idx_blks_read = 1? For 100 inserts, pg_statio_user_tables shows 1 index block read. For insert only operation, why read any index blocks? Does it zero-fill the B-Tree metapage and then read it?
  2. Why is the FSM dirtied on a small table? If the FSM isn't used for allocation on small tables (≤ 4 pages), why does it get dirtied at all? The FSM fork exists, but I thought it would be untouched. What exactly writes to FSM block 2 of a 2-page main-fork table?
  3. Where is the FPI discrepancy coming from? Why is Round 1 wal_fpi = 11 while pg_waldump shows 13 XLOG/FPI_FOR_HINT records? The delta between Round 1 and 2 & 3 is consistent (2 records), but the base values differ.

原文:https://www.a8q9.cn/en/article/postgresql-18-internals-phase-0-lab-1-counting-811-wal-records-in-100-inserts-95dae4.html

相关文章