PostgreSQL 18: What's New and How to Upgrade
PostgreSQL 18 adds better query parallelism, parallel logical replication apply, and more flexible partitioning, plus the pg_upgrade and dump/restore paths to get there.
· 3 min read
New Features in PostgreSQL 18
PostgreSQL 18 focuses on performance, replication, and partitioning rather than headline new syntax.
- Query parallelism: more complex queries can now execute across multiple CPU cores, which helps analytical workloads and large data sets in particular.
- Logical replication: changes can now be applied in parallel on the subscriber side, cutting replication lag in high-transaction environments.
- Declarative partitioning: more flexible management of partitioned tables, with easier maintenance and better performance.
- Security and authentication: more granular control over user roles and permissions, plus improved authentication method support.
- New SQL features: additional SQL-standard features for data manipulation and query writing, closing gaps with the latest standard.
Step-by-Step Upgrade Guide
Upgrading a major PostgreSQL version involves more than a simple apt-get upgrade. It requires migrating data from the old database cluster to a new one, as the internal data storage format can change between major releases. This guide outlines the two primary methods for upgrading: pg_upgrade for in-place upgrades and pg_dump/pg_restore for logical backups.
Method 1: Using pg_upgrade
pg_upgrade is the recommended method for most users because it’s significantly faster and results in minimal downtime. It works by creating hard links or copying files from the old cluster to the new one, avoiding a full data dump and reload.
- Install the new PostgreSQL version (18): Install the new binaries alongside your existing PostgreSQL 17 installation.
- Initialize the new cluster: Create a new, empty PostgreSQL 18 data directory using the
initdbcommand. - Perform a dry run: Run
pg_upgradewith the--checkflag to ensure compatibility between your old and new clusters. This is where incompatible extensions or data type mismatches surface, before the actual upgrade. - Shut down both clusters: Stop both the old PostgreSQL 17 and the new PostgreSQL 18 services.
- Run
pg_upgrade: Execute thepg_upgradecommand to perform the actual upgrade. You must specify the paths to the old and new binary directories and data directories. - Analyze the new cluster: After the upgrade,
pg_upgradewill generate a script to runvacuumdb --all --analyze-in-stages. This step is important becausepg_upgradedoes not transfer optimizer statistics, and running this script will rebuild them for optimal performance on the new cluster. - Clean up: Once you’ve confirmed that the new cluster is working correctly, you can run the provided script to delete the old cluster’s data files.
Method 2: Using pg_dump and pg_restore
This method involves a full logical backup and restore. It is a good option if you need to migrate to a different server or operating system, or if you want to eliminate database bloat during the upgrade. However, it can be much slower and result in more downtime than pg_upgrade.
- Backup global objects: Use
pg_dumpall -gto back up global objects like users, roles, and tablespaces. - Back up individual databases: Use
pg_dumpto create a logical backup of each database you want to migrate. For best results, use thepg_dumpbinary from the new PostgreSQL 18 installation to perform the backup from the old cluster. - Install PostgreSQL 18: Install the new binaries on your target server.
- Restore the global objects: Use
psqlto restore the global objects dump file to the new cluster. - Restore individual databases: Use
pg_restoreto restore the individual database dumps to the new cluster. You can also pipe thepg_dumpoutput directly topsqlto avoid creating intermediate files.
Important Considerations for Both Methods:
- Backup: Always perform a full backup of your entire PostgreSQL data directory before attempting any major version upgrade.
- Test Environment: Always test the upgrade process on a non-production system first to ensure a smooth transition and verify that your applications work with the new version.
- Downtime: Plan for downtime, as the database will be unavailable during the upgrade process. Use the
--checkflag onpg_upgradeto get a rough estimate of the time required.