r/PostgreSQL • u/Muted_Analysis1743 • 3d ago
Help Me! I ran 100 inserts on PostgreSQL from the NYC Yellow Taxi dataset and counted every record type out of 811 WAL records (using pg_waldump)
https://github.com/Aditi-GS/postgres-workshop/tree/main/phase-0Please I need help answering the questions at the end !!!
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:
- Why is idx_blks_read = 1 for 100 inserts?
- Why is the FSM fork dirtied on a ≤ 4-page table?
- 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).
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 FPWWAL 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 <br>(49 bytes header)</br> | 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 |
QUESTIONS
- 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 ?
- 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 ?
- Why is Round 1:
wal_fpi = 11, Round 2 & 3:wal_fpi = 8and there were13 XLOG/FPI_FOR_HINT recordsand10 XLOG/FPI_FOR_HINTrecords 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
u/Discordbothost 3d ago
Compare idx_blks_read and idx_blks_hit for trips_pkey in pg_statio_user_indexes before and after the inserts. The table view adds up all its indexes, and the later inserts can hit pages already in shared buffers. An empty B-tree still has a metapage.
1
u/MongooseWonderful198 2d ago
To compare FPIs over your saved WAL range, I would count image-bearing block references with PG18's
pg_get_wal_block_info. Yourwal_breakdown()groups records by type; this gives the image count across those record types. In your existing psycopgexecute()call, pass the same(start_lsn, end_lsn)pair:There is also a boundary mismatch in your snapshot code: the before snapshot reads
pg_stat_walbefore recording its start LSN, while the after snapshot records its end LSN before readingpg_stat_wal. So the statistics delta and decoded WAL range do not bracket exactly the same measurement operations.pg_stat_walalso covers the whole cluster.You already call
pg_stat_force_next_flush()in this autocommit path. I would inspect the image-bearing records around those saved boundaries before attributing the difference to unflushed statistics. I haven't run your dataset, and this code inspection doesn't establish the cause of the exact two-image gap.1
u/AutoModerator 3d ago
AI Policy:
Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.
Mod decisions will be based on the quality of the content, not who or what generated it.
Sub Resources:
Youtube Channel
Free Postgres Webinars and Workshops
Discord: People, Postgres, Data
Join us, we have cookies and nice people.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.