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-2020, 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 "libpq/pqsignal.h"
29 #include "miscadmin.h"
30 #include "pgstat.h"
31 #include "postmaster/bgworker.h"
33 #include "postmaster/interrupt.h"
34 #include "postmaster/postmaster.h"
37 #include "replication/slot.h"
40 #include "storage/ipc.h"
41 #include "storage/proc.h"
42 #include "storage/procarray.h"
43 #include "storage/procsignal.h"
44 #include "tcop/tcopprot.h"
45 #include "utils/memutils.h"
46 #include "utils/pg_lsn.h"
47 #include "utils/ps_status.h"
48 #include "utils/snapmgr.h"
49 #include "utils/timeout.h"
50 
51 /* max sleep time between cycles (3min) */
52 #define DEFAULT_NAPTIME_PER_CYCLE 180000L
53 
56 
58 
59 typedef struct LogicalRepCtxStruct
60 {
61  /* Supervisor process. */
62  pid_t launcher_pid;
63 
64  /* Background workers. */
67 
69 
70 typedef struct LogicalRepWorkerId
71 {
75 
76 typedef struct StopWorkersData
77 {
78  int nestDepth; /* Sub-transaction nest level */
79  List *workers; /* List of LogicalRepWorkerId */
80  struct StopWorkersData *parent; /* This need not be an immediate
81  * subtransaction parent */
83 
84 /*
85  * Stack of StopWorkersData elements. Each stack element contains the workers
86  * to be stopped for that subtransaction.
87  */
89 
90 static void ApplyLauncherWakeup(void);
91 static void logicalrep_launcher_onexit(int code, Datum arg);
92 static void logicalrep_worker_onexit(int code, Datum arg);
93 static void logicalrep_worker_detach(void);
94 static void logicalrep_worker_cleanup(LogicalRepWorker *worker);
95 
96 static bool on_commit_launcher_wakeup = false;
97 
99 
100 
101 /*
102  * Load the list of subscriptions.
103  *
104  * Only the fields interesting for worker start/stop functions are filled for
105  * each subscription.
106  */
107 static List *
109 {
110  List *res = NIL;
111  Relation rel;
112  TableScanDesc scan;
113  HeapTuple tup;
114  MemoryContext resultcxt;
115 
116  /* This is the context that we will allocate our output data in */
117  resultcxt = CurrentMemoryContext;
118 
119  /*
120  * Start a transaction so we can access pg_database, and get a snapshot.
121  * We don't have a use for the snapshot itself, but we're interested in
122  * the secondary effect that it sets RecentGlobalXmin. (This is critical
123  * for anything that reads heap pages, because HOT may decide to prune
124  * them even if the process doesn't attempt to modify any tuples.)
125  *
126  * FIXME: This comment is inaccurate / the code buggy. A snapshot that is
127  * not pushed/active does not reliably prevent HOT pruning (->xmin could
128  * e.g. be cleared when cache invalidations are processed).
129  */
131  (void) GetTransactionSnapshot();
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  /* We don't fill fields we are not interested in. */
157 
158  res = lappend(res, sub);
159  MemoryContextSwitchTo(oldcxt);
160  }
161 
162  table_endscan(scan);
164 
166 
167  return res;
168 }
169 
170 /*
171  * Wait for a background worker to start up and attach to the shmem context.
172  *
173  * This is only needed for cleaning up the shared memory in case the worker
174  * fails to attach.
175  */
176 static void
178  uint16 generation,
179  BackgroundWorkerHandle *handle)
180 {
182  int rc;
183 
184  for (;;)
185  {
186  pid_t pid;
187 
189 
190  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
191 
192  /* Worker either died or has started; no need to do anything. */
193  if (!worker->in_use || worker->proc)
194  {
195  LWLockRelease(LogicalRepWorkerLock);
196  return;
197  }
198 
199  LWLockRelease(LogicalRepWorkerLock);
200 
201  /* Check if worker has died before attaching, and clean up after it. */
202  status = GetBackgroundWorkerPid(handle, &pid);
203 
204  if (status == BGWH_STOPPED)
205  {
206  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
207  /* Ensure that this was indeed the worker we waited for. */
208  if (generation == worker->generation)
210  LWLockRelease(LogicalRepWorkerLock);
211  return;
212  }
213 
214  /*
215  * We need timeout because we generally don't get notified via latch
216  * about the worker attach. But we don't expect to have to wait long.
217  */
218  rc = WaitLatch(MyLatch,
221 
222  if (rc & WL_LATCH_SET)
223  {
226  }
227  }
228 }
229 
230 /*
231  * Walks the workers array and searches for one that matches given
232  * subscription id and relid.
233  */
235 logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
236 {
237  int i;
238  LogicalRepWorker *res = NULL;
239 
240  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
241 
242  /* Search for attached worker for a given subscription id. */
243  for (i = 0; i < max_logical_replication_workers; i++)
244  {
245  LogicalRepWorker *w = &LogicalRepCtx->workers[i];
246 
247  if (w->in_use && w->subid == subid && w->relid == relid &&
248  (!only_running || w->proc))
249  {
250  res = w;
251  break;
252  }
253  }
254 
255  return res;
256 }
257 
258 /*
259  * Similar to logicalrep_worker_find(), but returns list of all workers for
260  * the subscription, instead just one.
261  */
262 List *
263 logicalrep_workers_find(Oid subid, bool only_running)
264 {
265  int i;
266  List *res = NIL;
267 
268  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
269 
270  /* Search for attached worker for a given subscription id. */
271  for (i = 0; i < max_logical_replication_workers; i++)
272  {
273  LogicalRepWorker *w = &LogicalRepCtx->workers[i];
274 
275  if (w->in_use && w->subid == subid && (!only_running || w->proc))
276  res = lappend(res, w);
277  }
278 
279  return res;
280 }
281 
282 /*
283  * Start new apply background worker, if possible.
284  */
285 void
286 logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid,
287  Oid relid)
288 {
289  BackgroundWorker bgw;
290  BackgroundWorkerHandle *bgw_handle;
291  uint16 generation;
292  int i;
293  int slot = 0;
294  LogicalRepWorker *worker = NULL;
295  int nsyncworkers;
297 
298  ereport(DEBUG1,
299  (errmsg("starting logical replication worker for subscription \"%s\"",
300  subname)));
301 
302  /* Report this after the initial starting message for consistency. */
303  if (max_replication_slots == 0)
304  ereport(ERROR,
305  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
306  errmsg("cannot start logical replication workers when max_replication_slots = 0")));
307 
308  /*
309  * We need to do the modification of the shared memory under lock so that
310  * we have consistent view.
311  */
312  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
313 
314 retry:
315  /* Find unused worker slot. */
316  for (i = 0; i < max_logical_replication_workers; i++)
317  {
318  LogicalRepWorker *w = &LogicalRepCtx->workers[i];
319 
320  if (!w->in_use)
321  {
322  worker = w;
323  slot = i;
324  break;
325  }
326  }
327 
328  nsyncworkers = logicalrep_sync_worker_count(subid);
329 
330  now = GetCurrentTimestamp();
331 
332  /*
333  * If we didn't find a free slot, try to do garbage collection. The
334  * reason we do this is because if some worker failed to start up and its
335  * parent has crashed while waiting, the in_use state was never cleared.
336  */
337  if (worker == NULL || nsyncworkers >= max_sync_workers_per_subscription)
338  {
339  bool did_cleanup = false;
340 
341  for (i = 0; i < max_logical_replication_workers; i++)
342  {
343  LogicalRepWorker *w = &LogicalRepCtx->workers[i];
344 
345  /*
346  * If the worker was marked in use but didn't manage to attach in
347  * time, clean it up.
348  */
349  if (w->in_use && !w->proc &&
352  {
353  elog(WARNING,
354  "logical replication worker for subscription %u took too long to start; canceled",
355  w->subid);
356 
358  did_cleanup = true;
359  }
360  }
361 
362  if (did_cleanup)
363  goto retry;
364  }
365 
366  /*
367  * If we reached the sync worker limit per subscription, just exit
368  * silently as we might get here because of an otherwise harmless race
369  * condition.
370  */
371  if (nsyncworkers >= max_sync_workers_per_subscription)
372  {
373  LWLockRelease(LogicalRepWorkerLock);
374  return;
375  }
376 
377  /*
378  * However if there are no more free worker slots, inform user about it
379  * before exiting.
380  */
381  if (worker == NULL)
382  {
383  LWLockRelease(LogicalRepWorkerLock);
385  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
386  errmsg("out of logical replication worker slots"),
387  errhint("You might need to increase max_logical_replication_workers.")));
388  return;
389  }
390 
391  /* Prepare the worker slot. */
392  worker->launch_time = now;
393  worker->in_use = true;
394  worker->generation++;
395  worker->proc = NULL;
396  worker->dbid = dbid;
397  worker->userid = userid;
398  worker->subid = subid;
399  worker->relid = relid;
400  worker->relstate = SUBREL_STATE_UNKNOWN;
402  worker->last_lsn = InvalidXLogRecPtr;
405  worker->reply_lsn = InvalidXLogRecPtr;
406  TIMESTAMP_NOBEGIN(worker->reply_time);
407 
408  /* Before releasing lock, remember generation for future identification. */
409  generation = worker->generation;
410 
411  LWLockRelease(LogicalRepWorkerLock);
412 
413  /* Register the new dynamic worker. */
414  memset(&bgw, 0, sizeof(bgw));
418  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
419  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyWorkerMain");
420  if (OidIsValid(relid))
422  "logical replication worker for subscription %u sync %u", subid, relid);
423  else
425  "logical replication worker for subscription %u", subid);
426  snprintf(bgw.bgw_type, BGW_MAXLEN, "logical replication worker");
427 
430  bgw.bgw_main_arg = Int32GetDatum(slot);
431 
432  if (!RegisterDynamicBackgroundWorker(&bgw, &bgw_handle))
433  {
434  /* Failed to start worker, so clean up the worker slot. */
435  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
436  Assert(generation == worker->generation);
438  LWLockRelease(LogicalRepWorkerLock);
439 
441  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
442  errmsg("out of background worker slots"),
443  errhint("You might need to increase max_worker_processes.")));
444  return;
445  }
446 
447  /* Now wait until it attaches. */
448  WaitForReplicationWorkerAttach(worker, generation, bgw_handle);
449 }
450 
451 /*
452  * Stop the logical replication worker for subid/relid, if any, and wait until
453  * it detaches from the slot.
454  */
455 void
457 {
458  LogicalRepWorker *worker;
459  uint16 generation;
460 
461  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
462 
463  worker = logicalrep_worker_find(subid, relid, false);
464 
465  /* No worker, nothing to do. */
466  if (!worker)
467  {
468  LWLockRelease(LogicalRepWorkerLock);
469  return;
470  }
471 
472  /*
473  * Remember which generation was our worker so we can check if what we see
474  * is still the same one.
475  */
476  generation = worker->generation;
477 
478  /*
479  * If we found a worker but it does not have proc set then it is still
480  * starting up; wait for it to finish starting and then kill it.
481  */
482  while (worker->in_use && !worker->proc)
483  {
484  int rc;
485 
486  LWLockRelease(LogicalRepWorkerLock);
487 
488  /* Wait a bit --- we don't expect to have to wait long. */
489  rc = WaitLatch(MyLatch,
492 
493  if (rc & WL_LATCH_SET)
494  {
497  }
498 
499  /* Recheck worker status. */
500  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
501 
502  /*
503  * Check whether the worker slot is no longer used, which would mean
504  * that the worker has exited, or whether the worker generation is
505  * different, meaning that a different worker has taken the slot.
506  */
507  if (!worker->in_use || worker->generation != generation)
508  {
509  LWLockRelease(LogicalRepWorkerLock);
510  return;
511  }
512 
513  /* Worker has assigned proc, so it has started. */
514  if (worker->proc)
515  break;
516  }
517 
518  /* Now terminate the worker ... */
519  kill(worker->proc->pid, SIGTERM);
520 
521  /* ... and wait for it to die. */
522  for (;;)
523  {
524  int rc;
525 
526  /* is it gone? */
527  if (!worker->proc || worker->generation != generation)
528  break;
529 
530  LWLockRelease(LogicalRepWorkerLock);
531 
532  /* Wait a bit --- we don't expect to have to wait long. */
533  rc = WaitLatch(MyLatch,
536 
537  if (rc & WL_LATCH_SET)
538  {
541  }
542 
543  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
544  }
545 
546  LWLockRelease(LogicalRepWorkerLock);
547 }
548 
549 /*
550  * Request worker for specified sub/rel to be stopped on commit.
551  */
552 void
554 {
556  LogicalRepWorkerId *wid;
557  MemoryContext oldctx;
558 
559  /* Make sure we store the info in context that survives until commit. */
561 
562  /* Check that previous transactions were properly cleaned up. */
563  Assert(on_commit_stop_workers == NULL ||
564  nestDepth >= on_commit_stop_workers->nestDepth);
565 
566  /*
567  * Push a new stack element if we don't already have one for the current
568  * nestDepth.
569  */
570  if (on_commit_stop_workers == NULL ||
571  nestDepth > on_commit_stop_workers->nestDepth)
572  {
573  StopWorkersData *newdata = palloc(sizeof(StopWorkersData));
574 
575  newdata->nestDepth = nestDepth;
576  newdata->workers = NIL;
577  newdata->parent = on_commit_stop_workers;
578  on_commit_stop_workers = newdata;
579  }
580 
581  /*
582  * Finally add a new worker into the worker list of the current
583  * subtransaction.
584  */
585  wid = palloc(sizeof(LogicalRepWorkerId));
586  wid->subid = subid;
587  wid->relid = relid;
588  on_commit_stop_workers->workers =
589  lappend(on_commit_stop_workers->workers, wid);
590 
591  MemoryContextSwitchTo(oldctx);
592 }
593 
594 /*
595  * Wake up (using latch) any logical replication worker for specified sub/rel.
596  */
597 void
599 {
600  LogicalRepWorker *worker;
601 
602  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
603 
604  worker = logicalrep_worker_find(subid, relid, true);
605 
606  if (worker)
608 
609  LWLockRelease(LogicalRepWorkerLock);
610 }
611 
612 /*
613  * Wake up (using latch) the specified logical replication worker.
614  *
615  * Caller must hold lock, else worker->proc could change under us.
616  */
617 void
619 {
620  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
621 
622  SetLatch(&worker->proc->procLatch);
623 }
624 
625 /*
626  * Attach to a slot.
627  */
628 void
630 {
631  /* Block concurrent access. */
632  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
633 
634  Assert(slot >= 0 && slot < max_logical_replication_workers);
635  MyLogicalRepWorker = &LogicalRepCtx->workers[slot];
636 
637  if (!MyLogicalRepWorker->in_use)
638  {
639  LWLockRelease(LogicalRepWorkerLock);
640  ereport(ERROR,
641  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
642  errmsg("logical replication worker slot %d is empty, cannot attach",
643  slot)));
644  }
645 
646  if (MyLogicalRepWorker->proc)
647  {
648  LWLockRelease(LogicalRepWorkerLock);
649  ereport(ERROR,
650  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
651  errmsg("logical replication worker slot %d is already used by "
652  "another worker, cannot attach", slot)));
653  }
654 
655  MyLogicalRepWorker->proc = MyProc;
657 
658  LWLockRelease(LogicalRepWorkerLock);
659 }
660 
661 /*
662  * Detach the worker (cleans up the worker info).
663  */
664 static void
666 {
667  /* Block concurrent access. */
668  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
669 
670  logicalrep_worker_cleanup(MyLogicalRepWorker);
671 
672  LWLockRelease(LogicalRepWorkerLock);
673 }
674 
675 /*
676  * Clean up worker info.
677  */
678 static void
680 {
681  Assert(LWLockHeldByMeInMode(LogicalRepWorkerLock, LW_EXCLUSIVE));
682 
683  worker->in_use = false;
684  worker->proc = NULL;
685  worker->dbid = InvalidOid;
686  worker->userid = InvalidOid;
687  worker->subid = InvalidOid;
688  worker->relid = InvalidOid;
689 }
690 
691 /*
692  * Cleanup function for logical replication launcher.
693  *
694  * Called on logical replication launcher exit.
695  */
696 static void
698 {
699  LogicalRepCtx->launcher_pid = 0;
700 }
701 
702 /*
703  * Cleanup function.
704  *
705  * Called on logical replication worker exit.
706  */
707 static void
709 {
710  /* Disconnect gracefully from the remote side. */
711  if (wrconn)
713 
715 
717 }
718 
719 /*
720  * Count the number of registered (not necessarily running) sync workers
721  * for a subscription.
722  */
723 int
725 {
726  int i;
727  int res = 0;
728 
729  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
730 
731  /* Search for attached worker for a given subscription id. */
732  for (i = 0; i < max_logical_replication_workers; i++)
733  {
734  LogicalRepWorker *w = &LogicalRepCtx->workers[i];
735 
736  if (w->subid == subid && OidIsValid(w->relid))
737  res++;
738  }
739 
740  return res;
741 }
742 
743 /*
744  * ApplyLauncherShmemSize
745  * Compute space needed for replication launcher shared memory
746  */
747 Size
749 {
750  Size size;
751 
752  /*
753  * Need the fixed struct and the array of LogicalRepWorker.
754  */
755  size = sizeof(LogicalRepCtxStruct);
756  size = MAXALIGN(size);
758  sizeof(LogicalRepWorker)));
759  return size;
760 }
761 
762 /*
763  * ApplyLauncherRegister
764  * Register a background worker running the logical replication launcher.
765  */
766 void
768 {
769  BackgroundWorker bgw;
770 
772  return;
773 
774  memset(&bgw, 0, sizeof(bgw));
778  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
779  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
781  "logical replication launcher");
783  "logical replication launcher");
784  bgw.bgw_restart_time = 5;
785  bgw.bgw_notify_pid = 0;
786  bgw.bgw_main_arg = (Datum) 0;
787 
789 }
790 
791 /*
792  * ApplyLauncherShmemInit
793  * Allocate and initialize replication launcher shared memory
794  */
795 void
797 {
798  bool found;
799 
800  LogicalRepCtx = (LogicalRepCtxStruct *)
801  ShmemInitStruct("Logical Replication Launcher Data",
803  &found);
804 
805  if (!found)
806  {
807  int slot;
808 
809  memset(LogicalRepCtx, 0, ApplyLauncherShmemSize());
810 
811  /* Initialize memory and spin locks for each worker slot. */
812  for (slot = 0; slot < max_logical_replication_workers; slot++)
813  {
814  LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
815 
816  memset(worker, 0, sizeof(LogicalRepWorker));
817  SpinLockInit(&worker->relmutex);
818  }
819  }
820 }
821 
822 /*
823  * Check whether current transaction has manipulated logical replication
824  * workers.
825  */
826 bool
828 {
829  return (on_commit_stop_workers != NULL);
830 }
831 
832 /*
833  * Wakeup the launcher on commit if requested.
834  */
835 void
837 {
838 
839  Assert(on_commit_stop_workers == NULL ||
840  (on_commit_stop_workers->nestDepth == 1 &&
841  on_commit_stop_workers->parent == NULL));
842 
843  if (isCommit)
844  {
845  ListCell *lc;
846 
847  if (on_commit_stop_workers != NULL)
848  {
849  List *workers = on_commit_stop_workers->workers;
850 
851  foreach(lc, workers)
852  {
853  LogicalRepWorkerId *wid = lfirst(lc);
854 
855  logicalrep_worker_stop(wid->subid, wid->relid);
856  }
857  }
858 
861  }
862 
863  /*
864  * No need to pfree on_commit_stop_workers. It was allocated in
865  * transaction memory context, which is going to be cleaned soon.
866  */
867  on_commit_stop_workers = NULL;
869 }
870 
871 /*
872  * On commit, merge the current on_commit_stop_workers list into the
873  * immediate parent, if present.
874  * On rollback, discard the current on_commit_stop_workers list.
875  * Pop out the stack.
876  */
877 void
879 {
881 
882  /* Exit immediately if there's no work to do at this level. */
883  if (on_commit_stop_workers == NULL ||
884  on_commit_stop_workers->nestDepth < nestDepth)
885  return;
886 
887  Assert(on_commit_stop_workers->nestDepth == nestDepth);
888 
889  parent = on_commit_stop_workers->parent;
890 
891  if (isCommit)
892  {
893  /*
894  * If the upper stack element is not an immediate parent
895  * subtransaction, just decrement the notional nesting depth without
896  * doing any real work. Else, we need to merge the current workers
897  * list into the parent.
898  */
899  if (!parent || parent->nestDepth < nestDepth - 1)
900  {
901  on_commit_stop_workers->nestDepth--;
902  return;
903  }
904 
905  parent->workers =
906  list_concat(parent->workers, on_commit_stop_workers->workers);
907  }
908  else
909  {
910  /*
911  * Abandon everything that was done at this nesting level. Explicitly
912  * free memory to avoid a transaction-lifespan leak.
913  */
914  list_free_deep(on_commit_stop_workers->workers);
915  }
916 
917  /*
918  * We have taken care of the current subtransaction workers list for both
919  * abort or commit. So we are ready to pop the stack.
920  */
921  pfree(on_commit_stop_workers);
922  on_commit_stop_workers = parent;
923 }
924 
925 /*
926  * Request wakeup of the launcher on commit of the transaction.
927  *
928  * This is used to send launcher signal to stop sleeping and process the
929  * subscriptions when current transaction commits. Should be used when new
930  * tuple was added to the pg_subscription catalog.
931 */
932 void
934 {
937 }
938 
939 static void
941 {
942  if (LogicalRepCtx->launcher_pid != 0)
943  kill(LogicalRepCtx->launcher_pid, SIGUSR1);
944 }
945 
946 /*
947  * Main loop for the apply launcher process.
948  */
949 void
951 {
952  TimestampTz last_start_time = 0;
953 
954  ereport(DEBUG1,
955  (errmsg("logical replication launcher started")));
956 
958 
959  Assert(LogicalRepCtx->launcher_pid == 0);
960  LogicalRepCtx->launcher_pid = MyProcPid;
961 
962  /* Establish signal handlers. */
964  pqsignal(SIGTERM, die);
966 
967  /*
968  * Establish connection to nailed catalogs (we only ever access
969  * pg_subscription).
970  */
972 
973  /* Enter main loop */
974  for (;;)
975  {
976  int rc;
977  List *sublist;
978  ListCell *lc;
979  MemoryContext subctx;
980  MemoryContext oldctx;
982  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
983 
985 
986  now = GetCurrentTimestamp();
987 
988  /* Limit the start retry to once a wal_retrieve_retry_interval */
989  if (TimestampDifferenceExceeds(last_start_time, now,
991  {
992  /* Use temporary context for the database list and worker info. */
994  "Logical Replication Launcher sublist",
996  oldctx = MemoryContextSwitchTo(subctx);
997 
998  /* search for subscriptions to start or stop. */
999  sublist = get_subscription_list();
1000 
1001  /* Start the missing workers for enabled subscriptions. */
1002  foreach(lc, sublist)
1003  {
1004  Subscription *sub = (Subscription *) lfirst(lc);
1005  LogicalRepWorker *w;
1006 
1007  if (!sub->enabled)
1008  continue;
1009 
1010  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1011  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
1012  LWLockRelease(LogicalRepWorkerLock);
1013 
1014  if (w == NULL)
1015  {
1016  last_start_time = now;
1017  wait_time = wal_retrieve_retry_interval;
1018 
1019  logicalrep_worker_launch(sub->dbid, sub->oid, sub->name,
1020  sub->owner, InvalidOid);
1021  }
1022  }
1023 
1024  /* Switch back to original memory context. */
1025  MemoryContextSwitchTo(oldctx);
1026  /* Clean the temporary memory. */
1027  MemoryContextDelete(subctx);
1028  }
1029  else
1030  {
1031  /*
1032  * The wait in previous cycle was interrupted in less than
1033  * wal_retrieve_retry_interval since last worker was started, this
1034  * usually means crash of the worker, so we should retry in
1035  * wal_retrieve_retry_interval again.
1036  */
1037  wait_time = wal_retrieve_retry_interval;
1038  }
1039 
1040  /* Wait for more work. */
1041  rc = WaitLatch(MyLatch,
1043  wait_time,
1045 
1046  if (rc & WL_LATCH_SET)
1047  {
1050  }
1051 
1052  if (ConfigReloadPending)
1053  {
1054  ConfigReloadPending = false;
1056  }
1057  }
1058 
1059  /* Not reachable */
1060 }
1061 
1062 /*
1063  * Is current process the logical replication launcher?
1064  */
1065 bool
1067 {
1068  return LogicalRepCtx->launcher_pid == MyProcPid;
1069 }
1070 
1071 /*
1072  * Returns state of the subscriptions.
1073  */
1074 Datum
1076 {
1077 #define PG_STAT_GET_SUBSCRIPTION_COLS 8
1078  Oid subid = PG_ARGISNULL(0) ? InvalidOid : PG_GETARG_OID(0);
1079  int i;
1080  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1081  TupleDesc tupdesc;
1082  Tuplestorestate *tupstore;
1083  MemoryContext per_query_ctx;
1084  MemoryContext oldcontext;
1085 
1086  /* check to see if caller supports us returning a tuplestore */
1087  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1088  ereport(ERROR,
1089  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1090  errmsg("set-valued function called in context that cannot accept a set")));
1091  if (!(rsinfo->allowedModes & SFRM_Materialize))
1092  ereport(ERROR,
1093  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1094  errmsg("materialize mode required, but it is not allowed in this context")));
1095 
1096  /* Build a tuple descriptor for our result type */
1097  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1098  elog(ERROR, "return type must be a row type");
1099 
1100  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1101  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1102 
1103  tupstore = tuplestore_begin_heap(true, false, work_mem);
1104  rsinfo->returnMode = SFRM_Materialize;
1105  rsinfo->setResult = tupstore;
1106  rsinfo->setDesc = tupdesc;
1107 
1108  MemoryContextSwitchTo(oldcontext);
1109 
1110  /* Make sure we get consistent view of the workers. */
1111  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1112 
1113  for (i = 0; i <= max_logical_replication_workers; i++)
1114  {
1115  /* for each row */
1117  bool nulls[PG_STAT_GET_SUBSCRIPTION_COLS];
1118  int worker_pid;
1119  LogicalRepWorker worker;
1120 
1121  memcpy(&worker, &LogicalRepCtx->workers[i],
1122  sizeof(LogicalRepWorker));
1123  if (!worker.proc || !IsBackendPid(worker.proc->pid))
1124  continue;
1125 
1126  if (OidIsValid(subid) && worker.subid != subid)
1127  continue;
1128 
1129  worker_pid = worker.proc->pid;
1130 
1131  MemSet(values, 0, sizeof(values));
1132  MemSet(nulls, 0, sizeof(nulls));
1133 
1134  values[0] = ObjectIdGetDatum(worker.subid);
1135  if (OidIsValid(worker.relid))
1136  values[1] = ObjectIdGetDatum(worker.relid);
1137  else
1138  nulls[1] = true;
1139  values[2] = Int32GetDatum(worker_pid);
1140  if (XLogRecPtrIsInvalid(worker.last_lsn))
1141  nulls[3] = true;
1142  else
1143  values[3] = LSNGetDatum(worker.last_lsn);
1144  if (worker.last_send_time == 0)
1145  nulls[4] = true;
1146  else
1147  values[4] = TimestampTzGetDatum(worker.last_send_time);
1148  if (worker.last_recv_time == 0)
1149  nulls[5] = true;
1150  else
1151  values[5] = TimestampTzGetDatum(worker.last_recv_time);
1152  if (XLogRecPtrIsInvalid(worker.reply_lsn))
1153  nulls[6] = true;
1154  else
1155  values[6] = LSNGetDatum(worker.reply_lsn);
1156  if (worker.reply_time == 0)
1157  nulls[7] = true;
1158  else
1159  values[7] = TimestampTzGetDatum(worker.reply_time);
1160 
1161  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
1162 
1163  /*
1164  * If only a single subscription was requested, and we found it,
1165  * break.
1166  */
1167  if (OidIsValid(subid))
1168  break;
1169  }
1170 
1171  LWLockRelease(LogicalRepWorkerLock);
1172 
1173  /* clean up and return the tuplestore */
1174  tuplestore_donestoring(tupstore);
1175 
1176  return (Datum) 0;
1177 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:836
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
#define NIL
Definition: pg_list.h:65
WalReceiverConn * wrconn
Definition: worker.c:159
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:834
List * logicalrep_workers_find(Oid subid, bool only_running)
Definition: launcher.c:263
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
bool LWLockHeldByMeInMode(LWLock *l, LWLockMode mode)
Definition: lwlock.c:1946
#define AllocSetContextCreate
Definition: memutils.h:170
#define DEBUG1
Definition: elog.h:25
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:205
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
int MyProcPid
Definition: globals.c:40
int errhint(const char *fmt,...)
Definition: elog.c:1068
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
MemoryContext TopTransactionContext
Definition: mcxt.c:49
#define WL_TIMEOUT
Definition: latch.h:127
void ProcessConfigFile(GucContext context)
void ApplyLauncherMain(Datum main_arg)
Definition: launcher.c:950
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:112
#define SIGUSR1
Definition: win32_port.h:171
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1928
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1578
void logicalrep_worker_wakeup(Oid subid, Oid relid)
Definition: launcher.c:598
PGPROC * MyProc
Definition: proc.c:67
int64 TimestampTz
Definition: timestamp.h:39
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:56
char * pstrdup(const char *in)
Definition: mcxt.c:1187
void CommitTransactionCommand(void)
Definition: xact.c:2947
#define SpinLockInit(lock)
Definition: spin.h:60
List * workers
Definition: launcher.c:79
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:65
TimestampTz last_send_time
XLogRecPtr last_lsn
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:284
int bgw_restart_time
Definition: bgworker.h:94
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
int errcode(int sqlerrcode)
Definition: elog.c:610
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
static StopWorkersData * on_commit_stop_workers
Definition: launcher.c:88
#define MemSet(start, val, len)
Definition: c.h:950
#define kill(pid, sig)
Definition: win32_port.h:454
FormData_pg_subscription * Form_pg_subscription
unsigned int Oid
Definition: postgres_ext.h:31
void SetLatch(Latch *latch)
Definition: latch.c:505
NameData subname
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:52
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1677
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
#define OidIsValid(objectId)
Definition: c.h:652
void list_free_deep(List *list)
Definition: list.c:1390
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:96
void ResetLatch(Latch *latch)
Definition: latch.c:588
int wal_receiver_timeout
Definition: walreceiver.c:89
Latch procLatch
Definition: proc.h:121
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:390
XLogRecPtr relstate_lsn
static void logicalrep_worker_detach(void)
Definition: launcher.c:665
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition: launcher.c:618
Datum bgw_main_arg
Definition: bgworker.h:97
unsigned short uint16
Definition: c.h:374
void pfree(void *pointer)
Definition: mcxt.c:1057
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
int max_sync_workers_per_subscription
Definition: launcher.c:55
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:392
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
XLogRecPtr reply_lsn
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
static void logicalrep_worker_cleanup(LogicalRepWorker *worker)
Definition: launcher.c:679
void logicalrep_worker_attach(int slot)
Definition: launcher.c:629
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1286
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
void logicalrep_worker_stop(Oid subid, Oid relid)
Definition: launcher.c:456
void AtEOSubXact_ApplyLauncher(bool isCommit, int nestDepth)
Definition: launcher.c:878
#define SIGHUP
Definition: win32_port.h:159
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:748
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:333
#define BGW_NEVER_RESTART
Definition: bgworker.h:84
static void logicalrep_launcher_onexit(int code, Datum arg)
Definition: launcher.c:697
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
BgwHandleStatus
Definition: bgworker.h:102
static bool on_commit_launcher_wakeup
Definition: launcher.c:96
MemoryContext TopMemoryContext
Definition: mcxt.c:44
Definition: guc.h:72
List * lappend(List *list, void *datum)
Definition: list.c:321
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define WARNING
Definition: elog.h:40
int wal_retrieve_retry_interval
Definition: xlog.c:110
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
void BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
Definition: postmaster.c:5736
Size mul_size(Size s1, Size s2)
Definition: shmem.c:515
static void logicalrep_worker_onexit(int code, Datum arg)
Definition: launcher.c:708
void * palloc0(Size size)
Definition: mcxt.c:981
#define DEFAULT_NAPTIME_PER_CYCLE
Definition: launcher.c:52
uintptr_t Datum
Definition: postgres.h:367
void ApplyLauncherRegister(void)
Definition: launcher.c:767
#define PG_STAT_GET_SUBSCRIPTION_COLS
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
int work_mem
Definition: globals.c:121
int logicalrep_sync_worker_count(Oid subid)
Definition: launcher.c:724
static void WaitForReplicationWorkerAttach(LogicalRepWorker *worker, uint16 generation, BackgroundWorkerHandle *handle)
Definition: launcher.c:177
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
int allowedModes
Definition: execnodes.h:304
TimestampTz launch_time
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
static List * get_subscription_list(void)
Definition: launcher.c:108
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:857
struct StopWorkersData StopWorkersData
SetFunctionReturnMode returnMode
Definition: execnodes.h:306
int max_replication_slots
Definition: slot.c:99
TimestampTz last_recv_time
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
Definition: launcher.c:286
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define Assert(condition)
Definition: c.h:746
#define lfirst(lc)
Definition: pg_list.h:169
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:235
void StartTransactionCommand(void)
Definition: xact.c:2846
int max_logical_replication_workers
Definition: launcher.c:54
#define BGW_MAXLEN
Definition: bgworker.h:85
size_t Size
Definition: c.h:474
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:93
struct StopWorkersData * parent
Definition: launcher.c:80
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:918
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
#define MAXALIGN(LEN)
Definition: c.h:699
void ApplyLauncherShmemInit(void)
Definition: launcher.c:796
#define walrcv_disconnect(conn)
Definition: walreceiver.h:426
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:232
struct LogicalRepCtxStruct LogicalRepCtxStruct
Tuplestorestate * setResult
Definition: execnodes.h:309
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:863
static Datum values[MAXATTR]
Definition: bootstrap.c:165
ExprContext * econtext
Definition: execnodes.h:302
#define Int32GetDatum(X)
Definition: postgres.h:479
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:91
TupleDesc setDesc
Definition: execnodes.h:310
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:821
pid_t bgw_notify_pid
Definition: bgworker.h:99
bool IsLogicalLauncher(void)
Definition: launcher.c:1066
#define elog(elevel,...)
Definition: elog.h:214
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:68
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:26
int i
#define NameStr(name)
Definition: c.h:623
bool IsBackendPid(int pid)
Definition: procarray.c:3127
void * arg
struct Latch * MyLatch
Definition: globals.c:54
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
Datum pg_stat_get_subscription(PG_FUNCTION_ARGS)
Definition: launcher.c:1075
void logicalrep_worker_stop_at_commit(Oid subid, Oid relid)
Definition: launcher.c:553
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
static void ApplyLauncherWakeup(void)
Definition: launcher.c:940
void ApplyLauncherWakeupAtCommit(void)
Definition: launcher.c:933
bool XactManipulatesLogicalReplicationWorkers(void)
Definition: launcher.c:827
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:95
Definition: pg_list.h:50
#define snprintf
Definition: port.h:215
int pid
Definition: proc.h:137
#define WL_LATCH_SET
Definition: latch.h:124
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1542
#define die(msg)
Definition: pg_test_fsync.c:97
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1030
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129
TimestampTz reply_time
struct LogicalRepWorkerId LogicalRepWorkerId
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5788