"""Read the two padding bytes of every XLogRecord header in a WAL directory.

XLogRecord has a 2-byte hole at offset 18 (between xl_rmid and xl_crc).
For each record listed by pg_waldump, read those bytes straight from the
segment file and count how many records have them non-zero.
"""
import re
import subprocess
import sys
from pathlib import Path

WAL_SEG = 16 * 1024 * 1024
PAGE = 8192
HOLE_OFF, HOLE_LEN, HDR = 18, 2, 24

waldump, waldir = sys.argv[1], Path(sys.argv[2])
segments = sorted(p for p in waldir.iterdir() if re.fullmatch(r"[0-9A-F]{24}", p.name))

total = skipped = nonzero = 0
examples = []
for seg in segments:
    out = subprocess.run([waldump, "-p", str(waldir), seg.name],
                         capture_output=True, text=True).stdout
    data = seg.read_bytes()
    for m in re.finditer(r"lsn: ([0-9A-F]+)/([0-9A-F]+)", out):
        lsn = (int(m.group(1), 16) << 32) | int(m.group(2), 16)
        off = lsn % WAL_SEG
        if off % PAGE + HDR > PAGE:
            skipped += 1
            continue
        total += 1
        pad = data[off + HOLE_OFF: off + HOLE_OFF + HOLE_LEN]
        if pad != b"\0\0":
            nonzero += 1
            if len(examples) < 3:
                examples.append(f"{m.group(1)}/{m.group(2)} -> {pad.hex()}")

print(f"records checked: {total}  (skipped, header across a page boundary: {skipped})")
print(f"records with non-zero padding: {nonzero}")
for e in examples:
    print(f"  e.g. lsn {e}")
