PostgreSQL Source Code  git master
logicalworker.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void ApplyWorkerMain (Datum main_arg)
 
bool IsLogicalWorker (void)
 

Function Documentation

◆ ApplyWorkerMain()

void ApplyWorkerMain ( Datum  main_arg)

Definition at line 1582 of file worker.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, am_tablesync_worker(), BackgroundWorkerInitializeConnectionByOid(), BackgroundWorkerUnblockSignals(), CacheRegisterSyscacheCallback(), CommitTransactionCommand(), Subscription::conninfo, DatumGetInt32, LogicalRepWorker::dbid, DEBUG1, die, elog, Subscription::enabled, ereport, errmsg(), ERROR, get_rel_name(), GetCurrentTimestamp(), GetSubscription(), invalidate_syncing_table_states(), LogicalRepWorker::last_recv_time, LogicalRepWorker::last_send_time, load_file(), LOG, WalRcvStreamOptions::logical, LOGICALREP_PROTO_VERSION_NUM, logicalrep_worker_attach(), logicalrep_worker_sighup(), LogicalRepApplyLoop(), LogicalRepSyncTableStart(), MemoryContextSwitchTo(), MyLogicalRepWorker, MySubscriptionValid, Subscription::name, NAMEDATALEN, Subscription::oid, OidIsValid, options, pfree(), PGC_BACKEND, PGC_S_OVERRIDE, PGC_SUSET, pqsignal(), proc_exit(), WalRcvStreamOptions::proto, pstrdup(), Subscription::publications, LogicalRepWorker::relid, replorigin_by_name(), replorigin_create(), replorigin_session_get_progress(), replorigin_session_origin, replorigin_session_setup(), LogicalRepWorker::reply_time, SetConfigOption(), SIGHUP, Subscription::slotname, WalRcvStreamOptions::slotname, snprintf, WalRcvStreamOptions::startpoint, StartTransactionCommand(), LogicalRepWorker::subid, subscription_change_cb(), SUBSCRIPTIONOID, SUBSCRIPTIONRELMAP, Subscription::synccommit, TopMemoryContext, LogicalRepWorker::userid, walrcv_connect, walrcv_identify_system, and walrcv_startstreaming.

1583 {
1584  int worker_slot = DatumGetInt32(main_arg);
1585  MemoryContext oldctx;
1586  char originname[NAMEDATALEN];
1587  XLogRecPtr origin_startpos;
1588  char *myslotname;
1590 
1591  /* Attach to slot */
1592  logicalrep_worker_attach(worker_slot);
1593 
1594  /* Setup signal handling */
1596  pqsignal(SIGTERM, die);
1598 
1599  /*
1600  * We don't currently need any ResourceOwner in a walreceiver process, but
1601  * if we did, we could call CreateAuxProcessResourceOwner here.
1602  */
1603 
1604  /* Initialise stats to a sanish value */
1607 
1608  /* Load the libpq-specific functions */
1609  load_file("libpqwalreceiver", false);
1610 
1611  /* Run as replica session replication role. */
1612  SetConfigOption("session_replication_role", "replica",
1614 
1615  /* Connect to our database. */
1618  0);
1619 
1620  /* Load the subscription into persistent memory context. */
1622  "ApplyContext",
1626 
1628  if (!MySubscription)
1629  {
1630  ereport(LOG,
1631  (errmsg("logical replication apply worker for subscription %u will not "
1632  "start because the subscription was removed during startup",
1634  proc_exit(0);
1635  }
1636 
1637  MySubscriptionValid = true;
1638  MemoryContextSwitchTo(oldctx);
1639 
1640  if (!MySubscription->enabled)
1641  {
1642  ereport(LOG,
1643  (errmsg("logical replication apply worker for subscription \"%s\" will not "
1644  "start because the subscription was disabled during startup",
1645  MySubscription->name)));
1646 
1647  proc_exit(0);
1648  }
1649 
1650  /* Setup synchronous commit according to the user's wishes */
1651  SetConfigOption("synchronous_commit", MySubscription->synccommit,
1653 
1654  /* Keep us informed about subscription changes. */
1657  (Datum) 0);
1658 
1659  if (am_tablesync_worker())
1660  ereport(LOG,
1661  (errmsg("logical replication table synchronization worker for subscription \"%s\", table \"%s\" has started",
1663  else
1664  ereport(LOG,
1665  (errmsg("logical replication apply worker for subscription \"%s\" has started",
1666  MySubscription->name)));
1667 
1669 
1670  /* Connect to the origin and start the replication. */
1671  elog(DEBUG1, "connecting to publisher using connection string \"%s\"",
1673 
1674  if (am_tablesync_worker())
1675  {
1676  char *syncslotname;
1677 
1678  /* This is table synchronization worker, call initial sync. */
1679  syncslotname = LogicalRepSyncTableStart(&origin_startpos);
1680 
1681  /* The slot name needs to be allocated in permanent memory context. */
1683  myslotname = pstrdup(syncslotname);
1684  MemoryContextSwitchTo(oldctx);
1685 
1686  pfree(syncslotname);
1687  }
1688  else
1689  {
1690  /* This is main apply worker */
1691  RepOriginId originid;
1692  TimeLineID startpointTLI;
1693  char *err;
1694 
1695  myslotname = MySubscription->slotname;
1696 
1697  /*
1698  * This shouldn't happen if the subscription is enabled, but guard
1699  * against DDL bugs or manual catalog changes. (libpqwalreceiver will
1700  * crash if slot is NULL.)
1701  */
1702  if (!myslotname)
1703  ereport(ERROR,
1704  (errmsg("subscription has no replication slot set")));
1705 
1706  /* Setup replication origin tracking. */
1708  snprintf(originname, sizeof(originname), "pg_%u", MySubscription->oid);
1709  originid = replorigin_by_name(originname, true);
1710  if (!OidIsValid(originid))
1711  originid = replorigin_create(originname);
1712  replorigin_session_setup(originid);
1713  replorigin_session_origin = originid;
1714  origin_startpos = replorigin_session_get_progress(false);
1716 
1718  &err);
1719  if (wrconn == NULL)
1720  ereport(ERROR,
1721  (errmsg("could not connect to the publisher: %s", err)));
1722 
1723  /*
1724  * We don't really use the output identify_system for anything but it
1725  * does some initializations on the upstream so let's still call it.
1726  */
1727  (void) walrcv_identify_system(wrconn, &startpointTLI);
1728 
1729  }
1730 
1731  /*
1732  * Setup callback for syscache so that we know when something changes in
1733  * the subscription relation state.
1734  */
1737  (Datum) 0);
1738 
1739  /* Build logical replication streaming options. */
1740  options.logical = true;
1741  options.startpoint = origin_startpos;
1742  options.slotname = myslotname;
1743  options.proto.logical.proto_version = LOGICALREP_PROTO_VERSION_NUM;
1744  options.proto.logical.publication_names = MySubscription->publications;
1745 
1746  /* Start normal logical streaming replication. */
1747  walrcv_startstreaming(wrconn, &options);
1748 
1749  /* Run the main loop. */
1750  LogicalRepApplyLoop(origin_startpos);
1751 
1752  proc_exit(0);
1753 }
Subscription * MySubscription
Definition: worker.c:102
#define LOGICALREP_PROTO_VERSION_NUM
Definition: logicalproto.h:28
WalReceiverConn * wrconn
Definition: worker.c:100
#define AllocSetContextCreate
Definition: memutils.h:170
#define DEBUG1
Definition: elog.h:25
uint32 TimeLineID
Definition: xlogdefs.h:52
#define DatumGetInt32(X)
Definition: postgres.h:472
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1583
#define walrcv_identify_system(conn, primary_tli)
Definition: walreceiver.h:263
char * pstrdup(const char *in)
Definition: mcxt.c:1186
void CommitTransactionCommand(void)
Definition: xact.c:2898
union WalRcvStreamOptions::@106 proto
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
TimestampTz last_send_time
uint16 RepOriginId
Definition: xlogdefs.h:58
#define walrcv_startstreaming(conn, options)
Definition: walreceiver.h:269
void proc_exit(int code)
Definition: ipc.c:104
XLogRecPtr replorigin_session_get_progress(bool flush)
Definition: origin.c:1188
void replorigin_session_setup(RepOriginId node)
Definition: origin.c:1053
#define LOG
Definition: elog.h:26
char * LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
Definition: tablesync.c:798
#define OidIsValid(objectId)
Definition: c.h:645
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:208
#define NAMEDATALEN
Subscription * GetSubscription(Oid subid, bool missing_ok)
static void subscription_change_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: worker.c:1561
void pfree(void *pointer)
Definition: mcxt.c:1056
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:56
#define ERROR
Definition: elog.h:43
Definition: guc.h:75
static bool am_tablesync_worker(void)
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void logicalrep_worker_attach(int slot)
Definition: launcher.c:627
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:7540
#define SIGHUP
Definition: win32_port.h:154
XLogRecPtr startpoint
Definition: walreceiver.h:153
List * publications
RepOriginId replorigin_create(char *roname)
Definition: origin.c:239
#define ereport(elevel, rest)
Definition: elog.h:141
MemoryContext TopMemoryContext
Definition: mcxt.c:44
MemoryContext ApplyContext
Definition: worker.c:98
static char ** options
static void logicalrep_worker_sighup(SIGNAL_ARGS)
Definition: worker.c:1568
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1426
uintptr_t Datum
Definition: postgres.h:367
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
Definition: postmaster.c:5708
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
TimestampTz last_recv_time
uint64 XLogRecPtr
Definition: xlogdefs.h:21
RepOriginId replorigin_session_origin
Definition: origin.c:152
void StartTransactionCommand(void)
Definition: xact.c:2797
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:146
bool MySubscriptionValid
Definition: worker.c:103
int errmsg(const char *fmt,...)
Definition: elog.c:822
static void LogicalRepApplyLoop(XLogRecPtr last_received)
Definition: worker.c:1106
#define elog(elevel,...)
Definition: elog.h:228
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1730
#define snprintf
Definition: port.h:192
#define die(msg)
Definition: pg_test_fsync.c:96
TimestampTz reply_time
void invalidate_syncing_table_states(Datum arg, int cacheid, uint32 hashvalue)
Definition: tablesync.c:256
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5737
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:255

◆ IsLogicalWorker()

bool IsLogicalWorker ( void  )

Definition at line 1759 of file worker.c.

References MyLogicalRepWorker.

Referenced by ProcessInterrupts().

1760 {
1761  return MyLogicalRepWorker != NULL;
1762 }
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:56