3

When reading at postgreSQL documentation:

WAL records are appended to the WAL logs as each new record is written. The insert position is described by a Log Sequence Number (LSN) that is a byte offset into the logs, increasing monotonically with each new record. LSN values are returned as the datatype pg_lsn. Values can be compared to calculate the volume of WAL data that separates them, so they are used to measure the progress of replication and recovery.

Can we rely on the fact that this sequence number is strictly monotonic? Isn't there a point in time where this offset resetting to a previous position (due to WAL archive, or another type of operations ) ?

Artholl
  • 83
  • 1
  • 13

2 Answers2

5

It is strictly monotonic but LSN points to a physical offset in a certain segment file.

Isn't there a point in time where this offset resetting to a previous position (due to WAL archive, or another type of operations ) ?

There is such a point of time but in another segment file.

Just for reference here is a macro from PG code which extracts segment number from LSN (https://github.com/postgres/postgres/blob/master/src/include/access/xlog_internal.h) :

/*
 * Compute a segment number from an XLogRecPtr.
 ........
 */
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes) \
    logSegNo = (xlrp) / (wal_segsz_bytes)
Ilia Maskov
  • 1,858
  • 16
  • 26
2

From the postgres documentation on wal internals (emphasis mine)

WAL records are appended to the WAL logs as each new record is written. The insert position is described by a Log Sequence Number (LSN) that is a byte offset into the logs, increasing monotonically with each new record. LSN values are returned as the datatype pg_lsn. Values can be compared to calculate the volume of WAL data that separates them, so they are used to measure the progress of replication and recovery.

WAL logs are stored in the directory pg_wal under the data directory, as a set of segment files, normally each 16 MB in size (but the size can be changed by altering the --wal-segsize initdb option). Each segment is divided into pages, normally 8 kB each (this size can be changed via the --with-wal-blocksize configure option). The log record headers are described in access/xlogrecord.h; the record content is dependent on the type of event that is being logged. Segment files are given ever-increasing numbers as names, starting at 000000010000000000000000. The numbers do not wrap, but it will take a very, very long time to exhaust the available stock of numbers.

Malt
  • 28,965
  • 9
  • 65
  • 105