Skip to content

Commit 68de499

Browse files
committed
New SQL functons pg_backup_in_progress() and pg_backup_start_time()
Darold Gilles, reviewed by Gabriele Bartolini and others, rebased by Marco Nenciarini. Stylistic cleanup and OID fixes by me.
1 parent cd80073 commit 68de499

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

doc/src/sgml/func.sgml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14455,6 +14455,12 @@ SELECT set_config('log_statement_stats', 'off', false);
1445514455
<indexterm>
1445614456
<primary>pg_stop_backup</primary>
1445714457
</indexterm>
14458+
<indexterm>
14459+
<primary>pg_is_in_backup</primary>
14460+
</indexterm>
14461+
<indexterm>
14462+
<primary>pg_backup_start_time</primary>
14463+
</indexterm>
1445814464
<indexterm>
1445914465
<primary>pg_switch_xlog</primary>
1446014466
</indexterm>
@@ -14519,6 +14525,20 @@ SELECT set_config('log_statement_stats', 'off', false);
1451914525
<entry><type>text</type></entry>
1452014526
<entry>Finish performing on-line backup (restricted to superusers or replication roles)</entry>
1452114527
</row>
14528+
<row>
14529+
<entry>
14530+
<literal><function>pg_is_in_backup()</function></literal>
14531+
</entry>
14532+
<entry><type>bool</type></entry>
14533+
<entry>True if an on-line exclusive backup is still in progress.</entry>
14534+
</row>
14535+
<row>
14536+
<entry>
14537+
<literal><function>pg_backup_start_time()</function></literal>
14538+
</entry>
14539+
<entry><type>timestamp with time zone</type></entry>
14540+
<entry>Get start time of an online exclusive backup in progress.</entry>
14541+
</row>
1452214542
<row>
1452314543
<entry>
1452414544
<literal><function>pg_switch_xlog()</function></literal>

src/backend/access/transam/xlogfuncs.c

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "utils/numeric.h"
3030
#include "utils/guc.h"
3131
#include "utils/timestamp.h"
32-
32+
#include "storage/fd.h"
3333

3434
static void validate_xlog_location(char *str);
3535

@@ -563,3 +563,74 @@ pg_xlog_location_diff(PG_FUNCTION_ARGS)
563563

564564
PG_RETURN_NUMERIC(result);
565565
}
566+
567+
/*
568+
* Returns bool with current on-line backup mode, a global state.
569+
*/
570+
Datum
571+
pg_is_in_backup(PG_FUNCTION_ARGS)
572+
{
573+
PG_RETURN_BOOL(BackupInProgress());
574+
}
575+
576+
/*
577+
* Returns start time of an online exclusive backup.
578+
*
579+
* When there's no exclusive backup in progress, the function
580+
* returns NULL.
581+
*/
582+
Datum
583+
pg_backup_start_time(PG_FUNCTION_ARGS)
584+
{
585+
Datum xtime;
586+
FILE *lfp;
587+
char fline[MAXPGPATH];
588+
char backup_start_time[30];
589+
590+
/*
591+
* See if label file is present
592+
*/
593+
lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
594+
if (lfp == NULL)
595+
{
596+
if (errno != ENOENT)
597+
ereport(ERROR,
598+
(errcode_for_file_access(),
599+
errmsg("could not read file \"%s\": %m",
600+
BACKUP_LABEL_FILE)));
601+
PG_RETURN_NULL();
602+
}
603+
604+
/*
605+
* Parse the file to find the the START TIME line.
606+
*/
607+
backup_start_time[0] = '\0';
608+
while (fgets(fline, sizeof(fline), lfp) != NULL)
609+
{
610+
if (sscanf(fline, "START TIME: %25[^\n]\n", backup_start_time) == 1)
611+
break;
612+
}
613+
614+
/*
615+
* Close the backup label file.
616+
*/
617+
if (ferror(lfp) || FreeFile(lfp))
618+
ereport(ERROR,
619+
(errcode_for_file_access(),
620+
errmsg("could not read file \"%s\": %m", BACKUP_LABEL_FILE)));
621+
622+
if (strlen(backup_start_time) == 0)
623+
ereport(ERROR,
624+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
625+
errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
626+
627+
/*
628+
* Convert the time string read from file to TimestampTz form.
629+
*/
630+
xtime = DirectFunctionCall3(timestamptz_in,
631+
CStringGetDatum(backup_start_time),
632+
ObjectIdGetDatum(InvalidOid),
633+
Int32GetDatum(-1));
634+
635+
PG_RETURN_DATUM(xtime);
636+
}

src/include/access/xlog_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,5 +282,7 @@ extern Datum pg_xlog_replay_pause(PG_FUNCTION_ARGS);
282282
extern Datum pg_xlog_replay_resume(PG_FUNCTION_ARGS);
283283
extern Datum pg_is_xlog_replay_paused(PG_FUNCTION_ARGS);
284284
extern Datum pg_xlog_location_diff(PG_FUNCTION_ARGS);
285+
extern Datum pg_is_in_backup(PG_FUNCTION_ARGS);
286+
extern Datum pg_backup_start_time(PG_FUNCTION_ARGS);
285287

286288
#endif /* XLOG_INTERNAL_H */

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201204301
56+
#define CATALOG_VERSION_NO 201206141
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,6 +2936,10 @@ DATA(insert OID = 2172 ( pg_start_backup PGNSP PGUID 12 1 0 0 0 f f f f t f v 2
29362936
DESCR("prepare for taking an online backup");
29372937
DATA(insert OID = 2173 ( pg_stop_backup PGNSP PGUID 12 1 0 0 0 f f f f t f v 0 0 25 "" _null_ _null_ _null_ _null_ pg_stop_backup _null_ _null_ _null_ ));
29382938
DESCR("finish taking an online backup");
2939+
DATA(insert OID = 3813 ( pg_is_in_backup PGNSP PGUID 12 1 0 0 0 f f f f t f v 0 0 16 "" _null_ _null_ _null_ _null_ pg_is_in_backup _null_ _null_ _null_ ));
2940+
DESCR("true if server is in online backup");
2941+
DATA(insert OID = 3814 ( pg_backup_start_time PGNSP PGUID 12 1 0 0 0 f f f f t f s 0 0 1184 "" _null_ _null_ _null_ _null_ pg_backup_start_time _null_ _null_ _null_ ));
2942+
DESCR("start time of an online backup");
29392943
DATA(insert OID = 2848 ( pg_switch_xlog PGNSP PGUID 12 1 0 0 0 f f f f t f v 0 0 25 "" _null_ _null_ _null_ _null_ pg_switch_xlog _null_ _null_ _null_ ));
29402944
DESCR("switch to new xlog file");
29412945
DATA(insert OID = 3098 ( pg_create_restore_point PGNSP PGUID 12 1 0 0 0 f f f f t f v 1 0 25 "25" _null_ _null_ _null_ _null_ pg_create_restore_point _null_ _null_ _null_ ));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy