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

1495 {
1496  int worker_slot = DatumGetInt32(main_arg);
1497  MemoryContext oldctx;
1498  char originname[NAMEDATALEN];
1499  XLogRecPtr origin_startpos;
1500  char *myslotname;
1502 
1503  /* Attach to slot */
1504  logicalrep_worker_attach(worker_slot);
1505 
1506  /* Setup signal handling */
1508  pqsignal(SIGTERM, die);
1510 
1511  /* Initialise stats to a sanish value */
1514 
1515  /* Load the libpq-specific functions */
1516  load_file("libpqwalreceiver", false);
1517 
1518  Assert(CurrentResourceOwner == NULL);
1520  "logical replication apply");
1521 
1522  /* Run as replica session replication role. */
1523  SetConfigOption("session_replication_role", "replica",
1525 
1526  /* Connect to our database. */
1529 
1530  /* Load the subscription into persistent memory context. */
1532  "ApplyContext",
1537  MySubscriptionValid = true;
1538  MemoryContextSwitchTo(oldctx);
1539 
1540  /* Setup synchronous commit according to the user's wishes */
1541  SetConfigOption("synchronous_commit", MySubscription->synccommit,
1543 
1544  if (!MySubscription->enabled)
1545  {
1546  ereport(LOG,
1547  (errmsg("logical replication apply worker for subscription \"%s\" will not "
1548  "start because the subscription was disabled during startup",
1549  MySubscription->name)));
1550 
1551  proc_exit(0);
1552  }
1553 
1554  /* Keep us informed about subscription changes. */
1557  (Datum) 0);
1558 
1559  if (am_tablesync_worker())
1560  ereport(LOG,
1561  (errmsg("logical replication table synchronization worker for subscription \"%s\", table \"%s\" has started",
1563  else
1564  ereport(LOG,
1565  (errmsg("logical replication apply worker for subscription \"%s\" has started",
1566  MySubscription->name)));
1567 
1569 
1570  /* Connect to the origin and start the replication. */
1571  elog(DEBUG1, "connecting to publisher using connection string \"%s\"",
1573 
1574  if (am_tablesync_worker())
1575  {
1576  char *syncslotname;
1577 
1578  /* This is table synchroniation worker, call initial sync. */
1579  syncslotname = LogicalRepSyncTableStart(&origin_startpos);
1580 
1581  /* The slot name needs to be allocated in permanent memory context. */
1583  myslotname = pstrdup(syncslotname);
1584  MemoryContextSwitchTo(oldctx);
1585 
1586  pfree(syncslotname);
1587  }
1588  else
1589  {
1590  /* This is main apply worker */
1591  RepOriginId originid;
1592  TimeLineID startpointTLI;
1593  char *err;
1594  int server_version;
1595 
1596  myslotname = MySubscription->slotname;
1597 
1598  /*
1599  * This shouldn't happen if the subscription is enabled, but guard
1600  * against DDL bugs or manual catalog changes. (libpqwalreceiver will
1601  * crash if slot is NULL.)
1602  */
1603  if (!myslotname)
1604  ereport(ERROR,
1605  (errmsg("subscription has no replication slot set")));
1606 
1607  /* Setup replication origin tracking. */
1609  snprintf(originname, sizeof(originname), "pg_%u", MySubscription->oid);
1610  originid = replorigin_by_name(originname, true);
1611  if (!OidIsValid(originid))
1612  originid = replorigin_create(originname);
1613  replorigin_session_setup(originid);
1614  replorigin_session_origin = originid;
1615  origin_startpos = replorigin_session_get_progress(false);
1617 
1619  &err);
1620  if (wrconn == NULL)
1621  ereport(ERROR,
1622  (errmsg("could not connect to the publisher: %s", err)));
1623 
1624  /*
1625  * We don't really use the output identify_system for anything but it
1626  * does some initializations on the upstream so let's still call it.
1627  */
1628  (void) walrcv_identify_system(wrconn, &startpointTLI,
1629  &server_version);
1630 
1631  }
1632 
1633  /*
1634  * Setup callback for syscache so that we know when something changes in
1635  * the subscription relation state.
1636  */
1639  (Datum) 0);
1640 
1641  /* Build logical replication streaming options. */
1642  options.logical = true;
1643  options.startpoint = origin_startpos;
1644  options.slotname = myslotname;
1645  options.proto.logical.proto_version = LOGICALREP_PROTO_VERSION_NUM;
1646  options.proto.logical.publication_names = MySubscription->publications;
1647 
1648  /* Start normal logical streaming replication. */
1649  walrcv_startstreaming(wrconn, &options);
1650 
1651  /* Run the main loop. */
1652  LogicalRepApplyLoop(origin_startpos);
1653 
1654  proc_exit(0);
1655 }
Subscription * MySubscription
Definition: worker.c:112
#define LOGICALREP_PROTO_VERSION_NUM
Definition: logicalproto.h:28
WalReceiverConn * wrconn
Definition: worker.c:110
#define DEBUG1
Definition: elog.h:25
uint32 TimeLineID
Definition: xlogdefs.h:45
#define DatumGetInt32(X)
Definition: postgres.h:478
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
char * pstrdup(const char *in)
Definition: mcxt.c:1063
void CommitTransactionCommand(void)
Definition: xact.c:2745
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:253
void proc_exit(int code)
Definition: ipc.c:99
XLogRecPtr replorigin_session_get_progress(bool flush)
Definition: origin.c:1148
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void replorigin_session_setup(RepOriginId node)
Definition: origin.c:1013
#define LOG
Definition: elog.h:26
char * LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
Definition: tablesync.c:815
#define OidIsValid(objectId)
Definition: c.h:586
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:211
#define NAMEDATALEN
Subscription * GetSubscription(Oid subid, bool missing_ok)
static void subscription_change_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: worker.c:1473
void pfree(void *pointer)
Definition: mcxt.c:936
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:197
#define walrcv_identify_system(conn, primary_tli, server_version)
Definition: walreceiver.h:249
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:6700
#define SIGHUP
Definition: win32_port.h:163
XLogRecPtr startpoint
Definition: walreceiver.h:146
List * publications
RepOriginId replorigin_create(char *roname)
Definition: origin.c:242
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid)
Definition: postmaster.c:5611
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
MemoryContext ApplyContext
Definition: worker.c:108
static char ** options
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:165
union WalRcvStreamOptions::@97 proto
static void logicalrep_worker_sighup(SIGNAL_ARGS)
Definition: worker.c:1480
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1389
uintptr_t Datum
Definition: postgres.h:372
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:680
RepOriginId replorigin_session_origin
Definition: origin.c:155
void StartTransactionCommand(void)
Definition: xact.c:2674
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:137
static int server_version
Definition: pg_dumpall.c:82
bool MySubscriptionValid
Definition: worker.c:113
int errmsg(const char *fmt,...)
Definition: elog.c:797
void die(SIGNAL_ARGS)
Definition: postgres.c:2652
static void LogicalRepApplyLoop(XLogRecPtr last_received)
Definition: worker.c:1015
#define elog
Definition: elog.h:219
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1745
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:416
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5640
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:243

◆ IsLogicalWorker()

bool IsLogicalWorker ( void  )

Definition at line 1661 of file worker.c.

References MyLogicalRepWorker.

Referenced by ProcessInterrupts().

1662 {
1663  return MyLogicalRepWorker != NULL;
1664 }
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:63