Repository navigation
adapter: don't append table writes a concurrent ALTER TABLE made stale - #38051
Conversation
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
| # 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 |
There was a problem hiding this comment.
"two places" feels like it might rot quickly 😅
|
|
||
| // 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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 ... 🤷♂️
There was a problem hiding this comment.
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.
aljoscha
left a comment
There was a problem hiding this comment.
looks good now! but we need to keep track of doing that deeper/structural fix 🙏
SQL-627 is logged |
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