Skip to content

adapter: keep catalog upper up-to-date with read ts - #35402

Merged
teskje merged 1 commit into
MaterializeInc:mainfrom
teskje:catalog-strict-serializable
Mar 16, 2026
Merged

teskje merged 1 commit into
MaterializeInc:mainfrom
teskje:catalog-strict-serializable

Conversation

@teskje

@teskje teskje commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

This PR introduces a way to advance the frontier of the catalog shard without appending any new updates and then uses that to keep the catalog shard's frontier up to date with the oracle read ts. This ensures that mz_catalog_raw is always readable with strict serializable isolation, since under that isolation level, the oracle read ts will be used as the query timestamp.

This increases the amount of CRDB queries we make, instead of bumping only the txn-wal, we now additionally need to bump the catalog shard. But note that in two of the three instances the advance_upper call replaces a confirm_leadership call, which also did a CRDB query, so one can hope that the resulting number of CRDB calls is similar.

This could potentially be further improved by moving the catalog shard into txn-wal, but that's a larger lift so we leave it as future work.

Motivation

Closes SQL-117

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for opening this PR! Here are a few tips to help make the review process smooth for everyone.

PR title guidelines

  • Use imperative mood: "Fix X" not "Fixed X" or "Fixes X"
  • Be specific: "Fix panic in catalog sync when controller restarts" not "Fix bug" or "Update catalog code"
  • Prefix with area if helpful: compute: , storage: , adapter: , sql:

Pre-merge checklist

  • The PR title is descriptive and will make sense in the git log.
  • This PR has adequate test coverage / QA involvement has been duly considered. (trigger-ci for additional test/nightly runs)
  • If this PR includes major user-facing behavior changes, I have pinged the relevant PM to schedule a changelog post.
  • This PR has an associated up-to-date design doc, is a design doc (template), or is sufficiently small to not require a design.
  • If this PR evolves an existing $T ⇔ Proto$T mapping (possibly in a backwards-incompatible way), then it is tagged with a T-proto label.
  • If this PR will require changes to cloud orchestration or tests, there is a companion cloud PR to account for those changes that is tagged with the release-blocker label (example).

@teskje
teskje force-pushed the catalog-strict-serializable branch 4 times, most recently from 1f6f633 to 62ea854 Compare March 12, 2026 13:51
///
/// Panics if not in `Writable` mode.
/// Panics if `next_upper` is not greater than `self.upper`.
async fn compare_and_append_inner(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split out from compare_and_append so that we can also call it from advance_upper. In advance_upper, we don't need to sync the catalog afterwards, so calling compare_and_append would be needlessly expensive.

This commit introduces a way to advance the frontier of the catalog
shard without appending any new updates and then uses that to keep the
catalog shard's frontier up to date with the oracle read ts. This
ensures that `mz_catalog_raw` is always readable with strict
serializable isolation, since under that isolation level, the oracle
read ts will be used as the query timestamp.

This increases the amount of CRDB queries we make, instead of bumping
only the txn-wal, we now additionally need to bump the catalog shard.
But note that in two of the three instances the `advance_upper` call
replaces a `confirm_leadership` call, which also did a CRDB query, so
one can hope that the resulting number of CRDB calls is similar.

This could potentially be further improved by moving the catalog shard
into txn-wal, but that's a larger lift so we leave it as future work.
@teskje
teskje force-pushed the catalog-strict-serializable branch from 62ea854 to 903cecc Compare March 12, 2026 15:32
@teskje
teskje marked this pull request as ready for review March 13, 2026 09:13
@teskje
teskje requested review from a team as code owners March 13, 2026 09:13
@teskje
teskje requested review from aljoscha, ggevay and mtabebe and removed request for aljoscha March 13, 2026 09:13

@mtabebe mtabebe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change lgtm, mostly questions

// read_ts do not block waiting for the catalog shard's upper to advance.
let catalog_upper_start = Instant::now();
self.catalog
.advance_upper(advance_to)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So my understanding is that instead of just confirming leadership, we know also advance the upper which ends up being a write, even if there are no changes? Which is really the goal of this whole PR, so that it ensures strict serializability.

What is the performance trade-off of this? Or was it an incorrect behaviour before?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't incorrect before when the catalog was not queryable. Before we didn't actually care about the timestamps used for the catalog shard, they could have just been a counter. I guess we decided to use the timestamp oracle for it instead, in preparation for making it queryable later, which saves me a lot of work now!

As to the performance impact, I hope it to be small. We are trading one database call with another. Before we had to fetch the current frontier of the catalog shard, which afaict amounts to running (a consensus "scan"):

SELECT sequence_number, data FROM consensus
WHERE shard = $1 AND sequence_number >= $2
ORDER BY sequence_number ASC LIMIT $3

Now we do a consensus compare-and-set, which translates to:

INSERT INTO consensus (shard, sequence_number, data)
SELECT $1, $2, $3
WHERE (SELECT sequence_number FROM consensus
    WHERE shard = $1
    ORDER BY sequence_number DESC LIMIT 1) = $4

The compare-and-set query is certainly more expensive than the scan query, so there will be some impact, a couple milliseconds. If that turns out to be too much, we can consider moving the compare-and-append off the coordinator thread, though we might have to re-introduce the confirm_leadership call then.

async fn advance_upper(&mut self, new_upper: Timestamp) -> Result<(), CatalogError> {
if self.upper >= new_upper {
// We don't expect a no-op advancement, but if we are wrong we'd crash the process.
// Seems safer to only soft-assert and return gracefully in production. If we get here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to gracefully introduce this assertion 👍

simple conn=mz_system,user=mz_system
SET transaction_isolation = serializable;
SELECT data->'value'->>'name' FROM mz_internal.mz_catalog_raw WHERE data->>'kind' = 'Item' AND data->'value'->>'name' = 't';
SELECT data->'value'->>'name'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, so previously this was legal, and now it isn't.

Out of curiosity, if you tried to do this with serializable, what would happen? Do we expect to throw an error. Just silently act strictly serializable?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still legal to query with serializable isolation! It's just not necessary anymore. Previously the frontier of mz_catalog_raw didn't advance with time, so queries with strict serializable isolation would hang until some catalog change happened, advancing the frontier to a current time. Now they work.

In tests strict serializable isolation is preferable since serializable is allowed to give you any past result, which could lead to flaky tests. For example, when creating a new table, and then querying mz_catalog_raw with serializable isolation afterwards, the new table isn't guaranteed to show up. It is guaranteed to show up with strict serializable though.

@teskje

teskje commented Mar 16, 2026

Copy link
Copy Markdown
Contributor Author

TFTRs!

@teskje
teskje merged commit af5783a into MaterializeInc:main Mar 16, 2026
2 checks passed
@teskje
teskje deleted the catalog-strict-serializable branch March 16, 2026 09:45
antiguru pushed a commit to antiguru/materialize that referenced this pull request Mar 26, 2026
…5402)

This PR introduces a way to advance the frontier of the catalog shard
without appending any new updates and then uses that to keep the catalog
shard's frontier up to date with the oracle read ts. This ensures that
`mz_catalog_raw` is always readable with strict serializable isolation,
since under that isolation level, the oracle read ts will be used as the
query timestamp.

This increases the amount of CRDB queries we make, instead of bumping
only the txn-wal, we now additionally need to bump the catalog shard.
But note that in two of the three instances the `advance_upper` call
replaces a `confirm_leadership` call, which also did a CRDB query, so
one can hope that the resulting number of CRDB calls is similar.

This could potentially be further improved by moving the catalog shard
into txn-wal, but that's a larger lift so we leave it as future work.

### Motivation

Closes SQL-117
mtabebe added a commit to mtabebe/materialize that referenced this pull request Aug 21, 2026
Review of MaterializeInc#38399 found several claims that no longer match the code:

- The `allow_writes_in_read_only` tripwire falls back to a no-op, which
  now blocks promotion rather than leaving a cold collection at cut-over,
  and the storage-side check it points at is a hard assert, not a soft
  panic with a fallback. Say both, and say why compute is deliberately
  softer.
- Exclusive ownership of a replacement shard holds per (build version,
  deploy generation), not per process. Two read-only processes of one
  generation both write the shard, the same shape as a multi-replica MV,
  which the self-correcting persist sink is built to tolerate.
- The replica-side handler enables persist compaction process-wide, which
  this path is the first thing to trigger inside a read-only deployment.
- "Replacement" meant two unrelated things three lines apart in
  `bootstrap`: user `REPLACEMENT FOR` DDL and the builtin-migration
  mechanism. The sets are disjoint, so this is wording only.
- Record why a *new* builtin MV cannot be write-enabled the same way: its
  shard allocation lives only in the read-only savepoint, so the promoted
  leader allocates a different shard.
- Cite PR MaterializeInc#35402 for the v26.17 leader floor, and note in `caught_up.rs`
  that a future `Replacement` of `mz_cluster_replica_frontiers` would make
  the gate read its own frontiers.

The design doc stated the force-write unconditionally; give the two
conditions and the exclude-from-gate fallback.

Co-Authored-By: Claude Opus 5 <[email protected]>
mtabebe added a commit to mtabebe/materialize that referenced this pull request Aug 21, 2026
Review of MaterializeInc#38399 found several claims that no longer match the code:

- The `allow_writes_in_read_only` tripwire falls back to a no-op, which
  now blocks promotion rather than leaving a cold collection at cut-over,
  and the storage-side check it points at is a hard assert, not a soft
  panic with a fallback. Say both, and say why compute is deliberately
  softer.
- Exclusive ownership of a replacement shard holds per (build version,
  deploy generation), not per process. Two read-only processes of one
  generation both write the shard, the same shape as a multi-replica MV,
  which the self-correcting persist sink is built to tolerate.
- The replica-side handler enables persist compaction process-wide, which
  this path is the first thing to trigger inside a read-only deployment.
- "Replacement" meant two unrelated things three lines apart in
  `bootstrap`: user `REPLACEMENT FOR` DDL and the builtin-migration
  mechanism. The sets are disjoint, so this is wording only.
- Record why a *new* builtin MV cannot be write-enabled the same way: its
  shard allocation lives only in the read-only savepoint, so the promoted
  leader allocates a different shard.
- Cite PR MaterializeInc#35402 for the v26.17 leader floor, and note in `caught_up.rs`
  that a future `Replacement` of `mz_cluster_replica_frontiers` would make
  the gate read its own frontiers.

The design doc stated the force-write unconditionally; give the two
conditions and the exclude-from-gate fallback.

Co-Authored-By: Claude Opus 5 <[email protected]>
mtabebe added a commit to mtabebe/materialize that referenced this pull request Aug 21, 2026
Review of MaterializeInc#38399 found several claims that no longer match the code:

- The `allow_writes_in_read_only` tripwire falls back to a no-op, which
  now blocks promotion rather than leaving a cold collection at cut-over,
  and the storage-side check it points at is a hard assert, not a soft
  panic with a fallback. Say both, and say why compute is deliberately
  softer.
- Exclusive ownership of a replacement shard holds per (build version,
  deploy generation), not per process. Two read-only processes of one
  generation both write the shard, the same shape as a multi-replica MV,
  which the self-correcting persist sink is built to tolerate.
- The replica-side handler enables persist compaction process-wide, which
  this path is the first thing to trigger inside a read-only deployment.
- "Replacement" meant two unrelated things three lines apart in
  `bootstrap`: user `REPLACEMENT FOR` DDL and the builtin-migration
  mechanism. The sets are disjoint, so this is wording only.
- Record why a *new* builtin MV cannot be write-enabled the same way: its
  shard allocation lives only in the read-only savepoint, so the promoted
  leader allocates a different shard.
- Cite PR MaterializeInc#35402 for the v26.17 leader floor, and note that forcing the
  replacement mechanism across all builtins, as the 0dt tests do, leaves
  the caught-up gate reading its own frontiers.

The design doc stated the force-write unconditionally; give the two
conditions and the exclude-from-gate fallback.

Co-Authored-By: Claude Opus 5 <[email protected]>
mtabebe added a commit to mtabebe/materialize that referenced this pull request Aug 21, 2026
Review of MaterializeInc#38399 found several claims that no longer match the code:

- The `allow_writes_in_read_only` tripwire falls back to a no-op, which
  now blocks promotion rather than leaving a cold collection at cut-over,
  and the storage-side check it points at is a hard assert, not a soft
  panic with a fallback. Say both, and say why compute is deliberately
  softer.
- Exclusive ownership of a replacement shard holds per (build version,
  deploy generation), not per process. Two read-only processes of one
  generation both write the shard, the same shape as a multi-replica MV,
  which the self-correcting persist sink is built to tolerate.
- The replica-side handler enables persist compaction process-wide, which
  this path is the first thing to trigger inside a read-only deployment.
- "Replacement" meant two unrelated things three lines apart in
  `bootstrap`: user `REPLACEMENT FOR` DDL and the builtin-migration
  mechanism. The sets are disjoint, so this is wording only.
- Record why a *new* builtin MV cannot be write-enabled the same way: its
  shard allocation lives only in the read-only savepoint, so the promoted
  leader allocates a different shard.
- Cite PR MaterializeInc#35402 for the v26.17 leader floor, and note that forcing the
  replacement mechanism across all builtins, as the 0dt tests do, leaves
  the caught-up gate reading its own frontiers.

The design doc stated the force-write unconditionally; give the two
conditions and the exclude-from-gate fallback.

Co-Authored-By: Claude Opus 5 <[email protected]>
mtabebe added a commit to mtabebe/materialize that referenced this pull request Aug 21, 2026
- Seed the caught-up gate's transitive-dependent walk from all of
  `new_builtin_collections`, not just the MVs, and require the write
  frontier to be within the allowed lag of `now` on the
  no-live-frontier path so a hydrated-then-stalled collection no longer
  passes the gate forever.
- Document the `enable_0dt_hydrate_migrated_builtin_mvs` break-glass
  flag: it is read once at startup, so flipping it means changing the
  setting on the leader and restarting, and it is not an exact revert.
- Correct the read-only write-enable comments to match the code: the
  `allow_writes_in_read_only` tripwire blocks promotion, exclusive
  shard ownership holds per (build version, deploy generation), a new
  builtin MV cannot be write-enabled, and cite PR MaterializeInc#35402 for the
  v26.17 leader floor.
mtabebe added a commit to mtabebe/materialize that referenced this pull request Aug 24, 2026
- Seed the caught-up gate's transitive-dependent walk from all of
  `new_builtin_collections`, not just the MVs, and require the write
  frontier to be within the allowed lag of `now` on the
  no-live-frontier path so a hydrated-then-stalled collection no longer
  passes the gate forever.
- Document the `enable_0dt_hydrate_migrated_builtin_mvs` break-glass
  flag: it is read once at startup, so flipping it means changing the
  setting on the leader and restarting, and it is not an exact revert.
- Correct the read-only write-enable comments to match the code: the
  `allow_writes_in_read_only` tripwire blocks promotion, exclusive
  shard ownership holds per (build version, deploy generation), a new
  builtin MV cannot be write-enabled, and cite PR MaterializeInc#35402 for the
  v26.17 leader floor.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants