PostgreSQL Source Code git master
snapbuild_internal.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * snapbuild_internal.h
4 * This file contains declarations for logical decoding utility
5 * functions for internal use.
6 *
7 * Copyright (c) 2024-2025, PostgreSQL Global Development Group
8 *
9 * src/include/replication/snapbuild_internal.h
10 *
11 *-------------------------------------------------------------------------
12 */
13
14#ifndef SNAPBUILD_INTERNAL_H
15#define SNAPBUILD_INTERNAL_H
16
17#include "port/pg_crc32c.h"
20
21/*
22 * This struct contains the current state of the snapshot building
23 * machinery. It is exposed to the public, so pay attention when changing its
24 * contents.
25 */
27{
28 /* how far are we along building our first full snapshot */
30
31 /* private memory context used to allocate memory for this module. */
33
34 /* all transactions < than this have committed/aborted */
36
37 /* all transactions >= than this are uncommitted */
39
40 /*
41 * Don't replay commits from an LSN < this LSN. This can be set externally
42 * but it will also be advanced (never retreat) from within snapbuild.c.
43 */
45
46 /*
47 * LSN at which two-phase decoding was enabled or LSN at which we found a
48 * consistent point at the time of slot creation.
49 *
50 * The prepared transactions, that were skipped because previously
51 * two-phase was not enabled or are not covered by initial snapshot, need
52 * to be sent later along with commit prepared and they must be before
53 * this point.
54 */
56
57 /*
58 * Don't start decoding WAL until the "xl_running_xacts" information
59 * indicates there are no running xids with an xid smaller than this.
60 */
62
63 /* Indicates if we are building full snapshot or just catalog one. */
65
66 /*
67 * Indicates if we are using the snapshot builder for the creation of a
68 * logical replication slot. If it's true, the start point for decoding
69 * changes is not determined yet. So we skip snapshot restores to properly
70 * find the start point. See SnapBuildFindSnapshot() for details.
71 */
73
74 /*
75 * Snapshot that's valid to see the catalog state seen at this moment.
76 */
78
79 /*
80 * LSN of the last location we are sure a snapshot has been serialized to.
81 */
83
84 /*
85 * The reorderbuffer we need to update with usable snapshots et al.
86 */
88
89 /*
90 * TransactionId at which the next phase of initial snapshot building will
91 * happen. InvalidTransactionId if not known (i.e. SNAPBUILD_START), or
92 * when no next phase necessary (SNAPBUILD_CONSISTENT).
93 */
95
96 /*
97 * Array of transactions which could have catalog changes that committed
98 * between xmin and xmax.
99 */
100 struct
101 {
102 /* number of committed transactions */
103 size_t xcnt;
104
105 /* available space for committed transactions */
107
108 /*
109 * Until we reach a CONSISTENT state, we record commits of all
110 * transactions, not just the catalog changing ones. Record when that
111 * changes so we know we cannot export a snapshot safely anymore.
112 */
114
115 /*
116 * Array of committed transactions that have modified the catalog.
117 *
118 * As this array is frequently modified we do *not* keep it in
119 * xidComparator order. Instead we sort the array when building &
120 * distributing a snapshot.
121 *
122 * TODO: It's unclear whether that reasoning has much merit. Every
123 * time we add something here after becoming consistent will also
124 * require distributing a snapshot. Storing them sorted would
125 * potentially also make it easier to purge (but more complicated wrt
126 * wraparound?). Should be improved if sorting while building the
127 * snapshot shows up in profiles.
128 */
131
132 /*
133 * Array of transactions and subtransactions that had modified catalogs
134 * and were running when the snapshot was serialized.
135 *
136 * We normally rely on some WAL record types such as HEAP2_NEW_CID to know
137 * if the transaction has changed the catalog. But it could happen that
138 * the logical decoding decodes only the commit record of the transaction
139 * after restoring the previously serialized snapshot in which case we
140 * will miss adding the xid to the snapshot and end up looking at the
141 * catalogs with the wrong snapshot.
142 *
143 * Now to avoid the above problem, we serialize the transactions that had
144 * modified the catalogs and are still running at the time of snapshot
145 * serialization. We fill this array while restoring the snapshot and then
146 * refer it while decoding commit to ensure if the xact has modified the
147 * catalog. We discard this array when all the xids in the list become old
148 * enough to matter. See SnapBuildPurgeOlderTxn for details.
149 */
150 struct
151 {
152 /* number of transactions */
153 size_t xcnt;
154
155 /* This array must be sorted in xidComparator order */
158};
159
160/* -----------------------------------
161 * Snapshot serialization support
162 * -----------------------------------
163 */
164
165/*
166 * We store current state of struct SnapBuild on disk in the following manner:
167 *
168 * struct SnapBuildOnDisk;
169 * TransactionId * committed.xcnt; (*not xcnt_space*)
170 * TransactionId * catchange.xcnt;
171 *
172 * Check if the SnapBuildOnDiskConstantSize and SnapBuildOnDiskNotChecksummedSize
173 * macros need to be updated when modifying the SnapBuildOnDisk struct.
174 */
175typedef struct SnapBuildOnDisk
176{
177 /* first part of this struct needs to be version independent */
178
179 /* data not covered by checksum */
182
183 /* data covered by checksum */
184
185 /* version, in case we want to support pg_upgrade */
187 /* how large is the on disk data, excluding the constant sized part */
189
190 /* version dependent part */
192
193 /* variable amount of TransactionIds follows */
195
197 MemoryContext context, bool missing_ok);
198
199#endif /* SNAPBUILD_INTERNAL_H */
uint32_t uint32
Definition: c.h:502
uint32 TransactionId
Definition: c.h:623
uint32 pg_crc32c
Definition: pg_crc32c.h:38
SnapBuildState
Definition: snapbuild.h:23
bool SnapBuildRestoreSnapshot(SnapBuildOnDisk *ondisk, XLogRecPtr lsn, MemoryContext context, bool missing_ok)
Definition: snapbuild.c:1694
struct SnapBuildOnDisk SnapBuildOnDisk
XLogRecPtr start_decoding_at
SnapBuildState state
TransactionId xmin
TransactionId initial_xmin_horizon
struct SnapBuild::@117 committed
TransactionId xmax
TransactionId * xip
Snapshot snapshot
XLogRecPtr two_phase_at
bool building_full_snapshot
TransactionId next_phase_at
XLogRecPtr last_serialized_snapshot
bool includes_all_transactions
MemoryContext context
ReorderBuffer * reorder
struct SnapBuild::@118 catchange
uint64 XLogRecPtr
Definition: xlogdefs.h:21