LCOV - code coverage report
Current view: top level - src/include/replication - walreceiver.h (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 8 10 80.0 %
Date: 2019-09-19 17:07:13 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * walreceiver.h
       4             :  *    Exports from replication/walreceiverfuncs.c.
       5             :  *
       6             :  * Portions Copyright (c) 2010-2019, PostgreSQL Global Development Group
       7             :  *
       8             :  * src/include/replication/walreceiver.h
       9             :  *
      10             :  *-------------------------------------------------------------------------
      11             :  */
      12             : #ifndef _WALRECEIVER_H
      13             : #define _WALRECEIVER_H
      14             : 
      15             : #include "access/xlog.h"
      16             : #include "access/xlogdefs.h"
      17             : #include "getaddrinfo.h"      /* for NI_MAXHOST */
      18             : #include "replication/logicalproto.h"
      19             : #include "replication/walsender.h"
      20             : #include "storage/latch.h"
      21             : #include "storage/spin.h"
      22             : #include "pgtime.h"
      23             : #include "utils/tuplestore.h"
      24             : 
      25             : /* user-settable parameters */
      26             : extern int  wal_receiver_status_interval;
      27             : extern int  wal_receiver_timeout;
      28             : extern bool hot_standby_feedback;
      29             : 
      30             : /*
      31             :  * MAXCONNINFO: maximum size of a connection string.
      32             :  *
      33             :  * XXX: Should this move to pg_config_manual.h?
      34             :  */
      35             : #define MAXCONNINFO     1024
      36             : 
      37             : /* Can we allow the standby to accept replication connection from another standby? */
      38             : #define AllowCascadeReplication() (EnableHotStandby && max_wal_senders > 0)
      39             : 
      40             : /*
      41             :  * Values for WalRcv->walRcvState.
      42             :  */
      43             : typedef enum
      44             : {
      45             :     WALRCV_STOPPED,             /* stopped and mustn't start up again */
      46             :     WALRCV_STARTING,            /* launched, but the process hasn't
      47             :                                  * initialized yet */
      48             :     WALRCV_STREAMING,           /* walreceiver is streaming */
      49             :     WALRCV_WAITING,             /* stopped streaming, waiting for orders */
      50             :     WALRCV_RESTARTING,          /* asked to restart streaming */
      51             :     WALRCV_STOPPING             /* requested to stop, but still running */
      52             : } WalRcvState;
      53             : 
      54             : /* Shared memory area for management of walreceiver process */
      55             : typedef struct
      56             : {
      57             :     /*
      58             :      * PID of currently active walreceiver process, its current state and
      59             :      * start time (actually, the time at which it was requested to be
      60             :      * started).
      61             :      */
      62             :     pid_t       pid;
      63             :     WalRcvState walRcvState;
      64             :     pg_time_t   startTime;
      65             : 
      66             :     /*
      67             :      * receiveStart and receiveStartTLI indicate the first byte position and
      68             :      * timeline that will be received. When startup process starts the
      69             :      * walreceiver, it sets these to the point where it wants the streaming to
      70             :      * begin.
      71             :      */
      72             :     XLogRecPtr  receiveStart;
      73             :     TimeLineID  receiveStartTLI;
      74             : 
      75             :     /*
      76             :      * receivedUpto-1 is the last byte position that has already been
      77             :      * received, and receivedTLI is the timeline it came from.  At the first
      78             :      * startup of walreceiver, these are set to receiveStart and
      79             :      * receiveStartTLI. After that, walreceiver updates these whenever it
      80             :      * flushes the received WAL to disk.
      81             :      */
      82             :     XLogRecPtr  receivedUpto;
      83             :     TimeLineID  receivedTLI;
      84             : 
      85             :     /*
      86             :      * latestChunkStart is the starting byte position of the current "batch"
      87             :      * of received WAL.  It's actually the same as the previous value of
      88             :      * receivedUpto before the last flush to disk.  Startup process can use
      89             :      * this to detect whether it's keeping up or not.
      90             :      */
      91             :     XLogRecPtr  latestChunkStart;
      92             : 
      93             :     /*
      94             :      * Time of send and receive of any message received.
      95             :      */
      96             :     TimestampTz lastMsgSendTime;
      97             :     TimestampTz lastMsgReceiptTime;
      98             : 
      99             :     /*
     100             :      * Latest reported end of WAL on the sender
     101             :      */
     102             :     XLogRecPtr  latestWalEnd;
     103             :     TimestampTz latestWalEndTime;
     104             : 
     105             :     /*
     106             :      * connection string; initially set to connect to the primary, and later
     107             :      * clobbered to hide security-sensitive fields.
     108             :      */
     109             :     char        conninfo[MAXCONNINFO];
     110             : 
     111             :     /*
     112             :      * Host name (this can be a host name, an IP address, or a directory path)
     113             :      * and port number of the active replication connection.
     114             :      */
     115             :     char        sender_host[NI_MAXHOST];
     116             :     int         sender_port;
     117             : 
     118             :     /*
     119             :      * replication slot name; is also used for walreceiver to connect with the
     120             :      * primary
     121             :      */
     122             :     char        slotname[NAMEDATALEN];
     123             : 
     124             :     /* set true once conninfo is ready to display (obfuscated pwds etc) */
     125             :     bool        ready_to_display;
     126             : 
     127             :     /*
     128             :      * Latch used by startup process to wake up walreceiver after telling it
     129             :      * where to start streaming (after setting receiveStart and
     130             :      * receiveStartTLI), and also to tell it to send apply feedback to the
     131             :      * primary whenever specially marked commit records are applied. This is
     132             :      * normally mapped to procLatch when walreceiver is running.
     133             :      */
     134             :     Latch      *latch;
     135             : 
     136             :     slock_t     mutex;          /* locks shared variables shown above */
     137             : 
     138             :     /*
     139             :      * force walreceiver reply?  This doesn't need to be locked; memory
     140             :      * barriers for ordering are sufficient.  But we do need atomic fetch and
     141             :      * store semantics, so use sig_atomic_t.
     142             :      */
     143             :     sig_atomic_t force_reply;   /* used as a bool */
     144             : } WalRcvData;
     145             : 
     146             : extern WalRcvData *WalRcv;
     147             : 
     148             : typedef struct
     149             : {
     150             :     bool        logical;        /* True if this is logical replication stream,
     151             :                                  * false if physical stream.  */
     152             :     char       *slotname;       /* Name of the replication slot or NULL. */
     153             :     XLogRecPtr  startpoint;     /* LSN of starting point. */
     154             : 
     155             :     union
     156             :     {
     157             :         struct
     158             :         {
     159             :             TimeLineID  startpointTLI;  /* Starting timeline */
     160             :         }           physical;
     161             :         struct
     162             :         {
     163             :             uint32      proto_version;  /* Logical protocol version */
     164             :             List       *publication_names;  /* String list of publications */
     165             :         }           logical;
     166             :     }           proto;
     167             : } WalRcvStreamOptions;
     168             : 
     169             : struct WalReceiverConn;
     170             : typedef struct WalReceiverConn WalReceiverConn;
     171             : 
     172             : /*
     173             :  * Status of walreceiver query execution.
     174             :  *
     175             :  * We only define statuses that are currently used.
     176             :  */
     177             : typedef enum
     178             : {
     179             :     WALRCV_ERROR,               /* There was error when executing the query. */
     180             :     WALRCV_OK_COMMAND,          /* Query executed utility or replication
     181             :                                  * command. */
     182             :     WALRCV_OK_TUPLES,           /* Query returned tuples. */
     183             :     WALRCV_OK_COPY_IN,          /* Query started COPY FROM. */
     184             :     WALRCV_OK_COPY_OUT,         /* Query started COPY TO. */
     185             :     WALRCV_OK_COPY_BOTH         /* Query started COPY BOTH replication
     186             :                                  * protocol. */
     187             : } WalRcvExecStatus;
     188             : 
     189             : /*
     190             :  * Return value for walrcv_exec, returns the status of the execution and
     191             :  * tuples if any.
     192             :  */
     193             : typedef struct WalRcvExecResult
     194             : {
     195             :     WalRcvExecStatus status;
     196             :     char       *err;
     197             :     Tuplestorestate *tuplestore;
     198             :     TupleDesc   tupledesc;
     199             : } WalRcvExecResult;
     200             : 
     201             : /* libpqwalreceiver hooks */
     202             : typedef WalReceiverConn *(*walrcv_connect_fn) (const char *conninfo, bool logical,
     203             :                                                const char *appname,
     204             :                                                char **err);
     205             : typedef void (*walrcv_check_conninfo_fn) (const char *conninfo);
     206             : typedef char *(*walrcv_get_conninfo_fn) (WalReceiverConn *conn);
     207             : typedef void (*walrcv_get_senderinfo_fn) (WalReceiverConn *conn,
     208             :                                           char **sender_host,
     209             :                                           int *sender_port);
     210             : typedef char *(*walrcv_identify_system_fn) (WalReceiverConn *conn,
     211             :                                             TimeLineID *primary_tli);
     212             : typedef int (*walrcv_server_version_fn) (WalReceiverConn *conn);
     213             : typedef void (*walrcv_readtimelinehistoryfile_fn) (WalReceiverConn *conn,
     214             :                                                    TimeLineID tli,
     215             :                                                    char **filename,
     216             :                                                    char **content, int *size);
     217             : typedef bool (*walrcv_startstreaming_fn) (WalReceiverConn *conn,
     218             :                                           const WalRcvStreamOptions *options);
     219             : typedef void (*walrcv_endstreaming_fn) (WalReceiverConn *conn,
     220             :                                         TimeLineID *next_tli);
     221             : typedef int (*walrcv_receive_fn) (WalReceiverConn *conn, char **buffer,
     222             :                                   pgsocket *wait_fd);
     223             : typedef void (*walrcv_send_fn) (WalReceiverConn *conn, const char *buffer,
     224             :                                 int nbytes);
     225             : typedef char *(*walrcv_create_slot_fn) (WalReceiverConn *conn,
     226             :                                         const char *slotname, bool temporary,
     227             :                                         CRSSnapshotAction snapshot_action,
     228             :                                         XLogRecPtr *lsn);
     229             : typedef WalRcvExecResult *(*walrcv_exec_fn) (WalReceiverConn *conn,
     230             :                                              const char *query,
     231             :                                              const int nRetTypes,
     232             :                                              const Oid *retTypes);
     233             : typedef void (*walrcv_disconnect_fn) (WalReceiverConn *conn);
     234             : 
     235             : typedef struct WalReceiverFunctionsType
     236             : {
     237             :     walrcv_connect_fn walrcv_connect;
     238             :     walrcv_check_conninfo_fn walrcv_check_conninfo;
     239             :     walrcv_get_conninfo_fn walrcv_get_conninfo;
     240             :     walrcv_get_senderinfo_fn walrcv_get_senderinfo;
     241             :     walrcv_identify_system_fn walrcv_identify_system;
     242             :     walrcv_server_version_fn walrcv_server_version;
     243             :     walrcv_readtimelinehistoryfile_fn walrcv_readtimelinehistoryfile;
     244             :     walrcv_startstreaming_fn walrcv_startstreaming;
     245             :     walrcv_endstreaming_fn walrcv_endstreaming;
     246             :     walrcv_receive_fn walrcv_receive;
     247             :     walrcv_send_fn walrcv_send;
     248             :     walrcv_create_slot_fn walrcv_create_slot;
     249             :     walrcv_exec_fn walrcv_exec;
     250             :     walrcv_disconnect_fn walrcv_disconnect;
     251             : } WalReceiverFunctionsType;
     252             : 
     253             : extern PGDLLIMPORT WalReceiverFunctionsType *WalReceiverFunctions;
     254             : 
     255             : #define walrcv_connect(conninfo, logical, appname, err) \
     256             :     WalReceiverFunctions->walrcv_connect(conninfo, logical, appname, err)
     257             : #define walrcv_check_conninfo(conninfo) \
     258             :     WalReceiverFunctions->walrcv_check_conninfo(conninfo)
     259             : #define walrcv_get_conninfo(conn) \
     260             :     WalReceiverFunctions->walrcv_get_conninfo(conn)
     261             : #define walrcv_get_senderinfo(conn, sender_host, sender_port) \
     262             :     WalReceiverFunctions->walrcv_get_senderinfo(conn, sender_host, sender_port)
     263             : #define walrcv_identify_system(conn, primary_tli) \
     264             :     WalReceiverFunctions->walrcv_identify_system(conn, primary_tli)
     265             : #define walrcv_server_version(conn) \
     266             :     WalReceiverFunctions->walrcv_server_version(conn)
     267             : #define walrcv_readtimelinehistoryfile(conn, tli, filename, content, size) \
     268             :     WalReceiverFunctions->walrcv_readtimelinehistoryfile(conn, tli, filename, content, size)
     269             : #define walrcv_startstreaming(conn, options) \
     270             :     WalReceiverFunctions->walrcv_startstreaming(conn, options)
     271             : #define walrcv_endstreaming(conn, next_tli) \
     272             :     WalReceiverFunctions->walrcv_endstreaming(conn, next_tli)
     273             : #define walrcv_receive(conn, buffer, wait_fd) \
     274             :     WalReceiverFunctions->walrcv_receive(conn, buffer, wait_fd)
     275             : #define walrcv_send(conn, buffer, nbytes) \
     276             :     WalReceiverFunctions->walrcv_send(conn, buffer, nbytes)
     277             : #define walrcv_create_slot(conn, slotname, temporary, snapshot_action, lsn) \
     278             :     WalReceiverFunctions->walrcv_create_slot(conn, slotname, temporary, snapshot_action, lsn)
     279             : #define walrcv_exec(conn, exec, nRetTypes, retTypes) \
     280             :     WalReceiverFunctions->walrcv_exec(conn, exec, nRetTypes, retTypes)
     281             : #define walrcv_disconnect(conn) \
     282             :     WalReceiverFunctions->walrcv_disconnect(conn)
     283             : 
     284             : static inline void
     285         410 : walrcv_clear_result(WalRcvExecResult *walres)
     286             : {
     287         410 :     if (!walres)
     288           0 :         return;
     289             : 
     290         410 :     if (walres->err)
     291           0 :         pfree(walres->err);
     292             : 
     293         410 :     if (walres->tuplestore)
     294         184 :         tuplestore_end(walres->tuplestore);
     295             : 
     296         410 :     if (walres->tupledesc)
     297         184 :         FreeTupleDesc(walres->tupledesc);
     298             : 
     299         410 :     pfree(walres);
     300             : }
     301             : 
     302             : /* prototypes for functions in walreceiver.c */
     303             : extern void WalReceiverMain(void) pg_attribute_noreturn();
     304             : extern void ProcessWalRcvInterrupts(void);
     305             : 
     306             : /* prototypes for functions in walreceiverfuncs.c */
     307             : extern Size WalRcvShmemSize(void);
     308             : extern void WalRcvShmemInit(void);
     309             : extern void ShutdownWalRcv(void);
     310             : extern bool WalRcvStreaming(void);
     311             : extern bool WalRcvRunning(void);
     312             : extern void RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr,
     313             :                                  const char *conninfo, const char *slotname);
     314             : extern XLogRecPtr GetWalRcvWriteRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI);
     315             : extern int  GetReplicationApplyDelay(void);
     316             : extern int  GetReplicationTransferLatency(void);
     317             : extern void WalRcvForceReply(void);
     318             : 
     319             : #endif                          /* _WALRECEIVER_H */

Generated by: LCOV version 1.13