Hi Gabriele, Le 31/03/2012 14:25, Gabriele Bartolini a écrit : > Hi Gilles, > > first and foremost, sorry for jumping in this thread so late. I > read all previous discussions and I'd be happy to help you with this > patch. > >> Agreed and sorry for the response delay. I've attached 2 patches >> here, the first one is the same as before with just the renaming of >> the function into pg_is_in_exclusive_backup(). > > My quick response: I really like the idea of having a function that > simply returns a boolean value. To be honest, I would have called it > pg_is_in_backup() as you originally did - after all, I do not execute > pg_start_exclusive_backup() or pg_stop_exclusive_backup(). It is more > intuitive and pairs with pg_is_in_recovery(). I have never found any > mention whatsoever in the documentation that talks about exclusive > backup, and I am afraid it would generate confusion.
+1, this is also my point of view. > However, I leave this up to the rest of the community's judgement > (here is my opinion). > > I agree also that a function that returns the timestamp of the > start of the backup might be useful. My opinion with the 'exclusive' > keyword applies here too (I would simply name it pg_backup_start_time()). Ok, I've reverted the name to pg_is_in_backup() and pg_backup_start_time(), if at the end the functions names have to be changed a simple replacement in the patch will be easy to do. > Finally, I tried to apply the patch but it looks like we need a new > version that can apply with current HEAD. If you can do that, I am > happy to assist with the review. I've attach 3 patches, the first one include both functions, the others are patches per function. They are all from the current HEAD branch, if they don't work please help me with the correct git/diff command to perform. Thank you, regards, -- Gilles Darold http://dalibo.com - http://dalibo.org
diff -ru postgresql-head/doc/src/sgml/func.sgml postgresql/doc/src/sgml/func.sgml --- postgresql-head/doc/src/sgml/func.sgml 2012-04-03 10:44:43.979702643 +0200 +++ postgresql/doc/src/sgml/func.sgml 2012-04-03 12:00:13.099700433 +0200 @@ -14467,6 +14467,12 @@ <primary>pg_stop_backup</primary> </indexterm> <indexterm> + <primary>pg_is_in_backup</primary> + </indexterm> + <indexterm> + <primary>pg_backup_start_time</primary> + </indexterm> + <indexterm> <primary>pg_switch_xlog</primary> </indexterm> <indexterm> @@ -14532,6 +14538,20 @@ </row> <row> <entry> + <literal><function>pg_is_in_backup()</function></literal> + </entry> + <entry><type>bool</type></entry> + <entry>True if an on-line exclusive backup is still in progress.</entry> + </row> + <row> + <entry> + <literal><function>pg_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_switch_xlog()</function></literal> </entry> <entry><type>text</type></entry> diff -ru postgresql-head/src/backend/access/transam/xlogfuncs.c postgresql/src/backend/access/transam/xlogfuncs.c --- postgresql-head/src/backend/access/transam/xlogfuncs.c 2012-04-03 10:44:44.227702645 +0200 +++ postgresql/src/backend/access/transam/xlogfuncs.c 2012-04-03 12:02:51.043700358 +0200 @@ -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,75 @@ 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) +{ + 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-head/src/include/access/xlog_internal.h postgresql/src/include/access/xlog_internal.h --- postgresql-head/src/include/access/xlog_internal.h 2012-04-03 10:44:45.051702642 +0200 +++ postgresql/src/include/access/xlog_internal.h 2012-04-03 12:06:38.955700246 +0200 @@ -282,5 +282,7 @@ extern Datum pg_xlog_replay_resume(PG_FUNCTION_ARGS); extern Datum pg_is_xlog_replay_paused(PG_FUNCTION_ARGS); extern Datum pg_xlog_location_diff(PG_FUNCTION_ARGS); +extern Datum pg_is_in_backup(PG_FUNCTION_ARGS); +extern Datum pg_backup_start_time(PG_FUNCTION_ARGS); #endif /* XLOG_INTERNAL_H */ diff -ru postgresql-head/src/include/catalog/pg_proc.h postgresql/src/include/catalog/pg_proc.h --- postgresql-head/src/include/catalog/pg_proc.h 2012-04-03 10:44:45.051702642 +0200 +++ postgresql/src/include/catalog/pg_proc.h 2012-04-03 12:07:44.555700213 +0200 @@ -2924,6 +2924,10 @@ DESCR("prepare for taking an online backup"); 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_ )); DESCR("finish taking an online backup"); +DATA(insert OID = 3882 ( 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_ )); +DESCR("true if server is in online backup"); +DATA(insert OID = 3883 ( 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_ )); +DESCR("start time of an online backup"); 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_ )); DESCR("switch to new xlog file"); 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_ ));
diff -ru postgresql-head/doc/src/sgml/func.sgml postgresql/doc/src/sgml/func.sgml --- postgresql-head/doc/src/sgml/func.sgml 2012-04-03 10:44:43.979702643 +0200 +++ postgresql/doc/src/sgml/func.sgml 2012-04-03 11:21:15.075701574 +0200 @@ -14467,6 +14467,9 @@ <primary>pg_stop_backup</primary> </indexterm> <indexterm> + <primary>pg_backup_start_time</primary> + </indexterm> + <indexterm> <primary>pg_switch_xlog</primary> </indexterm> <indexterm> @@ -14532,6 +14535,13 @@ </row> <row> <entry> + <literal><function>pg_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_switch_xlog()</function></literal> </entry> <entry><type>text</type></entry> diff -ru postgresql-head/src/backend/access/transam/xlogfuncs.c postgresql/src/backend/access/transam/xlogfuncs.c --- postgresql-head/src/backend/access/transam/xlogfuncs.c 2012-04-03 10:44:44.227702645 +0200 +++ postgresql/src/backend/access/transam/xlogfuncs.c 2012-04-03 11:21:15.075701574 +0200 @@ -29,6 +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 +564,67 @@ PG_RETURN_NUMERIC(result); } + +/* + * 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) +{ + 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-head/src/include/access/xlog_internal.h postgresql/src/include/access/xlog_internal.h --- postgresql-head/src/include/access/xlog_internal.h 2012-04-03 10:44:45.051702642 +0200 +++ postgresql/src/include/access/xlog_internal.h 2012-04-03 11:21:15.075701574 +0200 @@ -282,5 +282,6 @@ extern Datum pg_xlog_replay_resume(PG_FUNCTION_ARGS); extern Datum pg_is_xlog_replay_paused(PG_FUNCTION_ARGS); extern Datum pg_xlog_location_diff(PG_FUNCTION_ARGS); +extern Datum pg_backup_start_time(PG_FUNCTION_ARGS); #endif /* XLOG_INTERNAL_H */ diff -ru postgresql-head/src/include/catalog/pg_proc.h postgresql/src/include/catalog/pg_proc.h --- postgresql-head/src/include/catalog/pg_proc.h 2012-04-03 10:44:45.051702642 +0200 +++ postgresql/src/include/catalog/pg_proc.h 2012-04-03 11:21:15.075701574 +0200 @@ -2924,6 +2924,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 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 = 3883 ( 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_ )); +DESCR("start time of an online backup"); 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_ )); DESCR("switch to new xlog file"); 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_ ));
diff -ru postgresql-head/doc/src/sgml/func.sgml postgresql/doc/src/sgml/func.sgml --- postgresql-head/doc/src/sgml/func.sgml 2012-04-03 10:44:43.979702643 +0200 +++ postgresql/doc/src/sgml/func.sgml 2012-04-03 11:24:41.427701474 +0200 @@ -14467,6 +14467,9 @@ <primary>pg_stop_backup</primary> </indexterm> <indexterm> + <primary>pg_is_in_backup</primary> + </indexterm> + <indexterm> <primary>pg_switch_xlog</primary> </indexterm> <indexterm> @@ -14532,6 +14535,13 @@ </row> <row> <entry> + <literal><function>pg_is_in_backup()</function></literal> + </entry> + <entry><type>bool</type></entry> + <entry>True if an on-line exclusive backup is still in progress.</entry> + </row> + <row> + <entry> <literal><function>pg_switch_xlog()</function></literal> </entry> <entry><type>text</type></entry> diff -ru postgresql-head/src/backend/access/transam/xlogfuncs.c postgresql/src/backend/access/transam/xlogfuncs.c --- postgresql-head/src/backend/access/transam/xlogfuncs.c 2012-04-03 10:44:44.227702645 +0200 +++ postgresql/src/backend/access/transam/xlogfuncs.c 2012-04-03 11:24:41.427701474 +0200 @@ -563,3 +563,12 @@ 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()); +} diff -ru postgresql-head/src/include/access/xlog_internal.h postgresql/src/include/access/xlog_internal.h --- postgresql-head/src/include/access/xlog_internal.h 2012-04-03 10:44:45.051702642 +0200 +++ postgresql/src/include/access/xlog_internal.h 2012-04-03 11:24:41.427701474 +0200 @@ -282,5 +282,6 @@ extern Datum pg_xlog_replay_resume(PG_FUNCTION_ARGS); extern Datum pg_is_xlog_replay_paused(PG_FUNCTION_ARGS); extern Datum pg_xlog_location_diff(PG_FUNCTION_ARGS); +extern Datum pg_is_in_backup(PG_FUNCTION_ARGS); #endif /* XLOG_INTERNAL_H */ diff -ru postgresql-head/src/include/catalog/pg_proc.h postgresql/src/include/catalog/pg_proc.h --- postgresql-head/src/include/catalog/pg_proc.h 2012-04-03 10:44:45.051702642 +0200 +++ postgresql/src/include/catalog/pg_proc.h 2012-04-03 11:24:41.427701474 +0200 @@ -2924,6 +2924,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 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_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_ )); +DESCR("true if server is in online backup"); 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_ )); DESCR("switch to new xlog file"); 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_ ));
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers