Core Concepts of pg_upgrade
pg_upgrade enables fast PostgreSQL upgrades by:
- Directly copying data files from old to new instances (avoids re-export/import).
- Migrating database definitions via
pg_dump
/pg_restore
.
Key Requirements:
- Runs only between compatible PostgreSQL versions (e.g., v11 → v16).
- Requires both old and new PostgreSQL installations.
- New instances must use
initdb
with matching parameters (e.g.,--wal-segsize
).
Critical Usage Notes
1. Upgrade Modes
- **
--check
**: Validates compatibility without modifying data (useful for pre-flight checks). - **
--link
/--clone
**: Shares data files between instances (faster but riskier).
2. Operational Constraints
- Locking: Upgrades block access to both old and new instances.
- Port Conflict: Defaults to port
50432
for temporary instances. - Manual Post-Upgrade Steps:
- Sync
postgresql.conf
/pg_hba.conf
. - Run
ANALYZE
(viaanalyze_new_cluster.sh
). - Upgrade extensions (via
update_extensions.sql
).
- Sync
3. Limitations
- No Automatic Syncs: Filesystem paths, plugins, or full-text indexes must be manually migrated.
- High Availability: Requires per-node execution in clustered environments.
Detailed Workflow (Version 13.8+)
Phase 1: Pre-Upgrade Checks
- Compatibility Validation:
- Verifies binary compatibility using
pg_controldata
. - Checks for deprecated features (e.g., system types like
regclass
).
- Verifies binary compatibility using
- Old Instance Inspection:
- Dumps global objects (users, tablespaces) with
pg_dumpall --globals-only
. - Extracts schema-only dumps for user databases (excluding
template0
).
- Dumps global objects (users, tablespaces) with
Phase 2: New Instance Setup
- Initialization:
- Creates a new data directory with
initdb
. - Ensures compatibility with old instance settings (e.g., locale, encoding).
- Creates a new data directory with
- Data Import:
- Restores global objects and user schemas using
psql
/pg_restore
. - Prioritizes
template1
to avoid dependency issues.
- Restores global objects and user schemas using
Phase 3: Data Migration
- File Copy Strategies:
- Hard Links (
--link
): Shares data files between instances (fastest). - Clone (
--clone
): Uses OS-level references (requires filesystem support).
- Hard Links (
- Finalization:
- Resets transaction IDs with
pg_resetwal
. - Generates cleanup scripts (
delete_old_cluster.sh
).
- Resets transaction IDs with
Troubleshooting & Logs
Key log files for debugging:
pg_upgrade_internal.log
: Tool execution details.pg_upgrade_server.log
: Server logs during startup/shutdown.pg_upgrade_dump_*.log
: Schema dump errors.
Best Practices
- Test Thoroughly: Use
--check
in a staging environment. - Parallelize: Leverage
--jobs
for faster exports/imports. - Backup First: Always retain backups before upgrading.
- Monitor Disk Space: Data duplication can double storage needs.
For distributed systems, follow this rule: Upgrade all nodes sequentially to maintain cluster integrity.
Need help optimizing your upgrade strategy? Share your use case! 🚀