Agent Skills › archestra-ai/archestra › archestra-dev-interactions-migrations

archestra-dev-interactions-migrations

GitHub

针对Drizzle迁移中操作大型高频写入表(如interactions)的风险控制指南,涵盖安全与高风险操作区分、禁止在事务中建索引规则及预审计流程。

.agents/skills/archestra-dev-interactions-migrations/SKILL.md archestra-ai/archestra

Trigger Scenarios

执行涉及大表的数据库迁移 生成或审查Drizzle迁移脚本

Install

npx skills add archestra-ai/archestra --skill archestra-dev-interactions-migrations -g -y
More Options

Non-standard path

npx skills add https://github.com/archestra-ai/archestra/tree/main/.agents/skills/archestra-dev-interactions-migrations -g -y

Use without installing

npx skills use archestra-ai/archestra@archestra-dev-interactions-migrations

指定 Agent (Claude Code)

npx skills add archestra-ai/archestra --skill archestra-dev-interactions-migrations -a claude-code -g -y

安装 repo 全部 skill

npx skills add archestra-ai/archestra --all -g -y

预览 repo 内 skill

npx skills add archestra-ai/archestra --list

SKILL.md

Frontmatter
{
    "name": "archestra-dev-interactions-migrations",
    "description": "Use BEFORE writing or running any Drizzle migration that touches the `interactions` table (or any other very large, write-hot table). The interactions table is the platform's biggest, append-heavy table — every LLM proxy call writes a row — so a careless migration can take a write-blocking lock and stall the proxy. Covers which operations are safe vs table-rewriting\/lock-taking, the \"never rebuild an index in a transactional migration\" rule, and a read-only audit procedure against the GKE staging database to size the risk first."
}

Migrations against the interactions table

The interactions table is special: it is the largest table in the platform and is on the LLM proxy's hot write path — every proxied LLM call inserts a row. A migration that takes a strong lock on it, even briefly, blocks those inserts, so the proxy cannot record interactions until the migration finishes. On a large table a "quick" CREATE INDEX can hold that lock for minutes.

Treat any schema change to interactions as production-risk work. The same rules apply to any other very large, write-hot table.

Safe vs risky operations

Fast once the required lock is acquired (metadata-only, no table rewrite in PostgreSQL 11+):

  • ADD COLUMN ... DEFAULT <constant> NOT NULL — the default is stored as metadata; existing rows are not rewritten. This avoids a table-size-dependent rewrite. (The billing_mode column was added this way.)
  • ADD COLUMN nullable, with no default.
  • Nonvolatile defaults such as now() also avoid the rewrite; now() is stable, not volatile.
  • DROP DEFAULT, SET DEFAULT <constant>, renaming a column.

These operations still acquire table locks: ordinary ADD COLUMN requires ACCESS EXCLUSIVE, even without a rewrite. Check long-running transactions and use a bounded lock_timeout rather than assuming metadata-only means lock-free. See the PostgreSQL ALTER TABLE reference.

Operations with potentially table-size-dependent work:

  • ADD COLUMN ... DEFAULT <volatile expr> (e.g. clock_timestamp(), gen_random_uuid()) — rewrites every row.
  • ALTER COLUMN ... TYPE ... — usually rewrites the table.
  • SET NOT NULL on an existing column — full scan to validate.
  • CREATE INDEX / DROP INDEX (non-concurrent) — this is the most common trap. A plain CREATE INDEX takes a SHARE lock that blocks writes for the entire build; adding a column to an existing covering index means a DROP INDEX + CREATE INDEX rebuild.

The index rule

Never add, drop, or rebuild an index on interactions inside a Drizzle migration. Drizzle runs each migration in a single transaction, and CREATE INDEX CONCURRENTLY / DROP INDEX CONCURRENTLY cannot run inside a transaction — so the only thing a generated migration can emit is the blocking, non-concurrent form.

Instead:

  1. Keep the Drizzle schema's index definition matching what is actually deployed, so pnpm db:generate does not emit an index change. If you need a new index for a query, decide whether the query can tolerate a heap fetch instead — for an analytics query (not the hot path) it usually can.
  2. If the index is genuinely needed, apply it out of band as an ops step with CREATE INDEX CONCURRENTLY (and DROP INDEX CONCURRENTLY for the old one) during a maintenance window, then update the schema to match. CONCURRENTLY builds without blocking writes, at the cost of a slower build and a second table scan.

The migration linter (pnpm --dir backend check:migrations) flags DROP INDEX as an error and non-concurrent CREATE INDEX as a warning for exactly this reason. If it fires on an interactions migration, stop and rework the change — do not just add the allow-breaking marker.

Even CREATE INDEX IF NOT EXISTS on an index that already exists takes the write-blocking SHARE lock, and Drizzle holds it until the whole migration batch commits. Building the index out of band does not make a plain statement in the migration harmless.

Concurrent index builds in a migration

For a large, write-hot table whose concurrent build finishes well within the migration Job's deadline (archestra.migrationJob.activeDeadlineSeconds, 600 s), a migration can build the index itself without blocking writes. 0609_absent_scourge.sql (agents, conversations) is the reference. Every part is needed:

  1. COMMIT; first. drizzle-kit runs all pending migrations in one transaction, where CONCURRENTLY is refused. After the COMMIT, each statement runs on its own. The migrations before this one commit at that point.
  2. SET lock_timeout = '5min';. The Job sets lock_timeout=5s. A concurrent build waits for older transactions and fails at 5 s, leaving an invalid index. Its waits block no one, so the longer timeout is safe.
  3. CREATE INDEX CONCURRENTLY IF NOT EXISTS "<name>" ... for each index.
  4. A DO block that raises when any of the indexes is missing or invalid. Before a rerun, scripts/migrate-with-lock.mjs drops the invalid leftovers of indexes that migrations build concurrently (DROP INDEX CONCURRENTLY needs a condition, which a migration cannot express).
  5. RESET lock_timeout; to give later migrations the Job's value back, then BEGIN; to give them their transaction back. drizzle-kit commits it.

PGlite cannot build an index concurrently, so the test harnesses (src/test/migrations-helper.ts, scripts/migration-tag-upgrades.test.mjs) run these statements in their plain form.

Verify such a migration with the production command (node ./scripts/migrate-with-lock.mjs with PGOPTIONS=-c lock_timeout=5s) on the PostgreSQL major version that production runs:

  • with realistic table sizes and concurrent writes;
  • with a transaction held open longer than 5 s;
  • with the build interrupted and then rerun;
  • with a later migration in the same batch.

interactions itself stays out-of-band: its builds can exceed the Job's deadline.

Audit the table on staging before you ship

Before merging a migration that touches interactions, size the real table on the GKE staging database so you know the blast radius. This is read-only — never run the migration DDL by hand against staging or production; migrations deploy through the normal pipeline.

Access is via GCP/GKE IAM (managed separately from this repo), so the commands below grant nothing on their own.

  1. Switch kubectl to the GKE staging context:

    kubectl config get-contexts -o name | grep archestra-staging
    # e.g. gke_<project>_us-central1-a_archestra-staging
    kubectl config use-context <that-context>
    
  2. Find the Postgres pod (namespace archestra, container postgresql):

    kubectl get pods -n archestra | grep postgresql   # archestra-platform-postgresql-0
    
  3. Open a read-only psql session (use the app credentials already in the pod's environment; do not export secrets):

    kubectl exec -it -n archestra archestra-platform-postgresql-0 -c postgresql \
      -- bash -lc 'PGPASSWORD="$POSTGRES_PASSWORD" psql -U "$POSTGRES_USER" -d "$POSTGRES_USER"'
    
  4. Run the audit queries (all read-only):

    -- PostgreSQL version. Metadata-only ADD COLUMN ... DEFAULT needs 11+.
    SELECT version();
    
    -- Fast row estimate. NEVER run count(*) on this table — it scans everything.
    SELECT reltuples::bigint AS est_rows, relpages
    FROM pg_class WHERE relname = 'interactions';
    
    -- Heap / TOAST / index sizes.
    SELECT pg_size_pretty(pg_total_relation_size('interactions')) AS total,
           pg_size_pretty(pg_relation_size('interactions'))       AS heap,
           pg_size_pretty(pg_indexes_size('interactions'))        AS indexes;
    
    -- Per-index size — a non-concurrent rebuild is at least this expensive.
    SELECT indexrelname,
           pg_size_pretty(pg_relation_size(indexrelid)) AS size
    FROM pg_stat_user_indexes
    WHERE relname = 'interactions'
    ORDER BY pg_relation_size(indexrelid) DESC;
    
    -- Long-running transactions. A CREATE INDEX waits behind these AND, once it
    -- starts, blocks writes until it finishes — so know what's open first.
    SELECT pid, now() - xact_start AS xact_age, state, left(query, 80) AS query
    FROM pg_stat_activity
    WHERE xact_start IS NOT NULL AND pid <> pg_backend_pid()
    ORDER BY xact_start
    LIMIT 10;
    
  5. Read the numbers:

    • Metadata-only changes avoid a table rewrite, but can still wait for a conflicting transaction. Assess lock waits as well as table size.
    • A table rewrite or a non-concurrent index build scales with heap/index size. As a rough order of magnitude, an index build reads the whole table, sorts, and writes the index — expect it to be at least as slow as a full scan of the heap, and it holds the write lock the entire time. If that is more than a couple of seconds of estimated build time, do not do it in a transactional migration (see "The index rule").

When you switch away, restore your previous kubectl context (kubectl config use-context <previous>).

See also

  • archestra-dev-migrations — the general migration flow (pnpm db:generate, drizzle-kit check, check:migrations, data migrations, conflict resolution).

Version History

  • fa6fc52 Current 2026-11-02 10:42
  • 6290aab 2026-09-11 16:19

Same Skill Collection

.agents/skills/archestra-dev-backend-tests/SKILL.md
.agents/skills/archestra-dev-backend/SKILL.md
.agents/skills/archestra-dev-bench-analysis/SKILL.md
.agents/skills/archestra-dev-e2e/SKILL.md
.agents/skills/archestra-dev-frontend/SKILL.md
.agents/skills/archestra-dev-investigate/SKILL.md
.agents/skills/archestra-dev-llm-providers/SKILL.md
.agents/skills/archestra-dev-migrations/SKILL.md
.agents/skills/archestra-dev-observability/SKILL.md
.agents/skills/archestra-dev-override-sweep/SKILL.md
.agents/skills/archestra-dev-resource-cleanup/SKILL.md
.agents/skills/archestra-dev-rust-napi/SKILL.md
.agents/skills/archestra-dev-testing/SKILL.md
.agents/skills/archestra-docs-writer/SKILL.md
.agents/skills/archestra-mcp-catalog-entry/SKILL.md
.agents/skills/archestra-merge-queue/SKILL.md
.agents/skills/archestra-pr-hygiene/SKILL.md
.agents/skills/design-arena/SKILL.md
.agents/skills/design-taste-frontend/SKILL.md
.agents/skills/managing-archestra-releases/SKILL.md
.claude/skills/archestra-dev-backend-tests/SKILL.md
.claude/skills/archestra-dev-backend/SKILL.md
.claude/skills/archestra-dev-bench-analysis/SKILL.md
.claude/skills/archestra-dev-e2e/SKILL.md
.claude/skills/archestra-dev-frontend/SKILL.md
.claude/skills/archestra-dev-investigate/SKILL.md
.claude/skills/archestra-dev-llm-providers/SKILL.md
.claude/skills/archestra-dev-migrations/SKILL.md
.claude/skills/archestra-dev-observability/SKILL.md
.claude/skills/archestra-dev-override-sweep/SKILL.md
.claude/skills/archestra-dev-rust-napi/SKILL.md
.claude/skills/archestra-dev-testing/SKILL.md
.claude/skills/archestra-docs-writer/SKILL.md
.claude/skills/archestra-mcp-catalog-entry/SKILL.md
.codex/skills/archestra-dev-backend-tests/SKILL.md
.codex/skills/archestra-dev-backend/SKILL.md
.codex/skills/archestra-dev-bench-analysis/SKILL.md
.codex/skills/archestra-dev-e2e/SKILL.md
.codex/skills/archestra-dev-frontend/SKILL.md
.codex/skills/archestra-dev-investigate/SKILL.md
.codex/skills/archestra-dev-llm-providers/SKILL.md
.codex/skills/archestra-dev-migrations/SKILL.md
.codex/skills/archestra-dev-observability/SKILL.md
.codex/skills/archestra-dev-override-sweep/SKILL.md
.codex/skills/archestra-dev-rust-napi/SKILL.md
.codex/skills/archestra-dev-testing/SKILL.md
.codex/skills/archestra-docs-writer/SKILL.md
.codex/skills/archestra-mcp-catalog-entry/SKILL.md
.codex/skills/archestra-pr-hygiene/SKILL.md
ai-labs/skills/access-request-intake/SKILL.md

Metadata

Files
0
Version
9308d78
Hash
b606f0ff
Indexed
2026-09-11 16:19

inicio - Wiki
Copyright © 2011-2026 iteam. Current version is 2.156.0. UTC+08:00, 2026-11-27 05:56
浙ICP备14020137号-1