Migrations 000609, 000611 and 000615 start with a lock-retry block (added in #30297 and #30412) that is meant to stop live chat traffic from deadlocking with the migration. On a release upgrade it cannot do that, and it can turn a failure into a 2-minute hang or cause a failure that would otherwise not happen. This PR removes the blocks. A release upgrade that runs 000607 to 000615 while chats are in use is unreliable with or without them, so it needs chat traffic stopped, for example by scaling the deployment to zero first. `coder server` runs all pending migrations in one transaction, so every migration's locks are held until the transaction commits. On a release upgrade (from v2.38 or earlier), 000607 and 000608 lock `chats` before any block runs. A chat request that writes `chat_messages` or `chat_queued_messages`, or that reads `users`, holds a lock that a later migration needs and then waits for `chats`. The block can release only the locks it took, so it can avoid this deadlock only when its migration takes the first chat lock of the upgrade. Postgres checks a waiting request for a deadlock once, after `deadlock_timeout` (1 second by default). The block pauses 100ms between 100ms lock attempts, so a check that lands in a pause finds no deadlock, the request keeps its lock, and the block retries until it fails the upgrade after 2 minutes, with the request stuck until then. Without the block, the outcome depends on timing. If the request's check fires after the migration starts waiting for it, Postgres cancels the request and the upgrade continues. If the request had already waited longer than `deadlock_timeout`, its check found no deadlock, so Postgres cancels the migration instead and the upgrade fails. Main does not finish those cases either: its block was still retrying after a minute, and it gives up at its 2-minute deadline. 000609 and 000611 return to their content before #30297, and 000615 loses the block in both directions. A block's migration takes the first chat lock of an upgrade only when upgrading from a build of main at 608 through 614: dogfood, where the #30297 deploy failed, or a `coder-preview` image built between 2026-10-02 and 2026-10-11. No release stops in that range, and dogfood is already past 615. v2.39 ships these migrations with the blocks. Editing them leaves every schema unchanged: databases on v2.39 or later already ran them and do not run them again, and `dump.sql` regenerates without a diff. The change affects only databases upgrading from v2.38 or earlier. WI-46 proposed the opposite direction: make the migrator survive live chat traffic. The v2.38 upgrade thread concluded instead that old replicas must be stopped before an upgrade runs migrations. So `TestMigrationChatTablesConcurrentTraffic`, which tested the blocks under chat traffic, is removed without a replacement, and its helpers `async` and `dedicatedConn` move to `migration000638_test.go`, their remaining caller. <details> <summary>Measurements</summary> Each run starts chat requests while the upgrade transaction is open, on the Postgres 13 test container with the default `deadlock_timeout`. "Request canceled" means Postgres canceled the chat request as the deadlock victim. "Stuck" means the migration was still retrying when the run was stopped, after 10 seconds unless noted. | Upgrade from | Migration | Chat requests | main | This PR | |---|---|---|---|---| | v2.38 (605) | 000609 | queued message insert | upgrade failed after 2m0s | request canceled after 1s, upgrade ok | | v2.38 (605) | 000609 | chat message insert | stuck | request canceled after 1s, upgrade ok | | v2.38 (605) | 000609 | chat message insert that waited 1.2s before 000609 started | stuck (stopped after 1 minute) | migration canceled after 1s | | v2.38 (605) | 000609 | chat message insert and queued message insert | stuck (stopped after 1 minute) | migration canceled after 2s | | v2.38 (605) | 000615 | read `users`, then `chats` | stuck | request canceled after 1s, upgrade ok | | v2.38 (605) | 000615 | read `users`, then `chats`, plus a chat message insert | stuck (stopped after 1 minute) | migration canceled after 2s | | 607 (main build) | 000609 | queued or chat message insert | stuck | request canceled after 1s, upgrade ok | | 610 (main build) | 000615 | queued message insert | stuck | request canceled after 1s, upgrade ok | | 608, 610, 614 (main build) | 000609, 000611, 000615 as the first chat lock | the four cases in `TestMigrationChatTablesConcurrentTraffic` | no deadlock | 2, 1 and 1 of 4 deadlock; in the 000615 case Postgres canceled the migration, in the others a chat request | </details> > 🤖 This PR was created with the help of Coder Agents, and has been reviewed by a human. 🏂🏻
96 lines
2.8 KiB
SQL
96 lines
2.8 KiB
SQL
-- Removes the chat automations experiment (migrations 000609 to 000611).
|
|
-- Those migrations stay because deployments already ran them.
|
|
|
|
DROP VIEW IF EXISTS chats_expanded;
|
|
|
|
-- Messages that automations queued cannot be delivered without the
|
|
-- automation that owns them. Ordinary queued messages stay. The queue
|
|
-- version trigger sets chats.queue_version to chats.snapshot_version for
|
|
-- each affected chat. snapshot_version is not advanced: coderd restarts to
|
|
-- run this migration, every stream loop then bootstraps from an empty local
|
|
-- state and refetches the queue, and the chat worker reads the queue from
|
|
-- the database whenever it promotes a message.
|
|
DELETE FROM chat_queued_messages WHERE automation_id IS NOT NULL;
|
|
|
|
DROP INDEX chats_automation_idx;
|
|
ALTER TABLE chats
|
|
DROP COLUMN manage_automations_enabled,
|
|
DROP COLUMN automation_id;
|
|
|
|
DROP INDEX chat_messages_automation_idx;
|
|
ALTER TABLE chat_messages
|
|
DROP COLUMN input_id,
|
|
DROP COLUMN automation_id;
|
|
|
|
DROP INDEX chat_queued_messages_automation_idx;
|
|
ALTER TABLE chat_queued_messages
|
|
DROP CONSTRAINT chat_queued_messages_automation_shape,
|
|
DROP COLUMN queue_generation,
|
|
DROP COLUMN input_id,
|
|
DROP COLUMN automation_id;
|
|
|
|
DROP TABLE chat_automations;
|
|
DROP FUNCTION enforce_chat_automation_chat_organization();
|
|
|
|
DROP TYPE chat_automation_when_busy;
|
|
DROP TYPE chat_automation_webhook_use;
|
|
DROP TYPE chat_automation_target_mode;
|
|
DROP TYPE chat_automation_kind;
|
|
|
|
-- Recreate chats_expanded, dropped at the top of this migration, without
|
|
-- the removed columns.
|
|
CREATE VIEW chats_expanded AS
|
|
SELECT c.id,
|
|
c.owner_id,
|
|
c.workspace_id,
|
|
c.title,
|
|
c.status,
|
|
c.worker_id,
|
|
c.started_at,
|
|
c.heartbeat_at,
|
|
c.created_at,
|
|
c.updated_at,
|
|
c.parent_chat_id,
|
|
c.root_chat_id,
|
|
c.last_model_config_id,
|
|
c.last_reasoning_effort,
|
|
c.archived,
|
|
c.last_error,
|
|
c.mode,
|
|
c.mcp_server_ids,
|
|
c.labels,
|
|
c.build_id,
|
|
c.agent_id,
|
|
c.pin_order,
|
|
c.last_read_message_id,
|
|
c.dynamic_tools,
|
|
c.organization_id,
|
|
c.project_id,
|
|
c.plan_mode,
|
|
c.client_type,
|
|
c.last_turn_summary,
|
|
c.summary,
|
|
c.summary_generated_at,
|
|
c.snapshot_version,
|
|
c.history_version,
|
|
c.queue_version,
|
|
c.generation_attempt,
|
|
c.retry_state,
|
|
c.retry_state_version,
|
|
c.runner_id,
|
|
c.requires_action_deadline_at,
|
|
COALESCE(root.user_acl, c.user_acl) AS user_acl,
|
|
COALESCE(root.group_acl, c.group_acl) AS group_acl,
|
|
owner.username AS owner_username,
|
|
owner.name AS owner_name,
|
|
c.context_aggregate_hash,
|
|
c.context_dirty_since,
|
|
c.context_dirty_resources,
|
|
c.context_error,
|
|
c.compaction_requested_at,
|
|
c.title_source,
|
|
c.title_updated_at
|
|
FROM ((chats c
|
|
LEFT JOIN chats root ON ((root.id = COALESCE(c.root_chat_id, c.parent_chat_id))))
|
|
JOIN visible_users owner ON ((owner.id = c.owner_id)));
|