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 1581 of file worker.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, am_tablesync_worker(), Assert, BackgroundWorkerInitializeConnectionByOid(), BackgroundWorkerUnblockSignals(), CacheRegisterSyscacheCallback(), CommitTransactionCommand(), Subscription::conninfo, CurrentResourceOwner, 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, ResourceOwnerCreate(), server_version, 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.

1582 {
1583  int worker_slot = DatumGetInt32(main_arg);
1584  MemoryContext oldctx;
1585  char originname[NAMEDATALEN];
1586  XLogRecPtr origin_startpos;
1587  char *myslotname;
1589 
1590  /* Attach to slot */
1591  logicalrep_worker_attach(worker_slot);
1592 
1593  /* Setup signal handling */
1595  pqsignal(SIGTERM, die);
1597 
1598  /* Initialise stats to a sanish value */
1601 
1602  /* Load the libpq-specific functions */
1603  load_file("libpqwalreceiver", false);
1604 
1605  Assert(CurrentResourceOwner == NULL);
1607  "logical replication apply");
1608 
1609  /* Run as replica session replication role. */
1610  SetConfigOption("session_replication_role", "replica",
1612 
1613  /* Connect to our database. */
1616  0);
1617 
1618  /* Load the subscription into persistent memory context. */
1620  "ApplyContext",
1624 
1626  if (!MySubscription)
1627  {
1628  ereport(LOG,
1629  (errmsg("logical replication apply worker for subscription %u will not "
1630  "start because the subscription was removed during startup",
1632  proc_exit(0);
1633  }
1634 
1635  MySubscriptionValid = true;
1636  MemoryContextSwitchTo(oldctx);
1637 
1638  if (!MySubscription->enabled)
1639  {
1640  ereport(LOG,
1641  (errmsg("logical replication apply worker for subscription \"%s\" will not "
1642  "start because the subscription was disabled during startup",
1643  MySubscription->name)));
1644 
1645  proc_exit(0);
1646  }
1647 
1648  /* Setup synchronous commit according to the user's wishes */
1649  SetConfigOption("synchronous_commit", MySubscription->synccommit,
1651 
1652  /* Keep us informed about subscription changes. */
1655  (Datum) 0);
1656 
1657  if (am_tablesync_worker())
1658  ereport(LOG,
1659  (errmsg("logical replication table synchronization worker for subscription \"%s\", table \"%s\" has started",
1661  else
1662  ereport(LOG,
1663  (errmsg("logical replication apply worker for subscription \"%s\" has started",
1664  MySubscription->name)));
1665 
1667 
1668  /* Connect to the origin and start the replication. */
1669  elog(DEBUG1, "connecting to publisher using connection string \"%s\"",
1671 
1672  if (am_tablesync_worker())
1673  {
1674  char *syncslotname;
1675 
1676  /* This is table synchroniation worker, call initial sync. */
1677  syncslotname = LogicalRepSyncTableStart(&origin_startpos);
1678 
1679  /* The slot name needs to be allocated in permanent memory context. */
1681  myslotname = pstrdup(syncslotname);
1682  MemoryContextSwitchTo(oldctx);
1683 
1684  pfree(syncslotname);
1685  }
1686  else
1687  {
1688  /* This is main apply worker */
1689  RepOriginId originid;
1690  TimeLineID startpointTLI;
1691  char *err;
1692  int server_version;
1693 
1694  myslotname = MySubscription->slotname;
1695 
1696  /*
1697  * This shouldn't happen if the subscription is enabled, but guard
1698  * against DDL bugs or manual catalog changes. (libpqwalreceiver will
1699  * crash if slot is NULL.)
1700  */
1701  if (!myslotname)
1702  ereport(ERROR,
1703  (errmsg("subscription has no replication slot set")));
1704 
1705  /* Setup replication origin tracking. */
1707  snprintf(originname, sizeof(originname), "pg_%u", MySubscription->oid);
1708  originid = replorigin_by_name(originname, true);
1709  if (!OidIsValid(originid))
1710  originid = replorigin_create(originname);
1711  replorigin_session_setup(originid);
1712  replorigin_session_origin = originid;
1713  origin_startpos = replorigin_session_get_progress(false);
1715 
1717  &err);
1718  if (wrconn == NULL)
1719  ereport(ERROR,
1720  (errmsg("could not connect to the publisher: %s", err)));
1721 
1722  /*
1723  * We don't really use the output identify_system for anything but it
1724  * does some initializations on the upstream so let's still call it.
1725  */
1726  (void) walrcv_identify_system(wrconn, &startpointTLI,
1727  &server_version);
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:116
#define LOGICALREP_PROTO_VERSION_NUM
Definition: logicalproto.h:28
WalReceiverConn * wrconn
Definition: worker.c:114
#define DEBUG1
Definition: elog.h:25
uint32 TimeLineID
Definition: xlogdefs.h:45
#define DatumGetInt32(X)
Definition: postgres.h:457
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
ResourceOwner CurrentResourceOwner
Definition: resowner.c:140
char * pstrdup(const char *in)
Definition: mcxt.c:1161
void CommitTransactionCommand(void)
Definition: xact.c:2744
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
TimestampTz last_send_time
uint16 RepOriginId
Definition: xlogdefs.h:51
#define walrcv_startstreaming(conn, options)
Definition: walreceiver.h:267
void proc_exit(int code)
Definition: ipc.c:104
XLogRecPtr replorigin_session_get_progress(bool flush)
Definition: origin.c:1165
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void replorigin_session_setup(RepOriginId node)
Definition: origin.c:1030
#define LOG
Definition: elog.h:26
char * LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
Definition: tablesync.c:815
#define OidIsValid(objectId)
Definition: c.h:605
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:211
union WalRcvStreamOptions::@107 proto
#define NAMEDATALEN
Subscription * GetSubscription(Oid subid, bool missing_ok)
static void subscription_change_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: worker.c:1560
void pfree(void *pointer)
Definition: mcxt.c:1031
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:63
#define ERROR
Definition: elog.h:43
Definition: guc.h:75
static bool am_tablesync_worker(void)
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define walrcv_identify_system(conn, primary_tli, server_version)
Definition: walreceiver.h:263
void logicalrep_worker_attach(int slot)
Definition: launcher.c:612
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:6907
#define SIGHUP
Definition: win32_port.h:163
XLogRecPtr startpoint
Definition: walreceiver.h:154
List * publications
RepOriginId replorigin_create(char *roname)
Definition: origin.c:242
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:44
MemoryContext ApplyContext
Definition: worker.c:112
static char ** options
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
static void logicalrep_worker_sighup(SIGNAL_ARGS)
Definition: worker.c:1567
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1389
uintptr_t Datum
Definition: postgres.h:367
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
Definition: postmaster.c:5568
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
TimestampTz last_recv_time
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:699
RepOriginId replorigin_session_origin
Definition: origin.c:155
void StartTransactionCommand(void)
Definition: xact.c:2673
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:137
static int server_version
Definition: pg_dumpall.c:79
bool MySubscriptionValid
Definition: worker.c:117
int errmsg(const char *fmt,...)
Definition: elog.c:797
void die(SIGNAL_ARGS)
Definition: postgres.c:2658
static void LogicalRepApplyLoop(XLogRecPtr last_received)
Definition: worker.c:1102
#define elog
Definition: elog.h:219
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1730
TimestampTz reply_time
void invalidate_syncing_table_states(Datum arg, int cacheid, uint32 hashvalue)
Definition: tablesync.c:271
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:418
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5597
#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:63