Retrieve the snapshot's LSN - Mailing list pgsql-hackers

From Florent Guiliani
Subject Retrieve the snapshot's LSN
Date
Msg-id CAMN0T-vzzNy6TV1Jvh4xzNQdAvCLBQK_kh6_U7kAXgGU3ZFg-Q@mail.gmail.com
Whole thread Raw
Responses Re: Retrieve the snapshot's LSN  (Robert Haas <robertmhaas@gmail.com>)
List pgsql-hackers
Hello everyone,

I would need to start a read repeatable transaction and retrieve the
corresponding LSN. I'm looking for pointers or Ideas on how to achieve
this.

Andres F. suggested me to extend pg_export_snapshot() [1] and call
GetLatestSnapshot() [2] while reliably retrieving the current LSN.
Should I call
GetXLogWriteRecPtr() [3] for that ? What lock(s) could I take to
synchronize the two calls?

Any other Idea ?

A snapshot is exported when creating a logical replication slot [4]
and the corresponding LSN is also returned [5]. This is what I need
except that I'd rather prefer to not create a replication slot each
time I need the snapshot.

During slot creation, the snapshot building and exporting code seems
highly coupled with the logical decoding stuff. It doesn't seems much
reusable to retrieve the snapshot's LSN outside of logical decoding.

Thank you for your help,

References:

[1] pg_export_snapshot()

https://github.com/postgres/postgres/blob/aa9eac45ea868e6ddabc4eb076d18be10ce84c6a/src/backend/utils/time/snapmgr.c#L1111

[2] GetLatestSnapshot()

https://github.com/postgres/postgres/blob/aa9eac45ea868e6ddabc4eb076d18be10ce84c6a/src/backend/utils/time/snapmgr.c#L259

[3] GetXLogWriteRecPtr()

https://github.com/postgres/postgres/blob/7b156c1e0746a46d083d7dbcd28afb303b3484ef/src/backend/access/transam/xlog.c#L10616

[4] Exported snapshot in logical replication slot creation

https://github.com/postgres/postgres/blob/aa9eac45ea868e6ddabc4eb076d18be10ce84c6a/src/backend/replication/walsender.c#L815
   /* build initial snapshot, might take a while */   DecodingContextFindStartpoint(ctx);
   /*    * Export a plain (not of the snapbuild.c type) snapshot to the user    * that can be imported into another
session.   */    snapshot_name = SnapBuildExportSnapshot(ctx->snapshot_builder);
 


[5] Consistent point LSN in logical replication slot creation:

https://github.com/postgres/postgres/blob/aa9eac45ea868e6ddabc4eb076d18be10ce84c6a/src/backend/replication/walsender.c#L831

snprintf(xpos, sizeof(xpos), "%X/%X",                                           (uint32)
(MyReplicationSlot->data.confirmed_flush >> 32),                                           (uint32)
MyReplicationSlot->data.confirmed_flush);

...cut...

/* second field: LSN at which we became consistent */
pq_sendstring(&buf, "consistent_point"); /* col name */

...cut....

/* consistent wal location */
pq_sendint(&buf, strlen(xpos), 4); /* col2 len */
pq_sendbytes(&buf, xpos, strlen(xpos));

--
Florent



pgsql-hackers by date:

Previous
From: Andrew Gierth
Date:
Subject: Re: Grouping Sets: Fix unrecognized node type bug
Next
From: Andrew Dunstan
Date:
Subject: Re: [PATCH] Generalized JSON output functions