Offline Upgrade of PostgreSQL and Patroni

How to Upgrade PostgreSQL, PostGIS and Patroni in Offline Environment

In this blog post, I will show how a complete PostgreSQL environment major update can be done. Minor upgrades are not a tough task; they are no different from a standard Linux package upgrade. As my production environment serves in an airgapped-environment, I will complete all the major upgrade with no internet connection. I’ve downloaded all the required whl and .rpm packages before upgrade.

Before starting, A short description of major and minor Postgresql upgrades are provided below:

Major Upgrade: Upgrading PostgreSQL to a new major version, such as moving from 10 to 11 or 12 to 13. Major upgrades bring significant changes, introduce new features, and may require thorough testing and planning due to potential incompatibilities. Minor Upgrade: Updating PostgreSQL to a newer minor version within the same major release, like going from 13.1 to 13.2 or 12.5 to 12.6. Minor upgrades focus on bug fixes, security patches, and performance enhancements, typically requiring less planning and downtime than major upgrades.

I will not only perform a PostgreSQL software update, but also update the following extensions: Patroni, pgbackrest, PostGIS, pg_cron, and Oracle_FDW.

My current environment is:

  • 2 node patroni cluster – current release – 3.0.1
  • Postgresql software – current release – 14.2
  • Postgis – current release – 3.2.7
  • Pgbackrest – current release – 2.44
  • Oracle_fdw – current release – 2.5.0
  • Pg_cron – current release – 1.4.2

I will upgrade to :

  • Patroni – target release – 3.3.0
  • Postgresql software – target release – 16.2
  • Postgis – target release – 3.4.2
  • Pgbackrest – target release – 2.51
  • Oracle_fdw – target release – 2.6.0
  • Pg_cron – target release – 1.6.2

First i will upgrade current patroni release on both servers. I have installed patroni with pip3 before and will use pip3 for update.

Here it is.

Upgrading pgbackrest is similar with a standard Linux package upgrade.

[root@posvt01 postgres16]# yum upgrade ./pgbackrest-2.51-1PGDG.rhel8.x86_64.rpm ./libssh2-1.8.0-8.module+el8.0.0+5339+ebcc8ca7.1.x86_64.rpm

Prior to installing the new PostgreSQL release, I’ll update the current PostGIS version to the latest one. PostGIS updates can be a bit complicated. Since two different GDAL libraries cannot be installed simultaneously, I utilized the –allowerasing flag. The provided command reflects the setup in my environment. Next, I’ll proceed to install PostgreSQL 16 release along with PostGIS 3.4.2 for PostgreSQL 16.

[root@posvt01 postgres16]# yum install ./libdeflate-1.9-3.el8.x86_64.rpm ./re2-20190801-1.el8.x86_64.rpm ./libkml-1.3.0-24.el8.x86_64.rpm \
./geos312-3.12.1-1PGDG.rhel8.x86_64.rpm ./libgeotiff17-1.7.1-6PGDG.rhel8.x86_64.rpm ./libgeotiff17-devel-1.7.1-6PGDG.rhel8.x86_64.rpm \
./proj94-9.4.0-1PGDG.rhel8.x86_64.rpm ./libspatialite50-5.1.0-5PGDG.rhel8.x86_64.rpm ./libspatialite50-devel-5.1.0-5PGDG.rhel8.x86_64.rpm \
./uriparser-0.9.7-1.el8.x86_64.rpm ./postgis34_14-3.4.2-3PGDG.rhel8.x86_64.rpm ./gdal38-libs-3.8.5-3PGDG.rhel8.x86_64.rpm \
./libqhull_r-2015.2-5.el8.x86_64.rpm ./libarrow-8.0.1-2.el8.x86_64.rpm ./armadillo-12.6.6-1.el8.x86_64.rpm –allowerasing
Dependencies resolved.
=========================================================================================================================================
Package Architecture Version Repository Size
=========================================================================================================================================
Installing:
gdal38-libs x86_64 3.8.5-3PGDG.rhel8 @commandline 9.3 M
geos312 x86_64 3.12.1-1PGDG.rhel8 @commandline 1.2 M
libarrow x86_64 8.0.1-2.el8 @commandline 4.7 M
libdeflate x86_64 1.9-3.el8 @commandline 54 k
libgeotiff17 x86_64 1.7.1-6PGDG.rhel8 @commandline 126 k
libgeotiff17-devel x86_64 1.7.1-6PGDG.rhel8 @commandline 33 k
libkml x86_64 1.3.0-24.el8 @commandline 386 k
libqhull_r x86_64 2015.2-5.el8 @commandline 170 k
proj94 x86_64 9.4.0-1PGDG.rhel8 @commandline 2.9 M
re2 x86_64 20190801-1.el8 @commandline 192 k
uriparser x86_64 0.9.7-1.el8 @commandline 71 k
Upgrading:
armadillo x86_64 12.6.6-1.el8 @commandline 42 k
libspatialite50 x86_64 5.1.0-5PGDG.rhel8 @commandline 3.3 M
libspatialite50-devel x86_64 5.1.0-5PGDG.rhel8 @commandline 102 k
postgis32_14 x86_64 3.4.2-3PGDG.rhel8 @commandline 4.0 M
Removing dependent packages:
gdal34-libs x86_64 3.4.3-2.rhel8 @@commandline 28 M
Transaction Summary
=========================================================================================================================================
Install 11 Packages
Upgrade 4 Packages
Remove 1 Package
Total size: 27 M
Is this ok [y/N]: y
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : proj94-9.4.0-1PGDG.rhel8.x86_64 1/20
Running scriptlet: proj94-9.4.0-1PGDG.rhel8.x86_64 1/20
Installing : geos312-3.12.1-1PGDG.rhel8.x86_64 2/20
Running scriptlet: geos312-3.12.1-1PGDG.rhel8.x86_64 2/20
Upgrading : libspatialite50-5.1.0-5PGDG.rhel8.x86_64 3/20
Running scriptlet: libspatialite50-5.1.0-5PGDG.rhel8.x86_64 3/20
Installing : libgeotiff17-1.7.1-6PGDG.rhel8.x86_64 4/20
Running scriptlet: libgeotiff17-1.7.1-6PGDG.rhel8.x86_64 4/20
Installing : libgeotiff17-devel-1.7.1-6PGDG.rhel8.x86_64 5/20
Upgrading : libspatialite50-devel-5.1.0-5PGDG.rhel8.x86_64 6/20
Running scriptlet: libspatialite50-devel-5.1.0-5PGDG.rhel8.x86_64 6/20
Upgrading : armadillo-12.6.6-1.el8.x86_64 7/20
Installing : libqhull_r-2015.2-5.el8.x86_64 8/20
Running scriptlet: libqhull_r-2015.2-5.el8.x86_64 8/20
Installing : uriparser-0.9.7-1.el8.x86_64 9/20
Installing : libkml-1.3.0-24.el8.x86_64 10/20
Installing : re2-20190801-1.el8.x86_64 11/20
Installing : libarrow-8.0.1-2.el8.x86_64 12/20
Installing : libdeflate-1.9-3.el8.x86_64 13/20
Installing : gdal38-libs-3.8.5-3PGDG.rhel8.x86_64 14/20
Upgrading : postgis34_14-3.4.2-3PGDG.rhel8.x86_64 15/20
Cleanup : postgis32_14-3.2.7-1.rhel8.x86_64 16/20
Erasing : gdal34-libs-3.4.3-2.rhel8.x86_64 17/20
Running scriptlet: gdal34-libs-3.4.3-2.rhel8.x86_64 17/20
Cleanup : libspatialite50-devel-5.0.1-4.rhel8.x86_64 18/20
Running scriptlet: libspatialite50-devel-5.0.1-4.rhel8.x86_64 18/20
Cleanup : libspatialite50-5.0.1-4.rhel8.x86_64 19/20
Running scriptlet: libspatialite50-5.0.1-4.rhel8.x86_64 19/20
Cleanup : armadillo-10.8.2-1.el8.x86_64 20/20
Running scriptlet: armadillo-10.8.2-1.el8.x86_64 20/20
Verifying : libdeflate-1.9-3.el8.x86_64 1/20
Verifying : re2-20190801-1.el8.x86_64 2/20
Verifying : libkml-1.3.0-24.el8.x86_64 3/20
Verifying : geos312-3.12.1-1PGDG.rhel8.x86_64 4/20
Verifying : libgeotiff17-1.7.1-6PGDG.rhel8.x86_64 5/20
Verifying : libgeotiff17-devel-1.7.1-6PGDG.rhel8.x86_64 6/20
Verifying : proj94-9.4.0-1PGDG.rhel8.x86_64 7/20
Verifying : uriparser-0.9.7-1.el8.x86_64 8/20
Verifying : gdal38-libs-3.8.5-3PGDG.rhel8.x86_64 9/20
Verifying : libqhull_r-2015.2-5.el8.x86_64 10/20
Verifying : libarrow-8.0.1-2.el8.x86_64 11/20
Verifying : libspatialite50-5.1.0-5PGDG.rhel8.x86_64 12/20
Verifying : libspatialite50-5.0.1-4.rhel8.x86_64 13/20
Verifying : libspatialite50-devel-5.1.0-5PGDG.rhel8.x86_64 14/20
Verifying : libspatialite50-devel-5.0.1-4.rhel8.x86_64 15/20
Verifying : postgis32_14-3.4.2-3PGDG.rhel8.x86_64 16/20
Verifying : postgis32_14-3.4.2-1.rhel8.x86_64 17/20
Verifying : armadillo-12.6.6-1.el8.x86_64 18/20
Verifying : armadillo-10.8.2-1.el8.x86_64 19/20
Verifying : gdal34-libs-3.4.3-2.rhel8.x86_64 20/20
Upgraded:
armadillo-12.6.6-1.el8.x86_64 libspatialite50-5.1.0-5PGDG.rhel8.x86_64 libspatialite50-devel-5.1.0-5PGDG.rhel8.x86_64
postgis32_14-3.4.2-3PGDG.rhel8.x86_64
Installed:
gdal38-libs-3.8.5-3PGDG.rhel8.x86_64 geos312-3.12.1-1PGDG.rhel8.x86_64 libarrow-8.0.1-2.el8.x86_64
libdeflate-1.9-3.el8.x86_64 libgeotiff17-1.7.1-6PGDG.rhel8.x86_64 libgeotiff17-devel-1.7.1-6PGDG.rhel8.x86_64
libkml-1.3.0-24.el8.x86_64 libqhull_r-2015.2-5.el8.x86_64 proj94-9.4.0-1PGDG.rhel8.x86_64
re2-20190801-1.el8.x86_64 uriparser-0.9.7-1.el8.x86_64
Removed:
gdal34-libs-3.4.3-2.rhel8.x86_64
postgres=# select postgis.postgis_full_version();
postgis_full_version
—————————————————————————————————————————————–
—————————————————————————————————————————————–
———————————————————————————————-
POSTGIS="3.4.2 c19ce56" [EXTENSION] PGSQL="140" GEOS="3.12.1-CAPI-1.18.1" PROJ="9.4.0 NETWORK_ENABLED=OFF URL_ENDPOINT=https://cdn.proj.
org USER_WRITABLE_DIRECTORY=/var/lib/pgsql/.local/share/proj DATABASE_PATH=/usr/proj94/share/proj/proj.db" LIBXML="2.9.7" LIBJSON="0.13.1
" LIBPROTOBUF="1.3.0" WAGYU="0.5.0 (Internal)" (core procs from "3.2.7 32dc0cc" need upgrade)
postgres=# SELECT postgis.PostGIS_Extensions_Upgrade();
NOTICE: Updating extension postgis 3.4.2
postgis_extensions_upgrade
————————————————————————————
Upgrade to version 3.4.2 completed, run SELECT postgis_full_version(); for details
(1 row)
postgres=# select postgis.postgis_full_version();
postgis_full_version
—————————————————————————————————————————————–
———————————————–
POSTGIS="3.4.2 c19ce56" [EXTENSION] PGSQL="140" GEOS="3.12.1-CAPI-1.18.1" PROJ="9.4.0 NETWORK_ENABLED=OFF URL_ENDPOINT=https://cdn.proj.
org USER_WRITABLE_DIRECTORY=/var/lib/pgsql/.local/share/proj DATABASE_PATH=/usr/proj94/share/proj/proj.db" LIBXML="2.9.7" LIBJSON="0.13.1
" LIBPROTOBUF="1.3.0" WAGYU="0.5.0 (Internal)"
(1 row)

Now i will install the Postgresql 16 release and postgis 3.4.2 for PostgreSQL 16.

[root@posvt01 postgres16]# yum install ./postgresql16-libs-16.2-1PGDG.rhel8.x86_64.rpm ./postgresql16-16.2-1PGDG.rhel8.x86_64.rpm
[root@posvt01 postgres16]# yum install ./postgresql16-server-16.2-1PGDG.rhel8.x86_64.rpm ./postgresql16-contrib-16.2-1PGDG.rhel8.x86_64.rpm
[root@posvt01 ~]# vi .bash_profile
PATH=$PATH:$HOME/bin:/usr/pgsql-16/bin
[root@posvt01 ~]# yum install ./Packages/postgres16/postgis34_16-3.4.2-3PGDG.rhel8.x86_64.rpm

At this point, i will proceed too deploy oracle_fdw and pg_cron extensions for PostgreSQL 16.

[root@posvt01 postgres16]# yum install ./postgresql16-devel-16.2-1PGDG.rhel8.x86_64.rpm ./perl-IPC-Run-0.99-1.el8.noarch.rpm \
./perl-IO-Tty-1.12-11.el8.x86_64.rpm ./perl-Test-Simple-1.302135-1.el8.noarch.rpm ./perl-Time-HiRes-1.9758-2.el8.x86_64.rpm
[root@posvt01 Packages]# unzip oracle_fdw-ORACLE_FDW_2_6_0.zip
[root@posvt01 Packages]# cd oracle_fdw-ORACLE_FDW_2_6_0/
[root@posvt01 oracle_fdw-ORACLE_FDW_2_6_0]# export LD_LIBRARY_PATH=/usr/lib/oracle/19.14/client64
[root@posvt01 oracle_fdw-ORACLE_FDW_2_6_0]# export ORACLE_HOME=/usr/lib/oracle/19.14/client64
[root@posvt01 oracle_fdw-ORACLE_FDW_2_6_0]# make
[root@posvt01 oracle_fdw-ORACLE_FDW_2_6_0]# make install
/usr/bin/mkdir -p '/usr/pgsql-16/lib'
/usr/bin/mkdir -p '/usr/pgsql-16/share/extension'
/usr/bin/mkdir -p '/usr/pgsql-16/share/extension'
/usr/bin/mkdir -p '/usr/pgsql-16/doc/extension'
/usr/bin/install -c -m 755 oracle_fdw.so '/usr/pgsql-16/lib/oracle_fdw.so'
/usr/bin/install -c -m 644 .//oracle_fdw.control '/usr/pgsql-16/share/extension/'
/usr/bin/install -c -m 644 .//oracle_fdw–1.2.sql .//oracle_fdw–1.0–1.1.sql .//oracle_fdw–1.1–1.2.sql '/usr/pgsql-16/share/extension/'
/usr/bin/install -c -m 644 .//README.oracle_fdw '/usr/pgsql-16/doc/extension/'
[root@posvt01 pg_cron]# unzip pg_cron-main.zip
[root@posvt01 pg_cron]# cd pg_cron-main/
[root@posvt01 pg_cron-main]# make
[root@posvt01 pg_cron-main]# make install
/usr/bin/mkdir -p '/usr/pgsql-16/lib'
/usr/bin/mkdir -p '/usr/pgsql-16/share/extension'
/usr/bin/mkdir -p '/usr/pgsql-16/share/extension'
/usr/bin/install -c -m 755 pg_cron.so '/usr/pgsql-16/lib/pg_cron.so'
/usr/bin/install -c -m 644 .//pg_cron.control '/usr/pgsql-16/share/extension/'
/usr/bin/install -c -m 644 .//pg_cron–1.4-1–1.5.sql .//pg_cron–1.3–1.4.sql .//pg_cron–1.5–1.6.sql .//pg_cron–1.2–1.3.sql .//pg_cron–1.0–1.1.sql .//pg_cron–1.4–1.4-1.sql .//pg_cron–1.1–1.2.sql pg_cron–1.0.sql '/usr/pgsql-16/share/extension/'
/usr/bin/mkdir -p '/usr/pgsql-16/lib/bitcode/pg_cron'
/usr/bin/mkdir -p '/usr/pgsql-16/lib/bitcode'/pg_cron/src/
/usr/bin/install -c -m 644 src/pg_cron.bc '/usr/pgsql-16/lib/bitcode'/pg_cron/src/
/usr/bin/install -c -m 644 src/job_metadata.bc '/usr/pgsql-16/lib/bitcode'/pg_cron/src/
/usr/bin/install -c -m 644 src/misc.bc '/usr/pgsql-16/lib/bitcode'/pg_cron/src/
/usr/bin/install -c -m 644 src/task_states.bc '/usr/pgsql-16/lib/bitcode'/pg_cron/src/
/usr/bin/install -c -m 644 src/entry.bc '/usr/pgsql-16/lib/bitcode'/pg_cron/src/
cd '/usr/pgsql-16/lib/bitcode' && /usr/bin/llvm-lto -thinlto -thinlto-action=thinlink -o pg_cron.index.bc pg_cron/src/pg_cron.bc pg_cron/src/job_metadata.bc pg_cron/src/misc.bc pg_cron/src/task_states.bc pg_cron/src/entry.bc

Up to this point, we’ve updated Patroni, pgbackrest, and PostGIS to their latest versions and installed PostgreSQL 16 along with the PostGIS 3.4.2 packages. Following the installation of PostgreSQL 16, I’ve compiled the pg_cron and Oracle_FDW extensions for the newer PostgreSQL version. 

At this stage, it’s essential to pay extra attention to a few key points.

We’ll begin by stopping Patroni and the PostgreSQL database. From this point onward, downtime commences. Since our database is relatively small in size, we’ve opted to utilize the pg_upgrade method for the upgrade process. In cases where the database size exceeds 2 TB, the –link or –clone flags may be employed. Alternatively, pg_dump is also a viable option. For situations where no large objects are stored in the database, upgrading with a logical streaming method is feasible. However, it’s important to note that as of PostgreSQL 16, large objects are not replicated due to Logical replication restrictions. Unfortunately, there is no workaround for this limitation other than storing data in normal tables.

We’ll initiate the creation of a new PostgreSQL cluster database using ‘initdb’ from the newly installed PostgreSQL 16 directory, ensuring it conforms to the current locale and WAL segment size settings. Following the setup of the new data directory, it’s crucial to carefully configure the new PostgreSQL database. During the upgrade phase, it’s essential to use a different port for the new version since both databases will be operational simultaneously. Additionally, parameters related to the data directory (such as hba_file and ident_file) need to be verified.

Moreover, it’s important to monitor the available memory resources on your server. You may need to adjust the shared_buffers to a lower value temporarily during the upgrade phase to accommodate the operation of two different PostgreSQL cluster databases simultaneously.

The upgrade process begins with running ‘pg_upgrade’ with the check flag to ensure all necessary checks are passed. Once verified, the ‘pg_upgrade’ process will be executed. Upon completion of the upgrade phase, ‘update_extensions.sql’ will be executed, followed by a vacuum analyze operation to generate fresh statistics for the new cluster.

Once the upgrade phase is successfully completed, we may proceed to remove the current Patroni cluster configuration. Before starting Patroni, it’s essential to update the current pgbackrest configuration to align with the new pgdata directories, and the stanza should also be upgraded accordingly. After ensuring all preceding steps are confirmed to be completed successfully, we can initialize Patroni after verifying the configuration (version specific directory related parameters) in the patroni.yml file on both the master and standby servers. 

[postgres@posvt01 ~]$ patronictl -c /etc/patroni.yml remove pgcluster
+ Cluster: pgcluster (7099446795009447890) —–+
| Member | Host | Role | State | TL | Lag in MB |
+——–+——+——+——-+—-+———–+
+——–+——+——+——-+—-+———–+
Please confirm the cluster name to remove: pgcluster
You are about to remove all information in DCS for pgcluster, please type: "Yes I am aware": Yes I am aware
#– Environment variables provided below are updated
[postgres@posvt01 ~]$ vi /var/lib/pgsql/.pgsql_profile
export PGDATA=/mnt/postgres/pgdata/16
export PGHOME=/usr/pgsql-16/
#– Environment variables provided below are updated
[postgres@posvt02 ~]$ vi /var/lib/pgsql/.pgsql_profile
export PGDATA=/mnt/postgres/pgdata/16
export PGHOME=/usr/pgsql-16/
#– parameters provided below are updated
[root@posvt01 ~]# vi /etc/patroni.yml
initdb:
– waldir: '/mnt/postgres/pg_wal/16'
postgresql:
data_dir: /mnt/postgres/pgdata/16
bin_dir: /usr/pgsql-16/bin
basebackup:
waldir: /mnt/postgres/pg_wal/16
#– parameters provided below are updated
[postgres@posvt01 ~]$ vi /etc/pgbackrest/pgbackrest.conf
pg1-path=/mnt/postgres/pgdata/16
#– parameters provided below are updated
[postgres@posvt02 ~]$ vi /etc/pgbackrest/pgbackrest.conf
pg1-path=/mnt/postgres/pgdata/16
#– parameters provided below are updated
[pgbackrest@etcd03 ~]# vi /etc/pgbackrest/pgbackrest.conf
pg1-path=/mnt/postgres/pgdata/16
pg1-port=3531
pg1-socket-path=/var/run/postgresql
pg2-host=posvt02.localdomain
pg2-path=/mnt/postgres/pgdata/16
[pgbackrest@etcd03 ~]$ pgbackrest –stanza=pgcluster –no-online stanza-upgrade
2024-04-18 21:53:00.857 P00 INFO: stanza-upgrade command begin 2.51: –exec-id=15361-8ec0423f –log-level-console=detail –log-level-file=off –no-online –pg1-host=posvt01.localdomain –pg2-host=posvt02.localdomain –pg1-host-ca-file=/etc/ssl/pgbackrest/root.crt –pg2-host-ca-file=/etc/ssl/pgbackrest/root.crt –pg1-host-cert-file=/etc/ssl/pgbackrest/serverbckp.crt –pg2-host-cert-file=/etc/ssl/pgbackrest/serverbckp.crt –pg1-host-key-file=/etc/ssl/pgbackrest/serverbckp.key –pg2-host-key-file=/etc/ssl/pgbackrest/serverbckp.key –pg1-host-type=tls –pg2-host-type=tls –pg1-host-user=postgres –pg2-host-user=postgres –pg1-path=/mnt/postgres/pgdata/16 –pg2-path=/mnt/postgres/pgdata/16 –pg1-port=3535 –pg2-port=3535 –pg1-socket-path=/var/run/postgresql –pg2-socket-path=/var/run/postgresql –repo1-path=/pgbackrest –stanza=pgcluster
2024-04-18 21:53:00.872 P00 INFO: stanza-upgrade for stanza 'pgcluster' on repo1
2024-04-18 21:53:00.944 P00 DETAIL: statistics: {"socket.client":{"total":1},"socket.session":{"total":1},"tls.client":{"total":1},"tls.session":{"total":1}}
2024-04-18 21:53:00.944 P00 INFO: stanza-upgrade command end: completed successfully (88ms)
[root@posvt01 ~]# systemctl start patroni
[pgbackrest@etcd03 ~]$ pgbackrest –type=full –stanza=pgcluster backup
[root@posvt02 ~]# systemctl start patroni

I’ve provided a sample of a comprehensive PostgreSQL environment update. Choosing to upgrade your major version annually offers a dependable solution. Approximately every quarter, a new minor version becomes available. It’s recommended to apply these updates without delay, as they primarily focus on critical bug fixes and are notably easier to install compared to the major release upgrades.

Hope it helps.


Discover More from Osman DİNÇ


Comments

Leave your comment