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 1565 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.

1566 {
1567  int worker_slot = DatumGetInt32(main_arg);
1568  MemoryContext oldctx;
1569  char originname[NAMEDATALEN];
1570  XLogRecPtr origin_startpos;
1571  char *myslotname;
1573 
1574  /* Attach to slot */
1575  logicalrep_worker_attach(worker_slot);
1576 
1577  /* Setup signal handling */
1579  pqsignal(SIGTERM, die);
1581 
1582  /*
1583  * We don't currently need any ResourceOwner in a walreceiver process, but
1584  * if we did, we could call CreateAuxProcessResourceOwner here.
1585  */
1586 
1587  /* Initialise stats to a sanish value */
1590 
1591  /* Load the libpq-specific functions */
1592  load_file("libpqwalreceiver", false);
1593 
1594  /* Run as replica session replication role. */
1595  SetConfigOption("session_replication_role", "replica",
1597 
1598  /* Connect to our database. */
1601  0);
1602 
1603  /* Load the subscription into persistent memory context. */
1605  "ApplyContext",
1609 
1611  if (!MySubscription)
1612  {
1613  ereport(LOG,
1614  (errmsg("logical replication apply worker for subscription %u will not "
1615  "start because the subscription was removed during startup",
1617  proc_exit(0);
1618  }
1619 
1620  MySubscriptionValid = true;
1621  MemoryContextSwitchTo(oldctx);
1622 
1623  if (!MySubscription->enabled)
1624  {
1625  ereport(LOG,
1626  (errmsg("logical replication apply worker for subscription \"%s\" will not "
1627  "start because the subscription was disabled during startup",
1628  MySubscription->name)));
1629 
1630  proc_exit(0);
1631  }
1632 
1633  /* Setup synchronous commit according to the user's wishes */
1634  SetConfigOption("synchronous_commit", MySubscription->synccommit,
1636 
1637  /* Keep us informed about subscription changes. */
1640  (Datum) 0);
1641 
1642  if (am_tablesync_worker())
1643  ereport(LOG,
1644  (errmsg("logical replication table synchronization worker for subscription \"%s\", table \"%s\" has started",
1646  else
1647  ereport(LOG,
1648  (errmsg("logical replication apply worker for subscription \"%s\" has started",
1649  MySubscription->name)));
1650 
1652 
1653  /* Connect to the origin and start the replication. */
1654  elog(DEBUG1, "connecting to publisher using connection string \"%s\"",
1656 
1657  if (am_tablesync_worker())
1658  {
1659  char *syncslotname;
1660 
1661  /* This is table synchronization worker, call initial sync. */
1662  syncslotname = LogicalRepSyncTableStart(&origin_startpos);
1663 
1664  /* The slot name needs to be allocated in permanent memory context. */
1666  myslotname = pstrdup(syncslotname);
1667  MemoryContextSwitchTo(oldctx);
1668 
1669  pfree(syncslotname);
1670  }
1671  else
1672  {
1673  /* This is main apply worker */
1674  RepOriginId originid;
1675  TimeLineID startpointTLI;
1676  char *err;
1677 
1678  myslotname = MySubscription->slotname;
1679 
1680  /*
1681  * This shouldn't happen if the subscription is enabled, but guard
1682  * against DDL bugs or manual catalog changes. (libpqwalreceiver will
1683  * crash if slot is NULL.)
1684  */
1685  if (!myslotname)
1686  ereport(ERROR,
1687  (errmsg("subscription has no replication slot set")));
1688 
1689  /* Setup replication origin tracking. */
1691  snprintf(originname, sizeof(originname), "pg_%u", MySubscription->oid);
1692  originid = replorigin_by_name(originname, true);
1693  if (!OidIsValid(originid))
1694  originid = replorigin_create(originname);
1695  replorigin_session_setup(originid);
1696  replorigin_session_origin = originid;
1697  origin_startpos = replorigin_session_get_progress(false);
1699 
1701  &err);
1702  if (wrconn == NULL)
1703  ereport(ERROR,
1704  (errmsg("could not connect to the publisher: %s", err)));
1705 
1706  /*
1707  * We don't really use the output identify_system for anything but it
1708  * does some initializations on the upstream so let's still call it.
1709  */
1710  (void) walrcv_identify_system(wrconn, &startpointTLI);
1711 
1712  }
1713 
1714  /*
1715  * Setup callback for syscache so that we know when something changes in
1716  * the subscription relation state.
1717  */
1720  (Datum) 0);
1721 
1722  /* Build logical replication streaming options. */
1723  options.logical = true;
1724  options.startpoint = origin_startpos;
1725  options.slotname = myslotname;
1726  options.proto.logical.proto_version = LOGICALREP_PROTO_VERSION_NUM;
1727  options.proto.logical.publication_names = MySubscription->publications;
1728 
1729  /* Start normal logical streaming replication. */
1730  walrcv_startstreaming(wrconn, &options);
1731 
1732  /* Run the main loop. */
1733  LogicalRepApplyLoop(origin_startpos);
1734 
1735  proc_exit(0);
1736 }
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:2895
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:1192
void replorigin_session_setup(RepOriginId node)
Definition: origin.c:1057
#define LOG
Definition: elog.h:26
char * LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
Definition: tablesync.c:805
#define OidIsValid(objectId)
Definition: c.h:638
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:212
#define NAMEDATALEN
Subscription * GetSubscription(Oid subid, bool missing_ok)
static void subscription_change_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: worker.c:1544
void pfree(void *pointer)
Definition: mcxt.c:1056
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:64
#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:637
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:7487
#define SIGHUP
Definition: win32_port.h:154
XLogRecPtr startpoint
Definition: walreceiver.h:153
List * publications
RepOriginId replorigin_create(char *roname)
Definition: origin.c:243
#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:1551
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:156
void StartTransactionCommand(void)
Definition: xact.c:2794
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:784
static void LogicalRepApplyLoop(XLogRecPtr last_received)
Definition: worker.c:1090
#define elog(elevel,...)
Definition: elog.h:226
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1730
#define snprintf
Definition: port.h:192
#define die(msg)
Definition: pg_test_fsync.c:97
TimestampTz reply_time
void invalidate_syncing_table_states(Datum arg, int cacheid, uint32 hashvalue)
Definition: tablesync.c:263
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 1742 of file worker.c.

References MyLogicalRepWorker.

Referenced by ProcessInterrupts().

1743 {
1744  return MyLogicalRepWorker != NULL;
1745 }
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:64