skip to content

What can hold an MVCC database's snapshot horizon back when no session shows an old open transaction, and how do you release each holder safely?

level: seniorimportance: should knowfreq 38%

answer

  1. not every holder is a session
  2. prepared transactions survive restarts
  3. slots retain for absent consumers
  4. feedback imports replica queries
  5. ask the coordinator before deciding

basics

~20 s

Holders outside the session list: orphaned prepared (two-phase) transactions, replication or change-capture slots with stalled consumers, and replica feedback. Release each: resolve the prepared transaction with its coordinator, catch up or drop the slot, end the replica query or disable feedback.

solid answer

~50 s

When cleanup reports dead versions it may not remove while every session's transaction is young, the horizon is held by something that is not a client session. - **Prepared (two-phase) transactions.** A prepared transaction survives disconnects and restarts and counts as running until someone commits or rolls it back. Get the global outcome from the coordinator first; deciding it locally can break atomicity across participants. - **Replication and change-capture slots.** A slot retains history for its consumer and, on some engines, also holds the cleanup horizon. Restart the consumer so it catches up, or drop the slot and accept that the consumer must re-snapshot. - **Replica feedback.** A replica that reports its oldest query extends the primary's horizon to cover it. End that query, or turn feedback off and accept replica query cancellations. Then alert on each holder's age, not on sessions alone.

code

pseudocode · 13 lines
pseudocode
-- hidden horizon holders, checked in order
if not cleanup.reports("dead but not removable"):
    stop                                   -- a throughput problem, not a horizon one
check oldest_client_transaction_age        -- young? keep going
for p in prepared_transactions order by prepared_at:
    outcome := coordinator.outcome(p.global_id)
    commit or roll back p to match outcome
for s in slots order by retained_history desc:
    if s.consumer_attached: let it catch up
    else: drop s                           -- consumer must re-snapshot
for r in replicas where r.feedback_on:
    if r.reported_horizon is old:
        end r's long query, or turn feedback off for r

go deeper

for a junior

Know that client sessions are not the only thing that can stop cleanup: prepared transactions, replication slots and replica feedback can hold old history in place too.

for a middle

Explain how each hidden holder pins the horizon, where you would look for it, and why tuning cleanup cannot help while one of them is present.

for a senior

Run the diagnosis in order, then release each holder safely: the coordinator's outcome before resolving a prepared transaction, catch-up before drop for slots, a feedback decision per replica.

for a principal

Make every holder owned and observable: age alerts per holder type, slot lifecycle tied to consumer lifecycle, automated in-doubt recovery, and a written feedback policy for each replica.

## The signature: cleanup is not allowed, yet nothing looks old Background cleanup in an MVCC engine may reclaim only row versions superseded before the **horizon**, the oldest point in history that anything in the system might still read. The usual holder is a client session with an old open transaction, and the session list shows it at a glance. This scenario is the harder one: cleanup reports dead versions it is **not permitted** to remove, the tables keep growing, and every session's transaction is a few minutes old at most. Something outside the session list holds the horizon, and tuning cleanup harder changes nothing, because the constraint is correctness, not throughput. Three kinds of holder hide there. ## Holder 1: orphaned prepared transactions In **two-phase commit**, a coordinator asks each participating database to *prepare*: write everything needed to commit, then wait for the global decision. A prepared transaction is deliberately durable. It is not tied to a connection, it survives client crashes and server restarts, and until it is committed or rolled back it counts as a running transaction, so it holds its place in the horizon. If the coordinator crashed or lost track of it, it can sit there for weeks. **Find it** in the engine's list of prepared transactions, sorted by prepare time. **Release it safely** by asking the coordinator, or its recovery log, what the global outcome was, then committing or rolling back to match. Resolving it locally without that answer is a *heuristic decision*: if other participants went the other way, the distributed transaction is no longer atomic and someone must reconcile the data by hand. ## Holder 2: replication and change-capture slots A **slot** records how far a consumer, a replica or a change-data-capture pipeline, has read, so the source keeps everything that consumer has not yet received. Depending on the engine and the slot type, that retention covers only the change log, or also holds the cleanup horizon, either for every table or for the system catalogs only. A consumer that crashed, was decommissioned or fell hopelessly behind leaves its slot pinned at its last position. **Find it** in the engine's slot view: last confirmed position, whether a consumer is attached, and how much history the slot retains. **Release it** by restarting the consumer so it catches up, which preserves the stream. If the consumer is gone or cannot catch up, **drop the slot**: the source is relieved at once, and the consumer, if it ever returns, must start again from a fresh snapshot. Where the engine supports it, cap the history a slot may retain, so an abandoned consumer breaks instead of filling the source's disk. ## Holder 3: replica feedback On engines whose replicas replay the primary's physical changes, replay can remove a version that a replica query is still reading; the replica must then cancel that query or delay replay. **Feedback** avoids both by having the replica report its oldest snapshot to the primary, which treats it as part of its own horizon. A six-hour report on the replica therefore pins the primary as if it ran there. With some engines, a replica that disconnects keeps its last reported horizon pinned through its slot. **Find it** by comparing the horizon each replica reports with the primary's oldest local transaction. **Release it** by ending the replica query, or by turning feedback off for that replica and accepting that its long queries may be cancelled. That is a per-replica policy choice: a reporting replica and a failover replica rarely want the same answer. ## The diagnostic order 1. Confirm that cleanup reports versions it is **not allowed** to remove, rather than running out of time or I/O budget. 2. Check the oldest client transaction; if it is young, move on. 3. List prepared transactions by age. 4. List slots by age and retained history, noting any with no consumer attached. 5. Check the horizon each replica reports through feedback. ## Preventing a repeat - **Alert on each holder's age separately**: oldest transaction, oldest prepared transaction, oldest slot position, oldest replica-reported horizon. A dashboard that watches sessions only is blind to all three hidden holders. - **Give every slot an owner**: each one has a named consumer, and decommissioning a consumer includes dropping its slot. - **Give the coordinator a recovery process** that resolves in-doubt transactions after a crash, so none depends on a person noticing. - **Decide feedback per replica**, according to what that replica is for.

  • Why is rolling back an orphaned prepared transaction on your own a risk, even though it frees the horizon?
    Because a prepared transaction is one participant's half of a distributed decision. If the coordinator had already told other participants to commit, a local rollback leaves the transaction committed in some databases and rolled back in yours, so atomicity is gone and someone must reconcile the data by hand. Get the outcome from the coordinator or its recovery log, then commit or roll back to match.
  • What does dropping a change-capture slot cost the pipeline that reads it?
    Its position. The source stops retaining history for that consumer, so when the pipeline returns it cannot resume where it stopped; it must take a fresh snapshot of the tables it captures and stream from there, and everything downstream must tolerate the reload. That is why dropping is for consumers that are gone, while a live but lagging consumer should be helped to catch up.
  • Cleanup removes everything it is allowed to and still falls behind. Which of these holders could explain that?
    None of them. Holders show up as versions cleanup is not allowed to remove. When it removes what it may and still loses ground, the horizon is healthy and the problem is reclamation capacity, such as coarse trigger thresholds, too few workers or a throttled I/O budget, which is tuning work rather than a holder to release.

  • Concluding the horizon is free because the session list shows nothing old
  • Rolling back a prepared transaction without asking the coordinator
  • Dropping a slot whose consumer is alive and merely lagging
  • Assuming a replica's queries can never affect the primary
  • Tuning cleanup harder when it reports versions it may not remove

context