Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
launcher.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 * launcher.c
3 * PostgreSQL logical replication worker launcher process
4 *
5 * Copyright (c) 2016-2025, PostgreSQL Global Development Group
6 *
7 * IDENTIFICATION
8 * src/backend/replication/logical/launcher.c
9 *
10 * NOTES
11 * This module contains the logical replication worker launcher which
12 * uses the background worker infrastructure to start the logical
13 * replication workers for every enabled subscription.
14 *
15 *-------------------------------------------------------------------------
16 */
17
18#include "postgres.h"
19
20#include "access/heapam.h"
21#include "access/htup.h"
22#include "access/htup_details.h"
23#include "access/tableam.h"
24#include "access/xact.h"
27#include "funcapi.h"
28#include "lib/dshash.h"
29#include "miscadmin.h"
30#include "pgstat.h"
31#include "postmaster/bgworker.h"
34#include "replication/origin.h"
35#include "replication/slot.h"
38#include "storage/ipc.h"
39#include "storage/proc.h"
40#include "storage/procarray.h"
41#include "tcop/tcopprot.h"
42#include "utils/builtins.h"
43#include "utils/memutils.h"
44#include "utils/pg_lsn.h"
45#include "utils/snapmgr.h"
46#include "utils/syscache.h"
47
48/* max sleep time between cycles (3min) */
49#define DEFAULT_NAPTIME_PER_CYCLE 180000L
50
51/* GUC variables */
55
57
58typedef struct LogicalRepCtxStruct
59{
60 /* Supervisor process. */
62
63 /* Hash table holding last start times of subscriptions' apply workers. */
66
67 /* Background workers. */
70
72
73/* an entry in the last-start-times shared hash table */
75{
76 Oid subid; /* OID of logrep subscription (hash key) */
77 TimestampTz last_start_time; /* last time its apply worker was started */
79
80/* parameters for the last-start-times shared hash table */
82 sizeof(Oid),
87 LWTRANCHE_LAUNCHER_HASH
88};
89
92
93static bool on_commit_launcher_wakeup = false;
94
95
96static void logicalrep_launcher_onexit(int code, Datum arg);
97static void logicalrep_worker_onexit(int code, Datum arg);
98static void logicalrep_worker_detach(void);
100static int logicalrep_pa_worker_count(Oid subid);
101static void logicalrep_launcher_attach_dshmem(void);
105static bool acquire_conflict_slot_if_exists(void);
106static void update_conflict_slot_xmin(TransactionId new_xmin);
107static void init_conflict_slot_xmin(void);
108
109
110/*
111 * Load the list of subscriptions.
112 *
113 * Only the fields interesting for worker start/stop functions are filled for
114 * each subscription.
115 */
116static List *
118{
119 List *res = NIL;
120 Relation rel;
121 TableScanDesc scan;
122 HeapTuple tup;
123 MemoryContext resultcxt;
124
125 /* This is the context that we will allocate our output data in */
126 resultcxt = CurrentMemoryContext;
127
128 /*
129 * Start a transaction so we can access pg_subscription.
130 */
132
133 rel = table_open(SubscriptionRelationId, AccessShareLock);
134 scan = table_beginscan_catalog(rel, 0, NULL);
135
137 {
139 Subscription *sub;
140 MemoryContext oldcxt;
141
142 /*
143 * Allocate our results in the caller's context, not the
144 * transaction's. We do this inside the loop, and restore the original
145 * context at the end, so that leaky things like heap_getnext() are
146 * not called in a potentially long-lived context.
147 */
148 oldcxt = MemoryContextSwitchTo(resultcxt);
149
150 sub = (Subscription *) palloc0(sizeof(Subscription));
151 sub->oid = subform->oid;
152 sub->dbid = subform->subdbid;
153 sub->owner = subform->subowner;
154 sub->enabled = subform->subenabled;
155 sub->name = pstrdup(NameStr(subform->subname));
156 sub->retaindeadtuples = subform->subretaindeadtuples;
157 sub->retentionactive = subform->subretentionactive;
158 /* We don't fill fields we are not interested in. */
159
160 res = lappend(res, sub);
161 MemoryContextSwitchTo(oldcxt);
162 }
163
164 table_endscan(scan);
166
168
169 return res;
170}
171
172/*
173 * Wait for a background worker to start up and attach to the shmem context.
174 *
175 * This is only needed for cleaning up the shared memory in case the worker
176 * fails to attach.
177 *
178 * Returns whether the attach was successful.
179 */
180static bool
182 uint16 generation,
184{
185 bool result = false;
186 bool dropped_latch = false;
187
188 for (;;)
189 {
190 BgwHandleStatus status;
191 pid_t pid;
192 int rc;
193
195
196 LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
197
198 /* Worker either died or has started. Return false if died. */
199 if (!worker->in_use || worker->proc)
200 {
201 result = worker->in_use;
202 LWLockRelease(LogicalRepWorkerLock);
203 break;
204 }
205
206 LWLockRelease(LogicalRepWorkerLock);
207
208 /* Check if worker has died before attaching, and clean up after it. */
209 status = GetBackgroundWorkerPid(handle, &pid);
210
211 if (status == BGWH_STOPPED)
212 {
213 LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
214 /* Ensure that this was indeed the worker we waited for. */
215 if (generation == worker->generation)
217 LWLockRelease(LogicalRepWorkerLock);
218 break; /* result is already false */
219 }
220
221 /*
222 * We need timeout because we generally don't get notified via latch
223 * about the worker attach. But we don't expect to have to wait long.
224 */
225 rc = WaitLatch(MyLatch,
227 10L, WAIT_EVENT_BGWORKER_STARTUP);
228
229 if (rc & WL_LATCH_SET)
230 {
233 dropped_latch = true;
234 }
235 }
236
237 /*
238 * If we had to clear a latch event in order to wait, be sure to restore
239 * it before exiting. Otherwise caller may miss events.
240 */
241 if (dropped_latch)
243
244 return result;
245}
246
247/*
248 * Walks the workers array and searches for one that matches given
249 * subscription id and relid.
250 *
251 * We are only interested in the leader apply worker or table sync worker.
252 */
254logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
255{
256 int i;
257 LogicalRepWorker *res = NULL;
258
259 Assert(LWLockHeldByMe(LogicalRepWorkerLock));
260
261 /* Search for attached worker for a given subscription id. */
262 for (i = 0; i < max_logical_replication_workers; i++)
263 {
265
266 /* Skip parallel apply workers. */
268 continue;
269
270 if (w->in_use && w->subid == subid && w->relid == relid &&
271 (!only_running || w->proc))
272 {
273 res = w;
274 break;
275 }
276 }
277
278 return res;
279}
280
281/*
282 * Similar to logicalrep_worker_find(), but returns a list of all workers for
283 * the subscription, instead of just one.
284 */
285List *
286logicalrep_workers_find(Oid subid, bool only_running, bool acquire_lock)
287{
288 int i;
289 List *res = NIL;
290
291 if (acquire_lock)
292 LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
293
294 Assert(LWLockHeldByMe(LogicalRepWorkerLock));
295
296 /* Search for attached worker for a given subscription id. */
297 for (i = 0; i < max_logical_replication_workers; i++)
298 {
300
301 if (w->in_use && w->subid == subid && (!only_running || w->proc))
302 res = lappend(res, w);
303 }
304
305 if (acquire_lock)
306 LWLockRelease(LogicalRepWorkerLock);
307
308 return res;
309}
310
311/*
312 * Start new logical replication background worker, if possible.
313 *
314 * Returns true on success, false on failure.
315 */
316bool
318 Oid dbid, Oid subid, const char *subname, Oid userid,
319 Oid relid, dsm_handle subworker_dsm,
320 bool retain_dead_tuples)
321{
323 BackgroundWorkerHandle *bgw_handle;
324 uint16 generation;
325 int i;
326 int slot = 0;
327 LogicalRepWorker *worker = NULL;
328 int nsyncworkers;
329 int nparallelapplyworkers;
331 bool is_tablesync_worker = (wtype == WORKERTYPE_TABLESYNC);
332 bool is_parallel_apply_worker = (wtype == WORKERTYPE_PARALLEL_APPLY);
333
334 /*----------
335 * Sanity checks:
336 * - must be valid worker type
337 * - tablesync workers are only ones to have relid
338 * - parallel apply worker is the only kind of subworker
339 * - The replication slot used in conflict detection is created when
340 * retain_dead_tuples is enabled
341 */
342 Assert(wtype != WORKERTYPE_UNKNOWN);
343 Assert(is_tablesync_worker == OidIsValid(relid));
344 Assert(is_parallel_apply_worker == (subworker_dsm != DSM_HANDLE_INVALID));
345 Assert(!retain_dead_tuples || MyReplicationSlot);
346
348 (errmsg_internal("starting logical replication worker for subscription \"%s\"",
349 subname)));
350
351 /* Report this after the initial starting message for consistency. */
354 (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
355 errmsg("cannot start logical replication workers when \"max_active_replication_origins\" is 0")));
356
357 /*
358 * We need to do the modification of the shared memory under lock so that
359 * we have consistent view.
360 */
361 LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
362
363retry:
364 /* Find unused worker slot. */
365 for (i = 0; i < max_logical_replication_workers; i++)
366 {
368
369 if (!w->in_use)
370 {
371 worker = w;
372 slot = i;
373 break;
374 }
375 }
376
377 nsyncworkers = logicalrep_sync_worker_count(subid);
378
380
381 /*
382 * If we didn't find a free slot, try to do garbage collection. The
383 * reason we do this is because if some worker failed to start up and its
384 * parent has crashed while waiting, the in_use state was never cleared.
385 */
386 if (worker == NULL || nsyncworkers >= max_sync_workers_per_subscription)
387 {
388 bool did_cleanup = false;
389
390 for (i = 0; i < max_logical_replication_workers; i++)
391 {
393
394 /*
395 * If the worker was marked in use but didn't manage to attach in
396 * time, clean it up.
397 */
398 if (w->in_use && !w->proc &&
401 {
403 "logical replication worker for subscription %u took too long to start; canceled",
404 w->subid);
405
407 did_cleanup = true;
408 }
409 }
410
411 if (did_cleanup)
412 goto retry;
413 }
414
415 /*
416 * We don't allow to invoke more sync workers once we have reached the
417 * sync worker limit per subscription. So, just return silently as we
418 * might get here because of an otherwise harmless race condition.
419 */
420 if (is_tablesync_worker && nsyncworkers >= max_sync_workers_per_subscription)
421 {
422 LWLockRelease(LogicalRepWorkerLock);
423 return false;
424 }
425
426 nparallelapplyworkers = logicalrep_pa_worker_count(subid);
427
428 /*
429 * Return false if the number of parallel apply workers reached the limit
430 * per subscription.
431 */
432 if (is_parallel_apply_worker &&
433 nparallelapplyworkers >= max_parallel_apply_workers_per_subscription)
434 {
435 LWLockRelease(LogicalRepWorkerLock);
436 return false;
437 }
438
439 /*
440 * However if there are no more free worker slots, inform user about it
441 * before exiting.
442 */
443 if (worker == NULL)
444 {
445 LWLockRelease(LogicalRepWorkerLock);
447 (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
448 errmsg("out of logical replication worker slots"),
449 errhint("You might need to increase \"%s\".", "max_logical_replication_workers")));
450 return false;
451 }
452
453 /* Prepare the worker slot. */
454 worker->type = wtype;
455 worker->launch_time = now;
456 worker->in_use = true;
457 worker->generation++;
458 worker->proc = NULL;
459 worker->dbid = dbid;
460 worker->userid = userid;
461 worker->subid = subid;
462 worker->relid = relid;
463 worker->relstate = SUBREL_STATE_UNKNOWN;
465 worker->stream_fileset = NULL;
466 worker->leader_pid = is_parallel_apply_worker ? MyProcPid : InvalidPid;
467 worker->parallel_apply = is_parallel_apply_worker;
468 worker->oldest_nonremovable_xid = retain_dead_tuples
471 worker->last_lsn = InvalidXLogRecPtr;
476
477 /* Before releasing lock, remember generation for future identification. */
478 generation = worker->generation;
479
480 LWLockRelease(LogicalRepWorkerLock);
481
482 /* Register the new dynamic worker. */
483 memset(&bgw, 0, sizeof(bgw));
487 snprintf(bgw.bgw_library_name, MAXPGPATH, "postgres");
488
489 switch (worker->type)
490 {
491 case WORKERTYPE_APPLY:
492 snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyWorkerMain");
494 "logical replication apply worker for subscription %u",
495 subid);
496 snprintf(bgw.bgw_type, BGW_MAXLEN, "logical replication apply worker");
497 break;
498
500 snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ParallelApplyWorkerMain");
502 "logical replication parallel apply worker for subscription %u",
503 subid);
504 snprintf(bgw.bgw_type, BGW_MAXLEN, "logical replication parallel worker");
505
506 memcpy(bgw.bgw_extra, &subworker_dsm, sizeof(dsm_handle));
507 break;
508
510 snprintf(bgw.bgw_function_name, BGW_MAXLEN, "TablesyncWorkerMain");
512 "logical replication tablesync worker for subscription %u sync %u",
513 subid,
514 relid);
515 snprintf(bgw.bgw_type, BGW_MAXLEN, "logical replication tablesync worker");
516 break;
517
519 /* Should never happen. */
520 elog(ERROR, "unknown worker type");
521 }
522
525 bgw.bgw_main_arg = Int32GetDatum(slot);
526
527 if (!RegisterDynamicBackgroundWorker(&bgw, &bgw_handle))
528 {
529 /* Failed to start worker, so clean up the worker slot. */
530 LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
531 Assert(generation == worker->generation);
533 LWLockRelease(LogicalRepWorkerLock);
534
536 (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
537 errmsg("out of background worker slots"),
538 errhint("You might need to increase \"%s\".", "max_worker_processes")));
539 return false;
540 }
541
542 /* Now wait until it attaches. */
543 return WaitForReplicationWorkerAttach(worker, generation, bgw_handle);
544}
545
546/*
547 * Internal function to stop the worker and wait until it detaches from the
548 * slot.
549 */
550static void
552{
553 uint16 generation;
554
555 Assert(LWLockHeldByMeInMode(LogicalRepWorkerLock, LW_SHARED));
556
557 /*
558 * Remember which generation was our worker so we can check if what we see
559 * is still the same one.
560 */
561 generation = worker->generation;
562
563 /*
564 * If we found a worker but it does not have proc set then it is still
565 * starting up; wait for it to finish starting and then kill it.
566 */
567 while (worker->in_use && !worker->proc)
568 {
569 int rc;
570
571 LWLockRelease(LogicalRepWorkerLock);
572
573 /* Wait a bit --- we don't expect to have to wait long. */
574 rc = WaitLatch(MyLatch,
576 10L, WAIT_EVENT_BGWORKER_STARTUP);
577
578 if (rc & WL_LATCH_SET)
579 {
582 }
583
584 /* Recheck worker status. */
585 LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
586
587 /*
588 * Check whether the worker slot is no longer used, which would mean
589 * that the worker has exited, or whether the worker generation is
590 * different, meaning that a different worker has taken the slot.
591 */
592 if (!worker->in_use || worker->generation != generation)
593 return;
594
595 /* Worker has assigned proc, so it has started. */
596 if (worker->proc)
597 break;
598 }
599
600 /* Now terminate the worker ... */
601 kill(worker->proc->pid, signo);
602
603 /* ... and wait for it to die. */
604 for (;;)
605 {
606 int rc;
607
608 /* is it gone? */
609 if (!worker->proc || worker->generation != generation)
610 break;
611
612 LWLockRelease(LogicalRepWorkerLock);
613
614 /* Wait a bit --- we don't expect to have to wait long. */
615 rc = WaitLatch(MyLatch,
617 10L, WAIT_EVENT_BGWORKER_SHUTDOWN);
618
619 if (rc & WL_LATCH_SET)
620 {
623 }
624
625 LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
626 }
627}
628
629/*
630 * Stop the logical replication worker for subid/relid, if any.
631 */
632void
634{
635 LogicalRepWorker *worker;
636
637 LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
638
639 worker = logicalrep_worker_find(subid, relid, false);
640
641 if (worker)
642 {
644 logicalrep_worker_stop_internal(worker, SIGTERM);
645 }
646
647 LWLockRelease(LogicalRepWorkerLock);
648}
649
650/*
651 * Stop the given logical replication parallel apply worker.
652 *
653 * Node that the function sends SIGUSR2 instead of SIGTERM to the parallel apply
654 * worker so that the worker exits cleanly.
655 */
656void
658{
659 int slot_no;
660 uint16 generation;
661 LogicalRepWorker *worker;
662
663 SpinLockAcquire(&winfo->shared->mutex);
664 generation = winfo->shared->logicalrep_worker_generation;
665 slot_no = winfo->shared->logicalrep_worker_slot_no;
666 SpinLockRelease(&winfo->shared->mutex);
667
668 Assert(slot_no >= 0 && slot_no < max_logical_replication_workers);
669
670 /*
671 * Detach from the error_mq_handle for the parallel apply worker before
672 * stopping it. This prevents the leader apply worker from trying to
673 * receive the message from the error queue that might already be detached
674 * by the parallel apply worker.
675 */
676 if (winfo->error_mq_handle)
677 {
679 winfo->error_mq_handle = NULL;
680 }
681
682 LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
683
684 worker = &LogicalRepCtx->workers[slot_no];
686
687 /*
688 * Only stop the worker if the generation matches and the worker is alive.
689 */
690 if (worker->generation == generation && worker->proc)
692
693 LWLockRelease(LogicalRepWorkerLock);
694}
695
696/*
697 * Wake up (using latch) any logical replication worker for specified sub/rel.
698 */
699void
701{
702 LogicalRepWorker *worker;
703
704 LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
705
706 worker = logicalrep_worker_find(subid, relid, true);
707
708 if (worker)
710
711 LWLockRelease(LogicalRepWorkerLock);
712}
713
714/*
715 * Wake up (using latch) the specified logical replication worker.
716 *
717 * Caller must hold lock, else worker->proc could change under us.
718 */
719void
721{
722 Assert(LWLockHeldByMe(LogicalRepWorkerLock));
723
724 SetLatch(&worker->proc->procLatch);
725}
726
727/*
728 * Attach to a slot.
729 */
730void
732{
733 /* Block concurrent access. */
734 LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
735
736 Assert(slot >= 0 && slot < max_logical_replication_workers);
738
740 {
741 LWLockRelease(LogicalRepWorkerLock);
743 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
744 errmsg("logical replication worker slot %d is empty, cannot attach",
745 slot)));
746 }
747
749 {
750 LWLockRelease(LogicalRepWorkerLock);
752 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
753 errmsg("logical replication worker slot %d is already used by "
754 "another worker, cannot attach", slot)));
755 }
756
759
760 LWLockRelease(LogicalRepWorkerLock);
761}
762
763/*
764 * Stop the parallel apply workers if any, and detach the leader apply worker
765 * (cleans up the worker info).
766 */
767static void
769{
770 /* Stop the parallel apply workers. */
772 {
773 List *workers;
774 ListCell *lc;
775
776 /*
777 * Detach from the error_mq_handle for all parallel apply workers
778 * before terminating them. This prevents the leader apply worker from
779 * receiving the worker termination message and sending it to logs
780 * when the same is already done by the parallel worker.
781 */
783
784 LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
785
786 workers = logicalrep_workers_find(MyLogicalRepWorker->subid, true, false);
787 foreach(lc, workers)
788 {
790
793 }
794
795 LWLockRelease(LogicalRepWorkerLock);
796
797 list_free(workers);
798 }
799
800 /* Block concurrent access. */
801 LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
802
804
805 LWLockRelease(LogicalRepWorkerLock);
806}
807
808/*
809 * Clean up worker info.
810 */
811static void
813{
814 Assert(LWLockHeldByMeInMode(LogicalRepWorkerLock, LW_EXCLUSIVE));
815
816 worker->type = WORKERTYPE_UNKNOWN;
817 worker->in_use = false;
818 worker->proc = NULL;
819 worker->dbid = InvalidOid;
820 worker->userid = InvalidOid;
821 worker->subid = InvalidOid;
822 worker->relid = InvalidOid;
823 worker->leader_pid = InvalidPid;
824 worker->parallel_apply = false;
825}
826
827/*
828 * Cleanup function for logical replication launcher.
829 *
830 * Called on logical replication launcher exit.
831 */
832static void
834{
836}
837
838/*
839 * Cleanup function.
840 *
841 * Called on logical replication worker exit.
842 */
843static void
845{
846 /* Disconnect gracefully from the remote side. */
849
851
852 /* Cleanup fileset used for streaming transactions. */
855
856 /*
857 * Session level locks may be acquired outside of a transaction in
858 * parallel apply mode and will not be released when the worker
859 * terminates, so manually release all locks before the worker exits.
860 *
861 * The locks will be acquired once the worker is initialized.
862 */
865
867}
868
869/*
870 * Count the number of registered (not necessarily running) sync workers
871 * for a subscription.
872 */
873int
875{
876 int i;
877 int res = 0;
878
879 Assert(LWLockHeldByMe(LogicalRepWorkerLock));
880
881 /* Search for attached worker for a given subscription id. */
882 for (i = 0; i < max_logical_replication_workers; i++)
883 {
885
886 if (isTablesyncWorker(w) && w->subid == subid)
887 res++;
888 }
889
890 return res;
891}
892
893/*
894 * Count the number of registered (but not necessarily running) parallel apply
895 * workers for a subscription.
896 */
897static int
899{
900 int i;
901 int res = 0;
902
903 Assert(LWLockHeldByMe(LogicalRepWorkerLock));
904
905 /*
906 * Scan all attached parallel apply workers, only counting those which
907 * have the given subscription id.
908 */
909 for (i = 0; i < max_logical_replication_workers; i++)
910 {
912
913 if (isParallelApplyWorker(w) && w->subid == subid)
914 res++;
915 }
916
917 return res;
918}
919
920/*
921 * ApplyLauncherShmemSize
922 * Compute space needed for replication launcher shared memory
923 */
924Size
926{
927 Size size;
928
929 /*
930 * Need the fixed struct and the array of LogicalRepWorker.
931 */
932 size = sizeof(LogicalRepCtxStruct);
933 size = MAXALIGN(size);
935 sizeof(LogicalRepWorker)));
936 return size;
937}
938
939/*
940 * ApplyLauncherRegister
941 * Register a background worker running the logical replication launcher.
942 */
943void
945{
947
948 /*
949 * The logical replication launcher is disabled during binary upgrades, to
950 * prevent logical replication workers from running on the source cluster.
951 * That could cause replication origins to move forward after having been
952 * copied to the target cluster, potentially creating conflicts with the
953 * copied data files.
954 */
956 return;
957
958 memset(&bgw, 0, sizeof(bgw));
962 snprintf(bgw.bgw_library_name, MAXPGPATH, "postgres");
963 snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
965 "logical replication launcher");
967 "logical replication launcher");
968 bgw.bgw_restart_time = 5;
969 bgw.bgw_notify_pid = 0;
970 bgw.bgw_main_arg = (Datum) 0;
971
973}
974
975/*
976 * ApplyLauncherShmemInit
977 * Allocate and initialize replication launcher shared memory
978 */
979void
981{
982 bool found;
983
985 ShmemInitStruct("Logical Replication Launcher Data",
987 &found);
988
989 if (!found)
990 {
991 int slot;
992
994
997
998 /* Initialize memory and spin locks for each worker slot. */
999 for (slot = 0; slot < max_logical_replication_workers; slot++)
1000 {
1001 LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
1002
1003 memset(worker, 0, sizeof(LogicalRepWorker));
1004 SpinLockInit(&worker->relmutex);
1005 }
1006 }
1007}
1008
1009/*
1010 * Initialize or attach to the dynamic shared hash table that stores the
1011 * last-start times, if not already done.
1012 * This must be called before accessing the table.
1013 */
1014static void
1016{
1017 MemoryContext oldcontext;
1018
1019 /* Quick exit if we already did this. */
1021 last_start_times != NULL)
1022 return;
1023
1024 /* Otherwise, use a lock to ensure only one process creates the table. */
1025 LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
1026
1027 /* Be sure any local memory allocated by DSA routines is persistent. */
1029
1031 {
1032 /* Initialize dynamic shared hash table for last-start times. */
1033 last_start_times_dsa = dsa_create(LWTRANCHE_LAUNCHER_DSA);
1037
1038 /* Store handles in shared memory for other backends to use. */
1041 }
1042 else if (!last_start_times)
1043 {
1044 /* Attach to existing dynamic shared hash table. */
1049 }
1050
1051 MemoryContextSwitchTo(oldcontext);
1052 LWLockRelease(LogicalRepWorkerLock);
1053}
1054
1055/*
1056 * Set the last-start time for the subscription.
1057 */
1058static void
1060{
1062 bool found;
1063
1065
1066 entry = dshash_find_or_insert(last_start_times, &subid, &found);
1067 entry->last_start_time = start_time;
1069}
1070
1071/*
1072 * Return the last-start time for the subscription, or 0 if there isn't one.
1073 */
1074static TimestampTz
1076{
1078 TimestampTz ret;
1079
1081
1082 entry = dshash_find(last_start_times, &subid, false);
1083 if (entry == NULL)
1084 return 0;
1085
1086 ret = entry->last_start_time;
1088
1089 return ret;
1090}
1091
1092/*
1093 * Remove the last-start-time entry for the subscription, if one exists.
1094 *
1095 * This has two use-cases: to remove the entry related to a subscription
1096 * that's been deleted or disabled (just to avoid leaking shared memory),
1097 * and to allow immediate restart of an apply worker that has exited
1098 * due to subscription parameter changes.
1099 */
1100void
1102{
1104
1105 (void) dshash_delete_key(last_start_times, &subid);
1106}
1107
1108/*
1109 * Wakeup the launcher on commit if requested.
1110 */
1111void
1113{
1114 if (isCommit)
1115 {
1118 }
1119
1121}
1122
1123/*
1124 * Request wakeup of the launcher on commit of the transaction.
1125 *
1126 * This is used to send launcher signal to stop sleeping and process the
1127 * subscriptions when current transaction commits. Should be used when new
1128 * tuple was added to the pg_subscription catalog.
1129*/
1130void
1132{
1135}
1136
1137/*
1138 * Wakeup the launcher immediately.
1139 */
1140void
1142{
1143 if (LogicalRepCtx->launcher_pid != 0)
1145}
1146
1147/*
1148 * Main loop for the apply launcher process.
1149 */
1150void
1152{
1154 (errmsg_internal("logical replication launcher started")));
1155
1157
1160
1161 /* Establish signal handlers. */
1163 pqsignal(SIGTERM, die);
1165
1166 /*
1167 * Establish connection to nailed catalogs (we only ever access
1168 * pg_subscription).
1169 */
1171
1172 /*
1173 * Acquire the conflict detection slot at startup to ensure it can be
1174 * dropped if no longer needed after a restart.
1175 */
1177
1178 /* Enter main loop */
1179 for (;;)
1180 {
1181 int rc;
1182 List *sublist;
1183 ListCell *lc;
1184 MemoryContext subctx;
1185 MemoryContext oldctx;
1186 long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
1187 bool can_update_xmin = true;
1188 bool retain_dead_tuples = false;
1190
1192
1193 /* Use temporary context to avoid leaking memory across cycles. */
1195 "Logical Replication Launcher sublist",
1197 oldctx = MemoryContextSwitchTo(subctx);
1198
1199 /*
1200 * Start any missing workers for enabled subscriptions.
1201 *
1202 * Also, during the iteration through all subscriptions, we compute
1203 * the minimum XID required to protect deleted tuples for conflict
1204 * detection if one of the subscription enables retain_dead_tuples
1205 * option.
1206 */
1207 sublist = get_subscription_list();
1208 foreach(lc, sublist)
1209 {
1210 Subscription *sub = (Subscription *) lfirst(lc);
1212 TimestampTz last_start;
1214 long elapsed;
1215
1216 if (sub->retaindeadtuples)
1217 {
1218 retain_dead_tuples = true;
1219
1220 /*
1221 * Create a replication slot to retain information necessary
1222 * for conflict detection such as dead tuples, commit
1223 * timestamps, and origins.
1224 *
1225 * The slot is created before starting the apply worker to
1226 * prevent it from unnecessarily maintaining its
1227 * oldest_nonremovable_xid.
1228 *
1229 * The slot is created even for a disabled subscription to
1230 * ensure that conflict-related information is available when
1231 * applying remote changes that occurred before the
1232 * subscription was enabled.
1233 */
1235
1236 if (sub->retentionactive)
1237 {
1238 /*
1239 * Can't advance xmin of the slot unless all the
1240 * subscriptions actively retaining dead tuples are
1241 * enabled. This is required to ensure that we don't
1242 * advance the xmin of CONFLICT_DETECTION_SLOT if one of
1243 * the subscriptions is not enabled. Otherwise, we won't
1244 * be able to detect conflicts reliably for such a
1245 * subscription even though it has set the
1246 * retain_dead_tuples option.
1247 */
1248 can_update_xmin &= sub->enabled;
1249
1250 /*
1251 * Initialize the slot once the subscription activiates
1252 * retention.
1253 */
1256 }
1257 }
1258
1259 if (!sub->enabled)
1260 continue;
1261
1262 LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1263 w = logicalrep_worker_find(sub->oid, InvalidOid, false);
1264
1265 if (w != NULL)
1266 {
1267 /*
1268 * Compute the minimum xmin required to protect dead tuples
1269 * required for conflict detection among all running apply
1270 * workers. This computation is performed while holding
1271 * LogicalRepWorkerLock to prevent accessing invalid worker
1272 * data, in scenarios where a worker might exit and reset its
1273 * state concurrently.
1274 */
1275 if (sub->retaindeadtuples &&
1276 sub->retentionactive &&
1277 can_update_xmin)
1279
1280 LWLockRelease(LogicalRepWorkerLock);
1281
1282 /* worker is running already */
1283 continue;
1284 }
1285
1286 LWLockRelease(LogicalRepWorkerLock);
1287
1288 /*
1289 * Can't advance xmin of the slot unless all the workers
1290 * corresponding to subscriptions actively retaining dead tuples
1291 * are running, disabling the further computation of the minimum
1292 * nonremovable xid.
1293 */
1294 if (sub->retaindeadtuples && sub->retentionactive)
1295 can_update_xmin = false;
1296
1297 /*
1298 * If the worker is eligible to start now, launch it. Otherwise,
1299 * adjust wait_time so that we'll wake up as soon as it can be
1300 * started.
1301 *
1302 * Each subscription's apply worker can only be restarted once per
1303 * wal_retrieve_retry_interval, so that errors do not cause us to
1304 * repeatedly restart the worker as fast as possible. In cases
1305 * where a restart is expected (e.g., subscription parameter
1306 * changes), another process should remove the last-start entry
1307 * for the subscription so that the worker can be restarted
1308 * without waiting for wal_retrieve_retry_interval to elapse.
1309 */
1310 last_start = ApplyLauncherGetWorkerStartTime(sub->oid);
1312 if (last_start == 0 ||
1314 {
1317 sub->dbid, sub->oid, sub->name,
1318 sub->owner, InvalidOid,
1320 sub->retaindeadtuples &&
1321 sub->retentionactive))
1322 {
1323 /*
1324 * We get here either if we failed to launch a worker
1325 * (perhaps for resource-exhaustion reasons) or if we
1326 * launched one but it immediately quit. Either way, it
1327 * seems appropriate to try again after
1328 * wal_retrieve_retry_interval.
1329 */
1330 wait_time = Min(wait_time,
1332 }
1333 }
1334 else
1335 {
1336 wait_time = Min(wait_time,
1337 wal_retrieve_retry_interval - elapsed);
1338 }
1339 }
1340
1341 /*
1342 * Drop the CONFLICT_DETECTION_SLOT slot if there is no subscription
1343 * that requires us to retain dead tuples. Otherwise, if required,
1344 * advance the slot's xmin to protect dead tuples required for the
1345 * conflict detection.
1346 *
1347 * Additionally, if all apply workers for subscriptions with
1348 * retain_dead_tuples enabled have requested to stop retention, the
1349 * slot's xmin will be set to InvalidTransactionId allowing the
1350 * removal of dead tuples.
1351 */
1353 {
1354 if (!retain_dead_tuples)
1356 else if (can_update_xmin)
1358 }
1359
1360 /* Switch back to original memory context. */
1361 MemoryContextSwitchTo(oldctx);
1362 /* Clean the temporary memory. */
1363 MemoryContextDelete(subctx);
1364
1365 /* Wait for more work. */
1366 rc = WaitLatch(MyLatch,
1368 wait_time,
1369 WAIT_EVENT_LOGICAL_LAUNCHER_MAIN);
1370
1371 if (rc & WL_LATCH_SET)
1372 {
1375 }
1376
1378 {
1379 ConfigReloadPending = false;
1381 }
1382 }
1383
1384 /* Not reachable */
1385}
1386
1387/*
1388 * Determine the minimum non-removable transaction ID across all apply workers
1389 * for subscriptions that have retain_dead_tuples enabled. Store the result
1390 * in *xmin.
1391 */
1392static void
1394{
1395 TransactionId nonremovable_xid;
1396
1397 Assert(worker != NULL);
1398
1399 /*
1400 * The replication slot for conflict detection must be created before the
1401 * worker starts.
1402 */
1404
1405 SpinLockAcquire(&worker->relmutex);
1406 nonremovable_xid = worker->oldest_nonremovable_xid;
1407 SpinLockRelease(&worker->relmutex);
1408
1409 /*
1410 * Return if the apply worker has stopped retention concurrently.
1411 *
1412 * Although this function is invoked only when retentionactive is true,
1413 * the apply worker might stop retention after the launcher fetches the
1414 * retentionactive flag.
1415 */
1416 if (!TransactionIdIsValid(nonremovable_xid))
1417 return;
1418
1419 if (!TransactionIdIsValid(*xmin) ||
1420 TransactionIdPrecedes(nonremovable_xid, *xmin))
1421 *xmin = nonremovable_xid;
1422}
1423
1424/*
1425 * Acquire the replication slot used to retain information for conflict
1426 * detection, if it exists.
1427 *
1428 * Return true if successfully acquired, otherwise return false.
1429 */
1430static bool
1432{
1434 return false;
1435
1437 return true;
1438}
1439
1440/*
1441 * Update the xmin the replication slot used to retain information required
1442 * for conflict detection.
1443 */
1444static void
1446{
1448 Assert(!TransactionIdIsValid(new_xmin) ||
1450
1451 /* Return if the xmin value of the slot cannot be updated */
1453 return;
1454
1457 MyReplicationSlot->data.xmin = new_xmin;
1459
1460 elog(DEBUG1, "updated xmin: %u", MyReplicationSlot->data.xmin);
1461
1464
1465 /*
1466 * Like PhysicalConfirmReceivedLocation(), do not save slot information
1467 * each time. This is acceptable because all concurrent transactions on
1468 * the publisher that require the data preceding the slot's xmin should
1469 * have already been applied and flushed on the subscriber before the xmin
1470 * is advanced. So, even if the slot's xmin regresses after a restart, it
1471 * will be advanced again in the next cycle. Therefore, no data required
1472 * for conflict detection will be prematurely removed.
1473 */
1474 return;
1475}
1476
1477/*
1478 * Initialize the xmin for the conflict detection slot.
1479 */
1480static void
1482{
1483 TransactionId xmin_horizon;
1484
1485 /* Replication slot must exist but shouldn't be initialized. */
1488
1489 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1490
1491 xmin_horizon = GetOldestSafeDecodingTransactionId(false);
1492
1494 MyReplicationSlot->effective_xmin = xmin_horizon;
1495 MyReplicationSlot->data.xmin = xmin_horizon;
1497
1499
1500 LWLockRelease(ProcArrayLock);
1501
1502 /* Write this slot to disk */
1505}
1506
1507/*
1508 * Create and acquire the replication slot used to retain information for
1509 * conflict detection, if not yet.
1510 */
1511void
1513{
1514 /* Exit early, if the replication slot is already created and acquired */
1516 return;
1517
1518 ereport(LOG,
1519 errmsg("creating replication conflict detection slot"));
1520
1522 false, false);
1523
1525}
1526
1527/*
1528 * Is current process the logical replication launcher?
1529 */
1530bool
1532{
1534}
1535
1536/*
1537 * Return the pid of the leader apply worker if the given pid is the pid of a
1538 * parallel apply worker, otherwise, return InvalidPid.
1539 */
1540pid_t
1542{
1543 int leader_pid = InvalidPid;
1544 int i;
1545
1546 LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1547
1548 for (i = 0; i < max_logical_replication_workers; i++)
1549 {
1551
1552 if (isParallelApplyWorker(w) && w->proc && pid == w->proc->pid)
1553 {
1554 leader_pid = w->leader_pid;
1555 break;
1556 }
1557 }
1558
1559 LWLockRelease(LogicalRepWorkerLock);
1560
1561 return leader_pid;
1562}
1563
1564/*
1565 * Returns state of the subscriptions.
1566 */
1567Datum
1569{
1570#define PG_STAT_GET_SUBSCRIPTION_COLS 10
1571 Oid subid = PG_ARGISNULL(0) ? InvalidOid : PG_GETARG_OID(0);
1572 int i;
1573 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1574
1575 InitMaterializedSRF(fcinfo, 0);
1576
1577 /* Make sure we get consistent view of the workers. */
1578 LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1579
1580 for (i = 0; i < max_logical_replication_workers; i++)
1581 {
1582 /* for each row */
1584 bool nulls[PG_STAT_GET_SUBSCRIPTION_COLS] = {0};
1585 int worker_pid;
1586 LogicalRepWorker worker;
1587
1588 memcpy(&worker, &LogicalRepCtx->workers[i],
1589 sizeof(LogicalRepWorker));
1590 if (!worker.proc || !IsBackendPid(worker.proc->pid))
1591 continue;
1592
1593 if (OidIsValid(subid) && worker.subid != subid)
1594 continue;
1595
1596 worker_pid = worker.proc->pid;
1597
1598 values[0] = ObjectIdGetDatum(worker.subid);
1599 if (isTablesyncWorker(&worker))
1600 values[1] = ObjectIdGetDatum(worker.relid);
1601 else
1602 nulls[1] = true;
1603 values[2] = Int32GetDatum(worker_pid);
1604
1605 if (isParallelApplyWorker(&worker))
1606 values[3] = Int32GetDatum(worker.leader_pid);
1607 else
1608 nulls[3] = true;
1609
1610 if (XLogRecPtrIsInvalid(worker.last_lsn))
1611 nulls[4] = true;
1612 else
1613 values[4] = LSNGetDatum(worker.last_lsn);
1614 if (worker.last_send_time == 0)
1615 nulls[5] = true;
1616 else
1618 if (worker.last_recv_time == 0)
1619 nulls[6] = true;
1620 else
1622 if (XLogRecPtrIsInvalid(worker.reply_lsn))
1623 nulls[7] = true;
1624 else
1625 values[7] = LSNGetDatum(worker.reply_lsn);
1626 if (worker.reply_time == 0)
1627 nulls[8] = true;
1628 else
1630
1631 switch (worker.type)
1632 {
1633 case WORKERTYPE_APPLY:
1634 values[9] = CStringGetTextDatum("apply");
1635 break;
1637 values[9] = CStringGetTextDatum("parallel apply");
1638 break;
1640 values[9] = CStringGetTextDatum("table synchronization");
1641 break;
1642 case WORKERTYPE_UNKNOWN:
1643 /* Should never happen. */
1644 elog(ERROR, "unknown worker type");
1645 }
1646
1647 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
1648 values, nulls);
1649
1650 /*
1651 * If only a single subscription was requested, and we found it,
1652 * break.
1653 */
1654 if (OidIsValid(subid))
1655 break;
1656 }
1657
1658 LWLockRelease(LogicalRepWorkerLock);
1659
1660 return (Datum) 0;
1661}
void pa_detach_all_error_mq(void)
bool InitializingApplyWorker
Definition: worker.c:499
WalReceiverConn * LogRepWorkerWalRcvConn
Definition: worker.c:477
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition: timestamp.c:1757
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1781
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1609
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:940
void BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
Definition: bgworker.c:853
void BackgroundWorkerUnblockSignals(void)
Definition: bgworker.c:927
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1158
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:1046
#define BGW_NEVER_RESTART
Definition: bgworker.h:85
BgwHandleStatus
Definition: bgworker.h:104
@ BGWH_STOPPED
Definition: bgworker.h:107
@ BgWorkerStart_RecoveryFinished
Definition: bgworker.h:81
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:60
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:53
#define BGW_MAXLEN
Definition: bgworker.h:86
static Datum values[MAXATTR]
Definition: bootstrap.c:153
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define NameStr(name)
Definition: c.h:752
#define Min(x, y)
Definition: c.h:1004
#define MAXALIGN(LEN)
Definition: c.h:811
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:471
uint16_t uint16
Definition: c.h:538
uint32 TransactionId
Definition: c.h:658
#define OidIsValid(objectId)
Definition: c.h:775
size_t Size
Definition: c.h:611
int64 TimestampTz
Definition: timestamp.h:39
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:159
dsa_area * dsa_attach(dsa_handle handle)
Definition: dsa.c:510
void dsa_pin_mapping(dsa_area *area)
Definition: dsa.c:650
dsa_handle dsa_get_handle(dsa_area *area)
Definition: dsa.c:498
void dsa_pin(dsa_area *area)
Definition: dsa.c:990
#define dsa_create(tranche_id)
Definition: dsa.h:117
dsm_handle dsa_handle
Definition: dsa.h:136
#define DSA_HANDLE_INVALID
Definition: dsa.h:139
bool dshash_delete_key(dshash_table *hash_table, const void *key)
Definition: dshash.c:503
void dshash_memcpy(void *dest, const void *src, size_t size, void *arg)
Definition: dshash.c:590
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:558
void * dshash_find(dshash_table *hash_table, const void *key, bool exclusive)
Definition: dshash.c:390
dshash_table_handle dshash_get_hash_table_handle(dshash_table *hash_table)
Definition: dshash.c:367
dshash_table * dshash_attach(dsa_area *area, const dshash_parameters *params, dshash_table_handle handle, void *arg)
Definition: dshash.c:270
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:433
dshash_hash dshash_memhash(const void *v, size_t size, void *arg)
Definition: dshash.c:581
dshash_table * dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
Definition: dshash.c:206
int dshash_memcmp(const void *a, const void *b, size_t size, void *arg)
Definition: dshash.c:572
#define DSHASH_HANDLE_INVALID
Definition: dshash.h:27
dsa_pointer dshash_table_handle
Definition: dshash.h:24
uint32 dsm_handle
Definition: dsm_impl.h:55
#define DSM_HANDLE_INVALID
Definition: dsm_impl.h:58
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1161
int errhint(const char *fmt,...)
Definition: elog.c:1321
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define LOG
Definition: elog.h:31
#define WARNING
Definition: elog.h:36
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:150
void FileSetDeleteAll(FileSet *fileset)
Definition: fileset.c:150
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
bool IsBinaryUpgrade
Definition: globals.c:121
int MyProcPid
Definition: globals.c:47
struct Latch * MyLatch
Definition: globals.c:63
void ProcessConfigFile(GucContext context)
Definition: guc-file.l:120
@ PGC_SIGHUP
Definition: guc.h:75
Assert(PointerIsAligned(start, uint64))
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1346
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:27
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:61
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337
int i
Definition: isn.c:77
void SetLatch(Latch *latch)
Definition: latch.c:290
void ResetLatch(Latch *latch)
Definition: latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:172
Datum pg_stat_get_subscription(PG_FUNCTION_ARGS)
Definition: launcher.c:1568
#define DEFAULT_NAPTIME_PER_CYCLE
Definition: launcher.c:49
List * logicalrep_workers_find(Oid subid, bool only_running, bool acquire_lock)
Definition: launcher.c:286
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:1112
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition: launcher.c:720
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:925
bool logicalrep_worker_launch(LogicalRepWorkerType wtype, Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid, dsm_handle subworker_dsm, bool retain_dead_tuples)
Definition: launcher.c:317
bool IsLogicalLauncher(void)
Definition: launcher.c:1531
void logicalrep_worker_attach(int slot)
Definition: launcher.c:731
void ApplyLauncherWakeup(void)
Definition: launcher.c:1141
static void ApplyLauncherSetWorkerStartTime(Oid subid, TimestampTz start_time)
Definition: launcher.c:1059
static void update_conflict_slot_xmin(TransactionId new_xmin)
Definition: launcher.c:1445
static void compute_min_nonremovable_xid(LogicalRepWorker *worker, TransactionId *xmin)
Definition: launcher.c:1393
static void logicalrep_launcher_onexit(int code, Datum arg)
Definition: launcher.c:833
static dsa_area * last_start_times_dsa
Definition: launcher.c:90
void ApplyLauncherMain(Datum main_arg)
Definition: launcher.c:1151
void CreateConflictDetectionSlot(void)
Definition: launcher.c:1512
#define PG_STAT_GET_SUBSCRIPTION_COLS
int max_logical_replication_workers
Definition: launcher.c:52
static void init_conflict_slot_xmin(void)
Definition: launcher.c:1481
void logicalrep_pa_worker_stop(ParallelApplyWorkerInfo *winfo)
Definition: launcher.c:657
static int logicalrep_pa_worker_count(Oid subid)
Definition: launcher.c:898
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:254
static bool on_commit_launcher_wakeup
Definition: launcher.c:93
struct LogicalRepCtxStruct LogicalRepCtxStruct
static TimestampTz ApplyLauncherGetWorkerStartTime(Oid subid)
Definition: launcher.c:1075
void logicalrep_worker_wakeup(Oid subid, Oid relid)
Definition: launcher.c:700
void ApplyLauncherShmemInit(void)
Definition: launcher.c:980
static void logicalrep_worker_stop_internal(LogicalRepWorker *worker, int signo)
Definition: launcher.c:551
static dshash_table * last_start_times
Definition: launcher.c:91
void logicalrep_worker_stop(Oid subid, Oid relid)
Definition: launcher.c:633
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:56
void ApplyLauncherWakeupAtCommit(void)
Definition: launcher.c:1131
static const dshash_parameters dsh_params
Definition: launcher.c:81
static LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:71
static void logicalrep_worker_onexit(int code, Datum arg)
Definition: launcher.c:844
pid_t GetLeaderApplyWorkerPid(pid_t pid)
Definition: launcher.c:1541
int max_sync_workers_per_subscription
Definition: launcher.c:53
static void logicalrep_worker_detach(void)
Definition: launcher.c:768
static bool WaitForReplicationWorkerAttach(LogicalRepWorker *worker, uint16 generation, BackgroundWorkerHandle *handle)
Definition: launcher.c:181
int logicalrep_sync_worker_count(Oid subid)
Definition: launcher.c:874
void ApplyLauncherForgetWorkerStartTime(Oid subid)
Definition: launcher.c:1101
static bool acquire_conflict_slot_if_exists(void)
Definition: launcher.c:1431
void ApplyLauncherRegister(void)
Definition: launcher.c:944
struct LauncherLastStartTimesEntry LauncherLastStartTimesEntry
static void logicalrep_launcher_attach_dshmem(void)
Definition: launcher.c:1015
static List * get_subscription_list(void)
Definition: launcher.c:117
int max_parallel_apply_workers_per_subscription
Definition: launcher.c:54
static void logicalrep_worker_cleanup(LogicalRepWorker *worker)
Definition: launcher.c:812
List * lappend(List *list, void *datum)
Definition: list.c:339
void list_free(List *list)
Definition: list.c:1546
void LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks)
Definition: lock.c:2307
#define DEFAULT_LOCKMETHOD
Definition: lock.h:127
#define AccessShareLock
Definition: lockdefs.h:36
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1977
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1174
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:2021
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1894
@ LW_SHARED
Definition: lwlock.h:113
@ LW_EXCLUSIVE
Definition: lwlock.h:112
char * pstrdup(const char *in)
Definition: mcxt.c:1759
void * palloc0(Size size)
Definition: mcxt.c:1395
MemoryContext TopMemoryContext
Definition: mcxt.c:166
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:469
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define InvalidPid
Definition: miscadmin.h:32
int max_active_replication_origins
Definition: origin.c:104
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
void * arg
#define MAXPGPATH
static time_t start_time
Definition: pg_ctl.c:95
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
static Datum LSNGetDatum(XLogRecPtr X)
Definition: pg_lsn.h:31
NameData subname
FormData_pg_subscription * Form_pg_subscription
#define die(msg)
#define pqsignal
Definition: port.h:531
#define snprintf
Definition: port.h:239
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
uint64_t Datum
Definition: postgres.h:70
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:222
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2907
bool IsBackendPid(int pid)
Definition: procarray.c:3253
@ ForwardScanDirection
Definition: sdir.h:28
void shm_mq_detach(shm_mq_handle *mqh)
Definition: shm_mq.c:843
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
Size mul_size(Size s1, Size s2)
Definition: shmem.c:510
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387
void ReplicationSlotAcquire(const char *name, bool nowait, bool error_if_invalid)
Definition: slot.c:593
void ReplicationSlotCreate(const char *name, bool db_specific, ReplicationSlotPersistency persistency, bool two_phase, bool failover, bool synced)
Definition: slot.c:352
void ReplicationSlotDropAcquired(void)
Definition: slot.c:964
void ReplicationSlotMarkDirty(void)
Definition: slot.c:1106
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:1145
ReplicationSlot * MyReplicationSlot
Definition: slot.c:148
void ReplicationSlotSave(void)
Definition: slot.c:1088
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:513
#define CONFLICT_DETECTION_SLOT
Definition: slot.h:28
@ RS_PERSISTENT
Definition: slot.h:45
#define SpinLockInit(lock)
Definition: spin.h:57
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
PGPROC * MyProc
Definition: proc.c:66
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:97
Datum bgw_main_arg
Definition: bgworker.h:98
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:91
int bgw_restart_time
Definition: bgworker.h:95
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:92
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:94
char bgw_extra[BGW_EXTRALEN]
Definition: bgworker.h:99
pid_t bgw_notify_pid
Definition: bgworker.h:100
char bgw_library_name[MAXPGPATH]
Definition: bgworker.h:96
TimestampTz last_start_time
Definition: launcher.c:77
Definition: pg_list.h:54
dsa_handle last_start_dsa
Definition: launcher.c:64
dshash_table_handle last_start_dsh
Definition: launcher.c:65
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:68
XLogRecPtr relstate_lsn
TimestampTz last_recv_time
LogicalRepWorkerType type
TimestampTz launch_time
TimestampTz reply_time
FileSet * stream_fileset
TransactionId oldest_nonremovable_xid
XLogRecPtr reply_lsn
TimestampTz last_send_time
int pid
Definition: proc.h:199
Latch procLatch
Definition: proc.h:186
shm_mq_handle * error_mq_handle
ParallelApplyWorkerShared * shared
TransactionId xmin
Definition: slot.h:96
slock_t mutex
Definition: slot.h:165
TransactionId effective_xmin
Definition: slot.h:188
ReplicationSlotPersistentData data
Definition: slot.h:192
TupleDesc setDesc
Definition: execnodes.h:364
Tuplestorestate * setResult
Definition: execnodes.h:363
Definition: dsa.c:348
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, ScanKeyData *key)
Definition: tableam.c:113
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:985
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:299
#define InvalidTransactionId
Definition: transam.h:31
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
Definition: tuplestore.c:784
static Datum TimestampTzGetDatum(TimestampTz X)
Definition: timestamp.h:52
#define WL_TIMEOUT
Definition: waiteventset.h:37
#define WL_EXIT_ON_PM_DEATH
Definition: waiteventset.h:39
#define WL_LATCH_SET
Definition: waiteventset.h:34
int wal_receiver_timeout
Definition: walreceiver.c:89
#define walrcv_disconnect(conn)
Definition: walreceiver.h:467
#define SIGHUP
Definition: win32_port.h:158
#define kill(pid, sig)
Definition: win32_port.h:493
#define SIGUSR1
Definition: win32_port.h:170
#define SIGUSR2
Definition: win32_port.h:171
#define isParallelApplyWorker(worker)
LogicalRepWorkerType
@ WORKERTYPE_TABLESYNC
@ WORKERTYPE_UNKNOWN
@ WORKERTYPE_PARALLEL_APPLY
@ WORKERTYPE_APPLY
#define isTablesyncWorker(worker)
static bool am_leader_apply_worker(void)
void StartTransactionCommand(void)
Definition: xact.c:3071
void CommitTransactionCommand(void)
Definition: xact.c:3169
int wal_retrieve_retry_interval
Definition: xlog.c:135
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28