walreceiver.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*-------------------------------------------------------------------------
  2. *
  3. * walreceiver.h
  4. * Exports from replication/walreceiverfuncs.c.
  5. *
  6. * Portions Copyright (c) 2010-2016, PostgreSQL Global Development Group
  7. *
  8. * src/include/replication/walreceiver.h
  9. *
  10. *-------------------------------------------------------------------------
  11. */
  12. #ifndef _WALRECEIVER_H
  13. #define _WALRECEIVER_H
  14. #include "access/xlog.h"
  15. #include "access/xlogdefs.h"
  16. #include "fmgr.h"
  17. #include "storage/latch.h"
  18. #include "storage/spin.h"
  19. #include "pgtime.h"
  20. /* user-settable parameters */
  21. extern int wal_receiver_status_interval;
  22. extern int wal_receiver_timeout;
  23. extern bool hot_standby_feedback;
  24. /*
  25. * MAXCONNINFO: maximum size of a connection string.
  26. *
  27. * XXX: Should this move to pg_config_manual.h?
  28. */
  29. #define MAXCONNINFO 1024
  30. /* Can we allow the standby to accept replication connection from another standby? */
  31. #define AllowCascadeReplication() (EnableHotStandby && max_wal_senders > 0)
  32. /*
  33. * Values for WalRcv->walRcvState.
  34. */
  35. typedef enum
  36. {
  37. WALRCV_STOPPED, /* stopped and mustn't start up again */
  38. WALRCV_STARTING, /* launched, but the process hasn't
  39. * initialized yet */
  40. WALRCV_STREAMING, /* walreceiver is streaming */
  41. WALRCV_WAITING, /* stopped streaming, waiting for orders */
  42. WALRCV_RESTARTING, /* asked to restart streaming */
  43. WALRCV_STOPPING /* requested to stop, but still running */
  44. } WalRcvState;
  45. /* Shared memory area for management of walreceiver process */
  46. typedef struct
  47. {
  48. /*
  49. * PID of currently active walreceiver process, its current state and
  50. * start time (actually, the time at which it was requested to be
  51. * started).
  52. */
  53. pid_t pid;
  54. WalRcvState walRcvState;
  55. pg_time_t startTime;
  56. /*
  57. * receiveStart and receiveStartTLI indicate the first byte position and
  58. * timeline that will be received. When startup process starts the
  59. * walreceiver, it sets these to the point where it wants the streaming to
  60. * begin.
  61. */
  62. XLogRecPtr receiveStart;
  63. TimeLineID receiveStartTLI;
  64. /*
  65. * receivedUpto-1 is the last byte position that has already been
  66. * received, and receivedTLI is the timeline it came from. At the first
  67. * startup of walreceiver, these are set to receiveStart and
  68. * receiveStartTLI. After that, walreceiver updates these whenever it
  69. * flushes the received WAL to disk.
  70. */
  71. XLogRecPtr receivedUpto;
  72. TimeLineID receivedTLI;
  73. /*
  74. * latestChunkStart is the starting byte position of the current "batch"
  75. * of received WAL. It's actually the same as the previous value of
  76. * receivedUpto before the last flush to disk. Startup process can use
  77. * this to detect whether it's keeping up or not.
  78. */
  79. XLogRecPtr latestChunkStart;
  80. /*
  81. * Time of send and receive of any message received.
  82. */
  83. TimestampTz lastMsgSendTime;
  84. TimestampTz lastMsgReceiptTime;
  85. /*
  86. * Latest reported end of WAL on the sender
  87. */
  88. XLogRecPtr latestWalEnd;
  89. TimestampTz latestWalEndTime;
  90. /*
  91. * connection string; initially set to connect to the primary, and later
  92. * clobbered to hide security-sensitive fields.
  93. */
  94. char conninfo[MAXCONNINFO];
  95. /*
  96. * replication slot name; is also used for walreceiver to connect with the
  97. * primary
  98. */
  99. char slotname[NAMEDATALEN];
  100. slock_t mutex; /* locks shared variables shown above */
  101. /*
  102. * force walreceiver reply? This doesn't need to be locked; memory
  103. * barriers for ordering are sufficient.
  104. */
  105. bool force_reply;
  106. /* set true once conninfo is ready to display (obfuscated pwds etc) */
  107. bool ready_to_display;
  108. /*
  109. * Latch used by startup process to wake up walreceiver after telling it
  110. * where to start streaming (after setting receiveStart and
  111. * receiveStartTLI), and also to tell it to send apply feedback to the
  112. * primary whenever specially marked commit records are applied.
  113. */
  114. Latch latch;
  115. } WalRcvData;
  116. extern WalRcvData *WalRcv;
  117. /* libpqwalreceiver hooks */
  118. typedef void (*walrcv_connect_type) (char *conninfo);
  119. extern PGDLLIMPORT walrcv_connect_type walrcv_connect;
  120. typedef char *(*walrcv_get_conninfo_type) (void);
  121. extern PGDLLIMPORT walrcv_get_conninfo_type walrcv_get_conninfo;
  122. typedef void (*walrcv_identify_system_type) (TimeLineID *primary_tli);
  123. extern PGDLLIMPORT walrcv_identify_system_type walrcv_identify_system;
  124. typedef void (*walrcv_readtimelinehistoryfile_type) (TimeLineID tli, char **filename, char **content, int *size);
  125. extern PGDLLIMPORT walrcv_readtimelinehistoryfile_type walrcv_readtimelinehistoryfile;
  126. typedef bool (*walrcv_startstreaming_type) (TimeLineID tli, XLogRecPtr startpoint, char *slotname);
  127. extern PGDLLIMPORT walrcv_startstreaming_type walrcv_startstreaming;
  128. typedef void (*walrcv_endstreaming_type) (TimeLineID *next_tli);
  129. extern PGDLLIMPORT walrcv_endstreaming_type walrcv_endstreaming;
  130. typedef int (*walrcv_receive_type) (char **buffer, pgsocket *wait_fd);
  131. extern PGDLLIMPORT walrcv_receive_type walrcv_receive;
  132. typedef void (*walrcv_send_type) (const char *buffer, int nbytes);
  133. extern PGDLLIMPORT walrcv_send_type walrcv_send;
  134. typedef void (*walrcv_disconnect_type) (void);
  135. extern PGDLLIMPORT walrcv_disconnect_type walrcv_disconnect;
  136. /* prototypes for functions in walreceiver.c */
  137. extern void WalReceiverMain(void) pg_attribute_noreturn();
  138. extern Datum pg_stat_get_wal_receiver(PG_FUNCTION_ARGS);
  139. /* prototypes for functions in walreceiverfuncs.c */
  140. extern Size WalRcvShmemSize(void);
  141. extern void WalRcvShmemInit(void);
  142. extern void ShutdownWalRcv(void);
  143. extern bool WalRcvStreaming(void);
  144. extern bool WalRcvRunning(void);
  145. extern void RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr,
  146. const char *conninfo, const char *slotname);
  147. extern XLogRecPtr GetWalRcvWriteRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI);
  148. extern int GetReplicationApplyDelay(void);
  149. extern int GetReplicationTransferLatency(void);
  150. extern void WalRcvForceReply(void);
  151. #endif /* _WALRECEIVER_H */