Skip to content

Commit 78adfdf

Browse files
committed
Port to PostgreSQL 11
1 parent cfb63fb commit 78adfdf

File tree

15 files changed

+264
-96
lines changed

15 files changed

+264
-96
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ else
3232
srchome=$(top_srcdir)
3333
endif
3434

35-
ifeq ($(MAJORVERSION),10)
35+
ifneq (,$(filter 10 11 12,$(MAJORVERSION)))
3636
OBJS += src/walmethods.o
3737
EXTRA_CLEAN += src/walmethods.c src/walmethods.h
3838
INCLUDES += src/walmethods.h
@@ -64,7 +64,7 @@ src/streamutil.h: $(top_srcdir)/src/bin/pg_basebackup/streamutil.h
6464
rm -f $@ && $(LN_S) $(srchome)/src/bin/pg_basebackup/streamutil.h $@
6565

6666

67-
ifeq ($(MAJORVERSION),10)
67+
ifneq (,$(filter 10 11 12,$(MAJORVERSION)))
6868
src/walmethods.c: $(top_srcdir)/src/bin/pg_basebackup/walmethods.c
6969
rm -f $@ && $(LN_S) $(srchome)/src/bin/pg_basebackup/walmethods.c $@
7070
src/walmethods.h: $(top_srcdir)/src/bin/pg_basebackup/walmethods.h

src/backup.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,9 @@ do_backup_instance(void)
650650
* reading WAL segments present in archives up to the point
651651
* where this backup has started.
652652
*/
653-
extractPageMap(arclog_path, prev_backup->start_lsn, current.tli,
654-
current.start_lsn, backup_files_list);
653+
extractPageMap(arclog_path, current.tli, xlog_seg_size,
654+
prev_backup->start_lsn, current.start_lsn,
655+
backup_files_list);
655656
}
656657
else if (current.backup_mode == BACKUP_MODE_DIFF_PTRACK)
657658
{
@@ -827,6 +828,11 @@ do_backup(time_t start_time)
827828

828829
current.primary_conninfo = pgut_get_conninfo_string(backup_conn);
829830

831+
#if PG_VERSION_NUM >= 110000
832+
if (!RetrieveWalSegSize(backup_conn))
833+
elog(ERROR, "Failed to retreive wal_segment_size");
834+
#endif
835+
830836
current.compress_alg = compress_alg;
831837
current.compress_level = compress_level;
832838

@@ -918,8 +924,9 @@ do_backup(time_t start_time)
918924
/* compute size of wal files of this backup stored in the archive */
919925
if (!current.stream)
920926
{
921-
current.wal_bytes = XLOG_SEG_SIZE *
922-
(current.stop_lsn/XLogSegSize - current.start_lsn/XLogSegSize + 1);
927+
current.wal_bytes = xlog_seg_size *
928+
(current.stop_lsn / xlog_seg_size -
929+
current.start_lsn / xlog_seg_size + 1);
923930
}
924931

925932
/* Backup is done. Update backup status */
@@ -1467,10 +1474,10 @@ wait_wal_lsn(XLogRecPtr lsn, bool is_start_lsn, bool wait_prev_segment)
14671474
tli = get_current_timeline(false);
14681475

14691476
/* Compute the name of the WAL file containig requested LSN */
1470-
XLByteToSeg(lsn, targetSegNo);
1477+
GetXLogSegNo(lsn, targetSegNo, xlog_seg_size);
14711478
if (wait_prev_segment)
14721479
targetSegNo--;
1473-
XLogFileName(wal_segment, tli, targetSegNo);
1480+
GetXLogFileName(wal_segment, tli, targetSegNo, xlog_seg_size);
14741481

14751482
/*
14761483
* In pg_start_backup we wait for 'lsn' in 'pg_wal' directory iff it is
@@ -1536,7 +1543,7 @@ wait_wal_lsn(XLogRecPtr lsn, bool is_start_lsn, bool wait_prev_segment)
15361543
/*
15371544
* A WAL segment found. Check LSN on it.
15381545
*/
1539-
if (wal_contains_lsn(wal_segment_dir, lsn, tli))
1546+
if (wal_contains_lsn(wal_segment_dir, lsn, tli, xlog_seg_size))
15401547
/* Target LSN was found */
15411548
{
15421549
elog(LOG, "Found LSN: %X/%X", (uint32) (lsn >> 32), (uint32) lsn);
@@ -1946,7 +1953,7 @@ pg_stop_backup(pgBackup *backup)
19461953

19471954
elog(LOG, "Getting the Recovery Time from WAL");
19481955

1949-
if (!read_recovery_info(xlog_path, backup->tli,
1956+
if (!read_recovery_info(xlog_path, backup->tli, xlog_seg_size,
19501957
backup->start_lsn, backup->stop_lsn,
19511958
&backup->recovery_time, &backup->recovery_xid))
19521959
{
@@ -2553,7 +2560,7 @@ StreamLog(void *arg)
25532560
/*
25542561
* Always start streaming at the beginning of a segment
25552562
*/
2556-
startpos -= startpos % XLOG_SEG_SIZE;
2563+
startpos -= startpos % xlog_seg_size;
25572564

25582565
/* Initialize timeout */
25592566
stream_stop_timeout = 0;

src/catalog.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -572,23 +572,23 @@ readBackupControlFile(const char *path)
572572
pgBackupInit(backup);
573573
if (access(path, F_OK) != 0)
574574
{
575-
elog(WARNING, "control file \"%s\" doesn't exist", path);
575+
elog(WARNING, "Control file \"%s\" doesn't exist", path);
576576
pgBackupFree(backup);
577577
return NULL;
578578
}
579579

580-
parsed_options = pgut_readopt(path, options, WARNING);
580+
parsed_options = pgut_readopt(path, options, WARNING, true);
581581

582582
if (parsed_options == 0)
583583
{
584-
elog(WARNING, "control file \"%s\" is empty", path);
584+
elog(WARNING, "Control file \"%s\" is empty", path);
585585
pgBackupFree(backup);
586586
return NULL;
587587
}
588588

589589
if (backup->start_time == 0)
590590
{
591-
elog(WARNING, "invalid ID/start-time, control file \"%s\" is corrupted", path);
591+
elog(WARNING, "Invalid ID/start-time, control file \"%s\" is corrupted", path);
592592
pgBackupFree(backup);
593593
return NULL;
594594
}
@@ -607,7 +607,7 @@ readBackupControlFile(const char *path)
607607
if (sscanf(start_lsn, "%X/%X", &xlogid, &xrecoff) == 2)
608608
backup->start_lsn = (XLogRecPtr) ((uint64) xlogid << 32) | xrecoff;
609609
else
610-
elog(WARNING, "invalid START_LSN \"%s\"", start_lsn);
610+
elog(WARNING, "Invalid START_LSN \"%s\"", start_lsn);
611611
free(start_lsn);
612612
}
613613

@@ -619,7 +619,7 @@ readBackupControlFile(const char *path)
619619
if (sscanf(stop_lsn, "%X/%X", &xlogid, &xrecoff) == 2)
620620
backup->stop_lsn = (XLogRecPtr) ((uint64) xlogid << 32) | xrecoff;
621621
else
622-
elog(WARNING, "invalid STOP_LSN \"%s\"", stop_lsn);
622+
elog(WARNING, "Invalid STOP_LSN \"%s\"", stop_lsn);
623623
free(stop_lsn);
624624
}
625625

@@ -644,7 +644,7 @@ readBackupControlFile(const char *path)
644644
else if (strcmp(status, "CORRUPT") == 0)
645645
backup->status = BACKUP_STATUS_CORRUPT;
646646
else
647-
elog(WARNING, "invalid STATUS \"%s\"", status);
647+
elog(WARNING, "Invalid STATUS \"%s\"", status);
648648
free(status);
649649
}
650650

src/configure.c

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ void
102102
pgBackupConfigInit(pgBackupConfig *config)
103103
{
104104
config->system_identifier = 0;
105+
106+
#if PG_VERSION_NUM >= 110000
107+
config->xlog_seg_size = 0;
108+
#else
109+
config->xlog_seg_size = XLOG_SEG_SIZE;
110+
#endif
111+
105112
config->pgdata = NULL;
106113
config->pgdatabase = NULL;
107114
config->pghost = NULL;
@@ -140,6 +147,9 @@ writeBackupCatalogConfig(FILE *out, pgBackupConfig *config)
140147
fprintf(out, "#Backup instance info\n");
141148
fprintf(out, "PGDATA = %s\n", config->pgdata);
142149
fprintf(out, "system-identifier = " UINT64_FORMAT "\n", config->system_identifier);
150+
#if PG_VERSION_NUM >= 110000
151+
fprintf(out, "xlog-seg-size = %u\n", config->xlog_seg_size);
152+
#endif
143153

144154
fprintf(out, "#Connection parameters:\n");
145155
if (config->pgdatabase)
@@ -253,6 +263,9 @@ readBackupCatalogConfigFile(void)
253263
{ 'u', 0, "replica-timeout", &(config->replica_timeout), SOURCE_CMDLINE, SOURCE_DEFAULT, OPTION_UNIT_MS },
254264
/* other options */
255265
{ 'U', 0, "system-identifier", &(config->system_identifier), SOURCE_FILE_STRICT },
266+
#if PG_VERSION_NUM >= 110000
267+
{'u', 0, "xlog-seg-size", &config->xlog_seg_size, SOURCE_FILE_STRICT},
268+
#endif
256269
/* archive options */
257270
{ 'u', 0, "archive-timeout", &(config->archive_timeout), SOURCE_CMDLINE, SOURCE_DEFAULT, OPTION_UNIT_MS },
258271
{0}
@@ -263,11 +276,44 @@ readBackupCatalogConfigFile(void)
263276
join_path_components(path, backup_instance_path, BACKUP_CATALOG_CONF_FILE);
264277

265278
pgBackupConfigInit(config);
266-
pgut_readopt(path, options, ERROR);
279+
pgut_readopt(path, options, ERROR, true);
280+
281+
#if PG_VERSION_NUM >= 110000
282+
if (!IsValidWalSegSize(config->xlog_seg_size))
283+
elog(ERROR, "Invalid WAL segment size %u", config->xlog_seg_size);
284+
#endif
267285

268286
return config;
269287
}
270288

289+
/*
290+
* Read xlog-seg-size from BACKUP_CATALOG_CONF_FILE.
291+
*/
292+
uint32
293+
get_config_xlog_seg_size(void)
294+
{
295+
#if PG_VERSION_NUM >= 110000
296+
char path[MAXPGPATH];
297+
uint32 seg_size;
298+
pgut_option options[] =
299+
{
300+
{'u', 0, "xlog-seg-size", &seg_size, SOURCE_FILE_STRICT},
301+
{0}
302+
};
303+
304+
join_path_components(path, backup_instance_path, BACKUP_CATALOG_CONF_FILE);
305+
pgut_readopt(path, options, ERROR, false);
306+
307+
if (!IsValidWalSegSize(seg_size))
308+
elog(ERROR, "Invalid WAL segment size %u", seg_size);
309+
310+
return seg_size;
311+
312+
#else
313+
return (uint32) XLOG_SEG_SIZE;
314+
#endif
315+
}
316+
271317
static void
272318
opt_log_level_console(pgut_option *opt, const char *arg)
273319
{
@@ -349,6 +395,11 @@ show_configure_json(pgBackupConfig *config)
349395
json_add_key(buf, "system-identifier", json_level, true);
350396
appendPQExpBuffer(buf, UINT64_FORMAT, config->system_identifier);
351397

398+
#if PG_VERSION_NUM >= 110000
399+
json_add_key(buf, "xlog-seg-size", json_level, true);
400+
appendPQExpBuffer(buf, "%u", config->xlog_seg_size);
401+
#endif
402+
352403
/* Connection parameters */
353404
if (config->pgdatabase)
354405
json_add_value(buf, "pgdatabase", config->pgdatabase, json_level, true);

src/delete.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
#include <unistd.h>
1616

1717
static int pgBackupDeleteFiles(pgBackup *backup);
18-
static void delete_walfiles(XLogRecPtr oldest_lsn, TimeLineID oldest_tli);
18+
static void delete_walfiles(XLogRecPtr oldest_lsn, TimeLineID oldest_tli,
19+
uint32 xlog_seg_size);
1920

2021
int
2122
do_delete(time_t backup_id)
2223
{
2324
int i;
2425
parray *backup_list,
2526
*delete_list;
27+
pgBackup *target_backup = NULL;
2628
time_t parent_id = 0;
27-
bool backup_found = false;
2829
XLogRecPtr oldest_lsn = InvalidXLogRecPtr;
2930
TimeLineID oldest_tli = 0;
3031

@@ -56,9 +57,9 @@ do_delete(time_t backup_id)
5657

5758
/* Save backup id to retreive increment backups */
5859
parent_id = backup->start_time;
59-
backup_found = true;
60+
target_backup = backup;
6061
}
61-
else if (backup_found)
62+
else if (target_backup)
6263
{
6364
if (backup->backup_mode != BACKUP_MODE_FULL &&
6465
backup->parent_backup == parent_id)
@@ -93,6 +94,8 @@ do_delete(time_t backup_id)
9394
/* Clean WAL segments */
9495
if (delete_wal)
9596
{
97+
Assert(target_backup);
98+
9699
/* Find oldest LSN, used by backups */
97100
for (i = (int) parray_num(backup_list) - 1; i >= 0; i--)
98101
{
@@ -106,7 +109,7 @@ do_delete(time_t backup_id)
106109
}
107110
}
108111

109-
delete_walfiles(oldest_lsn, oldest_tli);
112+
delete_walfiles(oldest_lsn, oldest_tli, xlog_seg_size);
110113
}
111114

112115
/* cleanup */
@@ -225,7 +228,7 @@ do_retention_purge(void)
225228
/* Purge WAL files */
226229
if (delete_wal)
227230
{
228-
delete_walfiles(oldest_lsn, oldest_tli);
231+
delete_walfiles(oldest_lsn, oldest_tli, xlog_seg_size);
229232
}
230233

231234
/* Cleanup */
@@ -313,7 +316,8 @@ pgBackupDeleteFiles(pgBackup *backup)
313316
* oldest_lsn.
314317
*/
315318
static void
316-
delete_walfiles(XLogRecPtr oldest_lsn, TimeLineID oldest_tli)
319+
delete_walfiles(XLogRecPtr oldest_lsn, TimeLineID oldest_tli,
320+
uint32 xlog_seg_size)
317321
{
318322
XLogSegNo targetSegNo;
319323
char oldestSegmentNeeded[MAXFNAMELEN];
@@ -329,8 +333,9 @@ delete_walfiles(XLogRecPtr oldest_lsn, TimeLineID oldest_tli)
329333

330334
if (!XLogRecPtrIsInvalid(oldest_lsn))
331335
{
332-
XLByteToSeg(oldest_lsn, targetSegNo);
333-
XLogFileName(oldestSegmentNeeded, oldest_tli, targetSegNo);
336+
GetXLogSegNo(oldest_lsn, targetSegNo, xlog_seg_size);
337+
GetXLogFileName(oldestSegmentNeeded, oldest_tli, targetSegNo,
338+
xlog_seg_size);
334339

335340
elog(LOG, "removing WAL segments older than %s", oldestSegmentNeeded);
336341
}
@@ -436,7 +441,7 @@ do_delete_instance(void)
436441
parray_free(backup_list);
437442

438443
/* Delete all wal files. */
439-
delete_walfiles(InvalidXLogRecPtr, 0);
444+
delete_walfiles(InvalidXLogRecPtr, 0, xlog_seg_size);
440445

441446
/* Delete backup instance config file */
442447
join_path_components(instance_config_path, backup_instance_path, BACKUP_CATALOG_CONF_FILE);

src/init.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ do_add_instance(void)
5454
{
5555
char path[MAXPGPATH];
5656
char arclog_path_dir[MAXPGPATH];
57-
struct stat st;
57+
struct stat st;
5858
pgBackupConfig *config = pgut_new(pgBackupConfig);
5959

6060
/* PGDATA is always required */
@@ -64,6 +64,8 @@ do_add_instance(void)
6464

6565
/* Read system_identifier from PGDATA */
6666
system_identifier = get_system_identifier(pgdata);
67+
/* Starting from PostgreSQL 11 read WAL segment size from PGDATA */
68+
xlog_seg_size = get_xlog_seg_size(pgdata);
6769

6870
/* Ensure that all root directories already exist */
6971
if (access(backup_path, F_OK) != 0)
@@ -97,6 +99,7 @@ do_add_instance(void)
9799
*/
98100
pgBackupConfigInit(config);
99101
config->system_identifier = system_identifier;
102+
config->xlog_seg_size = xlog_seg_size;
100103
config->pgdata = pgdata;
101104
writeBackupCatalogConfigFile(config);
102105

src/merge.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,10 @@ merge_backups(pgBackup *to_backup, pgBackup *from_backup)
320320
to_backup->data_bytes += file->write_size;
321321
}
322322
/* compute size of wal files of this backup stored in the archive */
323-
if (!current.stream)
324-
to_backup->wal_bytes = XLOG_SEG_SIZE *
325-
(to_backup->stop_lsn / XLogSegSize - to_backup->start_lsn / XLogSegSize + 1);
323+
if (!to_backup->stream)
324+
to_backup->wal_bytes = xlog_seg_size *
325+
(to_backup->stop_lsn / xlog_seg_size -
326+
to_backup->start_lsn / xlog_seg_size + 1);
326327
else
327328
to_backup->wal_bytes = BYTES_INVALID;
328329

0 commit comments

Comments
 (0)