1 /*-------------------------------------------------------------------------
3 * pg_receivewal.c - receive streaming WAL data and write it
6 * Author: Magnus Hagander <magnus@hagander.net>
8 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
11 * src/bin/pg_basebackup/pg_receivewal.c
12 *-------------------------------------------------------------------------
15 #include "postgres_fe.h"
30 #include "access/xlog_internal.h"
31 #include "common/file_perm.h"
32 #include "common/logging.h"
33 #include "fe_utils/option_utils.h"
34 #include "getopt_long.h"
36 #include "receivelog.h"
37 #include "streamutil.h"
39 /* Time to sleep between reconnection attempts */
40 #define RECONNECT_SLEEP_TIME 5
43 static char *basedir
= NULL
;
44 static int verbose
= 0;
45 static int compresslevel
= 0;
46 static int noloop
= 0;
47 static int standby_message_timeout
= 10 * 1000; /* 10 sec = default */
48 static volatile sig_atomic_t time_to_stop
= false;
49 static bool do_create_slot
= false;
50 static bool slot_exists_ok
= false;
51 static bool do_drop_slot
= false;
52 static bool do_sync
= true;
53 static bool synchronous
= false;
54 static char *replication_slot
= NULL
;
55 static pg_compress_algorithm compression_algorithm
= PG_COMPRESSION_NONE
;
56 static XLogRecPtr endpos
= InvalidXLogRecPtr
;
59 static void usage(void);
60 static void parse_compress_options(char *option
, char **algorithm
,
62 static DIR *get_destination_dir(char *dest_folder
);
63 static void close_destination_dir(DIR *dest_dir
, char *dest_folder
);
64 static XLogRecPtr
FindStreamingStart(uint32
*tli
);
65 static void StreamLog(void);
66 static bool stop_streaming(XLogRecPtr xlogpos
, uint32 timeline
,
67 bool segment_finished
);
70 disconnect_atexit(void)
79 printf(_("%s receives PostgreSQL streaming write-ahead logs.\n\n"),
81 printf(_("Usage:\n"));
82 printf(_(" %s [OPTION]...\n"), progname
);
83 printf(_("\nOptions:\n"));
84 printf(_(" -D, --directory=DIR receive write-ahead log files into this directory\n"));
85 printf(_(" -E, --endpos=LSN exit after receiving the specified LSN\n"));
86 printf(_(" --if-not-exists do not error if slot already exists when creating a slot\n"));
87 printf(_(" -n, --no-loop do not loop on connection lost\n"));
88 printf(_(" --no-sync do not wait for changes to be written safely to disk\n"));
89 printf(_(" -s, --status-interval=SECS\n"
90 " time between status packets sent to server (default: %d)\n"), (standby_message_timeout
/ 1000));
91 printf(_(" -S, --slot=SLOTNAME replication slot to use\n"));
92 printf(_(" --synchronous flush write-ahead log immediately after writing\n"));
93 printf(_(" -v, --verbose output verbose messages\n"));
94 printf(_(" -V, --version output version information, then exit\n"));
95 printf(_(" -Z, --compress=METHOD[:DETAIL]\n"
96 " compress as specified\n"));
97 printf(_(" -?, --help show this help, then exit\n"));
98 printf(_("\nConnection options:\n"));
99 printf(_(" -d, --dbname=CONNSTR connection string\n"));
100 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
101 printf(_(" -p, --port=PORT database server port number\n"));
102 printf(_(" -U, --username=NAME connect as specified database user\n"));
103 printf(_(" -w, --no-password never prompt for password\n"));
104 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
105 printf(_("\nOptional actions:\n"));
106 printf(_(" --create-slot create a new replication slot (for the slot's name see --slot)\n"));
107 printf(_(" --drop-slot drop the replication slot (for the slot's name see --slot)\n"));
108 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
109 printf(_("%s home page: <%s>\n"), PACKAGE_NAME
, PACKAGE_URL
);
113 * Basic parsing of a value specified for -Z/--compress
115 * The parsing consists of a METHOD:DETAIL string fed later on to a more
116 * advanced routine in charge of proper validation checks. This only extracts
117 * METHOD and DETAIL. If only an integer is found, the method is implied by
118 * the value specified.
121 parse_compress_options(char *option
, char **algorithm
, char **detail
)
128 * Check whether the compression specification consists of a bare integer.
130 * For backward-compatibility, assume "none" if the integer found is zero
131 * and "gzip" otherwise.
133 result
= strtol(option
, &endp
, 10);
138 *algorithm
= pstrdup("none");
143 *algorithm
= pstrdup("gzip");
144 *detail
= pstrdup(option
);
150 * Check whether there is a compression detail following the algorithm
153 sep
= strchr(option
, ':');
156 *algorithm
= pstrdup(option
);
163 alg
= palloc((sep
- option
) + 1);
164 memcpy(alg
, option
, sep
- option
);
165 alg
[sep
- option
] = '\0';
168 *detail
= pstrdup(sep
+ 1);
173 * Check if the filename looks like a WAL file, letting caller know if this
174 * WAL segment is partial and/or compressed.
177 is_xlogfilename(const char *filename
, bool *ispartial
,
178 pg_compress_algorithm
*wal_compression_algorithm
)
180 size_t fname_len
= strlen(filename
);
181 size_t xlog_pattern_len
= strspn(filename
, "0123456789ABCDEF");
183 /* File does not look like a WAL file */
184 if (xlog_pattern_len
!= XLOG_FNAME_LEN
)
187 /* File looks like a completed uncompressed WAL file */
188 if (fname_len
== XLOG_FNAME_LEN
)
191 *wal_compression_algorithm
= PG_COMPRESSION_NONE
;
195 /* File looks like a completed gzip-compressed WAL file */
196 if (fname_len
== XLOG_FNAME_LEN
+ strlen(".gz") &&
197 strcmp(filename
+ XLOG_FNAME_LEN
, ".gz") == 0)
200 *wal_compression_algorithm
= PG_COMPRESSION_GZIP
;
204 /* File looks like a completed LZ4-compressed WAL file */
205 if (fname_len
== XLOG_FNAME_LEN
+ strlen(".lz4") &&
206 strcmp(filename
+ XLOG_FNAME_LEN
, ".lz4") == 0)
209 *wal_compression_algorithm
= PG_COMPRESSION_LZ4
;
213 /* File looks like a partial uncompressed WAL file */
214 if (fname_len
== XLOG_FNAME_LEN
+ strlen(".partial") &&
215 strcmp(filename
+ XLOG_FNAME_LEN
, ".partial") == 0)
218 *wal_compression_algorithm
= PG_COMPRESSION_NONE
;
222 /* File looks like a partial gzip-compressed WAL file */
223 if (fname_len
== XLOG_FNAME_LEN
+ strlen(".gz.partial") &&
224 strcmp(filename
+ XLOG_FNAME_LEN
, ".gz.partial") == 0)
227 *wal_compression_algorithm
= PG_COMPRESSION_GZIP
;
231 /* File looks like a partial LZ4-compressed WAL file */
232 if (fname_len
== XLOG_FNAME_LEN
+ strlen(".lz4.partial") &&
233 strcmp(filename
+ XLOG_FNAME_LEN
, ".lz4.partial") == 0)
236 *wal_compression_algorithm
= PG_COMPRESSION_LZ4
;
240 /* File does not look like something we know */
245 stop_streaming(XLogRecPtr xlogpos
, uint32 timeline
, bool segment_finished
)
247 static uint32 prevtimeline
= 0;
248 static XLogRecPtr prevpos
= InvalidXLogRecPtr
;
250 /* we assume that we get called once at the end of each segment */
251 if (verbose
&& segment_finished
)
252 pg_log_info("finished segment at %X/%X (timeline %u)",
253 LSN_FORMAT_ARGS(xlogpos
),
256 if (!XLogRecPtrIsInvalid(endpos
) && endpos
< xlogpos
)
259 pg_log_info("stopped log streaming at %X/%X (timeline %u)",
260 LSN_FORMAT_ARGS(xlogpos
),
267 * Note that we report the previous, not current, position here. After a
268 * timeline switch, xlogpos points to the beginning of the segment because
269 * that's where we always begin streaming. Reporting the end of previous
270 * timeline isn't totally accurate, because the next timeline can begin
271 * slightly before the end of the WAL that we received on the previous
272 * timeline, but it's close enough for reporting purposes.
274 if (verbose
&& prevtimeline
!= 0 && prevtimeline
!= timeline
)
275 pg_log_info("switched to timeline %u at %X/%X",
277 LSN_FORMAT_ARGS(prevpos
));
279 prevtimeline
= timeline
;
285 pg_log_info("received interrupt signal, exiting");
293 * Get destination directory.
296 get_destination_dir(char *dest_folder
)
300 Assert(dest_folder
!= NULL
);
301 dir
= opendir(dest_folder
);
303 pg_fatal("could not open directory \"%s\": %m", dest_folder
);
310 * Close existing directory.
313 close_destination_dir(DIR *dest_dir
, char *dest_folder
)
315 Assert(dest_dir
!= NULL
&& dest_folder
!= NULL
);
316 if (closedir(dest_dir
))
317 pg_fatal("could not close directory \"%s\": %m", dest_folder
);
322 * Determine starting location for streaming, based on any existing xlog
323 * segments in the directory. We start at the end of the last one that is
324 * complete (size matches wal segment size), on the timeline with highest ID.
326 * If there are no WAL files in the directory, returns InvalidXLogRecPtr.
329 FindStreamingStart(uint32
*tli
)
332 struct dirent
*dirent
;
333 XLogSegNo high_segno
= 0;
335 bool high_ispartial
= false;
337 dir
= get_destination_dir(basedir
);
339 while (errno
= 0, (dirent
= readdir(dir
)) != NULL
)
343 pg_compress_algorithm wal_compression_algorithm
;
346 if (!is_xlogfilename(dirent
->d_name
,
347 &ispartial
, &wal_compression_algorithm
))
351 * Looks like an xlog file. Parse its position.
353 XLogFromFileName(dirent
->d_name
, &tli
, &segno
, WalSegSz
);
356 * Check that the segment has the right size, if it's supposed to be
357 * completed. For non-compressed segments just check the on-disk size
358 * and see if it matches a completed segment. For gzip-compressed
359 * segments, look at the last 4 bytes of the compressed file, which is
360 * where the uncompressed size is located for files with a size lower
361 * than 4GB, and then compare it to the size of a completed segment.
362 * The 4 last bytes correspond to the ISIZE member according to
363 * http://www.zlib.org/rfc-gzip.html.
365 * For LZ4-compressed segments, uncompress the file in a throw-away
366 * buffer keeping track of the uncompressed size, then compare it to
367 * the size of a completed segment. Per its protocol, LZ4 does not
368 * store the uncompressed size of an object by default. contentSize
369 * is one possible way to do that, but we need to rely on a method
370 * where WAL segments could have been compressed by a different source
371 * than pg_receivewal, like an archive_command with lz4.
373 if (!ispartial
&& wal_compression_algorithm
== PG_COMPRESSION_NONE
)
376 char fullpath
[MAXPGPATH
* 2];
378 snprintf(fullpath
, sizeof(fullpath
), "%s/%s", basedir
, dirent
->d_name
);
379 if (stat(fullpath
, &statbuf
) != 0)
380 pg_fatal("could not stat file \"%s\": %m", fullpath
);
382 if (statbuf
.st_size
!= WalSegSz
)
384 pg_log_warning("segment file \"%s\" has incorrect size %lld, skipping",
385 dirent
->d_name
, (long long int) statbuf
.st_size
);
389 else if (!ispartial
&& wal_compression_algorithm
== PG_COMPRESSION_GZIP
)
394 char fullpath
[MAXPGPATH
* 2];
397 snprintf(fullpath
, sizeof(fullpath
), "%s/%s", basedir
, dirent
->d_name
);
399 fd
= open(fullpath
, O_RDONLY
| PG_BINARY
, 0);
401 pg_fatal("could not open compressed file \"%s\": %m",
403 if (lseek(fd
, (off_t
) (-4), SEEK_END
) < 0)
404 pg_fatal("could not seek in compressed file \"%s\": %m",
406 r
= read(fd
, (char *) buf
, sizeof(buf
));
407 if (r
!= sizeof(buf
))
410 pg_fatal("could not read compressed file \"%s\": %m",
413 pg_fatal("could not read compressed file \"%s\": read %d of %zu",
414 fullpath
, r
, sizeof(buf
));
418 bytes_out
= (buf
[3] << 24) | (buf
[2] << 16) |
419 (buf
[1] << 8) | buf
[0];
421 if (bytes_out
!= WalSegSz
)
423 pg_log_warning("compressed segment file \"%s\" has incorrect uncompressed size %d, skipping",
424 dirent
->d_name
, bytes_out
);
428 else if (!ispartial
&& wal_compression_algorithm
== PG_COMPRESSION_LZ4
)
431 #define LZ4_CHUNK_SZ 64 * 1024 /* 64kB as maximum chunk size read */
434 size_t uncompressed_size
= 0;
435 char fullpath
[MAXPGPATH
* 2];
438 LZ4F_decompressionContext_t ctx
= NULL
;
439 LZ4F_decompressOptions_t dec_opt
;
440 LZ4F_errorCode_t status
;
442 memset(&dec_opt
, 0, sizeof(dec_opt
));
443 snprintf(fullpath
, sizeof(fullpath
), "%s/%s", basedir
, dirent
->d_name
);
445 fd
= open(fullpath
, O_RDONLY
| PG_BINARY
, 0);
447 pg_fatal("could not open file \"%s\": %m", fullpath
);
449 status
= LZ4F_createDecompressionContext(&ctx
, LZ4F_VERSION
);
450 if (LZ4F_isError(status
))
451 pg_fatal("could not create LZ4 decompression context: %s",
452 LZ4F_getErrorName(status
));
454 outbuf
= pg_malloc0(LZ4_CHUNK_SZ
);
455 readbuf
= pg_malloc0(LZ4_CHUNK_SZ
);
461 r
= read(fd
, readbuf
, LZ4_CHUNK_SZ
);
463 pg_fatal("could not read file \"%s\": %m", fullpath
);
465 /* Done reading the file */
469 /* Process one chunk */
471 readend
= readbuf
+ r
;
472 while (readp
< readend
)
474 size_t out_size
= LZ4_CHUNK_SZ
;
475 size_t read_size
= readend
- readp
;
477 memset(outbuf
, 0, LZ4_CHUNK_SZ
);
478 status
= LZ4F_decompress(ctx
, outbuf
, &out_size
,
479 readp
, &read_size
, &dec_opt
);
480 if (LZ4F_isError(status
))
481 pg_fatal("could not decompress file \"%s\": %s",
483 LZ4F_getErrorName(status
));
486 uncompressed_size
+= out_size
;
490 * No need to continue reading the file when the
491 * uncompressed_size exceeds WalSegSz, even if there are still
492 * data left to read. However, if uncompressed_size is equal
493 * to WalSegSz, it should verify that there is no more data to
496 } while (uncompressed_size
<= WalSegSz
&& r
> 0);
502 status
= LZ4F_freeDecompressionContext(ctx
);
503 if (LZ4F_isError(status
))
504 pg_fatal("could not free LZ4 decompression context: %s",
505 LZ4F_getErrorName(status
));
507 if (uncompressed_size
!= WalSegSz
)
509 pg_log_warning("compressed segment file \"%s\" has incorrect uncompressed size %zu, skipping",
510 dirent
->d_name
, uncompressed_size
);
514 pg_log_error("could not check file \"%s\"",
516 pg_log_error_detail("This build does not support compression with %s.",
522 /* Looks like a valid segment. Remember that we saw it. */
523 if ((segno
> high_segno
) ||
524 (segno
== high_segno
&& tli
> high_tli
) ||
525 (segno
== high_segno
&& tli
== high_tli
&& high_ispartial
&& !ispartial
))
529 high_ispartial
= ispartial
;
534 pg_fatal("could not read directory \"%s\": %m", basedir
);
536 close_destination_dir(dir
, basedir
);
543 * Move the starting pointer to the start of the next segment, if the
544 * highest one we saw was completed. Otherwise start streaming from
545 * the beginning of the .partial segment.
550 XLogSegNoOffsetToRecPtr(high_segno
, 0, WalSegSz
, high_ptr
);
556 return InvalidXLogRecPtr
;
560 * Start the log streaming
565 XLogRecPtr serverpos
;
566 TimeLineID servertli
;
567 StreamCtl stream
= {0};
571 * Connect in replication mode to the server
574 conn
= GetConnection();
576 /* Error message already written in GetConnection() */
579 if (!CheckServerVersionForStreaming(conn
))
582 * Error message already written in CheckServerVersionForStreaming().
583 * There's no hope of recovering from a version mismatch, so don't
590 * Identify server, obtaining start LSN position and current timeline ID
591 * at the same time, necessary if not valid data can be found in the
592 * existing output directory.
594 if (!RunIdentifySystem(conn
, &sysidentifier
, &servertli
, &serverpos
, NULL
))
598 * Figure out where to start streaming. First scan the local directory.
600 stream
.startpos
= FindStreamingStart(&stream
.timeline
);
601 if (stream
.startpos
== InvalidXLogRecPtr
)
604 * Try to get the starting point from the slot if any. This is
605 * supported in PostgreSQL 15 and newer.
607 if (replication_slot
!= NULL
&&
608 PQserverVersion(conn
) >= 150000)
610 if (!GetSlotInformation(conn
, replication_slot
, &stream
.startpos
,
613 /* Error is logged by GetSlotInformation() */
619 * If it the starting point is still not known, use the current WAL
620 * flush value as last resort.
622 if (stream
.startpos
== InvalidXLogRecPtr
)
624 stream
.startpos
= serverpos
;
625 stream
.timeline
= servertli
;
629 Assert(stream
.startpos
!= InvalidXLogRecPtr
&&
630 stream
.timeline
!= 0);
633 * Always start streaming at the beginning of a segment
635 stream
.startpos
-= XLogSegmentOffset(stream
.startpos
, WalSegSz
);
638 * Start the replication
641 pg_log_info("starting log streaming at %X/%X (timeline %u)",
642 LSN_FORMAT_ARGS(stream
.startpos
),
645 stream
.stream_stop
= stop_streaming
;
646 stream
.stop_socket
= PGINVALID_SOCKET
;
647 stream
.standby_message_timeout
= standby_message_timeout
;
648 stream
.synchronous
= synchronous
;
649 stream
.do_sync
= do_sync
;
650 stream
.mark_done
= false;
651 stream
.walmethod
= CreateWalDirectoryMethod(basedir
,
652 compression_algorithm
,
655 stream
.partial_suffix
= ".partial";
656 stream
.replication_slot
= replication_slot
;
657 stream
.sysidentifier
= sysidentifier
;
659 ReceiveXlogStream(conn
, &stream
);
661 if (!stream
.walmethod
->ops
->finish(stream
.walmethod
))
663 pg_log_info("could not finish writing WAL files: %m");
670 stream
.walmethod
->ops
->free(stream
.walmethod
);
674 * When SIGINT/SIGTERM are caught, just tell the system to exit at the next
680 sigexit_handler(SIGNAL_ARGS
)
687 main(int argc
, char **argv
)
689 static struct option long_options
[] = {
690 {"help", no_argument
, NULL
, '?'},
691 {"version", no_argument
, NULL
, 'V'},
692 {"directory", required_argument
, NULL
, 'D'},
693 {"dbname", required_argument
, NULL
, 'd'},
694 {"endpos", required_argument
, NULL
, 'E'},
695 {"host", required_argument
, NULL
, 'h'},
696 {"port", required_argument
, NULL
, 'p'},
697 {"username", required_argument
, NULL
, 'U'},
698 {"no-loop", no_argument
, NULL
, 'n'},
699 {"no-password", no_argument
, NULL
, 'w'},
700 {"password", no_argument
, NULL
, 'W'},
701 {"status-interval", required_argument
, NULL
, 's'},
702 {"slot", required_argument
, NULL
, 'S'},
703 {"verbose", no_argument
, NULL
, 'v'},
704 {"compress", required_argument
, NULL
, 'Z'},
706 {"create-slot", no_argument
, NULL
, 1},
707 {"drop-slot", no_argument
, NULL
, 2},
708 {"if-not-exists", no_argument
, NULL
, 3},
709 {"synchronous", no_argument
, NULL
, 4},
710 {"no-sync", no_argument
, NULL
, 5},
719 pg_compress_specification compression_spec
;
720 char *compression_detail
= NULL
;
721 char *compression_algorithm_str
= "none";
722 char *error_detail
= NULL
;
724 pg_logging_init(argv
[0]);
725 progname
= get_progname(argv
[0]);
726 set_pglocale_pgservice(argv
[0], PG_TEXTDOMAIN("pg_basebackup"));
730 if (strcmp(argv
[1], "--help") == 0 || strcmp(argv
[1], "-?") == 0)
735 else if (strcmp(argv
[1], "-V") == 0 ||
736 strcmp(argv
[1], "--version") == 0)
738 puts("pg_receivewal (PostgreSQL) " PG_VERSION
);
743 while ((c
= getopt_long(argc
, argv
, "D:d:E:h:p:U:s:S:nwWvZ:",
744 long_options
, &option_index
)) != -1)
749 basedir
= pg_strdup(optarg
);
752 connection_string
= pg_strdup(optarg
);
755 dbhost
= pg_strdup(optarg
);
758 dbport
= pg_strdup(optarg
);
761 dbuser
= pg_strdup(optarg
);
770 if (!option_parse_int(optarg
, "-s/--status-interval", 0,
772 &standby_message_timeout
))
774 standby_message_timeout
*= 1000;
777 replication_slot
= pg_strdup(optarg
);
780 if (sscanf(optarg
, "%X/%X", &hi
, &lo
) != 2)
781 pg_fatal("could not parse end position \"%s\"", optarg
);
782 endpos
= ((uint64
) hi
) << 32 | lo
;
791 parse_compress_options(optarg
, &compression_algorithm_str
,
792 &compression_detail
);
796 do_create_slot
= true;
802 slot_exists_ok
= true;
811 /* getopt_long already emitted a complaint */
812 pg_log_error_hint("Try \"%s --help\" for more information.", progname
);
818 * Any non-option arguments?
822 pg_log_error("too many command-line arguments (first is \"%s\")",
824 pg_log_error_hint("Try \"%s --help\" for more information.", progname
);
828 if (do_drop_slot
&& do_create_slot
)
830 pg_log_error("cannot use --create-slot together with --drop-slot");
831 pg_log_error_hint("Try \"%s --help\" for more information.", progname
);
835 if (replication_slot
== NULL
&& (do_drop_slot
|| do_create_slot
))
837 /* translator: second %s is an option name */
838 pg_log_error("%s needs a slot to be specified using --slot",
839 do_drop_slot
? "--drop-slot" : "--create-slot");
840 pg_log_error_hint("Try \"%s --help\" for more information.", progname
);
844 if (synchronous
&& !do_sync
)
846 pg_log_error("cannot use --synchronous together with --no-sync");
847 pg_log_error_hint("Try \"%s --help\" for more information.", progname
);
854 if (basedir
== NULL
&& !do_drop_slot
&& !do_create_slot
)
856 pg_log_error("no target directory specified");
857 pg_log_error_hint("Try \"%s --help\" for more information.", progname
);
862 * Compression options
864 if (!parse_compress_algorithm(compression_algorithm_str
,
865 &compression_algorithm
))
866 pg_fatal("unrecognized compression algorithm \"%s\"",
867 compression_algorithm_str
);
869 parse_compress_specification(compression_algorithm
, compression_detail
,
871 error_detail
= validate_compress_specification(&compression_spec
);
872 if (error_detail
!= NULL
)
873 pg_fatal("invalid compression specification: %s",
876 /* Extract the compression level */
877 compresslevel
= compression_spec
.level
;
879 if (compression_algorithm
== PG_COMPRESSION_ZSTD
)
880 pg_fatal("compression with %s is not yet supported", "ZSTD");
883 * Check existence of destination folder.
885 if (!do_drop_slot
&& !do_create_slot
)
887 DIR *dir
= get_destination_dir(basedir
);
889 close_destination_dir(dir
, basedir
);
893 * Obtain a connection before doing anything.
895 conn
= GetConnection();
897 /* error message already written in GetConnection() */
899 atexit(disconnect_atexit
);
902 * Trap signals. (Don't do this until after the initial password prompt,
903 * if one is needed, in GetConnection.)
906 pqsignal(SIGINT
, sigexit_handler
);
907 pqsignal(SIGTERM
, sigexit_handler
);
911 * Run IDENTIFY_SYSTEM to make sure we've successfully have established a
912 * replication connection and haven't connected using a database specific
915 if (!RunIdentifySystem(conn
, NULL
, NULL
, NULL
, &db_name
))
919 * Check that there is a database associated with connection, none should
920 * be defined in this context.
923 pg_fatal("replication connection using slot \"%s\" is unexpectedly database specific",
927 * Set umask so that directories/files are created with the same
928 * permissions as directories/files in the source data directory.
930 * pg_mode_mask is set to owner-only by default and then updated in
931 * GetConnection() where we get the mode from the server-side with
932 * RetrieveDataDirCreatePerm() and then call SetDataDirectoryCreatePerm().
937 * Drop a replication slot.
942 pg_log_info("dropping replication slot \"%s\"", replication_slot
);
944 if (!DropReplicationSlot(conn
, replication_slot
))
949 /* Create a replication slot */
953 pg_log_info("creating replication slot \"%s\"", replication_slot
);
955 if (!CreateReplicationSlot(conn
, replication_slot
, NULL
, false, true, false,
956 slot_exists_ok
, false))
961 /* determine remote server's xlog segment size */
962 if (!RetrieveWalSegSize(conn
))
966 * Don't close the connection here so that subsequent StreamLog() can
976 * We've been Ctrl-C'ed or end of streaming position has been
977 * willingly reached, so exit without an error code.
982 pg_fatal("disconnected");
985 /* translator: check source for value for %d */
986 pg_log_info("disconnected; waiting %d seconds to try again",
987 RECONNECT_SLEEP_TIME
);
988 pg_usleep(RECONNECT_SLEEP_TIME
* 1000000);