Coverage Report

Created: 2026-12-01 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/postgres/src/include/replication/walreceiver.h
Line
Count
Source
1
/*-------------------------------------------------------------------------
2
 *
3
 * walreceiver.h
4
 *    Exports from replication/walreceiverfuncs.c.
5
 *
6
 * Portions Copyright (c) 2010-2026, 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 <netdb.h>
16
17
#include "access/xlog.h"
18
#include "access/xlogdefs.h"
19
#include "pgtime.h"
20
#include "port/atomics.h"
21
#include "replication/logicalproto.h"
22
#include "replication/walsender.h"
23
#include "storage/condition_variable.h"
24
#include "storage/spin.h"
25
#include "utils/tuplestore.h"
26
27
/* user-settable parameters */
28
extern PGDLLIMPORT int wal_receiver_status_interval;
29
extern PGDLLIMPORT int wal_receiver_timeout;
30
extern PGDLLIMPORT bool hot_standby_feedback;
31
32
/*
33
 * MAXCONNINFO: maximum size of a connection string.
34
 *
35
 * XXX: Should this move to pg_config_manual.h?
36
 */
37
0
#define MAXCONNINFO   1024
38
39
/* Can we allow the standby to accept replication connection from another standby? */
40
0
#define AllowCascadeReplication() (EnableHotStandby && max_wal_senders > 0)
41
42
/*
43
 * Values for WalRcv->walRcvState.
44
 */
45
typedef enum
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_CONNECTING,      /* connecting to upstream server */
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;
56
57
/* Shared memory area for management of walreceiver process */
58
typedef struct
59
{
60
  /*
61
   * Currently active walreceiver process's proc number and PID.
62
   *
63
   * The startup process uses the proc number to wake it up after telling it
64
   * where to start streaming (after setting receiveStart and
65
   * receiveStartTLI), and also to tell it to send apply feedback to the
66
   * primary whenever specially marked commit records are applied.
67
   */
68
  ProcNumber  procno;
69
  pid_t   pid;
70
71
  /* Its current state */
72
  WalRcvState walRcvState;
73
  ConditionVariable walRcvStoppedCV;
74
75
  /*
76
   * Its start time (actually, the time at which it was requested to be
77
   * started).
78
   */
79
  pg_time_t startTime;
80
81
  /*
82
   * receiveStart and receiveStartTLI indicate the first byte position and
83
   * timeline that will be received. When startup process starts the
84
   * walreceiver, it sets these to the point where it wants the streaming to
85
   * begin.
86
   */
87
  XLogRecPtr  receiveStart;
88
  TimeLineID  receiveStartTLI;
89
90
  /*
91
   * flushedUpto-1 is the last byte position that has already been received,
92
   * and receivedTLI is the timeline it came from.  At the first startup of
93
   * walreceiver, these are set to receiveStart and receiveStartTLI. After
94
   * that, walreceiver updates these whenever it flushes the received WAL to
95
   * disk.
96
   */
97
  XLogRecPtr  flushedUpto;
98
  TimeLineID  receivedTLI;
99
100
  /*
101
   * latestChunkStart is the starting byte position of the current "batch"
102
   * of received WAL.  It's actually the same as the previous value of
103
   * flushedUpto before the last flush to disk.  Startup process can use
104
   * this to detect whether it's keeping up or not.
105
   */
106
  XLogRecPtr  latestChunkStart;
107
108
  /*
109
   * Time of send and receive of any message received.
110
   */
111
  TimestampTz lastMsgSendTime;
112
  TimestampTz lastMsgReceiptTime;
113
114
  /*
115
   * Latest reported end of WAL on the sender
116
   */
117
  XLogRecPtr  latestWalEnd;
118
  TimestampTz latestWalEndTime;
119
120
  /*
121
   * connection string; initially set to connect to the primary, and later
122
   * clobbered to hide security-sensitive fields.
123
   */
124
  char    conninfo[MAXCONNINFO];
125
126
  /*
127
   * Host name (this can be a host name, an IP address, or a directory path)
128
   * and port number of the active replication connection.
129
   */
130
  char    sender_host[NI_MAXHOST];
131
  int     sender_port;
132
133
  /*
134
   * replication slot name; is also used for walreceiver to connect with the
135
   * primary
136
   */
137
  char    slotname[NAMEDATALEN];
138
139
  /*
140
   * If it's a temporary replication slot, it needs to be recreated when
141
   * connecting.
142
   */
143
  bool    is_temp_slot;
144
145
  /* set true once conninfo is ready to display (obfuscated pwds etc) */
146
  bool    ready_to_display;
147
148
  slock_t   mutex;      /* locks shared variables shown above */
149
150
  /*
151
   * Like flushedUpto, but advanced after writing and before flushing,
152
   * without the need to acquire the spin lock.  Data can be read by another
153
   * process up to this point, but shouldn't be used for data integrity
154
   * purposes.
155
   */
156
  pg_atomic_uint64 writtenUpto;
157
158
  /*
159
   * request walreceiver reply?  This doesn't need to be locked; memory
160
   * barriers for ordering are sufficient.  But we do need atomic fetch and
161
   * store semantics, so use sig_atomic_t.
162
   */
163
  sig_atomic_t apply_reply_requested; /* used as a bool */
164
} WalRcvData;
165
166
extern PGDLLIMPORT WalRcvData *WalRcv;
167
168
typedef struct
169
{
170
  bool    logical;    /* True if this is logical replication stream,
171
                 * false if physical stream.  */
172
  char     *slotname;   /* Name of the replication slot or NULL. */
173
  XLogRecPtr  startpoint;   /* LSN of starting point. */
174
175
  union
176
  {
177
    struct
178
    {
179
      TimeLineID  startpointTLI;  /* Starting timeline */
180
    }     physical;
181
    struct
182
    {
183
      uint32    proto_version;  /* Logical protocol version */
184
      List     *publication_names;  /* String list of publications */
185
      bool    binary; /* Ask publisher to use binary */
186
      char     *streaming_str;  /* Streaming of large transactions */
187
      bool    twophase; /* Streaming of two-phase transactions at
188
                   * prepare time */
189
      char     *origin; /* Only publish data originating from the
190
                 * specified origin */
191
    }     logical;
192
  }     proto;
193
} WalRcvStreamOptions;
194
195
struct WalReceiverConn;
196
typedef struct WalReceiverConn WalReceiverConn;
197
198
/*
199
 * Status of walreceiver query execution.
200
 *
201
 * We only define statuses that are currently used.
202
 */
203
typedef enum
204
{
205
  WALRCV_ERROR,       /* There was error when executing the query. */
206
  WALRCV_OK_COMMAND,      /* Query executed utility or replication
207
                 * command. */
208
  WALRCV_OK_TUPLES,     /* Query returned tuples. */
209
  WALRCV_OK_COPY_IN,      /* Query started COPY FROM. */
210
  WALRCV_OK_COPY_OUT,     /* Query started COPY TO. */
211
  WALRCV_OK_COPY_BOTH,    /* Query started COPY BOTH replication
212
                 * protocol. */
213
} WalRcvExecStatus;
214
215
/*
216
 * Return value for walrcv_exec, returns the status of the execution and
217
 * tuples if any.
218
 */
219
typedef struct WalRcvExecResult
220
{
221
  WalRcvExecStatus status;
222
  int     sqlstate;
223
  char     *err;
224
  Tuplestorestate *tuplestore;
225
  TupleDesc tupledesc;
226
} WalRcvExecResult;
227
228
/* WAL receiver - libpqwalreceiver hooks */
229
230
/*
231
 * walrcv_connect_fn
232
 *
233
 * Establish connection to a cluster.  'replication' is true if the
234
 * connection is a replication connection, and false if it is a
235
 * regular connection.  If it is a replication connection, it could
236
 * be either logical or physical based on input argument 'logical'.
237
 * 'appname' is a name associated to the connection, to use for example
238
 * with fallback_application_name or application_name.  Returns the
239
 * details about the connection established, as defined by
240
 * WalReceiverConn for each WAL receiver module.  On error, NULL is
241
 * returned with 'err' including the error generated.
242
 */
243
typedef WalReceiverConn *(*walrcv_connect_fn) (const char *conninfo,
244
                         bool replication,
245
                         bool logical,
246
                         bool must_use_password,
247
                         const char *appname,
248
                         char **err);
249
250
/*
251
 * walrcv_check_conninfo_fn
252
 *
253
 * Parse and validate the connection string given as of 'conninfo'.
254
 */
255
typedef void (*walrcv_check_conninfo_fn) (const char *conninfo,
256
                      bool must_use_password);
257
258
/*
259
 * walrcv_get_conninfo_fn
260
 *
261
 * Returns a user-displayable conninfo string.  Note that any
262
 * security-sensitive fields should be obfuscated.
263
 */
264
typedef char *(*walrcv_get_conninfo_fn) (WalReceiverConn *conn);
265
266
/*
267
 * walrcv_get_senderinfo_fn
268
 *
269
 * Provide information of the WAL sender this WAL receiver is connected
270
 * to, as of 'sender_host' for the host of the sender and 'sender_port'
271
 * for its port.
272
 */
273
typedef void (*walrcv_get_senderinfo_fn) (WalReceiverConn *conn,
274
                      char **sender_host,
275
                      int *sender_port);
276
277
/*
278
 * walrcv_identify_system_fn
279
 *
280
 * Run IDENTIFY_SYSTEM on the cluster connected to and validate the
281
 * identity of the cluster.  Returns the system ID of the cluster
282
 * connected to.  'primary_tli' is the timeline ID of the sender.
283
 * If 'server_lsn' is not NULL, it is set to the current WAL flush
284
 * position of the sender.
285
 */
286
typedef char *(*walrcv_identify_system_fn) (WalReceiverConn *conn,
287
                      TimeLineID *primary_tli,
288
                      XLogRecPtr *server_lsn);
289
290
/*
291
 * walrcv_get_dbname_from_conninfo_fn
292
 *
293
 * Returns the database name from the primary_conninfo
294
 */
295
typedef char *(*walrcv_get_dbname_from_conninfo_fn) (const char *conninfo);
296
297
/*
298
 * walrcv_server_version_fn
299
 *
300
 * Returns the version number of the cluster connected to.
301
 */
302
typedef int (*walrcv_server_version_fn) (WalReceiverConn *conn);
303
304
/*
305
 * walrcv_readtimelinehistoryfile_fn
306
 *
307
 * Fetch from cluster the timeline history file for timeline 'tli'.
308
 * Returns the name of the timeline history file as of 'filename', its
309
 * contents as of 'content' and its 'size'.
310
 */
311
typedef void (*walrcv_readtimelinehistoryfile_fn) (WalReceiverConn *conn,
312
                           TimeLineID tli,
313
                           char **filename,
314
                           char **content,
315
                           size_t *size);
316
317
/*
318
 * walrcv_startstreaming_fn
319
 *
320
 * Start streaming WAL data from given streaming options.  Returns true
321
 * if the connection has switched successfully to copy-both mode and false
322
 * if the server received the command and executed it successfully, but
323
 * didn't switch to copy-mode.
324
 */
325
typedef bool (*walrcv_startstreaming_fn) (WalReceiverConn *conn,
326
                      const WalRcvStreamOptions *options);
327
328
/*
329
 * walrcv_endstreaming_fn
330
 *
331
 * Stop streaming of WAL data.  Returns the next timeline ID of the cluster
332
 * connected to in 'next_tli', or 0 if there was no report.
333
 */
334
typedef void (*walrcv_endstreaming_fn) (WalReceiverConn *conn,
335
                    TimeLineID *next_tli);
336
337
/*
338
 * walrcv_receive_fn
339
 *
340
 * Receive a message available from the WAL stream.  'buffer' is a pointer
341
 * to a buffer holding the message received.  Returns the length of the data,
342
 * 0 if no data is available yet ('wait_fd' is a socket descriptor which can
343
 * be waited on before a retry), and -1 if the cluster ended the COPY.
344
 */
345
typedef int (*walrcv_receive_fn) (WalReceiverConn *conn,
346
                  char **buffer,
347
                  pgsocket *wait_fd);
348
349
/*
350
 * walrcv_send_fn
351
 *
352
 * Send a message of size 'nbytes' to the WAL stream with 'buffer' as
353
 * contents.
354
 */
355
typedef void (*walrcv_send_fn) (WalReceiverConn *conn,
356
                const char *buffer,
357
                int nbytes);
358
359
/*
360
 * walrcv_create_slot_fn
361
 *
362
 * Create a new replication slot named 'slotname'.  'temporary' defines
363
 * if the slot is temporary.  'snapshot_action' defines the behavior wanted
364
 * for an exported snapshot (see replication protocol for more details).
365
 * 'lsn' includes the LSN position at which the created slot became
366
 * consistent.  Returns the name of the exported snapshot for a logical
367
 * slot, or NULL for a physical slot.
368
 */
369
typedef char *(*walrcv_create_slot_fn) (WalReceiverConn *conn,
370
                    const char *slotname,
371
                    bool temporary,
372
                    bool two_phase,
373
                    bool failover,
374
                    CRSSnapshotAction snapshot_action,
375
                    XLogRecPtr *lsn);
376
377
/*
378
 * walrcv_alter_slot_fn
379
 *
380
 * Change the definition of a replication slot. Currently, it supports
381
 * changing the failover and two_phase properties of the slot.
382
 */
383
typedef void (*walrcv_alter_slot_fn) (WalReceiverConn *conn,
384
                    const char *slotname,
385
                    const bool *failover,
386
                    const bool *two_phase);
387
388
389
/*
390
 * walrcv_get_backend_pid_fn
391
 *
392
 * Returns the PID of the remote backend process.
393
 */
394
typedef pid_t (*walrcv_get_backend_pid_fn) (WalReceiverConn *conn);
395
396
/*
397
 * walrcv_exec_fn
398
 *
399
 * Send generic queries (and commands) to the remote cluster.  'nRetTypes'
400
 * is the expected number of returned attributes, and 'retTypes' an array
401
 * including their type OIDs.  Returns the status of the execution and
402
 * tuples if any.
403
 */
404
typedef WalRcvExecResult *(*walrcv_exec_fn) (WalReceiverConn *conn,
405
                       const char *query,
406
                       const int nRetTypes,
407
                       const Oid *retTypes);
408
409
/*
410
 * walrcv_disconnect_fn
411
 *
412
 * Disconnect with the cluster.
413
 */
414
typedef void (*walrcv_disconnect_fn) (WalReceiverConn *conn);
415
416
typedef struct WalReceiverFunctionsType
417
{
418
  walrcv_connect_fn walrcv_connect;
419
  walrcv_check_conninfo_fn walrcv_check_conninfo;
420
  walrcv_get_conninfo_fn walrcv_get_conninfo;
421
  walrcv_get_senderinfo_fn walrcv_get_senderinfo;
422
  walrcv_identify_system_fn walrcv_identify_system;
423
  walrcv_get_dbname_from_conninfo_fn walrcv_get_dbname_from_conninfo;
424
  walrcv_server_version_fn walrcv_server_version;
425
  walrcv_readtimelinehistoryfile_fn walrcv_readtimelinehistoryfile;
426
  walrcv_startstreaming_fn walrcv_startstreaming;
427
  walrcv_endstreaming_fn walrcv_endstreaming;
428
  walrcv_receive_fn walrcv_receive;
429
  walrcv_send_fn walrcv_send;
430
  walrcv_create_slot_fn walrcv_create_slot;
431
  walrcv_alter_slot_fn walrcv_alter_slot;
432
  walrcv_get_backend_pid_fn walrcv_get_backend_pid;
433
  walrcv_exec_fn walrcv_exec;
434
  walrcv_disconnect_fn walrcv_disconnect;
435
} WalReceiverFunctionsType;
436
437
extern PGDLLIMPORT WalReceiverFunctionsType *WalReceiverFunctions;
438
439
#define walrcv_connect(conninfo, replication, logical, must_use_password, appname, err) \
440
0
  WalReceiverFunctions->walrcv_connect(conninfo, replication, logical, must_use_password, appname, err)
441
#define walrcv_check_conninfo(conninfo, must_use_password) \
442
0
  WalReceiverFunctions->walrcv_check_conninfo(conninfo, must_use_password)
443
#define walrcv_get_conninfo(conn) \
444
  WalReceiverFunctions->walrcv_get_conninfo(conn)
445
#define walrcv_get_senderinfo(conn, sender_host, sender_port) \
446
  WalReceiverFunctions->walrcv_get_senderinfo(conn, sender_host, sender_port)
447
#define walrcv_identify_system(conn, primary_tli, server_lsn) \
448
  WalReceiverFunctions->walrcv_identify_system(conn, primary_tli, server_lsn)
449
#define walrcv_get_dbname_from_conninfo(conninfo) \
450
0
  WalReceiverFunctions->walrcv_get_dbname_from_conninfo(conninfo)
451
#define walrcv_server_version(conn) \
452
0
  WalReceiverFunctions->walrcv_server_version(conn)
453
#define walrcv_readtimelinehistoryfile(conn, tli, filename, content, size) \
454
0
  WalReceiverFunctions->walrcv_readtimelinehistoryfile(conn, tli, filename, content, size)
455
#define walrcv_startstreaming(conn, options) \
456
0
  WalReceiverFunctions->walrcv_startstreaming(conn, options)
457
#define walrcv_endstreaming(conn, next_tli) \
458
0
  WalReceiverFunctions->walrcv_endstreaming(conn, next_tli)
459
#define walrcv_receive(conn, buffer, wait_fd) \
460
0
  WalReceiverFunctions->walrcv_receive(conn, buffer, wait_fd)
461
#define walrcv_send(conn, buffer, nbytes) \
462
0
  WalReceiverFunctions->walrcv_send(conn, buffer, nbytes)
463
#define walrcv_create_slot(conn, slotname, temporary, two_phase, failover, snapshot_action, lsn) \
464
0
  WalReceiverFunctions->walrcv_create_slot(conn, slotname, temporary, two_phase, failover, snapshot_action, lsn)
465
#define walrcv_alter_slot(conn, slotname, failover, two_phase) \
466
0
  WalReceiverFunctions->walrcv_alter_slot(conn, slotname, failover, two_phase)
467
#define walrcv_get_backend_pid(conn) \
468
  WalReceiverFunctions->walrcv_get_backend_pid(conn)
469
#define walrcv_exec(conn, exec, nRetTypes, retTypes) \
470
0
  WalReceiverFunctions->walrcv_exec(conn, exec, nRetTypes, retTypes)
471
#define walrcv_disconnect(conn) \
472
0
  WalReceiverFunctions->walrcv_disconnect(conn)
473
474
static inline void
475
walrcv_clear_result(WalRcvExecResult *walres)
476
0
{
477
0
  if (!walres)
478
0
    return;
479
480
0
  if (walres->err)
481
0
    pfree(walres->err);
482
483
0
  if (walres->tuplestore)
484
0
    tuplestore_end(walres->tuplestore);
485
486
0
  if (walres->tupledesc)
487
0
    FreeTupleDesc(walres->tupledesc);
488
489
0
  pfree(walres);
490
0
}
Unexecuted instantiation: rewriteheap.c:walrcv_clear_result
Unexecuted instantiation: rmgr.c:walrcv_clear_result
Unexecuted instantiation: xact.c:walrcv_clear_result
Unexecuted instantiation: xlog.c:walrcv_clear_result
Unexecuted instantiation: xlogfuncs.c:walrcv_clear_result
Unexecuted instantiation: xlogrecovery.c:walrcv_clear_result
Unexecuted instantiation: xlogwait.c:walrcv_clear_result
Unexecuted instantiation: dbcommands.c:walrcv_clear_result
Unexecuted instantiation: repack.c:walrcv_clear_result
Unexecuted instantiation: repack_worker.c:walrcv_clear_result
Unexecuted instantiation: subscriptioncmds.c:walrcv_clear_result
Unexecuted instantiation: launch_backend.c:walrcv_clear_result
Unexecuted instantiation: postmaster.c:walrcv_clear_result
Unexecuted instantiation: walsummarizer.c:walrcv_clear_result
Unexecuted instantiation: applyparallelworker.c:walrcv_clear_result
Unexecuted instantiation: conflict.c:walrcv_clear_result
Unexecuted instantiation: decode.c:walrcv_clear_result
Unexecuted instantiation: launcher.c:walrcv_clear_result
Unexecuted instantiation: logical.c:walrcv_clear_result
Unexecuted instantiation: logicalctl.c:walrcv_clear_result
Unexecuted instantiation: logicalfuncs.c:walrcv_clear_result
Unexecuted instantiation: origin.c:walrcv_clear_result
Unexecuted instantiation: relation.c:walrcv_clear_result
Unexecuted instantiation: reorderbuffer.c:walrcv_clear_result
Unexecuted instantiation: sequencesync.c:walrcv_clear_result
Unexecuted instantiation: slotsync.c:walrcv_clear_result
Unexecuted instantiation: snapbuild.c:walrcv_clear_result
Unexecuted instantiation: syncutils.c:walrcv_clear_result
Unexecuted instantiation: tablesync.c:walrcv_clear_result
Unexecuted instantiation: worker.c:walrcv_clear_result
Unexecuted instantiation: slot.c:walrcv_clear_result
Unexecuted instantiation: slotfuncs.c:walrcv_clear_result
Unexecuted instantiation: walreceiver.c:walrcv_clear_result
Unexecuted instantiation: walreceiverfuncs.c:walrcv_clear_result
Unexecuted instantiation: walsender.c:walrcv_clear_result
Unexecuted instantiation: basebackup.c:walrcv_clear_result
Unexecuted instantiation: procsignal.c:walrcv_clear_result
Unexecuted instantiation: standby.c:walrcv_clear_result
Unexecuted instantiation: proc.c:walrcv_clear_result
Unexecuted instantiation: postgres.c:walrcv_clear_result
Unexecuted instantiation: pgstat_replslot.c:walrcv_clear_result
Unexecuted instantiation: pgstat_subscription.c:walrcv_clear_result
Unexecuted instantiation: genfile.c:walrcv_clear_result
Unexecuted instantiation: pg_upgrade_support.c:walrcv_clear_result
Unexecuted instantiation: miscinit.c:walrcv_clear_result
Unexecuted instantiation: postinit.c:walrcv_clear_result
Unexecuted instantiation: guc_tables.c:walrcv_clear_result
491
492
/* prototypes for functions in walreceiver.c */
493
pg_noreturn extern void WalReceiverMain(const void *startup_data, size_t startup_data_len);
494
extern void WalRcvRequestApplyReply(void);
495
496
/* prototypes for functions in walreceiverfuncs.c */
497
extern void ShutdownWalRcv(void);
498
extern bool WalRcvStreaming(void);
499
extern bool WalRcvRunning(void);
500
extern WalRcvState WalRcvGetState(void);
501
extern void RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr,
502
                 const char *conninfo, const char *slotname,
503
                 bool create_temp_slot);
504
extern XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI);
505
extern XLogRecPtr GetWalRcvWriteRecPtr(void);
506
extern int  GetReplicationApplyDelay(void);
507
extern int  GetReplicationTransferLatency(void);
508
509
#endif              /* _WALRECEIVER_H */