PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
worker_internal.h File Reference
#include <signal.h>
#include "access/xlogdefs.h"
#include "catalog/pg_subscription.h"
#include "datatype/timestamp.h"
#include "storage/lock.h"
Include dependency graph for worker_internal.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  LogicalRepWorker
 

Typedefs

typedef struct LogicalRepWorker LogicalRepWorker
 

Functions

void logicalrep_worker_attach (int slot)
 
LogicalRepWorkerlogicalrep_worker_find (Oid subid, Oid relid, bool only_running)
 
void logicalrep_worker_launch (Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
 
void logicalrep_worker_stop (Oid subid, Oid relid)
 
void logicalrep_worker_wakeup (Oid subid, Oid relid)
 
void logicalrep_worker_wakeup_ptr (LogicalRepWorker *worker)
 
int logicalrep_sync_worker_count (Oid subid)
 
void logicalrep_worker_sighup (SIGNAL_ARGS)
 
void logicalrep_worker_sigterm (SIGNAL_ARGS)
 
char * LogicalRepSyncTableStart (XLogRecPtr *origin_startpos)
 
void process_syncing_tables (XLogRecPtr current_lsn)
 
void invalidate_syncing_table_states (Datum arg, int cacheid, uint32 hashvalue)
 
static bool am_tablesync_worker (void)
 

Variables

MemoryContext ApplyCacheContext
 
struct WalReceiverConnwrconn
 
SubscriptionMySubscription
 
LogicalRepWorkerMyLogicalRepWorker
 
bool in_remote_transaction
 
volatile sig_atomic_t got_SIGHUP
 
volatile sig_atomic_t got_SIGTERM
 

Typedef Documentation

Function Documentation

static bool am_tablesync_worker ( void  )
inlinestatic

Definition at line 92 of file worker_internal.h.

References OidIsValid, and LogicalRepWorker::relid.

Referenced by apply_handle_commit(), apply_handle_origin(), ApplyWorkerMain(), process_syncing_tables(), and should_apply_changes_for_rel().

93 {
95 }
#define OidIsValid(objectId)
Definition: c.h:538
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:63
void invalidate_syncing_table_states ( Datum  arg,
int  cacheid,
uint32  hashvalue 
)

Definition at line 193 of file tablesync.c.

References table_states_valid.

Referenced by ApplyWorkerMain().

194 {
195  table_states_valid = false;
196 }
static bool table_states_valid
Definition: tablesync.c:109
int logicalrep_sync_worker_count ( Oid  subid)

Definition at line 652 of file launcher.c.

References Assert, i, LWLockHeldByMe(), max_logical_replication_workers, OidIsValid, LogicalRepWorker::relid, LogicalRepWorker::subid, and LogicalRepCtxStruct::workers.

Referenced by logicalrep_worker_launch(), and process_syncing_tables_for_apply().

653 {
654  int i;
655  int res = 0;
656 
657  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
658 
659  /* Search for attached worker for a given subscription id. */
660  for (i = 0; i < max_logical_replication_workers; i++)
661  {
663  if (w->subid == subid && OidIsValid(w->relid))
664  res++;
665  }
666 
667  return res;
668 }
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1831
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:71
#define OidIsValid(objectId)
Definition: c.h:538
#define Assert(condition)
Definition: c.h:675
int max_logical_replication_workers
Definition: launcher.c:60
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
int i
void logicalrep_worker_attach ( int  slot)

Definition at line 535 of file launcher.c.

References Assert, before_shmem_exit(), ereport, errcode(), errmsg(), ERROR, LogicalRepWorker::in_use, logicalrep_worker_onexit(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), max_logical_replication_workers, MyProc, LogicalRepWorker::proc, and LogicalRepCtxStruct::workers.

Referenced by ApplyWorkerMain().

536 {
537  /* Block concurrent access. */
538  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
539 
540  Assert(slot >= 0 && slot < max_logical_replication_workers);
542 
544  {
545  LWLockRelease(LogicalRepWorkerLock);
546  ereport(ERROR,
547  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
548  errmsg("logical replication worker slot %d is empty, cannot attach",
549  slot)));
550  }
551 
553  {
554  LWLockRelease(LogicalRepWorkerLock);
555  ereport(ERROR,
556  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
557  errmsg("logical replication worker slot %d is already used by "
558  "another worker, cannot attach", slot)));
559  }
560 
563 
564  LWLockRelease(LogicalRepWorkerLock);
565 }
PGPROC * MyProc
Definition: proc.c:67
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:71
int errcode(int sqlerrcode)
Definition: elog.c:575
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:63
#define ERROR
Definition: elog.h:43
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:320
#define ereport(elevel, rest)
Definition: elog.h:122
static void logicalrep_worker_onexit(int code, Datum arg)
Definition: launcher.c:614
uintptr_t Datum
Definition: postgres.h:372
#define Assert(condition)
Definition: c.h:675
int max_logical_replication_workers
Definition: launcher.c:60
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int errmsg(const char *fmt,...)
Definition: elog.c:797
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
LogicalRepWorker* logicalrep_worker_find ( Oid  subid,
Oid  relid,
bool  only_running 
)

Definition at line 225 of file launcher.c.

References Assert, i, LogicalRepWorker::in_use, LWLockHeldByMe(), max_logical_replication_workers, NULL, LogicalRepWorker::proc, LogicalRepWorker::relid, LogicalRepWorker::subid, and LogicalRepCtxStruct::workers.

Referenced by ApplyLauncherMain(), logicalrep_worker_stop(), logicalrep_worker_wakeup(), process_syncing_tables_for_apply(), and wait_for_sync_status_change().

226 {
227  int i;
228  LogicalRepWorker *res = NULL;
229 
230  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
231 
232  /* Search for attached worker for a given subscription id. */
233  for (i = 0; i < max_logical_replication_workers; i++)
234  {
236  if (w->in_use && w->subid == subid && w->relid == relid &&
237  (!only_running || w->proc))
238  {
239  res = w;
240  break;
241  }
242  }
243 
244  return res;
245 }
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1831
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:71
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
int max_logical_replication_workers
Definition: launcher.c:60
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
int i
void logicalrep_worker_launch ( Oid  dbid,
Oid  subid,
const char *  subname,
Oid  userid,
Oid  relid 
)

Definition at line 251 of file launcher.c.

References BackgroundWorker::bgw_flags, BackgroundWorker::bgw_function_name, BackgroundWorker::bgw_library_name, BackgroundWorker::bgw_main_arg, BGW_MAXLEN, BackgroundWorker::bgw_name, BGW_NEVER_RESTART, BackgroundWorker::bgw_notify_pid, BackgroundWorker::bgw_restart_time, BackgroundWorker::bgw_start_time, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_SHMEM_ACCESS, BgWorkerStart_RecoveryFinished, LogicalRepWorker::dbid, elog, ereport, errcode(), errhint(), errmsg(), ERROR, LogicalRepWorker::generation, GetCurrentTimestamp(), i, LogicalRepWorker::in_use, Int32GetDatum, InvalidXLogRecPtr, LogicalRepWorker::last_lsn, LogicalRepWorker::last_recv_time, LogicalRepWorker::last_send_time, LogicalRepWorker::launch_time, LOG, logicalrep_sync_worker_count(), logicalrep_worker_cleanup(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), max_logical_replication_workers, max_replication_slots, max_sync_workers_per_subscription, MyProcPid, now(), NULL, OidIsValid, LogicalRepWorker::proc, RegisterDynamicBackgroundWorker(), LogicalRepWorker::relid, LogicalRepWorker::relstate, LogicalRepWorker::relstate_lsn, LogicalRepWorker::reply_lsn, LogicalRepWorker::reply_time, snprintf(), LogicalRepWorker::subid, SUBREL_STATE_UNKNOWN, TIMESTAMP_NOBEGIN, TimestampDifferenceExceeds(), LogicalRepWorker::userid, WaitForReplicationWorkerAttach(), wal_receiver_timeout, WARNING, and LogicalRepCtxStruct::workers.

Referenced by ApplyLauncherMain(), and process_syncing_tables_for_apply().

253 {
254  BackgroundWorker bgw;
255  BackgroundWorkerHandle *bgw_handle;
256  int i;
257  int slot = 0;
258  LogicalRepWorker *worker = NULL;
259  int nsyncworkers;
261 
262  ereport(LOG,
263  (errmsg("starting logical replication worker for subscription \"%s\"",
264  subname)));
265 
266  /* Report this after the initial starting message for consistency. */
267  if (max_replication_slots == 0)
268  ereport(ERROR,
269  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
270  errmsg("cannot start logical replication workers when max_replication_slots = 0")));
271 
272  /*
273  * We need to do the modification of the shared memory under lock so that
274  * we have consistent view.
275  */
276  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
277 
278 retry:
279  /* Find unused worker slot. */
280  for (i = 0; i < max_logical_replication_workers; i++)
281  {
283 
284  if (!w->in_use)
285  {
286  worker = w;
287  slot = i;
288  break;
289  }
290  }
291 
292  nsyncworkers = logicalrep_sync_worker_count(subid);
293 
294  now = GetCurrentTimestamp();
295 
296  /*
297  * If we didn't find a free slot, try to do garbage collection. The
298  * reason we do this is because if some worker failed to start up and its
299  * parent has crashed while waiting, the in_use state was never cleared.
300  */
301  if (worker == NULL || nsyncworkers >= max_sync_workers_per_subscription)
302  {
303  bool did_cleanup = false;
304 
305  for (i = 0; i < max_logical_replication_workers; i++)
306  {
308 
309  /*
310  * If the worker was marked in use but didn't manage to attach in
311  * time, clean it up.
312  */
313  if (w->in_use && !w->proc &&
316  {
317  elog(WARNING,
318  "logical replication worker for subscription \"%d\" took too long to start; canceled",
319  worker->subid);
320 
322  did_cleanup = true;
323  }
324  }
325 
326  if (did_cleanup)
327  goto retry;
328  }
329 
330  /*
331  * If we reached the sync worker limit per subscription, just exit
332  * silently as we might get here because of an otherwise harmless race
333  * condition.
334  */
335  if (nsyncworkers >= max_sync_workers_per_subscription)
336  {
337  LWLockRelease(LogicalRepWorkerLock);
338  return;
339  }
340 
341  /*
342  * However if there are no more free worker slots, inform user about it
343  * before exiting.
344  */
345  if (worker == NULL)
346  {
347  LWLockRelease(LogicalRepWorkerLock);
349  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
350  errmsg("out of logical replication workers slots"),
351  errhint("You might need to increase max_logical_replication_workers.")));
352  return;
353  }
354 
355  /* Prepare the worker slot. */
356  worker->launch_time = now;
357  worker->in_use = true;
358  worker->generation++;
359  worker->proc = NULL;
360  worker->dbid = dbid;
361  worker->userid = userid;
362  worker->subid = subid;
363  worker->relid = relid;
364  worker->relstate = SUBREL_STATE_UNKNOWN;
366  worker->last_lsn = InvalidXLogRecPtr;
369  worker->reply_lsn = InvalidXLogRecPtr;
370  TIMESTAMP_NOBEGIN(worker->reply_time);
371 
372  LWLockRelease(LogicalRepWorkerLock);
373 
374  /* Register the new dynamic worker. */
375  memset(&bgw, 0, sizeof(bgw));
379  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
380  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyWorkerMain");
381  if (OidIsValid(relid))
383  "logical replication worker for subscription %u sync %u", subid, relid);
384  else
386  "logical replication worker for subscription %u", subid);
387 
390  bgw.bgw_main_arg = Int32GetDatum(slot);
391 
392  if (!RegisterDynamicBackgroundWorker(&bgw, &bgw_handle))
393  {
395  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
396  errmsg("out of background workers slots"),
397  errhint("You might need to increase max_worker_processes.")));
398  return;
399  }
400 
401  /* Now wait until it attaches. */
402  WaitForReplicationWorkerAttach(worker, bgw_handle);
403 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int MyProcPid
Definition: globals.c:38
int errhint(const char *fmt,...)
Definition: elog.c:987
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
int64 TimestampTz
Definition: timestamp.h:39
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:71
TimestampTz last_send_time
XLogRecPtr last_lsn
int bgw_restart_time
Definition: bgworker.h:93
int errcode(int sqlerrcode)
Definition: elog.c:575
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define LOG
Definition: elog.h:26
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:52
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1649
#define OidIsValid(objectId)
Definition: c.h:538
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:95
int wal_receiver_timeout
Definition: walreceiver.c:75
XLogRecPtr relstate_lsn
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
Datum bgw_main_arg
Definition: bgworker.h:96
#define ERROR
Definition: elog.h:43
int max_sync_workers_per_subscription
Definition: launcher.c:61
XLogRecPtr reply_lsn
static void logicalrep_worker_cleanup(LogicalRepWorker *worker)
Definition: launcher.c:585
#define SUBREL_STATE_UNKNOWN
#define BGW_NEVER_RESTART
Definition: bgworker.h:84
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
int logicalrep_sync_worker_count(Oid subid)
Definition: launcher.c:652
TimestampTz launch_time
int max_replication_slots
Definition: slot.c:99
TimestampTz last_recv_time
#define NULL
Definition: c.h:229
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
int max_logical_replication_workers
Definition: launcher.c:60
#define BGW_MAXLEN
Definition: bgworker.h:85
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:92
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:933
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
static void WaitForReplicationWorkerAttach(LogicalRepWorker *worker, BackgroundWorkerHandle *handle)
Definition: launcher.c:163
#define Int32GetDatum(X)
Definition: postgres.h:485
int errmsg(const char *fmt,...)
Definition: elog.c:797
pid_t bgw_notify_pid
Definition: bgworker.h:98
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
int i
#define elog
Definition: elog.h:219
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:94
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1534
TimestampTz reply_time
void logicalrep_worker_sighup ( SIGNAL_ARGS  )

Definition at line 635 of file launcher.c.

References got_SIGHUP, MyLatch, and SetLatch().

Referenced by ApplyLauncherMain(), and ApplyWorkerMain().

636 {
637  int save_errno = errno;
638 
639  got_SIGHUP = true;
640 
641  /* Waken anything waiting on the process latch */
642  SetLatch(MyLatch);
643 
644  errno = save_errno;
645 }
volatile sig_atomic_t got_SIGHUP
Definition: launcher.c:83
void SetLatch(volatile Latch *latch)
Definition: latch.c:367
struct Latch * MyLatch
Definition: globals.c:51
void logicalrep_worker_sigterm ( SIGNAL_ARGS  )

Definition at line 621 of file launcher.c.

References got_SIGTERM, MyLatch, and SetLatch().

Referenced by ApplyLauncherMain(), and ApplyWorkerMain().

622 {
623  int save_errno = errno;
624 
625  got_SIGTERM = true;
626 
627  /* Waken anything waiting on the process latch */
628  SetLatch(MyLatch);
629 
630  errno = save_errno;
631 }
void SetLatch(volatile Latch *latch)
Definition: latch.c:367
volatile sig_atomic_t got_SIGTERM
Definition: launcher.c:84
struct Latch * MyLatch
Definition: globals.c:51
void logicalrep_worker_stop ( Oid  subid,
Oid  relid 
)

Definition at line 410 of file launcher.c.

References CHECK_FOR_INTERRUPTS, LogicalRepWorker::generation, LogicalRepWorker::in_use, logicalrep_worker_find(), LW_SHARED, LWLockAcquire(), LWLockRelease(), MyProc, PGPROC::pid, LogicalRepWorker::proc, proc_exit(), PGPROC::procLatch, ResetLatch(), WAIT_EVENT_BGWORKER_SHUTDOWN, WAIT_EVENT_BGWORKER_STARTUP, WaitLatch(), WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_TIMEOUT.

Referenced by DropSubscription().

411 {
412  LogicalRepWorker *worker;
413  uint16 generation;
414 
415  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
416 
417  worker = logicalrep_worker_find(subid, relid, false);
418 
419  /* No worker, nothing to do. */
420  if (!worker)
421  {
422  LWLockRelease(LogicalRepWorkerLock);
423  return;
424  }
425 
426  /*
427  * Remember which generation was our worker so we can check if what we see
428  * is still the same one.
429  */
430  generation = worker->generation;
431 
432  /*
433  * If we found worker but it does not have proc set it is starting up,
434  * wait for it to finish and then kill it.
435  */
436  while (worker->in_use && !worker->proc)
437  {
438  int rc;
439 
440  LWLockRelease(LogicalRepWorkerLock);
441 
443 
444  /* Wait for signal. */
445  rc = WaitLatch(&MyProc->procLatch,
448 
449  /* emergency bailout if postmaster has died */
450  if (rc & WL_POSTMASTER_DEATH)
451  proc_exit(1);
452 
454 
455  /* Check worker status. */
456  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
457 
458  /*
459  * Check whether the worker slot is no longer used, which would mean
460  * that the worker has exited, or whether the worker generation is
461  * different, meaning that a different worker has taken the slot.
462  */
463  if (!worker->in_use || worker->generation != generation)
464  {
465  LWLockRelease(LogicalRepWorkerLock);
466  return;
467  }
468 
469  /* Worker has assigned proc, so it has started. */
470  if (worker->proc)
471  break;
472  }
473 
474  /* Now terminate the worker ... */
475  kill(worker->proc->pid, SIGTERM);
476  LWLockRelease(LogicalRepWorkerLock);
477 
478  /* ... and wait for it to die. */
479  for (;;)
480  {
481  int rc;
482 
483  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
484  if (!worker->proc || worker->generation != generation)
485  {
486  LWLockRelease(LogicalRepWorkerLock);
487  break;
488  }
489  LWLockRelease(LogicalRepWorkerLock);
490 
492 
493  /* Wait for more work. */
494  rc = WaitLatch(&MyProc->procLatch,
495  WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
497 
498  /* emergency bailout if postmaster has died */
499  if (rc & WL_POSTMASTER_DEATH)
500  proc_exit(1);
501 
503  }
504 }
#define WL_TIMEOUT
Definition: latch.h:127
PGPROC * MyProc
Definition: proc.c:67
void proc_exit(int code)
Definition: ipc.c:99
void ResetLatch(volatile Latch *latch)
Definition: latch.c:450
Latch procLatch
Definition: proc.h:103
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:288
unsigned short uint16
Definition: c.h:267
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:225
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
int pid
Definition: proc.h:108
#define WL_LATCH_SET
Definition: latch.h:124
void logicalrep_worker_wakeup ( Oid  subid,
Oid  relid 
)

Definition at line 510 of file launcher.c.

References logicalrep_worker_find(), logicalrep_worker_wakeup_ptr(), LW_SHARED, LWLockAcquire(), and LWLockRelease().

Referenced by pg_attribute_noreturn().

511 {
512  LogicalRepWorker *worker;
513 
514  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
515  worker = logicalrep_worker_find(subid, relid, true);
516  LWLockRelease(LogicalRepWorkerLock);
517 
518  if (worker)
520 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition: launcher.c:526
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:225
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
void logicalrep_worker_wakeup_ptr ( LogicalRepWorker worker)

Definition at line 526 of file launcher.c.

References LogicalRepWorker::proc, PGPROC::procLatch, and SetLatch().

Referenced by logicalrep_worker_wakeup(), and process_syncing_tables_for_apply().

527 {
528  SetLatch(&worker->proc->procLatch);
529 }
Latch procLatch
Definition: proc.h:103
void SetLatch(volatile Latch *latch)
Definition: latch.c:367
char* LogicalRepSyncTableStart ( XLogRecPtr origin_startpos)

Definition at line 705 of file tablesync.c.

References CommandCounterIncrement(), CommitTransactionCommand(), Subscription::conninfo, copy_table(), CRS_USE_SNAPSHOT, elog, ereport, WalRcvExecResult::err, errdetail(), errmsg(), ERROR, GetSubscriptionRelState(), heap_close, heap_open(), InvalidXLogRecPtr, MyLogicalRepWorker, MySubscription, NAMEDATALEN, NoLock, NULL, Subscription::oid, psprintf(), LogicalRepWorker::relid, LogicalRepWorker::relmutex, LogicalRepWorker::relstate, LogicalRepWorker::relstate_lsn, RowExclusiveLock, SetSubscriptionRelState(), Subscription::slotname, SpinLockAcquire, SpinLockRelease, StartTransactionCommand(), StaticAssertStmt, WalRcvExecResult::status, LogicalRepWorker::subid, SUBREL_STATE_CATCHUP, SUBREL_STATE_DATASYNC, SUBREL_STATE_INIT, SUBREL_STATE_READY, SUBREL_STATE_SYNCDONE, SUBREL_STATE_SYNCWAIT, wait_for_sync_status_change(), walrcv_clear_result(), walrcv_connect, walrcv_create_slot, walrcv_exec, WALRCV_OK_COMMAND, and wrconn.

Referenced by ApplyWorkerMain().

706 {
707  char *slotname;
708  char *err;
709  char relstate;
710  XLogRecPtr relstate_lsn;
711 
712  /* Check the state of the table synchronization. */
716  &relstate_lsn, false);
718 
720  MyLogicalRepWorker->relstate = relstate;
721  MyLogicalRepWorker->relstate_lsn = relstate_lsn;
723 
724  /*
725  * To build a slot name for the sync work, we are limited to NAMEDATALEN -
726  * 1 characters. We cut the original slot name to NAMEDATALEN - 28 chars
727  * and append _%u_sync_%u (1 + 10 + 6 + 10 + '\0'). (It's actually the
728  * NAMEDATALEN on the remote that matters, but this scheme will also work
729  * reasonably if that is different.)
730  */
731  StaticAssertStmt(NAMEDATALEN >= 32, "NAMEDATALEN too small"); /* for sanity */
732  slotname = psprintf("%.*s_%u_sync_%u",
733  NAMEDATALEN - 28,
737 
738  wrconn = walrcv_connect(MySubscription->conninfo, true, slotname, &err);
739  if (wrconn == NULL)
740  ereport(ERROR,
741  (errmsg("could not connect to the publisher: %s", err)));
742 
743  switch (MyLogicalRepWorker->relstate)
744  {
745  case SUBREL_STATE_INIT:
747  {
748  Relation rel;
749  WalRcvExecResult *res;
750 
755 
756  /* Update the state and make it visible to others. */
763 
764  /*
765  * We want to do the table data sync in single
766  * transaction.
767  */
769 
770  /*
771  * Use standard write lock here. It might be better to
772  * disallow access to table while it's being synchronized.
773  * But we don't want to block the main apply process from
774  * working and it has to open relation in RowExclusiveLock
775  * when remapping remote relation id to local one.
776  */
778 
779  /*
780  * Create temporary slot for the sync process.
781  * We do this inside transaction so that we can use the
782  * snapshot made by the slot to get existing data.
783  */
784  res = walrcv_exec(wrconn,
785  "BEGIN READ ONLY ISOLATION LEVEL "
786  "REPEATABLE READ", 0, NULL);
787  if (res->status != WALRCV_OK_COMMAND)
788  ereport(ERROR,
789  (errmsg("table copy could not start transaction on publisher"),
790  errdetail("The error was: %s", res->err)));
791  walrcv_clear_result(res);
792 
793  /*
794  * Create new temporary logical decoding slot.
795  *
796  * We'll use slot for data copy so make sure the snapshot
797  * is used for the transaction, that way the COPY will get
798  * data that is consistent with the lsn used by the slot
799  * to start decoding.
800  */
801  walrcv_create_slot(wrconn, slotname, true,
802  CRS_USE_SNAPSHOT, origin_startpos);
803 
804  copy_table(rel);
805 
806  res = walrcv_exec(wrconn, "COMMIT", 0, NULL);
807  if (res->status != WALRCV_OK_COMMAND)
808  ereport(ERROR,
809  (errmsg("table copy could not finish transaction on publisher"),
810  errdetail("The error was: %s", res->err)));
811  walrcv_clear_result(res);
812 
813  heap_close(rel, NoLock);
814 
815  /* Make the copy visible. */
817 
818  /*
819  * We are done with the initial data synchronization,
820  * update the state.
821  */
824  MyLogicalRepWorker->relstate_lsn = *origin_startpos;
826 
827  /*
828  * Wait for main apply worker to either tell us to
829  * catchup or that we are done.
830  */
834  {
835  /* Update the new state. */
840  finish_sync_worker();
841  }
842  break;
843  }
845  case SUBREL_STATE_READY:
846  /* Nothing to do here but finish. */
847  finish_sync_worker();
848  break;
849  default:
850  elog(ERROR, "unknown relation state \"%c\"",
852  }
853 
854  return slotname;
855 }
Subscription * MySubscription
Definition: worker.c:109
#define SUBREL_STATE_INIT
WalReceiverConn * wrconn
Definition: worker.c:107
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define SUBREL_STATE_DATASYNC
void CommitTransactionCommand(void)
Definition: xact.c:2747
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void copy_table(Relation rel)
Definition: tablesync.c:652
#define heap_close(r, l)
Definition: heapam.h:97
#define walrcv_create_slot(conn, slotname, temporary, snapshot_action, lsn)
Definition: walreceiver.h:260
#define SUBREL_STATE_SYNCWAIT
XLogRecPtr relstate_lsn
#define NAMEDATALEN
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:757
#define SpinLockAcquire(lock)
Definition: spin.h:62
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:268
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:63
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
int errdetail(const char *fmt,...)
Definition: elog.c:873
static bool wait_for_sync_status_change(Oid relid, char origstate)
Definition: tablesync.c:149
#define ereport(elevel, rest)
Definition: elog.h:122
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SUBREL_STATE_SYNCDONE
void CommandCounterIncrement(void)
Definition: xact.c:922
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
WalRcvExecStatus status
Definition: walreceiver.h:187
void StartTransactionCommand(void)
Definition: xact.c:2677
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn, bool missing_ok)
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
Oid SetSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn)
#define SUBREL_STATE_READY
#define SUBREL_STATE_CATCHUP
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:262
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:242
void process_syncing_tables ( XLogRecPtr  current_lsn)

Definition at line 420 of file tablesync.c.

References am_tablesync_worker(), process_syncing_tables_for_apply(), and process_syncing_tables_for_sync().

Referenced by apply_handle_commit(), and LogicalRepApplyLoop().

421 {
422  if (am_tablesync_worker())
423  process_syncing_tables_for_sync(current_lsn);
424  else
426 }
static void process_syncing_tables_for_apply(XLogRecPtr current_lsn)
Definition: tablesync.c:264
static bool am_tablesync_worker(void)
static void process_syncing_tables_for_sync(XLogRecPtr current_lsn)
Definition: tablesync.c:208

Variable Documentation

MemoryContext ApplyCacheContext

Definition at line 105 of file worker.c.

volatile sig_atomic_t got_SIGHUP

Definition at line 83 of file launcher.c.

Referenced by ApplyLauncherMain(), and logicalrep_worker_sighup().

volatile sig_atomic_t got_SIGTERM

Definition at line 84 of file launcher.c.

Referenced by ApplyLauncherMain(), and logicalrep_worker_sigterm().

bool in_remote_transaction
Subscription* MySubscription