PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pgrepack.c File Reference
Include dependency graph for pgrepack.c:

Go to the source code of this file.

Functions

static void repack_startup (LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
 
static void repack_shutdown (LogicalDecodingContext *ctx)
 
static void repack_begin_txn (LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
 
static void repack_commit_txn (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void repack_process_change (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
static void repack_store_change (LogicalDecodingContext *ctx, Relation relation, ConcurrentChangeKind kind, HeapTuple tuple)
 
void _PG_output_plugin_init (OutputPluginCallbacks *cb)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

◆ _PG_output_plugin_init()

void _PG_output_plugin_init ( OutputPluginCallbacks cb)

Definition at line 35 of file pgrepack.c.

36{
42}
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
static void repack_process_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition pgrepack.c:93
static void repack_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
Definition pgrepack.c:78
LogicalDecodeStartupCB startup_cb
LogicalDecodeCommitCB commit_cb
LogicalDecodeBeginCB begin_cb
LogicalDecodeChangeCB change_cb
LogicalDecodeShutdownCB shutdown_cb

References OutputPluginCallbacks::begin_cb, OutputPluginCallbacks::change_cb, OutputPluginCallbacks::commit_cb, repack_begin_txn(), repack_commit_txn(), repack_process_change(), repack_shutdown(), repack_startup(), OutputPluginCallbacks::shutdown_cb, and OutputPluginCallbacks::startup_cb.

◆ repack_begin_txn()

static void repack_begin_txn ( LogicalDecodingContext ctx,
ReorderBufferTXN txn 
)
static

Definition at line 78 of file pgrepack.c.

79{
80}

Referenced by _PG_output_plugin_init().

◆ repack_commit_txn()

static void repack_commit_txn ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
XLogRecPtr  commit_lsn 
)
static

Definition at line 84 of file pgrepack.c.

86{
87}

Referenced by _PG_output_plugin_init().

◆ repack_process_change()

static void repack_process_change ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
Relation  relation,
ReorderBufferChange change 
)
static

Definition at line 93 of file pgrepack.c.

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}
#define PG_USED_FOR_ASSERTS_ONLY
Definition c.h:249
#define Assert(condition)
Definition c.h:943
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
static void repack_store_change(LogicalDecodingContext *ctx, Relation relation, ConcurrentChangeKind kind, HeapTuple tuple)
Definition pgrepack.c:173
static int fb(int x)
#define RelationGetRelid(relation)
Definition rel.h:516
@ REORDER_BUFFER_CHANGE_INSERT
@ REORDER_BUFFER_CHANGE_DELETE
@ REORDER_BUFFER_CHANGE_UPDATE
#define CHANGE_UPDATE_OLD
#define CHANGE_DELETE
#define CHANGE_UPDATE_NEW
#define CHANGE_INSERT
void * output_writer_private
Definition logical.h:81
ReorderBufferChangeType action
struct ReorderBufferChange::@120::@121 tp
union ReorderBufferChange::@120 data

References ReorderBufferChange::action, Assert, CHANGE_DELETE, CHANGE_INSERT, CHANGE_UPDATE_NEW, CHANGE_UPDATE_OLD, ReorderBufferChange::data, elog, ERROR, fb(), ReorderBufferChange::newtuple, ReorderBufferChange::oldtuple, LogicalDecodingContext::output_writer_private, PG_USED_FOR_ASSERTS_ONLY, RelationGetRelid, REORDER_BUFFER_CHANGE_DELETE, REORDER_BUFFER_CHANGE_INSERT, REORDER_BUFFER_CHANGE_UPDATE, repack_store_change(), and ReorderBufferChange::tp.

Referenced by _PG_output_plugin_init().

◆ repack_shutdown()

static void repack_shutdown ( LogicalDecodingContext ctx)
static

Definition at line 64 of file pgrepack.c.

65{
66}

Referenced by _PG_output_plugin_init().

◆ repack_startup()

static void repack_startup ( LogicalDecodingContext ctx,
OutputPluginOptions opt,
bool  is_init 
)
static

Definition at line 47 of file pgrepack.c.

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}
int errcode(int sqlerrcode)
Definition elog.c:875
#define ereport(elevel,...)
Definition elog.h:152
static char * errmsg
@ OUTPUT_PLUGIN_BINARY_OUTPUT
#define NIL
Definition pg_list.h:68
void * output_plugin_private
Definition logical.h:76
List * output_plugin_options
Definition logical.h:59
OutputPluginOutputType output_type

References ereport, errcode(), errmsg, ERROR, fb(), NIL, OUTPUT_PLUGIN_BINARY_OUTPUT, LogicalDecodingContext::output_plugin_options, LogicalDecodingContext::output_plugin_private, and OutputPluginOptions::output_type.

Referenced by _PG_output_plugin_init().

◆ repack_store_change()

static void repack_store_change ( LogicalDecodingContext ctx,
Relation  relation,
ConcurrentChangeKind  kind,
HeapTuple  tuple 
)
static

Definition at line 173 of file pgrepack.c.

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
varlena * detoast_external_attr(varlena *attr)
Definition detoast.c:45
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 MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138
static int list_length(const List *l)
Definition pg_list.h:152
#define foreach_ptr(type, var, lst)
Definition pg_list.h:501
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
#define RelationGetDescr(relation)
Definition rel.h:542
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
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

References Assert, CompactAttribute::attisdropped, CompactAttribute::attlen, BufFileWrite(), CurrentResourceOwner, DatumGetPointer(), detoast_external_attr(), ExecClearTuple(), ExecStoreHeapTuple(), fb(), foreach_ptr, HeapTupleHasExternal(), i, lappend(), list_length(), MakeSingleTupleTableSlot(), MemoryContextReset(), MemoryContextSwitchTo(), TupleDescData::natts, NIL, LogicalDecodingContext::output_writer_private, pfree(), RelationGetDescr, slot_attisnull(), slot_getsomeattrs(), HeapTupleData::t_data, HeapTupleData::t_len, TupleTableSlot::tts_values, TTSOpsHeapTuple, TupleDescCompactAttr(), VARATT_IS_EXTERNAL(), VARATT_IS_EXTERNAL_INDIRECT(), VARATT_IS_EXTERNAL_ONDISK(), and VARSIZE_ANY().

Referenced by repack_process_change().

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 20 of file pgrepack.c.