Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 39

Designing Log Record Data Structures

Recommended resources

Hello! Welcome back.

In our last lesson, we established the fundamental principles of Write-Ahead Logging (WAL). We learned why a STEAL/NO-FORCE buffer policy is essential for performance and how the two golden rules of WAL ensure Atomicity and Durability, making this policy safe. We now understand the what and the why of logging.

This lesson drills down into the how. We will move from the abstract concept of a log to the concrete implementation details. Your goal is to design the data structures for the various log record types used in a modern recovery algorithm like ARIES. These records are not just simple text entries; they are carefully structured data packets, with each field serving a critical purpose in the recovery process.

We will examine the anatomy of five key log record types:

  • UPDATE records, the workhorses of the log.
  • COMMIT and END records, which manage transaction lifecycle.
  • Compensation Log Records (CLRs), which document UNDO actions.
  • CHECKPOINT records, which make recovery efficient.

By the end of this lesson, you will understand the specific information each log record must contain to enable a robust crash recovery system.

The Anatomy of a Log Record: Common Fields

Before diving into specific record types, let's look at the fields that are common to nearly every log record in ARIES. These fields provide the fundamental context and linkage needed to navigate the log.

Every log record is assigned a unique, monotonically increasing Log Sequence Number (LSN). Think of the LSN as the primary key for the log file. It establishes a total order for all logged operations.

To track all the operations belonging to a single transaction, log records are chained together like a linked list, pointing backward in time.

Lecture 20 Part 8: ARIES Logging

This brief clip from UC Berkeley's CS186 course introduces the three most important common fields in an ARIES log record: LSN, prevLSN, and transaction ID.

Watch the first 50 seconds of this video. Focus on how the prevLSN field creates a backward-chained linked list for each transaction's records within the larger log file.

So, the three common fields are:

  1. LSN (Log Sequence Number): The unique address of this log record.
  2. transID (Transaction ID): The ID of the transaction that generated this record.
  3. prevLSN (Previous LSN): The LSN of the previous log record generated by this same transaction. The first record for a transaction has a null prevLSN.

Beyond just being an ID, LSNs are integrated throughout the database system to coordinate recovery. For instance, every data page on disk stores the LSN of the latest update that touched it (pageLSN). The system also tracks the LSN of the last log record flushed to disk (flushedLSN). This allows the buffer manager to enforce the WAL UNDO rule: it can only write a dirty page to disk if that page's pageLSN is less than or equal to the flushedLSN.

Lecture #20: Database Crash Recovery - CMU 15-445/645

These lecture notes from CMU provide an excellent, concise summary of the different LSNs used in a database system and their purposes. The table is particularly useful as a reference.

Please read the section '2 WAL Records' on the first page. Pay close attention to the diagram and the table defining flushedLSN, pageLSN, and recLSN. This shows how LSNs are more than just log identifiers; they are a core part of the system's state.

The image below shows an example log file just before a crash. You can clearly see the structure: each row is a log record with its LSN, TxnID, and Type, among other fields.

ARIES Recovery Example: Log and System Tables
An example of a log file at the time of a crash. Note the common structure of each record, including the LSN, transaction ID (TxnID), and record type.

UPDATE Records: Capturing the Change

The most frequent log records are UPDATE records. Each time a transaction modifies data on a page, an UPDATE record is generated. For the recovery algorithm to work, this record must contain enough information to both redo the change (for durability) and undo it (for atomicity).

Therefore, an UPDATE record contains additional fields beyond the common ones:

  • type: Set to UPDATE.
  • pageID: The identifier of the page that was modified.
  • offset & length: Specifies the exact location and size of the change on the page.
  • before-image: The value of the data before the modification. This is essential for UNDO.
  • after-image: The value of the data after the modification. This is essential for REDO.

This is often called physiological logging: it's physical in that it describes byte-level changes (before/after images), but logical in that it's constrained to a single page.

Lecture 20 Part 8: ARIES Logging

Let's return to the UC Berkeley video, which now details the specific structure of an UPDATE log record.

Watch from 00:50 to 02:11. The video first lists the various record types and then breaks down the fields of an UPDATE record, explaining their role in redo and undo.


Transaction Lifecycle Records: COMMIT and END

When a transaction successfully finishes, it must be marked as durable.

  • A COMMIT record is written to the log. As we learned in the last lesson, the REDO rule of WAL states that the log, up to and including this COMMIT record, must be flushed to disk before notifying the client of success.

After a transaction commits or aborts, the system may need to do some internal cleanup.

  • An END record is written to the log to signify that this transaction is completely finished and will generate no more log records. This allows the DBMS to safely remove the transaction from its internal data structures, like the Active Transaction Table. Unlike COMMIT records, END records are for internal bookkeeping and don't need to be flushed to disk immediately.

#21 - Database Recovery with ARIES (CMU Intro to Database Systems)

In this segment from CMU's database course, Professor Andy Pavlo explains the distinction between the COMMIT record and the internal-facing TRANSACTION END record.

Watch from 16:35 to 18:12. Focus on the different purposes of these two records: COMMIT is for durability guarantees to the client, while END is for internal resource management.

Test your understanding!

A transaction T1 updates a record and then commits. The DBMS writes an <T1, UPDATE, ...> record, followed by a <T1, COMMIT> record to the log buffer in memory. It then flushes the log to disk and tells the client the transaction is committed. Later, it writes a <T1, END> record to the log buffer. The power goes out immediately after.

Why is it okay that the <T1, END> record was not on disk, while it was critical for the <T1, COMMIT> record to be there?

Show answer

The <T1, COMMIT> record is the database's promise of durability. Once it's on disk, the change is permanent and can be redone after a crash. The <T1, END> record is purely for internal bookkeeping. If it's lost in a crash, the recovery process will simply see T1 as a committed transaction that hasn't been fully cleaned up yet. It can then perform the cleanup after recovery, without affecting correctness. The absence of the END record doesn't compromise the atomicity or durability of T1.


Aborts and Compensation Log Records (CLRs)

What happens when a transaction aborts, or when the recovery manager needs to undo a transaction that was active during a crash? The system can't just silently delete its changes. ARIES follows a key principle: all actions, including undo actions, must be logged.

When an UPDATE from an aborted transaction needs to be reversed, the system doesn't modify the original log. Instead, it writes a new log record called a Compensation Log Record (CLR).

A CLR has a structure similar to an UPDATE record, but it documents the action of reversing a previous update. A crucial property of CLRs is that they are never undone. If the system crashes while writing CLRs, it simply continues the undo process upon restart. This prevents infinite loops of undoing an undo action.

To make the undo process efficient, a CLR contains one more special pointer:

  • undoNextLSN: This points to the prevLSN of the record being compensated. This allows the system to quickly jump to the next record that needs to be undone for this transaction, skipping over any intermediate records from other transactions.

#21 - Database Recovery with ARIES (CMU Intro to Database Systems)

This is one of the most clever parts of ARIES. The following segments from Andy Pavlo's lecture explain how aborts are handled by writing CLRs and how the prevLSN and undoNextLSN fields are used to navigate the log during an undo.

Watch from 18:59 to 25:13. This is a dense but important section. The first part introduces the concept of logging undo actions by writing CLRs. The second part uses a table to clearly show the fields in a CLR, including undoNextLSN, and explains how it reverses a prior update. Pay attention to the distinction between prevLSN (linking the original transaction forward) and undoNextLSN (linking the chain of work-to-be-undone backward).


Checkpoint Records: Making Recovery Fast

If a system crashes, it theoretically has to scan the entire log from the beginning of time. For a long-running system, this is impractical. Checkpoints are a mechanism to limit the amount of work needed for recovery.

ARIES uses a "fuzzy checkpoint" that doesn't require pausing the entire system. When a checkpoint is initiated, the DBMS writes a BEGIN_CHECKPOINT record. When it's finished, it writes an END_CHECKPOINT record. The END_CHECKPOINT record is the important one; it contains a snapshot of two key data structures at the time the checkpoint began:

  1. Active Transaction Table (ATT): A list of all transactions that were running at the start of the checkpoint. For each transaction, it stores the transID, its status (e.g., running, committing), and the lastLSN it wrote.
  2. Dirty Page Table (DPT): A list of all pages in the buffer pool that were dirty (had unpersisted changes) at the start of the checkpoint. For each page, it stores the pageID and the recLSN—the LSN of the log record that first made the page dirty.

During recovery, the system finds the last END_CHECKPOINT record and can use its ATT and DPT to dramatically reduce the amount of the log it needs to analyze.

Lecture #20: Database Crash Recovery - CMU 15-445/645

These notes provide a structured overview of fuzzy checkpoints and the data they contain.

First, re-read the section '3 Normal Execution' on page 2 to solidify your understanding of ABORT and CLR records. Then, read '4 Checkpointing' and the description of 'Fuzzy Checkpoints' on pages 3-4. Focus on the definitions of the Active Transaction Table (ATT) and Dirty Page Table (DPT).


Conclusion

In this lesson, we dissected the fundamental data structures that power database recovery. We've seen that a log file is far more than a simple history; it's a highly structured file where every field is engineered for the specific demands of the ARIES recovery algorithm.

Key Takeaways:

  • Log records share common fields: a unique LSN, a transID, and a prevLSN pointer to chain a transaction's operations together.
  • UPDATE records are the core, containing before-image and after-image data to enable both UNDO and REDO operations.
  • COMMIT and END records manage the transaction lifecycle, separating the guarantee of durability from internal system cleanup.
  • Compensation Log Records (CLRs) are written to log UNDO actions. They are never undone themselves and contain an undoNextLSN pointer to efficiently traverse the chain of updates that must be reversed.
  • CHECKPOINT records contain snapshots of the Active Transaction Table (ATT) and Dirty Page Table (DPT) to limit the portion of the log that must be processed during recovery.

Preview of the Next Lesson

Now that we have designed the building blocks, we are ready to construct the building. In the next lesson, we will see how the ARIES algorithm uses these very log records to orchestrate recovery. We will walk through the three phases of ARIES—Analysis, Redo, and Undo—and see exactly how the information we've structured today is put into action to bring a crashed database back to a perfect, consistent state.