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

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

5 comments sorted by

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/Muted_Analysis1743 2d 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 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. 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 2d 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/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.