Skip to content

Commit

Permalink
Merge pull request #56 from galaxyproject/pgsql-15plus-pitr
Browse files Browse the repository at this point in the history
Support PITR backups on PostgreSQL 15+
  • Loading branch information
hexylena authored Apr 22, 2024
2 parents 0942512 + 9f8f86c commit 364630e
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions files/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import psycopg2


START_BACKUP_SQL = "SELECT pg_start_backup(%(label)s, false, false)"
STOP_BACKUP_SQL = "SELECT * FROM pg_stop_backup(false, true)"
RSYNC_EXCLUDES = (
'pg_wal/*', # >= 10
'pg_xlog/*', # < 10
Expand Down Expand Up @@ -87,10 +85,26 @@ def __init__(self):
self._cursor = None
self._label = None
self._rsync_opts = None
self._pg_major_version = None

def set_rsync_opts(self, opts):
self._rsync_opts = opts

@property
def pg_major_version(self):
if self._pg_major_version is None:
self.cursor.execute('SHOW server_version_num')
# server_version_num is an int in the format (major*10000)+minor for > 9.6 or
# (major*10000)+(minor*100)+patch for 9.6 and older.
version_int = self.cursor.fetchone()[0]
try:
self._pg_major_version = int(int(version_int) / 10000)
log.info("PostgreSQL server major version: %s", self._pg_major_version)
except:
log.error("Unable to parse PostgreSQL server_version: %s", version_int)
raise
return self._pg_major_version

@property
def rsync_cmd(self):
cmd = ['rsync']
Expand Down Expand Up @@ -152,7 +166,11 @@ def log_command(cmd):

def initiate_backup():
log.info("Initiating backup with pg_start_backup()")
state.cursor.execute(START_BACKUP_SQL, {'label': state.label})
if state.pg_major_version < 15:
start_backup_sql = "SELECT pg_start_backup(%(label)s, false, false)"
else:
start_backup_sql = "SELECT pg_backup_start(%(label)s, false)"
state.cursor.execute(start_backup_sql, {'label': state.label})


def perform_backup(backup_path, rsync_backup_opts):
Expand Down Expand Up @@ -193,7 +211,11 @@ def write_backup_file(backup_path, file_contents, file_name):

def finalize_backup(backup_path):
log.info("Finalizing backup with pg_stop_backup()")
state.cursor.execute(STOP_BACKUP_SQL)
if state.pg_major_version < 15:
stop_backup_sql = "SELECT * FROM pg_stop_backup(false, true)"
else:
stop_backup_sql = "SELECT * FROM pg_packup_stop(true)"
state.cursor.execute(stop_backup_sql)
row = state.cursor.fetchone()
last_segment = row[0]
backup_label = row[1]
Expand Down

0 comments on commit 364630e

Please sign in to comment.