Upgrading Postgres major versions
Upgrading a Postgres database's major version to access improved features, performance enhancements, and security updates is a common administration task. Doing the same for an EDB Postgres Distributed (PGD) cluster is essentially the same process but performed as a rolling upgrade.
The rolling upgrade process allows updating individual cluster nodes to a new major Postgres version while maintaining cluster availability and operational continuity. This approach minimizes downtime and ensures data integrity by allowing the rest of the cluster to remain operational as each node is upgraded sequentially.
The following overview of the general instructions and worked example help to provide a smooth and controlled upgrade process.
Note
The following overview and worked example assume you're upgrading to or from 5.7.0+.
For upgrading to older versions of PGD, you need to use the command bdr_pg_upgrade, which has almost the same behavior and requirements as pgd node upgrade.
The only difference is that with bdr_pg_upgrade you can only upgrade major versions of Postgres (community/PGE/EDB Postgres Advanced Server), but with pgd node upgrade you can upgrade major versions of PGD and Postgres at once.
Prepare the upgrade
To prepare for the upgrade, identify the subgroups and nodes you're trying to upgrade and note an initial upgrade order.
To do this, connect to one of the nodes using SSH and run the pgd nodes list command:
sudo -u postgres pgd nodes list
The pgd nodes list command shows you all the nodes in your PGD cluster and the subgroup to which each node belongs.
Then you want to find out which node is the write leader in each subgroup:
sudo -u postgres pgd group <group_name> show --summary
This command shows you information about the pgd group tokened by your <group_name> running in your cluster, including which node is the write leader.
To maintain operational continuity, you need to switch write leaders over to another node in their subgroup before you can upgrade them.
To keep the number of planned switchovers to a minimum, when upgrading a subgroup of nodes, upgrade the writer leaders last.
Even though you verified which node is the current write leader for planning purposes, the write leader of a subgroup could change to another node at any moment for operational reasons before you upgrade that node. Therefore, you still need to verify that a node isn't the write leader just before upgrading that node.
You now have enough information to determine your upgrade order, one subgroup at a time, aiming to upgrade the identified write leader node last in each subgroup.
Perform the upgrade on each node
Note
To help prevent data loss, before starting the upgrade process, ensure that your databases and configuration files are backed up.
Using the preliminary order, perform the following steps on each node while connected via SSH:
Confirm the current Postgres version
View versions from PGD:
sudo -u postgres pgd nodes list --versions.Ensure that the expected major version is running.
Verify that the target node isn't the write leader
Check whether the target node is the write leader for the group you're upgrading:
sudo -u postgres pgd group <group_name> show --summaryIf the target node is the current write leader for the group/subgroup you're upgrading, perform a planned switchover to another node:
sudo -u postgres pgd group <group_name> set-leader <new_leader_node_name>
Stop Postgres on the target node
Stop the Postgres service on the current node:
sudo systemctl stop postgresThe target node is no longer actively participating as a node in the cluster.
Install PGD and utilities
Install PGD and its utilities compatible with the Postgres version you're upgrading to:
sudo apt install edb-bdr5-pg<new_postgres_version_number> edb-bdr-utilities
Initialize the new Postgres instance
Create a directory to house the database files for the new version of PostgreSQL:
sudo mkdir -p /opt/postgres/datanewEnsure that the user postgres has ownership permissions to the directory using
chown.Initialize a new PostgreSQL database cluster in the directory you just created. This step involves using the
initdbcommand provided by the newly installed version of PostgreSQL. Include the--data-checksumsflag to ensure the cluster uses data checksums.sudo -u postgres <path_to_postgres_bin>/initdb -D /opt/postgres/datanew --data-checksumsReplace
<path_to_postgres_bin>with the path to the bin directory of the newly installed PostgreSQL version.You may need to run this command as the postgres user or another user with appropriate permissions.
Migrate configuration to the new Postgres version
- Locate the following configuration files in your current PostgreSQL data directory:
postgresql.conf— The main configuration file containing settings related to the database system.postgresql.auto.conf— Contains settings set by PostgreSQL, such as those modified by theALTER SYSTEMcommand.pg_hba.conf— Manages client authentication, specifying which users can connect to which databases from which hosts.- The entire
conf.ddirectory (if present) — Allows for organizing configuration settings into separate files for better manageability.
- Copy these files and the
conf.ddirectory to the new data directory you created for the upgraded version of PostgreSQL.
- Locate the following configuration files in your current PostgreSQL data directory:
Verify the Postgres service is inactive
Before proceeding, it's important to ensure that no PostgreSQL processes are active for both the old and the new data directories. This verification step prevents any data corruption or conflicts during the upgrade process.
Use the
sudo systemctl status postgrescommand to verify that Postgres was stopped. If it isn't stopped, runsystemctl stop postgresand verify again that it was stopped.
Swap PGDATA directories for version upgrade
- Rename
/opt/postgres/datato/opt/postgres/dataoldand/opt/postgres/datanewto/opt/postgres/data.
This step readies your system for the next crucial phase: running pgd node upgrade to finalize the PostgreSQL version transition.
- Rename
Verify upgrade feasibility
The
pgd node upgradetool offers a--checkoption designed to perform a preliminary scan of your current setup, identifying any potential issues that could hinder the upgrade process.You need to run this check from an upgrade directory with ownership given to user postgres, such as
/home/upgrade/, so that the upgrade log files created bypgd node upgradecan be stored. To initiate the safety check, append the--checkoption to yourpgd node upgradecommand.This operation simulates the upgrade process without making any changes, providing insights into any compatibility issues, deprecated features, or configuration adjustments required for a successful upgrade.
Address any warnings or errors indicated by this check to ensure an uneventful transition to the new version.
- Execute the Postgres major version upgrade
- Execute the upgrade process by running the
pgd node <node_name> upgradecommand without the--checkoption. - It's essential to monitor the command output for any errors or warnings that require attention.
- The time the upgrade process take depends on the size of your database and the complexity of your setup.
- Execute the upgrade process by running the
Update the Postgres service configuration
Update the service configuration to reflect the new PostgreSQL version by updating the version number in the
postgres.servicefile:sudo sed -i -e 's/<old_version_number>/<new_version_number>/g' /etc/systemd/system/postgres.serviceRefresh the system's service manager to apply these changes:
sudo systemctl daemon-reload
Restart Postgres
Proceed to restart the PostgreSQL service:
systemctl start postgres
Validate the new Postgres version
Verify that your PostgreSQL instance is now upgraded:
sudo -u postgres pgd nodes list --versions
- Clean up post-upgrade
- Run
vacuumdbwith theANALYZEoption immediately after the upgrade but before introducing a heavy production load. Running this command minimizes the immediate performance impact, preparing the database for more accurate testing. - Remove the old version's data directory,
/opt/postgres/dataold.
- Run
The worked example that follows shows upgrading the Postgres major version from 16 to 17 on a PGD 5 cluster deployed with TPA in detail.
Worked example
This worked example starts with a TPA-managed PGD cluster deployed using the AWS quick start, which create Debian OS nodes. The cluster has three nodes: kaboom, kaolin, and kaftan, all running Postgres 16.
This example starts with the node named kaboom.
Note
Some steps of this process involve running commands as the Postgres owner. We refer to this user as postgres throughout, when appropriate. If you're running EDB Postgres Advanced Server, substitute the postgres user with enterprisedb in all relevant commands.
Confirm the current Postgres version
SSH into kaboom to confirm the major version of Postgres is expected:
sudo -u postgres pgd nodes list --versions
The output will be similar to this for your cluster:
Node Name BDR Version Postgres Version --------- ------------------------------ -------------------------------- kaboom 5.7.0-dev (snapshot 8516bb3ab) 16.6 (Debian 16.6-1EDB.bullseye) kaftan 5.7.0-dev (snapshot 8516bb3ab) 16.6 (Debian 16.6-1EDB.bullseye) kaolin 5.7.0-dev (snapshot 8516bb3ab) 16.6 (Debian 16.6-1EDB.bullseye)
Confirm that the Postgres version is the expected version.
Verify that the target node isn't the write leader
The cluster must be available throughout the process (that is, a rolling upgrade). There must always be an available write leader to maintain continuous cluster availability. So, if the target node is the current write leader, you must perform a planned switchover of the write leader node before upgrading it so that a write leader is always available.
While connected via SSH to kaboom, see which node is the current write leader of the group you're upgrading using the pgd group show --summary command:
sudo -u postgres pgd group dc1_subgroup show --summary
In this case, you can see that kaboom is the current write leader of the sole subgroup dc1_subgroup:
Group Property Value ----------------- ------------ Group Name dc1_subgroup Parent Group Name democluster Group Type data Write Leader kaboom Commit Scope
So you must perform a planned switchover of the write leader of dc1_subgroup to another node in the cluster.
Perform a planned switchover
Change the write leader to kaftan so kaboom's Postgres instance can be stopped:
sudo -u postgres pgd group dc1_subgroup set-leader kaftan
After the switchover is successful, this message appears:
Command executed successfully
Then it's safe to stop Postgres on the target node. Of course, if kaftan is switched back to being the write leader when you come to upgrading it, you'll need to perform another planned switchover at that time.
Stop Postgres on the target node
While connected via SSH to the target node (in this case, kaboom), stop Postgres on the node by running:
sudo systemctl stop postgresThis command halts the server on kaboom. Your cluster continues running using the other two nodes.
Install PGD and utilities
Next, install the new version of Postgres (PG16) and the upgrade tool:
sudo apt install edb-bdr5-pg17 edb-bdr-utilities
Initialize the new Postgres instance
Make a new data directory for the upgraded Postgres, and give the postgres user ownership of the directory: