Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pgrepack.c
4 : : * Logical Replication output plugin for REPACK command
5 : : *
6 : : * Copyright (c) 2026, PostgreSQL Global Development Group
7 : : *
8 : : * IDENTIFICATION
9 : : * src/backend/replication/pgrepack/pgrepack.c
10 : : *
11 : : *-------------------------------------------------------------------------
12 : : */
13 : : #include "postgres.h"
14 : :
15 : : #include "access/detoast.h"
16 : : #include "commands/repack.h"
17 : : #include "commands/repack_internal.h"
18 : : #include "replication/snapbuild.h"
19 : : #include "utils/memutils.h"
20 : :
21 : 7 : PG_MODULE_MAGIC_EXT(
22 : : .name = "pgrepack",
23 : : .version = PG_VERSION
24 : : );
25 : :
26 : : static void repack_startup(LogicalDecodingContext *ctx,
27 : : OutputPluginOptions *opt, bool is_init);
28 : : static void repack_shutdown(LogicalDecodingContext *ctx);
29 : : static void repack_begin_txn(LogicalDecodingContext *ctx,
30 : : ReorderBufferTXN *txn);
31 : : static void repack_commit_txn(LogicalDecodingContext *ctx,
32 : : ReorderBufferTXN *txn, XLogRecPtr commit_lsn);
33 : : static void repack_process_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
34 : : Relation relation, ReorderBufferChange *change);
35 : : static void repack_store_change(LogicalDecodingContext *ctx, Relation relation,
36 : : ConcurrentChangeKind kind, HeapTuple tuple);
37 : :
38 : : void
39 : 7 : _PG_output_plugin_init(OutputPluginCallbacks *cb)
40 : : {
41 : 7 : cb->startup_cb = repack_startup;
42 : 7 : cb->begin_cb = repack_begin_txn;
43 : 7 : cb->change_cb = repack_process_change;
44 : 7 : cb->commit_cb = repack_commit_txn;
45 : 7 : cb->shutdown_cb = repack_shutdown;
46 : 7 : }
47 : :
48 : :
49 : : /* initialize this plugin */
50 : : static void
51 : 7 : repack_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
52 : : bool is_init)
53 : : {
54 : : RepackDecodingState *dstate;
55 : :
56 [ - + ]: 7 : if (!AmRepackWorker())
57 : : {
58 : : /* StartupDecodingContext() should have caught this case already */
59 [ # # ]: 0 : elog(FATAL, "unexpected pgrepack startup outside of repack worker");
60 : : }
61 : :
62 : : /* Initial setup of our private state */
63 : : Assert(CurrentMemoryContext == ctx->context);
64 : 7 : dstate = palloc0_object(RepackDecodingState);
65 : 7 : dstate->change_cxt = AllocSetContextCreate(ctx->context,
66 : : "REPACK - change",
67 : : ALLOCSET_DEFAULT_SIZES);
68 : : /* repack_setup_logical_decoding fills in the rest */
69 : 7 : ctx->output_writer_private = dstate;
70 : :
71 : : /* Probably unnecessary, as we don't use the SQL interface ... */
72 : 7 : opt->output_type = OUTPUT_PLUGIN_BINARY_OUTPUT;
73 : :
74 [ - + ]: 7 : if (ctx->output_plugin_options != NIL)
75 : : {
76 [ # # ]: 0 : ereport(ERROR,
77 : : errcode(ERRCODE_INVALID_PARAMETER_VALUE),
78 : : errmsg("this plugin does not expect any options"));
79 : : }
80 : 7 : }
81 : :
82 : : static void
83 : 7 : repack_shutdown(LogicalDecodingContext *ctx)
84 : : {
85 : 7 : }
86 : :
87 : : /*
88 : : * As we don't release the slot during processing of particular table, there's
89 : : * no room for SQL interface, even for debugging purposes. Therefore we need
90 : : * neither OutputPluginPrepareWrite() nor OutputPluginWrite() in the plugin
91 : : * callbacks. (Although we might want to write custom callbacks, this API
92 : : * seems to be unnecessarily generic for our purposes.)
93 : : */
94 : :
95 : : /* BEGIN callback */
96 : : static void
97 : 11 : repack_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
98 : : {
99 : 11 : }
100 : :
101 : : /* COMMIT callback */
102 : : static void
103 : 11 : repack_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
104 : : XLogRecPtr commit_lsn)
105 : : {
106 : 11 : }
107 : :
108 : : /*
109 : : * Callback for individual changed tuples
110 : : */
111 : : static void
112 : 32 : repack_process_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
113 : : Relation relation, ReorderBufferChange *change)
114 : : {
115 : 32 : RepackDecodingState *private PG_USED_FOR_ASSERTS_ONLY =
116 : : (RepackDecodingState *) ctx->output_writer_private;
117 : :
118 : : /* Changes of other relation should not have been decoded. */
119 : : Assert(RelationGetRelid(relation) == private->relid);
120 : :
121 : : /* Decode entry depending on its type */
122 [ + + + - ]: 32 : switch (change->action)
123 : : {
124 : 7 : case REORDER_BUFFER_CHANGE_INSERT:
125 : : {
126 : : HeapTuple newtuple;
127 : :
128 : 7 : newtuple = change->data.tp.newtuple;
129 : :
130 : : /*
131 : : * Identity checks in the main function should have made this
132 : : * impossible.
133 : : */
134 [ - + ]: 7 : if (newtuple == NULL)
135 [ # # ]: 0 : elog(ERROR, "incomplete insert info");
136 : :
137 : 7 : repack_store_change(ctx, relation, CHANGE_INSERT, newtuple);
138 : : }
139 : 7 : break;
140 : 22 : case REORDER_BUFFER_CHANGE_UPDATE:
141 : : {
142 : : HeapTuple oldtuple,
143 : : newtuple;
144 : :
145 : 22 : oldtuple = change->data.tp.oldtuple;
146 : 22 : newtuple = change->data.tp.newtuple;
147 : :
148 [ - + ]: 22 : if (newtuple == NULL)
149 [ # # ]: 0 : elog(ERROR, "incomplete update info");
150 : :
151 [ + + ]: 22 : if (oldtuple != NULL)
152 : 8 : repack_store_change(ctx, relation, CHANGE_UPDATE_OLD, oldtuple);
153 : :
154 : 22 : repack_store_change(ctx, relation, CHANGE_UPDATE_NEW, newtuple);
155 : : }
156 : 22 : break;
157 : 3 : case REORDER_BUFFER_CHANGE_DELETE:
158 : : {
159 : : HeapTuple oldtuple;
160 : :
161 : 3 : oldtuple = change->data.tp.oldtuple;
162 : :
163 [ - + ]: 3 : if (oldtuple == NULL)
164 [ # # ]: 0 : elog(ERROR, "incomplete delete info");
165 : :
166 : 3 : repack_store_change(ctx, relation, CHANGE_DELETE, oldtuple);
167 : : }
168 : 3 : break;
169 : 0 : default:
170 : :
171 : : /*
172 : : * Should not come here. This includes TRUNCATE of the table being
173 : : * processed. heap_decode() cannot check the file locator easily,
174 : : * but we assume that TRUNCATE uses AccessExclusiveLock on the
175 : : * table so it should not occur during REPACK (CONCURRENTLY).
176 : : */
177 : : Assert(false);
178 : 0 : break;
179 : : }
180 : 32 : }
181 : :
182 : : /*
183 : : * Write the given tuple, with the given change kind, to the repack spill
184 : : * file. Later, the repack decoding worker can read these and replay
185 : : * the operations on the new copy of the table.
186 : : *
187 : : * For each change affecting the table being repacked, we store enough
188 : : * information about each tuple in it, so that it can be replayed in the
189 : : * new copy of the table.
190 : : */
191 : : static void
192 : 40 : repack_store_change(LogicalDecodingContext *ctx, Relation relation,
193 : : ConcurrentChangeKind kind, HeapTuple tuple)
194 : : {
195 : : RepackDecodingState *dstate;
196 : : MemoryContext oldcxt;
197 : : BufFile *file;
198 : 40 : List *attrs_ext = NIL;
199 : : int natt_ext;
200 : :
201 : 40 : dstate = (RepackDecodingState *) ctx->output_writer_private;
202 : 40 : file = dstate->file;
203 : :
204 : : /* Store the change kind. */
205 : 40 : BufFileWrite(file, &kind, 1);
206 : :
207 : : /* Use a frequently-reset context to avoid dealing with leaks manually */
208 : 40 : oldcxt = MemoryContextSwitchTo(dstate->change_cxt);
209 : :
210 : : /*
211 : : * If the tuple contains "external indirect" attributes, we need to write
212 : : * the contents to the file because we have no control over that memory.
213 : : */
214 [ + + ]: 40 : if (HeapTupleHasExternal(tuple))
215 : : {
216 : 13 : TupleDesc desc = RelationGetDescr(relation);
217 : : TupleTableSlot *slot;
218 : :
219 : : /* Initialize the slot, if not done already */
220 [ + + ]: 13 : if (dstate->slot == NULL)
221 : : {
222 : : ResourceOwner saveResourceOwner;
223 : :
224 : 1 : MemoryContextSwitchTo(dstate->worker_cxt);
225 : 1 : saveResourceOwner = CurrentResourceOwner;
226 : 1 : CurrentResourceOwner = dstate->worker_resowner;
227 : 1 : dstate->slot = MakeSingleTupleTableSlot(desc, &TTSOpsHeapTuple);
228 : 1 : MemoryContextSwitchTo(dstate->change_cxt);
229 : 1 : CurrentResourceOwner = saveResourceOwner;
230 : : }
231 : :
232 : 13 : slot = dstate->slot;
233 : 13 : ExecStoreHeapTuple(tuple, slot, false);
234 : :
235 : : /*
236 : : * Loop over all attributes, and find out which ones we need to spill
237 : : * separately, to wit: each one that's a non-null varlena and stored
238 : : * out of line.
239 : : */
240 [ + + ]: 78 : for (int i = 0; i < desc->natts; i++)
241 : : {
242 : 65 : CompactAttribute *attr = TupleDescCompactAttr(desc, i);
243 : : varlena *varlen;
244 : :
245 [ + + + + : 91 : if (attr->attisdropped || attr->attlen != -1 ||
- + ]
246 : 26 : slot_attisnull(slot, i + 1))
247 : 39 : continue;
248 : :
249 : 26 : slot_getsomeattrs(slot, i + 1);
250 : :
251 : : /*
252 : : * This is a non-null varlena datum, but we only care if it's
253 : : * out-of-line
254 : : */
255 : 26 : varlen = (varlena *) DatumGetPointer(slot->tts_values[i]);
256 [ + + ]: 26 : if (!VARATT_IS_EXTERNAL(varlen))
257 : 9 : continue;
258 : :
259 : : /*
260 : : * We spill any indirect-external attributes separately from the
261 : : * heap tuple. Anything else is written as is.
262 : : */
263 [ + + ]: 17 : if (VARATT_IS_EXTERNAL_INDIRECT(varlen))
264 : 15 : attrs_ext = lappend(attrs_ext, varlen);
265 : : else
266 : : {
267 : : /*
268 : : * Logical decoding should not produce "external expanded"
269 : : * attributes (those actually should never appear on disk), so
270 : : * only TOASTed attribute can be seen here.
271 : : *
272 : : * We get here if the table has external values but only
273 : : * in-line values are being updated now.
274 : : */
275 : : Assert(VARATT_IS_EXTERNAL_ONDISK(varlen));
276 : : }
277 : : }
278 : :
279 : 13 : ExecClearTuple(slot);
280 : : }
281 : :
282 : : /*
283 : : * First, write the original heap tuple, prefixed by its length. Note
284 : : * that the external-toast tag for each toasted attribute will be present
285 : : * in what we write, so that we know where to restore each one later.
286 : : */
287 : 40 : BufFileWrite(file, &tuple->t_len, sizeof(tuple->t_len));
288 : 40 : BufFileWrite(file, tuple->t_data, tuple->t_len);
289 : :
290 : : /* Then, write the number of external attributes we found. */
291 : 40 : natt_ext = list_length(attrs_ext);
292 : 40 : BufFileWrite(file, &natt_ext, sizeof(natt_ext));
293 : :
294 : : /* Finally, the attributes themselves, if any */
295 [ + + + + : 95 : foreach_ptr(varlena, attr_val, attrs_ext)
+ + ]
296 : : {
297 : 15 : attr_val = detoast_external_attr(attr_val);
298 : 15 : BufFileWrite(file, attr_val, VARSIZE_ANY(attr_val));
299 : : /* These attributes could be large, so free them right away */
300 : 15 : pfree(attr_val);
301 : : }
302 : :
303 : : /* Cleanup. */
304 : 40 : MemoryContextSwitchTo(oldcxt);
305 : 40 : MemoryContextReset(dstate->change_cxt);
306 : 40 : }
|