r/PostgreSQL • • 12d 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-0

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

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

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 <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

  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 ?
4 Upvotes

10 comments sorted by

1

u/Discordbothost 12d 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/Muted_Analysis1743 11d ago
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


Correct me if I understood it wrong, 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.

1

u/MongooseWonderful198 12d ago

To compare FPIs over your saved WAL range, I would count image-bearing block references with PG18's pg_get_wal_block_info. Your wal_breakdown() groups records by type; this gives the image count across those record types. In your existing psycopg execute() call, pass the same (start_lsn, end_lsn) pair:

SELECT count(*) AS fpi_blocks
FROM pg_get_wal_block_info(%s, %s, false)
WHERE block_fpi_length > 0;

There is also a boundary mismatch in your snapshot code: the before snapshot reads pg_stat_wal before recording its start LSN, while the after snapshot records its end LSN before reading pg_stat_wal. So the statistics delta and decoded WAL range do not bracket exactly the same measurement operations. pg_stat_wal also 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/Muted_Analysis1743 11d ago

I changed the order in which LSN is captured for before and after snapshots (both now run before any of the stats queries do), but the results didn't change:

pg_stat_wal.wal_fpi = 8

XLOG/FPI_FOR_HINT count = 10

fpi_blocks (pg_get_wal_block_info WHERE block_fpi_length > 0) = 10

The count from pg_waldump and pg_get_wal_block_info agree on 10 but wal_fpi is still lower (8).

Now I am confused - I'm still not sure what this discrepancy points to ?

1

u/MongooseWonderful198 6d ago

With both decoders agreeing on 10 image-bearing block references, I would stop treating wal_fpi as the same count. It counts full-page images recorded in WAL statistics; your decoder query counts block references whose block_fpi_length is nonzero inside the selected LSN range. One WAL record can carry more than one block reference.

Group the decoder output by record LSN and compare count(distinct record_lsn) with 10. If that drops to 8, the two extra values are additional FPI-bearing blocks inside records already counted once by wal_fpi. If it stays at 10, then the next useful artifact is the ten record LSNs and block IDs, because the discrepancy is elsewhere. I would not call it a flush problem now that two independent decoders agree.

1

u/Muted_Analysis1743 3d ago edited 3d ago

All XLOG records are of FPI_FOR_HINT type.

I thought FPI_FOR_HINT can only contain a single block reference.

rmgr: XLOG        len (rec/tot):     49/  4681, tx:          0, lsn: 0/82E69058, prev 0/82E68FE0, desc: FPI_FOR_HINT , blkref #0: rel 1663/16388/1259 blk 15 FPW

rmgr: XLOG        len (rec/tot):     49/  8061, tx:          0, lsn: 0/82E6A2C0, prev 0/82E69058, desc: FPI_FOR_HINT , blkref #0: rel 1663/16388/2663 blk 2 FPW

rmgr: XLOG        len (rec/tot):     49/  5509, tx:          0, lsn: 0/82E6C258, prev 0/82E6A2C0, desc: FPI_FOR_HINT , blkref #0: rel 1663/16388/2662 blk 2 FPW

rmgr: XLOG        len (rec/tot):     49/   281, tx:    1554713, lsn: 0/82E6D980, prev 0/82E6D940, desc: FPI_FOR_HINT , blkref #0: rel 1663/16388/32950 blk 0 FPW

rmgr: XLOG        len (rec/tot):     49/   389, tx:    1554713, lsn: 0/82E6DAD8, prev 0/82E6DAA0, desc: FPI_FOR_HINT , blkref #0: rel 1663/16388/32961 blk 0 FPW

rmgr: XLOG        len (rec/tot):     49/   381, tx:    1554713, lsn: 0/82E6DC98, prev 0/82E6DC60, desc: FPI_FOR_HINT , blkref #0: rel 1663/16388/32971 blk 0 FPW

rmgr: XLOG        len (rec/tot):     49/  8181, tx:    1554713, lsn: 0/82E6DE50, prev 0/82E6DE18, desc: FPI_FOR_HINT , blkref #0: rel 1663/16388/33006 blk 1 FPW

rmgr: XLOG        len (rec/tot):     49/  2629, tx:    1554713, lsn: 0/82E6FE98, prev 0/82E6FE60, desc: FPI_FOR_HINT , blkref #0: rel 1663/16388/33006 blk 2 FPW

rmgr: XLOG        len (rec/tot):     49/  8229, tx:    1554718, lsn: 0/82E71300, prev 0/82E712C8, desc: FPI_FOR_HINT , blkref #0: rel 1663/16388/33006 blk 0 FPW

rmgr: XLOG        len (rec/tot):     49/  8241, tx:    1554778, lsn: 0/82E7AF80, prev 0/82E7AF58, desc: FPI_FOR_HINT , blkref #0: rel 1663/16388/33247 fork fsm blk 2 FPW

Start LSN = 0/82E69058 End LSN = 0/82E819E0

=> all records sit in the same segment = 000000010000000000000082

All the record LSNs are different (belong to diff relfilenode + blocknum)

Does the pg_stat_wal account for FSM fork FPIs ? If not, then 1 count out of 2 discrepancy is explained ?

What about 1 more ?

1

u/MongooseWonderful198 3d ago

FPI_FOR_HINT has one block reference in each record you pasted. The FSM record should still count in pg_stat_wal.wal_fpi; that counter is not limited to the main fork.

The harder mismatch is scope. wal_fpi is cluster-wide, while your pg_waldump range is bounded by two LSNs. Reset the shared WAL stats on an idle instance, capture the start LSN, run the inserts, capture the end LSN, then dump that exact range. Also pause autovacuum for the test. If the extra two disappear, another backend or maintenance process wrote them outside your workload rather than one record holding multiple blocks.

1

u/AutoModerator 12d 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.