1/*-------------------------------------------------------------------------
3 * pg_receivewal.c - receive streaming WAL data and write it
6 * Author: Magnus Hagander <magnus@hagander.net>
8 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
11 * src/bin/pg_basebackup/pg_receivewal.c
12 *-------------------------------------------------------------------------
39/* Time to sleep between reconnection attempts */
40 #define RECONNECT_SLEEP_TIME 5
59static void usage(
void);
65 bool segment_finished);
77 printf(
_(
"%s receives PostgreSQL streaming write-ahead logs.\n\n"),
82 printf(
_(
" -D, --directory=DIR receive write-ahead log files into this directory\n"));
83 printf(
_(
" -E, --endpos=LSN exit after receiving the specified LSN\n"));
84 printf(
_(
" --if-not-exists do not error if slot already exists when creating a slot\n"));
85 printf(
_(
" -n, --no-loop do not loop on connection lost\n"));
86 printf(
_(
" --no-sync do not wait for changes to be written safely to disk\n"));
87 printf(
_(
" -s, --status-interval=SECS\n"
89 printf(
_(
" -S, --slot=SLOTNAME replication slot to use\n"));
90 printf(
_(
" --synchronous flush write-ahead log immediately after writing\n"));
91 printf(
_(
" -v, --verbose output verbose messages\n"));
92 printf(
_(
" -V, --version output version information, then exit\n"));
93 printf(
_(
" -Z, --compress=METHOD[:DETAIL]\n"
94 " compress as specified\n"));
95 printf(
_(
" -?, --help show this help, then exit\n"));
96 printf(
_(
"\nConnection options:\n"));
97 printf(
_(
" -d, --dbname=CONNSTR connection string\n"));
98 printf(
_(
" -h, --host=HOSTNAME database server host or socket directory\n"));
99 printf(
_(
" -p, --port=PORT database server port number\n"));
100 printf(
_(
" -U, --username=NAME connect as specified database user\n"));
101 printf(
_(
" -w, --no-password never prompt for password\n"));
102 printf(
_(
" -W, --password force password prompt (should happen automatically)\n"));
103 printf(
_(
"\nOptional actions:\n"));
104 printf(
_(
" --create-slot create a new replication slot (for the slot's name see --slot)\n"));
105 printf(
_(
" --drop-slot drop the replication slot (for the slot's name see --slot)\n"));
106 printf(
_(
"\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
107 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
112 * Check if the filename looks like a WAL file, letting caller know if this
113 * WAL segment is partial and/or compressed.
119 size_t fname_len = strlen(
filename);
120 size_t xlog_pattern_len = strspn(
filename,
"0123456789ABCDEF");
122 /* File does not look like a WAL file */
126 /* File looks like a completed uncompressed WAL file */
134 /* File looks like a completed gzip-compressed WAL file */
143 /* File looks like a completed LZ4-compressed WAL file */
152 /* File looks like a partial uncompressed WAL file */
161 /* File looks like a partial gzip-compressed WAL file */
170 /* File looks like a partial LZ4-compressed WAL file */
179 /* File does not look like something we know */
186 static uint32 prevtimeline = 0;
189 /* we assume that we get called once at the end of each segment */
190 if (
verbose && segment_finished)
191 pg_log_info(
"finished segment at %X/%08X (timeline %u)",
198 pg_log_info(
"stopped log streaming at %X/%08X (timeline %u)",
206 * Note that we report the previous, not current, position here. After a
207 * timeline switch, xlogpos points to the beginning of the segment because
208 * that's where we always begin streaming. Reporting the end of previous
209 * timeline isn't totally accurate, because the next timeline can begin
210 * slightly before the end of the WAL that we received on the previous
211 * timeline, but it's close enough for reporting purposes.
213 if (
verbose && prevtimeline != 0 && prevtimeline != timeline)
218 prevtimeline = timeline;
232 * Get destination directory.
239 Assert(dest_folder != NULL);
242 pg_fatal(
"could not open directory \"%s\": %m", dest_folder);
249 * Close existing directory.
254 Assert(dest_dir != NULL && dest_folder != NULL);
256 pg_fatal(
"could not close directory \"%s\": %m", dest_folder);
261 * Determine starting location for streaming, based on any existing xlog
262 * segments in the directory. We start at the end of the last one that is
263 * complete (size matches wal segment size), on the timeline with highest ID.
265 * If there are no WAL files in the directory, returns InvalidXLogRecPtr.
274 bool high_ispartial =
false;
286 &ispartial, &wal_compression_algorithm))
290 * Looks like an xlog file. Parse its position.
295 * Check that the segment has the right size, if it's supposed to be
296 * completed. For non-compressed segments just check the on-disk size
297 * and see if it matches a completed segment. For gzip-compressed
298 * segments, look at the last 4 bytes of the compressed file, which is
299 * where the uncompressed size is located for files with a size lower
300 * than 4GB, and then compare it to the size of a completed segment.
301 * The 4 last bytes correspond to the ISIZE member according to
302 * http://www.zlib.org/rfc-gzip.html.
304 * For LZ4-compressed segments, uncompress the file in a throw-away
305 * buffer keeping track of the uncompressed size, then compare it to
306 * the size of a completed segment. Per its protocol, LZ4 does not
307 * store the uncompressed size of an object by default. contentSize
308 * is one possible way to do that, but we need to rely on a method
309 * where WAL segments could have been compressed by a different source
310 * than pg_receivewal, like an archive_command with lz4.
318 if (
stat(fullpath, &statbuf) != 0)
319 pg_fatal(
"could not stat file \"%s\": %m", fullpath);
323 pg_log_warning(
"segment file \"%s\" has incorrect size %lld, skipping",
340 pg_fatal(
"could not open compressed file \"%s\": %m",
342 if (lseek(
fd, (off_t) (-4), SEEK_END) < 0)
343 pg_fatal(
"could not seek in compressed file \"%s\": %m",
346 if (r !=
sizeof(
buf))
349 pg_fatal(
"could not read compressed file \"%s\": %m",
352 pg_fatal(
"could not read compressed file \"%s\": read %d of %zu",
353 fullpath, r,
sizeof(
buf));
357 bytes_out = (
buf[3] << 24) | (
buf[2] << 16) |
362 pg_log_warning(
"compressed segment file \"%s\" has incorrect uncompressed size %d, skipping",
370#define LZ4_CHUNK_SZ 64 * 1024 /* 64kB as maximum chunk size read */
373 size_t uncompressed_size = 0;
377 LZ4F_decompressionContext_t ctx = NULL;
378 LZ4F_decompressOptions_t dec_opt;
379 LZ4F_errorCode_t status;
381 memset(&dec_opt, 0,
sizeof(dec_opt));
386 pg_fatal(
"could not open file \"%s\": %m", fullpath);
388 status = LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION);
389 if (LZ4F_isError(status))
390 pg_fatal(
"could not create LZ4 decompression context: %s",
391 LZ4F_getErrorName(status));
400 r =
read(
fd, readbuf, LZ4_CHUNK_SZ);
402 pg_fatal(
"could not read file \"%s\": %m", fullpath);
404 /* Done reading the file */
408 /* Process one chunk */
410 readend = readbuf + r;
411 while (readp < readend)
413 size_t out_size = LZ4_CHUNK_SZ;
414 size_t read_size = readend - readp;
416 memset(outbuf, 0, LZ4_CHUNK_SZ);
417 status = LZ4F_decompress(ctx, outbuf, &out_size,
418 readp, &read_size, &dec_opt);
419 if (LZ4F_isError(status))
420 pg_fatal(
"could not decompress file \"%s\": %s",
422 LZ4F_getErrorName(status));
425 uncompressed_size += out_size;
429 * No need to continue reading the file when the
430 * uncompressed_size exceeds WalSegSz, even if there are still
431 * data left to read. However, if uncompressed_size is equal
432 * to WalSegSz, it should verify that there is no more data to
435 }
while (uncompressed_size <= WalSegSz && r > 0);
441 status = LZ4F_freeDecompressionContext(ctx);
442 if (LZ4F_isError(status))
443 pg_fatal(
"could not free LZ4 decompression context: %s",
444 LZ4F_getErrorName(status));
448 pg_log_warning(
"compressed segment file \"%s\" has incorrect uncompressed size %zu, skipping",
453 pg_log_error(
"cannot check file \"%s\": compression with %s not supported by this build",
459 /* Looks like a valid segment. Remember that we saw it. */
460 if ((segno > high_segno) ||
461 (segno == high_segno && tli > high_tli) ||
462 (segno == high_segno && tli == high_tli && high_ispartial && !ispartial))
466 high_ispartial = ispartial;
480 * Move the starting pointer to the start of the next segment, if the
481 * highest one we saw was completed. Otherwise start streaming from
482 * the beginning of the .partial segment.
497 * Start the log streaming
508 * Connect in replication mode to the server
513 /* Error message already written in GetConnection() */
519 * Error message already written in CheckServerVersionForStreaming().
520 * There's no hope of recovering from a version mismatch, so don't
527 * Identify server, obtaining start LSN position and current timeline ID
528 * at the same time, necessary if not valid data can be found in the
529 * existing output directory.
535 * Figure out where to start streaming. First scan the local directory.
541 * Try to get the starting point from the slot if any. This is
542 * supported in PostgreSQL 15 and newer.
550 /* Error is logged by GetSlotInformation() */
556 * If it the starting point is still not known, use the current WAL
557 * flush value as last resort.
570 * Always start streaming at the beginning of a segment
575 * Start the replication
578 pg_log_info(
"starting log streaming at %X/%08X (timeline %u)",
600 pg_log_info(
"could not finish writing WAL files: %m");
611 * When SIGINT/SIGTERM are caught, just tell the system to exit at the next
626 static struct option long_options[] = {
657 char *compression_detail = NULL;
658 char *compression_algorithm_str =
"none";
659 char *error_detail = NULL;
667 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
672 else if (strcmp(argv[1],
"-V") == 0 ||
673 strcmp(argv[1],
"--version") == 0)
675 puts(
"pg_receivewal (PostgreSQL) " PG_VERSION);
680 while ((
c =
getopt_long(argc, argv,
"d:D:E:h:np:s:S:U:vwWZ:",
681 long_options, &option_index)) != -1)
692 if (sscanf(
optarg,
"%X/%08X", &hi, &lo) != 2)
729 &compression_detail);
747 /* getopt_long already emitted a complaint */
754 * Any non-option arguments?
758 pg_log_error(
"too many command-line arguments (first is \"%s\")",
766 pg_log_error(
"cannot use --create-slot together with --drop-slot");
773 /* translator: second %s is an option name */
774 pg_log_error(
"%s needs a slot to be specified using --slot",
782 pg_log_error(
"cannot use --synchronous together with --no-sync");
798 * Compression options
802 pg_fatal(
"unrecognized compression algorithm: \"%s\"",
803 compression_algorithm_str);
808 if (error_detail != NULL)
809 pg_fatal(
"invalid compression specification: %s",
812 /* Extract the compression level */
816 pg_fatal(
"compression with %s is not yet supported",
"ZSTD");
819 * Check existence of destination folder.
829 * Obtain a connection before doing anything.
833 /* error message already written in GetConnection() */
838 * Trap signals. (Don't do this until after the initial password prompt,
839 * if one is needed, in GetConnection.)
847 * Run IDENTIFY_SYSTEM to make sure we've successfully have established a
848 * replication connection and haven't connected using a database specific
855 * Check that there is a database associated with connection, none should
856 * be defined in this context.
859 pg_fatal(
"replication connection using slot \"%s\" is unexpectedly database specific",
863 * Set umask so that directories/files are created with the same
864 * permissions as directories/files in the source data directory.
866 * pg_mode_mask is set to owner-only by default and then updated in
867 * GetConnection() where we get the mode from the server-side with
868 * RetrieveDataDirCreatePerm() and then call SetDataDirectoryCreatePerm().
873 * Drop a replication slot.
885 /* Create a replication slot */
897 /* determine remote server's xlog segment size */
902 * Don't close the connection here so that subsequent StreamLog() can
912 * We've been Ctrl-C'ed or end of streaming position has been
913 * willingly reached, so exit without an error code.
921 /* translator: check source for value for %d */
922 pg_log_info(
"disconnected; waiting %d seconds to try again",
#define PG_TEXTDOMAIN(domain)
void set_pglocale_pgservice(const char *argv0, const char *app)
char * validate_compress_specification(pg_compress_specification *spec)
bool parse_compress_algorithm(char *name, pg_compress_algorithm *algorithm)
void parse_compress_specification(pg_compress_algorithm algorithm, char *specification, pg_compress_specification *result)
void parse_compress_options(const char *option, char **algorithm, char **detail)
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state)
struct dirent * readdir(DIR *)
DIR * opendir(const char *)
int PQserverVersion(const PGconn *conn)
void PQfinish(PGconn *conn)
char * pg_strdup(const char *in)
void * pg_malloc0(size_t size)
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
Assert(PointerIsAligned(start, uint64))
void pg_logging_init(const char *argv0)
#define pg_log_error(...)
#define pg_log_error_hint(...)
bool option_parse_int(const char *optarg, const char *optname, int min_range, int max_range, int *result)
PGDLLIMPORT char * optarg
static void close_destination_dir(DIR *dest_dir, char *dest_folder)
static pg_compress_algorithm compression_algorithm
int main(int argc, char **argv)
static volatile sig_atomic_t time_to_stop
static bool slot_exists_ok
static DIR * get_destination_dir(char *dest_folder)
static bool stop_streaming(XLogRecPtr xlogpos, uint32 timeline, bool segment_finished)
static char * replication_slot
static XLogRecPtr FindStreamingStart(uint32 *tli)
static void sigexit_handler(SIGNAL_ARGS)
#define RECONNECT_SLEEP_TIME
static bool do_create_slot
static void disconnect_atexit(void)
static int standby_message_timeout
static bool is_xlogfilename(const char *filename, bool *ispartial, pg_compress_algorithm *wal_compression_algorithm)
static void StreamLog(void)
#define pg_log_warning(...)
const char * get_progname(const char *argv0)
static int fd(const char *x, int i)
bool ReceiveXlogStream(PGconn *conn, StreamCtl *stream)
bool CheckServerVersionForStreaming(PGconn *conn)
void pg_usleep(long microsec)
bool RetrieveWalSegSize(PGconn *conn)
bool GetSlotInformation(PGconn *conn, const char *slot_name, XLogRecPtr *restart_lsn, TimeLineID *restart_tli)
bool RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
stream_stop_callback stream_stop
int standby_message_timeout
WalWriteMethod * walmethod
void(* free)(WalWriteMethod *wwmethod)
bool(* finish)(WalWriteMethod *wwmethod)
const WalWriteMethodOps * ops
WalWriteMethod * CreateWalDirectoryMethod(const char *basedir, pg_compress_algorithm compression_algorithm, int compression_level, bool sync)
static void CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
static void DropReplicationSlot(DropReplicationSlotCmd *cmd)
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
static void XLogFromFileName(const char *fname, TimeLineID *tli, XLogSegNo *logSegNo, int wal_segsz_bytes)
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
#define LSN_FORMAT_ARGS(lsn)
#define XLogRecPtrIsInvalid(r)
#define InvalidXLogRecPtr