Re: Commit Sequence Numbers and Visibility
Re: Commit Sequence Numbers and Visibility
От:
Jeff Davis <pgsql@j-davis.com>
Дата:
On Wed, 2026-06-10 at 14:19 +0200, Matthias van de Meent wrote: > Our current snapshot mechanism allows it to be a decision that the > committing transaction makes (+ edge cases related to cancellation > etc.); That's true, but I thought that was considered to be incidental and undesirable behavior. > > > visibility is determined (and logged) > separately from durability, That stretches the definition of "CSN". We'd have to redefine "commit" to mean "when the transaction writes the commit-visibility record". But crash recovery and failover would be using a different definition? > in which case it could be made to work by > requiring transactions with higher durability expectations to log > their visibility, or offloading that visibility-logging job to a > background process. > > It doesn't even have to be very fancy; including the durability > requirement in the commit record, and then logging the 3 commit > durability horizons every once in a short while for as long as there > are commits waiting for durability (and actually become durable) > should be sufficient - such a "durability horizons" record should be > sufficient as CSN (visibility horizon) for the differently-durable > commits that were logged ahead of their respective durability > horizon. > It'd be comparable to the commit_delay system that combines commits' > fsyncs, but with an additional XLog record getting logged. If I understand your proposal: Naively, each committing transaction can write an additional commit- visibility record right before ProcArrayEndTransaction(), but that could be expensive. You are suggesting an optimization to infer the visibility LSN of a given transaction from its commit record (including an extra durability requirement setting) along with some other records in the WAL that are written less often than every commit. Can you describe in more detail what will be logged and how you make the inference? Also, how do you choose the CSN when acquiring a snapshot? I assume just the insert pointer? > > [...] > > SERIALIZABLE: > > > > I didn't fully analyze the implications for SSI yet, but I believe > > it's > > compatible. Snapshots are still snapshots, and SSI can still detect > > conflicts the same way as before. The serializable order can be > > different from CSN order, which could be confusing, but that's an > > existing problem. > > Do you have a reference where I can read up on this issue? https://arxiv.org/pdf/1208.4179 Regards, Jeff Davis
Re: Commit Sequence Numbers and Visibility
От:
Matthias van de Meent <boekewurm+postgres@gmail.com>
Дата:
On Wed, 10 Jun 2026 at 18:40, Jeff Davis wrote: > > On Wed, 2026-06-10 at 14:19 +0200, Matthias van de Meent wrote: > > Our current snapshot mechanism allows it to be a decision that the > > committing transaction makes (+ edge cases related to cancellation > > etc.); > > That's true, but I thought that was considered to be incidental and > undesirable behavior. I think it's desireable that snapshots don't need to take special care around the durability of the transactions that are included in their snapshot. Async transactions may want to see sync transactions' durable data, but probably prefer not to have to wait for that durability just because its session logged its latest commit record after the sync transaction did. > > > > > > visibility is determined (and logged) > > separately from durability, > > That stretches the definition of "CSN". We'd have to redefine "commit" > to mean "when the transaction writes the commit-visibility record". But > crash recovery and failover would be using a different definition? Visibility would presumably happen after the recovery/replica has made sure that the durability of the pending-visible transactions is guaranteed; which presumably could be done with a sync wait at end-of-recovery. > > in which case it could be made to work by > > requiring transactions with higher durability expectations to log > > their visibility, or offloading that visibility-logging job to a > > background process. > > > > It doesn't even have to be very fancy; including the durability > > requirement in the commit record, and then logging the 3 commit > > durability horizons every once in a short while for as long as there > > are commits waiting for durability (and actually become durable) > > should be sufficient - such a "durability horizons" record should be > > sufficient as CSN (visibility horizon) for the differently-durable > > commits that were logged ahead of their respective durability > > horizon. > > It'd be comparable to the commit_delay system that combines commits' > > fsyncs, but with an additional XLog record getting logged. > > If I understand your proposal: > > Naively, each committing transaction can write an additional commit- > visibility record right before ProcArrayEndTransaction(), but that > could be expensive. Correct. Not all that much more expensive, but for unlogged workloads it could double the number of WAL records emitted. > You are suggesting an optimization to infer the visibility LSN of a > given transaction from its commit record (including an extra durability > requirement setting) along with some other records in the WAL that are > written less often than every commit. Can you describe in more detail > what will be logged and how you make the inference? I've thought about these different approaches: 0.) synchronous_commit=off doesn't care about durability and doesn't need to wait to release its locks; and is excluded from consideration in the systems below. The commit record itself would be used to supply the CSN visibility threshold. 1.) Each commit for themselves, logging their own visibility record when they achieve durability. Lowest per-commit durability latency, but higher pressure on WAL insertions. The visibility record's LSN would be the CSN threshold for the visibility of this XID, and doesn't need a separate durability guarantee - recovery has rules to recover, apply, and make visible those yet-to-become-visible transactions. 1a.) A batched approach to 1, where one backend gathers sibling committers (like in commit_delay) with the same durability expectations for a single visibility record that includes the sibling commits' IDs; they all become visible once that record's logged. This reduces WAL pressure, at the cost of slightly increased latency (the oldest xact will have to wait for the newest xact in the batch to also become durable). 3.) A BGWorker approach, where each backend includes its durability requirement (sync/remote_write/remote_apply) in the commit record, and a bgworker logs records every (e.g.) commit_delay that include the (monotonically increasing) LSNs of the various durability horizons, if any commits were waiting for that horizon (sync/rw/ra). This limits WAL overhead to once per commit_delay, at the cost of each commit having an up to commit_delay latency penalty past their durability window; and less fine-grained snapshot boundaries. > Also, how do you choose the CSN when acquiring a snapshot? I assume > just the insert pointer? Yes, that, or the record-end pointer of (e.g.) the most recently inserted visibility record. > > [...] > > > SERIALIZABLE: > > > > > > I didn't fully analyze the implications for SSI yet, but I believe > > > it's > > > compatible. Snapshots are still snapshots, and SSI can still detect > > > conflicts the same way as before. The serializable order can be > > > different from CSN order, which could be confusing, but that's an > > > existing problem. > > > > Do you have a reference where I can read up on this issue? > > https://arxiv.org/pdf/1208.4179 Thanks! Kind regards, Matthias van de Meent Databricks (https://www.databricks.com)
Re: Commit Sequence Numbers and Visibility
От:
Jeff Davis <pgsql@j-davis.com>
Дата:
On Wed, 2026-06-10 at 21:18 +0200, Matthias van de Meent wrote:
> I think it's desireable that snapshots don't need to take special
> care
> around the durability of the transactions that are included in their
> snapshot.
> Async transactions may want to see sync transactions' durable data,
> but probably prefer not to have to wait for that durability just
> because its session logged its latest commit record after the sync
> transaction did.
For the problem sequence with CSNs:
1. sync transaction T1 writes commit record
2. async transaction T2 writes commit record
3. T2 releases locks
4. T3 takes a snapshot
5. T1's commit record is flushed
6. T1 releases locks
there are three resolutions:
a. force T3 to wait until T1's commit record is flushed before
using the snapshot, slowing down the sync part of the
workload; or
b. force T2 to wait until all unflushed sync transactions with
an earlier commit LSN are flushed before releasing locks
(thereby making the above seqeunce impossible), slowing down
the async part of the workload; or
c. let T3 use the snapshot immediately, potentially returning
unflushed data to the client.
Perhaps none of those options is great for everyone, but we could allow
users to select the behavior they want. That seems better than today,
when any async transaction can cause sync transactions to start
returning unflushed data to the client, and there's no way to prevent
that.
> >
> Visibility would presumably happen after the recovery/replica has
> made
> sure that the durability of the pending-visible transactions is
> guaranteed; which presumably could be done with a sync wait at
> end-of-recovery.
That might be fine, but it's different from CSNs as I understand the
meaning (at least in the simplest and most intuitive meaning). Should
we call them "Visibility Sequence Numbers" (VSNs) instead?
For instance, given a sequence like (T1-T3 all sync transactions):
1. T1 writes commit record at LSN 123
2. T2 writes commit record at LSN 124
3. Flush to LSN 124
4. T2 writes commit-visibility record at LSN 125
5. T3 takes a snapshot with VSN=125 and returns data to client that
includes T2's changes but not T1's changes
6. crash
Recovery does not have enough information to know whether T1 or T2
became visible first, so we'd probably need to create a new "multi-
commit-visible" record that makes them all visible at the same LSN.
That would mean that the snapshot taken by T3 (that was externally
observed) could never exist in the recovered system, even though both
T1 and T2 exist.
That might have consequences for PITR. With recovery_target_xid, do you
recover up to its commit record or its commit-visibility record?
Regards,
Jeff Davis
Re: Commit Sequence Numbers and Visibility
От:
Matthias van de Meent <boekewurm+postgres@gmail.com>
Дата:
On Thu, 11 Jun 2026 at 02:50, Jeff Davis wrote: > > On Wed, 2026-06-10 at 21:18 +0200, Matthias van de Meent wrote: > > I think it's desireable that snapshots don't need to take special > > care > > around the durability of the transactions that are included in their > > snapshot. > > Async transactions may want to see sync transactions' durable data, > > but probably prefer not to have to wait for that durability just > > because its session logged its latest commit record after the sync > > transaction did. > > For the problem sequence with CSNs: > > 1. sync transaction T1 writes commit record > 2. async transaction T2 writes commit record > 3. T2 releases locks > 4. T3 takes a snapshot > 5. T1's commit record is flushed > 6. T1 releases locks > > there are three resolutions: > > a. force T3 to wait until T1's commit record is flushed before > using the snapshot, slowing down the sync part of the > workload; or > > b. force T2 to wait until all unflushed sync transactions with > an earlier commit LSN are flushed before releasing locks > (thereby making the above seqeunce impossible), slowing down > the async part of the workload; or > > c. let T3 use the snapshot immediately, potentially returning > unflushed data to the client. > > Perhaps none of those options is great for everyone, but we could allow > users to select the behavior they want. That seems better than today, > when any async transaction can cause sync transactions to start > returning unflushed data to the client, and there's no way to prevent > that. Yes, "wait for data I'm reading to match my own durability requirement" should be configurable. But I'm not sure if we want to do that at snapshot acquisition time, or only when the data is being read that was modified by said transaction -- the latter would be a very attractive optimization for workloads with different durability expectations which touch completely -or, mostly- disjoint datasets in the same database. > > > > > Visibility would presumably happen after the recovery/replica has > > made > > sure that the durability of the pending-visible transactions is > > guaranteed; which presumably could be done with a sync wait at > > end-of-recovery. > > That might be fine, but it's different from CSNs as I understand the > meaning (at least in the simplest and most intuitive meaning). Should > we call them "Visibility Sequence Numbers" (VSNs) instead? I think that'd help the difference between durability and visibility, yes. > For instance, given a sequence like (T1-T3 all sync transactions): > > 1. T1 writes commit record at LSN 123 > 2. T2 writes commit record at LSN 124 > 3. Flush to LSN 124 > 4. T2 writes commit-visibility record at LSN 125 > 5. T3 takes a snapshot with VSN=125 and returns data to client that > includes T2's changes but not T1's changes > 6. crash > > Recovery does not have enough information to know whether T1 or T2 > became visible first, so we'd probably need to create a new "multi- > commit-visible" record that makes them all visible at the same LSN. > That would mean that the snapshot taken by T3 (that was externally > observed) could never exist in the recovered system, even though both > T1 and T2 exist. Correct, but are we also targeting true snapshot transferability from primary to replicas? I don't think we need to be able to recreate all snapshots that could exist on a primary on the replica (we'd have to consider CommandIDs), as long as all snapshots that can be created on a replica can also have been created on the primary; which I think is what this VSN approach is able to guarantee. Note that I believe that there is no meaningful difference from a consistency standpoint between T1 and T2 both becoming visible at the same time (with the same VSN) during recovery, and the system not having any snapshot acquired between their VSNs -- which is something that could happen on replicas. Side effects of a transaction won't appear in WAL before the VSN of that transaction, so there should be no opportunity for ordering issues here. > That might have consequences for PITR. With recovery_target_xid, do you > recover up to its commit record or its commit-visibility record? Good point. I suspect we'd have to make that a configurable option, but VSN (which would be end of WAL if no VSN was logged for that commit) would probably make the most sense, as it is least likely to cause snapshot issues if further wal may be replayed afterward. Kind regards, Matthias van de Meent Databricks (https://www.databricks.com)
Re: Commit Sequence Numbers and Visibility
От:
Jeff Davis <pgsql@j-davis.com>
Дата:
On Thu, 2026-06-11 at 10:28 +0200, Matthias van de Meent wrote:
>
> Yes, "wait for data I'm reading to match my own durability
> requirement" should be configurable.
>
> But I'm not sure if we want to do that at snapshot acquisition time,
> or only when the data is being read that was modified by said
> transaction -- the latter would be a very attractive optimization for
> workloads with different durability expectations which touch
> completely -or, mostly- disjoint datasets in the same database.
Agreed, there are several options here and I think it's an area where
configuration makes sense.
> > >
> > That might be fine, but it's different from CSNs as I understand
> > the
> > meaning (at least in the simplest and most intuitive meaning).
> > Should
> > we call them "Visibility Sequence Numbers" (VSNs) instead?
>
> I think that'd help the difference between durability and visibility,
> yes.
WAL is such a central part of our overall architecture that I think a
simpler visibility foundation (i.e. real CSNs) will pay off in a big
way.
I see "Visibility Sequence Numbers" as a somewhat-unfortunate
compromise. VSNs get the right information in WAL to keep the primary
and replica consistent, which is certainly good; but they leave us with
visibility semantics that don't match the commit order, which is a
source of confusion.
If we have the right set of configuration knobs as suggested above (to
handle mixed async/sync workloads), do you still think we should pursue
VSNs over CSNs?
> Correct, but are we also targeting true snapshot transferability from
> primary to replicas?
That would be a significant benefit of CSNs over VSNs. There would
still be some details to work out about timelines, and maybe other
things I haven't considered, but CSNs get us a lot closer.
> Note that I believe that there is no meaningful difference from a
> consistency standpoint between T1 and T2 both becoming visible at the
> same time (with the same VSN) during recovery, and the system not
> having any snapshot acquired between their VSNs -- which is something
> that could happen on replicas. Side effects of a transaction won't
> appear in WAL before the VSN of that transaction, so there should be
> no opportunity for ordering issues here.
You're correct that VSNs eliminate inconsistencies between the primary
and the replica (or failed-over/recovered system). If there's some
snapshot on the primary with T2 but not T1, there won't be a snapshot
somewhere else with T1 but not T2.
But we will have collapsed T1 and T2 into a single visibility event,
which is technically a loss of information, and we'd need to sort
through the nuances and implications.
> > That might have consequences for PITR. With recovery_target_xid, do
> > you
> > recover up to its commit record or its commit-visibility record?
>
> Good point.
>
> I suspect we'd have to make that a configurable option, but VSN
> (which
> would be end of WAL if no VSN was logged for that commit) would
> probably make the most sense, as it is least likely to cause snapshot
> issues if further wal may be replayed afterward.
Let recovery_target_xid=T1 and let the WAL contents be:
LSN 122 TL1: commit record for T0
LSN 123 TL1: commit record for T1
LSN 124 TL1: commit record for T2
LSN 125 TL1: commit-visible record for T1
LSN 126 TL1: commit-visible record for T0
LSN 127 TL1: whatever
LSN 128 TL1: commit-visible record for T2
If you recover to the commit-visible record (LSN=125), then T0's and
T2's commit records have been replayed, but not their commit-visible
record, so you must write the multi-visible record:
LSN 126 TL2: multi-visible record for {T0, T2}
before startup. That means you have effectively recovered to T2, not
T1. That's wrong, therefore we must define recovery based on commit
records.
If you recover to the commit record (LSN=123) instead, then T0's and
T1's commit records have been replayed, but not their commit-visible
records, so you must write a multi-visible record:
LSN 124 TL2: multi-visible record for {T0, T1}
which loses the information that T1 became visible before T0. That
might be acceptable, but CSNs just seem a lot simpler.
Regards,
Jeff Davis
Re: Commit Sequence Numbers and Visibility
От:
Matthias van de Meent <boekewurm+postgres@gmail.com>
Дата:
On Fri, 12 Jun 2026 at 00:24, Jeff Davis wrote:
>
> I see "Visibility Sequence Numbers" as a somewhat-unfortunate
> compromise. VSNs get the right information in WAL to keep the primary
> and replica consistent, which is certainly good; but they leave us with
> visibility semantics that don't match the commit order, which is a
> source of confusion.
>
> If we have the right set of configuration knobs as suggested above (to
> handle mixed async/sync workloads), do you still think we should pursue
> VSNs over CSNs?
Maybe we should add VSNs as fourth option in the list, as it keeps
PG's current visibility ordering mostly intact - except in the case of
recovery where all transactions might become visible all at once.
Which is not something I'm particularly worried about, for the reasons
I mentioned in my mail yesterday.
Current snapshot semantics in PG are quite nice [ignoring cancellation
bugs, and replicas] for workloads with mixed durability requirements,
in that visibility == transaction's configured durability, and no
snapshot has to spend its own time waiting for a transaction's
durability expectation, unless it is explicitly blocked by that
transaction's locks.
> > Correct, but are we also targeting true snapshot transferability from
> > primary to replicas?
>
> That would be a significant benefit of CSNs over VSNs. There would
> still be some details to work out about timelines, and maybe other
> things I haven't considered, but CSNs get us a lot closer.
CommandIds are a big issue with full RW snapshot transfers. RO
snapshot transfers would be easy with both CSN and VSN, in both cases
assuming that both systems are using the same timeline; it's just that
for those transfers you may have to add some not-yet-durable ordering
data (or wait for the VSN to arrive on the replica).
> > Note that I believe that there is no meaningful difference from a
> > consistency standpoint between T1 and T2 both becoming visible at the
> > same time (with the same VSN) during recovery, and the system not
> > having any snapshot acquired between their VSNs -- which is something
> > that could happen on replicas. Side effects of a transaction won't
> > appear in WAL before the VSN of that transaction, so there should be
> > no opportunity for ordering issues here.
>
> You're correct that VSNs eliminate inconsistencies between the primary
> and the replica (or failed-over/recovered system). If there's some
> snapshot on the primary with T2 but not T1, there won't be a snapshot
> somewhere else with T1 but not T2.
>
> But we will have collapsed T1 and T2 into a single visibility event,
> which is technically a loss of information, and we'd need to sort
> through the nuances and implications.
I don't think there are many nuances. If the replica/recovery doesn't
have the VSN of certain transactions yet, then there also can't be any
effects in the database that depend on the VSN ordering of those
transactions: locks are only released after the VSN is assigned, so
any persistent VSN ordering dependencies can only appear after the
VSN.
And, for external readers, at worst, they won't be able to see or know
the intermediate snapshot ordering they could've seen on the primary
before the server crash/failover/recovery point, but after the last
durable WAL was logged. I don't think we should break our minds over
that: every transaction with its (sufficiently high) durability
guarantee satisfied that was visible on the primary will be visible
after recovery/promotion.
> Let recovery_target_xid=T1 and let the WAL contents be:
>
> LSN 122 TL1: commit record for T0
> LSN 123 TL1: commit record for T1
> LSN 124 TL1: commit record for T2
> LSN 125 TL1: commit-visible record for T1
> LSN 126 TL1: commit-visible record for T0
> LSN 127 TL1: whatever
> LSN 128 TL1: commit-visible record for T2
>
> If you recover to the commit-visible record (LSN=125), then T0's and
> T2's commit records have been replayed, but not their commit-visible
> record, so you must write the multi-visible record:
>
> LSN 126 TL2: multi-visible record for {T0, T2}
>
> before startup. That means you have effectively recovered to T2, not
> T1. That's wrong, therefore we must define recovery based on commit
> records.
>
> If you recover to the commit record (LSN=123) instead, then T0's and
> T1's commit records have been replayed, but not their commit-visible
> records, so you must write a multi-visible record:
>
> LSN 124 TL2: multi-visible record for {T0, T1}
>
> which loses the information that T1 became visible before T0. That
> might be acceptable, but CSNs just seem a lot simpler.
It's probably simpler, yes, but the VSN approach does resolve my concerns about
i.) commit throughput in light of the waits introduced to every
backend by options (a) and (b), and
ii.) the lack of visible data's commit durability guarantee in (c).
Adjusting (c) with durability waits when the snapshot encounters data
modified by not-yet-fully-durable transactions would just add this
option to the 'newly introduced waits' concern list, too.
None of that would be a concern if we only allowed a global
synchronous_commit setting, but that's not a world we live in, nor a
world we're planning to live in (I think), so I do worry about these
concerns.
Kind regards,
Matthias van de Meent
Databricks (https://www.databricks.com)
Re: Commit Sequence Numbers and Visibility
От:
Matthias van de Meent <boekewurm+postgres@gmail.com>
Дата:
On Sat, 13 Jun 2026 at 02:25, Jeff Davis wrote: > > On Fri, 2026-06-12 at 12:03 +0200, Matthias van de Meent wrote: > > > > But we will have collapsed T1 and T2 into a single visibility > > > event, > > > which is technically a loss of information, and we'd need to sort > > > through the nuances and implications. > > > > I don't think there are many nuances. > > How would remote_apply work? The docs say: > > "When set to remote_apply, commits will wait until replies from the > current synchronous standby(s) indicate they have received the commit > record of the transaction and applied it, so that it has become visible > to queries on the standby(s), and also written to durable storage on > the standbys." > > For it to have become visible remotely, the commit-visible record must > also have been applied remotely. But the commit-visible record can't > even be written locally until we've met the transaction's durability > requirement. If remote_apply counts as a "durability requirement", then > we have a cycle. Yeah, that is something I hadn't yet taken into consideration. That does disqualify remote_apply from being a requirement for VSN logging, at least in its current form. > [various problems] > Perhaps these are not real problems or perhaps they have some > resolution. Regardless, my main point is that we want to get away from > the need to reason through this complexity. CSNs are simple to reason > about, and that simplicity will pay off across all the dependent > subsystems: replication, PITR, etc. That is a good point. I agree CSNs are easier to reason about in those cases. > > Adjusting (c) with durability waits when the snapshot encounters data > > modified by not-yet-fully-durable transactions would just add this > > option to the 'newly introduced waits' concern list, too. > > I'll think about this some more, it might eliminate most of the waits > in practical cases. I'm not sure it'll be sufficient for specific cases, but many cases, probably, yes. I think it's not weird to use async xacts for frequently updated rows (counters), whose effects are then used to affect blindly-issued sync xacts. It's not entirely unlike how sequences work right now, except that sequences don't have MVCC. - Matthias