从给定的LSN开始进行PostgreSQL逻辑复制。

5

Postgres逻辑复制的初始同步是一个非常缓慢的过程,特别是当原始数据库很大时。

我想知道是否可以从给定的LSN开始复制?

期望的工作流程如下:

  1. 获取源数据库的当前LSN
  2. 在源数据库中创建所需对象的逻辑转储
  3. 在目标数据库上还原导出文件
  4. 从第1步获得的LSN开始启动逻辑复制

我没有找到任何允许第4步的文档,有人知道是否可能吗?


听起来你正在寻找pg_replication_origin_advance https://www.postgresql.org/docs/current/functions-admin.html#PG-REPLICATION-ORIGIN-ADVANCE - jjanes
1个回答

8

文档给出了一个提示:

当使用流复制接口(参见CREATE_REPLICATION_SLOT)创建新的复制槽时,会导出一个快照(参见第9.27.5节),它将显示所有更改都包括在更改流中之后数据库的确切状态。这可以通过使用SET TRANSACTION SNAPSHOT读取创建槽时的数据库状态来创建新的副本。然后可以使用该事务在该时间点上转储数据库状态,之后可以使用槽的内容更新而不会丢失任何更改。

因此,步骤如下:

  • Start a replication connection to the database:

    psql "dbname=yourdatabasename replication=database"
    
  • Create a replication slot and copy the snapshot name from the output. It is important to leave the connection open until the next step, otherwise the snapshot will cease to exist

    CREATE_REPLICATION_SLOT slot_name LOGICAL pgoutput;
    
  • Dump the database at the snapshot with the following command. You can close the replication connection once that has started.

    pg_dump --snapshot=snapshotname [...]
    
  • Restore the dump to the target database.

  • Start replication using the replication slot.


1
@VitaliiZurian 因为否则快照将不复存在。 - Laurenz Albe
1
啊哈,所以快照只要被至少一个会话引用,就一直存在?不像复制槽那样永久创建? - Vitalii Zurian
1
那是正确的。 - Laurenz Albe
1
@DonSeiler 这应该不是问题。快照仍然存在,但它不再是导出的快照。 - Laurenz Albe
1
@DonSeiler 我不认为初始数据复制特别慢。我觉得那只是一个未经验证的说法。 - Laurenz Albe
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接