Skip to content

Commit 7de7284

Browse files
kulaginmgsmolklubennikovaavindrups
authored
Catchup command implementation (#392)
[ PR #392] New command "catchup" is added, it allows fallen-behind standby to "catch up" with master, or create standby from scratch without resorting to restore from backup Co-authored-by: Grigory Smolkin <[email protected]> Co-authored-by: anastasia <[email protected]> Co-authored-by: Elena Indrupskaya <[email protected]>
1 parent b13d3b8 commit 7de7284

25 files changed

+3146
-102
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ OBJS = src/utils/configuration.o src/utils/json.o src/utils/logger.o \
77
OBJS += src/archive.o src/backup.o src/catalog.o src/checkdb.o src/configure.o src/data.o \
88
src/delete.o src/dir.o src/fetch.o src/help.o src/init.o src/merge.o \
99
src/parsexlog.o src/ptrack.o src/pg_probackup.o src/restore.o src/show.o src/stream.o \
10-
src/util.o src/validate.o src/datapagemap.o
10+
src/util.o src/validate.o src/datapagemap.o src/catchup.o
1111

1212
# borrowed files
1313
OBJS += src/pg_crc.o src/receivelog.o src/streamutil.o \

doc/pgprobackup.xml

Lines changed: 279 additions & 3 deletions
Large diffs are not rendered by default.

src/archive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ do_archive_push(InstanceState *instanceState, InstanceConfig *instance, char *wa
148148
elog(ERROR, "getcwd() error");
149149

150150
/* verify that archive-push --instance parameter is valid */
151-
system_id = get_system_identifier(current_dir);
151+
system_id = get_system_identifier(current_dir, FIO_DB_HOST);
152152

153153
if (instance->pgdata == NULL)
154154
elog(ERROR, "Cannot read pg_probackup.conf for this instance");

src/backup.c

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ do_backup_pg(InstanceState *instanceState, PGconn *backup_conn,
9494
{
9595
int i;
9696
char external_prefix[MAXPGPATH]; /* Temp value. Used as template */
97-
char dst_backup_path[MAXPGPATH];
9897
char label[1024];
9998
XLogRecPtr prev_backup_start_lsn = InvalidXLogRecPtr;
10099

@@ -137,7 +136,7 @@ do_backup_pg(InstanceState *instanceState, PGconn *backup_conn,
137136
#if PG_VERSION_NUM >= 90600
138137
current.tli = get_current_timeline(backup_conn);
139138
#else
140-
current.tli = get_current_timeline_from_control(false);
139+
current.tli = get_current_timeline_from_control(instance_config.pgdata, FIO_DB_HOST, false);
141140
#endif
142141

143142
/*
@@ -258,17 +257,19 @@ do_backup_pg(InstanceState *instanceState, PGconn *backup_conn,
258257
/* start stream replication */
259258
if (current.stream)
260259
{
261-
join_path_components(dst_backup_path, current.database_dir, PG_XLOG_DIR);
262-
fio_mkdir(dst_backup_path, DIR_PERMISSION, FIO_BACKUP_HOST);
260+
char stream_xlog_path[MAXPGPATH];
263261

264-
start_WAL_streaming(backup_conn, dst_backup_path, &instance_config.conn_opt,
262+
join_path_components(stream_xlog_path, current.database_dir, PG_XLOG_DIR);
263+
fio_mkdir(stream_xlog_path, DIR_PERMISSION, FIO_BACKUP_HOST);
264+
265+
start_WAL_streaming(backup_conn, stream_xlog_path, &instance_config.conn_opt,
265266
current.start_lsn, current.tli);
266267

267268
/* Make sure that WAL streaming is working
268269
* PAGE backup in stream mode is waited twice, first for
269270
* segment in WAL archive and then for streamed segment
270271
*/
271-
wait_wal_lsn(dst_backup_path, current.start_lsn, true, current.tli, false, true, ERROR, true);
272+
wait_wal_lsn(stream_xlog_path, current.start_lsn, true, current.tli, false, true, ERROR, true);
272273
}
273274

274275
/* initialize backup's file list */
@@ -315,23 +316,7 @@ do_backup_pg(InstanceState *instanceState, PGconn *backup_conn,
315316
elog(ERROR, "PGDATA is almost empty. Either it was concurrently deleted or "
316317
"pg_probackup do not possess sufficient permissions to list PGDATA content");
317318

318-
/* Calculate pgdata_bytes */
319-
for (i = 0; i < parray_num(backup_files_list); i++)
320-
{
321-
pgFile *file = (pgFile *) parray_get(backup_files_list, i);
322-
323-
if (file->external_dir_num != 0)
324-
continue;
325-
326-
if (S_ISDIR(file->mode))
327-
{
328-
current.pgdata_bytes += 4096;
329-
continue;
330-
}
331-
332-
current.pgdata_bytes += file->size;
333-
}
334-
319+
current.pgdata_bytes += calculate_datasize_of_filelist(backup_files_list);
335320
pretty_size(current.pgdata_bytes, pretty_bytes, lengthof(pretty_bytes));
336321
elog(INFO, "PGDATA size: %s", pretty_bytes);
337322

@@ -697,7 +682,7 @@ pgdata_basic_setup(ConnectionOptions conn_opt, PGNodeInfo *nodeInfo)
697682

698683
if (nodeInfo->is_superuser)
699684
elog(WARNING, "Current PostgreSQL role is superuser. "
700-
"It is not recommended to run backup or checkdb as superuser.");
685+
"It is not recommended to run pg_probackup under superuser.");
701686

702687
strlcpy(current.server_version, nodeInfo->server_version_str,
703688
sizeof(current.server_version));
@@ -786,7 +771,7 @@ do_backup(InstanceState *instanceState, pgSetBackupParams *set_backup_params,
786771
// elog(WARNING, "ptrack_version_num %d", ptrack_version_num);
787772

788773
if (nodeInfo.ptrack_version_num > 0)
789-
nodeInfo.is_ptrack_enable = pg_ptrack_enable(backup_conn, nodeInfo.ptrack_version_num);
774+
nodeInfo.is_ptrack_enabled = pg_is_ptrack_enabled(backup_conn, nodeInfo.ptrack_version_num);
790775

791776
if (current.backup_mode == BACKUP_MODE_DIFF_PTRACK)
792777
{
@@ -795,7 +780,7 @@ do_backup(InstanceState *instanceState, pgSetBackupParams *set_backup_params,
795780
elog(ERROR, "This PostgreSQL instance does not support ptrack");
796781
else
797782
{
798-
if (!nodeInfo.is_ptrack_enable)
783+
if (!nodeInfo.is_ptrack_enabled)
799784
elog(ERROR, "Ptrack is disabled");
800785
}
801786
}
@@ -953,12 +938,12 @@ check_server_version(PGconn *conn, PGNodeInfo *nodeInfo)
953938
* All system identifiers must be equal.
954939
*/
955940
void
956-
check_system_identifiers(PGconn *conn, char *pgdata)
941+
check_system_identifiers(PGconn *conn, const char *pgdata)
957942
{
958943
uint64 system_id_conn;
959944
uint64 system_id_pgdata;
960945

961-
system_id_pgdata = get_system_identifier(pgdata);
946+
system_id_pgdata = get_system_identifier(pgdata, FIO_DB_HOST);
962947
system_id_conn = get_remote_system_identifier(conn);
963948

964949
/* for checkdb check only system_id_pgdata and system_id_conn */
@@ -1069,7 +1054,7 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup,
10691054
* Switch to a new WAL segment. It should be called only for master.
10701055
* For PG 9.5 it should be called only if pguser is superuser.
10711056
*/
1072-
static void
1057+
void
10731058
pg_switch_wal(PGconn *conn)
10741059
{
10751060
PGresult *res;
@@ -2282,7 +2267,7 @@ process_block_change(ForkNumber forknum, RelFileNode rnode, BlockNumber blkno)
22822267

22832268
}
22842269

2285-
static void
2270+
void
22862271
check_external_for_tablespaces(parray *external_list, PGconn *backup_conn)
22872272
{
22882273
PGresult *res;
@@ -2346,3 +2331,36 @@ check_external_for_tablespaces(parray *external_list, PGconn *backup_conn)
23462331
}
23472332
}
23482333
}
2334+
2335+
/*
2336+
* Calculate pgdata_bytes
2337+
* accepts (parray *) of (pgFile *)
2338+
*/
2339+
int64
2340+
calculate_datasize_of_filelist(parray *filelist)
2341+
{
2342+
int64 bytes = 0;
2343+
int i;
2344+
2345+
/* parray_num don't check for NULL */
2346+
if (filelist == NULL)
2347+
return 0;
2348+
2349+
for (i = 0; i < parray_num(filelist); i++)
2350+
{
2351+
pgFile *file = (pgFile *) parray_get(filelist, i);
2352+
2353+
if (file->external_dir_num != 0)
2354+
continue;
2355+
2356+
if (S_ISDIR(file->mode))
2357+
{
2358+
// TODO is a dir always 4K?
2359+
bytes += 4096;
2360+
continue;
2361+
}
2362+
2363+
bytes += file->size;
2364+
}
2365+
return bytes;
2366+
}

src/catalog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2883,7 +2883,7 @@ pgNodeInit(PGNodeInfo *node)
28832883
node->server_version_str[0] = '\0';
28842884

28852885
node->ptrack_version_num = 0;
2886-
node->is_ptrack_enable = false;
2886+
node->is_ptrack_enabled = false;
28872887
node->ptrack_schema = NULL;
28882888
}
28892889

0 commit comments

Comments
 (0)