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

Commit cb68f93

Browse files
committed
Add pg_get_snapshot_timestamp function
1 parent 2e7ce9a commit cb68f93

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

src/backend/storage/file/fd.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
#include "utils/guc.h"
9595
#include "utils/inval.h"
9696
#include "utils/resowner_private.h"
97+
#include "utils/timestamp.h"
9798

9899

99100
/* Define PG_FLUSH_DATA_WORKS if we have an implementation for pg_flush_data */
@@ -3965,6 +3966,40 @@ sfs_get_snapshot_size(SnapshotId snap_id)
39653966
return sfs_snapshot_size;
39663967
}
39673968

3969+
static time_t sfs_snapshot_time;
3970+
3971+
static void
3972+
sfs_snapshot_file_time(const char *fname, bool isdir, int elevel)
3973+
{
3974+
if (!isdir)
3975+
{
3976+
char* suf = strstr(fname, ".snap.");
3977+
if (suf != NULL)
3978+
{
3979+
int snap_id;
3980+
if (sscanf(suf + 6, "%d", &snap_id) == 1
3981+
&& snap_id == sfs_current_snapshot)
3982+
{
3983+
struct stat fst;
3984+
if (!stat(fname, &fst))
3985+
elog(elevel, "[SFS] Failed to stat snapshot file %s: %m", fname);
3986+
if (sfs_snapshot_time > fst.st_ctime) {
3987+
sfs_snapshot_time = fst.st_ctime;
3988+
}
3989+
}
3990+
}
3991+
}
3992+
}
3993+
3994+
TimestampTz
3995+
sfs_get_snapshot_timestamp(SnapshotId snap_id)
3996+
{
3997+
sfs_snapshot_time = -1;
3998+
sfs_current_snapshot = snap_id;
3999+
walk_data_dir(sfs_snapshot_file_time, LOG);
4000+
return time_t_to_timestamptz(sfs_snapshot_time);
4001+
}
4002+
39684003
void
39694004
sfs_remove_snapshot(SnapshotId snap_id)
39704005
{
@@ -4008,6 +4043,3 @@ sfs_recover_to_snapshot(SnapshotId snap_id)
40084043

40094044
sfs_unlock_database();
40104045
}
4011-
4012-
int64
4013-
sfs_get_shapshot_size(SnapshotId sid);

src/backend/storage/file/snapfs.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,10 @@ Datum pg_get_backend_snapshot(PG_FUNCTION_ARGS)
289289
{
290290
PG_RETURN_INT32(sfs_backend_snapshot);
291291
}
292+
293+
Datum pg_get_snapshot_timestamp(PG_FUNCTION_ARGS)
294+
{
295+
SnapshotId snap_id = PG_GETARG_INT32(0);
296+
PG_RETURN_TIMESTAMPTZ(sfs_get_snapshot_timestamp(snap_id));
297+
}
298+

src/include/catalog/pg_proc.dat

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10251,4 +10251,10 @@
1025110251
prorettype => 'int4', proargtypes => '',
1025210252
prosrc => 'pg_get_backend_snapshot' },
1025310253

10254+
{ oid => '6023',
10255+
descr => 'get snapshot timestamp',
10256+
proname => 'pg_get_snapshot_timestamp', provolatile => 'v',
10257+
proisstrict => 't', prorettype => 'timestamptz', proargtypes => 'int4',
10258+
prosrc => 'pg_get_snapshot_timestamp' },
10259+
1025410260
]

src/include/storage/snapfs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ extern void sfs_set_backend_snapshot(SnapshotId sid);
5151
*/
5252
extern int64 sfs_get_snapshot_size(SnapshotId sid);
5353

54+
/*
55+
* Get snapshot timestamp (time of first database modification in this snapshot)
56+
*/
57+
extern TimestampTz sfs_get_snapshot_timestamp(SnapshotId sid);
58+
5459
/*
5560
* Iternal functions and variables
5661
*/

0 commit comments

Comments
 (0)