Skip to content

Commit 364630e

Browse files
authored
Merge pull request #56 from galaxyproject/pgsql-15plus-pitr
Support PITR backups on PostgreSQL 15+
2 parents 0942512 + 9f8f86c commit 364630e

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

files/backup.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import psycopg2
3434

3535

36-
START_BACKUP_SQL = "SELECT pg_start_backup(%(label)s, false, false)"
37-
STOP_BACKUP_SQL = "SELECT * FROM pg_stop_backup(false, true)"
3836
RSYNC_EXCLUDES = (
3937
'pg_wal/*', # >= 10
4038
'pg_xlog/*', # < 10
@@ -87,10 +85,26 @@ def __init__(self):
8785
self._cursor = None
8886
self._label = None
8987
self._rsync_opts = None
88+
self._pg_major_version = None
9089

9190
def set_rsync_opts(self, opts):
9291
self._rsync_opts = opts
9392

93+
@property
94+
def pg_major_version(self):
95+
if self._pg_major_version is None:
96+
self.cursor.execute('SHOW server_version_num')
97+
# server_version_num is an int in the format (major*10000)+minor for > 9.6 or
98+
# (major*10000)+(minor*100)+patch for 9.6 and older.
99+
version_int = self.cursor.fetchone()[0]
100+
try:
101+
self._pg_major_version = int(int(version_int) / 10000)
102+
log.info("PostgreSQL server major version: %s", self._pg_major_version)
103+
except:
104+
log.error("Unable to parse PostgreSQL server_version: %s", version_int)
105+
raise
106+
return self._pg_major_version
107+
94108
@property
95109
def rsync_cmd(self):
96110
cmd = ['rsync']
@@ -152,7 +166,11 @@ def log_command(cmd):
152166

153167
def initiate_backup():
154168
log.info("Initiating backup with pg_start_backup()")
155-
state.cursor.execute(START_BACKUP_SQL, {'label': state.label})
169+
if state.pg_major_version < 15:
170+
start_backup_sql = "SELECT pg_start_backup(%(label)s, false, false)"
171+
else:
172+
start_backup_sql = "SELECT pg_backup_start(%(label)s, false)"
173+
state.cursor.execute(start_backup_sql, {'label': state.label})
156174

157175

158176
def perform_backup(backup_path, rsync_backup_opts):
@@ -193,7 +211,11 @@ def write_backup_file(backup_path, file_contents, file_name):
193211

194212
def finalize_backup(backup_path):
195213
log.info("Finalizing backup with pg_stop_backup()")
196-
state.cursor.execute(STOP_BACKUP_SQL)
214+
if state.pg_major_version < 15:
215+
stop_backup_sql = "SELECT * FROM pg_stop_backup(false, true)"
216+
else:
217+
stop_backup_sql = "SELECT * FROM pg_packup_stop(true)"
218+
state.cursor.execute(stop_backup_sql)
197219
row = state.cursor.fetchone()
198220
last_segment = row[0]
199221
backup_label = row[1]

0 commit comments

Comments
 (0)