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 1946 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(), 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, SignalHandlerForConfigReload(), 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.

1947 {
1948  int worker_slot = DatumGetInt32(main_arg);
1949  MemoryContext oldctx;
1950  char originname[NAMEDATALEN];
1951  XLogRecPtr origin_startpos;
1952  char *myslotname;
1954 
1955  /* Attach to slot */
1956  logicalrep_worker_attach(worker_slot);
1957 
1958  /* Setup signal handling */
1960  pqsignal(SIGTERM, die);
1962 
1963  /*
1964  * We don't currently need any ResourceOwner in a walreceiver process, but
1965  * if we did, we could call CreateAuxProcessResourceOwner here.
1966  */
1967 
1968  /* Initialise stats to a sanish value */
1971 
1972  /* Load the libpq-specific functions */
1973  load_file("libpqwalreceiver", false);
1974 
1975  /* Run as replica session replication role. */
1976  SetConfigOption("session_replication_role", "replica",
1978 
1979  /* Connect to our database. */
1982  0);
1983 
1984  /* Load the subscription into persistent memory context. */
1986  "ApplyContext",
1990 
1992  if (!MySubscription)
1993  {
1994  ereport(LOG,
1995  (errmsg("logical replication apply worker for subscription %u will not "
1996  "start because the subscription was removed during startup",
1998  proc_exit(0);
1999  }
2000 
2001  MySubscriptionValid = true;
2002  MemoryContextSwitchTo(oldctx);
2003 
2004  if (!MySubscription->enabled)
2005  {
2006  ereport(LOG,
2007  (errmsg("logical replication apply worker for subscription \"%s\" will not "
2008  "start because the subscription was disabled during startup",
2009  MySubscription->name)));
2010 
2011  proc_exit(0);
2012  }
2013 
2014  /* Setup synchronous commit according to the user's wishes */
2015  SetConfigOption("synchronous_commit", MySubscription->synccommit,
2017 
2018  /* Keep us informed about subscription changes. */
2021  (Datum) 0);
2022 
2023  if (am_tablesync_worker())
2024  ereport(LOG,
2025  (errmsg("logical replication table synchronization worker for subscription \"%s\", table \"%s\" has started",
2027  else
2028  ereport(LOG,
2029  (errmsg("logical replication apply worker for subscription \"%s\" has started",
2030  MySubscription->name)));
2031 
2033 
2034  /* Connect to the origin and start the replication. */
2035  elog(DEBUG1, "connecting to publisher using connection string \"%s\"",
2037 
2038  if (am_tablesync_worker())
2039  {
2040  char *syncslotname;
2041 
2042  /* This is table synchronization worker, call initial sync. */
2043  syncslotname = LogicalRepSyncTableStart(&origin_startpos);
2044 
2045  /* The slot name needs to be allocated in permanent memory context. */
2047  myslotname = pstrdup(syncslotname);
2048  MemoryContextSwitchTo(oldctx);
2049 
2050  pfree(syncslotname);
2051  }
2052  else
2053  {
2054  /* This is main apply worker */
2055  RepOriginId originid;
2056  TimeLineID startpointTLI;
2057  char *err;
2058 
2059  myslotname = MySubscription->slotname;
2060 
2061  /*
2062  * This shouldn't happen if the subscription is enabled, but guard
2063  * against DDL bugs or manual catalog changes. (libpqwalreceiver will
2064  * crash if slot is NULL.)
2065  */
2066  if (!myslotname)
2067  ereport(ERROR,
2068  (errmsg("subscription has no replication slot set")));
2069 
2070  /* Setup replication origin tracking. */
2072  snprintf(originname, sizeof(originname), "pg_%u", MySubscription->oid);
2073  originid = replorigin_by_name(originname, true);
2074  if (!OidIsValid(originid))
2075  originid = replorigin_create(originname);
2076  replorigin_session_setup(originid);
2077  replorigin_session_origin = originid;
2078  origin_startpos = replorigin_session_get_progress(false);
2080 
2082  &err);
2083  if (wrconn == NULL)
2084  ereport(ERROR,
2085  (errmsg("could not connect to the publisher: %s", err)));
2086 
2087  /*
2088  * We don't really use the output identify_system for anything but it
2089  * does some initializations on the upstream so let's still call it.
2090  */
2091  (void) walrcv_identify_system(wrconn, &startpointTLI);
2092 
2093  }
2094 
2095  /*
2096  * Setup callback for syscache so that we know when something changes in
2097  * the subscription relation state.
2098  */
2101  (Datum) 0);
2102 
2103  /* Build logical replication streaming options. */
2104  options.logical = true;
2105  options.startpoint = origin_startpos;
2106  options.slotname = myslotname;
2107  options.proto.logical.proto_version = LOGICALREP_PROTO_VERSION_NUM;
2108  options.proto.logical.publication_names = MySubscription->publications;
2109 
2110  /* Start normal logical streaming replication. */
2111  walrcv_startstreaming(wrconn, &options);
2112 
2113  /* Run the main loop. */
2114  LogicalRepApplyLoop(origin_startpos);
2115 
2116  proc_exit(0);
2117 }
Subscription * MySubscription
Definition: worker.c:107
#define LOGICALREP_PROTO_VERSION_NUM
Definition: logicalproto.h:28
WalReceiverConn * wrconn
Definition: worker.c:105
#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:280
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:56
char * pstrdup(const char *in)
Definition: mcxt.c:1186
void CommitTransactionCommand(void)
Definition: xact.c:2917
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:286
union WalRcvStreamOptions::@104 proto
void proc_exit(int code)
Definition: ipc.c:104
XLogRecPtr replorigin_session_get_progress(bool flush)
Definition: origin.c:1191
void replorigin_session_setup(RepOriginId node)
Definition: origin.c:1056
#define LOG
Definition: elog.h:26
char * LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
Definition: tablesync.c:816
#define OidIsValid(objectId)
Definition: c.h:644
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:214
#define NAMEDATALEN
Subscription * GetSubscription(Oid subid, bool missing_ok)
static void subscription_change_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: worker.c:1939
void pfree(void *pointer)
Definition: mcxt.c:1056
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57
#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:625
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:7706
#define SIGHUP
Definition: win32_port.h:153
XLogRecPtr startpoint
Definition: walreceiver.h:168
List * publications
RepOriginId replorigin_create(char *roname)
Definition: origin.c:245
MemoryContext TopMemoryContext
Definition: mcxt.c:44
MemoryContext ApplyContext
Definition: worker.c:103
static char ** options
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:5734
#define ereport(elevel,...)
Definition: elog.h:144
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:154
void StartTransactionCommand(void)
Definition: xact.c:2816
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:146
bool MySubscriptionValid
Definition: worker.c:108
int errmsg(const char *fmt,...)
Definition: elog.c:824
static void LogicalRepApplyLoop(XLogRecPtr last_received)
Definition: worker.c:1484
#define elog(elevel,...)
Definition: elog.h:214
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1791
#define snprintf
Definition: port.h:193
#define die(msg)
Definition: pg_test_fsync.c:96
TimestampTz reply_time
void invalidate_syncing_table_states(Datum arg, int cacheid, uint32 hashvalue)
Definition: tablesync.c:256
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5763
#define walrcv_connect(conninfo, logical, appname, err)
Definition: walreceiver.h:272

◆ IsLogicalWorker()

bool IsLogicalWorker ( void  )

Definition at line 2123 of file worker.c.

References MyLogicalRepWorker.

Referenced by ProcessInterrupts().

2124 {
2125  return MyLogicalRepWorker != NULL;
2126 }
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:57