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.
- Setup NYC Taxi dataset has 20 columns
Settings :
postgresql v.18
pg_stat_statementsloadedtrack_io_timing= on,track_wal_io_timing= on,autovacuum= offsynchronous_commit= on,fsync= ondata_checksums= on,wal_log_hints= on,full_page_writes= onCREATE EXTENSION
pg_walinspect(superuser orpg_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:
- Why is
idx_blks_read= 1? For 100 inserts,pg_statio_user_tablesshows 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? - 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?
- Where is the FPI discrepancy coming from?
Why is Round 1
wal_fpi= 11 whilepg_waldumpshows 13 XLOG/FPI_FOR_HINT records? The delta between Round 1 and 2 & 3 is consistent (2 records), but the base values differ.
Top comments (8)
Correct me if I am wrong - for the first question :
BEFORE INSERT:
SELECT pg_stat_reset();
select pg_size_pretty(pg_relation_size('trips_pkey'));
-- 8192
select pg_relation_size('trips_pkey')/8192;
-- 1
SELECT indexrelname, idx_blks_read, idx_blks_hit
FROM pg_statio_user_indexes
WHERE indexrelname = 'trips_pkey';
-- "trips_pkey" 0 0
AFTER INSERT:
"indexrelname" "idx_blks_read" "idx_blks_hit"
"trips_pkey" 0 0
select pg_size_pretty(pg_relation_size('trips_pkey'));
-- 16kB
select pg_relation_size('trips_pkey')/8192;
-- 2
SELECT indexrelname, idx_blks_read, idx_blks_hit
FROM pg_statio_user_indexes
WHERE indexrelname = 'trips_pkey';
-- "trips_pkey" 1 101
So upon table creation an index metapage is created as well and on the first insert it is read into the shared buffers - idx_blks_read = 1. For inserts 2 - 100, it is already present + now tuples are being inserted into the root/leaf page - hence idx_blks_hit = 1 + 100.
It's also why the table had 1 page before insert and 2 after.
That 495 is the detail I would chase first — and your own hypothesis is probably right. With MATCH SIMPLE foreign keys, a NULL in any FK column skips the check entirely, so one NULL location in a row costs you one lock. If you count the rows where any of the five FK columns is NULL and it comes back as exactly 5, that is your answer — and it would also explain why the gap is a clean 5 and not a random number.
The other number that reframes the whole lab is the fsync split: 0.342s of the 0.384s total is fsync time, while the writes themselves took 0.001s. The inserts are nearly free; the durability is the cost. It makes the 811 records feel less surprising — most of them are the price of proving the data survived, not of storing it.
For the FPI gap, the fact that the delta is exactly 2 in every round is the telling part. Noise would not be that consistent, so something systematic is being counted by one tool and not the other. Which of your three questions are you taking into Lab 2 first — and did you already check how many of those 100 rows have a NULL in a foreign-key column?
I actually ran a check for NULLs in the FK columns, and there are none, so the 'duplicate location' (pu_location_id == do_location_id) hypothesis might be holding up.
But the fsync insight is spot on - durability is definitely the bottleneck, not the writes. I’m moving on to WAL Life-cycle next. Any thoughts on the systematic 2-record difference in the FPI counts?
That NULL check settles it for me, Aditi — with no NULLs in the FK columns, the duplicate-location read of 495 is now the strong one. Five FKs times 100 rows is 500, and 495 is exactly 5 short, so if exactly 5 of your 100 rows have pu_location_id == do_location_id, each of those rows locks the same taxi_zones row once instead of twice. One query — count the rows where the two are equal — would close that question for good.
On the FPI gap, the fixed delta of 2 across all three rounds is still what makes it interesting. Two checks I would run before trusting either number: first, pull the LSNs of the two extra FPI_FOR_HINT records pg_waldump sees and look at which fork each one is on — you already found FSM block 2 dirtied with FPW, so if one of the two carries its FPI on the FSM fork, that is a candidate for something pg_stat_wal counts differently. Second, check whether those two sit at the edge of the window your pg_stat_wal snapshot covers, since a boundary record counted by one tool and not the other would show up as exactly this kind of small, constant delta.
For the WAL life-cycle lab, which of those two would you run first — and are you planning to log the per-record LSNs alongside the pg_stat_wal snapshot so the two can be matched one to one?
Thank you - I hadn't considered the two checks you suggested (LSN matching and window boundaries) - I’m going to pull those LSNs from the pg_waldump output and see if one of them lands on the FSM fork.
To view the full lab code, observations and inferences on GitHub :
github.com/Aditi-GS/postgres-workshop
My own inserts never showed me 811 WAL records for 100 inserts, and that number made a single write feel far heavier to me.
That’s a fair reaction! It does feel heavy when you see that number.
The key is that those 811 records aren’t just storing the data; they are proving the data survived.
~60% of those records are Foreign Key lock checks. It’s the cost of relational integrity. If you removed the FKs, that number would drop significantly.
I’m curious if your inserts used Foreign Keys or Indexes? Those are the two biggest drivers of WAL volume.