12.3. ReorderBuffer Structure
Alpha Version: Work in progress.
A ReorderBuffer area is allocated for each walsender process. The size of this area is limited by the logical_decoding_work_mem configuration parameter (the default is 64MB).
The ReorderBuffer consists of the following three components:
Figure 12.11. ReorderBuffer Structure.
The central element of the ReorderBuffer structure is the by_txn hash table, which uses the transaction ID (txid) as its key. Each entry in this hash table is a ReorderBufferTXN structure, which stores metadata and the actual WAL data associated with each transaction.
Individual data modifications (such as INSERT, UPDATE, and DELETE) are represented by ReorderBufferChange structures. These are appended in LSN order to the changes doubly linked list within the corresponding ReorderBufferTXN.
12.3.1. ReorderBuffer
This structure maintains the primary context for logical decoding.
| Item | Type | Description |
|---|---|---|
| by_txn | HTAB_* | A hash table mapping txids to ReorderBufferTXN entries. It functions as an index for rapidly retrieving active transactions. |
12.3.2. ReorderBufferTXN
This structure manages the state of an individual transaction and its associated changes.
| Item | Type | Description |
|---|---|---|
| first_lsn | XLogRecPtr | The LSN of the first change record belonging to this transaction. It is used to identify the starting point of the transaction. |
| final_lsn | XLogRecPtr | The LSN of the commit (or abort) record for this transaction. |
| origin_id | RepOriginId | The ID of the replication origin where this transaction was initially created. |
| origin_lsn | XLogRecPtr | The LSN of the commit record on the publisher where this transaction originated. |
| base_snapshot | Snapshot | The historic snapshot used for decoding the transaction. It ensures correct visibility during catalog scans by identifying which data was visible at the start of the transaction. |
| changes | dlist_head | A doubly linked list of ReorderBufferChange structures, storing individual data change records in LSN order. See the following subsection. |
Note: While base_snapshot is essential for determining visibility immediately after slot creation or during catalog changes, it is omitted from subsequent discussions to focus on the steady-state data flow.
12.3.3. ReorderBufferChange
This structure represents an individual data modification. Here, only the main items are listed.
| Item | Type | Description |
|---|---|---|
| lsn | XLogRecPtr | The LSN of the WAL record that generated this specific change. |
| action | ReorderBufferChangeType | The type of change operation (e.g., INSERT, UPDATE, DELETE, or TRUNCATE). |
| data | union | A union containing action-specific data, such as the tp (tuple) or truncate structures. |
| data.tp.rlocator | RelFileLocator | Identifies the physical relation (table) affected by the change. This is a triplet consisting of spcOid (Tablespace), dbOid (Database), and relNumber (RelFilenode number). |
| data.tp.oldtuple | HeapTuple | The “before” version of the tuple. This is populated for UPDATE or DELETE operations if required by the Replica Identity configuration (see Section 12.4.3). |
| data.tp.newtuple | HeapTuple | The “after” version of the tuple, containing the new data for INSERT or UPDATE operations. |