@@ -93,6 +93,8 @@ static pg_time_t last_progress_report = 0;
9393static int32 maxrate = 0 ; /* no limit by default */
9494static char * replication_slot = NULL ;
9595static bool temp_replication_slot = true;
96+ static bool create_slot = false;
97+ static bool no_slot = false;
9698
9799static bool success = false;
98100static bool made_new_pgdata = false;
@@ -346,6 +348,7 @@ usage(void)
346348 printf (_ ("\nGeneral options:\n" ));
347349 printf (_ (" -c, --checkpoint=fast|spread\n"
348350 " set fast or spread checkpointing\n" ));
351+ printf (_ (" -C, --create-slot create replication slot\n" ));
349352 printf (_ (" -l, --label=LABEL set backup label\n" ));
350353 printf (_ (" -n, --no-clean do not clean up after errors\n" ));
351354 printf (_ (" -N, --no-sync do not wait for changes to be written safely to disk\n" ));
@@ -466,7 +469,6 @@ typedef struct
466469 char xlog [MAXPGPATH ]; /* directory or tarfile depending on mode */
467470 char * sysidentifier ;
468471 int timeline ;
469- bool temp_slot ;
470472} logstreamer_param ;
471473
472474static int
@@ -492,9 +494,6 @@ LogStreamerMain(logstreamer_param *param)
492494 stream .mark_done = true;
493495 stream .partial_suffix = NULL ;
494496 stream .replication_slot = replication_slot ;
495- stream .temp_slot = param -> temp_slot ;
496- if (stream .temp_slot && !stream .replication_slot )
497- stream .replication_slot = psprintf ("pg_basebackup_%d" , (int ) PQbackendPID (param -> bgconn ));
498497
499498 if (format == 'p' )
500499 stream .walmethod = CreateWalDirectoryMethod (param -> xlog , 0 , do_sync );
@@ -583,9 +582,29 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier)
583582
584583 /* Temporary replication slots are only supported in 10 and newer */
585584 if (PQserverVersion (conn ) < MINIMUM_VERSION_FOR_TEMP_SLOTS )
586- param -> temp_slot = false;
587- else
588- param -> temp_slot = temp_replication_slot ;
585+ temp_replication_slot = false;
586+
587+ /*
588+ * Create replication slot if requested
589+ */
590+ if (temp_replication_slot && !replication_slot )
591+ replication_slot = psprintf ("pg_basebackup_%d" , (int ) PQbackendPID (param -> bgconn ));
592+ if (temp_replication_slot || create_slot )
593+ {
594+ if (!CreateReplicationSlot (param -> bgconn , replication_slot , NULL ,
595+ temp_replication_slot , true, true, false))
596+ disconnect_and_exit (1 );
597+
598+ if (verbose )
599+ {
600+ if (temp_replication_slot )
601+ fprintf (stderr , _ ("%s: created temporary replication slot \"%s\"\n" ),
602+ progname , replication_slot );
603+ else
604+ fprintf (stderr , _ ("%s: created replication slot \"%s\"\n" ),
605+ progname , replication_slot );
606+ }
607+ }
589608
590609 if (format == 'p' )
591610 {
@@ -2079,6 +2098,7 @@ main(int argc, char **argv)
20792098 {"pgdata" , required_argument , NULL , 'D' },
20802099 {"format" , required_argument , NULL , 'F' },
20812100 {"checkpoint" , required_argument , NULL , 'c' },
2101+ {"create-slot" , no_argument , NULL , 'C' },
20822102 {"max-rate" , required_argument , NULL , 'r' },
20832103 {"write-recovery-conf" , no_argument , NULL , 'R' },
20842104 {"slot" , required_argument , NULL , 'S' },
@@ -2105,7 +2125,6 @@ main(int argc, char **argv)
21052125 int c ;
21062126
21072127 int option_index ;
2108- bool no_slot = false;
21092128
21102129 progname = get_progname (argv [0 ]);
21112130 set_pglocale_pgservice (argv [0 ], PG_TEXTDOMAIN ("pg_basebackup" ));
@@ -2127,11 +2146,14 @@ main(int argc, char **argv)
21272146
21282147 atexit (cleanup_directories_atexit );
21292148
2130- while ((c = getopt_long (argc , argv , "D :F:r:RT: X:l:nNzZ:d:c:h:p:U:s:S :wWvP" ,
2149+ while ((c = getopt_long (argc , argv , "CD :F:r:RS:T: X:l:nNzZ:d:c:h:p:U:s:wWvP" ,
21312150 long_options , & option_index )) != -1 )
21322151 {
21332152 switch (c )
21342153 {
2154+ case 'C' :
2155+ create_slot = true;
2156+ break ;
21352157 case 'D' :
21362158 basedir = pg_strdup (optarg );
21372159 break ;
@@ -2348,6 +2370,29 @@ main(int argc, char **argv)
23482370 temp_replication_slot = false;
23492371 }
23502372
2373+ if (create_slot )
2374+ {
2375+ if (!replication_slot )
2376+ {
2377+ fprintf (stderr ,
2378+ _ ("%s: --create-slot needs a slot to be specified using --slot\n" ),
2379+ progname );
2380+ fprintf (stderr , _ ("Try \"%s --help\" for more information.\n" ),
2381+ progname );
2382+ exit (1 );
2383+ }
2384+
2385+ if (no_slot )
2386+ {
2387+ fprintf (stderr ,
2388+ _ ("%s: --create-slot and --no-slot are incompatible options\n" ),
2389+ progname );
2390+ fprintf (stderr , _ ("Try \"%s --help\" for more information.\n" ),
2391+ progname );
2392+ exit (1 );
2393+ }
2394+ }
2395+
23512396 if (xlog_dir )
23522397 {
23532398 if (format != 'p' )
0 commit comments