简体   繁体   English

如何使用 PostgreSQL 的“pg_restore”和零停机恢复数据?

[英]How to restore data with PostgreSQL's “pg_restore” and zero downtime?

I try to restore a large table我尝试恢复一个大表

pg_restore.exe -U postgres -d db_name --clean --if-exists --single-transaction F:\Backups\PostgreSQL\data.dump.gz

So I have a read lock for a few minutes.所以我有几分钟的读锁。 How to restore data with zero downtime for reading?如何以零停机时间恢复数据以进行读取? I need only reading.我只需要阅读。

You would need to not do the --clean and instead do --data-only, but then do a DELETE from tablename inside the same transaction, before the COPY.您不需要执行 --clean 而是执行 --data-only,然后在 COPY 之前从同一事务中DELETE from tablename I don't think there is a way to make pg_restore do this for you, but you could dump the output of pg_restore to a file and edit it, or use something like sed or perl to inject the DELETE.我认为没有办法让 pg_restore 为您执行此操作,但您可以将 pg_restore 的 output 转储到文件中并进行编辑,或者使用 sed 或 ZF83A0AA1F5CA0F7DD59E4 之类的东西注入。

This should work for table names which don't need to be quoted, and assuming none of the data being copied has first column which starts with 'COPY ':这应该适用于不需要引用的表名,并且假设正在复制的数据都没有以“COPY”开头的第一列:

pg_restore --data-only --single-transaction dmp.dmp -f -| perl -pe 's/^COPY ([\w.]+)/delete from $1; copy $1/' | psql -U postgres -d db_name

However, your schema changing method doesn't seem so dirty to me.但是,您的架构更改方法对我来说似乎并不那么肮脏。 It still requires a momentary access exclusive lock, so it isn't really zero downtime, but it might be unnoticable downtime if it can acquire said lock quickly enough.它仍然需要一个瞬时访问排他锁,所以它并不是真正的零停机时间,但如果它能够足够快地获得所述锁,它可能是不明显的停机时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM