Skip to content

Commit

Permalink
Ensure correct bin_dir for pg params configuration (#966)
Browse files Browse the repository at this point in the history
Make in-place upgrade code use the bin_dir of the proper version
throught the whole process, so that
ConfigHandler.write_postgresql_conf() validates provided params using
the proper PG version.
  • Loading branch information
hughcapet authored Feb 15, 2024
1 parent 94f287d commit 9091d78
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
3 changes: 0 additions & 3 deletions postgres-appliance/major_upgrade/inplace_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,7 @@ def do_upgrade(self):
if self.replica_connections:
from patroni.postgresql.misc import parse_lsn

# Make sure we use the pg_controldata from the correct major version
self.postgresql.set_bin_dir(self.cluster_version)
controldata = self.postgresql.controldata()
self.postgresql.set_bin_dir(self.desired_version)

checkpoint_lsn = controldata.get('Latest checkpoint location')
if controldata.get('Database cluster state') != 'shut down' or not checkpoint_lsn:
Expand Down
23 changes: 17 additions & 6 deletions postgres-appliance/major_upgrade/pg_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def restore_shared_preload_libraries(self):
return True

def start_old_cluster(self, config, version):
self.set_bin_dir(version)
self._new_bin_dir = self._bin_dir
self.set_bin_dir_for_version(version)
self._old_bin_dir = self._bin_dir

# make sure we don't archive wals from the old version
self._old_config_values = {'archive_mode': self.config.get('parameters').get('archive_mode')}
Expand All @@ -50,11 +52,13 @@ def get_cluster_version(self):
with open(self._version_file) as f:
return f.read().strip()

def set_bin_dir(self, version):
def set_bin_dir_for_version(self, version):
from spilo_commons import get_bin_dir
self.set_bin_dir(get_bin_dir(version))

self._old_bin_dir = self._bin_dir
self._bin_dir = get_bin_dir(version)
def set_bin_dir(self, bin_dir):
self._bin_dir = bin_dir
self._available_gucs = None

@property
def local_conn_kwargs(self):
Expand Down Expand Up @@ -174,7 +178,7 @@ def pg_upgrade(self, check=False):
os.chdir(upgrade_dir)

pg_upgrade_args = ['-k', '-j', str(psutil.cpu_count()),
'-b', self._old_bin_dir, '-B', self._bin_dir,
'-b', self._old_bin_dir, '-B', self._new_bin_dir,
'-d', self._data_dir, '-D', self._new_data_dir,
'-O', "-c timescaledb.restoring='on'",
'-O', "-c archive_mode='off'"]
Expand All @@ -186,8 +190,12 @@ def pg_upgrade(self, check=False):
else:
self.config.write_postgresql_conf()

self.set_bin_dir(self._new_bin_dir)

logger.info('Executing pg_upgrade%s', (' --check' if check else ''))
if subprocess.call([self.pgcommand('pg_upgrade')] + pg_upgrade_args) == 0:
if check:
self.set_bin_dir(self._old_bin_dir)
os.chdir(old_cwd)
shutil.rmtree(upgrade_dir)
return True
Expand All @@ -212,7 +220,9 @@ def prepare_new_pgdata(self, version):
old_version_file = self._version_file
self._version_file = os.path.join(self._data_dir, 'PG_VERSION')

self.set_bin_dir(version)
self._old_bin_dir = self._bin_dir
self.set_bin_dir_for_version(version)
self._new_bin_dir = self._bin_dir

# shared_preload_libraries for the old cluster, cleaned from incompatible/missing libs
old_shared_preload_libraries = self.config.get('parameters').get('shared_preload_libraries')
Expand Down Expand Up @@ -245,6 +255,7 @@ def prepare_new_pgdata(self, version):
self._new_data_dir, self._data_dir = self._data_dir, self._new_data_dir
self.config._postgresql_conf = old_postgresql_conf
self._version_file = old_version_file
self.set_bin_dir(self._old_bin_dir)

if old_shared_preload_libraries:
self.config.get('parameters')['shared_preload_libraries'] = old_shared_preload_libraries
Expand Down
2 changes: 2 additions & 0 deletions postgres-appliance/tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ services:
loop_wait: 2
postgresql:
parameters:
wal_decode_buffer_size: '521kB'
wal_keep_segments: 8
jit: 'off'
postgresql:
parameters:
shared_buffers: 32MB
Expand Down

0 comments on commit 9091d78

Please sign in to comment.