/Users/deen/code/yugabyte-db/src/postgres/src/bin/pg_basebackup/pg_receivewal.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*------------------------------------------------------------------------- |
2 | | * |
3 | | * pg_receivewal.c - receive streaming WAL data and write it |
4 | | * to a local file. |
5 | | * |
6 | | * Author: Magnus Hagander <magnus@hagander.net> |
7 | | * |
8 | | * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group |
9 | | * |
10 | | * IDENTIFICATION |
11 | | * src/bin/pg_basebackup/pg_receivewal.c |
12 | | *------------------------------------------------------------------------- |
13 | | */ |
14 | | |
15 | | #include "postgres_fe.h" |
16 | | |
17 | | #include <dirent.h> |
18 | | #include <signal.h> |
19 | | #include <sys/stat.h> |
20 | | #include <unistd.h> |
21 | | |
22 | | #include "common/file_perm.h" |
23 | | #include "libpq-fe.h" |
24 | | #include "access/xlog_internal.h" |
25 | | #include "getopt_long.h" |
26 | | |
27 | | #include "receivelog.h" |
28 | | #include "streamutil.h" |
29 | | |
30 | | |
31 | | /* Time to sleep between reconnection attempts */ |
32 | | #define RECONNECT_SLEEP_TIME 5 |
33 | | |
34 | | /* Global options */ |
35 | | static char *basedir = NULL; |
36 | | static int verbose = 0; |
37 | | static int compresslevel = 0; |
38 | | static int noloop = 0; |
39 | | static int standby_message_timeout = 10 * 1000; /* 10 sec = default */ |
40 | | static volatile bool time_to_stop = false; |
41 | | static bool do_create_slot = false; |
42 | | static bool slot_exists_ok = false; |
43 | | static bool do_drop_slot = false; |
44 | | static bool do_sync = true; |
45 | | static bool synchronous = false; |
46 | | static char *replication_slot = NULL; |
47 | | static XLogRecPtr endpos = InvalidXLogRecPtr; |
48 | | |
49 | | |
50 | | static void usage(void); |
51 | | static DIR *get_destination_dir(char *dest_folder); |
52 | | static void close_destination_dir(DIR *dest_dir, char *dest_folder); |
53 | | static XLogRecPtr FindStreamingStart(uint32 *tli); |
54 | | static void StreamLog(void); |
55 | | static bool stop_streaming(XLogRecPtr segendpos, uint32 timeline, |
56 | | bool segment_finished); |
57 | | |
58 | | #define disconnect_and_exit(code) \ |
59 | 0 | { \ |
60 | 0 | if (conn != NULL) PQfinish(conn); \ |
61 | 0 | exit(code); \ |
62 | 0 | } |
63 | | |
64 | | /* Routines to evaluate segment file format */ |
65 | | #define IsCompressXLogFileName(fname) \ |
66 | 0 | (strlen(fname) == XLOG_FNAME_LEN + strlen(".gz") && \ |
67 | 0 | strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN && \ |
68 | 0 | strcmp((fname) + XLOG_FNAME_LEN, ".gz") == 0) |
69 | | #define IsPartialCompressXLogFileName(fname) \ |
70 | 0 | (strlen(fname) == XLOG_FNAME_LEN + strlen(".gz.partial") && \ |
71 | 0 | strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN && \ |
72 | 0 | strcmp((fname) + XLOG_FNAME_LEN, ".gz.partial") == 0) |
73 | | |
74 | | static void |
75 | | usage(void) |
76 | 0 | { |
77 | 0 | printf(_("%s receives PostgreSQL streaming write-ahead logs.\n\n"), |
78 | 0 | progname); |
79 | 0 | printf(_("Usage:\n")); |
80 | 0 | printf(_(" %s [OPTION]...\n"), progname); |
81 | 0 | printf(_("\nOptions:\n")); |
82 | 0 | printf(_(" -D, --directory=DIR receive write-ahead log files into this directory\n")); |
83 | 0 | printf(_(" -E, --endpos=LSN exit after receiving the specified LSN\n")); |
84 | 0 | printf(_(" --if-not-exists do not error if slot already exists when creating a slot\n")); |
85 | 0 | printf(_(" -n, --no-loop do not loop on connection lost\n")); |
86 | 0 | printf(_(" --no-sync do not wait for changes to be written safely to disk\n")); |
87 | 0 | printf(_(" -s, --status-interval=SECS\n" |
88 | 0 | " time between status packets sent to server (default: %d)\n"), (standby_message_timeout / 1000)); |
89 | 0 | printf(_(" -S, --slot=SLOTNAME replication slot to use\n")); |
90 | 0 | printf(_(" --synchronous flush write-ahead log immediately after writing\n")); |
91 | 0 | printf(_(" -v, --verbose output verbose messages\n")); |
92 | 0 | printf(_(" -V, --version output version information, then exit\n")); |
93 | 0 | printf(_(" -Z, --compress=0-9 compress logs with given compression level\n")); |
94 | 0 | printf(_(" -?, --help show this help, then exit\n")); |
95 | 0 | printf(_("\nConnection options:\n")); |
96 | 0 | printf(_(" -d, --dbname=CONNSTR connection string\n")); |
97 | 0 | printf(_(" -h, --host=HOSTNAME database server host or socket directory\n")); |
98 | 0 | printf(_(" -p, --port=PORT database server port number\n")); |
99 | 0 | printf(_(" -U, --username=NAME connect as specified database user\n")); |
100 | 0 | printf(_(" -w, --no-password never prompt for password\n")); |
101 | 0 | printf(_(" -W, --password force password prompt (should happen automatically)\n")); |
102 | 0 | printf(_("\nOptional actions:\n")); |
103 | 0 | printf(_(" --create-slot create a new replication slot (for the slot's name see --slot)\n")); |
104 | 0 | printf(_(" --drop-slot drop the replication slot (for the slot's name see --slot)\n")); |
105 | 0 | printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n")); |
106 | 0 | } |
107 | | |
108 | | static bool |
109 | | stop_streaming(XLogRecPtr xlogpos, uint32 timeline, bool segment_finished) |
110 | 0 | { |
111 | 0 | static uint32 prevtimeline = 0; |
112 | 0 | static XLogRecPtr prevpos = InvalidXLogRecPtr; |
113 | | |
114 | | /* we assume that we get called once at the end of each segment */ |
115 | 0 | if (verbose && segment_finished) |
116 | 0 | fprintf(stderr, _("%s: finished segment at %X/%X (timeline %u)\n"), |
117 | 0 | progname, (uint32) (xlogpos >> 32), (uint32) xlogpos, |
118 | 0 | timeline); |
119 | |
|
120 | 0 | if (!XLogRecPtrIsInvalid(endpos) && endpos < xlogpos) |
121 | 0 | { |
122 | 0 | if (verbose) |
123 | 0 | fprintf(stderr, _("%s: stopped log streaming at %X/%X (timeline %u)\n"), |
124 | 0 | progname, (uint32) (xlogpos >> 32), (uint32) xlogpos, |
125 | 0 | timeline); |
126 | 0 | time_to_stop = true; |
127 | 0 | return true; |
128 | 0 | } |
129 | | |
130 | | /* |
131 | | * Note that we report the previous, not current, position here. After a |
132 | | * timeline switch, xlogpos points to the beginning of the segment because |
133 | | * that's where we always begin streaming. Reporting the end of previous |
134 | | * timeline isn't totally accurate, because the next timeline can begin |
135 | | * slightly before the end of the WAL that we received on the previous |
136 | | * timeline, but it's close enough for reporting purposes. |
137 | | */ |
138 | 0 | if (verbose && prevtimeline != 0 && prevtimeline != timeline) |
139 | 0 | fprintf(stderr, _("%s: switched to timeline %u at %X/%X\n"), |
140 | 0 | progname, timeline, |
141 | 0 | (uint32) (prevpos >> 32), (uint32) prevpos); |
142 | |
|
143 | 0 | prevtimeline = timeline; |
144 | 0 | prevpos = xlogpos; |
145 | |
|
146 | 0 | if (time_to_stop) |
147 | 0 | { |
148 | 0 | if (verbose) |
149 | 0 | fprintf(stderr, _("%s: received interrupt signal, exiting\n"), |
150 | 0 | progname); |
151 | 0 | return true; |
152 | 0 | } |
153 | 0 | return false; |
154 | 0 | } |
155 | | |
156 | | |
157 | | /* |
158 | | * Get destination directory. |
159 | | */ |
160 | | static DIR * |
161 | | get_destination_dir(char *dest_folder) |
162 | 0 | { |
163 | 0 | DIR *dir; |
164 | |
|
165 | 0 | Assert(dest_folder != NULL); |
166 | 0 | dir = opendir(dest_folder); |
167 | 0 | if (dir == NULL) |
168 | 0 | { |
169 | 0 | fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"), |
170 | 0 | progname, basedir, strerror(errno)); |
171 | 0 | disconnect_and_exit(1); |
172 | 0 | } |
173 | | |
174 | 0 | return dir; |
175 | 0 | } |
176 | | |
177 | | |
178 | | /* |
179 | | * Close existing directory. |
180 | | */ |
181 | | static void |
182 | | close_destination_dir(DIR *dest_dir, char *dest_folder) |
183 | 0 | { |
184 | 0 | Assert(dest_dir != NULL && dest_folder != NULL); |
185 | 0 | if (closedir(dest_dir)) |
186 | 0 | { |
187 | 0 | fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"), |
188 | 0 | progname, dest_folder, strerror(errno)); |
189 | 0 | disconnect_and_exit(1); |
190 | 0 | } |
191 | 0 | } |
192 | | |
193 | | |
194 | | /* |
195 | | * Determine starting location for streaming, based on any existing xlog |
196 | | * segments in the directory. We start at the end of the last one that is |
197 | | * complete (size matches wal segment size), on the timeline with highest ID. |
198 | | * |
199 | | * If there are no WAL files in the directory, returns InvalidXLogRecPtr. |
200 | | */ |
201 | | static XLogRecPtr |
202 | | FindStreamingStart(uint32 *tli) |
203 | 0 | { |
204 | 0 | DIR *dir; |
205 | 0 | struct dirent *dirent; |
206 | 0 | XLogSegNo high_segno = 0; |
207 | 0 | uint32 high_tli = 0; |
208 | 0 | bool high_ispartial = false; |
209 | |
|
210 | 0 | dir = get_destination_dir(basedir); |
211 | |
|
212 | 0 | while (errno = 0, (dirent = readdir(dir)) != NULL) |
213 | 0 | { |
214 | 0 | uint32 tli; |
215 | 0 | XLogSegNo segno; |
216 | 0 | bool ispartial; |
217 | 0 | bool iscompress; |
218 | | |
219 | | /* |
220 | | * Check if the filename looks like an xlog file, or a .partial file. |
221 | | */ |
222 | 0 | if (IsXLogFileName(dirent->d_name)) |
223 | 0 | { |
224 | 0 | ispartial = false; |
225 | 0 | iscompress = false; |
226 | 0 | } |
227 | 0 | else if (IsPartialXLogFileName(dirent->d_name)) |
228 | 0 | { |
229 | 0 | ispartial = true; |
230 | 0 | iscompress = false; |
231 | 0 | } |
232 | 0 | else if (IsCompressXLogFileName(dirent->d_name)) |
233 | 0 | { |
234 | 0 | ispartial = false; |
235 | 0 | iscompress = true; |
236 | 0 | } |
237 | 0 | else if (IsPartialCompressXLogFileName(dirent->d_name)) |
238 | 0 | { |
239 | 0 | ispartial = true; |
240 | 0 | iscompress = true; |
241 | 0 | } |
242 | 0 | else |
243 | 0 | continue; |
244 | | |
245 | | /* |
246 | | * Looks like an xlog file. Parse its position. |
247 | | */ |
248 | 0 | XLogFromFileName(dirent->d_name, &tli, &segno, WalSegSz); |
249 | | |
250 | | /* |
251 | | * Check that the segment has the right size, if it's supposed to be |
252 | | * completed. For non-compressed segments just check the on-disk size |
253 | | * and see if it matches a completed segment. For compressed segments, |
254 | | * look at the last 4 bytes of the compressed file, which is where the |
255 | | * uncompressed size is located for gz files with a size lower than |
256 | | * 4GB, and then compare it to the size of a completed segment. The 4 |
257 | | * last bytes correspond to the ISIZE member according to |
258 | | * http://www.zlib.org/rfc-gzip.html. |
259 | | */ |
260 | 0 | if (!ispartial && !iscompress) |
261 | 0 | { |
262 | 0 | struct stat statbuf; |
263 | 0 | char fullpath[MAXPGPATH * 2]; |
264 | |
|
265 | 0 | snprintf(fullpath, sizeof(fullpath), "%s/%s", basedir, dirent->d_name); |
266 | 0 | if (stat(fullpath, &statbuf) != 0) |
267 | 0 | { |
268 | 0 | fprintf(stderr, _("%s: could not stat file \"%s\": %s\n"), |
269 | 0 | progname, fullpath, strerror(errno)); |
270 | 0 | disconnect_and_exit(1); |
271 | 0 | } |
272 | | |
273 | 0 | if (statbuf.st_size != WalSegSz) |
274 | 0 | { |
275 | 0 | fprintf(stderr, |
276 | 0 | _("%s: segment file \"%s\" has incorrect size %d, skipping\n"), |
277 | 0 | progname, dirent->d_name, (int) statbuf.st_size); |
278 | 0 | continue; |
279 | 0 | } |
280 | 0 | } |
281 | 0 | else if (!ispartial && iscompress) |
282 | 0 | { |
283 | 0 | int fd; |
284 | 0 | char buf[4]; |
285 | 0 | int bytes_out; |
286 | 0 | char fullpath[MAXPGPATH * 2]; |
287 | |
|
288 | 0 | snprintf(fullpath, sizeof(fullpath), "%s/%s", basedir, dirent->d_name); |
289 | |
|
290 | 0 | fd = open(fullpath, O_RDONLY | PG_BINARY); |
291 | 0 | if (fd < 0) |
292 | 0 | { |
293 | 0 | fprintf(stderr, _("%s: could not open compressed file \"%s\": %s\n"), |
294 | 0 | progname, fullpath, strerror(errno)); |
295 | 0 | disconnect_and_exit(1); |
296 | 0 | } |
297 | 0 | if (lseek(fd, (off_t) (-4), SEEK_END) < 0) |
298 | 0 | { |
299 | 0 | fprintf(stderr, _("%s: could not seek in compressed file \"%s\": %s\n"), |
300 | 0 | progname, fullpath, strerror(errno)); |
301 | 0 | disconnect_and_exit(1); |
302 | 0 | } |
303 | 0 | if (read(fd, (char *) buf, sizeof(buf)) != sizeof(buf)) |
304 | 0 | { |
305 | 0 | fprintf(stderr, _("%s: could not read compressed file \"%s\": %s\n"), |
306 | 0 | progname, fullpath, strerror(errno)); |
307 | 0 | disconnect_and_exit(1); |
308 | 0 | } |
309 | | |
310 | 0 | close(fd); |
311 | 0 | bytes_out = (buf[3] << 24) | (buf[2] << 16) | |
312 | 0 | (buf[1] << 8) | buf[0]; |
313 | |
|
314 | 0 | if (bytes_out != WalSegSz) |
315 | 0 | { |
316 | 0 | fprintf(stderr, |
317 | 0 | _("%s: compressed segment file \"%s\" has incorrect uncompressed size %d, skipping\n"), |
318 | 0 | progname, dirent->d_name, bytes_out); |
319 | 0 | continue; |
320 | 0 | } |
321 | 0 | } |
322 | | |
323 | | /* Looks like a valid segment. Remember that we saw it. */ |
324 | 0 | if ((segno > high_segno) || |
325 | 0 | (segno == high_segno && tli > high_tli) || |
326 | 0 | (segno == high_segno && tli == high_tli && high_ispartial && !ispartial)) |
327 | 0 | { |
328 | 0 | high_segno = segno; |
329 | 0 | high_tli = tli; |
330 | 0 | high_ispartial = ispartial; |
331 | 0 | } |
332 | 0 | } |
333 | | |
334 | 0 | if (errno) |
335 | 0 | { |
336 | 0 | fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"), |
337 | 0 | progname, basedir, strerror(errno)); |
338 | 0 | disconnect_and_exit(1); |
339 | 0 | } |
340 | | |
341 | 0 | close_destination_dir(dir, basedir); |
342 | |
|
343 | 0 | if (high_segno > 0) |
344 | 0 | { |
345 | 0 | XLogRecPtr high_ptr; |
346 | | |
347 | | /* |
348 | | * Move the starting pointer to the start of the next segment, if the |
349 | | * highest one we saw was completed. Otherwise start streaming from |
350 | | * the beginning of the .partial segment. |
351 | | */ |
352 | 0 | if (!high_ispartial) |
353 | 0 | high_segno++; |
354 | |
|
355 | 0 | XLogSegNoOffsetToRecPtr(high_segno, 0, WalSegSz, high_ptr); |
356 | |
|
357 | 0 | *tli = high_tli; |
358 | 0 | return high_ptr; |
359 | 0 | } |
360 | 0 | else |
361 | 0 | return InvalidXLogRecPtr; |
362 | 0 | } |
363 | | |
364 | | /* |
365 | | * Start the log streaming |
366 | | */ |
367 | | static void |
368 | | StreamLog(void) |
369 | 0 | { |
370 | 0 | XLogRecPtr serverpos; |
371 | 0 | TimeLineID servertli; |
372 | 0 | StreamCtl stream; |
373 | |
|
374 | 0 | MemSet(&stream, 0, sizeof(stream)); |
375 | | |
376 | | /* |
377 | | * Connect in replication mode to the server |
378 | | */ |
379 | 0 | if (conn == NULL) |
380 | 0 | conn = GetConnection(); |
381 | 0 | if (!conn) |
382 | | /* Error message already written in GetConnection() */ |
383 | 0 | return; |
384 | | |
385 | 0 | if (!CheckServerVersionForStreaming(conn)) |
386 | 0 | { |
387 | | /* |
388 | | * Error message already written in CheckServerVersionForStreaming(). |
389 | | * There's no hope of recovering from a version mismatch, so don't |
390 | | * retry. |
391 | | */ |
392 | 0 | disconnect_and_exit(1); |
393 | 0 | } |
394 | | |
395 | | /* |
396 | | * Identify server, obtaining start LSN position and current timeline ID |
397 | | * at the same time, necessary if not valid data can be found in the |
398 | | * existing output directory. |
399 | | */ |
400 | 0 | if (!RunIdentifySystem(conn, NULL, &servertli, &serverpos, NULL)) |
401 | 0 | disconnect_and_exit(1); |
402 | | |
403 | | /* |
404 | | * Figure out where to start streaming. |
405 | | */ |
406 | 0 | stream.startpos = FindStreamingStart(&stream.timeline); |
407 | 0 | if (stream.startpos == InvalidXLogRecPtr) |
408 | 0 | { |
409 | 0 | stream.startpos = serverpos; |
410 | 0 | stream.timeline = servertli; |
411 | 0 | } |
412 | | |
413 | | /* |
414 | | * Always start streaming at the beginning of a segment |
415 | | */ |
416 | 0 | stream.startpos -= XLogSegmentOffset(stream.startpos, WalSegSz); |
417 | | |
418 | | /* |
419 | | * Start the replication |
420 | | */ |
421 | 0 | if (verbose) |
422 | 0 | fprintf(stderr, |
423 | 0 | _("%s: starting log streaming at %X/%X (timeline %u)\n"), |
424 | 0 | progname, (uint32) (stream.startpos >> 32), (uint32) stream.startpos, |
425 | 0 | stream.timeline); |
426 | |
|
427 | 0 | stream.stream_stop = stop_streaming; |
428 | 0 | stream.stop_socket = PGINVALID_SOCKET; |
429 | 0 | stream.standby_message_timeout = standby_message_timeout; |
430 | 0 | stream.synchronous = synchronous; |
431 | 0 | stream.do_sync = do_sync; |
432 | 0 | stream.mark_done = false; |
433 | 0 | stream.walmethod = CreateWalDirectoryMethod(basedir, compresslevel, |
434 | 0 | stream.do_sync); |
435 | 0 | stream.partial_suffix = ".partial"; |
436 | 0 | stream.replication_slot = replication_slot; |
437 | |
|
438 | 0 | ReceiveXlogStream(conn, &stream); |
439 | |
|
440 | 0 | if (!stream.walmethod->finish()) |
441 | 0 | { |
442 | 0 | fprintf(stderr, |
443 | 0 | _("%s: could not finish writing WAL files: %s\n"), |
444 | 0 | progname, strerror(errno)); |
445 | 0 | return; |
446 | 0 | } |
447 | | |
448 | 0 | PQfinish(conn); |
449 | |
|
450 | 0 | FreeWalDirectoryMethod(); |
451 | 0 | pg_free(stream.walmethod); |
452 | |
|
453 | 0 | conn = NULL; |
454 | 0 | } |
455 | | |
456 | | /* |
457 | | * When sigint is called, just tell the system to exit at the next possible |
458 | | * moment. |
459 | | */ |
460 | | #ifndef WIN32 |
461 | | |
462 | | static void |
463 | | sigint_handler(int signum) |
464 | 0 | { |
465 | 0 | time_to_stop = true; |
466 | 0 | } |
467 | | #endif |
468 | | |
469 | | int |
470 | | main(int argc, char **argv) |
471 | | { |
472 | | static struct option long_options[] = { |
473 | | {"help", no_argument, NULL, '?'}, |
474 | | {"version", no_argument, NULL, 'V'}, |
475 | | {"directory", required_argument, NULL, 'D'}, |
476 | | {"dbname", required_argument, NULL, 'd'}, |
477 | | {"endpos", required_argument, NULL, 'E'}, |
478 | | {"host", required_argument, NULL, 'h'}, |
479 | | {"port", required_argument, NULL, 'p'}, |
480 | | {"username", required_argument, NULL, 'U'}, |
481 | | {"no-loop", no_argument, NULL, 'n'}, |
482 | | {"no-password", no_argument, NULL, 'w'}, |
483 | | {"password", no_argument, NULL, 'W'}, |
484 | | {"status-interval", required_argument, NULL, 's'}, |
485 | | {"slot", required_argument, NULL, 'S'}, |
486 | | {"verbose", no_argument, NULL, 'v'}, |
487 | | {"compress", required_argument, NULL, 'Z'}, |
488 | | /* action */ |
489 | | {"create-slot", no_argument, NULL, 1}, |
490 | | {"drop-slot", no_argument, NULL, 2}, |
491 | | {"if-not-exists", no_argument, NULL, 3}, |
492 | | {"synchronous", no_argument, NULL, 4}, |
493 | | {"no-sync", no_argument, NULL, 5}, |
494 | | {NULL, 0, NULL, 0} |
495 | | }; |
496 | | |
497 | | int c; |
498 | | int option_index; |
499 | | char *db_name; |
500 | | uint32 hi, |
501 | | lo; |
502 | | |
503 | | progname = get_progname(argv[0]); |
504 | | set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_basebackup")); |
505 | | |
506 | | if (argc > 1) |
507 | | { |
508 | | if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) |
509 | | { |
510 | | usage(); |
511 | | exit(0); |
512 | | } |
513 | | else if (strcmp(argv[1], "-V") == 0 || |
514 | | strcmp(argv[1], "--version") == 0) |
515 | | { |
516 | | puts("pg_receivewal (PostgreSQL) " PG_VERSION); |
517 | | exit(0); |
518 | | } |
519 | | } |
520 | | |
521 | | while ((c = getopt_long(argc, argv, "D:d:E:h:p:U:s:S:nwWvZ:", |
522 | | long_options, &option_index)) != -1) |
523 | | { |
524 | | switch (c) |
525 | | { |
526 | | case 'D': |
527 | | basedir = pg_strdup(optarg); |
528 | | break; |
529 | | case 'd': |
530 | | connection_string = pg_strdup(optarg); |
531 | | break; |
532 | | case 'h': |
533 | | dbhost = pg_strdup(optarg); |
534 | | break; |
535 | | case 'p': |
536 | | if (atoi(optarg) <= 0) |
537 | | { |
538 | | fprintf(stderr, _("%s: invalid port number \"%s\"\n"), |
539 | | progname, optarg); |
540 | | exit(1); |
541 | | } |
542 | | dbport = pg_strdup(optarg); |
543 | | break; |
544 | | case 'U': |
545 | | dbuser = pg_strdup(optarg); |
546 | | break; |
547 | | case 'w': |
548 | | dbgetpassword = -1; |
549 | | break; |
550 | | case 'W': |
551 | | dbgetpassword = 1; |
552 | | break; |
553 | | case 's': |
554 | | standby_message_timeout = atoi(optarg) * 1000; |
555 | | if (standby_message_timeout < 0) |
556 | | { |
557 | | fprintf(stderr, _("%s: invalid status interval \"%s\"\n"), |
558 | | progname, optarg); |
559 | | exit(1); |
560 | | } |
561 | | break; |
562 | | case 'S': |
563 | | replication_slot = pg_strdup(optarg); |
564 | | break; |
565 | | case 'E': |
566 | | if (sscanf(optarg, "%X/%X", &hi, &lo) != 2) |
567 | | { |
568 | | fprintf(stderr, |
569 | | _("%s: could not parse end position \"%s\"\n"), |
570 | | progname, optarg); |
571 | | exit(1); |
572 | | } |
573 | | endpos = ((uint64) hi) << 32 | lo; |
574 | | break; |
575 | | case 'n': |
576 | | noloop = 1; |
577 | | break; |
578 | | case 'v': |
579 | | verbose++; |
580 | | break; |
581 | | case 'Z': |
582 | | compresslevel = atoi(optarg); |
583 | | if (compresslevel < 0 || compresslevel > 9) |
584 | | { |
585 | | fprintf(stderr, _("%s: invalid compression level \"%s\"\n"), |
586 | | progname, optarg); |
587 | | exit(1); |
588 | | } |
589 | | break; |
590 | | /* action */ |
591 | | case 1: |
592 | | do_create_slot = true; |
593 | | break; |
594 | | case 2: |
595 | | do_drop_slot = true; |
596 | | break; |
597 | | case 3: |
598 | | slot_exists_ok = true; |
599 | | break; |
600 | | case 4: |
601 | | synchronous = true; |
602 | | break; |
603 | | case 5: |
604 | | do_sync = false; |
605 | | break; |
606 | | default: |
607 | | |
608 | | /* |
609 | | * getopt_long already emitted a complaint |
610 | | */ |
611 | | fprintf(stderr, _("Try \"%s --help\" for more information.\n"), |
612 | | progname); |
613 | | exit(1); |
614 | | } |
615 | | } |
616 | | |
617 | | /* |
618 | | * Any non-option arguments? |
619 | | */ |
620 | | if (optind < argc) |
621 | | { |
622 | | fprintf(stderr, |
623 | | _("%s: too many command-line arguments (first is \"%s\")\n"), |
624 | | progname, argv[optind]); |
625 | | fprintf(stderr, _("Try \"%s --help\" for more information.\n"), |
626 | | progname); |
627 | | exit(1); |
628 | | } |
629 | | |
630 | | if (do_drop_slot && do_create_slot) |
631 | | { |
632 | | fprintf(stderr, _("%s: cannot use --create-slot together with --drop-slot\n"), progname); |
633 | | fprintf(stderr, _("Try \"%s --help\" for more information.\n"), |
634 | | progname); |
635 | | exit(1); |
636 | | } |
637 | | |
638 | | if (replication_slot == NULL && (do_drop_slot || do_create_slot)) |
639 | | { |
640 | | /* translator: second %s is an option name */ |
641 | | fprintf(stderr, _("%s: %s needs a slot to be specified using --slot\n"), progname, |
642 | | do_drop_slot ? "--drop-slot" : "--create-slot"); |
643 | | fprintf(stderr, _("Try \"%s --help\" for more information.\n"), |
644 | | progname); |
645 | | exit(1); |
646 | | } |
647 | | |
648 | | if (synchronous && !do_sync) |
649 | | { |
650 | | fprintf(stderr, _("%s: cannot use --synchronous together with --no-sync\n"), progname); |
651 | | fprintf(stderr, _("Try \"%s --help\" for more information.\n"), |
652 | | progname); |
653 | | exit(1); |
654 | | } |
655 | | |
656 | | /* |
657 | | * Required arguments |
658 | | */ |
659 | | if (basedir == NULL && !do_drop_slot && !do_create_slot) |
660 | | { |
661 | | fprintf(stderr, _("%s: no target directory specified\n"), progname); |
662 | | fprintf(stderr, _("Try \"%s --help\" for more information.\n"), |
663 | | progname); |
664 | | exit(1); |
665 | | } |
666 | | |
667 | | #ifndef HAVE_LIBZ |
668 | | if (compresslevel != 0) |
669 | | { |
670 | | fprintf(stderr, |
671 | | _("%s: this build does not support compression\n"), |
672 | | progname); |
673 | | exit(1); |
674 | | } |
675 | | #endif |
676 | | |
677 | | /* |
678 | | * Check existence of destination folder. |
679 | | */ |
680 | | if (!do_drop_slot && !do_create_slot) |
681 | | { |
682 | | DIR *dir = get_destination_dir(basedir); |
683 | | |
684 | | close_destination_dir(dir, basedir); |
685 | | } |
686 | | |
687 | | #ifndef WIN32 |
688 | | pqsignal(SIGINT, sigint_handler); |
689 | | #endif |
690 | | |
691 | | /* |
692 | | * Obtain a connection before doing anything. |
693 | | */ |
694 | | conn = GetConnection(); |
695 | | if (!conn) |
696 | | /* error message already written in GetConnection() */ |
697 | | exit(1); |
698 | | |
699 | | /* |
700 | | * Run IDENTIFY_SYSTEM to make sure we've successfully have established a |
701 | | * replication connection and haven't connected using a database specific |
702 | | * connection. |
703 | | */ |
704 | | if (!RunIdentifySystem(conn, NULL, NULL, NULL, &db_name)) |
705 | | disconnect_and_exit(1); |
706 | | |
707 | | /* |
708 | | * Set umask so that directories/files are created with the same |
709 | | * permissions as directories/files in the source data directory. |
710 | | * |
711 | | * pg_mode_mask is set to owner-only by default and then updated in |
712 | | * GetConnection() where we get the mode from the server-side with |
713 | | * RetrieveDataDirCreatePerm() and then call SetDataDirectoryCreatePerm(). |
714 | | */ |
715 | | umask(pg_mode_mask); |
716 | | |
717 | | /* determine remote server's xlog segment size */ |
718 | | if (!RetrieveWalSegSize(conn)) |
719 | | disconnect_and_exit(1); |
720 | | |
721 | | /* |
722 | | * Check that there is a database associated with connection, none should |
723 | | * be defined in this context. |
724 | | */ |
725 | | if (db_name) |
726 | | { |
727 | | fprintf(stderr, |
728 | | _("%s: replication connection using slot \"%s\" is unexpectedly database specific\n"), |
729 | | progname, replication_slot); |
730 | | disconnect_and_exit(1); |
731 | | } |
732 | | |
733 | | /* |
734 | | * Drop a replication slot. |
735 | | */ |
736 | | if (do_drop_slot) |
737 | | { |
738 | | if (verbose) |
739 | | fprintf(stderr, |
740 | | _("%s: dropping replication slot \"%s\"\n"), |
741 | | progname, replication_slot); |
742 | | |
743 | | if (!DropReplicationSlot(conn, replication_slot)) |
744 | | disconnect_and_exit(1); |
745 | | disconnect_and_exit(0); |
746 | | } |
747 | | |
748 | | /* Create a replication slot */ |
749 | | if (do_create_slot) |
750 | | { |
751 | | if (verbose) |
752 | | fprintf(stderr, |
753 | | _("%s: creating replication slot \"%s\"\n"), |
754 | | progname, replication_slot); |
755 | | |
756 | | if (!CreateReplicationSlot(conn, replication_slot, NULL, false, true, false, |
757 | | slot_exists_ok)) |
758 | | disconnect_and_exit(1); |
759 | | disconnect_and_exit(0); |
760 | | } |
761 | | |
762 | | /* |
763 | | * Don't close the connection here so that subsequent StreamLog() can |
764 | | * reuse it. |
765 | | */ |
766 | | |
767 | | while (true) |
768 | | { |
769 | | StreamLog(); |
770 | | if (time_to_stop) |
771 | | { |
772 | | /* |
773 | | * We've been Ctrl-C'ed or end of streaming position has been |
774 | | * willingly reached, so exit without an error code. |
775 | | */ |
776 | | exit(0); |
777 | | } |
778 | | else if (noloop) |
779 | | { |
780 | | fprintf(stderr, _("%s: disconnected\n"), progname); |
781 | | exit(1); |
782 | | } |
783 | | else |
784 | | { |
785 | | fprintf(stderr, |
786 | | /* translator: check source for value for %d */ |
787 | | _("%s: disconnected; waiting %d seconds to try again\n"), |
788 | | progname, RECONNECT_SLEEP_TIME); |
789 | | pg_usleep(RECONNECT_SLEEP_TIME * 1000000); |
790 | | } |
791 | | } |
792 | | } |