PostgreSQL Source Code  git master
walreceiver.h File Reference
#include <netdb.h>
#include "access/xlog.h"
#include "access/xlogdefs.h"
#include "pgtime.h"
#include "port/atomics.h"
#include "replication/logicalproto.h"
#include "replication/walsender.h"
#include "storage/condition_variable.h"
#include "storage/latch.h"
#include "storage/spin.h"
#include "utils/tuplestore.h"
Include dependency graph for walreceiver.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  WalRcvData
 
struct  WalRcvStreamOptions
 
struct  WalRcvExecResult
 
struct  WalReceiverFunctionsType
 

Macros

#define MAXCONNINFO   1024
 
#define AllowCascadeReplication()   (EnableHotStandby && max_wal_senders > 0)
 
#define walrcv_connect(conninfo, replication, logical, must_use_password, appname, err)    WalReceiverFunctions->walrcv_connect(conninfo, replication, logical, must_use_password, appname, err)
 
#define walrcv_check_conninfo(conninfo, must_use_password)    WalReceiverFunctions->walrcv_check_conninfo(conninfo, must_use_password)
 
#define walrcv_get_conninfo(conn)    WalReceiverFunctions->walrcv_get_conninfo(conn)
 
#define walrcv_get_senderinfo(conn, sender_host, sender_port)    WalReceiverFunctions->walrcv_get_senderinfo(conn, sender_host, sender_port)
 
#define walrcv_identify_system(conn, primary_tli)    WalReceiverFunctions->walrcv_identify_system(conn, primary_tli)
 
#define walrcv_get_dbname_from_conninfo(conninfo)    WalReceiverFunctions->walrcv_get_dbname_from_conninfo(conninfo)
 
#define walrcv_server_version(conn)    WalReceiverFunctions->walrcv_server_version(conn)
 
#define walrcv_readtimelinehistoryfile(conn, tli, filename, content, size)    WalReceiverFunctions->walrcv_readtimelinehistoryfile(conn, tli, filename, content, size)
 
#define walrcv_startstreaming(conn, options)    WalReceiverFunctions->walrcv_startstreaming(conn, options)
 
#define walrcv_endstreaming(conn, next_tli)    WalReceiverFunctions->walrcv_endstreaming(conn, next_tli)
 
#define walrcv_receive(conn, buffer, wait_fd)    WalReceiverFunctions->walrcv_receive(conn, buffer, wait_fd)
 
#define walrcv_send(conn, buffer, nbytes)    WalReceiverFunctions->walrcv_send(conn, buffer, nbytes)
 
#define walrcv_create_slot(conn, slotname, temporary, two_phase, failover, snapshot_action, lsn)    WalReceiverFunctions->walrcv_create_slot(conn, slotname, temporary, two_phase, failover, snapshot_action, lsn)
 
#define walrcv_alter_slot(conn, slotname, failover)    WalReceiverFunctions->walrcv_alter_slot(conn, slotname, failover)
 
#define walrcv_get_backend_pid(conn)    WalReceiverFunctions->walrcv_get_backend_pid(conn)
 
#define walrcv_exec(conn, exec, nRetTypes, retTypes)    WalReceiverFunctions->walrcv_exec(conn, exec, nRetTypes, retTypes)
 
#define walrcv_disconnect(conn)    WalReceiverFunctions->walrcv_disconnect(conn)
 

Typedefs

typedef struct WalReceiverConn WalReceiverConn
 
typedef struct WalRcvExecResult WalRcvExecResult
 
typedef WalReceiverConn *(* walrcv_connect_fn) (const char *conninfo, bool replication, bool logical, bool must_use_password, const char *appname, char **err)
 
typedef void(* walrcv_check_conninfo_fn) (const char *conninfo, bool must_use_password)
 
typedef char *(* walrcv_get_conninfo_fn) (WalReceiverConn *conn)
 
typedef void(* walrcv_get_senderinfo_fn) (WalReceiverConn *conn, char **sender_host, int *sender_port)
 
typedef char *(* walrcv_identify_system_fn) (WalReceiverConn *conn, TimeLineID *primary_tli)
 
typedef char *(* walrcv_get_dbname_from_conninfo_fn) (const char *conninfo)
 
typedef int(* walrcv_server_version_fn) (WalReceiverConn *conn)
 
typedef void(* walrcv_readtimelinehistoryfile_fn) (WalReceiverConn *conn, TimeLineID tli, char **filename, char **content, int *size)
 
typedef bool(* walrcv_startstreaming_fn) (WalReceiverConn *conn, const WalRcvStreamOptions *options)
 
typedef void(* walrcv_endstreaming_fn) (WalReceiverConn *conn, TimeLineID *next_tli)
 
typedef int(* walrcv_receive_fn) (WalReceiverConn *conn, char **buffer, pgsocket *wait_fd)
 
typedef void(* walrcv_send_fn) (WalReceiverConn *conn, const char *buffer, int nbytes)
 
typedef char *(* walrcv_create_slot_fn) (WalReceiverConn *conn, const char *slotname, bool temporary, bool two_phase, bool failover, CRSSnapshotAction snapshot_action, XLogRecPtr *lsn)
 
typedef void(* walrcv_alter_slot_fn) (WalReceiverConn *conn, const char *slotname, bool failover)
 
typedef pid_t(* walrcv_get_backend_pid_fn) (WalReceiverConn *conn)
 
typedef WalRcvExecResult *(* walrcv_exec_fn) (WalReceiverConn *conn, const char *query, const int nRetTypes, const Oid *retTypes)
 
typedef void(* walrcv_disconnect_fn) (WalReceiverConn *conn)
 
typedef struct WalReceiverFunctionsType WalReceiverFunctionsType
 

Enumerations

enum  WalRcvState {
  WALRCV_STOPPED , WALRCV_STARTING , WALRCV_STREAMING , WALRCV_WAITING ,
  WALRCV_RESTARTING , WALRCV_STOPPING
}
 
enum  WalRcvExecStatus {
  WALRCV_ERROR , WALRCV_OK_COMMAND , WALRCV_OK_TUPLES , WALRCV_OK_COPY_IN ,
  WALRCV_OK_COPY_OUT , WALRCV_OK_COPY_BOTH
}
 

Functions

static void walrcv_clear_result (WalRcvExecResult *walres)
 
void WalReceiverMain (char *startup_data, size_t startup_data_len) pg_attribute_noreturn()
 
void ProcessWalRcvInterrupts (void)
 
void WalRcvForceReply (void)
 
Size WalRcvShmemSize (void)
 
void WalRcvShmemInit (void)
 
void ShutdownWalRcv (void)
 
bool WalRcvStreaming (void)
 
bool WalRcvRunning (void)
 
void RequestXLogStreaming (TimeLineID tli, XLogRecPtr recptr, const char *conninfo, const char *slotname, bool create_temp_slot)
 
XLogRecPtr GetWalRcvFlushRecPtr (XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
 
XLogRecPtr GetWalRcvWriteRecPtr (void)
 
int GetReplicationApplyDelay (void)
 
int GetReplicationTransferLatency (void)
 

Variables

PGDLLIMPORT int wal_receiver_status_interval
 
PGDLLIMPORT int wal_receiver_timeout
 
PGDLLIMPORT bool hot_standby_feedback
 
PGDLLIMPORT WalRcvDataWalRcv
 
PGDLLIMPORT WalReceiverFunctionsTypeWalReceiverFunctions
 

Macro Definition Documentation

◆ AllowCascadeReplication

#define AllowCascadeReplication ( )    (EnableHotStandby && max_wal_senders > 0)

Definition at line 41 of file walreceiver.h.

◆ MAXCONNINFO

#define MAXCONNINFO   1024

Definition at line 38 of file walreceiver.h.

◆ walrcv_alter_slot

#define walrcv_alter_slot (   conn,
  slotname,
  failover 
)     WalReceiverFunctions->walrcv_alter_slot(conn, slotname, failover)

Definition at line 458 of file walreceiver.h.

◆ walrcv_check_conninfo

#define walrcv_check_conninfo (   conninfo,
  must_use_password 
)     WalReceiverFunctions->walrcv_check_conninfo(conninfo, must_use_password)

Definition at line 434 of file walreceiver.h.

◆ walrcv_connect

#define walrcv_connect (   conninfo,
  replication,
  logical,
  must_use_password,
  appname,
  err 
)     WalReceiverFunctions->walrcv_connect(conninfo, replication, logical, must_use_password, appname, err)

Definition at line 432 of file walreceiver.h.

◆ walrcv_create_slot

#define walrcv_create_slot (   conn,
  slotname,
  temporary,
  two_phase,
  failover,
  snapshot_action,
  lsn 
)     WalReceiverFunctions->walrcv_create_slot(conn, slotname, temporary, two_phase, failover, snapshot_action, lsn)

Definition at line 456 of file walreceiver.h.

◆ walrcv_disconnect

#define walrcv_disconnect (   conn)     WalReceiverFunctions->walrcv_disconnect(conn)

Definition at line 464 of file walreceiver.h.

◆ walrcv_endstreaming

#define walrcv_endstreaming (   conn,
  next_tli 
)     WalReceiverFunctions->walrcv_endstreaming(conn, next_tli)

Definition at line 450 of file walreceiver.h.

◆ walrcv_exec

#define walrcv_exec (   conn,
  exec,
  nRetTypes,
  retTypes 
)     WalReceiverFunctions->walrcv_exec(conn, exec, nRetTypes, retTypes)

Definition at line 462 of file walreceiver.h.

◆ walrcv_get_backend_pid

#define walrcv_get_backend_pid (   conn)     WalReceiverFunctions->walrcv_get_backend_pid(conn)

Definition at line 460 of file walreceiver.h.

◆ walrcv_get_conninfo

#define walrcv_get_conninfo (   conn)     WalReceiverFunctions->walrcv_get_conninfo(conn)

Definition at line 436 of file walreceiver.h.

◆ walrcv_get_dbname_from_conninfo

#define walrcv_get_dbname_from_conninfo (   conninfo)     WalReceiverFunctions->walrcv_get_dbname_from_conninfo(conninfo)

Definition at line 442 of file walreceiver.h.

◆ walrcv_get_senderinfo

#define walrcv_get_senderinfo (   conn,
  sender_host,
  sender_port 
)     WalReceiverFunctions->walrcv_get_senderinfo(conn, sender_host, sender_port)

Definition at line 438 of file walreceiver.h.

◆ walrcv_identify_system

#define walrcv_identify_system (   conn,
  primary_tli 
)     WalReceiverFunctions->walrcv_identify_system(conn, primary_tli)

Definition at line 440 of file walreceiver.h.

◆ walrcv_readtimelinehistoryfile

#define walrcv_readtimelinehistoryfile (   conn,
  tli,
  filename,
  content,
  size 
)     WalReceiverFunctions->walrcv_readtimelinehistoryfile(conn, tli, filename, content, size)

Definition at line 446 of file walreceiver.h.

◆ walrcv_receive

#define walrcv_receive (   conn,
  buffer,
  wait_fd 
)     WalReceiverFunctions->walrcv_receive(conn, buffer, wait_fd)

Definition at line 452 of file walreceiver.h.

◆ walrcv_send

#define walrcv_send (   conn,
  buffer,
  nbytes 
)     WalReceiverFunctions->walrcv_send(conn, buffer, nbytes)

Definition at line 454 of file walreceiver.h.

◆ walrcv_server_version

#define walrcv_server_version (   conn)     WalReceiverFunctions->walrcv_server_version(conn)

Definition at line 444 of file walreceiver.h.

◆ walrcv_startstreaming

#define walrcv_startstreaming (   conn,
  options 
)     WalReceiverFunctions->walrcv_startstreaming(conn, options)

Definition at line 448 of file walreceiver.h.

Typedef Documentation

◆ walrcv_alter_slot_fn

typedef void(* walrcv_alter_slot_fn) (WalReceiverConn *conn, const char *slotname, bool failover)

Definition at line 378 of file walreceiver.h.

◆ walrcv_check_conninfo_fn

typedef void(* walrcv_check_conninfo_fn) (const char *conninfo, bool must_use_password)

Definition at line 253 of file walreceiver.h.

◆ walrcv_connect_fn

typedef WalReceiverConn*(* walrcv_connect_fn) (const char *conninfo, bool replication, bool logical, bool must_use_password, const char *appname, char **err)

Definition at line 241 of file walreceiver.h.

◆ walrcv_create_slot_fn

typedef char*(* walrcv_create_slot_fn) (WalReceiverConn *conn, const char *slotname, bool temporary, bool two_phase, bool failover, CRSSnapshotAction snapshot_action, XLogRecPtr *lsn)

Definition at line 364 of file walreceiver.h.

◆ walrcv_disconnect_fn

typedef void(* walrcv_disconnect_fn) (WalReceiverConn *conn)

Definition at line 407 of file walreceiver.h.

◆ walrcv_endstreaming_fn

typedef void(* walrcv_endstreaming_fn) (WalReceiverConn *conn, TimeLineID *next_tli)

Definition at line 329 of file walreceiver.h.

◆ walrcv_exec_fn

typedef WalRcvExecResult*(* walrcv_exec_fn) (WalReceiverConn *conn, const char *query, const int nRetTypes, const Oid *retTypes)

Definition at line 397 of file walreceiver.h.

◆ walrcv_get_backend_pid_fn

typedef pid_t(* walrcv_get_backend_pid_fn) (WalReceiverConn *conn)

Definition at line 387 of file walreceiver.h.

◆ walrcv_get_conninfo_fn

typedef char*(* walrcv_get_conninfo_fn) (WalReceiverConn *conn)

Definition at line 262 of file walreceiver.h.

◆ walrcv_get_dbname_from_conninfo_fn

typedef char*(* walrcv_get_dbname_from_conninfo_fn) (const char *conninfo)

Definition at line 290 of file walreceiver.h.

◆ walrcv_get_senderinfo_fn

typedef void(* walrcv_get_senderinfo_fn) (WalReceiverConn *conn, char **sender_host, int *sender_port)

Definition at line 271 of file walreceiver.h.

◆ walrcv_identify_system_fn

typedef char*(* walrcv_identify_system_fn) (WalReceiverConn *conn, TimeLineID *primary_tli)

Definition at line 282 of file walreceiver.h.

◆ walrcv_readtimelinehistoryfile_fn

typedef void(* walrcv_readtimelinehistoryfile_fn) (WalReceiverConn *conn, TimeLineID tli, char **filename, char **content, int *size)

Definition at line 306 of file walreceiver.h.

◆ walrcv_receive_fn

typedef int(* walrcv_receive_fn) (WalReceiverConn *conn, char **buffer, pgsocket *wait_fd)

Definition at line 340 of file walreceiver.h.

◆ walrcv_send_fn

typedef void(* walrcv_send_fn) (WalReceiverConn *conn, const char *buffer, int nbytes)

Definition at line 350 of file walreceiver.h.

◆ walrcv_server_version_fn

typedef int(* walrcv_server_version_fn) (WalReceiverConn *conn)

Definition at line 297 of file walreceiver.h.

◆ walrcv_startstreaming_fn

typedef bool(* walrcv_startstreaming_fn) (WalReceiverConn *conn, const WalRcvStreamOptions *options)

Definition at line 320 of file walreceiver.h.

◆ WalRcvExecResult

◆ WalReceiverConn

Definition at line 164 of file walreceiver.h.

◆ WalReceiverFunctionsType

Enumeration Type Documentation

◆ WalRcvExecStatus

Enumerator
WALRCV_ERROR 
WALRCV_OK_COMMAND 
WALRCV_OK_TUPLES 
WALRCV_OK_COPY_IN 
WALRCV_OK_COPY_OUT 
WALRCV_OK_COPY_BOTH 

Definition at line 201 of file walreceiver.h.

202 {
203  WALRCV_ERROR, /* There was error when executing the query. */
204  WALRCV_OK_COMMAND, /* Query executed utility or replication
205  * command. */
206  WALRCV_OK_TUPLES, /* Query returned tuples. */
207  WALRCV_OK_COPY_IN, /* Query started COPY FROM. */
208  WALRCV_OK_COPY_OUT, /* Query started COPY TO. */
209  WALRCV_OK_COPY_BOTH, /* Query started COPY BOTH replication
210  * protocol. */
WalRcvExecStatus
Definition: walreceiver.h:202
@ WALRCV_OK_COPY_IN
Definition: walreceiver.h:207
@ WALRCV_OK_COMMAND
Definition: walreceiver.h:204
@ WALRCV_ERROR
Definition: walreceiver.h:203
@ WALRCV_OK_TUPLES
Definition: walreceiver.h:206
@ WALRCV_OK_COPY_OUT
Definition: walreceiver.h:208
@ WALRCV_OK_COPY_BOTH
Definition: walreceiver.h:209

◆ WalRcvState

Enumerator
WALRCV_STOPPED 
WALRCV_STARTING 
WALRCV_STREAMING 
WALRCV_WAITING 
WALRCV_RESTARTING 
WALRCV_STOPPING 

Definition at line 46 of file walreceiver.h.

47 {
48  WALRCV_STOPPED, /* stopped and mustn't start up again */
49  WALRCV_STARTING, /* launched, but the process hasn't
50  * initialized yet */
51  WALRCV_STREAMING, /* walreceiver is streaming */
52  WALRCV_WAITING, /* stopped streaming, waiting for orders */
53  WALRCV_RESTARTING, /* asked to restart streaming */
54  WALRCV_STOPPING, /* requested to stop, but still running */
55 } WalRcvState;
WalRcvState
Definition: walreceiver.h:47
@ WALRCV_STARTING
Definition: walreceiver.h:49
@ WALRCV_STOPPED
Definition: walreceiver.h:48
@ WALRCV_RESTARTING
Definition: walreceiver.h:53
@ WALRCV_STREAMING
Definition: walreceiver.h:51
@ WALRCV_WAITING
Definition: walreceiver.h:52
@ WALRCV_STOPPING
Definition: walreceiver.h:54

Function Documentation

◆ GetReplicationApplyDelay()

int GetReplicationApplyDelay ( void  )

Definition at line 364 of file walreceiverfuncs.c.

365 {
366  WalRcvData *walrcv = WalRcv;
367  XLogRecPtr receivePtr;
368  XLogRecPtr replayPtr;
369  TimestampTz chunkReplayStartTime;
370 
371  SpinLockAcquire(&walrcv->mutex);
372  receivePtr = walrcv->flushedUpto;
373  SpinLockRelease(&walrcv->mutex);
374 
375  replayPtr = GetXLogReplayRecPtr(NULL);
376 
377  if (receivePtr == replayPtr)
378  return 0;
379 
380  chunkReplayStartTime = GetCurrentChunkReplayStartTime();
381 
382  if (chunkReplayStartTime == 0)
383  return -1;
384 
385  return TimestampDifferenceMilliseconds(chunkReplayStartTime,
387 }
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition: timestamp.c:1766
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1654
int64 TimestampTz
Definition: timestamp.h:39
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr flushedUpto
Definition: walreceiver.h:86
slock_t mutex
Definition: walreceiver.h:146
WalRcvData * WalRcv
uint64 XLogRecPtr
Definition: xlogdefs.h:21
TimestampTz GetCurrentChunkReplayStartTime(void)
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)

References WalRcvData::flushedUpto, GetCurrentChunkReplayStartTime(), GetCurrentTimestamp(), GetXLogReplayRecPtr(), WalRcvData::mutex, SpinLockAcquire, SpinLockRelease, TimestampDifferenceMilliseconds(), and WalRcv.

Referenced by ProcessWalSndrMessage().

◆ GetReplicationTransferLatency()

int GetReplicationTransferLatency ( void  )

Definition at line 394 of file walreceiverfuncs.c.

395 {
396  WalRcvData *walrcv = WalRcv;
397  TimestampTz lastMsgSendTime;
398  TimestampTz lastMsgReceiptTime;
399 
400  SpinLockAcquire(&walrcv->mutex);
401  lastMsgSendTime = walrcv->lastMsgSendTime;
402  lastMsgReceiptTime = walrcv->lastMsgReceiptTime;
403  SpinLockRelease(&walrcv->mutex);
404 
405  return TimestampDifferenceMilliseconds(lastMsgSendTime,
406  lastMsgReceiptTime);
407 }
TimestampTz lastMsgReceiptTime
Definition: walreceiver.h:101
TimestampTz lastMsgSendTime
Definition: walreceiver.h:100

References WalRcvData::lastMsgReceiptTime, WalRcvData::lastMsgSendTime, WalRcvData::mutex, SpinLockAcquire, SpinLockRelease, TimestampDifferenceMilliseconds(), and WalRcv.

Referenced by ProcessWalSndrMessage().

◆ GetWalRcvFlushRecPtr()

XLogRecPtr GetWalRcvFlushRecPtr ( XLogRecPtr latestChunkStart,
TimeLineID receiveTLI 
)

Definition at line 331 of file walreceiverfuncs.c.

332 {
333  WalRcvData *walrcv = WalRcv;
334  XLogRecPtr recptr;
335 
336  SpinLockAcquire(&walrcv->mutex);
337  recptr = walrcv->flushedUpto;
338  if (latestChunkStart)
339  *latestChunkStart = walrcv->latestChunkStart;
340  if (receiveTLI)
341  *receiveTLI = walrcv->receivedTLI;
342  SpinLockRelease(&walrcv->mutex);
343 
344  return recptr;
345 }
TimeLineID receivedTLI
Definition: walreceiver.h:87
XLogRecPtr latestChunkStart
Definition: walreceiver.h:95
static TimeLineID receiveTLI
Definition: xlogrecovery.c:263

References WalRcvData::flushedUpto, WalRcvData::latestChunkStart, WalRcvData::mutex, WalRcvData::receivedTLI, receiveTLI, SpinLockAcquire, SpinLockRelease, and WalRcv.

Referenced by CreateRestartPoint(), GetLatestLSN(), GetStandbyFlushRecPtr(), pg_last_wal_receive_lsn(), reserve_wal_for_local_slot(), and WaitForWALToBecomeAvailable().

◆ GetWalRcvWriteRecPtr()

XLogRecPtr GetWalRcvWriteRecPtr ( void  )

Definition at line 352 of file walreceiverfuncs.c.

353 {
354  WalRcvData *walrcv = WalRcv;
355 
356  return pg_atomic_read_u64(&walrcv->writtenUpto);
357 }
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:462
pg_atomic_uint64 writtenUpto
Definition: walreceiver.h:154

References pg_atomic_read_u64(), WalRcv, and WalRcvData::writtenUpto.

◆ ProcessWalRcvInterrupts()

void ProcessWalRcvInterrupts ( void  )

Definition at line 162 of file walreceiver.c.

163 {
164  /*
165  * Although walreceiver interrupt handling doesn't use the same scheme as
166  * regular backends, call CHECK_FOR_INTERRUPTS() to make sure we receive
167  * any incoming signals on Win32, and also to make sure we process any
168  * barrier events.
169  */
171 
173  {
174  ereport(FATAL,
175  (errcode(ERRCODE_ADMIN_SHUTDOWN),
176  errmsg("terminating walreceiver process due to administrator command")));
177  }
178 }
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define FATAL
Definition: elog.h:41
#define ereport(elevel,...)
Definition: elog.h:149
volatile sig_atomic_t ShutdownRequestPending
Definition: interrupt.c:28
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122

References CHECK_FOR_INTERRUPTS, ereport, errcode(), errmsg(), FATAL, and ShutdownRequestPending.

Referenced by libpqrcv_connect(), libpqrcv_PQgetResult(), libpqrcv_processTuples(), WalRcvWaitForStartPosition(), and WalReceiverMain().

◆ RequestXLogStreaming()

void RequestXLogStreaming ( TimeLineID  tli,
XLogRecPtr  recptr,
const char *  conninfo,
const char *  slotname,
bool  create_temp_slot 
)

Definition at line 245 of file walreceiverfuncs.c.

247 {
248  WalRcvData *walrcv = WalRcv;
249  bool launch = false;
250  pg_time_t now = (pg_time_t) time(NULL);
251  Latch *latch;
252 
253  /*
254  * We always start at the beginning of the segment. That prevents a broken
255  * segment (i.e., with no records in the first half of a segment) from
256  * being created by XLOG streaming, which might cause trouble later on if
257  * the segment is e.g archived.
258  */
259  if (XLogSegmentOffset(recptr, wal_segment_size) != 0)
260  recptr -= XLogSegmentOffset(recptr, wal_segment_size);
261 
262  SpinLockAcquire(&walrcv->mutex);
263 
264  /* It better be stopped if we try to restart it */
265  Assert(walrcv->walRcvState == WALRCV_STOPPED ||
266  walrcv->walRcvState == WALRCV_WAITING);
267 
268  if (conninfo != NULL)
269  strlcpy((char *) walrcv->conninfo, conninfo, MAXCONNINFO);
270  else
271  walrcv->conninfo[0] = '\0';
272 
273  /*
274  * Use configured replication slot if present, and ignore the value of
275  * create_temp_slot as the slot name should be persistent. Otherwise, use
276  * create_temp_slot to determine whether this WAL receiver should create a
277  * temporary slot by itself and use it, or not.
278  */
279  if (slotname != NULL && slotname[0] != '\0')
280  {
281  strlcpy((char *) walrcv->slotname, slotname, NAMEDATALEN);
282  walrcv->is_temp_slot = false;
283  }
284  else
285  {
286  walrcv->slotname[0] = '\0';
287  walrcv->is_temp_slot = create_temp_slot;
288  }
289 
290  if (walrcv->walRcvState == WALRCV_STOPPED)
291  {
292  launch = true;
293  walrcv->walRcvState = WALRCV_STARTING;
294  }
295  else
296  walrcv->walRcvState = WALRCV_RESTARTING;
297  walrcv->startTime = now;
298 
299  /*
300  * If this is the first startup of walreceiver (on this timeline),
301  * initialize flushedUpto and latestChunkStart to the starting point.
302  */
303  if (walrcv->receiveStart == 0 || walrcv->receivedTLI != tli)
304  {
305  walrcv->flushedUpto = recptr;
306  walrcv->receivedTLI = tli;
307  walrcv->latestChunkStart = recptr;
308  }
309  walrcv->receiveStart = recptr;
310  walrcv->receiveStartTLI = tli;
311 
312  latch = walrcv->latch;
313 
314  SpinLockRelease(&walrcv->mutex);
315 
316  if (launch)
318  else if (latch)
319  SetLatch(latch);
320 }
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1618
void SetLatch(Latch *latch)
Definition: latch.c:632
Assert(fmt[strlen(fmt) - 1] !='\n')
#define NAMEDATALEN
int64 pg_time_t
Definition: pgtime.h:23
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:181
@ PMSIGNAL_START_WALRECEIVER
Definition: pmsignal.h:41
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
Definition: latch.h:113
TimeLineID receiveStartTLI
Definition: walreceiver.h:77
char slotname[NAMEDATALEN]
Definition: walreceiver.h:126
Latch * latch
Definition: walreceiver.h:144
XLogRecPtr receiveStart
Definition: walreceiver.h:76
bool is_temp_slot
Definition: walreceiver.h:132
pg_time_t startTime
Definition: walreceiver.h:68
WalRcvState walRcvState
Definition: walreceiver.h:66
char conninfo[MAXCONNINFO]
Definition: walreceiver.h:113
#define MAXCONNINFO
Definition: walreceiver.h:38
int wal_segment_size
Definition: xlog.c:143
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)

References Assert(), WalRcvData::conninfo, WalRcvData::flushedUpto, WalRcvData::is_temp_slot, WalRcvData::latch, WalRcvData::latestChunkStart, MAXCONNINFO, WalRcvData::mutex, NAMEDATALEN, now(), PMSIGNAL_START_WALRECEIVER, WalRcvData::receivedTLI, WalRcvData::receiveStart, WalRcvData::receiveStartTLI, SendPostmasterSignal(), SetLatch(), WalRcvData::slotname, SpinLockAcquire, SpinLockRelease, WalRcvData::startTime, strlcpy(), wal_segment_size, WalRcv, WALRCV_RESTARTING, WALRCV_STARTING, WALRCV_STOPPED, WALRCV_WAITING, WalRcvData::walRcvState, and XLogSegmentOffset.

Referenced by WaitForWALToBecomeAvailable().

◆ ShutdownWalRcv()

void ShutdownWalRcv ( void  )

Definition at line 178 of file walreceiverfuncs.c.

179 {
180  WalRcvData *walrcv = WalRcv;
181  pid_t walrcvpid = 0;
182  bool stopped = false;
183 
184  /*
185  * Request walreceiver to stop. Walreceiver will switch to WALRCV_STOPPED
186  * mode once it's finished, and will also request postmaster to not
187  * restart itself.
188  */
189  SpinLockAcquire(&walrcv->mutex);
190  switch (walrcv->walRcvState)
191  {
192  case WALRCV_STOPPED:
193  break;
194  case WALRCV_STARTING:
195  walrcv->walRcvState = WALRCV_STOPPED;
196  stopped = true;
197  break;
198 
199  case WALRCV_STREAMING:
200  case WALRCV_WAITING:
201  case WALRCV_RESTARTING:
202  walrcv->walRcvState = WALRCV_STOPPING;
203  /* fall through */
204  case WALRCV_STOPPING:
205  walrcvpid = walrcv->pid;
206  break;
207  }
208  SpinLockRelease(&walrcv->mutex);
209 
210  /* Unnecessary but consistent. */
211  if (stopped)
213 
214  /*
215  * Signal walreceiver process if it was still running.
216  */
217  if (walrcvpid != 0)
218  kill(walrcvpid, SIGTERM);
219 
220  /*
221  * Wait for walreceiver to acknowledge its death by setting state to
222  * WALRCV_STOPPED.
223  */
225  while (WalRcvRunning())
227  WAIT_EVENT_WAL_RECEIVER_EXIT);
229 }
bool ConditionVariableCancelSleep(void)
void ConditionVariableBroadcast(ConditionVariable *cv)
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
pid_t pid
Definition: walreceiver.h:65
ConditionVariable walRcvStoppedCV
Definition: walreceiver.h:67
bool WalRcvRunning(void)
#define kill(pid, sig)
Definition: win32_port.h:485

References ConditionVariableBroadcast(), ConditionVariableCancelSleep(), ConditionVariablePrepareToSleep(), ConditionVariableSleep(), kill, WalRcvData::mutex, WalRcvData::pid, SpinLockAcquire, SpinLockRelease, WalRcv, WALRCV_RESTARTING, WALRCV_STARTING, WALRCV_STOPPED, WALRCV_STOPPING, WALRCV_STREAMING, WALRCV_WAITING, WalRcvRunning(), WalRcvData::walRcvState, and WalRcvData::walRcvStoppedCV.

Referenced by XLogShutdownWalRcv().

◆ walrcv_clear_result()

static void walrcv_clear_result ( WalRcvExecResult walres)
inlinestatic

Definition at line 468 of file walreceiver.h.

469 {
470  if (!walres)
471  return;
472 
473  if (walres->err)
474  pfree(walres->err);
475 
476  if (walres->tuplestore)
477  tuplestore_end(walres->tuplestore);
478 
479  if (walres->tupledesc)
480  FreeTupleDesc(walres->tupledesc);
481 
482  pfree(walres);
483 }
void pfree(void *pointer)
Definition: mcxt.c:1508
Tuplestorestate * tuplestore
Definition: walreceiver.h:222
TupleDesc tupledesc
Definition: walreceiver.h:223
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:331
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:453

References WalRcvExecResult::err, FreeTupleDesc(), pfree(), WalRcvExecResult::tupledesc, WalRcvExecResult::tuplestore, and tuplestore_end().

Referenced by check_publications(), check_publications_origin(), copy_table(), fetch_remote_table_info(), fetch_table_list(), LogicalRepSyncTableStart(), ReplicationSlotDropAtPubNode(), synchronize_slots(), and validate_remote_info().

◆ WalRcvForceReply()

void WalRcvForceReply ( void  )

Definition at line 1358 of file walreceiver.c.

1359 {
1360  Latch *latch;
1361 
1362  WalRcv->force_reply = true;
1363  /* fetching the latch pointer might not be atomic, so use spinlock */
1365  latch = WalRcv->latch;
1367  if (latch)
1368  SetLatch(latch);
1369 }
sig_atomic_t force_reply
Definition: walreceiver.h:161

References WalRcvData::force_reply, WalRcvData::latch, WalRcvData::mutex, SetLatch(), SpinLockAcquire, SpinLockRelease, and WalRcv.

Referenced by ApplyWalRecord(), and WaitForWALToBecomeAvailable().

◆ WalRcvRunning()

bool WalRcvRunning ( void  )

Definition at line 75 of file walreceiverfuncs.c.

76 {
77  WalRcvData *walrcv = WalRcv;
79  pg_time_t startTime;
80 
81  SpinLockAcquire(&walrcv->mutex);
82 
83  state = walrcv->walRcvState;
84  startTime = walrcv->startTime;
85 
86  SpinLockRelease(&walrcv->mutex);
87 
88  /*
89  * If it has taken too long for walreceiver to start up, give up. Setting
90  * the state to STOPPED ensures that if walreceiver later does start up
91  * after all, it will see that it's not supposed to be running and die
92  * without doing anything.
93  */
94  if (state == WALRCV_STARTING)
95  {
96  pg_time_t now = (pg_time_t) time(NULL);
97 
98  if ((now - startTime) > WALRCV_STARTUP_TIMEOUT)
99  {
100  bool stopped = false;
101 
102  SpinLockAcquire(&walrcv->mutex);
103  if (walrcv->walRcvState == WALRCV_STARTING)
104  {
105  state = walrcv->walRcvState = WALRCV_STOPPED;
106  stopped = true;
107  }
108  SpinLockRelease(&walrcv->mutex);
109 
110  if (stopped)
112  }
113  }
114 
115  if (state != WALRCV_STOPPED)
116  return true;
117  else
118  return false;
119 }
Definition: regguts.h:323
#define WALRCV_STARTUP_TIMEOUT

References ConditionVariableBroadcast(), WalRcvData::mutex, now(), SpinLockAcquire, SpinLockRelease, WalRcvData::startTime, WalRcv, WALRCV_STARTING, WALRCV_STARTUP_TIMEOUT, WALRCV_STOPPED, WalRcvData::walRcvState, and WalRcvData::walRcvStoppedCV.

Referenced by ShutdownWalRcv(), and StartupRequestWalReceiverRestart().

◆ WalRcvShmemInit()

void WalRcvShmemInit ( void  )

Definition at line 54 of file walreceiverfuncs.c.

55 {
56  bool found;
57 
58  WalRcv = (WalRcvData *)
59  ShmemInitStruct("Wal Receiver Ctl", WalRcvShmemSize(), &found);
60 
61  if (!found)
62  {
63  /* First time through, so initialize */
69  WalRcv->latch = NULL;
70  }
71 }
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:448
#define MemSet(start, val, len)
Definition: c.h:1007
void ConditionVariableInit(ConditionVariable *cv)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387
#define SpinLockInit(lock)
Definition: spin.h:60
Size WalRcvShmemSize(void)

References ConditionVariableInit(), WalRcvData::latch, MemSet, WalRcvData::mutex, pg_atomic_init_u64(), ShmemInitStruct(), SpinLockInit, WalRcv, WALRCV_STOPPED, WalRcvShmemSize(), WalRcvData::walRcvState, WalRcvData::walRcvStoppedCV, and WalRcvData::writtenUpto.

Referenced by CreateOrAttachShmemStructs().

◆ WalRcvShmemSize()

Size WalRcvShmemSize ( void  )

Definition at line 43 of file walreceiverfuncs.c.

44 {
45  Size size = 0;
46 
47  size = add_size(size, sizeof(WalRcvData));
48 
49  return size;
50 }
size_t Size
Definition: c.h:592
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
static pg_noinline void Size size
Definition: slab.c:607

References add_size(), and size.

Referenced by CalculateShmemSize(), and WalRcvShmemInit().

◆ WalRcvStreaming()

bool WalRcvStreaming ( void  )

Definition at line 126 of file walreceiverfuncs.c.

127 {
128  WalRcvData *walrcv = WalRcv;
130  pg_time_t startTime;
131 
132  SpinLockAcquire(&walrcv->mutex);
133 
134  state = walrcv->walRcvState;
135  startTime = walrcv->startTime;
136 
137  SpinLockRelease(&walrcv->mutex);
138 
139  /*
140  * If it has taken too long for walreceiver to start up, give up. Setting
141  * the state to STOPPED ensures that if walreceiver later does start up
142  * after all, it will see that it's not supposed to be running and die
143  * without doing anything.
144  */
145  if (state == WALRCV_STARTING)
146  {
147  pg_time_t now = (pg_time_t) time(NULL);
148 
149  if ((now - startTime) > WALRCV_STARTUP_TIMEOUT)
150  {
151  bool stopped = false;
152 
153  SpinLockAcquire(&walrcv->mutex);
154  if (walrcv->walRcvState == WALRCV_STARTING)
155  {
156  state = walrcv->walRcvState = WALRCV_STOPPED;
157  stopped = true;
158  }
159  SpinLockRelease(&walrcv->mutex);
160 
161  if (stopped)
163  }
164  }
165 
168  return true;
169  else
170  return false;
171 }

References ConditionVariableBroadcast(), WalRcvData::mutex, now(), SpinLockAcquire, SpinLockRelease, WalRcvData::startTime, WalRcv, WALRCV_RESTARTING, WALRCV_STARTING, WALRCV_STARTUP_TIMEOUT, WALRCV_STOPPED, WALRCV_STREAMING, WalRcvData::walRcvState, and WalRcvData::walRcvStoppedCV.

Referenced by FinishWalRecovery(), and WaitForWALToBecomeAvailable().

◆ WalReceiverMain()

void WalReceiverMain ( char *  startup_data,
size_t  startup_data_len 
)

Definition at line 183 of file walreceiver.c.

184 {
185  char conninfo[MAXCONNINFO];
186  char *tmp_conninfo;
187  char slotname[NAMEDATALEN];
188  bool is_temp_slot;
189  XLogRecPtr startpoint;
190  TimeLineID startpointTLI;
191  TimeLineID primaryTLI;
192  bool first_stream;
193  WalRcvData *walrcv;
195  char *err;
196  char *sender_host = NULL;
197  int sender_port = 0;
198 
199  Assert(startup_data_len == 0);
200 
203 
204  /*
205  * WalRcv should be set up already (if we are a backend, we inherit this
206  * by fork() or EXEC_BACKEND mechanism from the postmaster).
207  */
208  walrcv = WalRcv;
209  Assert(walrcv != NULL);
210 
211  /*
212  * Mark walreceiver as running in shared memory.
213  *
214  * Do this as early as possible, so that if we fail later on, we'll set
215  * state to STOPPED. If we die before this, the startup process will keep
216  * waiting for us to start up, until it times out.
217  */
218  SpinLockAcquire(&walrcv->mutex);
219  Assert(walrcv->pid == 0);
220  switch (walrcv->walRcvState)
221  {
222  case WALRCV_STOPPING:
223  /* If we've already been requested to stop, don't start up. */
224  walrcv->walRcvState = WALRCV_STOPPED;
225  /* fall through */
226 
227  case WALRCV_STOPPED:
228  SpinLockRelease(&walrcv->mutex);
230  proc_exit(1);
231  break;
232 
233  case WALRCV_STARTING:
234  /* The usual case */
235  break;
236 
237  case WALRCV_WAITING:
238  case WALRCV_STREAMING:
239  case WALRCV_RESTARTING:
240  default:
241  /* Shouldn't happen */
242  SpinLockRelease(&walrcv->mutex);
243  elog(PANIC, "walreceiver still running according to shared memory state");
244  }
245  /* Advertise our PID so that the startup process can kill us */
246  walrcv->pid = MyProcPid;
247  walrcv->walRcvState = WALRCV_STREAMING;
248 
249  /* Fetch information required to start streaming */
250  walrcv->ready_to_display = false;
251  strlcpy(conninfo, (char *) walrcv->conninfo, MAXCONNINFO);
252  strlcpy(slotname, (char *) walrcv->slotname, NAMEDATALEN);
253  is_temp_slot = walrcv->is_temp_slot;
254  startpoint = walrcv->receiveStart;
255  startpointTLI = walrcv->receiveStartTLI;
256 
257  /*
258  * At most one of is_temp_slot and slotname can be set; otherwise,
259  * RequestXLogStreaming messed up.
260  */
261  Assert(!is_temp_slot || (slotname[0] == '\0'));
262 
263  /* Initialise to a sanish value */
265  walrcv->lastMsgSendTime =
266  walrcv->lastMsgReceiptTime = walrcv->latestWalEndTime = now;
267 
268  /* Report the latch to use to awaken this process */
269  walrcv->latch = &MyProc->procLatch;
270 
271  SpinLockRelease(&walrcv->mutex);
272 
274 
275  /* Arrange to clean up at walreceiver exit */
276  on_shmem_exit(WalRcvDie, PointerGetDatum(&startpointTLI));
277 
278  /* Properly accept or ignore signals the postmaster might send us */
279  pqsignal(SIGHUP, SignalHandlerForConfigReload); /* set flag to read config
280  * file */
281  pqsignal(SIGINT, SIG_IGN);
282  pqsignal(SIGTERM, SignalHandlerForShutdownRequest); /* request shutdown */
283  /* SIGQUIT handler was already set up by InitPostmasterChild */
288 
289  /* Reset some signals that are accepted by postmaster but not here */
291 
292  /* Load the libpq-specific functions */
293  load_file("libpqwalreceiver", false);
294  if (WalReceiverFunctions == NULL)
295  elog(ERROR, "libpqwalreceiver didn't initialize correctly");
296 
297  /* Unblock signals (they were blocked when the postmaster forked us) */
298  sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
299 
300  /* Establish the connection to the primary for XLOG streaming */
301  wrconn = walrcv_connect(conninfo, true, false, false,
302  cluster_name[0] ? cluster_name : "walreceiver",
303  &err);
304  if (!wrconn)
305  ereport(ERROR,
306  (errcode(ERRCODE_CONNECTION_FAILURE),
307  errmsg("could not connect to the primary server: %s", err)));
308 
309  /*
310  * Save user-visible connection string. This clobbers the original
311  * conninfo, for security. Also save host and port of the sender server
312  * this walreceiver is connected to.
313  */
314  tmp_conninfo = walrcv_get_conninfo(wrconn);
315  walrcv_get_senderinfo(wrconn, &sender_host, &sender_port);
316  SpinLockAcquire(&walrcv->mutex);
317  memset(walrcv->conninfo, 0, MAXCONNINFO);
318  if (tmp_conninfo)
319  strlcpy((char *) walrcv->conninfo, tmp_conninfo, MAXCONNINFO);
320 
321  memset(walrcv->sender_host, 0, NI_MAXHOST);
322  if (sender_host)
323  strlcpy((char *) walrcv->sender_host, sender_host, NI_MAXHOST);
324 
325  walrcv->sender_port = sender_port;
326  walrcv->ready_to_display = true;
327  SpinLockRelease(&walrcv->mutex);
328 
329  if (tmp_conninfo)
330  pfree(tmp_conninfo);
331 
332  if (sender_host)
333  pfree(sender_host);
334 
335  first_stream = true;
336  for (;;)
337  {
338  char *primary_sysid;
339  char standby_sysid[32];
341 
342  /*
343  * Check that we're connected to a valid server using the
344  * IDENTIFY_SYSTEM replication command.
345  */
346  primary_sysid = walrcv_identify_system(wrconn, &primaryTLI);
347 
348  snprintf(standby_sysid, sizeof(standby_sysid), UINT64_FORMAT,
350  if (strcmp(primary_sysid, standby_sysid) != 0)
351  {
352  ereport(ERROR,
353  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
354  errmsg("database system identifier differs between the primary and standby"),
355  errdetail("The primary's identifier is %s, the standby's identifier is %s.",
356  primary_sysid, standby_sysid)));
357  }
358 
359  /*
360  * Confirm that the current timeline of the primary is the same or
361  * ahead of ours.
362  */
363  if (primaryTLI < startpointTLI)
364  ereport(ERROR,
365  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
366  errmsg("highest timeline %u of the primary is behind recovery timeline %u",
367  primaryTLI, startpointTLI)));
368 
369  /*
370  * Get any missing history files. We do this always, even when we're
371  * not interested in that timeline, so that if we're promoted to
372  * become the primary later on, we don't select the same timeline that
373  * was already used in the current primary. This isn't bullet-proof -
374  * you'll need some external software to manage your cluster if you
375  * need to ensure that a unique timeline id is chosen in every case,
376  * but let's avoid the confusion of timeline id collisions where we
377  * can.
378  */
379  WalRcvFetchTimeLineHistoryFiles(startpointTLI, primaryTLI);
380 
381  /*
382  * Create temporary replication slot if requested, and update slot
383  * name in shared memory. (Note the slot name cannot already be set
384  * in this case.)
385  */
386  if (is_temp_slot)
387  {
388  snprintf(slotname, sizeof(slotname),
389  "pg_walreceiver_%lld",
390  (long long int) walrcv_get_backend_pid(wrconn));
391 
392  walrcv_create_slot(wrconn, slotname, true, false, false, 0, NULL);
393 
394  SpinLockAcquire(&walrcv->mutex);
395  strlcpy(walrcv->slotname, slotname, NAMEDATALEN);
396  SpinLockRelease(&walrcv->mutex);
397  }
398 
399  /*
400  * Start streaming.
401  *
402  * We'll try to start at the requested starting point and timeline,
403  * even if it's different from the server's latest timeline. In case
404  * we've already reached the end of the old timeline, the server will
405  * finish the streaming immediately, and we will go back to await
406  * orders from the startup process. If recovery_target_timeline is
407  * 'latest', the startup process will scan pg_wal and find the new
408  * history file, bump recovery target timeline, and ask us to restart
409  * on the new timeline.
410  */
411  options.logical = false;
412  options.startpoint = startpoint;
413  options.slotname = slotname[0] != '\0' ? slotname : NULL;
414  options.proto.physical.startpointTLI = startpointTLI;
416  {
417  if (first_stream)
418  ereport(LOG,
419  (errmsg("started streaming WAL from primary at %X/%X on timeline %u",
420  LSN_FORMAT_ARGS(startpoint), startpointTLI)));
421  else
422  ereport(LOG,
423  (errmsg("restarted WAL streaming at %X/%X on timeline %u",
424  LSN_FORMAT_ARGS(startpoint), startpointTLI)));
425  first_stream = false;
426 
427  /* Initialize LogstreamResult and buffers for processing messages */
430 
431  /* Initialize nap wakeup times. */
433  for (int i = 0; i < NUM_WALRCV_WAKEUPS; ++i)
435 
436  /* Send initial reply/feedback messages. */
437  XLogWalRcvSendReply(true, false);
439 
440  /* Loop until end-of-streaming or error */
441  for (;;)
442  {
443  char *buf;
444  int len;
445  bool endofwal = false;
446  pgsocket wait_fd = PGINVALID_SOCKET;
447  int rc;
448  TimestampTz nextWakeup;
449  long nap;
450 
451  /*
452  * Exit walreceiver if we're not in recovery. This should not
453  * happen, but cross-check the status here.
454  */
455  if (!RecoveryInProgress())
456  ereport(FATAL,
457  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
458  errmsg("cannot continue WAL streaming, recovery has already ended")));
459 
460  /* Process any requests or signals received recently */
462 
464  {
465  ConfigReloadPending = false;
467  /* recompute wakeup times */
469  for (int i = 0; i < NUM_WALRCV_WAKEUPS; ++i)
472  }
473 
474  /* See if we can read data immediately */
475  len = walrcv_receive(wrconn, &buf, &wait_fd);
476  if (len != 0)
477  {
478  /*
479  * Process the received data, and any subsequent data we
480  * can read without blocking.
481  */
482  for (;;)
483  {
484  if (len > 0)
485  {
486  /*
487  * Something was received from primary, so adjust
488  * the ping and terminate wakeup times.
489  */
492  now);
494  XLogWalRcvProcessMsg(buf[0], &buf[1], len - 1,
495  startpointTLI);
496  }
497  else if (len == 0)
498  break;
499  else if (len < 0)
500  {
501  ereport(LOG,
502  (errmsg("replication terminated by primary server"),
503  errdetail("End of WAL reached on timeline %u at %X/%X.",
504  startpointTLI,
506  endofwal = true;
507  break;
508  }
509  len = walrcv_receive(wrconn, &buf, &wait_fd);
510  }
511 
512  /* Let the primary know that we received some data. */
513  XLogWalRcvSendReply(false, false);
514 
515  /*
516  * If we've written some records, flush them to disk and
517  * let the startup process and primary server know about
518  * them.
519  */
520  XLogWalRcvFlush(false, startpointTLI);
521  }
522 
523  /* Check if we need to exit the streaming loop. */
524  if (endofwal)
525  break;
526 
527  /* Find the soonest wakeup time, to limit our nap. */
528  nextWakeup = TIMESTAMP_INFINITY;
529  for (int i = 0; i < NUM_WALRCV_WAKEUPS; ++i)
530  nextWakeup = Min(wakeup[i], nextWakeup);
531 
532  /* Calculate the nap time, clamping as necessary. */
534  nap = TimestampDifferenceMilliseconds(now, nextWakeup);
535 
536  /*
537  * Ideally we would reuse a WaitEventSet object repeatedly
538  * here to avoid the overheads of WaitLatchOrSocket on epoll
539  * systems, but we can't be sure that libpq (or any other
540  * walreceiver implementation) has the same socket (even if
541  * the fd is the same number, it may have been closed and
542  * reopened since the last time). In future, if there is a
543  * function for removing sockets from WaitEventSet, then we
544  * could add and remove just the socket each time, potentially
545  * avoiding some system calls.
546  */
547  Assert(wait_fd != PGINVALID_SOCKET);
551  wait_fd,
552  nap,
553  WAIT_EVENT_WAL_RECEIVER_MAIN);
554  if (rc & WL_LATCH_SET)
555  {
558 
559  if (walrcv->force_reply)
560  {
561  /*
562  * The recovery process has asked us to send apply
563  * feedback now. Make sure the flag is really set to
564  * false in shared memory before sending the reply, so
565  * we don't miss a new request for a reply.
566  */
567  walrcv->force_reply = false;
569  XLogWalRcvSendReply(true, false);
570  }
571  }
572  if (rc & WL_TIMEOUT)
573  {
574  /*
575  * We didn't receive anything new. If we haven't heard
576  * anything from the server for more than
577  * wal_receiver_timeout / 2, ping the server. Also, if
578  * it's been longer than wal_receiver_status_interval
579  * since the last update we sent, send a status update to
580  * the primary anyway, to report any progress in applying
581  * WAL.
582  */
583  bool requestReply = false;
584 
585  /*
586  * Check if time since last receive from primary has
587  * reached the configured limit.
588  */
591  ereport(ERROR,
592  (errcode(ERRCODE_CONNECTION_FAILURE),
593  errmsg("terminating walreceiver due to timeout")));
594 
595  /*
596  * If we didn't receive anything new for half of receiver
597  * replication timeout, then ping the server.
598  */
599  if (now >= wakeup[WALRCV_WAKEUP_PING])
600  {
601  requestReply = true;
603  }
604 
605  XLogWalRcvSendReply(requestReply, requestReply);
607  }
608  }
609 
610  /*
611  * The backend finished streaming. Exit streaming COPY-mode from
612  * our side, too.
613  */
614  walrcv_endstreaming(wrconn, &primaryTLI);
615 
616  /*
617  * If the server had switched to a new timeline that we didn't
618  * know about when we began streaming, fetch its timeline history
619  * file now.
620  */
621  WalRcvFetchTimeLineHistoryFiles(startpointTLI, primaryTLI);
622  }
623  else
624  ereport(LOG,
625  (errmsg("primary server contains no more WAL on requested timeline %u",
626  startpointTLI)));
627 
628  /*
629  * End of WAL reached on the requested timeline. Close the last
630  * segment, and await for new orders from the startup process.
631  */
632  if (recvFile >= 0)
633  {
634  char xlogfname[MAXFNAMELEN];
635 
636  XLogWalRcvFlush(false, startpointTLI);
638  if (close(recvFile) != 0)
639  ereport(PANIC,
641  errmsg("could not close WAL segment %s: %m",
642  xlogfname)));
643 
644  /*
645  * Create .done file forcibly to prevent the streamed segment from
646  * being archived later.
647  */
649  XLogArchiveForceDone(xlogfname);
650  else
651  XLogArchiveNotify(xlogfname);
652  }
653  recvFile = -1;
654 
655  elog(DEBUG1, "walreceiver ended streaming and awaits new instructions");
656  WalRcvWaitForStartPosition(&startpoint, &startpointTLI);
657  }
658  /* not reached */
659 }
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:480
#define pg_memory_barrier()
Definition: atomics.h:138
void AuxiliaryProcessMainCommon(void)
Definition: auxprocess.c:44
sigset_t UnBlockSig
Definition: pqsignal.c:22
#define Min(x, y)
Definition: c.h:991
#define UINT64_FORMAT
Definition: c.h:536
#define TIMESTAMP_INFINITY
Definition: timestamp.h:151
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:144
int errcode_for_file_access(void)
Definition: elog.c:882
int errdetail(const char *fmt,...)
Definition: elog.c:1205
#define LOG
Definition: elog.h:31
#define PANIC
Definition: elog.h:42
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
void err(int eval, const char *fmt,...)
Definition: err.c:43
int MyProcPid
Definition: globals.c:45
struct Latch * MyLatch
Definition: globals.c:60
@ PGC_SIGHUP
Definition: guc.h:71
void ProcessConfigFile(GucContext context)
char * cluster_name
Definition: guc_tables.c:538
#define close(a)
Definition: win32.h:12
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:105
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:27
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:61
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:365
void proc_exit(int code)
Definition: ipc.c:104
int i
Definition: isn.c:73
int WaitLatchOrSocket(Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
Definition: latch.c:565
void ResetLatch(Latch *latch)
Definition: latch.c:724
#define WL_SOCKET_READABLE
Definition: latch.h:128
#define WL_TIMEOUT
Definition: latch.h:130
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:132
#define WL_LATCH_SET
Definition: latch.h:127
@ B_WAL_RECEIVER
Definition: miscadmin.h:356
BackendType MyBackendType
Definition: miscinit.c:63
const void size_t len
static char ** options
static char * buf
Definition: pg_test_fsync.c:73
pqsigfunc pqsignal(int signo, pqsigfunc func)
int pgsocket
Definition: port.h:29
#define snprintf
Definition: port.h:238
#define PGINVALID_SOCKET
Definition: port.h:31
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:635
PGPROC * MyProc
Definition: proc.c:66
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
Latch procLatch
Definition: proc.h:165
char sender_host[NI_MAXHOST]
Definition: walreceiver.h:119
TimestampTz latestWalEndTime
Definition: walreceiver.h:107
bool ready_to_display
Definition: walreceiver.h:135
int sender_port
Definition: walreceiver.h:120
void ProcessWalRcvInterrupts(void)
Definition: walreceiver.c:162
#define NUM_WALRCV_WAKEUPS
Definition: walreceiver.c:123
static WalReceiverConn * wrconn
Definition: walreceiver.c:92
static TimestampTz wakeup[NUM_WALRCV_WAKEUPS]
Definition: walreceiver.c:129
static StringInfoData reply_message
Definition: walreceiver.c:131
static int recvFile
Definition: walreceiver.c:100
static void WalRcvFetchTimeLineHistoryFiles(TimeLineID first, TimeLineID last)
Definition: walreceiver.c:745
static void XLogWalRcvFlush(bool dying, TimeLineID tli)
Definition: walreceiver.c:993
static struct @18 LogstreamResult
static TimeLineID recvFileTLI
Definition: walreceiver.c:101
WalReceiverFunctionsType * WalReceiverFunctions
Definition: walreceiver.c:93
static XLogSegNo recvSegNo
Definition: walreceiver.c:102
static void XLogWalRcvSendHSFeedback(bool immed)
Definition: walreceiver.c:1169
@ WALRCV_WAKEUP_TERMINATE
Definition: walreceiver.c:119
@ WALRCV_WAKEUP_PING
Definition: walreceiver.c:120
static void WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI)
Definition: walreceiver.c:665
static void XLogWalRcvProcessMsg(unsigned char type, char *buf, Size len, TimeLineID tli)
Definition: walreceiver.c:839
static void WalRcvComputeNextWakeup(WalRcvWakeupReason reason, TimestampTz now)
Definition: walreceiver.c:1317
static void WalRcvDie(int code, Datum arg)
Definition: walreceiver.c:801
static void XLogWalRcvSendReply(bool force, bool requestReply)
Definition: walreceiver.c:1100
#define walrcv_startstreaming(conn, options)
Definition: walreceiver.h:448
#define walrcv_connect(conninfo, replication, logical, must_use_password, appname, err)
Definition: walreceiver.h:432
#define walrcv_get_senderinfo(conn, sender_host, sender_port)
Definition: walreceiver.h:438
#define walrcv_create_slot(conn, slotname, temporary, two_phase, failover, snapshot_action, lsn)
Definition: walreceiver.h:456
#define walrcv_get_conninfo(conn)
Definition: walreceiver.h:436
#define walrcv_endstreaming(conn, next_tli)
Definition: walreceiver.h:450
#define walrcv_identify_system(conn, primary_tli)
Definition: walreceiver.h:440
#define walrcv_get_backend_pid(conn)
Definition: walreceiver.h:460
#define walrcv_receive(conn, buffer, wait_fd)
Definition: walreceiver.h:452
#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 SIGALRM
Definition: win32_port.h:174
#define SIGUSR2
Definition: win32_port.h:181
#define SIG_IGN
Definition: win32_port.h:165
uint64 GetSystemIdentifier(void)
Definition: xlog.c:4458
bool RecoveryInProgress(void)
Definition: xlog.c:6201
int XLogArchiveMode
Definition: xlog.c:119
@ ARCHIVE_MODE_ALWAYS
Definition: xlog.h:65
#define MAXFNAMELEN
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
void XLogArchiveForceDone(const char *xlog)
Definition: xlogarchive.c:510
void XLogArchiveNotify(const char *xlog)
Definition: xlogarchive.c:444
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
uint32 TimeLineID
Definition: xlogdefs.h:59

References ARCHIVE_MODE_ALWAYS, Assert(), AuxiliaryProcessMainCommon(), B_WAL_RECEIVER, buf, close, cluster_name, ConditionVariableBroadcast(), ConfigReloadPending, WalRcvData::conninfo, DEBUG1, elog, ereport, err(), errcode(), errcode_for_file_access(), errdetail(), errmsg(), ERROR, FATAL, WalRcvData::force_reply, GetCurrentTimestamp(), GetSystemIdentifier(), GetXLogReplayRecPtr(), i, initStringInfo(), WalRcvData::is_temp_slot, WalRcvData::lastMsgReceiptTime, WalRcvData::lastMsgSendTime, WalRcvData::latch, WalRcvData::latestWalEndTime, len, load_file(), LOG, LogstreamResult, LSN_FORMAT_ARGS, MAXCONNINFO, MAXFNAMELEN, Min, WalRcvData::mutex, MyBackendType, MyLatch, MyProc, MyProcPid, NAMEDATALEN, now(), NUM_WALRCV_WAKEUPS, on_shmem_exit(), options, PANIC, pfree(), pg_atomic_write_u64(), pg_memory_barrier, PGC_SIGHUP, PGINVALID_SOCKET, WalRcvData::pid, PointerGetDatum(), pqsignal(), proc_exit(), ProcessConfigFile(), ProcessWalRcvInterrupts(), PGPROC::procLatch, procsignal_sigusr1_handler(), WalRcvData::ready_to_display, WalRcvData::receiveStart, WalRcvData::receiveStartTLI, RecoveryInProgress(), recvFile, recvFileTLI, recvSegNo, reply_message, ResetLatch(), WalRcvData::sender_host, WalRcvData::sender_port, SIG_DFL, SIG_IGN, SIGALRM, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), SIGPIPE, SIGUSR1, SIGUSR2, WalRcvData::slotname, snprintf, SpinLockAcquire, SpinLockRelease, strlcpy(), TIMESTAMP_INFINITY, TimestampDifferenceMilliseconds(), UINT64_FORMAT, UnBlockSig, WaitLatchOrSocket(), wakeup, wal_segment_size, WalRcv, walrcv_connect, walrcv_create_slot, walrcv_endstreaming, walrcv_get_backend_pid, walrcv_get_conninfo, walrcv_get_senderinfo, walrcv_identify_system, walrcv_receive, WALRCV_RESTARTING, WALRCV_STARTING, walrcv_startstreaming, WALRCV_STOPPED, WALRCV_STOPPING, WALRCV_STREAMING, WALRCV_WAITING, WALRCV_WAKEUP_PING, WALRCV_WAKEUP_TERMINATE, WalRcvComputeNextWakeup(), WalRcvDie(), WalRcvFetchTimeLineHistoryFiles(), WalRcvData::walRcvState, WalRcvData::walRcvStoppedCV, WalRcvWaitForStartPosition(), WalReceiverFunctions, WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_SOCKET_READABLE, WL_TIMEOUT, wrconn, WalRcvData::writtenUpto, XLogArchiveForceDone(), XLogArchiveMode, XLogArchiveNotify(), XLogFileName(), XLogWalRcvFlush(), XLogWalRcvProcessMsg(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

Variable Documentation

◆ hot_standby_feedback

PGDLLIMPORT bool hot_standby_feedback
extern

◆ wal_receiver_status_interval

PGDLLIMPORT int wal_receiver_status_interval
extern

◆ wal_receiver_timeout

PGDLLIMPORT int wal_receiver_timeout
extern

◆ WalRcv

◆ WalReceiverFunctions

PGDLLIMPORT WalReceiverFunctionsType* WalReceiverFunctions
extern

Definition at line 93 of file walreceiver.c.

Referenced by _PG_init(), and WalReceiverMain().