aboutsummaryrefslogtreecommitdiff
path: root/src/backend/access/transam/xlogfuncs.c
diff options
context:
space:
mode:
authorRobert Haas <rhaas@postgresql.org>2012-06-14 13:25:43 -0400
committerRobert Haas <rhaas@postgresql.org>2012-06-14 13:25:43 -0400
commit68de499bda40fdf1395b714bb63febad61046700 (patch)
tree91d33ff0cdd3b52e1358d619904d9a1147aaaf4b /src/backend/access/transam/xlogfuncs.c
parentcd80073445ff5d72ad42923ba3d017541feae103 (diff)
downloadpostgresql-68de499bda40fdf1395b714bb63febad61046700.tar.gz
postgresql-68de499bda40fdf1395b714bb63febad61046700.zip
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.
Diffstat (limited to 'src/backend/access/transam/xlogfuncs.c')
-rw-r--r--src/backend/access/transam/xlogfuncs.c73
1 files changed, 72 insertions, 1 deletions
diff --git a/src/backend/access/transam/xlogfuncs.c b/src/backend/access/transam/xlogfuncs.c
index f3c8a09c2aa..d94809ad7ee 100644
--- a/src/backend/access/transam/xlogfuncs.c
+++ b/src/backend/access/transam/xlogfuncs.c
@@ -29,7 +29,7 @@
#include "utils/numeric.h"
#include "utils/guc.h"
#include "utils/timestamp.h"
-
+#include "storage/fd.h"
static void validate_xlog_location(char *str);
@@ -563,3 +563,74 @@ pg_xlog_location_diff(PG_FUNCTION_ARGS)
PG_RETURN_NUMERIC(result);
}
+
+/*
+ * Returns bool with current on-line backup mode, a global state.
+ */
+Datum
+pg_is_in_backup(PG_FUNCTION_ARGS)
+{
+ PG_RETURN_BOOL(BackupInProgress());
+}
+
+/*
+ * Returns start time of an online exclusive backup.
+ *
+ * When there's no exclusive backup in progress, the function
+ * returns NULL.
+ */
+Datum
+pg_backup_start_time(PG_FUNCTION_ARGS)
+{
+ Datum xtime;
+ FILE *lfp;
+ char fline[MAXPGPATH];
+ char backup_start_time[30];
+
+ /*
+ * See if label file is present
+ */
+ lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
+ if (lfp == NULL)
+ {
+ if (errno != ENOENT)
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not read file \"%s\": %m",
+ BACKUP_LABEL_FILE)));
+ PG_RETURN_NULL();
+ }
+
+ /*
+ * Parse the file to find the the START TIME line.
+ */
+ backup_start_time[0] = '\0';
+ while (fgets(fline, sizeof(fline), lfp) != NULL)
+ {
+ if (sscanf(fline, "START TIME: %25[^\n]\n", backup_start_time) == 1)
+ break;
+ }
+
+ /*
+ * Close the backup label file.
+ */
+ if (ferror(lfp) || FreeFile(lfp))
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not read file \"%s\": %m", BACKUP_LABEL_FILE)));
+
+ if (strlen(backup_start_time) == 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
+
+ /*
+ * Convert the time string read from file to TimestampTz form.
+ */
+ xtime = DirectFunctionCall3(timestamptz_in,
+ CStringGetDatum(backup_start_time),
+ ObjectIdGetDatum(InvalidOid),
+ Int32GetDatum(-1));
+
+ PG_RETURN_DATUM(xtime);
+}