How to use exported snapshot in Postgres replication slot

1.1k views Asked by At

I'm using the EXPORT_SNAPSHOT option documented here when creating a postgres logical replication slot. The documentation states that 'export, which is the default, will export the snapshot for use in other sessions. This option can't be used inside a transaction...'

However, when I try to use the snapshot exported here from another transaction with the SET TRANSACTION SNAPSHOT command I keep getting the invalid snapshot identifier error.

Isn't it contradictory that there's an export snapshot option which explicitly states that it shouldn't be used inside a transaction(thus we cannot control when it commits) but we cannot use the exported snapshot in another transaction(precisely because we cannot control when the create_replication_slot commits and that snapshot is probably already purged by the time we try to use it)?

ps: What I want to achieve is to use the exported snapshot to copy table data prior to creating a replication slot before actually starting the logical replication.

1

There are 1 answers

0
Gene Z On BEST ANSWER

So here is what I had made working, just FYI:

  • when you create a replication slot, it will export a snapshot by default. That create_replication_slot command cannot be inside a transaction. The exported snapshot has "repeatable read" isolation level, seems no way to change. Hold the connection without executing any other command.
  • once you got the snapshot name (like "00000004-000003B9-1") from the create replication slot result, you begin another transaction on another connection, and the first statement in the transaction must be SET TRANSACTION SNAPSHOT <snapshot_name>, which you got from the above create_replication_slot command.
  • Note that the first connection must not be closed and no other commands can be executed before you start the snapshot consuming transaction on the second connection. Otherwise, the snapshot will be invalid anymore.