PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pgrepack.c
Go to the documentation of this file.
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"
18#include "utils/memutils.h"
19
21
23 OutputPluginOptions *opt, bool is_init);
26 ReorderBufferTXN *txn);
28 ReorderBufferTXN *txn, XLogRecPtr commit_lsn);
30 Relation relation, ReorderBufferChange *change);
31static void repack_store_change(LogicalDecodingContext *ctx, Relation relation,
33
34void
43
44
45/* initialize this plugin */
46static void
48 bool is_init)
49{
51
52 /* Probably unnecessary, as we don't use the SQL interface ... */
54
55 if (ctx->output_plugin_options != NIL)
56 {
59 errmsg("this plugin does not expect any options"));
60 }
61}
62
63static void
67
68/*
69 * As we don't release the slot during processing of particular table, there's
70 * no room for SQL interface, even for debugging purposes. Therefore we need
71 * neither OutputPluginPrepareWrite() nor OutputPluginWrite() in the plugin
72 * callbacks. (Although we might want to write custom callbacks, this API
73 * seems to be unnecessarily generic for our purposes.)
74 */
75
76/* BEGIN callback */
77static void
81
82/* COMMIT callback */
83static void
88
89/*
90 * Callback for individual changed tuples
91 */
92static void
94 Relation relation, ReorderBufferChange *change)
95{
98
99 /* Changes of other relation should not have been decoded. */
100 Assert(RelationGetRelid(relation) == private->relid);
101
102 /* Decode entry depending on its type */
103 switch (change->action)
104 {
106 {
107 HeapTuple newtuple;
108
109 newtuple = change->data.tp.newtuple;
110
111 /*
112 * Identity checks in the main function should have made this
113 * impossible.
114 */
115 if (newtuple == NULL)
116 elog(ERROR, "incomplete insert info");
117
118 repack_store_change(ctx, relation, CHANGE_INSERT, newtuple);
119 }
120 break;
122 {
123 HeapTuple oldtuple,
124 newtuple;
125
126 oldtuple = change->data.tp.oldtuple;
127 newtuple = change->data.tp.newtuple;
128
129 if (newtuple == NULL)
130 elog(ERROR, "incomplete update info");
131
132 if (oldtuple != NULL)
133 repack_store_change(ctx, relation, CHANGE_UPDATE_OLD, oldtuple);
134
135 repack_store_change(ctx, relation, CHANGE_UPDATE_NEW, newtuple);
136 }
137 break;
139 {
140 HeapTuple oldtuple;
141
142 oldtuple = change->data.tp.oldtuple;
143
144 if (oldtuple == NULL)
145 elog(ERROR, "incomplete delete info");
146
147 repack_store_change(ctx, relation, CHANGE_DELETE, oldtuple);
148 }
149 break;
150 default:
151
152 /*
153 * Should not come here. This includes TRUNCATE of the table being
154 * processed. heap_decode() cannot check the file locator easily,
155 * but we assume that TRUNCATE uses AccessExclusiveLock on the
156 * table so it should not occur during REPACK (CONCURRENTLY).
157 */
158 Assert(false);
159 break;
160 }
161}
162
163/*
164 * Write the given tuple, with the given change kind, to the repack spill
165 * file. Later, the repack decoding worker can read these and replay
166 * the operations on the new copy of the table.
167 *
168 * For each change affecting the table being repacked, we store enough
169 * information about each tuple in it, so that it can be replayed in the
170 * new copy of the table.
171 */
172static void
175{
178 BufFile *file;
179 List *attrs_ext = NIL;
180 int natt_ext;
181
183 file = dstate->file;
184
185 /* Store the change kind. */
186 BufFileWrite(file, &kind, 1);
187
188 /* Use a frequently-reset context to avoid dealing with leaks manually */
189 oldcxt = MemoryContextSwitchTo(dstate->change_cxt);
190
191 /*
192 * If the tuple contains "external indirect" attributes, we need to write
193 * the contents to the file because we have no control over that memory.
194 */
195 if (HeapTupleHasExternal(tuple))
196 {
197 TupleDesc desc = RelationGetDescr(relation);
198 TupleTableSlot *slot;
199
200 /* Initialize the slot, if not done already */
201 if (dstate->slot == NULL)
202 {
204
205 MemoryContextSwitchTo(dstate->worker_cxt);
207 CurrentResourceOwner = dstate->worker_resowner;
209 MemoryContextSwitchTo(dstate->change_cxt);
211 }
212
213 slot = dstate->slot;
214 ExecStoreHeapTuple(tuple, slot, false);
215
216 /*
217 * Loop over all attributes, and find out which ones we need to spill
218 * separately, to wit: each one that's a non-null varlena and stored
219 * out of line.
220 */
221 for (int i = 0; i < desc->natts; i++)
222 {
225
226 if (attr->attisdropped || attr->attlen != -1 ||
227 slot_attisnull(slot, i + 1))
228 continue;
229
230 slot_getsomeattrs(slot, i + 1);
231
232 /*
233 * This is a non-null varlena datum, but we only care if it's
234 * out-of-line
235 */
238 continue;
239
240 /*
241 * We spill any indirect-external attributes separately from the
242 * heap tuple. Anything else is written as is.
243 */
246 else
247 {
248 /*
249 * Logical decoding should not produce "external expanded"
250 * attributes (those actually should never appear on disk), so
251 * only TOASTed attribute can be seen here.
252 *
253 * We get here if the table has external values but only
254 * in-line values are being updated now.
255 */
257 }
258 }
259
260 ExecClearTuple(slot);
261 }
262
263 /*
264 * First, write the original heap tuple, prefixed by its length. Note
265 * that the external-toast tag for each toasted attribute will be present
266 * in what we write, so that we know where to restore each one later.
267 */
268 BufFileWrite(file, &tuple->t_len, sizeof(tuple->t_len));
269 BufFileWrite(file, tuple->t_data, tuple->t_len);
270
271 /* Then, write the number of external attributes we found. */
273 BufFileWrite(file, &natt_ext, sizeof(natt_ext));
274
275 /* Finally, the attributes themselves, if any */
277 {
280 /* These attributes could be large, so free them right away */
282 }
283
284 /* Cleanup. */
286 MemoryContextReset(dstate->change_cxt);
287}
void BufFileWrite(BufFile *file, const void *ptr, size_t size)
Definition buffile.c:677
#define PG_USED_FOR_ASSERTS_ONLY
Definition c.h:249
#define Assert(condition)
Definition c.h:943
varlena * detoast_external_attr(varlena *attr)
Definition detoast.c:45
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
const TupleTableSlotOps TTSOpsHeapTuple
Definition execTuples.c:85
TupleTableSlot * ExecStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
static bool HeapTupleHasExternal(const HeapTupleData *tuple)
int i
Definition isn.c:77
List * lappend(List *list, void *datum)
Definition list.c:339
void MemoryContextReset(MemoryContext context)
Definition mcxt.c:406
void pfree(void *pointer)
Definition mcxt.c:1619
static char * errmsg
@ OUTPUT_PLUGIN_BINARY_OUTPUT
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138
static int list_length(const List *l)
Definition pg_list.h:152
#define NIL
Definition pg_list.h:68
#define foreach_ptr(type, var, lst)
Definition pg_list.h:501
static void repack_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition pgrepack.c:84
static void repack_shutdown(LogicalDecodingContext *ctx)
Definition pgrepack.c:64
static void repack_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition pgrepack.c:47
PG_MODULE_MAGIC
Definition pgrepack.c:20
static void repack_process_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition pgrepack.c:93
static void repack_store_change(LogicalDecodingContext *ctx, Relation relation, ConcurrentChangeKind kind, HeapTuple tuple)
Definition pgrepack.c:173
static void repack_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
Definition pgrepack.c:78
void _PG_output_plugin_init(OutputPluginCallbacks *cb)
Definition pgrepack.c:35
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
static int fb(int x)
#define RelationGetRelid(relation)
Definition rel.h:516
#define RelationGetDescr(relation)
Definition rel.h:542
@ REORDER_BUFFER_CHANGE_INSERT
@ REORDER_BUFFER_CHANGE_DELETE
@ REORDER_BUFFER_CHANGE_UPDATE
#define CHANGE_UPDATE_OLD
#define CHANGE_DELETE
#define CHANGE_UPDATE_NEW
char ConcurrentChangeKind
#define CHANGE_INSERT
ResourceOwner CurrentResourceOwner
Definition resowner.c:173
bool attisdropped
Definition tupdesc.h:78
uint32 t_len
Definition htup.h:64
HeapTupleHeader t_data
Definition htup.h:68
Definition pg_list.h:54
void * output_writer_private
Definition logical.h:81
void * output_plugin_private
Definition logical.h:76
List * output_plugin_options
Definition logical.h:59
LogicalDecodeStartupCB startup_cb
LogicalDecodeCommitCB commit_cb
LogicalDecodeBeginCB begin_cb
LogicalDecodeChangeCB change_cb
LogicalDecodeShutdownCB shutdown_cb
OutputPluginOutputType output_type
ReorderBufferChangeType action
struct ReorderBufferChange::@120::@121 tp
union ReorderBufferChange::@120 data
Datum * tts_values
Definition tuptable.h:131
Definition c.h:776
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:195
static void slot_getsomeattrs(TupleTableSlot *slot, int attnum)
Definition tuptable.h:376
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition tuptable.h:476
static bool slot_attisnull(TupleTableSlot *slot, int attnum)
Definition tuptable.h:403
static bool VARATT_IS_EXTERNAL_ONDISK(const void *PTR)
Definition varatt.h:361
static Size VARSIZE_ANY(const void *PTR)
Definition varatt.h:460
static bool VARATT_IS_EXTERNAL(const void *PTR)
Definition varatt.h:354
static bool VARATT_IS_EXTERNAL_INDIRECT(const void *PTR)
Definition varatt.h:368
uint64 XLogRecPtr
Definition xlogdefs.h:21