Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 017e4f2

Browse files
committed
Expose waitforarchive option through pg_stop_backup()
Internally, we have supported the option to either wait for all of the WAL associated with a backup to be archived, or to return immediately. This option is useful to users of pg_stop_backup() as well, when they are reading the stop backup record position and checking that the WAL they need has been archived independently. This patch adds an additional, optional, argument to pg_stop_backup() which allows the user to indicate if they wish to wait for the WAL to be archived or not. The default matches current behavior, which is to wait. Author: David Steele, with some minor changes, doc updates by me. Reviewed by: Takayuki Tsunakawa, Fujii Masao Discussion: https://postgr.es/m/758e3fd1-45b4-5e28-75cd-e9e7f93a4c02@pgmasters.net
1 parent cbf7ed5 commit 017e4f2

File tree

7 files changed

+41
-9
lines changed

7 files changed

+41
-9
lines changed

doc/src/sgml/backup.sgml

+12-1
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ SELECT pg_start_backup('label', false, false);
887887
<para>
888888
In the same connection as before, issue the command:
889889
<programlisting>
890-
SELECT * FROM pg_stop_backup(false);
890+
SELECT * FROM pg_stop_backup(false, true);
891891
</programlisting>
892892
This terminates the backup mode and performs an automatic switch to
893893
the next WAL segment. The reason for the switch is to arrange for
@@ -924,6 +924,17 @@ SELECT * FROM pg_stop_backup(false);
924924
<function>pg_stop_backup</> terminates because of this your backup
925925
may not be valid.
926926
</para>
927+
<para>
928+
If the backup process monitors and ensures that all WAL segment files
929+
required for the backup are successfully archived then the second
930+
parameter (which defaults to true) can be set to false to have
931+
<function>pg_stop_backup</> return as soon as the stop backup record is
932+
written to the WAL. By default, <function>pg_stop_backup</> will wait
933+
until all WAL has been archived, which can take some time. This option
934+
must be used with caution: if WAL archiving is not monitored correctly
935+
then the backup might not include all of the WAL files and will
936+
therefore be incomplete and not able to be restored.
937+
</para>
927938
</listitem>
928939
</orderedlist>
929940
</para>

doc/src/sgml/func.sgml

+8-2
Original file line numberDiff line numberDiff line change
@@ -18355,7 +18355,7 @@ SELECT set_config('log_statement_stats', 'off', false);
1835518355
</row>
1835618356
<row>
1835718357
<entry>
18358-
<literal><function>pg_stop_backup(<parameter>exclusive</> <type>boolean</>)</function></literal>
18358+
<literal><function>pg_stop_backup(<parameter>exclusive</> <type>boolean</> <optional>, <parameter>wait_for_archive</> <type>boolean</> </optional>)</function></literal>
1835918359
</entry>
1836018360
<entry><type>setof record</type></entry>
1836118361
<entry>Finish performing exclusive or non-exclusive on-line backup (restricted to superusers by default, but other users can be granted EXECUTE to run the function)</entry>
@@ -18439,7 +18439,13 @@ postgres=# select pg_start_backup('label_goes_here');
1843918439
<function>pg_start_backup</>. In a non-exclusive backup, the contents of
1844018440
the <filename>backup_label</> and <filename>tablespace_map</> are returned
1844118441
in the result of the function, and should be written to files in the
18442-
backup (and not in the data directory).
18442+
backup (and not in the data directory). There is an optional second
18443+
parameter of type boolean. If false, the <function>pg_stop_backup</>
18444+
will return immediately after the backup is completed without waiting for
18445+
WAL to be archived. This behavior is only useful for backup
18446+
software which independently monitors WAL archiving. Otherwise, WAL
18447+
required to make the backup consistent might be missing and make the backup
18448+
useless.
1844318449
</para>
1844418450

1844518451
<para>

src/backend/access/transam/xlog.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -10944,7 +10944,8 @@ do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
1094410944
*
1094510945
* We wait forever, since archive_command is supposed to work and we
1094610946
* assume the admin wanted his backup to work completely. If you don't
10947-
* wish to wait, you can set statement_timeout. Also, some notices are
10947+
* wish to wait, then either waitforarchive should be passed in as false,
10948+
* or you can set statement_timeout. Also, some notices are
1094810949
* issued to clue in anyone who might be doing this interactively.
1094910950
*/
1095010951
if (waitforarchive && XLogArchivingActive())

src/backend/access/transam/xlogfuncs.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ pg_stop_backup(PG_FUNCTION_ARGS)
175175
* the backup label and tablespace map files as text fields in as part of the
176176
* resultset.
177177
*
178+
* The first parameter (variable 'exclusive') allows the user to tell us if
179+
* this is an exclusive or a non-exclusive backup.
180+
*
181+
* The second paramter (variable 'waitforarchive'), which is optional,
182+
* allows the user to choose if they want to wait for the WAL to be archived
183+
* or if we should just return as soon as the WAL record is written.
184+
*
178185
* Permission checking for this function is managed through the normal
179186
* GRANT system.
180187
*/
@@ -190,6 +197,7 @@ pg_stop_backup_v2(PG_FUNCTION_ARGS)
190197
bool nulls[3];
191198

192199
bool exclusive = PG_GETARG_BOOL(0);
200+
bool waitforarchive = PG_GETARG_BOOL(1);
193201
XLogRecPtr stoppoint;
194202

195203
/* check to see if caller supports us returning a tuplestore */
@@ -232,7 +240,7 @@ pg_stop_backup_v2(PG_FUNCTION_ARGS)
232240
* Stop the exclusive backup, and since we're in an exclusive backup
233241
* return NULL for both backup_label and tablespace_map.
234242
*/
235-
stoppoint = do_pg_stop_backup(NULL, true, NULL);
243+
stoppoint = do_pg_stop_backup(NULL, waitforarchive, NULL);
236244
exclusive_backup_running = false;
237245

238246
nulls[1] = true;
@@ -250,7 +258,7 @@ pg_stop_backup_v2(PG_FUNCTION_ARGS)
250258
* Stop the non-exclusive backup. Return a copy of the backup label
251259
* and tablespace map so they can be written to disk by the caller.
252260
*/
253-
stoppoint = do_pg_stop_backup(label_file->data, true, NULL);
261+
stoppoint = do_pg_stop_backup(label_file->data, waitforarchive, NULL);
254262
nonexclusive_backup_running = false;
255263
cancel_before_shmem_exit(nonexclusive_base_backup_cleanup, (Datum) 0);
256264

src/backend/catalog/system_views.sql

+7-1
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,12 @@ CREATE OR REPLACE FUNCTION
988988
RETURNS pg_lsn STRICT VOLATILE LANGUAGE internal AS 'pg_start_backup'
989989
PARALLEL RESTRICTED;
990990

991+
CREATE OR REPLACE FUNCTION pg_stop_backup (
992+
exclusive boolean, wait_for_archive boolean DEFAULT true,
993+
OUT lsn pg_lsn, OUT labelfile text, OUT spcmapfile text)
994+
RETURNS SETOF record STRICT VOLATILE LANGUAGE internal as 'pg_stop_backup_v2'
995+
PARALLEL RESTRICTED;
996+
991997
-- legacy definition for compatibility with 9.3
992998
CREATE OR REPLACE FUNCTION
993999
json_populate_record(base anyelement, from_json json, use_json_as_text boolean DEFAULT false)
@@ -1088,7 +1094,7 @@ AS 'jsonb_insert';
10881094
-- available to superuser / cluster owner, if they choose.
10891095
REVOKE EXECUTE ON FUNCTION pg_start_backup(text, boolean, boolean) FROM public;
10901096
REVOKE EXECUTE ON FUNCTION pg_stop_backup() FROM public;
1091-
REVOKE EXECUTE ON FUNCTION pg_stop_backup(boolean) FROM public;
1097+
REVOKE EXECUTE ON FUNCTION pg_stop_backup(boolean, boolean) FROM public;
10921098
REVOKE EXECUTE ON FUNCTION pg_create_restore_point(text) FROM public;
10931099
REVOKE EXECUTE ON FUNCTION pg_switch_wal() FROM public;
10941100
REVOKE EXECUTE ON FUNCTION pg_wal_replay_pause() FROM public;

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201703161
56+
#define CATALOG_VERSION_NO 201703221
5757

5858
#endif

src/include/catalog/pg_proc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3172,7 +3172,7 @@ DATA(insert OID = 2172 ( pg_start_backup PGNSP PGUID 12 1 0 0 0 f f f f t f v r
31723172
DESCR("prepare for taking an online backup");
31733173
DATA(insert OID = 2173 ( pg_stop_backup PGNSP PGUID 12 1 0 0 0 f f f f t f v r 0 0 3220 "" _null_ _null_ _null_ _null_ _null_ pg_stop_backup _null_ _null_ _null_ ));
31743174
DESCR("finish taking an online backup");
3175-
DATA(insert OID = 2739 ( pg_stop_backup PGNSP PGUID 12 1 1 0 0 f f f f t t v r 1 0 2249 "16" "{16,3220,25,25}" "{i,o,o,o}" "{exclusive,lsn,labelfile,spcmapfile}" _null_ _null_ pg_stop_backup_v2 _null_ _null_ _null_ ));
3175+
DATA(insert OID = 2739 ( pg_stop_backup PGNSP PGUID 12 1 1 0 0 f f f f t t v r 2 0 2249 "16 16" "{16,16,3220,25,25}" "{i,i,o,o,o}" "{exclusive,wait_for_archive,lsn,labelfile,spcmapfile}" _null_ _null_ pg_stop_backup_v2 _null_ _null_ _null_ ));
31763176
DESCR("finish taking an online backup");
31773177
DATA(insert OID = 3813 ( pg_is_in_backup PGNSP PGUID 12 1 0 0 0 f f f f t f v s 0 0 16 "" _null_ _null_ _null_ _null_ _null_ pg_is_in_backup _null_ _null_ _null_ ));
31783178
DESCR("true if server is in online backup");

0 commit comments

Comments
 (0)