postgresql-exclusive_backup_start_time-patch.diff
text/x-patch
Filename: postgresql-exclusive_backup_start_time-patch.diff
Type: text/x-patch
Part: 1
Message:
Re: Patch pg_is_in_backup()
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: unified
| File | + | − |
|---|---|---|
| doc/src/sgml/func.sgml | 11 | 0 |
| src/backend/access/transam/xlogfuncs.c | 69 | 0 |
| src/include/access/xlog_internal.h | 1 | 0 |
| src/include/catalog/pg_proc.h | 2 | 0 |
diff -ru postgresql/doc/src/sgml/func.sgml postgresql-exclusive_backup_start_time-patch/doc/src/sgml/func.sgml
--- postgresql/doc/src/sgml/func.sgml 2012-01-26 23:01:31.956613398 +0100
+++ postgresql-exclusive_backup_start_time-patch/doc/src/sgml/func.sgml 2012-03-02 10:56:56.986487006 +0100
@@ -14371,6 +14371,9 @@
<primary>pg_current_xlog_location</primary>
</indexterm>
<indexterm>
+ <primary>pg_exclusive_backup_start_time</primary>
+ </indexterm>
+ <indexterm>
<primary>pg_start_backup</primary>
</indexterm>
<indexterm>
@@ -14424,6 +14427,14 @@
</row>
<row>
<entry>
+ <literal><function>pg_exclusive_backup_start_time()</function></literal>
+ </entry>
+ <entry><type>timestamp with time zone</type></entry>
+ <entry>Get start time of an on-line exclusive backup in progress.
+ </entry>
+ </row>
+ <row>
+ <entry>
<literal><function>pg_start_backup(<parameter>label</> <type>text</> <optional>, <parameter>fast</> <type>boolean</> </optional>)</function></literal>
</entry>
<entry><type>text</type></entry>
diff -ru postgresql/src/backend/access/transam/xlogfuncs.c postgresql-exclusive_backup_start_time-patch/src/backend/access/transam/xlogfuncs.c
--- postgresql/src/backend/access/transam/xlogfuncs.c 2012-03-02 15:21:42.278479256 +0100
+++ postgresql-exclusive_backup_start_time-patch/src/backend/access/transam/xlogfuncs.c 2012-03-02 15:17:50.390479369 +0100
@@ -28,6 +28,7 @@
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/timestamp.h"
+#include "storage/fd.h"
/*
* pg_start_backup: set up for taking an on-line backup dump
@@ -465,3 +466,71 @@
{
PG_RETURN_BOOL(RecoveryInProgress());
}
+
+
+/*
+ * Returns start time of an online exclusive backup.
+ *
+ * When there's no exclusive backup in progress, the function
+ * returns NULL.
+ */
+Datum
+pg_exclusive_backup_start_time(PG_FUNCTION_ARGS)
+{
+ TimestampTz 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 = DatumGetTimestampTz(
+ DirectFunctionCall3(timestamptz_in, CStringGetDatum(backup_start_time),
+ ObjectIdGetDatum(InvalidOid),Int32GetDatum(-1))
+ );
+
+
+ PG_RETURN_TIMESTAMPTZ(xtime);
+}
+
diff -ru postgresql/src/include/access/xlog_internal.h postgresql-exclusive_backup_start_time-patch/src/include/access/xlog_internal.h
--- postgresql/src/include/access/xlog_internal.h 2012-01-26 23:01:32.332613398 +0100
+++ postgresql-exclusive_backup_start_time-patch/src/include/access/xlog_internal.h 2012-03-02 11:04:48.378486777 +0100
@@ -281,5 +281,6 @@
extern Datum pg_xlog_replay_pause(PG_FUNCTION_ARGS);
extern Datum pg_xlog_replay_resume(PG_FUNCTION_ARGS);
extern Datum pg_is_xlog_replay_paused(PG_FUNCTION_ARGS);
+extern Datum pg_exclusive_backup_start_time(PG_FUNCTION_ARGS);
#endif /* XLOG_INTERNAL_H */
diff -ru postgresql/src/include/catalog/pg_proc.h postgresql-exclusive_backup_start_time-patch/src/include/catalog/pg_proc.h
--- postgresql/src/include/catalog/pg_proc.h 2012-01-26 23:01:32.340613398 +0100
+++ postgresql-exclusive_backup_start_time-patch/src/include/catalog/pg_proc.h 2012-03-02 11:02:45.346486836 +0100
@@ -2899,6 +2899,8 @@
DESCR("prepare for taking an online backup");
DATA(insert OID = 2173 ( pg_stop_backup PGNSP PGUID 12 1 0 0 0 f f f t f v 0 0 25 "" _null_ _null_ _null_ _null_ pg_stop_backup _null_ _null_ _null_ ));
DESCR("finish taking an online backup");
+DATA(insert OID = 3882 ( pg_exclusive_backup_start_time PGNSP PGUID 12 1 0 0 0 f f f t f s 0 0 1184 "" _null_ _null_ _null_ _null_ pg_exclusive_backup_start_time _null_ _null_ _null_ ));
+DESCR("start time of an online backup");
DATA(insert OID = 2848 ( pg_switch_xlog PGNSP PGUID 12 1 0 0 0 f f f t f v 0 0 25 "" _null_ _null_ _null_ _null_ pg_switch_xlog _null_ _null_ _null_ ));
DESCR("switch to new xlog file");
DATA(insert OID = 3098 ( pg_create_restore_point PGNSP PGUID 12 1 0 0 0 f f f t f v 1 0 25 "25" _null_ _null_ _null_ _null_ pg_create_restore_point _null_ _null_ _null_ ));