Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * repack_worker.c
4 : : * Implementation of the background worker for ad-hoc logical decoding
5 : : * during REPACK (CONCURRENTLY).
6 : : *
7 : : *
8 : : * Copyright (c) 2026, PostgreSQL Global Development Group
9 : : *
10 : : *
11 : : * IDENTIFICATION
12 : : * src/backend/commands/repack_worker.c
13 : : *
14 : : *-------------------------------------------------------------------------
15 : : */
16 : : #include "postgres.h"
17 : :
18 : : #include "access/table.h"
19 : : #include "access/xlog_internal.h"
20 : : #include "access/xlogutils.h"
21 : : #include "access/xlogwait.h"
22 : : #include "commands/progress.h"
23 : : #include "commands/repack.h"
24 : : #include "commands/repack_internal.h"
25 : : #include "libpq/libpq.h"
26 : : #include "libpq/pqmq.h"
27 : : #include "replication/snapbuild.h"
28 : : #include "storage/ipc.h"
29 : : #include "storage/proc.h"
30 : : #include "storage/procsignal.h"
31 : : #include "tcop/tcopprot.h"
32 : : #include "utils/guc.h"
33 : : #include "utils/memutils.h"
34 : :
35 : : #define PGREPACK_PLUGIN "pgrepack"
36 : :
37 : : static void RepackWorkerShutdown(int code, Datum arg);
38 : : static void set_repack_worker_setup_phase(DecodingWorkerShared *shared,
39 : : int setup_phase);
40 : : static LogicalDecodingContext *repack_setup_logical_decoding(Oid relid,
41 : : DecodingWorkerShared *shared);
42 : : static void repack_cleanup_logical_decoding(LogicalDecodingContext *ctx);
43 : : static void export_initial_snapshot(Snapshot snapshot,
44 : : DecodingWorkerShared *shared);
45 : : static bool decode_concurrent_changes(LogicalDecodingContext *ctx,
46 : : DecodingWorkerShared *shared);
47 : :
48 : : /* Is this process a REPACK worker? */
49 : : static bool am_repack_worker = false;
50 : :
51 : : /* The WAL segment being decoded. */
52 : : static XLogSegNo repack_current_segment = 0;
53 : :
54 : : /*
55 : : * Keep track of the table we're processing, to skip logical decoding of data
56 : : * from other relations.
57 : : */
58 : : static RelFileLocator repacked_rel_locator = {.relNumber = InvalidOid};
59 : : static RelFileLocator repacked_rel_toast_locator = {.relNumber = InvalidOid};
60 : :
61 : :
62 : : /* REPACK decoding worker entry point */
63 : : void
236 alvherre@kurilemu.de 64 :CBC 13 : RepackWorkerMain(Datum main_arg)
65 : : {
66 : : dsm_segment *seg;
67 : : DecodingWorkerShared *shared;
68 : : shm_mq *mq;
69 : : shm_mq_handle *mqh;
70 : : LogicalDecodingContext *decoding_ctx;
71 : : SharedFileSet *sfs;
72 : : Snapshot snapshot;
73 : : char buf[32];
74 : :
75 : 13 : am_repack_worker = true;
76 : :
77 : 13 : BackgroundWorkerUnblockSignals();
78 : :
79 : 13 : seg = dsm_attach(DatumGetUInt32(main_arg));
80 [ - + ]: 13 : if (seg == NULL)
236 alvherre@kurilemu.de 81 [ # # ]:UBC 0 : ereport(ERROR,
82 : : errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
83 : : errmsg("could not map dynamic shared memory segment"));
84 : :
236 alvherre@kurilemu.de 85 :CBC 13 : shared = (DecodingWorkerShared *) dsm_segment_address(seg);
86 : :
87 : : /* Arrange to signal the steering process if we exit. */
72 88 : 13 : before_shmem_exit(RepackWorkerShutdown, PointerGetDatum(seg));
89 : :
90 : : /*
91 : : * Join locking group - see the comments around the call of
92 : : * start_repack_decoding_worker().
93 : : */
94 [ - + ]: 13 : if (!BecomeLockGroupMember(GetPGProcByNumber(shared->backend_proc_number),
95 : : shared->backend_pid))
236 alvherre@kurilemu.de 96 :UBC 0 : return; /* The leader is not running anymore. */
97 : :
98 : : /*
99 : : * Setup a queue to send error messages to the backend that launched this
100 : : * worker.
101 : : */
236 alvherre@kurilemu.de 102 :CBC 13 : mq = (shm_mq *) (char *) BUFFERALIGN(shared->error_queue);
103 : 13 : shm_mq_set_sender(mq, MyProc);
104 : 13 : mqh = shm_mq_attach(mq, seg, NULL);
105 : 13 : pq_redirect_to_shm_mq(seg, mqh);
106 : 13 : pq_set_parallel_leader(shared->backend_pid,
107 : : shared->backend_proc_number);
108 : :
109 : : /*
110 : : * Connect to the database, skipping the connection authorization checks
111 : : * as parallel workers do. Note that we run as the owner of the table
112 : : * being repacked, who need not be able to log in or connect; the leader
113 : : * checked the invoking user's privileges before starting us.
114 : : */
222 115 : 13 : BackgroundWorkerInitializeConnectionByOid(shared->dbid, shared->roleid,
116 : : BGWORKER_BYPASS_ALLOWCONN |
117 : : BGWORKER_BYPASS_ROLELOGINCHECK);
118 : :
119 : : /* Adopt the steering backend's relevant settings. */
72 120 : 13 : snprintf(buf, sizeof(buf), "%d", shared->lock_timeout);
121 : 13 : SetConfigOption("lock_timeout", buf, PGC_SUSET, PGC_S_OVERRIDE);
122 : 13 : snprintf(buf, sizeof(buf), "%d", shared->transaction_timeout);
123 : 13 : SetConfigOption("transaction_timeout", buf, PGC_SUSET, PGC_S_OVERRIDE);
17 124 : 13 : snprintf(buf, sizeof(buf), "%d", shared->temp_file_limit);
125 : 13 : SetConfigOption("temp_file_limit", buf, PGC_SUSET, PGC_S_OVERRIDE);
126 : :
127 : : /*
128 : : * Transaction is needed to open relation, and it also provides us with a
129 : : * resource owner.
130 : : */
236 131 : 13 : StartTransactionCommand();
132 : :
133 : 13 : shared = (DecodingWorkerShared *) dsm_segment_address(seg);
134 : :
135 : : /*
136 : : * Not sure the spinlock is needed here - the backend should not change
137 : : * anything in the shared memory until we have serialized the snapshot.
138 : : */
139 : 13 : SpinLockAcquire(&shared->mutex);
140 [ - + ]: 13 : Assert(!XLogRecPtrIsValid(shared->lsn_upto));
141 : 13 : sfs = &shared->sfs;
142 : 13 : SpinLockRelease(&shared->mutex);
143 : :
144 : 13 : SharedFileSetAttach(sfs, seg);
145 : :
146 : : /*
147 : : * Prepare to capture the concurrent data changes ourselves.
148 : : */
26 msawada@postgresql.o 149 :GNC 13 : decoding_ctx = repack_setup_logical_decoding(shared->relid, shared);
150 : :
151 : : /* Announce that we're ready. */
236 alvherre@kurilemu.de 152 :CBC 13 : SpinLockAcquire(&shared->mutex);
153 : 13 : shared->initialized = true;
154 : 13 : SpinLockRelease(&shared->mutex);
155 : 13 : ConditionVariableSignal(&shared->cv);
156 : :
157 : : /* There doesn't seem to a nice API to set these */
158 : 13 : XactIsoLevel = XACT_REPEATABLE_READ;
159 : 13 : XactReadOnly = true;
160 : :
161 : : /* Build the initial snapshot and export it. */
162 : 13 : snapshot = SnapBuildInitialSnapshot(decoding_ctx->snapshot_builder);
163 : 13 : export_initial_snapshot(snapshot, shared);
164 : :
165 : : /*
166 : : * Only historic snapshots should be used now. Do not let us restrict the
167 : : * progress of xmin horizon.
168 : : */
169 : 13 : InvalidateCatalogSnapshot();
170 : :
171 : : for (;;)
172 : 13 : {
173 : 26 : bool stop = decode_concurrent_changes(decoding_ctx, shared);
174 : :
175 [ + + ]: 26 : if (stop)
176 : 13 : break;
177 : : }
178 : :
179 : : /* Clean up and report termination to our steering process */
180 : 13 : repack_cleanup_logical_decoding(decoding_ctx);
181 : 13 : CommitTransactionCommand();
72 182 : 13 : pq_putmessage(PqRepackMsg_Terminate, NULL, 0);
183 : : }
184 : :
185 : : /*
186 : : * Make sure the repack steering process tries to read from our error queue one
187 : : * more time. This guards against the case where we exit uncleanly without
188 : : * sending an ErrorResponse to the leader, for example because some code calls
189 : : * proc_exit directly.
190 : : */
191 : : static void
236 192 : 13 : RepackWorkerShutdown(int code, Datum arg)
193 : : {
194 : : dsm_segment *seg;
195 : : DecodingWorkerShared *shared;
196 : : pid_t pid;
197 : : ProcNumber procno;
198 : :
72 199 : 13 : seg = (dsm_segment *) DatumGetPointer(arg);
200 : 13 : shared = (DecodingWorkerShared *) dsm_segment_address(seg);
201 : 13 : pid = shared->backend_pid;
202 : 13 : procno = shared->backend_proc_number;
203 : :
204 : : /*
205 : : * Detach from the shared memory segment before we signal the backend.
206 : : * Detaching also detaches the error message queue, and the backend learns
207 : : * that we are gone by reading that queue when it handles our signal. If
208 : : * we signaled first, the backend could read the queue while it still
209 : : * looks attached, and nothing would make it read again.
210 : : */
211 : 13 : dsm_detach(seg);
212 : :
213 : 13 : SendProcSignal(pid, PROCSIG_REPACK_MESSAGE, procno);
236 214 : 13 : }
215 : :
216 : : bool
217 : 2110 : AmRepackWorker(void)
218 : : {
219 : 2110 : return am_repack_worker;
220 : : }
221 : :
222 : : /*
223 : : * Tell the backend running REPACK which progress phase to report while we
224 : : * initialize the decoding.
225 : : */
226 : : static void
26 msawada@postgresql.o 227 :GNC 26 : set_repack_worker_setup_phase(DecodingWorkerShared *shared, int setup_phase)
228 : : {
229 : 26 : SpinLockAcquire(&shared->mutex);
230 : 26 : shared->setup_phase = setup_phase;
231 : 26 : SpinLockRelease(&shared->mutex);
232 : 26 : ConditionVariableSignal(&shared->cv);
233 : 26 : }
234 : :
235 : : /*
236 : : * This function is much like pg_create_logical_replication_slot() except that
237 : : * the new slot is neither released (if anyone else could read changes from
238 : : * our slot, we could miss changes other backends do while we copy the
239 : : * existing data into temporary table), nor persisted (it's easier to handle
240 : : * crash by restarting all the work from scratch).
241 : : */
242 : : static LogicalDecodingContext *
243 : 13 : repack_setup_logical_decoding(Oid relid, DecodingWorkerShared *shared)
244 : : {
245 : : Relation rel;
246 : : Oid toastrelid;
247 : : LogicalDecodingContext *ctx;
248 : : char slotname[NAMEDATALEN];
249 : : RepackDecodingState *dstate;
250 : : MemoryContext oldcxt;
251 : :
252 : : /*
253 : : * REPACK CONCURRENTLY is not allowed in a transaction block, so this
254 : : * should never fire.
255 : : */
236 alvherre@kurilemu.de 256 [ - + ]:CBC 13 : Assert(!TransactionIdIsValid(GetTopTransactionIdIfAny()));
257 : :
258 : : /* Make sure we can use logical decoding */
235 259 : 13 : CheckLogicalDecodingRequirements(true);
260 : :
261 : : /*
262 : : * Create the replication slot we'll use, and enable logical decoding in
263 : : * case it isn't already on.
264 : : *
265 : : * Make the slot RS_TEMPORARY so that it's removed on ERROR. A backend
266 : : * cannot execute multiple REPACK commands at a time, so the PID is enough
267 : : * to make the slot name unique.
268 : : */
172 269 : 13 : snprintf(slotname, NAMEDATALEN, "pg_repack_%d", MyProcPid);
270 : 13 : ReplicationSlotCreate(slotname, true, RS_TEMPORARY, false, true,
271 : : false, false);
26 msawada@postgresql.o 272 :GNC 13 : set_repack_worker_setup_phase(shared,
273 : : PROGRESS_REPACK_PHASE_ENABLE_LOGICAL_DECODING);
236 alvherre@kurilemu.de 274 :CBC 13 : EnsureLogicalDecodingEnabled();
275 : :
276 : : /*
277 : : * Set up repacked_rel_locator and repacked_rel_toast_locator, which we
278 : : * use to skip decoding of unrelated relations. The steering backend
279 : : * already holds ShareUpdateExclusiveLock on both these tables, so we
280 : : * don't need any locks here, other than to avoid relation_open()'s
281 : : * assertion that we hold one.
282 : : */
283 : 13 : rel = table_open(relid, AccessShareLock);
284 : 13 : repacked_rel_locator = rel->rd_locator;
285 : 13 : toastrelid = rel->rd_rel->reltoastrelid;
286 [ + + ]: 13 : if (OidIsValid(toastrelid))
287 : : {
288 : : Relation toastrel;
289 : :
290 : : /* Avoid logical decoding of other TOAST relations. */
291 : 8 : toastrel = table_open(toastrelid, AccessShareLock);
292 : 8 : repacked_rel_toast_locator = toastrel->rd_locator;
293 : 8 : table_close(toastrel, AccessShareLock);
294 : : }
295 : 13 : table_close(rel, AccessShareLock);
296 : :
26 msawada@postgresql.o 297 :GNC 13 : set_repack_worker_setup_phase(shared,
298 : : PROGRESS_REPACK_PHASE_INIT_LOGICAL_DECODING);
299 : :
300 : : /*
301 : : * Set up our logical decoding context. We initially use the blocking
302 : : * read_local_xlog_page until we find the start point, and switch to the
303 : : * non-blocking interface afterwards.
304 : : */
172 alvherre@kurilemu.de 305 :CBC 13 : ctx = CreateInitDecodingContext(PGREPACK_PLUGIN,
306 : : NIL,
307 : : true,
308 : : true,
309 : : InvalidXLogRecPtr,
310 : 13 : XL_ROUTINE(.page_read = read_local_xlog_page,
311 : : .segment_open = wal_segment_open,
312 : : .segment_close = wal_segment_close),
313 : : NULL, NULL, NULL);
314 : :
315 : : /* Complete setup of output_writer_private */
316 : 13 : dstate = (RepackDecodingState *) ctx->output_writer_private;
317 : 13 : dstate->relid = relid;
318 : 13 : dstate->worker_cxt = CurrentMemoryContext;
319 : 13 : dstate->worker_resowner = CurrentResourceOwner;
320 : :
321 : : /* We don't have control on fast_forward, but verify it's sane */
322 [ - + ]: 13 : Assert(!ctx->fast_forward);
323 : :
324 : : /* Find our decoding starting point. */
325 : 13 : DecodingContextFindStartpoint(ctx);
326 : :
327 : : /* From this point on, we need non-blocking WAL reads */
328 : 13 : ctx->reader->routine.page_read = read_local_xlog_page_no_wait;
329 : :
330 : : /*
331 : : * Initialize repack_current_segment so that we can notice WAL segment
332 : : * boundaries.
333 : : */
236 334 : 13 : XLByteToSeg(ctx->reader->EndRecPtr, repack_current_segment,
335 : : wal_segment_size);
336 : :
337 : : /*
338 : : * Set up our reader private state to let the page-read callback notify
339 : : * when end-of-WAL has been reached. This lives in the same context as
340 : : * the logical decoding itself.
341 : : */
172 342 : 13 : oldcxt = MemoryContextSwitchTo(ctx->context);
236 343 : 13 : ctx->reader->private_data = palloc0_object(ReadLocalXLogPageNoWaitPrivate);
344 : 13 : MemoryContextSwitchTo(oldcxt);
345 : :
346 : 13 : return ctx;
347 : : }
348 : :
349 : : static void
350 : 13 : repack_cleanup_logical_decoding(LogicalDecodingContext *ctx)
351 : : {
352 : : RepackDecodingState *dstate;
353 : :
354 : 13 : dstate = (RepackDecodingState *) ctx->output_writer_private;
355 [ + + ]: 13 : if (dstate->slot)
356 : 4 : ExecDropSingleTupleTableSlot(dstate->slot);
357 : :
358 : 13 : FreeDecodingContext(ctx);
185 359 : 13 : ReplicationSlotDropAcquired(true);
236 360 : 13 : }
361 : :
362 : : /*
363 : : * Make snapshot available to the backend that launched the decoding worker.
364 : : */
365 : : static void
366 : 13 : export_initial_snapshot(Snapshot snapshot, DecodingWorkerShared *shared)
367 : : {
368 : : char fname[MAXPGPATH];
369 : : BufFile *file;
370 : : Size snap_size;
371 : : char *snap_space;
372 : :
373 : 13 : snap_size = EstimateSnapshotSpace(snapshot);
374 : 13 : snap_space = (char *) palloc(snap_size);
375 : 13 : SerializeSnapshot(snapshot, snap_space);
376 : :
377 : 13 : DecodingWorkerFileName(fname, shared->relid, shared->last_exported + 1);
378 : 13 : file = BufFileCreateFileSet(&shared->sfs.fs, fname);
379 : : /* To make restoration easier, write the snapshot size first. */
380 : 13 : BufFileWrite(file, &snap_size, sizeof(snap_size));
381 : 13 : BufFileWrite(file, snap_space, snap_size);
382 : 13 : BufFileClose(file);
235 383 : 13 : pfree(snap_space);
384 : :
385 : : /* Increase the counter to tell the backend that the file is available. */
236 386 : 13 : SpinLockAcquire(&shared->mutex);
387 : 13 : shared->last_exported++;
388 : 13 : SpinLockRelease(&shared->mutex);
389 : 13 : ConditionVariableSignal(&shared->cv);
390 : 13 : }
391 : :
392 : : /*
393 : : * Decode logical changes from the WAL sequence and store them to a file.
394 : : *
395 : : * If true is returned, there is no more work for the worker.
396 : : */
397 : : static bool
398 : 26 : decode_concurrent_changes(LogicalDecodingContext *ctx,
399 : : DecodingWorkerShared *shared)
400 : : {
401 : : RepackDecodingState *dstate;
402 : : XLogRecPtr lsn_upto;
403 : : bool done;
404 : : char fname[MAXPGPATH];
405 : :
406 : 26 : dstate = (RepackDecodingState *) ctx->output_writer_private;
407 : :
408 : : /* Open the output file. */
409 : 26 : DecodingWorkerFileName(fname, shared->relid, shared->last_exported + 1);
410 : 26 : dstate->file = BufFileCreateFileSet(&shared->sfs.fs, fname);
411 : :
412 : 26 : SpinLockAcquire(&shared->mutex);
413 : 26 : lsn_upto = shared->lsn_upto;
414 : 26 : done = shared->done;
415 : 26 : SpinLockRelease(&shared->mutex);
416 : :
417 : : while (true)
418 : 5685 : {
419 : : XLogRecord *record;
420 : : XLogSegNo segno_new;
421 : 5711 : char *errm = NULL;
422 : : XLogRecPtr end_lsn;
423 : :
424 [ - + ]: 5711 : CHECK_FOR_INTERRUPTS();
425 : :
426 : 5711 : record = XLogReadRecord(ctx->reader, &errm);
427 [ + + ]: 5711 : if (record)
428 : : {
429 : 2987 : LogicalDecodingProcessRecord(ctx, ctx->reader);
430 : :
431 : : /*
432 : : * We want to allow WAL to be recycled while REPACK is running.
433 : : *
434 : : * In normal usage of a replication slot, we need to be very
435 : : * careful not to advance the LSN until it's been confirmed as
436 : : * received by the remote. In REPACK's case, this is not needed:
437 : : * REPACK will never try to replay the same WAL after a crash, and
438 : : * if there _is_ a crash, the whole REPACK has to be started from
439 : : * scratch anyway.
440 : : *
441 : : * So here we disregard the careful LSN tracking and just move the
442 : : * LSN locations forward to what we've processed. Note that it
443 : : * would be bogus to move the xmin forward, though, so we don't
444 : : * touch that.
445 : : *
446 : : * This can be done on whatever schedule is convenient, but in
447 : : * order not to cause unnecessary load, we only do it as we cross
448 : : * each WAL segment boundary.
449 : : */
450 : 2987 : end_lsn = ctx->reader->EndRecPtr;
451 : 2987 : XLByteToSeg(end_lsn, segno_new, wal_segment_size);
452 [ - + ]: 2987 : if (segno_new != repack_current_segment)
453 : : {
182 alvherre@kurilemu.de 454 :UBC 0 : LogicalIncreaseRestartDecodingForSlot(end_lsn, end_lsn);
236 455 : 0 : LogicalConfirmReceivedLocation(end_lsn);
131 456 [ # # ]: 0 : elog(DEBUG1, "REPACK: confirmed receive location %X/%08X",
457 : : LSN_FORMAT_ARGS(end_lsn));
236 458 : 0 : repack_current_segment = segno_new;
459 : : }
460 : : }
461 : : else
462 : : {
463 : : ReadLocalXLogPageNoWaitPrivate *priv;
464 : :
236 alvherre@kurilemu.de 465 [ - + ]:CBC 2724 : if (errm)
236 alvherre@kurilemu.de 466 [ # # ]:UBC 0 : ereport(ERROR,
467 : : errcode_for_file_access(),
468 : : errmsg("could not read WAL from timeline %u at %X/%08X: %s",
469 : : ctx->reader->currTLI,
470 : : LSN_FORMAT_ARGS(ctx->reader->EndRecPtr),
471 : : errm));
472 : :
473 : : /*
474 : : * In the decoding loop we do not want to get blocked when there
475 : : * is no more WAL available, otherwise the loop would become
476 : : * uninterruptible.
477 : : */
236 alvherre@kurilemu.de 478 :CBC 2724 : priv = (ReadLocalXLogPageNoWaitPrivate *) ctx->reader->private_data;
479 [ + - ]: 2724 : if (priv->end_of_wal)
480 : : /* Do not miss the end of WAL condition next time. */
481 : 2724 : priv->end_of_wal = false;
482 : : else
236 alvherre@kurilemu.de 483 [ # # ]:UBC 0 : ereport(ERROR,
484 : : errcode(ERRCODE_DATA_CORRUPTED),
485 : : errmsg("could not read WAL record"));
486 : : }
487 : :
488 : : /*
489 : : * Whether we could read new record or not, keep checking if
490 : : * 'lsn_upto' was specified.
491 : : */
236 alvherre@kurilemu.de 492 [ + + ]:CBC 5711 : if (!XLogRecPtrIsValid(lsn_upto))
493 : : {
494 : 5354 : SpinLockAcquire(&shared->mutex);
495 : 5354 : lsn_upto = shared->lsn_upto;
496 : : /* 'done' should be set at the same time as 'lsn_upto' */
497 : 5354 : done = shared->done;
498 : 5354 : SpinLockRelease(&shared->mutex);
499 : : }
500 [ + + ]: 5711 : if (XLogRecPtrIsValid(lsn_upto) &&
501 [ + + ]: 383 : ctx->reader->EndRecPtr >= lsn_upto)
502 : 26 : break;
503 : :
504 [ + + ]: 5685 : if (record == NULL)
505 : : {
75 akorotkov@postgresql 506 : 2709 : int timeout = 0;
507 : : WaitLSNResult res;
508 : :
509 : : /*
510 : : * Before we retry reading, wait until new WAL is flushed.
511 : : *
512 : : * There is a race condition such that the backend executing
513 : : * REPACK determines 'lsn_upto', but before it sets the shared
514 : : * variable, we reach the end of WAL. In that case we'd need to
515 : : * wait until the next WAL flush (unrelated to REPACK). Although
516 : : * that should not be a problem in a busy system, it might be
517 : : * noticeable in other cases, including regression tests (which
518 : : * are not necessarily executed in parallel). Therefore it makes
519 : : * sense to use timeout.
520 : : *
521 : : * If lsn_upto is valid, WAL records having LSN lower than that
522 : : * should already have been flushed to disk.
523 : : */
236 alvherre@kurilemu.de 524 [ + - ]: 2709 : if (!XLogRecPtrIsValid(lsn_upto))
75 akorotkov@postgresql 525 : 2709 : timeout = 100;
236 alvherre@kurilemu.de 526 : 2709 : res = WaitForLSN(WAIT_LSN_TYPE_PRIMARY_FLUSH,
527 : 2709 : ctx->reader->EndRecPtr + 1,
528 : : timeout);
529 [ + + - + ]: 2709 : if (res != WAIT_LSN_RESULT_SUCCESS &&
530 : : res != WAIT_LSN_RESULT_TIMEOUT)
236 alvherre@kurilemu.de 531 [ # # ]:UBC 0 : ereport(ERROR,
532 : : errcode(ERRCODE_INTERNAL_ERROR),
533 : : errmsg("waiting for WAL failed"));
534 : : }
535 : : }
536 : :
537 : : /*
538 : : * Close the file so we can make it available to the backend.
539 : : */
236 alvherre@kurilemu.de 540 :CBC 26 : BufFileClose(dstate->file);
541 : 26 : dstate->file = NULL;
542 : 26 : SpinLockAcquire(&shared->mutex);
543 : 26 : shared->lsn_upto = InvalidXLogRecPtr;
544 : 26 : shared->last_exported++;
545 : 26 : SpinLockRelease(&shared->mutex);
546 : 26 : ConditionVariableSignal(&shared->cv);
547 : :
548 : 26 : return done;
549 : : }
550 : :
551 : : /*
552 : : * Does the WAL record contain a data change that this backend does not need
553 : : * to decode on behalf of REPACK (CONCURRENTLY)?
554 : : */
555 : : bool
556 : 1611565 : change_useless_for_repack(XLogRecordBuffer *buf)
557 : : {
558 : 1611565 : XLogReaderState *r = buf->record;
559 : : RelFileLocator locator;
560 : :
561 : : /* TOAST locator should not be set unless the main is. */
562 [ + + - + ]: 1611565 : Assert(!OidIsValid(repacked_rel_toast_locator.relNumber) ||
563 : : OidIsValid(repacked_rel_locator.relNumber));
564 : :
565 : : /*
566 : : * Backends not involved in REPACK (CONCURRENTLY) should not do the
567 : : * filtering.
568 : : */
569 [ + + ]: 1611565 : if (!OidIsValid(repacked_rel_locator.relNumber))
570 : 1610694 : return false;
571 : :
572 : : /*
573 : : * If the record does not contain the block 0, it's probably not INSERT /
574 : : * UPDATE / DELETE. In any case, we do not have enough information to
575 : : * filter the change out.
576 : : */
577 [ - + ]: 871 : if (!XLogRecGetBlockTagExtended(r, 0, &locator, NULL, NULL, NULL))
236 alvherre@kurilemu.de 578 :UBC 0 : return false;
579 : :
580 : : /*
581 : : * Decode the change if it belongs to the table we are repacking, or if it
582 : : * belongs to its TOAST relation.
583 : : */
236 alvherre@kurilemu.de 584 [ + + + - :CBC 871 : if (RelFileLocatorEquals(locator, repacked_rel_locator))
+ - ]
585 : 57 : return false;
586 [ + + ]: 814 : if (OidIsValid(repacked_rel_toast_locator.relNumber) &&
587 [ + + + - : 692 : RelFileLocatorEquals(locator, repacked_rel_toast_locator))
+ - ]
588 : 112 : return false;
589 : :
590 : : /* Filter out changes of other tables. */
591 : 702 : return true;
592 : : }
|