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 1588 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(), 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, SignalHandlerForConfigReload(), 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.

1589 {
1590  int worker_slot = DatumGetInt32(main_arg);
1591  MemoryContext oldctx;
1592  char originname[NAMEDATALEN];
1593  XLogRecPtr origin_startpos;
1594  char *myslotname;
1596 
1597  /* Attach to slot */
1598  logicalrep_worker_attach(worker_slot);
1599 
1600  /* Setup signal handling */
1602  pqsignal(SIGTERM, die);
1604 
1605  /*
1606  * We don't currently need any ResourceOwner in a walreceiver process, but
1607  * if we did, we could call CreateAuxProcessResourceOwner here.
1608  */
1609 
1610  /* Initialise stats to a sanish value */
1613 
1614  /* Load the libpq-specific functions */
1615  load_file("libpqwalreceiver", false);
1616 
1617  /* Run as replica session replication role. */
1618  SetConfigOption("session_replication_role", "replica",
1620 
1621  /* Connect to our database. */
1624  0);
1625 
1626  /* Load the subscription into persistent memory context. */
1628  "ApplyContext",
1632 
1634  if (!MySubscription)
1635  {
1636  ereport(LOG,
1637  (errmsg("logical replication apply worker for subscription %u will not "
1638  "start because the subscription was removed during startup",
1640  proc_exit(0);
1641  }
1642 
1643  MySubscriptionValid = true;
1644  MemoryContextSwitchTo(oldctx);
1645 
1646  if (!MySubscription->enabled)
1647  {
1648  ereport(LOG,
1649  (errmsg("logical replication apply worker for subscription \"%s\" will not "
1650  "start because the subscription was disabled during startup",
1651  MySubscription->name)));
1652 
1653  proc_exit(0);
1654  }
1655 
1656  /* Setup synchronous commit according to the user's wishes */
1657  SetConfigOption("synchronous_commit", MySubscription->synccommit,
1659 
1660  /* Keep us informed about subscription changes. */
1663  (Datum) 0);
1664 
1665  if (am_tablesync_worker())
1666  ereport(LOG,
1667  (errmsg("logical replication table synchronization worker for subscription \"%s\", table \"%s\" has started",
1669  else
1670  ereport(LOG,
1671  (errmsg("logical replication apply worker for subscription \"%s\" has started",
1672  MySubscription->name)));
1673 
1675 
1676  /* Connect to the origin and start the replication. */
1677  elog(DEBUG1, "connecting to publisher using connection string \"%s\"",
1679 
1680  if (am_tablesync_worker())
1681  {
1682  char *syncslotname;
1683 
1684  /* This is table synchronization worker, call initial sync. */
1685  syncslotname = LogicalRepSyncTableStart(&origin_startpos);
1686 
1687  /* The slot name needs to be allocated in permanent memory context. */
1689  myslotname = pstrdup(syncslotname);
1690  MemoryContextSwitchTo(oldctx);
1691 
1692  pfree(syncslotname);
1693  }
1694  else
1695  {
1696  /* This is main apply worker */
1697  RepOriginId originid;
1698  TimeLineID startpointTLI;
1699  char *err;
1700 
1701  myslotname = MySubscription->slotname;
1702 
1703  /*
1704  * This shouldn't happen if the subscription is enabled, but guard
1705  * against DDL bugs or manual catalog changes. (libpqwalreceiver will
1706  * crash if slot is NULL.)
1707  */
1708  if (!myslotname)
1709  ereport(ERROR,
1710  (errmsg("subscription has no replication slot set")));
1711 
1712  /* Setup replication origin tracking. */
1714  snprintf(originname, sizeof(originname), "pg_%u", MySubscription->oid);
1715  originid = replorigin_by_name(originname, true);
1716  if (!OidIsValid(originid))
1717  originid = replorigin_create(originname);
1718  replorigin_session_setup(originid);
1719  replorigin_session_origin = originid;
1720  origin_startpos = replorigin_session_get_progress(false);
1722 
1724  &err);
1725  if (wrconn == NULL)
1726  ereport(ERROR,
1727  (errmsg("could not connect to the publisher: %s", err)));
1728 
1729  /*
1730  * We don't really use the output identify_system for anything but it
1731  * does some initializations on the upstream so let's still call it.
1732  */
1733  (void) walrcv_identify_system(wrconn, &startpointTLI);
1734 
1735  }
1736 
1737  /*
1738  * Setup callback for syscache so that we know when something changes in
1739  * the subscription relation state.
1740  */
1743  (Datum) 0);
1744 
1745  /* Build logical replication streaming options. */
1746  options.logical = true;
1747  options.startpoint = origin_startpos;
1748  options.slotname = myslotname;
1749  options.proto.logical.proto_version = LOGICALREP_PROTO_VERSION_NUM;
1750  options.proto.logical.publication_names = MySubscription->publications;
1751 
1752  /* Start normal logical streaming replication. */
1753  walrcv_startstreaming(wrconn, &options);
1754 
1755  /* Run the main loop. */
1756  LogicalRepApplyLoop(origin_startpos);
1757 
1758  proc_exit(0);
1759 }
Subscription * MySubscription
Definition: worker.c:104
#define LOGICALREP_PROTO_VERSION_NUM
Definition: logicalproto.h:28
WalReceiverConn * wrconn
Definition: worker.c:102
#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:272
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:56
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:278
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:644
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:1581
void pfree(void *pointer)
Definition: mcxt.c:1056
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57
#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:625
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:7598
#define SIGHUP
Definition: win32_port.h:153
XLogRecPtr startpoint
Definition: walreceiver.h:160
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:100
static char ** options
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:5714
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:105
int errmsg(const char *fmt,...)
Definition: elog.c:822
static void LogicalRepApplyLoop(XLogRecPtr last_received)
Definition: worker.c:1126
#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:5743
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:264

◆ IsLogicalWorker()

bool IsLogicalWorker ( void  )

Definition at line 1765 of file worker.c.

References MyLogicalRepWorker.

Referenced by ProcessInterrupts().

1766 {
1767  return MyLogicalRepWorker != NULL;
1768 }
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57