Sub topic 9
After the upgrade: statistics, extensions and cleanup
Assume you have completed pg_upgrade, started the new cluster, and confirmed that applications can connect. The upgrade changes the server binaries and system catalogs, but it does not finish every task needed for normal query performance and extension compatibility.
Rebuild planner statistics
PostgreSQL uses planner statistics to estimate how many rows a query will return. The query planner uses those estimates to choose indexes, join methods, and scan strategies.
pg_upgrade does not copy these statistics from the old cluster. Run vacuumdb with --analyze after starting the new cluster:
/opt/pg17/bin/vacuumdb \
--all \
--analyze-in-stagesThe --all option processes every database in the cluster. The --analyze-in-stages option gathers statistics in several passes. Early passes make the databases usable quickly, while later passes improve accuracy.
For a small database, you can run a complete analysis instead:
/opt/pg17/bin/vacuumdb \
--dbname=orders \
--analyzeRun analysis as a PostgreSQL superuser or as a user with permission to connect to each target database. Monitor the server while this runs. Large tables can consume substantial I/O and CPU.
Check and update extensions
Extensions have two separate parts:
- SQL objects stored inside a database, such as functions, types, and views.
- Files installed on the server, such as shared libraries and extension control files.
The new PostgreSQL installation must contain compatible versions of every extension used by the old cluster. Check the installed extensions in each database:
SELECT extname, extversion
FROM pg_extension
ORDER BY extname;Also check the extension files installed for the new server. Package names and paths depend on the operating system. An extension can appear in pg_extension but still fail later if its shared library is missing or incompatible.
After installing compatible extension files, update extensions where the extension author provides an upgrade path:
ALTER EXTENSION postgis UPDATE;
ALTER EXTENSION pg_stat_statements UPDATE;You can generate update statements for all installed extensions, then review them before execution:
SELECT format('ALTER EXTENSION %I UPDATE;', extname)
FROM pg_extension
WHERE extname NOT IN ('plpgsql')
ORDER BY extname;Do not blindly run every generated statement. Read the extension release notes, especially when an extension crosses several major versions. Some extensions require intermediate versions, special migration scripts, or a separate command outside SQL.
Validate the new cluster
Run application-level checks rather than relying only on a successful server start. Confirm that:
- All expected databases, roles, schemas, and privileges exist.
- Applications can connect using their normal credentials.
- Critical queries use suitable plans.
- Jobs, foreign data wrappers, and procedural languages work.
- Logs contain no repeated extension, authentication, or permission errors.
- Monitoring and backup tools point to the new data directory and port.
Compare important query plans before and after the upgrade with EXPLAIN (ANALYZE, BUFFERS). A changed plan is not automatically a problem. Confirm that response time and resource use meet your service requirements.
The upgrade may create scripts in the new cluster’s working directory, commonly named analyze_new_cluster.sh and delete_old_cluster.sh. Read these scripts before using them. Their exact contents and availability depend on the PostgreSQL version and platform.
Clean up safely
Keep the old cluster until validation, backup verification, and the rollback deadline have passed. If you used link mode, treat the old data directory as unsafe to start after the new cluster has been started. The two clusters may share data files.
When cleanup is approved:
- Stop and disable services that reference the old cluster.
- Confirm that the new cluster is serving traffic.
- Save the upgrade logs and the final configuration.
- Take or verify a backup of the new cluster.
- Remove the old cluster using your operating system’s documented procedure.
Do not delete the old data directory merely because pg_upgrade reported success. A successful upgrade proves compatibility checks passed. It does not prove that every application query, extension, backup, or operational job works correctly.
Exercise
Why should you delay deleting the old cluster after pg_upgrade succeeds?
Show answer
pg_upgrade checks structural compatibility, but it does not prove that applications, extensions, backups, and operational jobs work correctly. Keeping the old cluster preserves a rollback option until those checks pass.