PostgreSQL Source Code  git master
walsender.h File Reference
#include "access/xlogdefs.h"
Include dependency graph for walsender.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define WalSndWakeupRequest()    do { wake_wal_senders = true; } while (0)
 

Enumerations

enum  CRSSnapshotAction { CRS_EXPORT_SNAPSHOT , CRS_NOEXPORT_SNAPSHOT , CRS_USE_SNAPSHOT }
 

Functions

void InitWalSender (void)
 
bool exec_replication_command (const char *cmd_string)
 
void WalSndErrorCleanup (void)
 
void WalSndResourceCleanup (bool isCommit)
 
void PhysicalWakeupLogicalWalSnd (void)
 
XLogRecPtr GetStandbyFlushRecPtr (TimeLineID *tli)
 
void WalSndSignals (void)
 
Size WalSndShmemSize (void)
 
void WalSndShmemInit (void)
 
void WalSndWakeup (bool physical, bool logical)
 
void WalSndInitStopping (void)
 
void WalSndWaitStopping (void)
 
void HandleWalSndInitStopping (void)
 
void WalSndRqstFileReload (void)
 
static void WalSndWakeupProcessRequests (bool physical, bool logical)
 

Variables

PGDLLIMPORT bool am_walsender
 
PGDLLIMPORT bool am_cascading_walsender
 
PGDLLIMPORT bool am_db_walsender
 
PGDLLIMPORT bool wake_wal_senders
 
PGDLLIMPORT int max_wal_senders
 
PGDLLIMPORT int wal_sender_timeout
 
PGDLLIMPORT bool log_replication_commands
 

Macro Definition Documentation

◆ WalSndWakeupRequest

#define WalSndWakeupRequest ( )     do { wake_wal_senders = true; } while (0)

Definition at line 59 of file walsender.h.

Enumeration Type Documentation

◆ CRSSnapshotAction

Enumerator
CRS_EXPORT_SNAPSHOT 
CRS_NOEXPORT_SNAPSHOT 
CRS_USE_SNAPSHOT 

Definition at line 20 of file walsender.h.

21 {
CRSSnapshotAction
Definition: walsender.h:21
@ CRS_USE_SNAPSHOT
Definition: walsender.h:24
@ CRS_NOEXPORT_SNAPSHOT
Definition: walsender.h:23
@ CRS_EXPORT_SNAPSHOT
Definition: walsender.h:22

Function Documentation

◆ exec_replication_command()

bool exec_replication_command ( const char *  cmd_string)

Definition at line 1980 of file walsender.c.

1981 {
1982  int parse_rc;
1983  Node *cmd_node;
1984  const char *cmdtag;
1985  MemoryContext cmd_context;
1986  MemoryContext old_context;
1987 
1988  /*
1989  * If WAL sender has been told that shutdown is getting close, switch its
1990  * status accordingly to handle the next replication commands correctly.
1991  */
1992  if (got_STOPPING)
1994 
1995  /*
1996  * Throw error if in stopping mode. We need prevent commands that could
1997  * generate WAL while the shutdown checkpoint is being written. To be
1998  * safe, we just prohibit all new commands.
1999  */
2001  ereport(ERROR,
2002  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2003  errmsg("cannot execute new commands while WAL sender is in stopping mode")));
2004 
2005  /*
2006  * CREATE_REPLICATION_SLOT ... LOGICAL exports a snapshot until the next
2007  * command arrives. Clean up the old stuff if there's anything.
2008  */
2010 
2012 
2013  /*
2014  * Prepare to parse and execute the command.
2015  */
2017  "Replication command context",
2019  old_context = MemoryContextSwitchTo(cmd_context);
2020 
2021  replication_scanner_init(cmd_string);
2022 
2023  /*
2024  * Is it a WalSender command?
2025  */
2027  {
2028  /* Nope; clean up and get out. */
2030 
2031  MemoryContextSwitchTo(old_context);
2032  MemoryContextDelete(cmd_context);
2033 
2034  /* XXX this is a pretty random place to make this check */
2035  if (MyDatabaseId == InvalidOid)
2036  ereport(ERROR,
2037  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2038  errmsg("cannot execute SQL commands in WAL sender for physical replication")));
2039 
2040  /* Tell the caller that this wasn't a WalSender command. */
2041  return false;
2042  }
2043 
2044  /*
2045  * Looks like a WalSender command, so parse it.
2046  */
2047  parse_rc = replication_yyparse();
2048  if (parse_rc != 0)
2049  ereport(ERROR,
2050  (errcode(ERRCODE_SYNTAX_ERROR),
2051  errmsg_internal("replication command parser returned %d",
2052  parse_rc)));
2054 
2055  cmd_node = replication_parse_result;
2056 
2057  /*
2058  * Report query to various monitoring facilities. For this purpose, we
2059  * report replication commands just like SQL commands.
2060  */
2061  debug_query_string = cmd_string;
2062 
2064 
2065  /*
2066  * Log replication command if log_replication_commands is enabled. Even
2067  * when it's disabled, log the command with DEBUG1 level for backward
2068  * compatibility.
2069  */
2071  (errmsg("received replication command: %s", cmd_string)));
2072 
2073  /*
2074  * Disallow replication commands in aborted transaction blocks.
2075  */
2077  ereport(ERROR,
2078  (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
2079  errmsg("current transaction is aborted, "
2080  "commands ignored until end of transaction block")));
2081 
2083 
2084  /*
2085  * Allocate buffers that will be used for each outgoing and incoming
2086  * message. We do this just once per command to reduce palloc overhead.
2087  */
2091 
2092  switch (cmd_node->type)
2093  {
2094  case T_IdentifySystemCmd:
2095  cmdtag = "IDENTIFY_SYSTEM";
2096  set_ps_display(cmdtag);
2097  IdentifySystem();
2098  EndReplicationCommand(cmdtag);
2099  break;
2100 
2101  case T_ReadReplicationSlotCmd:
2102  cmdtag = "READ_REPLICATION_SLOT";
2103  set_ps_display(cmdtag);
2105  EndReplicationCommand(cmdtag);
2106  break;
2107 
2108  case T_BaseBackupCmd:
2109  cmdtag = "BASE_BACKUP";
2110  set_ps_display(cmdtag);
2111  PreventInTransactionBlock(true, cmdtag);
2113  EndReplicationCommand(cmdtag);
2114  break;
2115 
2116  case T_CreateReplicationSlotCmd:
2117  cmdtag = "CREATE_REPLICATION_SLOT";
2118  set_ps_display(cmdtag);
2120  EndReplicationCommand(cmdtag);
2121  break;
2122 
2123  case T_DropReplicationSlotCmd:
2124  cmdtag = "DROP_REPLICATION_SLOT";
2125  set_ps_display(cmdtag);
2127  EndReplicationCommand(cmdtag);
2128  break;
2129 
2130  case T_AlterReplicationSlotCmd:
2131  cmdtag = "ALTER_REPLICATION_SLOT";
2132  set_ps_display(cmdtag);
2134  EndReplicationCommand(cmdtag);
2135  break;
2136 
2137  case T_StartReplicationCmd:
2138  {
2139  StartReplicationCmd *cmd = (StartReplicationCmd *) cmd_node;
2140 
2141  cmdtag = "START_REPLICATION";
2142  set_ps_display(cmdtag);
2143  PreventInTransactionBlock(true, cmdtag);
2144 
2145  if (cmd->kind == REPLICATION_KIND_PHYSICAL)
2146  StartReplication(cmd);
2147  else
2149 
2150  /* dupe, but necessary per libpqrcv_endstreaming */
2151  EndReplicationCommand(cmdtag);
2152 
2153  Assert(xlogreader != NULL);
2154  break;
2155  }
2156 
2157  case T_TimeLineHistoryCmd:
2158  cmdtag = "TIMELINE_HISTORY";
2159  set_ps_display(cmdtag);
2160  PreventInTransactionBlock(true, cmdtag);
2162  EndReplicationCommand(cmdtag);
2163  break;
2164 
2165  case T_VariableShowStmt:
2166  {
2168  VariableShowStmt *n = (VariableShowStmt *) cmd_node;
2169 
2170  cmdtag = "SHOW";
2171  set_ps_display(cmdtag);
2172 
2173  /* syscache access needs a transaction environment */
2175  GetPGVariable(n->name, dest);
2177  EndReplicationCommand(cmdtag);
2178  }
2179  break;
2180 
2181  case T_UploadManifestCmd:
2182  cmdtag = "UPLOAD_MANIFEST";
2183  set_ps_display(cmdtag);
2184  PreventInTransactionBlock(true, cmdtag);
2185  UploadManifest();
2186  EndReplicationCommand(cmdtag);
2187  break;
2188 
2189  default:
2190  elog(ERROR, "unrecognized replication command node tag: %u",
2191  cmd_node->type);
2192  }
2193 
2194  /* done */
2195  MemoryContextSwitchTo(old_context);
2196  MemoryContextDelete(cmd_context);
2197 
2198  /*
2199  * We need not update ps display or pg_stat_activity, because PostgresMain
2200  * will reset those to "idle". But we must reset debug_query_string to
2201  * ensure it doesn't become a dangling pointer.
2202  */
2203  debug_query_string = NULL;
2204 
2205  return true;
2206 }
void pgstat_report_activity(BackendState state, const char *cmd_str)
@ STATE_RUNNING
void SendBaseBackup(BaseBackupCmd *cmd, IncrementalBackupInfo *ib)
Definition: basebackup.c:989
#define Assert(condition)
Definition: c.h:861
void EndReplicationCommand(const char *commandTag)
Definition: dest.c:205
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition: dest.c:113
@ DestRemoteSimple
Definition: dest.h:91
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define LOG
Definition: elog.h:31
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
Oid MyDatabaseId
Definition: globals.c:93
void GetPGVariable(const char *name, DestReceiver *dest)
Definition: guc_funcs.c:382
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
const char * debug_query_string
Definition: postgres.c:88
#define InvalidOid
Definition: postgres_ext.h:36
static void set_ps_display(const char *activity)
Definition: ps_status.h:40
MemoryContextSwitchTo(old_ctx)
@ REPLICATION_KIND_PHYSICAL
Definition: replnodes.h:22
void SnapBuildClearExportedSnapshot(void)
Definition: snapbuild.c:739
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
Definition: nodes.h:129
NodeTag type
Definition: nodes.h:130
ReplicationKind kind
Definition: replnodes.h:94
WalSndState state
static void AlterReplicationSlot(AlterReplicationSlotCmd *cmd)
Definition: walsender.c:1413
static void SendTimeLineHistory(TimeLineHistoryCmd *cmd)
Definition: walsender.c:587
WalSnd * MyWalSnd
Definition: walsender.c:112
static void ReadReplicationSlot(ReadReplicationSlotCmd *cmd)
Definition: walsender.c:488
static StringInfoData tmpbuf
Definition: walsender.c:170
static void IdentifySystem(void)
Definition: walsender.c:407
static StringInfoData reply_message
Definition: walsender.c:169
void WalSndSetState(WalSndState state)
Definition: walsender.c:3809
static StringInfoData output_message
Definition: walsender.c:168
static void UploadManifest(void)
Definition: walsender.c:677
static volatile sig_atomic_t got_STOPPING
Definition: walsender.c:198
bool log_replication_commands
Definition: walsender.c:125
static void CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
Definition: walsender.c:1199
static void StartLogicalReplication(StartReplicationCmd *cmd)
Definition: walsender.c:1455
static IncrementalBackupInfo * uploaded_manifest
Definition: walsender.c:147
static void DropReplicationSlot(DropReplicationSlotCmd *cmd)
Definition: walsender.c:1404
static void StartReplication(StartReplicationCmd *cmd)
Definition: walsender.c:817
static XLogReaderState * xlogreader
Definition: walsender.c:137
PGDLLIMPORT Node * replication_parse_result
@ WALSNDSTATE_STOPPING
void replication_scanner_finish(void)
int replication_yyparse(void)
void replication_scanner_init(const char *str)
bool replication_scanner_is_replication_command(void)
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition: xact.c:3628
void StartTransactionCommand(void)
Definition: xact.c:3039
bool IsAbortedTransactionBlockState(void)
Definition: xact.c:406
void CommitTransactionCommand(void)
Definition: xact.c:3137

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, AlterReplicationSlot(), Assert, CHECK_FOR_INTERRUPTS, CommitTransactionCommand(), CreateDestReceiver(), CreateReplicationSlot(), CurrentMemoryContext, DEBUG1, debug_query_string, generate_unaccent_rules::dest, DestRemoteSimple, DropReplicationSlot(), elog, EndReplicationCommand(), ereport, errcode(), errmsg(), errmsg_internal(), ERROR, GetPGVariable(), got_STOPPING, IdentifySystem(), initStringInfo(), InvalidOid, IsAbortedTransactionBlockState(), StartReplicationCmd::kind, LOG, log_replication_commands, MemoryContextDelete(), MemoryContextSwitchTo(), MyDatabaseId, MyWalSnd, VariableShowStmt::name, output_message, pgstat_report_activity(), PreventInTransactionBlock(), ReadReplicationSlot(), REPLICATION_KIND_PHYSICAL, replication_parse_result, replication_scanner_finish(), replication_scanner_init(), replication_scanner_is_replication_command(), replication_yyparse(), reply_message, SendBaseBackup(), SendTimeLineHistory(), set_ps_display(), SnapBuildClearExportedSnapshot(), StartLogicalReplication(), StartReplication(), StartTransactionCommand(), WalSnd::state, STATE_RUNNING, tmpbuf, Node::type, ReadReplicationSlotCmd::type, uploaded_manifest, UploadManifest(), WalSndSetState(), WALSNDSTATE_STOPPING, and xlogreader.

Referenced by PostgresMain().

◆ GetStandbyFlushRecPtr()

XLogRecPtr GetStandbyFlushRecPtr ( TimeLineID tli)

Definition at line 3504 of file walsender.c.

3505 {
3506  XLogRecPtr replayPtr;
3507  TimeLineID replayTLI;
3508  XLogRecPtr receivePtr;
3510  XLogRecPtr result;
3511 
3513 
3514  /*
3515  * We can safely send what's already been replayed. Also, if walreceiver
3516  * is streaming WAL from the same timeline, we can send anything that it
3517  * has streamed, but hasn't been replayed yet.
3518  */
3519 
3520  receivePtr = GetWalRcvFlushRecPtr(NULL, &receiveTLI);
3521  replayPtr = GetXLogReplayRecPtr(&replayTLI);
3522 
3523  if (tli)
3524  *tli = replayTLI;
3525 
3526  result = replayPtr;
3527  if (receiveTLI == replayTLI && receivePtr > replayPtr)
3528  result = receivePtr;
3529 
3530  return result;
3531 }
bool IsSyncingReplicationSlots(void)
Definition: slotsync.c:1651
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
bool am_cascading_walsender
Definition: walsender.c:116
uint64 XLogRecPtr
Definition: xlogdefs.h:21
uint32 TimeLineID
Definition: xlogdefs.h:59
static TimeLineID receiveTLI
Definition: xlogrecovery.c:264
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)

References am_cascading_walsender, Assert, GetWalRcvFlushRecPtr(), GetXLogReplayRecPtr(), IsSyncingReplicationSlots(), and receiveTLI.

Referenced by IdentifySystem(), StartReplication(), synchronize_one_slot(), XLogSendLogical(), and XLogSendPhysical().

◆ HandleWalSndInitStopping()

void HandleWalSndInitStopping ( void  )

Definition at line 3560 of file walsender.c.

3561 {
3563 
3564  /*
3565  * If replication has not yet started, die like with SIGTERM. If
3566  * replication is active, only set a flag and wake up the main loop. It
3567  * will send any outstanding WAL, wait for it to be replicated to the
3568  * standby, and then exit gracefully.
3569  */
3570  if (!replication_active)
3571  kill(MyProcPid, SIGTERM);
3572  else
3573  got_STOPPING = true;
3574 }
int MyProcPid
Definition: globals.c:46
bool am_walsender
Definition: walsender.c:115
static volatile sig_atomic_t replication_active
Definition: walsender.c:206
#define kill(pid, sig)
Definition: win32_port.h:503

References am_walsender, Assert, got_STOPPING, kill, MyProcPid, and replication_active.

Referenced by procsignal_sigusr1_handler().

◆ InitWalSender()

void InitWalSender ( void  )

Referenced by PostgresMain().

◆ PhysicalWakeupLogicalWalSnd()

void PhysicalWakeupLogicalWalSnd ( void  )

Definition at line 1736 of file walsender.c.

1737 {
1739 
1740  /*
1741  * If we are running in a standby, there is no need to wake up walsenders.
1742  * This is because we do not support syncing slots to cascading standbys,
1743  * so, there are no walsenders waiting for standbys to catch up.
1744  */
1745  if (RecoveryInProgress())
1746  return;
1747 
1750 }
#define NameStr(name)
Definition: c.h:749
void ConditionVariableBroadcast(ConditionVariable *cv)
ReplicationSlot * MyReplicationSlot
Definition: slot.c:138
bool SlotExistsInSyncStandbySlots(const char *slot_name)
Definition: slot.c:2575
#define SlotIsPhysical(slot)
Definition: slot.h:212
ReplicationSlotPersistentData data
Definition: slot.h:181
ConditionVariable wal_confirm_rcv_cv
WalSndCtlData * WalSndCtl
Definition: walsender.c:109
bool RecoveryInProgress(void)
Definition: xlog.c:6333

References Assert, ConditionVariableBroadcast(), ReplicationSlot::data, MyReplicationSlot, ReplicationSlotPersistentData::name, NameStr, RecoveryInProgress(), SlotExistsInSyncStandbySlots(), SlotIsPhysical, WalSndCtlData::wal_confirm_rcv_cv, and WalSndCtl.

Referenced by pg_physical_replication_slot_advance(), and PhysicalConfirmReceivedLocation().

◆ WalSndErrorCleanup()

void WalSndErrorCleanup ( void  )

Definition at line 327 of file walsender.c.

328 {
332 
333  if (xlogreader != NULL && xlogreader->seg.ws_file >= 0)
335 
336  if (MyReplicationSlot != NULL)
338 
339  ReplicationSlotCleanup(false);
340 
341  replication_active = false;
342 
343  /*
344  * If there is a transaction in progress, it will clean up our
345  * ResourceOwner, but if a replication command set up a resource owner
346  * without a transaction, we've got to clean that up now.
347  */
349  WalSndResourceCleanup(false);
350 
351  if (got_STOPPING || got_SIGUSR2)
352  proc_exit(0);
353 
354  /* Revert back to startup state */
356 }
bool ConditionVariableCancelSleep(void)
void proc_exit(int code)
Definition: ipc.c:104
void LWLockReleaseAll(void)
Definition: lwlock.c:1876
void ReplicationSlotRelease(void)
Definition: slot.c:652
void ReplicationSlotCleanup(bool synced_only)
Definition: slot.c:745
WALOpenSegment seg
Definition: xlogreader.h:272
static void pgstat_report_wait_end(void)
Definition: wait_event.h:101
static volatile sig_atomic_t got_SIGUSR2
Definition: walsender.c:197
void WalSndResourceCleanup(bool isCommit)
Definition: walsender.c:362
@ WALSNDSTATE_STARTUP
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4982
void wal_segment_close(XLogReaderState *state)
Definition: xlogutils.c:842

References ConditionVariableCancelSleep(), got_SIGUSR2, got_STOPPING, IsTransactionOrTransactionBlock(), LWLockReleaseAll(), MyReplicationSlot, pgstat_report_wait_end(), proc_exit(), replication_active, ReplicationSlotCleanup(), ReplicationSlotRelease(), XLogReaderState::seg, wal_segment_close(), WalSndResourceCleanup(), WalSndSetState(), WALSNDSTATE_STARTUP, WALOpenSegment::ws_file, and xlogreader.

Referenced by PostgresMain().

◆ WalSndInitStopping()

void WalSndInitStopping ( void  )

Definition at line 3745 of file walsender.c.

3746 {
3747  int i;
3748 
3749  for (i = 0; i < max_wal_senders; i++)
3750  {
3751  WalSnd *walsnd = &WalSndCtl->walsnds[i];
3752  pid_t pid;
3753 
3754  SpinLockAcquire(&walsnd->mutex);
3755  pid = walsnd->pid;
3756  SpinLockRelease(&walsnd->mutex);
3757 
3758  if (pid == 0)
3759  continue;
3760 
3762  }
3763 }
int i
Definition: isn.c:73
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
int SendProcSignal(pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
Definition: procsignal.c:281
@ PROCSIG_WALSND_INIT_STOPPING
Definition: procsignal.h:35
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
WalSnd walsnds[FLEXIBLE_ARRAY_MEMBER]
slock_t mutex
int max_wal_senders
Definition: walsender.c:121

References i, INVALID_PROC_NUMBER, max_wal_senders, WalSnd::mutex, WalSnd::pid, PROCSIG_WALSND_INIT_STOPPING, SendProcSignal(), SpinLockAcquire, SpinLockRelease, WalSndCtl, and WalSndCtlData::walsnds.

Referenced by ShutdownXLOG().

◆ WalSndResourceCleanup()

void WalSndResourceCleanup ( bool  isCommit)

Definition at line 362 of file walsender.c.

363 {
364  ResourceOwner resowner;
365 
366  if (CurrentResourceOwner == NULL)
367  return;
368 
369  /*
370  * Deleting CurrentResourceOwner is not allowed, so we must save a pointer
371  * in a local variable and clear it first.
372  */
373  resowner = CurrentResourceOwner;
374  CurrentResourceOwner = NULL;
375 
376  /* Now we can release resources and delete it. */
377  ResourceOwnerRelease(resowner,
378  RESOURCE_RELEASE_BEFORE_LOCKS, isCommit, true);
379  ResourceOwnerRelease(resowner,
380  RESOURCE_RELEASE_LOCKS, isCommit, true);
381  ResourceOwnerRelease(resowner,
382  RESOURCE_RELEASE_AFTER_LOCKS, isCommit, true);
383  ResourceOwnerDelete(resowner);
384 }
ResourceOwner CurrentResourceOwner
Definition: resowner.c:165
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:648
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:854
@ RESOURCE_RELEASE_LOCKS
Definition: resowner.h:55
@ RESOURCE_RELEASE_BEFORE_LOCKS
Definition: resowner.h:54
@ RESOURCE_RELEASE_AFTER_LOCKS
Definition: resowner.h:56

References CurrentResourceOwner, RESOURCE_RELEASE_AFTER_LOCKS, RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, ResourceOwnerDelete(), and ResourceOwnerRelease().

Referenced by perform_base_backup(), UploadManifest(), and WalSndErrorCleanup().

◆ WalSndRqstFileReload()

void WalSndRqstFileReload ( void  )

Definition at line 3537 of file walsender.c.

3538 {
3539  int i;
3540 
3541  for (i = 0; i < max_wal_senders; i++)
3542  {
3543  WalSnd *walsnd = &WalSndCtl->walsnds[i];
3544 
3545  SpinLockAcquire(&walsnd->mutex);
3546  if (walsnd->pid == 0)
3547  {
3548  SpinLockRelease(&walsnd->mutex);
3549  continue;
3550  }
3551  walsnd->needreload = true;
3552  SpinLockRelease(&walsnd->mutex);
3553  }
3554 }
bool needreload

References i, max_wal_senders, WalSnd::mutex, WalSnd::needreload, WalSnd::pid, SpinLockAcquire, SpinLockRelease, WalSndCtl, and WalSndCtlData::walsnds.

Referenced by KeepFileRestoredFromArchive().

◆ WalSndShmemInit()

void WalSndShmemInit ( void  )

Definition at line 3621 of file walsender.c.

3622 {
3623  bool found;
3624  int i;
3625 
3626  WalSndCtl = (WalSndCtlData *)
3627  ShmemInitStruct("Wal Sender Ctl", WalSndShmemSize(), &found);
3628 
3629  if (!found)
3630  {
3631  /* First time through, so initialize */
3633 
3634  for (i = 0; i < NUM_SYNC_REP_WAIT_MODE; i++)
3636 
3637  for (i = 0; i < max_wal_senders; i++)
3638  {
3639  WalSnd *walsnd = &WalSndCtl->walsnds[i];
3640 
3641  SpinLockInit(&walsnd->mutex);
3642  }
3643 
3647  }
3648 }
#define MemSet(start, val, len)
Definition: c.h:1023
void ConditionVariableInit(ConditionVariable *cv)
static void dlist_init(dlist_head *head)
Definition: ilist.h:314
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387
#define SpinLockInit(lock)
Definition: spin.h:57
ConditionVariable wal_replay_cv
dlist_head SyncRepQueue[NUM_SYNC_REP_WAIT_MODE]
ConditionVariable wal_flush_cv
#define NUM_SYNC_REP_WAIT_MODE
Definition: syncrep.h:27
Size WalSndShmemSize(void)
Definition: walsender.c:3609

References ConditionVariableInit(), dlist_init(), i, max_wal_senders, MemSet, WalSnd::mutex, NUM_SYNC_REP_WAIT_MODE, ShmemInitStruct(), SpinLockInit, WalSndCtlData::SyncRepQueue, WalSndCtlData::wal_confirm_rcv_cv, WalSndCtlData::wal_flush_cv, WalSndCtlData::wal_replay_cv, WalSndCtl, WalSndCtlData::walsnds, and WalSndShmemSize().

Referenced by CreateOrAttachShmemStructs().

◆ WalSndShmemSize()

Size WalSndShmemSize ( void  )

Definition at line 3609 of file walsender.c.

3610 {
3611  Size size = 0;
3612 
3613  size = offsetof(WalSndCtlData, walsnds);
3615 
3616  return size;
3617 }
size_t Size
Definition: c.h:608
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
Size mul_size(Size s1, Size s2)
Definition: shmem.c:510
static pg_noinline void Size size
Definition: slab.c:607

References add_size(), max_wal_senders, mul_size(), and size.

Referenced by CalculateShmemSize(), and WalSndShmemInit().

◆ WalSndSignals()

void WalSndSignals ( void  )

Definition at line 3590 of file walsender.c.

3591 {
3592  /* Set up signal handlers */
3594  pqsignal(SIGINT, StatementCancelHandler); /* query cancel */
3595  pqsignal(SIGTERM, die); /* request shutdown */
3596  /* SIGQUIT handler was already set up by InitPostmasterChild */
3597  InitializeTimeouts(); /* establishes SIGALRM handler */
3600  pqsignal(SIGUSR2, WalSndLastCycleHandler); /* request a last cycle and
3601  * shutdown */
3602 
3603  /* Reset some signals that are accepted by postmaster but not here */
3605 }
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:61
#define die(msg)
pqsigfunc pqsignal(int signo, pqsigfunc func)
void StatementCancelHandler(SIGNAL_ARGS)
Definition: postgres.c:3035
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:671
void InitializeTimeouts(void)
Definition: timeout.c:470
static void WalSndLastCycleHandler(SIGNAL_ARGS)
Definition: walsender.c:3582
#define SIGCHLD
Definition: win32_port.h:178
#define SIGHUP
Definition: win32_port.h:168
#define SIG_DFL
Definition: win32_port.h:163
#define SIGPIPE
Definition: win32_port.h:173
#define SIGUSR1
Definition: win32_port.h:180
#define SIGUSR2
Definition: win32_port.h:181
#define SIG_IGN
Definition: win32_port.h:165

References die, InitializeTimeouts(), pqsignal(), procsignal_sigusr1_handler(), SIG_DFL, SIG_IGN, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SIGPIPE, SIGUSR1, SIGUSR2, StatementCancelHandler(), and WalSndLastCycleHandler().

Referenced by PostgresMain().

◆ WalSndWaitStopping()

void WalSndWaitStopping ( void  )

Definition at line 3771 of file walsender.c.

3772 {
3773  for (;;)
3774  {
3775  int i;
3776  bool all_stopped = true;
3777 
3778  for (i = 0; i < max_wal_senders; i++)
3779  {
3780  WalSnd *walsnd = &WalSndCtl->walsnds[i];
3781 
3782  SpinLockAcquire(&walsnd->mutex);
3783 
3784  if (walsnd->pid == 0)
3785  {
3786  SpinLockRelease(&walsnd->mutex);
3787  continue;
3788  }
3789 
3790  if (walsnd->state != WALSNDSTATE_STOPPING)
3791  {
3792  all_stopped = false;
3793  SpinLockRelease(&walsnd->mutex);
3794  break;
3795  }
3796  SpinLockRelease(&walsnd->mutex);
3797  }
3798 
3799  /* safe to leave if confirmation is done for all WAL senders */
3800  if (all_stopped)
3801  return;
3802 
3803  pg_usleep(10000L); /* wait for 10 msec */
3804  }
3805 }
void pg_usleep(long microsec)
Definition: signal.c:53

References i, max_wal_senders, WalSnd::mutex, pg_usleep(), WalSnd::pid, SpinLockAcquire, SpinLockRelease, WalSnd::state, WalSndCtl, WalSndCtlData::walsnds, and WALSNDSTATE_STOPPING.

Referenced by ShutdownXLOG().

◆ WalSndWakeup()

void WalSndWakeup ( bool  physical,
bool  logical 
)

Definition at line 3666 of file walsender.c.

3667 {
3668  /*
3669  * Wake up all the walsenders waiting on WAL being flushed or replayed
3670  * respectively. Note that waiting walsender would have prepared to sleep
3671  * on the CV (i.e., added itself to the CV's waitlist) in WalSndWait()
3672  * before actually waiting.
3673  */
3674  if (physical)
3676 
3677  if (logical)
3679 }

References ConditionVariableBroadcast(), WalSndCtlData::wal_flush_cv, WalSndCtlData::wal_replay_cv, and WalSndCtl.

Referenced by ApplyWalRecord(), KeepFileRestoredFromArchive(), StartupXLOG(), WalSndWakeupProcessRequests(), and XLogWalRcvFlush().

◆ WalSndWakeupProcessRequests()

static void WalSndWakeupProcessRequests ( bool  physical,
bool  logical 
)
inlinestatic

Definition at line 66 of file walsender.h.

67 {
68  if (wake_wal_senders)
69  {
70  wake_wal_senders = false;
71  if (max_wal_senders > 0)
72  WalSndWakeup(physical, logical);
73  }
74 }
PGDLLIMPORT bool wake_wal_senders
Definition: walsender.c:130
void WalSndWakeup(bool physical, bool logical)
Definition: walsender.c:3666
PGDLLIMPORT int max_wal_senders
Definition: walsender.c:121

References max_wal_senders, wake_wal_senders, and WalSndWakeup().

Referenced by XLogBackgroundFlush(), and XLogFlush().

Variable Documentation

◆ am_cascading_walsender

◆ am_db_walsender

PGDLLIMPORT bool am_db_walsender
extern

Definition at line 118 of file walsender.c.

Referenced by check_db(), ClientAuthentication(), InitPostgres(), and ProcessStartupPacket().

◆ am_walsender

◆ log_replication_commands

PGDLLIMPORT bool log_replication_commands
extern

◆ max_wal_senders

◆ wake_wal_senders

PGDLLIMPORT bool wake_wal_senders
extern

Definition at line 130 of file walsender.c.

Referenced by WalSndWakeupProcessRequests().

◆ wal_sender_timeout