PostgreSQL upgrade

Are you interested in upgrading your Postgres database?

Written by Zeba

| Mar 23, 2023

7 MIN READ

To achieve your upgrade goals, this article provides you with detailed information

If you need to upgrade your PostgreSQL databases, there are several options available. This article will examine the use of the built-in tool, pg_upgrade, which allows for an in-place software upgrade and can potentially reduce downtime – a critical consideration for many organizations. By utilizing pg_upgrade, you can upgrade Postgres with ease and minimal effort.

The necessity of upgrading our Postgres database

Security is one of the reasons to consider upgrading to newer versions of Postgres as they often include bug fixes and security enhancements that can safeguard your data and system against potential security risks. Upgrading your Postgres database may lead to performance enhancements, resulting in faster and more efficient database operations. Postgres upgrades offer access to new features and capabilities, such as improved querying and indexing options, expanded support for data formats including JSON, and the introduction of new data types.

By upgrading to a newer version of Postgres, you may increase the compatibility of your database with other software and tools that you use.

If an older version of Postgres is no longer supported by the community, it may leave your database vulnerable to security vulnerabilities and bugs. In contrast, upgrading to a newer version ensures continued access to maintenance and support.

Upgrading to the latest version of PostgreSQL can provide a range of benefits, including

  • Enhanced security measures that help protect your system and data from potential security threats.
  • Improved database performance and efficiency, leading to faster query processing and better overall system performance.
  • Access to new features and capabilities, such as improved indexing and querying options, expanded data types, and increased support for JSON and other data formats.
  • Compatibility with other software and tools, ensuring seamless integration and reducing potential compatibility issues.
  • Ongoing support and maintenance, as newer versions of Postgres are typically supported by the community, while older versions may no longer receive updates or bug fixes.

The current version of Postgres presents challenges that one may encounter

  • Despite Postgres‘ reputation for robust security features, the current version may contain vulnerabilities that could be exploited by hackers.
  • As databases become more intricate and expand in scale, the current iteration of Postgres may encounter performance issues that can result in slow query processing and other performance-related challenges.
  • The current version of Postgres may not have all the features and capabilities needed to meet the specific needs of certain applications or use cases.
  • As other software and tools evolve, compatibility issues may arise with the current version of Postgres, requiring additional work and resources to address.
  • As newer versions of Postgres are released, the community support and maintenance for the current version may begin to decline, leaving users with fewer resources and options for resolving issues or troubleshooting problems.
  • Overall, while the current version of Postgres is a robust and reliable database system, users may face various challenges related to security, performance, functionality, compatibility, and support.

Let’s review some of the new features of Postgres 15

Except for the database owner, all users’ CREATE permissions are revoked. It makes permission assignment more customizable.

Sorting data rows is a common process in PostgreSQL programming. It is utilized not just when using the ORDER BY clause, but also for creating indices, splitting tables, and so on. Sorting is also one of the most algorithmically costly procedures. One of the key reasons to upgrade is the increased performance of both in-memory and on-disk sorting in version 15.

Export log data in JSON format facilitates log processing and organized output on the client side.

The MERGE operation was introduced, which allows you to alter target table data based on the specified source using the numerous conditional processing options. This enables data processing without the need for the creation of procedural language.

Parallel execution of queries using several worker processes is another part of PostgreSQL that develops fast. In version 15 SELECT DISTINCT queries that drop duplicate rows from output can improve performance by using parallel workers.

You can find a full changelog and full list of new features in the official changelog.

Let’s check how PG_UPGRADE can help us with all the change in the new version

Major PostgreSQL releases often introduce new features that may alter the layout of system tables, but the internal data storage format usually remains unchanged. pg_upgrade leverages this fact to perform rapid upgrades by creating new system tables and reusing old user data files. pg_upgrade attempts to ensure binary compatibility between the old and new clusters, such as by verifying compatible compile-time settings, including 32/64-bit binaries. However, it is crucial to ensure that any external modules are also binary compatible, although this cannot be verified by pg_upgrade.

Let’s go through the upgrading strategy we used for this post

The data in the current iteration of Postgres is provided below, and we can contrast it with the new version after upgrading with the extensions.

In the new version, we will verify if we have the same number of tables, databases, and extensions.

Postgresql b1

Create extension in new version manually before we start upgrade process

Postgresql b2

Prior to performing any upgrades, make sure a backup of the database is created so that the backup can be used in case something goes wrong.

Postgresql b3

Up to this point, we must have a new version installed, a new cluster initialized, start the postgresql cluster with a different port than the older version, we must make the necessary changes to the new configuration files referring to the older configurations. Once the backup has been taken, we can proceed with the upgrade.

Start compatibility check with the pg_upgrade between two servers in this example server 11 and server 15. In case you get any errors you need to fix these before you proceed to the actual upgrade.

Postgresql b4

Performing the necessary pre-checks

Before upgrading, it’s essential to prepare and perform consistency checks. After installing the new version of PostgreSQL, initialize a new data directory using the new binaries and start it .Next, utilize pg_upgrade to verify consistency between the two servers running – version 11 and version 15.The pg_upgrade tool performs a number of checks to ensure that the old and new versions of PostgreSQL are compatible. This includes checking the format of the system catalogs, checking for incompatible configuration settings, and checking for any missing or deprecated functions. If you encounter any errors during the consistency check, such as a missing extension, they must be fixed before proceeding with the upgrade. Once the consistency check is successful, you can proceed with the upgrade.

Postgresql b5

Here we begin with the actual upgrade

Once the compatibility check has been completed and no issues have been addressed, the actual upgrade process can be started. This involves using the pg_upgradetool to transfer the data from the old PostgreSQL installation to the new one.The upgrade process can take some time, especially for large databases.

Postgresql b6

Postgresql b7

After the upgrade is complete PostgreSQL provides a script that performs a “vacuum analyze” and “update_extensions” operation on all the databases in the cluster.

update_extensions.sql is a SQL script that is used during a PostgreSQL upgrade to update any installed extensions to work with the new version of PostgreSQL.

When upgrading to a new version of PostgreSQL, it’s important to update any installed extensions to ensure they are compatible with the new version. The update_extensions.sql script automates this process by executing the appropriate SQL commands for each extension.

Postgresql b8

Running vacuum analyze script is optional, but it can be a good idea to do so in order to optimize the performance of the database. The vacuum analyze operation helps to reclaim disk space and update statistics about the tables, which can improve query performance.To run the script, simply execute it from the command line. Once it has completed, you can check the size of the databases

Postgresql b9

Postgresql b10

You can now have a basic checks such as number of databases, users, extensions, tables etc in the new version of Postgresql database

Postgresql b11

Postgresql b12

Summary

After a successful upgrade of PostgreSQL it is important to test and verify that the system is functioning properly. This can include running tests on the database, checking for any errors or issues, and ensuring that all data has been migrated correctly.

It is also important to document the upgrade process and any changes made to the system. This documentation will be useful in the future for troubleshooting and for reference.

Once the upgrade is complete, you should also consider ongoing maintenance and support for your upgraded database. Stay up-to-date with future releases and updates to ensure that your system continues to perform optimally.

Overall, a successful upgrade of your Postgres database can bring significant benefits in terms of improved security, performance, and functionality. It is an important step in maintaining the reliability and longevity of your database system.

Ashnik is a trusted provider of open-source technology solutions, including PostgreSQL. Our aim is to provide our clients with reliable, stable, and efficient support that is aligned with the industry’s needs. With our deep understanding of open-source services, support, and solutions, we are well-equipped to offer expert assistance and help you overcome any challenges with your PostgreSQL database. Trust us to help you keep your database running smoothly and maximizing its value.

Get in touch today for a free consultation with our team of experts!


Go to Top