One commit's journey from XLogInsert to the ack, through the pipeliner's
submit/reap split, the shared-memory rings, the pfsd event loop, and io_uring. Strictly ordered
(Stage A): one device I/O in flight, completions arrive in submission order, and only a datasync
completion proves durability.
Backend XLogInsert writes the record into a WAL buffer page — physical pages of the one registered zero-copy memfd segment.
On commit the backend bumps LogwrtRqst, wakes the advance and write workers, and sleeps on its per-LSN flush-event slot.
The advance worker folds the recent-written array into ready_write_lsn, the writer's contiguity gate.
The write worker runs XLogWrite with its local LogwrtResult as the submitted watermark: full pages translate pointer→(buf_id, off); the live tail page is snapshotted (8k memcpy) into a bounce slot.
Each op becomes a 64-byte WalSqe pushed to the submit ring, with a descriptor (cookie → end_lsn, kind, fd) kept locally; the doorbell eventfd rings per submit.
The daemon's per-connection event loop wakes, pops the sqe, takes mount/inode refs, resolves the block map to (raw device fd, bda), and the buffer table to the data pointer.
The op goes onto the private io_uring via walring — plain SQE, slot-tracked, generation-tagged. Stage A keeps at most one in flight; a datasync launches only after the previous completion was reaped.
The kernel DMAs the bytes straight out of the shared memfd pages to the device at bda; a datasync is a device-wide FDATASYNC on the raw fd, covering every segment.
The CQE returns through walring (cookie/generation verified), becomes a 16-byte WalCqe in the completion ring, and ticks the completion eventfd.
The reaper thread reaps strictly in order — the cookie must equal the descriptor head, or PANIC.
Write completions advance logWriteResult; datasync completions advance logFlushResult (with the Write→Flush barrier), run segment-close actions, and wake walsenders + notify workers. The reaper is the only publisher of both results in mode 6.
A notify worker signals every flush-event slot below logFlushResult; the backend wakes with its commit durably acked.
Submitted ≠ completed. The pipeliner's local LogwrtResult tracks what was handed to the transport; the shared logWriteResult/logFlushResult track what completed. Nothing outside the reaper moves the shared results.
Only datasync proves durability. A pwrite completion means the device accepted the write; acks, walsender advertisement, and the flush watermark all key off datasync completions, which cover every earlier op by strict ordering.
Buffer stability. WAL buffer pages aren't recycled until logWriteResult passes them — which now means write completed, so zero-copy reads never race recycling. The mutable tail page is snapshotted, with stock-pwrite semantics for racy bytes past the request end.
Flow control closes the loop. Outstanding ops are bounded by ring capacity on the client side, which is exactly what makes the daemon's completion push infallible and the descriptor ring 1:1 with bounce slots.