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/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, two_phase)    WalReceiverFunctions->walrcv_alter_slot(conn, slotname, failover, two_phase)
 
#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, const bool *failover, const bool *two_phase)
 
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)
 
pg_noreturn void WalReceiverMain (const void *startup_data, size_t startup_data_len)
 
void WalRcvForceReply (void)
 
Size WalRcvShmemSize (void)
 
void WalRcvShmemInit (void)
 
void ShutdownWalRcv (void)
 
bool WalRcvStreaming (void)
 
bool WalRcvRunning (void)
 
WalRcvState WalRcvGetState (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 40 of file walreceiver.h.

◆ MAXCONNINFO

#define MAXCONNINFO   1024

Definition at line 37 of file walreceiver.h.

◆ walrcv_alter_slot

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

Definition at line 461 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 437 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 435 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 459 of file walreceiver.h.

◆ walrcv_disconnect

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

Definition at line 467 of file walreceiver.h.

◆ walrcv_endstreaming

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

Definition at line 453 of file walreceiver.h.

◆ walrcv_exec

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

Definition at line 465 of file walreceiver.h.

◆ walrcv_get_backend_pid

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

Definition at line 463 of file walreceiver.h.

◆ walrcv_get_conninfo

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

Definition at line 439 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 445 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 441 of file walreceiver.h.

◆ walrcv_identify_system

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

Definition at line 443 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 449 of file walreceiver.h.

◆ walrcv_receive

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

Definition at line 455 of file walreceiver.h.

◆ walrcv_send

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

Definition at line 457 of file walreceiver.h.

◆ walrcv_server_version

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

Definition at line 447 of file walreceiver.h.

◆ walrcv_startstreaming

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

Definition at line 451 of file walreceiver.h.

Typedef Documentation

◆ walrcv_alter_slot_fn

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

Definition at line 379 of file walreceiver.h.

◆ walrcv_check_conninfo_fn

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

Definition at line 254 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 242 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 365 of file walreceiver.h.

◆ walrcv_disconnect_fn

typedef void(* walrcv_disconnect_fn) (WalReceiverConn *conn)

Definition at line 410 of file walreceiver.h.

◆ walrcv_endstreaming_fn

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

Definition at line 330 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 400 of file walreceiver.h.

◆ walrcv_get_backend_pid_fn

typedef pid_t(* walrcv_get_backend_pid_fn) (WalReceiverConn *conn)

Definition at line 390 of file walreceiver.h.

◆ walrcv_get_conninfo_fn

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

Definition at line 263 of file walreceiver.h.

◆ walrcv_get_dbname_from_conninfo_fn

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

Definition at line 291 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 272 of file walreceiver.h.

◆ walrcv_identify_system_fn

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

Definition at line 283 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 307 of file walreceiver.h.

◆ walrcv_receive_fn

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

Definition at line 341 of file walreceiver.h.

◆ walrcv_send_fn

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

Definition at line 351 of file walreceiver.h.

◆ walrcv_server_version_fn

typedef int(* walrcv_server_version_fn) (WalReceiverConn *conn)

Definition at line 298 of file walreceiver.h.

◆ walrcv_startstreaming_fn

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

Definition at line 321 of file walreceiver.h.

◆ WalRcvExecResult

◆ WalReceiverConn

Definition at line 195 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 202 of file walreceiver.h.

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

◆ WalRcvState

Enumerator
WALRCV_STOPPED 
WALRCV_STARTING 
WALRCV_STREAMING 
WALRCV_WAITING 
WALRCV_RESTARTING 
WALRCV_STOPPING 

Definition at line 45 of file walreceiver.h.

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

Function Documentation

◆ GetReplicationApplyDelay()

int GetReplicationApplyDelay ( void  )

Definition at line 379 of file walreceiverfuncs.c.

380{
381 WalRcvData *walrcv = WalRcv;
382 XLogRecPtr receivePtr;
383 XLogRecPtr replayPtr;
384 TimestampTz chunkReplayStartTime;
385
386 SpinLockAcquire(&walrcv->mutex);
387 receivePtr = walrcv->flushedUpto;
388 SpinLockRelease(&walrcv->mutex);
389
390 replayPtr = GetXLogReplayRecPtr(NULL);
391
392 if (receivePtr == replayPtr)
393 return 0;
394
395 chunkReplayStartTime = GetCurrentChunkReplayStartTime();
396
397 if (chunkReplayStartTime == 0)
398 return -1;
399
400 return TimestampDifferenceMilliseconds(chunkReplayStartTime,
402}
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition: timestamp.c:1757
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
int64 TimestampTz
Definition: timestamp.h:39
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
XLogRecPtr flushedUpto
Definition: walreceiver.h:96
slock_t mutex
Definition: walreceiver.h:147
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 409 of file walreceiverfuncs.c.

410{
411 WalRcvData *walrcv = WalRcv;
412 TimestampTz lastMsgSendTime;
413 TimestampTz lastMsgReceiptTime;
414
415 SpinLockAcquire(&walrcv->mutex);
416 lastMsgSendTime = walrcv->lastMsgSendTime;
417 lastMsgReceiptTime = walrcv->lastMsgReceiptTime;
418 SpinLockRelease(&walrcv->mutex);
419
420 return TimestampDifferenceMilliseconds(lastMsgSendTime,
421 lastMsgReceiptTime);
422}
TimestampTz lastMsgReceiptTime
Definition: walreceiver.h:111
TimestampTz lastMsgSendTime
Definition: walreceiver.h:110

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 346 of file walreceiverfuncs.c.

347{
348 WalRcvData *walrcv = WalRcv;
349 XLogRecPtr recptr;
350
351 SpinLockAcquire(&walrcv->mutex);
352 recptr = walrcv->flushedUpto;
353 if (latestChunkStart)
354 *latestChunkStart = walrcv->latestChunkStart;
355 if (receiveTLI)
356 *receiveTLI = walrcv->receivedTLI;
357 SpinLockRelease(&walrcv->mutex);
358
359 return recptr;
360}
TimeLineID receivedTLI
Definition: walreceiver.h:97
XLogRecPtr latestChunkStart
Definition: walreceiver.h:105
static TimeLineID receiveTLI
Definition: xlogrecovery.c:266

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

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

◆ GetWalRcvWriteRecPtr()

XLogRecPtr GetWalRcvWriteRecPtr ( void  )

Definition at line 367 of file walreceiverfuncs.c.

368{
369 WalRcvData *walrcv = WalRcv;
370
371 return pg_atomic_read_u64(&walrcv->writtenUpto);
372}
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:467
pg_atomic_uint64 writtenUpto
Definition: walreceiver.h:155

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

Referenced by GetCurrentLSNForWaitType().

◆ RequestXLogStreaming()

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

Definition at line 260 of file walreceiverfuncs.c.

262{
263 WalRcvData *walrcv = WalRcv;
264 bool launch = false;
265 pg_time_t now = (pg_time_t) time(NULL);
266 ProcNumber walrcv_proc;
267
268 /*
269 * We always start at the beginning of the segment. That prevents a broken
270 * segment (i.e., with no records in the first half of a segment) from
271 * being created by XLOG streaming, which might cause trouble later on if
272 * the segment is e.g archived.
273 */
274 if (XLogSegmentOffset(recptr, wal_segment_size) != 0)
275 recptr -= XLogSegmentOffset(recptr, wal_segment_size);
276
277 SpinLockAcquire(&walrcv->mutex);
278
279 /* It better be stopped if we try to restart it */
280 Assert(walrcv->walRcvState == WALRCV_STOPPED ||
281 walrcv->walRcvState == WALRCV_WAITING);
282
283 if (conninfo != NULL)
284 strlcpy(walrcv->conninfo, conninfo, MAXCONNINFO);
285 else
286 walrcv->conninfo[0] = '\0';
287
288 /*
289 * Use configured replication slot if present, and ignore the value of
290 * create_temp_slot as the slot name should be persistent. Otherwise, use
291 * create_temp_slot to determine whether this WAL receiver should create a
292 * temporary slot by itself and use it, or not.
293 */
294 if (slotname != NULL && slotname[0] != '\0')
295 {
296 strlcpy(walrcv->slotname, slotname, NAMEDATALEN);
297 walrcv->is_temp_slot = false;
298 }
299 else
300 {
301 walrcv->slotname[0] = '\0';
302 walrcv->is_temp_slot = create_temp_slot;
303 }
304
305 if (walrcv->walRcvState == WALRCV_STOPPED)
306 {
307 launch = true;
309 }
310 else
312 walrcv->startTime = now;
313
314 /*
315 * If this is the first startup of walreceiver (on this timeline),
316 * initialize flushedUpto and latestChunkStart to the starting point.
317 */
318 if (!XLogRecPtrIsValid(walrcv->receiveStart) || walrcv->receivedTLI != tli)
319 {
320 walrcv->flushedUpto = recptr;
321 walrcv->receivedTLI = tli;
322 walrcv->latestChunkStart = recptr;
323 }
324 walrcv->receiveStart = recptr;
325 walrcv->receiveStartTLI = tli;
326
327 walrcv_proc = walrcv->procno;
328
329 SpinLockRelease(&walrcv->mutex);
330
331 if (launch)
333 else if (walrcv_proc != INVALID_PROC_NUMBER)
334 SetLatch(&GetPGProcByNumber(walrcv_proc)->procLatch);
335}
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1609
Assert(PointerIsAligned(start, uint64))
void SetLatch(Latch *latch)
Definition: latch.c:290
#define NAMEDATALEN
int64 pg_time_t
Definition: pgtime.h:23
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:165
@ PMSIGNAL_START_WALRECEIVER
Definition: pmsignal.h:42
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define GetPGProcByNumber(n)
Definition: proc.h:440
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
int ProcNumber
Definition: procnumber.h:24
TimeLineID receiveStartTLI
Definition: walreceiver.h:87
char slotname[NAMEDATALEN]
Definition: walreceiver.h:136
XLogRecPtr receiveStart
Definition: walreceiver.h:86
ProcNumber procno
Definition: walreceiver.h:67
bool is_temp_slot
Definition: walreceiver.h:142
pg_time_t startTime
Definition: walreceiver.h:78
WalRcvState walRcvState
Definition: walreceiver.h:71
char conninfo[MAXCONNINFO]
Definition: walreceiver.h:123
#define MAXCONNINFO
Definition: walreceiver.h:37
int wal_segment_size
Definition: xlog.c:146
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
#define XLogRecPtrIsValid(r)
Definition: xlogdefs.h:29

References Assert(), WalRcvData::conninfo, WalRcvData::flushedUpto, GetPGProcByNumber, INVALID_PROC_NUMBER, WalRcvData::is_temp_slot, WalRcvData::latestChunkStart, MAXCONNINFO, WalRcvData::mutex, NAMEDATALEN, now(), PMSIGNAL_START_WALRECEIVER, WalRcvData::procno, 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, XLogRecPtrIsValid, and XLogSegmentOffset.

Referenced by WaitForWALToBecomeAvailable().

◆ ShutdownWalRcv()

void ShutdownWalRcv ( void  )

Definition at line 193 of file walreceiverfuncs.c.

194{
195 WalRcvData *walrcv = WalRcv;
196 pid_t walrcvpid = 0;
197 bool stopped = false;
198
199 /*
200 * Request walreceiver to stop. Walreceiver will switch to WALRCV_STOPPED
201 * mode once it's finished, and will also request postmaster to not
202 * restart itself.
203 */
204 SpinLockAcquire(&walrcv->mutex);
205 switch (walrcv->walRcvState)
206 {
207 case WALRCV_STOPPED:
208 break;
209 case WALRCV_STARTING:
210 walrcv->walRcvState = WALRCV_STOPPED;
211 stopped = true;
212 break;
213
214 case WALRCV_STREAMING:
215 case WALRCV_WAITING:
218 /* fall through */
219 case WALRCV_STOPPING:
220 walrcvpid = walrcv->pid;
221 break;
222 }
223 SpinLockRelease(&walrcv->mutex);
224
225 /* Unnecessary but consistent. */
226 if (stopped)
228
229 /*
230 * Signal walreceiver process if it was still running.
231 */
232 if (walrcvpid != 0)
233 kill(walrcvpid, SIGTERM);
234
235 /*
236 * Wait for walreceiver to acknowledge its death by setting state to
237 * WALRCV_STOPPED.
238 */
240 while (WalRcvRunning())
242 WAIT_EVENT_WAL_RECEIVER_EXIT);
244}
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:68
ConditionVariable walRcvStoppedCV
Definition: walreceiver.h:72
bool WalRcvRunning(void)
#define kill(pid, sig)
Definition: win32_port.h:490

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 471 of file walreceiver.h.

472{
473 if (!walres)
474 return;
475
476 if (walres->err)
477 pfree(walres->err);
478
479 if (walres->tuplestore)
480 tuplestore_end(walres->tuplestore);
481
482 if (walres->tupledesc)
483 FreeTupleDesc(walres->tupledesc);
484
485 pfree(walres);
486}
void pfree(void *pointer)
Definition: mcxt.c:1616
Tuplestorestate * tuplestore
Definition: walreceiver.h:223
TupleDesc tupledesc
Definition: walreceiver.h:224
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:502
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:492

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

Referenced by check_pub_dead_tuple_retention(), check_publications(), check_publications_origin_sequences(), check_publications_origin_tables(), copy_sequences(), copy_table(), fetch_relation_list(), fetch_remote_slots(), fetch_remote_table_info(), LogicalRepSyncTableStart(), ReplicationSlotDropAtPubNode(), and validate_remote_info().

◆ WalRcvForceReply()

void WalRcvForceReply ( void  )

Definition at line 1368 of file walreceiver.c.

1369{
1370 ProcNumber procno;
1371
1372 WalRcv->force_reply = true;
1373 /* fetching the proc number is probably atomic, but don't rely on it */
1375 procno = WalRcv->procno;
1377 if (procno != INVALID_PROC_NUMBER)
1378 SetLatch(&GetPGProcByNumber(procno)->procLatch);
1379}
sig_atomic_t force_reply
Definition: walreceiver.h:162

References WalRcvData::force_reply, GetPGProcByNumber, INVALID_PROC_NUMBER, WalRcvData::mutex, WalRcvData::procno, SetLatch(), SpinLockAcquire, SpinLockRelease, and WalRcv.

Referenced by ApplyWalRecord(), and WaitForWALToBecomeAvailable().

◆ WalRcvGetState()

WalRcvState WalRcvGetState ( void  )

Definition at line 124 of file walreceiverfuncs.c.

125{
126 WalRcvData *walrcv = WalRcv;
128
129 SpinLockAcquire(&walrcv->mutex);
130 state = walrcv->walRcvState;
131 SpinLockRelease(&walrcv->mutex);
132
133 return state;
134}
Definition: regguts.h:323

References WalRcvData::mutex, SpinLockAcquire, SpinLockRelease, WalRcv, and WalRcvData::walRcvState.

Referenced by WaitForWALToBecomeAvailable().

◆ WalRcvRunning()

bool WalRcvRunning ( void  )

Definition at line 76 of file walreceiverfuncs.c.

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

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

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

Referenced by CreateOrAttachShmemStructs().

◆ WalRcvShmemSize()

Size WalRcvShmemSize ( void  )

Definition at line 44 of file walreceiverfuncs.c.

45{
46 Size size = 0;
47
48 size = add_size(size, sizeof(WalRcvData));
49
50 return size;
51}
size_t Size
Definition: c.h:625
Size add_size(Size s1, Size s2)
Definition: shmem.c:495

References add_size().

Referenced by CalculateShmemSize(), and WalRcvShmemInit().

◆ WalRcvStreaming()

bool WalRcvStreaming ( void  )

Definition at line 141 of file walreceiverfuncs.c.

142{
143 WalRcvData *walrcv = WalRcv;
145 pg_time_t startTime;
146
147 SpinLockAcquire(&walrcv->mutex);
148
149 state = walrcv->walRcvState;
150 startTime = walrcv->startTime;
151
152 SpinLockRelease(&walrcv->mutex);
153
154 /*
155 * If it has taken too long for walreceiver to start up, give up. Setting
156 * the state to STOPPED ensures that if walreceiver later does start up
157 * after all, it will see that it's not supposed to be running and die
158 * without doing anything.
159 */
160 if (state == WALRCV_STARTING)
161 {
162 pg_time_t now = (pg_time_t) time(NULL);
163
164 if ((now - startTime) > WALRCV_STARTUP_TIMEOUT)
165 {
166 bool stopped = false;
167
168 SpinLockAcquire(&walrcv->mutex);
169 if (walrcv->walRcvState == WALRCV_STARTING)
170 {
171 state = walrcv->walRcvState = WALRCV_STOPPED;
172 stopped = true;
173 }
174 SpinLockRelease(&walrcv->mutex);
175
176 if (stopped)
178 }
179 }
180
183 return true;
184 else
185 return false;
186}

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()

pg_noreturn void WalReceiverMain ( const void *  startup_data,
size_t  startup_data_len 
)

Definition at line 153 of file walreceiver.c.

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

References ARCHIVE_MODE_ALWAYS, Assert(), AuxiliaryProcessMainCommon(), B_WAL_RECEIVER, buf, CHECK_FOR_INTERRUPTS, close, cluster_name, ConditionVariableBroadcast(), ConfigReloadPending, WalRcvData::conninfo, DEBUG1, die, 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::latestWalEndTime, len, load_file(), LOG, LogstreamResult, LSN_FORMAT_ARGS, MAXCONNINFO, MAXFNAMELEN, Min, WalRcvData::mutex, MyBackendType, MyLatch, MyProcNumber, MyProcPid, NAMEDATALEN, now(), NUM_WALRCV_WAKEUPS, on_shmem_exit(), options, PANIC, pfree(), pg_atomic_write_u64(), pg_memory_barrier, PGC_SIGHUP, PGINVALID_SOCKET, pgstat_report_wal(), WalRcvData::pid, PointerGetDatum(), pqsignal, proc_exit(), ProcessConfigFile(), WalRcvData::procno, procsignal_sigusr1_handler(), WalRcvData::ready_to_display, WalRcvData::receiveStart, WalRcvData::receiveStartTLI, RecoveryInProgress(), recvFile, recvFileTLI, recvSegNo, reply_message, ResetLatch(), WalRcvData::sender_host, WalRcvData::sender_port, SIGALRM, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), 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 95 of file walreceiver.c.

Referenced by _PG_init(), and WalReceiverMain().