Skip to content

Commit 19ad13d

Browse files
committed
[Issue #134] PostgreSQL 12 support
1 parent 8ce5b7a commit 19ad13d

File tree

4 files changed

+134
-6
lines changed

4 files changed

+134
-6
lines changed

Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ src/datapagemap.c: $(top_srcdir)/src/bin/pg_rewind/datapagemap.c
5757
rm -f $@ && $(LN_S) $(srchome)/src/bin/pg_rewind/datapagemap.c $@
5858
src/datapagemap.h: $(top_srcdir)/src/bin/pg_rewind/datapagemap.h
5959
rm -f $@ && $(LN_S) $(srchome)/src/bin/pg_rewind/datapagemap.h $@
60-
src/logging.h: $(top_srcdir)/src/bin/pg_rewind/logging.h
61-
rm -f $@ && $(LN_S) $(srchome)/src/bin/pg_rewind/logging.h $@
6260
src/pg_crc.c: $(top_srcdir)/src/backend/utils/hash/pg_crc.c
6361
rm -f $@ && $(LN_S) $(srchome)/src/backend/utils/hash/pg_crc.c $@
6462
src/receivelog.c: $(top_srcdir)/src/bin/pg_basebackup/receivelog.c
@@ -72,6 +70,14 @@ src/streamutil.h: $(top_srcdir)/src/bin/pg_basebackup/streamutil.h
7270
src/xlogreader.c: $(top_srcdir)/src/backend/access/transam/xlogreader.c
7371
rm -f $@ && $(LN_S) $(srchome)/src/backend/access/transam/xlogreader.c $@
7472

73+
ifeq (12,$(MAJORVERSION))
74+
src/logging.h: $(top_srcdir)/src/include/common/logging.h
75+
rm -f $@ && $(LN_S) $(srchome)/src/include/common/logging.h $@
76+
else
77+
src/logging.h: $(top_srcdir)/src/bin/pg_rewind/logging.h
78+
rm -f $@ && $(LN_S) $(srchome)/src/bin/pg_rewind/logging.h $@
79+
endif
80+
7581
ifeq (,$(filter 9.5 9.6,$(MAJORVERSION)))
7682
src/walmethods.c: $(top_srcdir)/src/bin/pg_basebackup/walmethods.c
7783
rm -f $@ && $(LN_S) $(srchome)/src/bin/pg_basebackup/walmethods.c $@

src/data.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ do_decompress(void* dst, size_t dst_size, void const* src, size_t src_size,
111111
}
112112
#endif
113113
case PGLZ_COMPRESS:
114+
115+
#if PG_VERSION_NUM >= 120000
116+
return pglz_decompress(src, src_size, dst, dst_size, true);
117+
#else
114118
return pglz_decompress(src, src_size, dst, dst_size);
119+
#endif
115120
}
116121

117122
return -1;

src/dir.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static char *pgdata_exclude_files[] =
7979
"recovery.conf",
8080
"postmaster.pid",
8181
"postmaster.opts",
82+
"probackup_recovery.conf",
8283
NULL
8384
};
8485

src/restore.c

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static void create_recovery_conf(time_t backup_id,
4444
pgRestoreParams *params);
4545
static void *restore_files(void *arg);
4646
static void set_orphan_status(parray *backups, pgBackup *parent_backup);
47+
static void create_pg12_recovery_config(pgBackup *backup, bool add_include);
4748

4849
/*
4950
* Iterate over backup list to find all ancestors of the broken parent_backup
@@ -866,24 +867,43 @@ create_recovery_conf(time_t backup_id,
866867

867868
/* No need to generate recovery.conf at all. */
868869
if (!(need_restore_conf || params->restore_as_replica))
870+
{
871+
#if PG_VERSION_NUM >= 120000
872+
/*
873+
* Restoring STREAM backup without PITR and not as replica,
874+
* recovery.signal and standby.signal are not needed
875+
*/
876+
create_pg12_recovery_config(backup, false);
877+
#endif
869878
return;
879+
}
870880

871881
elog(LOG, "----------------------------------------");
882+
#if PG_VERSION_NUM >= 120000
883+
elog(LOG, "creating probackup_recovery.conf");
884+
create_pg12_recovery_config(backup, true);
885+
snprintf(path, lengthof(path), "%s/probackup_recovery.conf", instance_config.pgdata);
886+
#else
872887
elog(LOG, "creating recovery.conf");
873-
874888
snprintf(path, lengthof(path), "%s/recovery.conf", instance_config.pgdata);
889+
#endif
890+
875891
fp = fio_fopen(path, "w", FIO_DB_HOST);
876892
if (fp == NULL)
877-
elog(ERROR, "cannot open recovery.conf \"%s\": %s", path,
893+
elog(ERROR, "cannot open file \"%s\": %s", path,
878894
strerror(errno));
879895

896+
#if PG_VERSION_NUM >= 120000
897+
fio_fprintf(fp, "# probackup_recovery.conf generated by pg_probackup %s\n",
898+
PROGRAM_VERSION);
899+
#else
880900
fio_fprintf(fp, "# recovery.conf generated by pg_probackup %s\n",
881901
PROGRAM_VERSION);
902+
#endif
882903

883904
/* construct restore_command */
884905
if (need_restore_conf)
885906
{
886-
887907
char restore_command_guc[16384];
888908

889909
/* If restore_command is provided, use it */
@@ -957,15 +977,111 @@ create_recovery_conf(time_t backup_id,
957977

958978
if (params->restore_as_replica)
959979
{
980+
/* standby_mode was removed in PG12 */
981+
#if PG_VERSION_NUM < 120000
960982
fio_fprintf(fp, "standby_mode = 'on'\n");
983+
#endif
961984

962985
if (backup->primary_conninfo)
963986
fio_fprintf(fp, "primary_conninfo = '%s'\n", backup->primary_conninfo);
964987
}
965988

966989
if (fio_fflush(fp) != 0 ||
967990
fio_fclose(fp))
968-
elog(ERROR, "cannot write recovery.conf \"%s\": %s", path,
991+
elog(ERROR, "cannot write file \"%s\": %s", path,
992+
strerror(errno));
993+
994+
#if PG_VERSION_NUM >= 120000
995+
if (need_restore_conf)
996+
{
997+
elog(LOG, "creating recovery.signal file");
998+
snprintf(path, lengthof(path), "%s/recovery.signal", instance_config.pgdata);
999+
1000+
fp = fio_fopen(path, "w", FIO_DB_HOST);
1001+
if (fp == NULL)
1002+
elog(ERROR, "cannot open file \"%s\": %s", path,
1003+
strerror(errno));
1004+
1005+
if (fio_fflush(fp) != 0 ||
1006+
fio_fclose(fp))
1007+
elog(ERROR, "cannot write file \"%s\": %s", path,
1008+
strerror(errno));
1009+
}
1010+
1011+
if (params->restore_as_replica)
1012+
{
1013+
elog(LOG, "creating standby.signal file");
1014+
snprintf(path, lengthof(path), "%s/standby.signal", instance_config.pgdata);
1015+
1016+
fp = fio_fopen(path, "w", FIO_DB_HOST);
1017+
if (fp == NULL)
1018+
elog(ERROR, "cannot open file \"%s\": %s", path,
1019+
strerror(errno));
1020+
1021+
if (fio_fflush(fp) != 0 ||
1022+
fio_fclose(fp))
1023+
elog(ERROR, "cannot write file \"%s\": %s", path,
1024+
strerror(errno));
1025+
}
1026+
#endif
1027+
}
1028+
1029+
/*
1030+
* Create empty probackup_recovery.conf in PGDATA and
1031+
* add include directive to postgresql.auto.conf
1032+
1033+
* When restoring PG12 we always(!) must do this, even
1034+
* when restoring STREAM backup without PITR options
1035+
* because restored instance may have been backed up
1036+
* and restored again and user didn`t cleaned up postgresql.auto.conf.
1037+
1038+
* So for recovery to work regardless of all this factors
1039+
* we must always create empty probackup_recovery.conf file.
1040+
*/
1041+
static void
1042+
create_pg12_recovery_config(pgBackup *backup, bool add_include)
1043+
{
1044+
char probackup_recovery_path[MAXPGPATH];
1045+
char postgres_auto_path[MAXPGPATH];
1046+
FILE *fp;
1047+
1048+
if (add_include)
1049+
{
1050+
time_t current_time;
1051+
char current_time_str[100];
1052+
1053+
current_time = time(NULL);
1054+
time2iso(current_time_str, lengthof(current_time_str), current_time);
1055+
1056+
snprintf(postgres_auto_path, lengthof(postgres_auto_path),
1057+
"%s/postgresql.auto.conf", instance_config.pgdata);
1058+
1059+
fp = fio_fopen(postgres_auto_path, "a", FIO_DB_HOST);
1060+
if (fp == NULL)
1061+
elog(ERROR, "cannot write to file \"%s\": %s", postgres_auto_path,
1062+
strerror(errno));
1063+
1064+
fio_fprintf(fp, "\n# created by pg_probackup restore of backup %s at '%s'\n",
1065+
base36enc(backup->start_time), current_time_str);
1066+
fio_fprintf(fp, "include '%s'\n", "probackup_recovery.conf");
1067+
1068+
if (fio_fflush(fp) != 0 ||
1069+
fio_fclose(fp))
1070+
elog(ERROR, "cannot write to file \"%s\": %s", postgres_auto_path,
1071+
strerror(errno));
1072+
}
1073+
1074+
/* Create empty probackup_recovery.conf */
1075+
snprintf(probackup_recovery_path, lengthof(probackup_recovery_path),
1076+
"%s/probackup_recovery.conf", instance_config.pgdata);
1077+
fp = fio_fopen(probackup_recovery_path, "w", FIO_DB_HOST);
1078+
if (fp == NULL)
1079+
elog(ERROR, "cannot open file \"%s\": %s", probackup_recovery_path,
1080+
strerror(errno));
1081+
1082+
if (fio_fflush(fp) != 0 ||
1083+
fio_fclose(fp))
1084+
elog(ERROR, "cannot write to file \"%s\": %s", probackup_recovery_path,
9691085
strerror(errno));
9701086
}
9711087

0 commit comments

Comments
 (0)