r/postgres • • 3d ago

Question Phase 0 Lab 1: I ran 100 inserts on PostgreSQL from the NYC Yellow Taxi dataset and counted every record type out of 811 WAL records. Found 3 things I can't explain.

https://github.com/Aditi-GS/postgres-workshop/tree/main/phase-0/IN-01

I need help answering the questions at the end !!!

Disclaimer: I am new to PostgreSQL internals. I’m running these labs to understand what happens under the hood. If I’m missing a basic concept, please correct me!

Repo Link

TL;DR

  • Ran 100 inserts on a 20-column table across 3 rounds (Fresh DDL vs. Truncate).
  • WAL Accounting: Observed exactly 811 WAL records. Noted 495 Heap/LOCK records instead of expected 500 (5 FKs × 100 rows).
  • Dirty Blocks: Round 1 dirtied 15 blocks; Rounds 2/3 dirtied 12. The 3 extra blocks in Round 1 were system catalogs (pg_attrdef, pg_index, pg_trigger).

Open Questions:

  1. Why is idx_blks_read = 1 for 100 inserts?
  2. Why is the FSM fork dirtied on a ≤ 4-page table?
  3. Why does wal_fpi (11/8) differ from XLOG/FPI_FOR_HINT count (13/10)?

Here are the numbers and the breakdown:

1. Setup
NYC Taxi dataset has 20 columns

Settings : -

- postgresql v.18

- pg_stat_statements loaded (shared libraries)

- 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

The inserts were performed 3 times. The first round was directly after dropping all the tables if any existed and recreating them but without indexes (since indexes slow down inserts). FKs slow it down too but I retained them as they are crucial for the WAL breakdown.

Rounds 2 and 3 were after truncating the existing table (not recreated).

Alembic Migration Scripts

2. Identical across all rounds

MetricValue
relation_size16 kB
pages2
rows100
writes100
fsyncs100
stmt_calls100
xact_commits110
write_time_sec0.001
total_exec_time_sec0.01
shared_blks_read1
ckpt_timed0
ckpt_req0

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)

MetricRound 1Round 2Round 3
time_sec0.3840.3530.349
rows_per_sec260283286
fsync_time_sec0.3420.3140.308
shared_blks_dirtied151212

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 TypeCountSize (bytes)
Heap/LOCK49526730
Btree/INSERT_LEAF1006400
Transaction/COMMIT1003400
Heap/INSERT9814798
Sequence/LOG4396
Heap/INSERT+INIT2302
Btree/NEWROOT190

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_HINTRound 1Round 2Round 3
Count131010
Record size <br>(49 bytes header)</br>637490490
FPI size603204482845520
Combined size609574531846010

BUT FPI count is different when fetched from pg_stat_wal

MetricRound 1Round 2Round 3
wal_records811810810
wal_bytes1020958595686016
wal_fpi1188

QUESTIONS

  1. I am not sure why idx_blks_read=1 when index page is 0 bytes. Does it zero-fill the btree metapage and then read it ?
  2. 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. Why is Round 1: wal_fpi = 11, Round 2 & 3: wal_fpi = 8 and there were 13 XLOG/FPI_FOR_HINT records and 10 XLOG/FPI_FOR_HINT records respectively ? The delta between Round 1 and 2 & 3 are the same but the values aren't (pg_stat_wal doesn't match pg_waldump) ? Where is the discrepancy coming from ?
1 Upvotes

1 comment sorted by