Skip to content

adapter: don't append table writes a concurrent ALTER TABLE made stale - #38051

Merged
mtabebe merged 2 commits into
MaterializeInc:mainfrom
mtabebe:fix-bug-sql-267
Aug 12, 2026
Merged

mtabebe merged 2 commits into
MaterializeInc:mainfrom
mtabebe:fix-bug-sql-267

Conversation

@mtabebe

@mtabebe mtabebe commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem:

A race between ALTER table and table writes can lead to a panic... details follow...

The cause is that group commit resolves each write's target table to its latest collection and encodes the staged rows against that collection's RelationDesc. But the rows were packed earlier, against whatever version was latest when the statement ran. If an ALTER TABLE ... ADD COLUMN commits in between, the descriptor is wider than the rows are, and RowColumnarEncoder::append trips itertools::zip_eq and panics. That panic happens on the persist table write worker, so it aborts environmentd.

There are two windows where this can happen: (1) A blind INSERT that can't get the table's write lock parks in deferred_write_ops until the lock frees up and deferred plans get a PlanValidity check when they retry, deferred writes get nothing. (2) even with no lock contention at all, insert_constant packs its rows on a spawned task from an owned catalog snapshot, and the response then round-trips to the client before the implicit COMMIT stages the write.

Solution:

Check the staged rows against the descriptor group commit is about to resolve, and fail the transaction if they don't line up. This goes in the per-write loop, which still owns each transaction's ExecuteContext, before the writes get merged together across sessions.

The error is ConcurrentDependencyMutation, which carries a hint telling the client to retry.

TableData::Batches is exempt. It's already encoded and records its own schema with Persist, which migrates older parts on read, so COPY FROM racing an ALTER TABLE is fine.

Testing:

New regression test, test_insert_concurrent_alter_table. It checks that the INSERT comes back as 40001, that the table still works at its new schema, and that the rejected row didn't land.

The window is a cross-thread gap, so the test drives it through a new test-only failpoint, insert_after_pack_before_commit, in the same style as the existing peek_after_register_before_issue and subscribe_before_dispatch. The INSERT blocks there once its rows are packed and signals the test, the test widens the table, then the test hands it back. It's a rendezvous rather than a sleep, so there is no window to miss and nothing to tune.

Fixes SQL-267

Problem:

A race between ALTER table and table writes can lead to a panic... details...

The cause is that group commit resolves each write's target table to its latest
collection and encodes the staged rows against that collection's RelationDesc.
But the rows were packed earlier, against whatever version was latest when
the statement ran. If an ALTER TABLE ... ADD COLUMN commits in between,
the descriptor is wider than the rows are, and RowColumnarEncoder::append
trips itertools::zip_eq and panics. That panic happens on the persist table
write worker, so it aborts environmentd.

There are two windows where this can happen: (1) A blind INSERT that can't get
the table's write lock parks in deferred_write_ops until the lock frees up
and deferred plans get a PlanValidity check when they retry, deferred
writes get nothing. (2) even with no lock contention at all, insert_constant packs
its rows on a spawned task from an owned catalog snapshot, and the response then
round-trips to the client before the implicit COMMIT stages the write.

Solution:

Check the staged rows against the descriptor group commit is about to resolve, and
fail the transaction if they don't line up. This goes in the per-write loop, which
still owns each transaction's ExecuteContext, before the writes get merged
together across sessions.

The error is ConcurrentDependencyMutation, which carries a hint telling
the client to retry.

TableData::Batches is exempt. It's already encoded and records its own schema with
Persist, which migrates older parts on read, so COPY FROM racing an ALTER TABLE is
fine.

Testing:

New regression test, test_insert_concurrent_alter_table. It checks that the INSERT
comes back as 40001, that the table still works at its new schema, and that the
rejected row didn't land.

The window is a cross-thread gap, so the test drives it through a new test-only
failpoint, insert_after_pack_before_commit, in the same style as the existing
peek_after_register_before_issue and subscribe_before_dispatch. The INSERT blocks
there once its rows are packed and signals the test, the test widens the table,
then the test hands it back. It's a rendezvous rather than a sleep, so there is no
window to miss and nothing to tune.

Confirmed the test fails with the zip_eq panic when the new check is neutered, so
it does reproduce the bug and not just pass.

Fixes SQL-267
@mtabebe
mtabebe marked this pull request as ready for review August 5, 2026 00:06
@mtabebe
mtabebe requested review from a team as code owners August 5, 2026 00:06
# on purpose by pointing the default cluster at a nonexistent one, so the
# statement never reaches a write path at all. The trailing quote keeps it from
# matching "unknown cluster replica size" errors.
# A concurrently modified dependency comes from two places: the coordinator

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.

"two places" feels like it might rot quickly 😅

Comment thread src/adapter/src/coord/appends.rs Outdated

// These rows were packed against the table's RelationDesc as of whenever the
// statement ran, but the append below picks up whatever version is latest
// now. If an ALTER TABLE ... ADD COLUMN snuck in, that descriptor is wider

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.

the "is wider" bit will probably also rot, and we can instead concisely explain the situation here

}
}

/// Returns a table whose staged rows no longer match its latest `RelationDesc`, if any.

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.

I have a feeling this will only shorten the window for TOC/TOU problems, no? Can't the underlying persist schema still be changed, especially when/once there are concurrent envd instances. And so I'd think we have to fix at the source: trying to append at persist needs to bubble up the error, and we process here, instead of panic'ing in there?

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.

So in a single envd world, I think this solves the problem. stage_group_commit and sequence_alter_table both run on the coordinator's main loop. In stage_group_commit there's no .await between the stale_write_target check and sending the GroupCommitRequest, so the check and the gid we commit against are the same catalog snapshot. So, No ALTER can slip in between and interleave with a group commit.

In a multi envd world, I think you're right. A catalog snapshot isn't authoritative for what the shard will accept.

My feeling is, close the gap now since this is a bug tuesday thing, and defer the bigger change to later?

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.

Yeah fine for me, but we do have to get back to that. I also wouldn't know how much additional work it would be to fix at that deeper layer ... 🤷‍♂️

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.

I think it would be manageable in 1-2 medium sized PRs, since we would need persist to return the error on mismatch. The tricky bit is that I think this error would bubble up in group-commit so we need to somehow either make it retryable or handle the bad write...

I'd need to think about it more.

@mtabebe
mtabebe requested a review from aljoscha August 6, 2026 17:26

@aljoscha aljoscha 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.

looks good now! but we need to keep track of doing that deeper/structural fix 🙏

@mtabebe
mtabebe merged commit 6f5533e into MaterializeInc:main Aug 12, 2026
130 of 132 checks passed
@mtabebe

mtabebe commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

looks good now! but we need to keep track of doing that deeper/structural fix 🙏

SQL-627 is logged

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.

2 participants