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-2022, 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/builtins.h"
46 #include "utils/memutils.h"
47 #include "utils/pg_lsn.h"
48 #include "utils/ps_status.h"
49 #include "utils/snapmgr.h"
50 #include "utils/timeout.h"
51 
52 /* max sleep time between cycles (3min) */
53 #define DEFAULT_NAPTIME_PER_CYCLE 180000L
54 
55 /* GUC variables */
58 
60 
61 typedef struct LogicalRepCtxStruct
62 {
63  /* Supervisor process. */
64  pid_t launcher_pid;
65 
66  /* Background workers. */
69 
71 
72 static void ApplyLauncherWakeup(void);
73 static void logicalrep_launcher_onexit(int code, Datum arg);
74 static void logicalrep_worker_onexit(int code, Datum arg);
75 static void logicalrep_worker_detach(void);
76 static void logicalrep_worker_cleanup(LogicalRepWorker *worker);
77 
78 static bool on_commit_launcher_wakeup = false;
79 
80 
81 /*
82  * Load the list of subscriptions.
83  *
84  * Only the fields interesting for worker start/stop functions are filled for
85  * each subscription.
86  */
87 static List *
89 {
90  List *res = NIL;
91  Relation rel;
92  TableScanDesc scan;
93  HeapTuple tup;
94  MemoryContext resultcxt;
95 
96  /* This is the context that we will allocate our output data in */
97  resultcxt = CurrentMemoryContext;
98 
99  /*
100  * Start a transaction so we can access pg_database, and get a snapshot.
101  * We don't have a use for the snapshot itself, but we're interested in
102  * the secondary effect that it sets RecentGlobalXmin. (This is critical
103  * for anything that reads heap pages, because HOT may decide to prune
104  * them even if the process doesn't attempt to modify any tuples.)
105  *
106  * FIXME: This comment is inaccurate / the code buggy. A snapshot that is
107  * not pushed/active does not reliably prevent HOT pruning (->xmin could
108  * e.g. be cleared when cache invalidations are processed).
109  */
111  (void) GetTransactionSnapshot();
112 
113  rel = table_open(SubscriptionRelationId, AccessShareLock);
114  scan = table_beginscan_catalog(rel, 0, NULL);
115 
117  {
119  Subscription *sub;
120  MemoryContext oldcxt;
121 
122  /*
123  * Allocate our results in the caller's context, not the
124  * transaction's. We do this inside the loop, and restore the original
125  * context at the end, so that leaky things like heap_getnext() are
126  * not called in a potentially long-lived context.
127  */
128  oldcxt = MemoryContextSwitchTo(resultcxt);
129 
130  sub = (Subscription *) palloc0(sizeof(Subscription));
131  sub->oid = subform->oid;
132  sub->dbid = subform->subdbid;
133  sub->owner = subform->subowner;
134  sub->enabled = subform->subenabled;
135  sub->name = pstrdup(NameStr(subform->subname));
136  /* We don't fill fields we are not interested in. */
137 
138  res = lappend(res, sub);
139  MemoryContextSwitchTo(oldcxt);
140  }
141 
142  table_endscan(scan);
144 
146 
147  return res;
148 }
149 
150 /*
151  * Wait for a background worker to start up and attach to the shmem context.
152  *
153  * This is only needed for cleaning up the shared memory in case the worker
154  * fails to attach.
155  */
156 static void
158  uint16 generation,
159  BackgroundWorkerHandle *handle)
160 {
162  int rc;
163 
164  for (;;)
165  {
166  pid_t pid;
167 
169 
170  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
171 
172  /* Worker either died or has started; no need to do anything. */
173  if (!worker->in_use || worker->proc)
174  {
175  LWLockRelease(LogicalRepWorkerLock);
176  return;
177  }
178 
179  LWLockRelease(LogicalRepWorkerLock);
180 
181  /* Check if worker has died before attaching, and clean up after it. */
182  status = GetBackgroundWorkerPid(handle, &pid);
183 
184  if (status == BGWH_STOPPED)
185  {
186  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
187  /* Ensure that this was indeed the worker we waited for. */
188  if (generation == worker->generation)
190  LWLockRelease(LogicalRepWorkerLock);
191  return;
192  }
193 
194  /*
195  * We need timeout because we generally don't get notified via latch
196  * about the worker attach. But we don't expect to have to wait long.
197  */
198  rc = WaitLatch(MyLatch,
201 
202  if (rc & WL_LATCH_SET)
203  {
206  }
207  }
208 }
209 
210 /*
211  * Walks the workers array and searches for one that matches given
212  * subscription id and relid.
213  */
215 logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
216 {
217  int i;
218  LogicalRepWorker *res = NULL;
219 
220  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
221 
222  /* Search for attached worker for a given subscription id. */
223  for (i = 0; i < max_logical_replication_workers; i++)
224  {
226 
227  if (w->in_use && w->subid == subid && w->relid == relid &&
228  (!only_running || w->proc))
229  {
230  res = w;
231  break;
232  }
233  }
234 
235  return res;
236 }
237 
238 /*
239  * Similar to logicalrep_worker_find(), but returns a list of all workers for
240  * the subscription, instead of just one.
241  */
242 List *
243 logicalrep_workers_find(Oid subid, bool only_running)
244 {
245  int i;
246  List *res = NIL;
247 
248  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
249 
250  /* Search for attached worker for a given subscription id. */
251  for (i = 0; i < max_logical_replication_workers; i++)
252  {
254 
255  if (w->in_use && w->subid == subid && (!only_running || w->proc))
256  res = lappend(res, w);
257  }
258 
259  return res;
260 }
261 
262 /*
263  * Start new apply background worker, if possible.
264  */
265 void
266 logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid,
267  Oid relid)
268 {
269  BackgroundWorker bgw;
270  BackgroundWorkerHandle *bgw_handle;
271  uint16 generation;
272  int i;
273  int slot = 0;
274  LogicalRepWorker *worker = NULL;
275  int nsyncworkers;
277 
278  ereport(DEBUG1,
279  (errmsg_internal("starting logical replication worker for subscription \"%s\"",
280  subname)));
281 
282  /* Report this after the initial starting message for consistency. */
283  if (max_replication_slots == 0)
284  ereport(ERROR,
285  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
286  errmsg("cannot start logical replication workers when max_replication_slots = 0")));
287 
288  /*
289  * We need to do the modification of the shared memory under lock so that
290  * we have consistent view.
291  */
292  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
293 
294 retry:
295  /* Find unused worker slot. */
296  for (i = 0; i < max_logical_replication_workers; i++)
297  {
299 
300  if (!w->in_use)
301  {
302  worker = w;
303  slot = i;
304  break;
305  }
306  }
307 
308  nsyncworkers = logicalrep_sync_worker_count(subid);
309 
311 
312  /*
313  * If we didn't find a free slot, try to do garbage collection. The
314  * reason we do this is because if some worker failed to start up and its
315  * parent has crashed while waiting, the in_use state was never cleared.
316  */
317  if (worker == NULL || nsyncworkers >= max_sync_workers_per_subscription)
318  {
319  bool did_cleanup = false;
320 
321  for (i = 0; i < max_logical_replication_workers; i++)
322  {
324 
325  /*
326  * If the worker was marked in use but didn't manage to attach in
327  * time, clean it up.
328  */
329  if (w->in_use && !w->proc &&
332  {
333  elog(WARNING,
334  "logical replication worker for subscription %u took too long to start; canceled",
335  w->subid);
336 
338  did_cleanup = true;
339  }
340  }
341 
342  if (did_cleanup)
343  goto retry;
344  }
345 
346  /*
347  * We don't allow to invoke more sync workers once we have reached the
348  * sync worker limit per subscription. So, just return silently as we
349  * might get here because of an otherwise harmless race condition.
350  */
351  if (OidIsValid(relid) && nsyncworkers >= max_sync_workers_per_subscription)
352  {
353  LWLockRelease(LogicalRepWorkerLock);
354  return;
355  }
356 
357  /*
358  * However if there are no more free worker slots, inform user about it
359  * before exiting.
360  */
361  if (worker == NULL)
362  {
363  LWLockRelease(LogicalRepWorkerLock);
365  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
366  errmsg("out of logical replication worker slots"),
367  errhint("You might need to increase max_logical_replication_workers.")));
368  return;
369  }
370 
371  /* Prepare the worker slot. */
372  worker->launch_time = now;
373  worker->in_use = true;
374  worker->generation++;
375  worker->proc = NULL;
376  worker->dbid = dbid;
377  worker->userid = userid;
378  worker->subid = subid;
379  worker->relid = relid;
380  worker->relstate = SUBREL_STATE_UNKNOWN;
382  worker->stream_fileset = NULL;
383  worker->last_lsn = InvalidXLogRecPtr;
386  worker->reply_lsn = InvalidXLogRecPtr;
387  TIMESTAMP_NOBEGIN(worker->reply_time);
388 
389  /* Before releasing lock, remember generation for future identification. */
390  generation = worker->generation;
391 
392  LWLockRelease(LogicalRepWorkerLock);
393 
394  /* Register the new dynamic worker. */
395  memset(&bgw, 0, sizeof(bgw));
399  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
400  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyWorkerMain");
401  if (OidIsValid(relid))
403  "logical replication worker for subscription %u sync %u", subid, relid);
404  else
406  "logical replication worker for subscription %u", subid);
407  snprintf(bgw.bgw_type, BGW_MAXLEN, "logical replication worker");
408 
411  bgw.bgw_main_arg = Int32GetDatum(slot);
412 
413  if (!RegisterDynamicBackgroundWorker(&bgw, &bgw_handle))
414  {
415  /* Failed to start worker, so clean up the worker slot. */
416  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
417  Assert(generation == worker->generation);
419  LWLockRelease(LogicalRepWorkerLock);
420 
422  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
423  errmsg("out of background worker slots"),
424  errhint("You might need to increase max_worker_processes.")));
425  return;
426  }
427 
428  /* Now wait until it attaches. */
429  WaitForReplicationWorkerAttach(worker, generation, bgw_handle);
430 }
431 
432 /*
433  * Stop the logical replication worker for subid/relid, if any, and wait until
434  * it detaches from the slot.
435  */
436 void
438 {
439  LogicalRepWorker *worker;
440  uint16 generation;
441 
442  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
443 
444  worker = logicalrep_worker_find(subid, relid, false);
445 
446  /* No worker, nothing to do. */
447  if (!worker)
448  {
449  LWLockRelease(LogicalRepWorkerLock);
450  return;
451  }
452 
453  /*
454  * Remember which generation was our worker so we can check if what we see
455  * is still the same one.
456  */
457  generation = worker->generation;
458 
459  /*
460  * If we found a worker but it does not have proc set then it is still
461  * starting up; wait for it to finish starting and then kill it.
462  */
463  while (worker->in_use && !worker->proc)
464  {
465  int rc;
466 
467  LWLockRelease(LogicalRepWorkerLock);
468 
469  /* Wait a bit --- we don't expect to have to wait long. */
470  rc = WaitLatch(MyLatch,
473 
474  if (rc & WL_LATCH_SET)
475  {
478  }
479 
480  /* Recheck worker status. */
481  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
482 
483  /*
484  * Check whether the worker slot is no longer used, which would mean
485  * that the worker has exited, or whether the worker generation is
486  * different, meaning that a different worker has taken the slot.
487  */
488  if (!worker->in_use || worker->generation != generation)
489  {
490  LWLockRelease(LogicalRepWorkerLock);
491  return;
492  }
493 
494  /* Worker has assigned proc, so it has started. */
495  if (worker->proc)
496  break;
497  }
498 
499  /* Now terminate the worker ... */
500  kill(worker->proc->pid, SIGTERM);
501 
502  /* ... and wait for it to die. */
503  for (;;)
504  {
505  int rc;
506 
507  /* is it gone? */
508  if (!worker->proc || worker->generation != generation)
509  break;
510 
511  LWLockRelease(LogicalRepWorkerLock);
512 
513  /* Wait a bit --- we don't expect to have to wait long. */
514  rc = WaitLatch(MyLatch,
517 
518  if (rc & WL_LATCH_SET)
519  {
522  }
523 
524  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
525  }
526 
527  LWLockRelease(LogicalRepWorkerLock);
528 }
529 
530 /*
531  * Wake up (using latch) any logical replication worker for specified sub/rel.
532  */
533 void
535 {
536  LogicalRepWorker *worker;
537 
538  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
539 
540  worker = logicalrep_worker_find(subid, relid, true);
541 
542  if (worker)
544 
545  LWLockRelease(LogicalRepWorkerLock);
546 }
547 
548 /*
549  * Wake up (using latch) the specified logical replication worker.
550  *
551  * Caller must hold lock, else worker->proc could change under us.
552  */
553 void
555 {
556  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
557 
558  SetLatch(&worker->proc->procLatch);
559 }
560 
561 /*
562  * Attach to a slot.
563  */
564 void
566 {
567  /* Block concurrent access. */
568  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
569 
570  Assert(slot >= 0 && slot < max_logical_replication_workers);
572 
574  {
575  LWLockRelease(LogicalRepWorkerLock);
576  ereport(ERROR,
577  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
578  errmsg("logical replication worker slot %d is empty, cannot attach",
579  slot)));
580  }
581 
583  {
584  LWLockRelease(LogicalRepWorkerLock);
585  ereport(ERROR,
586  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
587  errmsg("logical replication worker slot %d is already used by "
588  "another worker, cannot attach", slot)));
589  }
590 
593 
594  LWLockRelease(LogicalRepWorkerLock);
595 }
596 
597 /*
598  * Detach the worker (cleans up the worker info).
599  */
600 static void
602 {
603  /* Block concurrent access. */
604  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
605 
607 
608  LWLockRelease(LogicalRepWorkerLock);
609 }
610 
611 /*
612  * Clean up worker info.
613  */
614 static void
616 {
617  Assert(LWLockHeldByMeInMode(LogicalRepWorkerLock, LW_EXCLUSIVE));
618 
619  worker->in_use = false;
620  worker->proc = NULL;
621  worker->dbid = InvalidOid;
622  worker->userid = InvalidOid;
623  worker->subid = InvalidOid;
624  worker->relid = InvalidOid;
625 }
626 
627 /*
628  * Cleanup function for logical replication launcher.
629  *
630  * Called on logical replication launcher exit.
631  */
632 static void
634 {
636 }
637 
638 /*
639  * Cleanup function.
640  *
641  * Called on logical replication worker exit.
642  */
643 static void
645 {
646  /* Disconnect gracefully from the remote side. */
649 
651 
652  /* Cleanup fileset used for streaming transactions. */
653  if (MyLogicalRepWorker->stream_fileset != NULL)
655 
657 }
658 
659 /*
660  * Count the number of registered (not necessarily running) sync workers
661  * for a subscription.
662  */
663 int
665 {
666  int i;
667  int res = 0;
668 
669  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
670 
671  /* Search for attached worker for a given subscription id. */
672  for (i = 0; i < max_logical_replication_workers; i++)
673  {
675 
676  if (w->subid == subid && OidIsValid(w->relid))
677  res++;
678  }
679 
680  return res;
681 }
682 
683 /*
684  * ApplyLauncherShmemSize
685  * Compute space needed for replication launcher shared memory
686  */
687 Size
689 {
690  Size size;
691 
692  /*
693  * Need the fixed struct and the array of LogicalRepWorker.
694  */
695  size = sizeof(LogicalRepCtxStruct);
696  size = MAXALIGN(size);
698  sizeof(LogicalRepWorker)));
699  return size;
700 }
701 
702 /*
703  * ApplyLauncherRegister
704  * Register a background worker running the logical replication launcher.
705  */
706 void
708 {
709  BackgroundWorker bgw;
710 
712  return;
713 
714  memset(&bgw, 0, sizeof(bgw));
718  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
719  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
721  "logical replication launcher");
723  "logical replication launcher");
724  bgw.bgw_restart_time = 5;
725  bgw.bgw_notify_pid = 0;
726  bgw.bgw_main_arg = (Datum) 0;
727 
729 }
730 
731 /*
732  * ApplyLauncherShmemInit
733  * Allocate and initialize replication launcher shared memory
734  */
735 void
737 {
738  bool found;
739 
741  ShmemInitStruct("Logical Replication Launcher Data",
743  &found);
744 
745  if (!found)
746  {
747  int slot;
748 
750 
751  /* Initialize memory and spin locks for each worker slot. */
752  for (slot = 0; slot < max_logical_replication_workers; slot++)
753  {
754  LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
755 
756  memset(worker, 0, sizeof(LogicalRepWorker));
757  SpinLockInit(&worker->relmutex);
758  }
759  }
760 }
761 
762 /*
763  * Wakeup the launcher on commit if requested.
764  */
765 void
767 {
768  if (isCommit)
769  {
772  }
773 
775 }
776 
777 /*
778  * Request wakeup of the launcher on commit of the transaction.
779  *
780  * This is used to send launcher signal to stop sleeping and process the
781  * subscriptions when current transaction commits. Should be used when new
782  * tuple was added to the pg_subscription catalog.
783 */
784 void
786 {
789 }
790 
791 static void
793 {
794  if (LogicalRepCtx->launcher_pid != 0)
796 }
797 
798 /*
799  * Main loop for the apply launcher process.
800  */
801 void
803 {
804  TimestampTz last_start_time = 0;
805 
806  ereport(DEBUG1,
807  (errmsg_internal("logical replication launcher started")));
808 
810 
813 
814  /* Establish signal handlers. */
816  pqsignal(SIGTERM, die);
818 
819  /*
820  * Establish connection to nailed catalogs (we only ever access
821  * pg_subscription).
822  */
824 
825  /* Enter main loop */
826  for (;;)
827  {
828  int rc;
829  List *sublist;
830  ListCell *lc;
831  MemoryContext subctx;
832  MemoryContext oldctx;
834  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
835 
837 
839 
840  /* Limit the start retry to once a wal_retrieve_retry_interval */
841  if (TimestampDifferenceExceeds(last_start_time, now,
843  {
844  /* Use temporary context for the database list and worker info. */
846  "Logical Replication Launcher sublist",
848  oldctx = MemoryContextSwitchTo(subctx);
849 
850  /* search for subscriptions to start or stop. */
851  sublist = get_subscription_list();
852 
853  /* Start the missing workers for enabled subscriptions. */
854  foreach(lc, sublist)
855  {
856  Subscription *sub = (Subscription *) lfirst(lc);
857  LogicalRepWorker *w;
858 
859  if (!sub->enabled)
860  continue;
861 
862  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
863  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
864  LWLockRelease(LogicalRepWorkerLock);
865 
866  if (w == NULL)
867  {
868  last_start_time = now;
869  wait_time = wal_retrieve_retry_interval;
870 
871  logicalrep_worker_launch(sub->dbid, sub->oid, sub->name,
872  sub->owner, InvalidOid);
873  }
874  }
875 
876  /* Switch back to original memory context. */
877  MemoryContextSwitchTo(oldctx);
878  /* Clean the temporary memory. */
879  MemoryContextDelete(subctx);
880  }
881  else
882  {
883  /*
884  * The wait in previous cycle was interrupted in less than
885  * wal_retrieve_retry_interval since last worker was started, this
886  * usually means crash of the worker, so we should retry in
887  * wal_retrieve_retry_interval again.
888  */
889  wait_time = wal_retrieve_retry_interval;
890  }
891 
892  /* Wait for more work. */
893  rc = WaitLatch(MyLatch,
895  wait_time,
897 
898  if (rc & WL_LATCH_SET)
899  {
902  }
903 
905  {
906  ConfigReloadPending = false;
908  }
909  }
910 
911  /* Not reachable */
912 }
913 
914 /*
915  * Is current process the logical replication launcher?
916  */
917 bool
919 {
921 }
922 
923 /*
924  * Returns state of the subscriptions.
925  */
926 Datum
928 {
929 #define PG_STAT_GET_SUBSCRIPTION_COLS 8
930  Oid subid = PG_ARGISNULL(0) ? InvalidOid : PG_GETARG_OID(0);
931  int i;
932  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
933 
934  InitMaterializedSRF(fcinfo, 0);
935 
936  /* Make sure we get consistent view of the workers. */
937  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
938 
939  for (i = 0; i < max_logical_replication_workers; i++)
940  {
941  /* for each row */
943  bool nulls[PG_STAT_GET_SUBSCRIPTION_COLS] = {0};
944  int worker_pid;
945  LogicalRepWorker worker;
946 
947  memcpy(&worker, &LogicalRepCtx->workers[i],
948  sizeof(LogicalRepWorker));
949  if (!worker.proc || !IsBackendPid(worker.proc->pid))
950  continue;
951 
952  if (OidIsValid(subid) && worker.subid != subid)
953  continue;
954 
955  worker_pid = worker.proc->pid;
956 
957  values[0] = ObjectIdGetDatum(worker.subid);
958  if (OidIsValid(worker.relid))
959  values[1] = ObjectIdGetDatum(worker.relid);
960  else
961  nulls[1] = true;
962  values[2] = Int32GetDatum(worker_pid);
963  if (XLogRecPtrIsInvalid(worker.last_lsn))
964  nulls[3] = true;
965  else
966  values[3] = LSNGetDatum(worker.last_lsn);
967  if (worker.last_send_time == 0)
968  nulls[4] = true;
969  else
971  if (worker.last_recv_time == 0)
972  nulls[5] = true;
973  else
975  if (XLogRecPtrIsInvalid(worker.reply_lsn))
976  nulls[6] = true;
977  else
978  values[6] = LSNGetDatum(worker.reply_lsn);
979  if (worker.reply_time == 0)
980  nulls[7] = true;
981  else
983 
984  tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
985  values, nulls);
986 
987  /*
988  * If only a single subscription was requested, and we found it,
989  * break.
990  */
991  if (OidIsValid(subid))
992  break;
993  }
994 
995  LWLockRelease(LogicalRepWorkerLock);
996 
997  return (Datum) 0;
998 }
WalReceiverConn * LogRepWorkerWalRcvConn
Definition: worker.c:251
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1719
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1573
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1537
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:872
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1068
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:956
#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:156
#define NameStr(name)
Definition: c.h:682
unsigned short uint16
Definition: c.h:441
#define MAXALIGN(LEN)
Definition: c.h:747
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:362
#define OidIsValid(objectId)
Definition: c.h:711
size_t Size
Definition: c.h:541
int64 TimestampTz
Definition: timestamp.h:39
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:151
int errmsg_internal(const char *fmt,...)
Definition: elog.c:993
int errhint(const char *fmt,...)
Definition: elog.c:1153
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define WARNING
Definition: elog.h:32
#define DEBUG1
Definition: elog.h:26
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
void FileSetDeleteAll(FileSet *fileset)
Definition: fileset.c:152
#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
int MyProcPid
Definition: globals.c:44
struct Latch * MyLatch
Definition: globals.c:58
@ PGC_SIGHUP
Definition: guc.h:71
void ProcessConfigFile(GucContext context)
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1299
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
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:333
int i
Definition: isn.c:73
void SetLatch(Latch *latch)
Definition: latch.c:591
void ResetLatch(Latch *latch)
Definition: latch.c:683
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:476
#define WL_TIMEOUT
Definition: latch.h:128
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130
#define WL_LATCH_SET
Definition: latch.h:125
Datum pg_stat_get_subscription(PG_FUNCTION_ARGS)
Definition: launcher.c:927
#define DEFAULT_NAPTIME_PER_CYCLE
Definition: launcher.c:53
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:215
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:766
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition: launcher.c:554
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:688
bool IsLogicalLauncher(void)
Definition: launcher.c:918
static void WaitForReplicationWorkerAttach(LogicalRepWorker *worker, uint16 generation, BackgroundWorkerHandle *handle)
Definition: launcher.c:157
void logicalrep_worker_attach(int slot)
Definition: launcher.c:565
static List * get_subscription_list(void)
Definition: launcher.c:88
static void logicalrep_launcher_onexit(int code, Datum arg)
Definition: launcher.c:633
void logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
Definition: launcher.c:266
void ApplyLauncherMain(Datum main_arg)
Definition: launcher.c:802
#define PG_STAT_GET_SUBSCRIPTION_COLS
int max_logical_replication_workers
Definition: launcher.c:56
List * logicalrep_workers_find(Oid subid, bool only_running)
Definition: launcher.c:243
static bool on_commit_launcher_wakeup
Definition: launcher.c:78
struct LogicalRepCtxStruct LogicalRepCtxStruct
void logicalrep_worker_wakeup(Oid subid, Oid relid)
Definition: launcher.c:534
void ApplyLauncherShmemInit(void)
Definition: launcher.c:736
void logicalrep_worker_stop(Oid subid, Oid relid)
Definition: launcher.c:437
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:59
void ApplyLauncherWakeupAtCommit(void)
Definition: launcher.c:785
static LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:70
static void logicalrep_worker_onexit(int code, Datum arg)
Definition: launcher.c:644
int max_sync_workers_per_subscription
Definition: launcher.c:57
static void logicalrep_worker_detach(void)
Definition: launcher.c:601
int logicalrep_sync_worker_count(Oid subid)
Definition: launcher.c:664
void ApplyLauncherRegister(void)
Definition: launcher.c:707
static void ApplyLauncherWakeup(void)
Definition: launcher.c:792
static void logicalrep_worker_cleanup(LogicalRepWorker *worker)
Definition: launcher.c:615
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:338
#define AccessShareLock
Definition: lockdefs.h:36
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1918
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1194
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1962
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1802
@ LW_SHARED
Definition: lwlock.h:113
@ LW_EXCLUSIVE
Definition: lwlock.h:112
char * pstrdup(const char *in)
Definition: mcxt.c:1483
MemoryContext TopMemoryContext
Definition: mcxt.c:130
void * palloc0(Size size)
Definition: mcxt.c:1230
MemoryContext CurrentMemoryContext
Definition: mcxt.c:124
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:376
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:135
void * arg
#define lfirst(lc)
Definition: pg_list.h:170
#define NIL
Definition: pg_list.h:66
static Datum LSNGetDatum(XLogRecPtr X)
Definition: pg_lsn.h:28
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
NameData subname
FormData_pg_subscription * Form_pg_subscription
#define die(msg)
Definition: pg_test_fsync.c:95
pqsigfunc pqsignal(int signo, pqsigfunc func)
#define snprintf
Definition: port.h:238
uintptr_t Datum
Definition: postgres.h:412
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:600
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:560
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
void BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
Definition: postmaster.c:5541
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5601
bool IsBackendPid(int pid)
Definition: procarray.c:3257
@ ForwardScanDirection
Definition: sdir.h:26
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
int max_replication_slots
Definition: slot.c:101
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:251
#define SpinLockInit(lock)
Definition: spin.h:60
PGPROC * MyProc
Definition: proc.c:68
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
pid_t bgw_notify_pid
Definition: bgworker.h:100
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:96
Definition: pg_list.h:52
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:67
XLogRecPtr relstate_lsn
TimestampTz last_recv_time
TimestampTz launch_time
TimestampTz reply_time
FileSet * stream_fileset
XLogRecPtr reply_lsn
XLogRecPtr last_lsn
TimestampTz last_send_time
int pid
Definition: proc.h:186
Latch procLatch
Definition: proc.h:170
TupleDesc setDesc
Definition: execnodes.h:332
Tuplestorestate * setResult
Definition: execnodes.h:331
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, struct ScanKeyData *key)
Definition: tableam.c:112
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:993
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
static Datum TimestampTzGetDatum(TimestampTz X)
Definition: timestamp.h:52
@ WAIT_EVENT_LOGICAL_LAUNCHER_MAIN
Definition: wait_event.h:44
@ WAIT_EVENT_BGWORKER_STARTUP
Definition: wait_event.h:87
@ WAIT_EVENT_BGWORKER_SHUTDOWN
Definition: wait_event.h:86
int wal_receiver_timeout
Definition: walreceiver.c:91
#define walrcv_disconnect(conn)
Definition: walreceiver.h:436
#define SIGHUP
Definition: win32_port.h:176
#define kill(pid, sig)
Definition: win32_port.h:482
#define SIGUSR1
Definition: win32_port.h:188
void StartTransactionCommand(void)
Definition: xact.c:2925
void CommitTransactionCommand(void)
Definition: xact.c:3022
int wal_retrieve_retry_interval
Definition: xlog.c:137
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28