Log Sequence Number (LSN)
In plain English
Plain definition
A strictly increasing number (or position marker) that identifies where a specific record sits in a database's write-ahead log, used to order writes and track replication/recovery progress.
An LSN is a total order token, not a timestamp. It is tied to physical or logical position within a single, append-only log stream produced by one authoring node (usually the primary). Internally it is often represented as a 64-bit value, sometimes split into a segment/file identifier and a byte offset (for example SQL Server’s VLF ID : log block ID : log record ID structure; PostgreSQL instead uses one 64-bit WAL byte position, printed as two hex halves such as 16/B374D848). Because the log is append-only and single-writer, LSN comparison gives a cheap, unambiguous < or > relation without needing clock synchronisation or vector-based causality tracking. It is strictly local ordering, not distributed causal ordering.
Several mechanisms are built on LSNs: crash recovery replays log records from the last checkpoint LSN forward; streaming replication replicas report the LSN they have received, flushed to disk, and applied, letting the primary compute replication lag in bytes rather than wall-clock time; point-in-time recovery (PITR) restores a base backup and replays WAL up to a target LSN; and logical decoding systems such as Debezium and pglogical use LSNs as resumable cursors, storing the last-consumed position so a consumer can restart exactly where it left off after a crash. In synchronous replication configurations, synchronous_commit semantics are frequently expressed as “commit does not return until replica LSN ≥ primary’s commit LSN,” making LSN the currency of durability guarantees.
The edge cases are where it goes wrong. A 64-bit LSN space is effectively inexhaustible (PostgreSQL documents that its WAL segment numbers do not wrap), so the resource operators must monitor is the disk holding retained log, not the LSN range. Log segment recycling and WAL retention policies must account for the slowest consumer’s reported LSN. A stalled logical replication slot holding a low watermark LSN will prevent WAL cleanup indefinitely, filling the disk unnoticed until the primary halts writes. Failover complicates LSN semantics further: a promoted replica starts a new timeline (PostgreSQL explicitly models this with timeline IDs alongside LSNs) because the old primary’s LSN sequence is no longer authoritative once split-brain risk is introduced; naively comparing LSNs across timelines without timeline-awareness produces corrupted recovery decisions.
LSN marks the boundary between single-node total ordering and distributed system design: it is trivial and cheap precisely because it assumes one log author, which is why systems needing multi-writer ordering must layer consensus (Raft terms/indices) or vector clocks on top rather than relying on LSN alone. You need to understand LSN semantics to reason correctly about replication lag alerts, safe WAL retention tuning, replica promotion procedures, and CDC pipeline exactly-once resumption. Getting any of these wrong is one of the most common causes of silent data loss or unbounded storage growth in production database fleets.
Legacy Reference
https://www.postgresql.org/docs/current/wal-internals.htmlAbout this page
Was this useful?
Reports go to the site owner and are used to correct pages. Corrections are listed at /corrections.
Found this useful? Follow KBY for more like it.
Recommended reading
More from KBY that goes with this term.
