Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0dc848b

Browse files
committed
pg_basebackup: Add --slot option
This option specifies a replication slot for WAL streaming (-X stream),so that there can be continuous replication slot use between WALstreaming during the base backup and the start of regular streamingreplication.Reviewed-by: Michael Paquier <michael.paquier@gmail.com>
1 parent90102bb commit0dc848b

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

‎doc/src/sgml/ref/pg_basebackup.sgml

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,35 @@ PostgreSQL documentation
215215
<listitem>
216216

217217
<para>
218-
Write a minimal <filename>recovery.conf</filename> in the output directory (or into
219-
the base archive file when using tar format) to ease setting
220-
up a standby server.
218+
Write a minimal <filename>recovery.conf</filename> in the output
219+
directory (or into the base archive file when using tar format) to
220+
ease setting up a standby server.
221+
The <filename>recovery.conf</filename> file will record the connection
222+
settings and, if specified, the replication slot
223+
that <application>pg_basebackup</application> is using, so that the
224+
streaming replication will use the same settings later on.
221225
</para>
222226

223227
</listitem>
224228
</varlistentry>
225229

230+
<varlistentry>
231+
<term><option>-S <replaceable>slotname</replaceable></option></term>
232+
<term><option>--slot=<replaceable class="parameter">slotname</replaceable></option></term>
233+
<listitem>
234+
<para>
235+
This option can only be used together with <literal>-X
236+
stream</literal>. It causes the WAL streaming to use the specified
237+
replication slot. If the base backup is intended to be used as a
238+
streaming replication standby using replication slots, it should then
239+
use the same replication slot name
240+
in <filename>recovery.conf</filename>. That way, it is ensured that
241+
the server does not remove any necessary WAL data in the time between
242+
the end of the base backup and the start of streaming replication.
243+
</para>
244+
</listitem>
245+
</varlistentry>
246+
226247
<varlistentry>
227248
<term><option>-T <replaceable class="parameter">olddir</replaceable>=<replaceable class="parameter">newdir</replaceable></option></term>
228249
<term><option>--tablespace-mapping=<replaceable class="parameter">olddir</replaceable>=<replaceable class="parameter">newdir</replaceable></option></term>

‎src/bin/pg_basebackup/pg_basebackup.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ usage(void)
239239
" (in kB/s, or use suffix \"k\" or \"M\")\n"));
240240
printf(_(" -R, --write-recovery-conf\n"
241241
" write recovery.conf after backup\n"));
242+
printf(_(" -S, --slot=SLOTNAME replication slot to use\n"));
242243
printf(_(" -T, --tablespace-mapping=OLDDIR=NEWDIR\n"
243244
" relocate tablespace in OLDDIR to NEWDIR\n"));
244245
printf(_(" -x, --xlog include required WAL files in backup (fetch mode)\n"));
@@ -1536,6 +1537,13 @@ GenerateRecoveryConf(PGconn *conn)
15361537
appendPQExpBuffer(recoveryconfcontents,"primary_conninfo = '%s'\n",escaped);
15371538
free(escaped);
15381539

1540+
if (replication_slot)
1541+
{
1542+
escaped=escape_quotes(replication_slot);
1543+
appendPQExpBuffer(recoveryconfcontents,"primary_slot_name = '%s'\n",replication_slot);
1544+
free(escaped);
1545+
}
1546+
15391547
if (PQExpBufferBroken(recoveryconfcontents)||
15401548
PQExpBufferDataBroken(conninfo_buf))
15411549
{
@@ -1934,6 +1942,7 @@ main(int argc, char **argv)
19341942
{"checkpoint",required_argument,NULL,'c'},
19351943
{"max-rate",required_argument,NULL,'r'},
19361944
{"write-recovery-conf",no_argument,NULL,'R'},
1945+
{"slot",required_argument,NULL,'S'},
19371946
{"tablespace-mapping",required_argument,NULL,'T'},
19381947
{"xlog",no_argument,NULL,'x'},
19391948
{"xlog-method",required_argument,NULL,'X'},
@@ -1974,7 +1983,7 @@ main(int argc, char **argv)
19741983
}
19751984
}
19761985

1977-
while ((c=getopt_long(argc,argv,"D:F:r:RT:xX:l:zZ:d:c:h:p:U:s:wWvP",
1986+
while ((c=getopt_long(argc,argv,"D:F:r:RT:xX:l:zZ:d:c:h:p:U:s:S:wWvP",
19781987
long_options,&option_index))!=-1)
19791988
{
19801989
switch (c)
@@ -2001,6 +2010,9 @@ main(int argc, char **argv)
20012010
case'R':
20022011
writerecoveryconf= true;
20032012
break;
2013+
case'S':
2014+
replication_slot=pg_strdup(optarg);
2015+
break;
20042016
case'T':
20052017
tablespace_list_append(optarg);
20062018
break;
@@ -2165,6 +2177,16 @@ main(int argc, char **argv)
21652177
exit(1);
21662178
}
21672179

2180+
if (replication_slot&& !streamwal)
2181+
{
2182+
fprintf(stderr,
2183+
_("%s: replication slots can only be used with WAL streaming\n"),
2184+
progname);
2185+
fprintf(stderr,_("Try \"%s --help\" for more information.\n"),
2186+
progname);
2187+
exit(1);
2188+
}
2189+
21682190
if (strcmp(xlog_dir,"")!=0)
21692191
{
21702192
if (format!='p')

‎src/bin/pg_basebackup/t/010_pg_basebackup.pl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use warnings;
33
use Cwd;
44
use TestLib;
5-
use Test::Moretests=>44;
5+
use Test::Moretests=>51;
66

77
program_help_ok('pg_basebackup');
88
program_version_ok('pg_basebackup');
@@ -37,6 +37,7 @@
3737
'pg_basebackup fails because of WAL configuration');
3838

3939
open CONF,">>$tempdir/pgdata/postgresql.conf";
40+
print CONF"max_replication_slots = 10\n";
4041
print CONF"max_wal_senders = 10\n";
4142
print CONF"wal_level = archive\n";
4243
close CONF;
@@ -156,3 +157,22 @@
156157
command_ok(['pg_basebackup','-D',"$tempdir/backupxs",'-X','stream' ],
157158
'pg_basebackup -X stream runs');
158159
ok(grep(/^[0-9A-F]{24}$/, slurp_dir("$tempdir/backupxf/pg_xlog")),'WAL files copied');
160+
161+
command_fails(['pg_basebackup','-D',"$tempdir/fail",'-S','slot1' ],
162+
'pg_basebackup with replication slot fails without -X stream');
163+
command_fails(['pg_basebackup','-D',"$tempdir/backupxs_sl_fail",'-X','stream','-S','slot1' ],
164+
'pg_basebackup fails with nonexistent replication slot');
165+
166+
psql'postgres',q{SELECT * FROM pg_create_physical_replication_slot('slot1')};
167+
my$lsn = psql'postgres',q{SELECT restart_lsn FROM pg_replication_slots WHERE slot_name = 'slot1'};
168+
is($lsn,'','restart LSN of new slot is null');
169+
command_ok(['pg_basebackup','-D',"$tempdir/backupxs_sl",'-X','stream','-S','slot1' ],
170+
'pg_basebackup -X stream with replication slot runs');
171+
$lsn = psql'postgres',q{SELECT restart_lsn FROM pg_replication_slots WHERE slot_name = 'slot1'};
172+
like($lsn,qr!^0/[0-9A-Z]{8}$!,'restart LSN of slot has advanced');
173+
174+
command_ok(['pg_basebackup','-D',"$tempdir/backupxs_sl_R",'-X','stream','-S','slot1','-R' ],
175+
'pg_basebackup with replication slot and -R runs');
176+
like(slurp_file("$tempdir/backupxs_sl_R/recovery.conf"),
177+
qr/^primary_slot_name = 'slot1'$/m,
178+
'recovery.conf sets primary_slot_name');

‎src/test/perl/TestLib.pm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,11 @@ END
173173
subpsql
174174
{
175175
my ($dbname,$sql) =@_;
176+
my ($stdout,$stderr);
176177
print("# Running SQL command:$sql\n");
177-
run ['psql','-X','-q','-d',$dbname,'-f','-' ],'<', \$sqlordie;
178+
run ['psql','-X','-A','-t','-q','-d',$dbname,'-f','-' ],'<', \$sql,'>', \$stdout,'2>', \$stderrordie;
179+
chomp$stdout;
180+
return$stdout;
178181
}
179182

180183
subslurp_dir

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp