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)
 

Function Documentation

void ApplyWorkerMain ( Datum  main_arg)

Definition at line 1416 of file worker.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), am_tablesync_worker(), Assert, BackgroundWorkerInitializeConnectionByOid(), BackgroundWorkerUnblockSignals(), BackgroundWorker::bgw_name, CacheMemoryContext, CacheRegisterSyscacheCallback(), CommitTransactionCommand(), Subscription::conninfo, CreateCacheMemoryContext(), CurrentResourceOwner, DatumGetObjectId, LogicalRepWorker::dbid, DEBUG1, 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_sigterm(), LogicalRepApplyLoop(), LogicalRepSyncTableStart(), MemoryContextSwitchTo(), MyBgworkerEntry, MyLogicalRepWorker, MySubscriptionValid, Subscription::name, NAMEDATALEN, NULL, Subscription::oid, OidIsValid, options, pfree(), PGC_S_OVERRIDE, PGC_S_SESSION, PGC_SUSET, PGC_USERSET, 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(), Subscription::slotname, WalRcvStreamOptions::slotname, snprintf(), WalRcvStreamOptions::startpoint, StartTransactionCommand(), LogicalRepWorker::subid, subscription_change_cb(), SUBSCRIPTIONOID, SUBSCRIPTIONRELMAP, LogicalRepWorker::userid, walrcv_connect, walrcv_disconnect, walrcv_identify_system, and walrcv_startstreaming.

Referenced by logicalrep_worker_launch().

1417 {
1418  int worker_slot = DatumGetObjectId(main_arg);
1419  MemoryContext oldctx;
1420  char originname[NAMEDATALEN];
1421  XLogRecPtr origin_startpos;
1422  char *myslotname;
1424 
1425  /* Attach to slot */
1426  logicalrep_worker_attach(worker_slot);
1427 
1428  /* Setup signal handling */
1431 
1432  /* Initialise stats to a sanish value */
1435 
1436  /* Make it easy to identify our processes. */
1437  SetConfigOption("application_name", MyBgworkerEntry->bgw_name,
1439 
1440  /* Load the libpq-specific functions */
1441  load_file("libpqwalreceiver", false);
1442 
1445  "logical replication apply");
1446 
1447  /* Run as replica session replication role. */
1448  SetConfigOption("session_replication_role", "replica",
1450 
1451  /* Connect to our database. */
1454 
1455  /* Load the subscription into persistent memory context. */
1458  "ApplyCacheContext",
1463  MySubscriptionValid = true;
1464  MemoryContextSwitchTo(oldctx);
1465 
1466  if (!MySubscription->enabled)
1467  {
1468  ereport(LOG,
1469  (errmsg("logical replication worker for subscription \"%s\" will not "
1470  "start because the subscription was disabled during startup",
1471  MySubscription->name)));
1472 
1473  proc_exit(0);
1474  }
1475 
1476  /* Keep us informed about subscription changes. */
1479  (Datum) 0);
1480 
1481  if (am_tablesync_worker())
1482  elog(LOG, "logical replication sync for subscription %s, table %s started",
1484  else
1485  elog(LOG, "logical replication apply for subscription %s started",
1486  MySubscription->name);
1487 
1489 
1490  /* Connect to the origin and start the replication. */
1491  elog(DEBUG1, "connecting to publisher using connection string \"%s\"",
1493 
1494  if (am_tablesync_worker())
1495  {
1496  char *syncslotname;
1497 
1498  /* This is table synchroniation worker, call initial sync. */
1499  syncslotname = LogicalRepSyncTableStart(&origin_startpos);
1500 
1501  /* The slot name needs to be allocated in permanent memory context. */
1503  myslotname = pstrdup(syncslotname);
1504  MemoryContextSwitchTo(oldctx);
1505 
1506  pfree(syncslotname);
1507  }
1508  else
1509  {
1510  /* This is main apply worker */
1511  RepOriginId originid;
1512  TimeLineID startpointTLI;
1513  char *err;
1514  int server_version;
1515 
1516  myslotname = MySubscription->slotname;
1517 
1518  /* Setup replication origin tracking. */
1520  snprintf(originname, sizeof(originname), "pg_%u", MySubscription->oid);
1521  originid = replorigin_by_name(originname, true);
1522  if (!OidIsValid(originid))
1523  originid = replorigin_create(originname);
1524  replorigin_session_setup(originid);
1525  replorigin_session_origin = originid;
1526  origin_startpos = replorigin_session_get_progress(false);
1528 
1529  wrconn = walrcv_connect(MySubscription->conninfo, true, myslotname,
1530  &err);
1531  if (wrconn == NULL)
1532  ereport(ERROR,
1533  (errmsg("could not connect to the publisher: %s", err)));
1534 
1535  /*
1536  * We don't really use the output identify_system for anything
1537  * but it does some initializations on the upstream so let's still
1538  * call it.
1539  */
1540  (void) walrcv_identify_system(wrconn, &startpointTLI,
1541  &server_version);
1542 
1543  }
1544 
1545  /*
1546  * Setup callback for syscache so that we know when something
1547  * changes in the subscription relation state.
1548  */
1551  (Datum) 0);
1552 
1553  /* Build logical replication streaming options. */
1554  options.logical = true;
1555  options.startpoint = origin_startpos;
1556  options.slotname = myslotname;
1557  options.proto.logical.proto_version = LOGICALREP_PROTO_VERSION_NUM;
1558  options.proto.logical.publication_names = MySubscription->publications;
1559 
1560  /* Start normal logical streaming replication. */
1561  walrcv_startstreaming(wrconn, &options);
1562 
1563  /* Run the main loop. */
1564  LogicalRepApplyLoop(origin_startpos);
1565 
1567 
1568  /* We should only get here if we received SIGTERM */
1569  proc_exit(0);
1570 }
Subscription * MySubscription
Definition: worker.c:109
#define LOGICALREP_PROTO_VERSION_NUM
Definition: logicalproto.h:28
WalReceiverConn * wrconn
Definition: worker.c:107
#define DEBUG1
Definition: elog.h:25
MemoryContext ApplyCacheContext
Definition: worker.c:105
uint32 TimeLineID
Definition: xlogdefs.h:45
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1569
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
#define DatumGetObjectId(X)
Definition: postgres.h:506
char * pstrdup(const char *in)
Definition: mcxt.c:1077
void CommitTransactionCommand(void)
Definition: xact.c:2747
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:1107
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:189
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void replorigin_session_setup(RepOriginId node)
Definition: origin.c:980
#define LOG
Definition: elog.h:26
char * LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
Definition: tablesync.c:693
#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:1408
void pfree(void *pointer)
Definition: mcxt.c:950
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:62
#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:445
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:6651
XLogRecPtr startpoint
Definition: walreceiver.h:147
List * publications
RepOriginId replorigin_create(char *roname)
Definition: origin.c:235
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid)
Definition: postmaster.c:5475
#define ereport(elevel, rest)
Definition: elog.h:122
static char ** options
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:1381
uintptr_t Datum
Definition: postgres.h:372
void logicalrep_worker_sigterm(SIGNAL_ARGS)
Definition: launcher.c:495
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
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define Assert(condition)
Definition: c.h:675
RepOriginId replorigin_session_origin
Definition: origin.c:150
void StartTransactionCommand(void)
Definition: xact.c:2677
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:133
static int server_version
Definition: pg_dumpall.c:80
void CreateCacheMemoryContext(void)
Definition: catcache.c:525
#define walrcv_disconnect(conn)
Definition: walreceiver.h:264
bool MySubscriptionValid
Definition: worker.c:110
union WalRcvStreamOptions::@90 proto
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void LogicalRepApplyLoop(XLogRecPtr last_received)
Definition: worker.c:992
#define elog
Definition: elog.h:219
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1694
TimestampTz reply_time
void invalidate_syncing_table_states(Datum arg, int cacheid, uint32 hashvalue)
Definition: tablesync.c:185
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:416
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5504
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:242