Definitioncore
Log sequence number
Also called LSN, pageLSN.
A monotone address of a log record; each page stores the LSN of its last update so recovery knows which updates it already holds.
A log sequence number (LSN) is a number that names a record of the write-ahead log and grows with the record's position, so that comparing two LSNs tells which record was written first. Its main use is the pageLSN: each page carries the LSN of the last logged change applied to it, and recovery compares that number with the LSN of a record to decide whether the page already holds the change. For a reader who knows algorithms, an LSN is a version stamp drawn from one global counter, the end of the log; unlike a Lamport clock it needs no message exchange, because a single log orders all records. The ARIES and fuzzy checkpoint pages use the LSN fields defined here; this page defines them and the tests built on them.
Definition
LSN. Let the log be the sequence of records in the order they were appended. An LSN assignment is a function with whenever , and every LSN is positive. On this site , so the first record has LSN 1. Real systems often use the byte offset of the record in an ever-growing log address space: ARIES defines the LSN as the address of the first byte of the record, a monotonically increasing value, and PostgreSQL stores an LSN as a 64-bit byte position in the WAL stream, printed as two hexadecimal numbers such as 16/B374D848. Gaps between LSNs carry no meaning; only the order does.
Fields that hold LSNs. The recovery literature names several:
| name | where | value |
|---|---|---|
| pageLSN | in each page | the LSN of the last update or CLR applied to the page; 0 if the page was never updated |
| flushedLSN | in memory, log manager | the largest LSN of a record on stable storage |
| prevLSN | in each log record | the LSN of the previous record of the same transaction; 0 for its first record |
| lastLSN | transaction table | the LSN of the newest record of the transaction |
| recLSN | dirty page table | the LSN of the first record that changed the page since it was last written |
| undoNextLSN | in each CLR | the prevLSN of the update that the CLR undid: the next record to undo |
| master record | fixed place on disk | the LSN of the last BEGIN_CHECKPOINT record whose end record is stable |
ARIES sets the recLSN, when a clean page is first changed, to the current end-of-log LSN, which is at most the LSN of the change; any value at most that LSN and larger than the last write of the page is correct.
Page invariant. If changes to a page are applied in LSN order and each one sets the pageLSN to its own LSN, then a page version with pageLSN holds exactly the effects of the logged changes on that page with LSN at most . This invariant is what makes the three tests below sound.
Tests.
- Redo test. During redo, apply record to its page if and only if , and then set . Because of the invariant, redo applies each missing change exactly once, even if the change is not idempotent.
- WAL test. The buffer manager may write page to disk only if ; otherwise it first flushes the log up to .
- Commit test. A commit is reported only when the LSN of the commit record.
Examples and non-examples
A redo decision. Pages (records , ) and (), both with pageLSN 0. The log:
| LSN | record | prevLSN |
|---|---|---|
| 1 | updates : 10 to 11 | 0 |
| 2 | updates : 30 to 31 | 0 |
| 3 | updates : 20 to 21 | 1 |
| 4 | commit | 3 |
| 5 | updates : 11 to 12 | 2 |
The commit forces the log, so flushedLSN is 4. The buffer manager writes after LSN 3: its pageLSN is 3 and , so the WAL test passes. After LSN 5 the pageLSN of is 5, so the buffer manager may not write again until it flushes the log to 5. The system crashes; the stable log holds LSNs 1 to 4, and the disk holds with pageLSN 3 and with pageLSN 0. Redo reads LSN 1: , skip. LSN 2: , apply, , pageLSN. LSN 3: , skip. The update of LSN 5 was never on disk and its record is lost, which is consistent: the page never held it. The prevLSN column chains as and as .
Byte offsets. If three records of 40, 64 and 40 bytes start at byte 1000 of the log, their LSNs are 1000, 1040 and 1104. The tests above work the same way, since they only compare LSNs.
Non-example: a wall-clock time. A time stamp can go backwards when the clock is set, and two records can get the same value. Neither the redo test nor the WAL test is sound then.
Non-example: a version counter per page. A counter that each change of a page increments is monotone for that page, but it cannot be compared with flushedLSN, so it does not give the WAL test, and it cannot be compared with a record's position in the log without a lookup.
Non-example: a transaction id. Transaction ids order transactions by their start, not records by their position. Two records of one transaction share it.
Several LSNs in one page. ARIES Section 10.4 discusses pages whose parts carry LSNs of their own, as DB2 did for minipages, and ARIES keeps one LSN per page. With one LSN per page, the redo test is on the whole page, which is why records must be applied per page in LSN order.
Related definitions
- Write-ahead logging is the technique whose rules the WAL test and the commit test state.
- A compensation log record carries undoNextLSN, an LSN that skips records already undone.
- Checkpointing and the fuzzy checkpoint store recLSNs and lastLSNs, and the master record stores the LSN of a checkpoint.
- ARIES recovery uses every field in the table.
- Logical logging and physiological logging need the redo test, because their records are not idempotent.
- The index of an entry in a replicated log plays a similar part in distributed systems: it orders entries and lets a replica say how far it has applied the log.
- A Lamport clock also gives monotone numbers, across processes that exchange messages, and its numbers respect the happened-before order; an LSN is simply the position in one log.
References
- C. Mohan, Don Haderle, Bruce Lindsay, Hamid Pirahesh and Peter Schwarz, ARIES, a transaction recovery method supporting fine-granularity locking and partial rollbacks using write-ahead logging, ACM Transactions on Database Systems 17(1), 1992, pages 94 to 162, Section 4.1, Log Records (LSN, PrevLSN, UndoNxtLSN), Section 4.2, Page Structure (page_LSN), Section 4.4, Dirty_Pages Table (RecLSN), Section 6.2, Redo Pass (the page_LSN test), and Section 10.4 (several LSNs in one page)
- PostgreSQL documentation, pg_lsn type, the definition of pg_lsn as a pointer to a location in the WAL, a 64-bit byte position printed as two hexadecimal numbers
- Raghu Ramakrishnan and Johannes Gehrke, Database Management Systems, third edition, McGraw-Hill, 2003, Chapter 18, Crash Recovery (the log, LSNs, pageLSN and flushedLSN)
- Jim Gray and Andreas Reuter, Transaction Processing, Concepts and Techniques, Morgan Kaufmann, 1993, Chapter 9, Log Manager, chapter level only
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag D6MZ35 Edit this page History Report an error Markdown