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 ) ?
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)