PostgreSQL Source Code git master
Loading...
Searching...
No Matches
heapam_xlog.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * heapam_xlog.h
4 * POSTGRES heap access XLOG definitions.
5 *
6 *
7 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * src/include/access/heapam_xlog.h
11 *
12 *-------------------------------------------------------------------------
13 */
14#ifndef HEAPAM_XLOG_H
15#define HEAPAM_XLOG_H
16
17#include "access/htup.h"
18#include "access/xlogreader.h"
19#include "lib/stringinfo.h"
20#include "storage/buf.h"
21#include "storage/bufpage.h"
23#include "storage/sinval.h"
24#include "utils/relcache.h"
25
26
27/*
28 * WAL record definitions for heapam.c's WAL operations
29 *
30 * XLOG allows to store some information in high 4 bits of log
31 * record xl_info field. We use 3 for opcode and one for init bit.
32 */
33#define XLOG_HEAP_INSERT 0x00
34#define XLOG_HEAP_DELETE 0x10
35#define XLOG_HEAP_UPDATE 0x20
36#define XLOG_HEAP_TRUNCATE 0x30
37#define XLOG_HEAP_HOT_UPDATE 0x40
38#define XLOG_HEAP_CONFIRM 0x50
39#define XLOG_HEAP_LOCK 0x60
40#define XLOG_HEAP_INPLACE 0x70
41
42#define XLOG_HEAP_OPMASK 0x70
43/*
44 * When we insert 1st item on new page in INSERT, UPDATE, HOT_UPDATE,
45 * or MULTI_INSERT, we can (and we do) restore entire page in redo
46 */
47#define XLOG_HEAP_INIT_PAGE 0x80
48/*
49 * We ran out of opcodes, so heapam.c now has a second RmgrId. These opcodes
50 * are associated with RM_HEAP2_ID, but are not logically different from
51 * the ones above associated with RM_HEAP_ID. XLOG_HEAP_OPMASK applies to
52 * these, too.
53 *
54 * There's no difference between XLOG_HEAP2_PRUNE_ON_ACCESS,
55 * XLOG_HEAP2_PRUNE_VACUUM_SCAN and XLOG_HEAP2_PRUNE_VACUUM_CLEANUP records.
56 * They have separate opcodes just for debugging and analysis purposes, to
57 * indicate why the WAL record was emitted.
58 */
59#define XLOG_HEAP2_REWRITE 0x00
60#define XLOG_HEAP2_PRUNE_ON_ACCESS 0x10
61#define XLOG_HEAP2_PRUNE_VACUUM_SCAN 0x20
62#define XLOG_HEAP2_PRUNE_VACUUM_CLEANUP 0x30
63/* 0x40 was XLOG_HEAP2_VISIBLE */
64#define XLOG_HEAP2_MULTI_INSERT 0x50
65#define XLOG_HEAP2_LOCK_UPDATED 0x60
66#define XLOG_HEAP2_NEW_CID 0x70
67
68/*
69 * xl_heap_insert/xl_heap_multi_insert flag values, 8 bits are available.
70 */
71/* PD_ALL_VISIBLE was cleared */
72#define XLH_INSERT_ALL_VISIBLE_CLEARED (1<<0)
73#define XLH_INSERT_LAST_IN_MULTI (1<<1)
74#define XLH_INSERT_IS_SPECULATIVE (1<<2)
75#define XLH_INSERT_CONTAINS_NEW_TUPLE (1<<3)
76#define XLH_INSERT_ON_TOAST_RELATION (1<<4)
77
78/* all_frozen_set always implies all_visible_set */
79#define XLH_INSERT_ALL_FROZEN_SET (1<<5)
80
81/*
82 * xl_heap_update flag values, 8 bits are available.
83 */
84/* PD_ALL_VISIBLE was cleared */
85#define XLH_UPDATE_OLD_ALL_VISIBLE_CLEARED (1<<0)
86/* PD_ALL_VISIBLE was cleared in the 2nd page */
87#define XLH_UPDATE_NEW_ALL_VISIBLE_CLEARED (1<<1)
88#define XLH_UPDATE_CONTAINS_OLD_TUPLE (1<<2)
89#define XLH_UPDATE_CONTAINS_OLD_KEY (1<<3)
90#define XLH_UPDATE_CONTAINS_NEW_TUPLE (1<<4)
91#define XLH_UPDATE_PREFIX_FROM_OLD (1<<5)
92#define XLH_UPDATE_SUFFIX_FROM_OLD (1<<6)
93
94/* convenience macro for checking whether any form of old tuple was logged */
95#define XLH_UPDATE_CONTAINS_OLD \
96 (XLH_UPDATE_CONTAINS_OLD_TUPLE | XLH_UPDATE_CONTAINS_OLD_KEY)
97
98/*
99 * xl_heap_delete flag values, 8 bits are available.
100 */
101/* PD_ALL_VISIBLE was cleared */
102#define XLH_DELETE_ALL_VISIBLE_CLEARED (1<<0)
103#define XLH_DELETE_CONTAINS_OLD_TUPLE (1<<1)
104#define XLH_DELETE_CONTAINS_OLD_KEY (1<<2)
105#define XLH_DELETE_IS_SUPER (1<<3)
106#define XLH_DELETE_IS_PARTITION_MOVE (1<<4)
107/* See heap_delete() */
108#define XLH_DELETE_NO_LOGICAL (1<<5)
109
110/* convenience macro for checking whether any form of old tuple was logged */
111#define XLH_DELETE_CONTAINS_OLD \
112 (XLH_DELETE_CONTAINS_OLD_TUPLE | XLH_DELETE_CONTAINS_OLD_KEY)
113
114/* This is what we need to know about delete */
115#define HEAP_DELETE_BLKREF_HEAP 0
116#define HEAP_DELETE_BLKREF_VM 1
117
118typedef struct xl_heap_delete
119{
120 TransactionId xmax; /* xmax of the deleted tuple */
121 OffsetNumber offnum; /* deleted tuple's offset */
122 uint8 infobits_set; /* infomask bits */
125
126#define SizeOfHeapDelete (offsetof(xl_heap_delete, flags) + sizeof(uint8))
127
128/*
129 * xl_heap_truncate flag values, 8 bits are available.
130 */
131#define XLH_TRUNCATE_CASCADE (1<<0)
132#define XLH_TRUNCATE_RESTART_SEQS (1<<1)
133
134/*
135 * For truncate we list all truncated relids in an array, followed by all
136 * sequence relids that need to be restarted, if any.
137 * All rels are always within the same database, so we just list dbid once.
138 */
146
147#define SizeOfHeapTruncate (offsetof(xl_heap_truncate, relids))
148
149/*
150 * We don't store the whole fixed part (HeapTupleHeaderData) of an inserted
151 * or updated tuple in WAL; we can save a few bytes by reconstructing the
152 * fields that are available elsewhere in the WAL record, or perhaps just
153 * plain needn't be reconstructed. These are the fields we must store.
154 */
161
162#define SizeOfHeapHeader (offsetof(xl_heap_header, t_hoff) + sizeof(uint8))
163
164/* This is what we need to know about insert */
165#define HEAP_INSERT_BLKREF_HEAP 0
166#define HEAP_INSERT_BLKREF_VM 1
167
168typedef struct xl_heap_insert
169{
170 OffsetNumber offnum; /* inserted tuple's offset */
172
173 /* xl_heap_header & TUPLE DATA in HEAP_INSERT_BLKREF_HEAP */
175
176#define SizeOfHeapInsert (offsetof(xl_heap_insert, flags) + sizeof(uint8))
177
178/*
179 * This is what we need to know about a multi-insert.
180 *
181 * The main data of the record consists of this xl_heap_multi_insert header.
182 * 'offsets' array is omitted if the whole page is reinitialized
183 * (XLOG_HEAP_INIT_PAGE).
184 */
185#define HEAP_MULTI_INSERT_BLKREF_HEAP 0
186#define HEAP_MULTI_INSERT_BLKREF_VM 1
187
188/*
189 * In HEAP_MULTI_INSERT_BLKREF_HEAP's data portion, there is an
190 * xl_multi_insert_tuple struct, followed by the tuple data for each tuple.
191 * There is padding to align each xl_multi_insert_tuple struct.
192 */
199
200#define SizeOfHeapMultiInsert offsetof(xl_heap_multi_insert, offsets)
201
203{
204 uint16 datalen; /* size of tuple data that follows */
208 /* TUPLE DATA FOLLOWS AT END OF STRUCT */
210
211#define SizeOfMultiInsertTuple (offsetof(xl_multi_insert_tuple, t_hoff) + sizeof(uint8))
212
213/*
214 * This is what we need to know about update|hot_update
215 *
216 * HEAP_UPDATE_BLKREF_HEAP_NEW: new page
217 *
218 * If XLH_UPDATE_PREFIX_FROM_OLD or XLH_UPDATE_SUFFIX_FROM_OLD flags are set,
219 * the prefix and/or suffix come first, as one or two uint16s.
220 *
221 * After that, xl_heap_header and new tuple data follow. The new tuple
222 * data doesn't include the prefix and suffix, which are copied from the
223 * old tuple on replay.
224 *
225 * If XLH_UPDATE_CONTAINS_NEW_TUPLE flag is given, the tuple data is
226 * included even if a full-page image was taken.
227 *
228 * HEAP_UPDATE_BLKREF_HEAP_OLD: old page, if different. (no data, just a reference
229 * to the block)
230 *
231 * HEAP_UPDATE_BLKREF_VM_NEW: VM page covering the new heap page. Registered
232 * when XLH_UPDATE_NEW_ALL_VISIBLE_CLEARED is set and the new heap page's VM bit
233 * was actually cleared. Also covers the old heap page's VM bits when both heap
234 * pages map to the same VM page and both blocks' VM bits were actually cleared.
235 *
236 * HEAP_UPDATE_BLKREF_VM_OLD: VM page covering the old heap page. Only
237 * registered when XLH_UPDATE_OLD_ALL_VISIBLE_CLEARED is set and the old heap
238 * page's VM bits were actually cleared. Also only registered when the old heap
239 * page's VM bits are on a different VM page than the new heap page's or they
240 * are on the same VM page and only the old block's VM bits are cleared.
241 */
242#define HEAP_UPDATE_BLKREF_HEAP_NEW 0
243#define HEAP_UPDATE_BLKREF_HEAP_OLD 1
244#define HEAP_UPDATE_BLKREF_VM_NEW 2
245#define HEAP_UPDATE_BLKREF_VM_OLD 3
246
247typedef struct xl_heap_update
248{
249 TransactionId old_xmax; /* xmax of the old tuple */
250 OffsetNumber old_offnum; /* old tuple's offset */
251 uint8 old_infobits_set; /* infomask bits to set on old tuple */
253 TransactionId new_xmax; /* xmax of the new tuple */
254 OffsetNumber new_offnum; /* new tuple's offset */
255
256 /*
257 * If XLH_UPDATE_CONTAINS_OLD_TUPLE or XLH_UPDATE_CONTAINS_OLD_KEY flags
258 * are set, xl_heap_header and tuple data for the old tuple follow.
259 */
261
262#define SizeOfHeapUpdate (offsetof(xl_heap_update, new_offnum) + sizeof(OffsetNumber))
263
264/*
265 * These structures and flags encode VACUUM pruning and freezing and on-access
266 * pruning page modifications.
267 *
268 * xl_heap_prune is the main record. The XLHP_HAS_* flags indicate which
269 * "sub-records" are included and the other XLHP_* flags provide additional
270 * information about the conditions for replay.
271 *
272 * The data for block reference 0 contains "sub-records" depending on which of
273 * the XLHP_HAS_* flags are set. See xlhp_* struct definitions below. The
274 * sub-records appear in the same order as the XLHP_* flags. An example
275 * record with every sub-record included:
276 *
277 *-----------------------------------------------------------------------------
278 * Main data section:
279 *
280 * xl_heap_prune
281 * uint16 flags
282 * TransactionId snapshot_conflict_horizon
283 *
284 * Block 0 data section:
285 *
286 * xlhp_freeze_plans
287 * uint16 nplans
288 * [2 bytes of padding]
289 * xlhp_freeze_plan plans[nplans]
290 *
291 * xlhp_prune_items
292 * uint16 nredirected
293 * OffsetNumber redirected[2 * nredirected]
294 *
295 * xlhp_prune_items
296 * uint16 ndead
297 * OffsetNumber nowdead[ndead]
298 *
299 * xlhp_prune_items
300 * uint16 nunused
301 * OffsetNumber nowunused[nunused]
302 *
303 * OffsetNumber frz_offsets[sum([plan.ntuples for plan in plans])]
304 *-----------------------------------------------------------------------------
305 *
306 * NOTE: because the record data is assembled from many optional parts, we
307 * have to pay close attention to alignment. In the main data section,
308 * 'snapshot_conflict_horizon' is stored unaligned after 'flags', to save
309 * space. In the block 0 data section, the freeze plans appear first, because
310 * they contain TransactionId fields that require 4-byte alignment. All the
311 * other fields require only 2-byte alignment. This is also the reason that
312 * 'frz_offsets' is stored separately from the xlhp_freeze_plan structs.
313 */
314typedef struct xl_heap_prune
315{
317
318 /*
319 * If XLHP_HAS_CONFLICT_HORIZON is set, the conflict horizon XID follows,
320 * unaligned
321 */
323
324#define SizeOfHeapPrune (offsetof(xl_heap_prune, flags) + sizeof(uint16))
325
326/* to handle recovery conflict during logical decoding on standby */
327#define XLHP_IS_CATALOG_REL (1 << 1)
328
329/*
330 * Does replaying the record require a cleanup-lock?
331 *
332 * Pruning, in VACUUM's first pass or when otherwise accessing a page,
333 * requires a cleanup lock. For freezing, and VACUUM's second pass which
334 * marks LP_DEAD line pointers as unused without moving any tuple data, an
335 * ordinary exclusive lock is sufficient.
336 */
337#define XLHP_CLEANUP_LOCK (1 << 2)
338
339/*
340 * If we remove or freeze any entries that contain xids, we need to include a
341 * snapshot conflict horizon. It's used in Hot Standby mode to ensure that
342 * there are no queries running for which the removed tuples are still
343 * visible, or which still consider the frozen XIDs as running.
344 */
345#define XLHP_HAS_CONFLICT_HORIZON (1 << 3)
346
347/*
348 * Indicates that an xlhp_freeze_plans sub-record and one or more
349 * xlhp_freeze_plan sub-records are present.
350 */
351#define XLHP_HAS_FREEZE_PLANS (1 << 4)
352
353/*
354 * XLHP_HAS_REDIRECTIONS, XLHP_HAS_DEAD_ITEMS, and XLHP_HAS_NOW_UNUSED_ITEMS
355 * indicate that xlhp_prune_items sub-records with redirected, dead, and
356 * unused item offsets are present.
357 */
358#define XLHP_HAS_REDIRECTIONS (1 << 5)
359#define XLHP_HAS_DEAD_ITEMS (1 << 6)
360#define XLHP_HAS_NOW_UNUSED_ITEMS (1 << 7)
361
362/*
363 * The xl_heap_prune record's flags may also contain which VM bits to set.
364 * xl_heap_prune should always use the XLHP_VM_ALL_VISIBLE and
365 * XLHP_VM_ALL_FROZEN flags and translate them to their visibilitymapdefs.h
366 * equivalents, VISIBILITYMAP_ALL_VISIBLE and VISIBILITYMAP_ALL_FROZEN.
367 */
368#define XLHP_VM_ALL_VISIBLE (1 << 8)
369#define XLHP_VM_ALL_FROZEN (1 << 9)
370
371/*
372 * xlhp_freeze_plan describes how to freeze a group of one or more heap tuples
373 * (appears in xl_heap_prune's xlhp_freeze_plans sub-record)
374 */
375/* 0x01 was XLH_FREEZE_XMIN */
376#define XLH_FREEZE_XVAC 0x02
377#define XLH_INVALID_XVAC 0x04
378
379typedef struct xlhp_freeze_plan
380{
385
386 /* Length of individual page offset numbers array for this plan */
389
390/*
391 * This is what we need to know about a block being frozen during vacuum
392 *
393 * The backup block's data contains an array of xlhp_freeze_plan structs (with
394 * nplans elements). The individual item offsets are located in an array at
395 * the end of the entire record with nplans * (each plan's ntuples) members
396 * Those offsets are in the same order as the plans. The REDO routine uses
397 * the offsets to freeze the corresponding heap tuples.
398 *
399 * (As of PostgreSQL 17, XLOG_HEAP2_PRUNE_VACUUM_SCAN records replace the
400 * separate XLOG_HEAP2_FREEZE_PAGE records.)
401 */
407
408/*
409 * Generic sub-record type contained in block reference 0 of an xl_heap_prune
410 * record and used for redirect, dead, and unused items if any of
411 * XLHP_HAS_REDIRECTIONS/XLHP_HAS_DEAD_ITEMS/XLHP_HAS_NOW_UNUSED_ITEMS are
412 * set. Note that in the XLHP_HAS_REDIRECTIONS variant, there are actually 2
413 * * length number of OffsetNumbers in the data.
414 */
420
421
422/* flags for infobits_set */
423#define XLHL_XMAX_IS_MULTI 0x01
424#define XLHL_XMAX_LOCK_ONLY 0x02
425#define XLHL_XMAX_EXCL_LOCK 0x04
426#define XLHL_XMAX_KEYSHR_LOCK 0x08
427#define XLHL_KEYS_UPDATED 0x10
428
429/* flag bits for xl_heap_lock / xl_heap_lock_updated's flag field */
430#define XLH_LOCK_ALL_FROZEN_CLEARED 0x01
431
432/* This is what we need to know about lock */
433#define HEAP_LOCK_BLKREF_HEAP 0
434#define HEAP_LOCK_BLKREF_VM 1
435
436typedef struct xl_heap_lock
437{
438 TransactionId xmax; /* might be a MultiXactId */
439 OffsetNumber offnum; /* locked tuple's offset on page */
440 uint8 infobits_set; /* infomask and infomask2 bits to set */
441 uint8 flags; /* XLH_LOCK_* flag bits */
443
444#define SizeOfHeapLock (offsetof(xl_heap_lock, flags) + sizeof(uint8))
445
446/* This is what we need to know about locking an updated version of a row */
454
455#define SizeOfHeapLockUpdated (offsetof(xl_heap_lock_updated, flags) + sizeof(uint8))
456
457/* This is what we need to know about confirmation of speculative insertion */
458typedef struct xl_heap_confirm
459{
460 OffsetNumber offnum; /* confirmed tuple's offset on page */
462
463#define SizeOfHeapConfirm (offsetof(xl_heap_confirm, offnum) + sizeof(OffsetNumber))
464
465/* This is what we need to know about in-place update */
466typedef struct xl_heap_inplace
467{
468 OffsetNumber offnum; /* updated tuple's offset on page */
469 Oid dbId; /* MyDatabaseId */
470 Oid tsId; /* MyDatabaseTableSpace */
471 bool relcacheInitFileInval; /* invalidate relcache init files */
472 int nmsgs; /* number of shared inval msgs */
475
476#define MinSizeOfHeapInplace (offsetof(xl_heap_inplace, nmsgs) + sizeof(int))
477
478typedef struct xl_heap_new_cid
479{
480 /*
481 * store toplevel xid so we don't have to merge cids from different
482 * transactions
483 */
487 CommandId combocid; /* just for debugging */
488
489 /*
490 * Store the relfilelocator/ctid pair to facilitate lookups.
491 */
495
496#define SizeOfHeapNewCid (offsetof(xl_heap_new_cid, target_tid) + sizeof(ItemPointerData))
497
498/* logical rewrite xlog record header */
500{
501 TransactionId mapped_xid; /* xid that might need to see the row */
502 Oid mapped_db; /* DbOid or InvalidOid for shared rels */
503 Oid mapped_rel; /* Oid of the mapped relation */
504 off_t offset; /* How far have we written so far */
505 uint32 num_mappings; /* Number of in-memory mappings */
506 XLogRecPtr start_lsn; /* Insert LSN at begin of rewrite */
508
510 TransactionId *snapshotConflictHorizon);
511
512extern void heap_redo(XLogReaderState *record);
513extern void heap_desc(StringInfo buf, XLogReaderState *record);
514extern const char *heap_identify(uint8 info);
515extern void heap_mask(char *pagedata, BlockNumber blkno);
516extern void heap2_redo(XLogReaderState *record);
517extern void heap2_desc(StringInfo buf, XLogReaderState *record);
518extern const char *heap2_identify(uint8 info);
520
521/* in heapdesc.c, so it can be shared between frontend/backend code */
523 int *nplans, xlhp_freeze_plan **plans,
525 int *nredirected, OffsetNumber **redirected,
526 int *ndead, OffsetNumber **nowdead,
527 int *nunused, OffsetNumber **nowunused);
528
529#endif /* HEAPAM_XLOG_H */
uint32 BlockNumber
Definition block.h:31
uint8_t uint8
Definition c.h:681
#define FLEXIBLE_ARRAY_MEMBER
Definition c.h:617
uint16_t uint16
Definition c.h:682
uint32_t uint32
Definition c.h:683
uint32 CommandId
Definition c.h:809
uint32 TransactionId
Definition c.h:795
void heap_desc(StringInfo buf, XLogReaderState *record)
Definition heapdesc.c:185
void heap_redo(XLogReaderState *record)
void heap2_desc(StringInfo buf, XLogReaderState *record)
Definition heapdesc.c:265
void heap_xlog_deserialize_prune_and_freeze(char *cursor, uint16 flags, int *nplans, xlhp_freeze_plan **plans, OffsetNumber **frz_offsets, int *nredirected, OffsetNumber **redirected, int *ndead, OffsetNumber **nowdead, int *nunused, OffsetNumber **nowunused)
Definition heapdesc.c:106
void heap_mask(char *pagedata, BlockNumber blkno)
void HeapTupleHeaderAdvanceConflictHorizon(HeapTupleHeader tuple, TransactionId *snapshotConflictHorizon)
Definition heapam.c:8194
void heap_xlog_logical_rewrite(XLogReaderState *r)
const char * heap2_identify(uint8 info)
Definition heapdesc.c:442
const char * heap_identify(uint8 info)
Definition heapdesc.c:397
void heap2_redo(XLogReaderState *record)
uint16 OffsetNumber
Definition off.h:24
static char buf[DEFAULT_XLOG_SEG_SIZE]
unsigned int Oid
static int fb(int x)
Definition type.h:139
OffsetNumber offnum
TransactionId xmax
OffsetNumber offnum
OffsetNumber offnum
SharedInvalidationMessage msgs[FLEXIBLE_ARRAY_MEMBER]
bool relcacheInitFileInval
OffsetNumber offnum
TransactionId xmax
OffsetNumber offnum
uint8 infobits_set
OffsetNumber offnum
TransactionId xmax
OffsetNumber offsets[FLEXIBLE_ARRAY_MEMBER]
CommandId combocid
ItemPointerData target_tid
TransactionId top_xid
RelFileLocator target_locator
TransactionId mapped_xid
Oid relids[FLEXIBLE_ARRAY_MEMBER]
TransactionId new_xmax
uint8 old_infobits_set
TransactionId old_xmax
OffsetNumber old_offnum
OffsetNumber new_offnum
TransactionId xmax
xlhp_freeze_plan plans[FLEXIBLE_ARRAY_MEMBER]
OffsetNumber data[FLEXIBLE_ARRAY_MEMBER]
uint64 XLogRecPtr
Definition xlogdefs.h:21