Retrieve the snapshot's LSN

From: Florent Guiliani <florent(at)guiliani(dot)fr>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Retrieve the snapshot's LSN
Date: 2015-07-15 16:51:44
Message-ID: CAMN0T-vzzNy6TV1Jvh4xzNQdAvCLBQK_kh6_U7kAXgGU3ZFg-Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2015-07-15 16:58:21 Re: [PATCH] Generalized JSON output functions
Previous Message Andrew Gierth 2015-07-15 16:51:03 Re: Grouping Sets: Fix unrecognized node type bug