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.
For a consistent, long-running database dump with minimal impact on the active database, I recommend using PostgreSQL's logical replication and point-in-time recovery (PITR) approach. Here's a detailed strategy:
Preparation on Replica Database:
bash
# Ensure replica is in hot standby mode
# In postgresql.conf
hot_standby = on
max_standby_streaming_delay = 30s
Create a Consistent Snapshot:
bash
# Use pg_export_snapshot to get a consistent snapshot
psql -c "SELECT pg_export_snapshot();"
# This will return a snapshot identifier like '000003A1-1'
Dump Database Using pg_dump with Consistent Snapshot:
Capture WAL Logs:
During the dump, ensure you're capturing Write-Ahead Logs (WAL) to replay changes after import:
bash
# Set up continuous WAL archiving
# In postgresql.conf
archive_mode = on
archive_command = 'cp %p /path/to/archive/%f'
Import to New Cluster:
bash
# Restore the base dump
pg_restore -h new_cluster_host -U postgres \
-d new_database full_database_dump.pgdump
# Apply WAL logs since snapshot
pg_basebackup -D /path/to/new/data \
--checkpoint=fast \
--wal-method=stream
Replay Changes:
After importing, you can replay the WAL logs to catch up to the exact point of consistency:
bash
pg_receivewal -D /path/to/wal/archive
Additional Considerations:
Ensure sufficient disk space for dump and WAL logs
Monitor disk I/O during long-running dump
Test the entire process in a staging environment first
Verify PostgreSQL version compatibility between source and target clusters
Recommendations:
Use pg_dump with --format=custom for flexibility
Compress dump to reduce storage requirements
Use replica to minimize impact on primary database
Capture WAL logs for point-in-time recovery
Potential Challenges:
Very large databases (24h dump time)
GIS raster data might require special handling
Network bandwidth for transferring large dumps
Alternative for Extremely Large Databases:
Consider using pg_basebackup with streaming replication for full database copy, which might be faster for very large databases with significant raster data.
Verification Steps:
Validate dump integrity
Check table consistency
Verify no data loss during transfer
Test database functionality after import
Would you like me to elaborate on any of these steps or discuss specific considerations for your GIS raster data?