Skip to content

Commit 56726b2

Browse files
committed
[Issue #128] Get timeline ID via pg_control_checkpoint() function
1 parent c2d32da commit 56726b2

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

Documentation.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ GRANT EXECUTE ON FUNCTION pg_catalog.pg_last_xlog_replay_location() TO backup;
245245
GRANT EXECUTE ON FUNCTION pg_catalog.txid_current() TO backup;
246246
GRANT EXECUTE ON FUNCTION pg_catalog.txid_current_snapshot() TO backup;
247247
GRANT EXECUTE ON FUNCTION pg_catalog.txid_snapshot_xmax(txid_snapshot) TO backup;
248+
GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_checkpoint() TO backup;
248249
COMMIT;
249250
```
250251

@@ -263,6 +264,7 @@ GRANT EXECUTE ON FUNCTION pg_catalog.pg_last_wal_replay_lsn() TO backup;
263264
GRANT EXECUTE ON FUNCTION pg_catalog.txid_current() TO backup;
264265
GRANT EXECUTE ON FUNCTION pg_catalog.txid_current_snapshot() TO backup;
265266
GRANT EXECUTE ON FUNCTION pg_catalog.txid_snapshot_xmax(txid_snapshot) TO backup;
267+
GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_checkpoint() TO backup;
266268
COMMIT;
267269
```
268270

src/backup.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,13 @@ do_backup_instance(PGconn *backup_conn, PGNodeInfo *nodeInfo)
183183
}
184184

185185
/* Obtain current timeline */
186-
current.tli = get_current_timeline(false);
186+
current.tli = get_current_timeline(backup_conn);
187+
188+
#if PG_VERSION_NUM >= 90600
189+
current.tli = get_current_timeline(backup_conn);
190+
#else
191+
current.tli = get_current_timeline_from_control(false);
192+
#endif
187193

188194
/*
189195
* In incremental backup mode ensure that already-validated

src/pg_probackup.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,8 @@ extern XLogRecPtr get_first_record_lsn(const char *archivedir, XLogRecPtr start_
788788
TimeLineID tli, uint32 wal_seg_size);
789789

790790
/* in util.c */
791-
extern TimeLineID get_current_timeline(bool safe);
791+
extern TimeLineID get_current_timeline(PGconn *conn);
792+
extern TimeLineID get_current_timeline_from_control(bool safe);
792793
extern XLogRecPtr get_checkpoint_location(PGconn *conn);
793794
extern uint64 get_system_identifier(const char *pgdata_path);
794795
extern uint64 get_remote_system_identifier(PGconn *conn);

src/util.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,36 @@ writeControlFile(ControlFileData *ControlFile, char *path, fio_location location
146146
* used by a node.
147147
*/
148148
TimeLineID
149-
get_current_timeline(bool safe)
149+
get_current_timeline(PGconn *conn)
150+
{
151+
152+
PGresult *res;
153+
TimeLineID tli = 0;
154+
char *val;
155+
156+
res = pgut_execute_extended(conn,
157+
"SELECT timeline_id FROM pg_control_checkpoint()", 0, NULL, true, true);
158+
159+
if (PQresultStatus(res) == PGRES_TUPLES_OK)
160+
val = PQgetvalue(res, 0, 0);
161+
else
162+
return get_current_timeline_from_control(false);
163+
164+
if (!parse_uint32(val, &tli, 0))
165+
{
166+
PQclear(res);
167+
elog(WARNING, "Invalid value of timeline_id %s", val);
168+
169+
/* TODO 3.0 remove it and just error out */
170+
return get_current_timeline_from_control(false);
171+
}
172+
173+
return tli;
174+
}
175+
176+
/* Get timeline from pg_control file */
177+
TimeLineID
178+
get_current_timeline_from_control(bool safe)
150179
{
151180
ControlFileData ControlFile;
152181
char *buffer;
@@ -164,6 +193,8 @@ get_current_timeline(bool safe)
164193
return ControlFile.checkPointCopy.ThisTimeLineID;
165194
}
166195

196+
197+
167198
/*
168199
* Get last check point record ptr from pg_tonrol.
169200
*/

0 commit comments

Comments
 (0)