Search code examples
postgresqlreplicationlogical-replicationpglogical

Snapshots in Logical Decoding


I understood that snapshot contains current state of database regarding current running,committed and uncommitted transactions.

I have few questions

1.How snapshots are used in logical decoding?

2.I found that there are different states in SnapBuid structure like SNAPBUILD_CONSISTENT,SNAPBUILD_FULL_SNAPSHOT,SNAPBUILD_BUILDING_SNAPSHOT,SNAPBUILD_START what are this value?why they are maintained and how they are used?

3.I also found that Walsender process will store the snapshots in pg_logical/snapshots/ directory,what is the need of doing this?

4.How this snapshots are updated in case of catalog modifying transactions?


Solution

  • After going through source code i found answers for above questions.

    1.While decoding the wal data, walsender process will maintain a consistent decoding snapshot in decoding context, which helps to collect changes that are need to be replayed.If snapshot is not consistent he will not collect changes but he will wait till he reaches consistent state.

    2.when Logical replication starts walsender process will read the wal contents and updates the state of snapshot using SnapBuild structure.It has four different states starting from SNAPBUILD_START to SNAPBUILD_CONSISTENT. Brief info is present in below link:- https://github.com/postgres/postgres/blob/011d60c4352c5c48c0f1a185e8a12833c22a58db/src/backend/replication/logical/snapbuild.c#L63

    3.In case of restarts or any new slot creation,snapshots stored in pg_logical/snapshots/ will help in quick transition to CONSISTENT state.

    4.This snapshots will maintain a list called xip which is collection of xid's of catalog modifying transactions,therefore when catalog modifying transaction is committed then its transaction id is added in this list and also the xmax value of snapshot gets updated.

    Overall we can conclude that when a slot is created he will create an in-memory catalog decoding snapshot,this snapshot tracks all catalog moifying transactions and based on this snapshot,the value of catalog_xmin of slot gets updated indirectly and also this snapshot protects the required catalogs of transaction through replication slot mechanism.