PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

void ApplyWorkerMain ( Datum  main_arg)

Definition at line 1477 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, NULL, 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.

1478 {
1479  int worker_slot = DatumGetInt32(main_arg);
1480  MemoryContext oldctx;
1481  char originname[NAMEDATALEN];
1482  XLogRecPtr origin_startpos;
1483  char *myslotname;
1485 
1486  /* Attach to slot */
1487  logicalrep_worker_attach(worker_slot);
1488 
1489  /* Setup signal handling */
1491  pqsignal(SIGTERM, die);
1493 
1494  /* Initialise stats to a sanish value */
1497 
1498  /* Load the libpq-specific functions */
1499  load_file("libpqwalreceiver", false);
1500 
1503  "logical replication apply");
1504 
1505  /* Run as replica session replication role. */
1506  SetConfigOption("session_replication_role", "replica",
1508 
1509  /* Connect to our database. */
1512 
1513  /* Load the subscription into persistent memory context. */
1515  "ApplyContext",
1520  MySubscriptionValid = true;
1521  MemoryContextSwitchTo(oldctx);
1522 
1523  /* Setup synchronous commit according to the user's wishes */
1524  SetConfigOption("synchronous_commit", MySubscription->synccommit,
1526 
1527  if (!MySubscription->enabled)
1528  {
1529  ereport(LOG,
1530  (errmsg("logical replication apply worker for subscription \"%s\" will not "
1531  "start because the subscription was disabled during startup",
1532  MySubscription->name)));
1533 
1534  proc_exit(0);
1535  }
1536 
1537  /* Keep us informed about subscription changes. */
1540  (Datum) 0);
1541 
1542  if (am_tablesync_worker())
1543  ereport(LOG,
1544  (errmsg("logical replication table synchronization worker for subscription \"%s\", table \"%s\" has started",
1546  else
1547  ereport(LOG,
1548  (errmsg("logical replication apply worker for subscription \"%s\" has started",
1549  MySubscription->name)));
1550 
1552 
1553  /* Connect to the origin and start the replication. */
1554  elog(DEBUG1, "connecting to publisher using connection string \"%s\"",
1556 
1557  if (am_tablesync_worker())
1558  {
1559  char *syncslotname;
1560 
1561  /* This is table synchroniation worker, call initial sync. */
1562  syncslotname = LogicalRepSyncTableStart(&origin_startpos);
1563 
1564  /* The slot name needs to be allocated in permanent memory context. */
1566  myslotname = pstrdup(syncslotname);
1567  MemoryContextSwitchTo(oldctx);
1568 
1569  pfree(syncslotname);
1570  }
1571  else
1572  {
1573  /* This is main apply worker */
1574  RepOriginId originid;
1575  TimeLineID startpointTLI;
1576  char *err;
1577  int server_version;
1578 
1579  myslotname = MySubscription->slotname;
1580 
1581  /*
1582  * This shouldn't happen if the subscription is enabled, but guard
1583  * against DDL bugs or manual catalog changes. (libpqwalreceiver will
1584  * crash if slot is NULL.)
1585  */
1586  if (!myslotname)
1587  ereport(ERROR,
1588  (errmsg("subscription has no replication slot set")));
1589 
1590  /* Setup replication origin tracking. */
1592  snprintf(originname, sizeof(originname), "pg_%u", MySubscription->oid);
1593  originid = replorigin_by_name(originname, true);
1594  if (!OidIsValid(originid))
1595  originid = replorigin_create(originname);
1596  replorigin_session_setup(originid);
1597  replorigin_session_origin = originid;
1598  origin_startpos = replorigin_session_get_progress(false);
1600 
1602  &err);
1603  if (wrconn == NULL)
1604  ereport(ERROR,
1605  (errmsg("could not connect to the publisher: %s", err)));
1606 
1607  /*
1608  * We don't really use the output identify_system for anything but it
1609  * does some initializations on the upstream so let's still call it.
1610  */
1611  (void) walrcv_identify_system(wrconn, &startpointTLI,
1612  &server_version);
1613 
1614  }
1615 
1616  /*
1617  * Setup callback for syscache so that we know when something changes in
1618  * the subscription relation state.
1619  */
1622  (Datum) 0);
1623 
1624  /* Build logical replication streaming options. */
1625  options.logical = true;
1626  options.startpoint = origin_startpos;
1627  options.slotname = myslotname;
1628  options.proto.logical.proto_version = LOGICALREP_PROTO_VERSION_NUM;
1629  options.proto.logical.publication_names = MySubscription->publications;
1630 
1631  /* Start normal logical streaming replication. */
1632  walrcv_startstreaming(wrconn, &options);
1633 
1634  /* Run the main loop. */
1635  LogicalRepApplyLoop(origin_startpos);
1636 
1637  proc_exit(0);
1638 }
Subscription * MySubscription
Definition: worker.c:111
#define LOGICALREP_PROTO_VERSION_NUM
Definition: logicalproto.h:28
WalReceiverConn * wrconn
Definition: worker.c:109
#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:1077
void CommitTransactionCommand(void)
Definition: xact.c:2748
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:252
void proc_exit(int code)
Definition: ipc.c:99
XLogRecPtr replorigin_session_get_progress(bool flush)
Definition: origin.c:1110
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void replorigin_session_setup(RepOriginId node)
Definition: origin.c:983
#define LOG
Definition: elog.h:26
char * LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
Definition: tablesync.c:789
#define OidIsValid(objectId)
Definition: c.h:538
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:206
#define NAMEDATALEN
Subscription * GetSubscription(Oid subid, bool missing_ok)
static void subscription_change_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: worker.c:1456
void pfree(void *pointer)
Definition: mcxt.c:950
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:165
#define walrcv_identify_system(conn, primary_tli, server_version)
Definition: walreceiver.h:248
void logicalrep_worker_attach(int slot)
Definition: launcher.c:546
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:6672
XLogRecPtr startpoint
Definition: walreceiver.h:145
List * publications
RepOriginId replorigin_create(char *roname)
Definition: origin.c:235
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid)
Definition: postmaster.c:5489
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
MemoryContext ApplyContext
Definition: worker.c:107
static char ** options
union WalRcvStreamOptions::@97 proto
static void logicalrep_worker_sighup(SIGNAL_ARGS)
Definition: worker.c:1463
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1389
uintptr_t Datum
Definition: postgres.h:372
#define SIGHUP
Definition: win32.h:188
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
TimestampTz last_recv_time
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:675
RepOriginId replorigin_session_origin
Definition: origin.c:150
void StartTransactionCommand(void)
Definition: xact.c:2678
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:112
int errmsg(const char *fmt,...)
Definition: elog.c:797
void die(SIGNAL_ARGS)
Definition: postgres.c:2617
static void LogicalRepApplyLoop(XLogRecPtr last_received)
Definition: worker.c:1008
#define elog
Definition: elog.h:219
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1726
TimestampTz reply_time
void invalidate_syncing_table_states(Datum arg, int cacheid, uint32 hashvalue)
Definition: tablesync.c:258
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:416
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5518
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:242
bool IsLogicalWorker ( void  )

Definition at line 1644 of file worker.c.

References MyLogicalRepWorker, and NULL.

Referenced by ProcessInterrupts().

1645 {
1646  return MyLogicalRepWorker != NULL;
1647 }
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:63
#define NULL
Definition: c.h:229