|
33 | 33 | import psycopg2 |
34 | 34 |
|
35 | 35 |
|
36 | | -START_BACKUP_SQL = "SELECT pg_start_backup(%(label)s, false, false)" |
37 | | -STOP_BACKUP_SQL = "SELECT * FROM pg_stop_backup(false, true)" |
38 | 36 | RSYNC_EXCLUDES = ( |
39 | 37 | 'pg_wal/*', # >= 10 |
40 | 38 | 'pg_xlog/*', # < 10 |
@@ -87,10 +85,26 @@ def __init__(self): |
87 | 85 | self._cursor = None |
88 | 86 | self._label = None |
89 | 87 | self._rsync_opts = None |
| 88 | + self._pg_major_version = None |
90 | 89 |
|
91 | 90 | def set_rsync_opts(self, opts): |
92 | 91 | self._rsync_opts = opts |
93 | 92 |
|
| 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 | + |
94 | 108 | @property |
95 | 109 | def rsync_cmd(self): |
96 | 110 | cmd = ['rsync'] |
@@ -152,7 +166,11 @@ def log_command(cmd): |
152 | 166 |
|
153 | 167 | def initiate_backup(): |
154 | 168 | 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}) |
156 | 174 |
|
157 | 175 |
|
158 | 176 | def perform_backup(backup_path, rsync_backup_opts): |
@@ -193,7 +211,11 @@ def write_backup_file(backup_path, file_contents, file_name): |
193 | 211 |
|
194 | 212 | def finalize_backup(backup_path): |
195 | 213 | 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) |
197 | 219 | row = state.cursor.fetchone() |
198 | 220 | last_segment = row[0] |
199 | 221 | backup_label = row[1] |
|
0 commit comments