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 1442 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, DatumGetInt32, 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_sighup(), logicalrep_worker_sigterm(), LogicalRepApplyLoop(), LogicalRepSyncTableStart(), MemoryContextSwitchTo(), MyBgworkerEntry, MyLogicalRepWorker, MySubscriptionValid, Subscription::name, NAMEDATALEN, NULL, Subscription::oid, OidIsValid, options, pfree(), PGC_BACKEND, 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(), SIGHUP, Subscription::slotname, WalRcvStreamOptions::slotname, snprintf(), WalRcvStreamOptions::startpoint, StartTransactionCommand(), LogicalRepWorker::subid, subscription_change_cb(), SUBSCRIPTIONOID, SUBSCRIPTIONRELMAP, Subscription::synccommit, LogicalRepWorker::userid, walrcv_connect, walrcv_disconnect, walrcv_identify_system, and walrcv_startstreaming.

1443 {
1444  int worker_slot = DatumGetInt32(main_arg);
1445  MemoryContext oldctx;
1446  char originname[NAMEDATALEN];
1447  XLogRecPtr origin_startpos;
1448  char *myslotname;
1450 
1451  /* Attach to slot */
1452  logicalrep_worker_attach(worker_slot);
1453 
1454  /* Setup signal handling */
1458 
1459  /* Initialise stats to a sanish value */
1462 
1463  /* Make it easy to identify our processes. */
1464  SetConfigOption("application_name", MyBgworkerEntry->bgw_name,
1466 
1467  /* Load the libpq-specific functions */
1468  load_file("libpqwalreceiver", false);
1469 
1472  "logical replication apply");
1473 
1474  /* Run as replica session replication role. */
1475  SetConfigOption("session_replication_role", "replica",
1477 
1478  /* Connect to our database. */
1481 
1482  /* Load the subscription into persistent memory context. */
1485  "ApplyCacheContext",
1490  MySubscriptionValid = true;
1491  MemoryContextSwitchTo(oldctx);
1492 
1493  /* Setup synchronous commit according to the user's wishes */
1494  SetConfigOption("synchronous_commit", MySubscription->synccommit,
1496 
1497  if (!MySubscription->enabled)
1498  {
1499  ereport(LOG,
1500  (errmsg("logical replication worker for subscription \"%s\" will not "
1501  "start because the subscription was disabled during startup",
1502  MySubscription->name)));
1503 
1504  proc_exit(0);
1505  }
1506 
1507  /* Keep us informed about subscription changes. */
1510  (Datum) 0);
1511 
1512  if (am_tablesync_worker())
1513  elog(LOG, "logical replication sync for subscription %s, table %s started",
1515  else
1516  elog(LOG, "logical replication apply for subscription %s started",
1517  MySubscription->name);
1518 
1520 
1521  /* Connect to the origin and start the replication. */
1522  elog(DEBUG1, "connecting to publisher using connection string \"%s\"",
1524 
1525  if (am_tablesync_worker())
1526  {
1527  char *syncslotname;
1528 
1529  /* This is table synchroniation worker, call initial sync. */
1530  syncslotname = LogicalRepSyncTableStart(&origin_startpos);
1531 
1532  /* The slot name needs to be allocated in permanent memory context. */
1534  myslotname = pstrdup(syncslotname);
1535  MemoryContextSwitchTo(oldctx);
1536 
1537  pfree(syncslotname);
1538  }
1539  else
1540  {
1541  /* This is main apply worker */
1542  RepOriginId originid;
1543  TimeLineID startpointTLI;
1544  char *err;
1545  int server_version;
1546 
1547  myslotname = MySubscription->slotname;
1548 
1549  /* Setup replication origin tracking. */
1551  snprintf(originname, sizeof(originname), "pg_%u", MySubscription->oid);
1552  originid = replorigin_by_name(originname, true);
1553  if (!OidIsValid(originid))
1554  originid = replorigin_create(originname);
1555  replorigin_session_setup(originid);
1556  replorigin_session_origin = originid;
1557  origin_startpos = replorigin_session_get_progress(false);
1559 
1560  wrconn = walrcv_connect(MySubscription->conninfo, true, myslotname,
1561  &err);
1562  if (wrconn == NULL)
1563  ereport(ERROR,
1564  (errmsg("could not connect to the publisher: %s", err)));
1565 
1566  /*
1567  * We don't really use the output identify_system for anything
1568  * but it does some initializations on the upstream so let's still
1569  * call it.
1570  */
1571  (void) walrcv_identify_system(wrconn, &startpointTLI,
1572  &server_version);
1573 
1574  }
1575 
1576  /*
1577  * Setup callback for syscache so that we know when something
1578  * changes in the subscription relation state.
1579  */
1582  (Datum) 0);
1583 
1584  /* Build logical replication streaming options. */
1585  options.logical = true;
1586  options.startpoint = origin_startpos;
1587  options.slotname = myslotname;
1588  options.proto.logical.proto_version = LOGICALREP_PROTO_VERSION_NUM;
1589  options.proto.logical.publication_names = MySubscription->publications;
1590 
1591  /* Start normal logical streaming replication. */
1592  walrcv_startstreaming(wrconn, &options);
1593 
1594  /* Run the main loop. */
1595  LogicalRepApplyLoop(origin_startpos);
1596 
1598 
1599  /* We should only get here if we received SIGTERM */
1600  proc_exit(0);
1601 }
Subscription * MySubscription
Definition: worker.c:109
#define LOGICALREP_PROTO_VERSION_NUM
Definition: logicalproto.h:28
void logicalrep_worker_sighup(SIGNAL_ARGS)
Definition: launcher.c:635
WalReceiverConn * wrconn
Definition: worker.c:107
#define DEBUG1
Definition: elog.h:25
MemoryContext ApplyCacheContext
Definition: worker.c:105
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: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:1110
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:983
#define LOG
Definition: elog.h:26
char * LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
Definition: tablesync.c:705
#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:1434
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:535
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:6676
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:5489
#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:1382
uintptr_t Datum
Definition: postgres.h:372
void logicalrep_worker_sigterm(SIGNAL_ARGS)
Definition: launcher.c:621
#define SIGHUP
Definition: win32.h:188
union WalRcvStreamOptions::@97 proto
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:137
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
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void LogicalRepApplyLoop(XLogRecPtr last_received)
Definition: worker.c:994
#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:193
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:416
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5518
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:242