Upgrading Postgres from v15 to v16: A Step-by-Step Guide

Oct 25, 2024

Recently, I had to upgrade a Postgres instance from v15 to v16. The production environment had almost 10 GB of data, and upgrading and migrating this large dataset was not an easy task. Here's a step-by-step guide, so you don't have to feel overwhelmed when dealing with similar upgrades in the future.

Before You Begin

As a PostgreSQL administrator, you're likely familiar with the importance of staying up-to-date with the latest version of your database management system. Here, I'm going to walk you through the process of upgrading from Postgres v15 to v16, covering essential steps to minimize data issues and ensure a smooth transition.

Pre-Migration Checklist

Before starting the upgrade process, ensure you've taken the following precautions:

  • Backup your database: Create a full backup of your Postgres database to an external location, such as Amazon S3 or an on-premises storage solution. This will serve as a safety net in case anything goes wrong during the upgrade process.
  • Assess your dependencies: Verify that your application and dependencies are compatible with Postgres v16. If you're using any custom plugins or extensions, check their compatibility before proceeding.
  • Gather logs: Collect any relevant logs or error messages related to your Postgres v15 installation. These may be useful during the upgrade process.

Upgrade Preparation

To ensure a smooth upgrade, follow these steps:

  • Update your Postgres configuration: Before upgrading, update your Postgres configuration to use the new default behavior for certain parameters, such as shared_buffers and effective_cache_size.
  • Set up a new Postgres instance: Create a new Postgres instance on your target server, using the same configuration and settings as your existing v15 instance.
  • Transfer data to the new instance: Use pg_dump to export your data from the old instance and import it into the new instance.

Avoiding Data Issues

To minimize data issues during the upgrade process, follow these best practices:

  1. Use pg_dump with --no-join: When exporting data from the old instance, use pg_dump with the --no-join option to avoid creating dependencies between tables.
  2. Use pg_restore with --no-join: When importing data into the new instance, use pg_restore with the --no-join option to avoid creating dependencies between tables.
  3. Run pg_check_partition: After importing data, run pg_check_partition to verify that your partitioning scheme is intact.

Verifying Data Correctness

To ensure your data is correct after the upgrade, follow these steps:

  1. Run SELECT * FROM your_table: Verify that your data is correct by running a simple SELECT * query on a representative table.
  2. Use pg_checksum: Run pg_checksum on your database to verify that the checksums match the expected values.
  3. Run pg_stat_user_tables: Use pg_stat_user_tables to verify that your tables are correctly indexed and that the statistics are up-to-date.

Parallel Copy for Large Data

To improve performance during the upgrade process, consider using pg_copy in parallel mode:

  1. Use pg_copy with --parallel: When exporting or importing data, use pg_copy with the --parallel option to take advantage of multiple CPU cores.
  2. Monitor progress: Use pg_stat_user_tables to monitor the progress of the pg_copy operation.

Post-Migration Tasks

After completing the upgrade process, perform the following tasks:

  1. Verify database functionality: Test your database to ensure that all features and functionality are working as expected.
  2. Update dependencies: Update any dependencies or plugins that require changes to work with Postgres v16.
  3. Monitor performance: Monitor your database performance to ensure that it's optimized for the new version.

By following these steps and best practices, you'll be able to upgrade your Postgres instance from v15 to v16 with minimal data issues and ensure a smooth transition to the new version.

Anthony Nguyen