There is a mysterious comment in the source code of debezium, which says that LSNs need not be ordered in the WAL file. How can that be possible? If LSN represents the byte offset in the WAL, how can it not be ordered? While we are at it, can someone explain precisely how the XLOG records are written during concurrent transactions? For example, tx1 commits, and the XLOG records of tx1 are being written to WAL. Now tx2 commits, so will the XLOG records of tx2 be intermixed with the XLOG records of tx1. For example, is the following order possible?

tx1_begin, tx2_begin, tx1_xlog1, tx2_xlog1, tx1_commit, tx2_commit

1 Answer 1


Yes, the comment you quote is mysterious. Your assessment is right — an LSN is just a position in the WAL.

XLOG records from different transactions can be freely intermixed; they are logged when they happen. WAL is not written at commit time, it is written when the data modifying statement happens. The only WAL record written at commit time is a COMMIT record, and WAL is flushed to disk.

During recovery, each WAL entry is replayed right away. The is no real rollback in PostgreSQL: if a transaction is aborted, that information is persisted in the commit log, and the data added by that transaction become invisible.

  • can you elaborate on "they are logged when they happen"? I thought the XLOG records are flushed to disk only at the time of commit. The only way different tx XLOG records get intermixed is if another commiting process also writes to the wal at the same time. This seems a bit weird to be to have two processes writing to the same file.
    – sha
    Sep 16, 2021 at 19:52
  • I tried to elaborate some more. Sep 17, 2021 at 21:40
  • thanks, that clears it up!
    – sha
    Sep 18, 2021 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.