I would like to make a consistent dump of a postgres database (postges13). By consitent I mean all the tables should be in state of the same time even when dump time will be long like 12h.
I need this dump to be able to migrate the database to postgres 16 to another existing postgres cluster
the problem is the database must be open and will be actively changed during the dump... we would like to have a consistent dump to a point in time and once it is dumped and imported to the new cluster we can re load all changes (manually) since this point in time. The most important is to have consistent dump of the database (as it is big and contains gis raster data we setimate export time for 24h)
we have replica DB and we pland to do dump on replica
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To create a consistent point-in-time dump of a large actively used PostgreSQL 13 database, especially when:
The dump may take several hours (e.g., ~24h),
The dump needs to reflect a consistent state at a single point in time,
The system must remain operational (users can write to the database),
You have a replica available (ideal!),
You’re migrating to PostgreSQL 16, and can afford to manually replay changes made after a certain point in time,
— you should leverage PostgreSQL's Point-in-Time Recovery (PITR)-capable physical backup mechanism, not pg_dump directly, because pg_dump doesn't guarantee true point-in-time consistency for large/dynamic DBs unless you take extreme care (e.g., using snapshots, locks, or export layers like pgbackrest or ZFS + WAL archiving).
✅ Ideal Approach: Use Physical Backup + WAL Archiving (PITR)
A PITR-based backup lets you:
Take a consistent backup of your DB at a specific point in time,
Let writes happen during the backup,
Restore that backup later and replay it to exactly that timestamp on the new cluster,
Afterwards, you can dump the restored target using pg_dump if needed,
Or even delay restore until needed.
Because you have a replica, you can do this entirely offline of the production server.
🔧 Step-by-Step Plan
✅ Step 1: Ensure WAL Archiving is Enabled on Replica
Check your postgresql.conf on the replica:
conf
archive_mode = on
archive_command = 'cp %p /path/to/wal_archive/%f'
archive_command must be functional and tested,
Make sure WALs are backed up continuously and are retained long enough.
⚠️ Important: the WALs are critical for PITR and must be available for the full duration between backup and the point you want to restore.
-Xs: stream WALs along with it (ensures consistency without needing WAL dir manually),
-P: progress,
-R: create a recovery config.
💡 If you use pgbackrest, it's even more efficient, supports compression, incremental backups, and simplifies recovery — useful for production systems.
You can also take a filesystem-level snapshot of the data directory (e.g., with LVM, ZFS) if you're using a replica and stopping it, but pg_basebackup is safer and easier.
✅ Step 4: Restore on PostgreSQL 16 (Target)
To migrate to PostgreSQL 16, you'll need:
A clean PG16 instance ready to accept base backup,
Another data directory,
WALs accessible to allow replay up to your restore point.
Place your backed-up data directory in the expected path for the PG16 instance (or initdb a new cluster and move data over).
Since you want to restore to a specific point (e.g., the pg_create_restore_point('migration_start')), configure the recovery:
📝 postgresql.conf
conf
restore_command = 'cp /wal_archive/%f %p'
📝 recovery.conf or postgresql.auto.conf (PG16+)
Since PG12, the recovery settings are read from postgresql.conf, and standby.signal/recovery.signal is used to control recovery mode
In postgresql.conf:
conf
restore_command = 'cp /wal_archive/%f %p'
recovery_target_name = 'migration_start'
recovery_target_action = 'pause' # Optional: pause when target is reached
Create a recovery.signal file in your data directory to trigger recovery.
Start PostgreSQL 16. It will:
Replay WAL from the base backup,
Stop at the migration_start point,
Result in a consistent database as it existed at exact restore point time,
You now have a stable DB for testing / dumping / comparison etc.
✅ Optional Follow-Up: Dump to Logical Format
If you eventually want to dump this restored PG16 DB for portability/logical diff/etc:
bash
pg_dumpall -U postgres -f db_dump.sql
Or per-db:
bash
pg_dump -Fc -U postgres -d mydb -f mydb.dump # for compressed format
🚀 Bonus: Replaying Later Changes
If you want to re-apply changes since the restore point:
You can use tools like logical replication to extract changes (if set up),
Or create logical replication slots around the restore point and pull modified data,
Or compare ctids or timestamps across systems,
Or re-export diff records manually using audit logs, cdc, or modified tables if you wrote changed-row tracking.
💡 Summary
Step
Action
1
Enable WAL archiving on replica
2
Create a restore point on primary
3
Use pg_basebackup from replica to dump
4
Restore on new PG16 node using the WALs and stop at restore point
5
Validate and use restored cluster
6
Optionally dump from restored PG16 node
7
Manually replay later changes post-restore point
📎 Recommendations
Use pgbackrest or barman if you plan on doing multiple backups or want automation.
Consider converting to logical replication after migration for continued upgrade flexibility.
Even if you don’t reuse replication slots, careful WAL retention setup is critical.
Don’t use pg_dump for large active DB migration — unless it's static or downtime is allowed.
Would you like a sample script with all commands or a docker-compose environment to test this?