Skip to content

fix(cli): fall back to per-object drops when db reset runs out of locks - #7907

Open
Coly010 wants to merge 3 commits into
developfrom
columferry/cli-3327-db-reset-remote-fails-with-out-of-shared
Open

Coly010 wants to merge 3 commits into
developfrom
columferry/cli-3327-db-reset-remote-fails-with-out-of-shared

Conversation

@Coly010

@Coly010 Coly010 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

supabase db reset --remote drops every user object in one transaction, so every dropped table, index, sequence, type and function holds its lock until the commit. On a database with a few thousand objects that runs past max_locks_per_transaction and fails with out of shared memory (SQLSTATE 53200) before any migration runs. Changing that setting needs a restart, so there was no way around it from the CLI.

What changes:

  • The drop still runs in one transaction first, so for most databases a failed drop leaves everything as it was, same as today.
  • Only if that transaction runs out of lock slots, and the rollback succeeds, does it retry with a commit after each step. It matches on 53200 plus the Postgres hint naming max_locks_per_transaction, since 53200 on its own also covers plain out of memory and the message text is translated with the server locale (setting names aren't). DbExecError now carries the hint. It prints a line on stderr when it falls back.
  • The per-object path truncates supabase_migrations first, so a partial reset never shows migrations as applied. The single-transaction path keeps the original order.
  • Any failure in the per-object path says the database may be partly reset and how to finish.
  • If a single schema or table is still too big to drop in one go, the error suggests checking the current max_locks_per_transaction and raising it. For a linked project, or a Supabase connection string the ref can be read from, it points at supabase postgres-config update --experimental or [db.settings] plus supabase config push. A local database gets the [db.settings] and restart route, and any other Postgres is pointed at postgresql.conf.

migration down uses the same drop SQL, so it now goes through the same helper and gets the same fallback and hints. Its partial-state hint points at supabase db reset --version <version> rather than re-running migration down, because --last can't work once the history has been cleared.

Testing

  • Unit tests for the helper cover the atomic path, the fallback, a 53200 that isn't lock exhaustion, the partial-state suggestion, and the hint for linked, db-url, local and non-Supabase targets. Integration tests cover both db reset and migration down.
  • There's no real-Postgres harness in the repo, so I checked the low-lock case by hand against a local postgres:17 with max_locks_per_transaction=10 and max_connections=20, running the CLI from source:
    • a small database takes the single-transaction path
    • 8 schemas of 60 tables each fail in one transaction, then the fallback finishes and the migration applies
    • a permission error in the single-transaction path leaves the database untouched
    • one 400-table schema still fails, with the partial-state note and the postgresql.conf hint
  • I haven't run reset.live.test.ts.

Follow-ups (not in this PR)

  • The public loops don't skip objects that belong to an extension (there's no pg_depend deptype = 'e' anti-join like the schema loop has), so something like uuid-ossp installed in public fails with 2BP01 on uuid_nil(). That still fails with this PR, but now nothing gets dropped.
  • The sequences loop filters on relkind = 's', but sequences are 'S', so standalone sequences in public are never dropped.
  • The confirmation prompt could mention that a reset which stops partway leaves the database partly reset.
  • It might be worth surfacing the raise notice output, or at least the object that failed, when the drop errors.

Linked issue

Fixes #7898

  • The linked issue is open and carries the open-for-contribution label (or I'm a Supabase maintainer).

Checklist

  • The PR title follows Conventional Commits (e.g. fix(cli): …).
  • Tests added or updated for the change.
  • From the repository root, pnpm check:all passes; relevant package tests pass for every touched workspace, and pnpm types:check passes for each touched TypeScript workspace (or workspace declaring it).

`db reset --remote` dropped every user object in one transaction, so
every dropped relation, index, sequence, type and function kept its lock
until COMMIT. On databases with a few thousand objects that went past
max_locks_per_transaction and failed with `out of shared memory`
(SQLSTATE 53200) before any migration ran.

The drop still runs in one transaction first, so a failure changes
nothing. Only when that transaction runs out of lock slots (SQLSTATE
53200 with "out of shared memory") is it rolled back and retried with a
commit after every step. The per-object path truncates the migration
history first, so a partial reset never reports migrations as applied,
and every failure in it says the database is partly reset and how to
finish. When a single object still exhausts the lock table, the hint
names the project ref, derived from a Supabase db-url when needed, and
offers `postgres-config update` or `[db.settings]` with `config push`.
Local databases and non-Supabase URLs get a matching hint without a ref.

`db reset` and `migration down` now share one drop helper, so both get
the same fallback and hints.

Fixes #7898
@Coly010
Coly010 requested a review from a team as a code owner December 1, 2026 13:01
@Coly010
Coly010 requested a review from Prashansa-K December 1, 2026 13:01
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

CLI benchmark (Lightweight Commands): no changes found

Commit 7a653b6 against develop at d6d2b21 · 10 runs each · report only, never blocks merging

Command Workload Before After Change
supabase --version version 182 ms 190 ms no real change
supabase --help help 175 ms 184 ms no real change (+5%, within normal variation)
supabase init init 235 ms 205 ms no real change (-13%, within normal variation)
supabase migration new bench migration-new 315 ms 365 ms no real change (+16%, within normal variation)
supabase config convert config-convert 286 ms 243 ms no real change (-15%, within normal variation)
supabase config validate config-validate 218 ms 218 ms no real change
supabase gen types --lang typescript --project-id replayfixtureprojref gen-types 218 ms 217 ms no real change
supabase projects list projects-list 184 ms 185 ms no real change
supabase functions list --project-ref replayfixtureprojref functions-list 200 ms 201 ms no real change

API calls: unchanged for all 9 commands.

Binary size: 135.7 MB, up 4.0 KB from 135.7 MB.

Details: timing ranges, API calls per command, slowest phases

Timing ranges

Command Workload Before range After range Change Verdict
supabase --version version 161-205 ms 163-203 ms +7 ms (+4%) within noise
supabase --help help 161-255 ms 160-304 ms +9 ms (+5%) within noise
supabase init init 183-322 ms 191-394 ms -30 ms (-13%) within noise
supabase migration new bench migration-new 237-495 ms 261-490 ms +49 ms (+16%) within noise
supabase config convert config-convert 204-362 ms 208-307 ms -43 ms (-15%) within noise
supabase config validate config-validate 199-277 ms 201-260 ms 0 ms (0%) within noise
supabase gen types --lang typescript --project-id replayfixtureprojref gen-types 205-235 ms 207-234 ms -1 ms (-1%) within noise
supabase projects list projects-list 179-222 ms 178-203 ms +1 ms (+0%) within noise
supabase functions list --project-ref replayfixtureprojref functions-list 187-214 ms 182-228 ms +0 ms (+0%) within noise

API calls per command

Command Workload Before After Endpoint changes
supabase --version version 0 0
supabase --help help 0 0
supabase init init 0 0
supabase migration new bench migration-new 0 0
supabase config convert config-convert 0 0
supabase config validate config-validate 0 0
supabase gen types --lang typescript --project-id replayfixtureprojref gen-types 2 2
supabase projects list projects-list 1 1
supabase functions list --project-ref replayfixtureprojref functions-list 2 2

Slowest phases

No command moved beyond noise, so there are no phase deltas.

Workflow run

Comment /bench to run the lightweight benchmark (this comment) again on the latest commit, or /bench full to also run the integration-heavy benchmark, which posts in its own comment.

@github-actions github-actions Bot 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.

Superseded by a newer AI review

🤖 AI Review

CLI reference prose guidance: snapshot supabase/supabase@b6c391a supplied to reviewers (advisory, not a style pass).

Confirmed all eight Codex findings. Claude reported none. Two findings are major: localized or ambiguous Postgres errors can lead the CLI to choose the wrong reset strategy, and truncating migration history before dropping user schemas can make previously successful atomic resets fail. The fallback can also start without a confirmed rollback and report partial changes before anything commits. I checked the added reference prose against the supplied style snapshot. Verification was static.

Findings

Severity Location Category Sources Claim
🟠 MAJOR apps/cli/src/command-internal/drop-objects.ts:192 error-handling codex The CLI uses an English error message to decide whether to switch to a reset that commits partial progress.
🟠 MAJOR apps/cli/src/command-internal/drop-objects.ts:40 database-correctness codex Truncating migration history before dropping user schemas can make previously successful atomic resets fail.
🟡 MINOR apps/cli/src/command-internal/drop-objects.ts:254 transaction-handling codex The fallback can run on the same session without confirming that the atomic transaction ended.
🟡 MINOR apps/cli/src/command-internal/drop-objects.ts:264 error-handling codex The fallback reports that the database changed and migration history was cleared even when its first SQL operation fails.
🟡 MINOR apps/cli/src/command-internal/drop-objects.ts:207 recovery-guidance codex The local and hosted recovery instructions can leave the lock setting unchanged or lower it on an already tuned database.
🟡 MINOR apps/cli/docs/supabase/db/reset.md:15 docs-drift codex The overlay says the fallback drops database objects one at a time, but a single fallback transaction can drop a schema and its dependent objects together.
🟡 MINOR apps/cli/src/command-internal/drop-objects.unit.test.ts:209 test-coverage codex The new SQL test can pass even if the fallback never clears migration history or commits any drops.
⚪ NIT apps/cli/src/command-internal/drop-objects.ts:258 tracing codex The successful drop span does not identify which reset strategy completed.

Stats

Claude findings: 0 · Codex findings: 8 · Confirmed: 8 · Refuted: 0 · Uncertain: 0


Models: claude-opus-5-5 + gpt-6.2-sol · Trigger: auto · Workflow run

This review runs once per PR. A maintainer can request another with a /ai-review comment.

Comment thread apps/cli/docs/supabase/db/reset.md Resolved
- Detect lock exhaustion from the Postgres hint naming
  max_locks_per_transaction, which stays untranslated in every locale,
  instead of the English message. DbExecError now carries the hint.
- Keep the original truncation order in the single-transaction drop;
  only the per-object drop clears migration history first.
- Fall back only after the drop statement itself fails and the rollback
  succeeds; a failed rollback reports both errors.
- Say the database may be partly reset, since the first per-object step
  can fail before anything commits.
- Ask users to check the current max_locks_per_transaction and raise it,
  with 1024 as an example.
- Annotate the drop span with the strategy used.
@Coly010 Coly010 self-assigned this Dec 1, 2026
@Coly010

Coly010 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

/ai-review

@github-actions github-actions Bot 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.

🤖 AI Review

CLI reference prose guidance: snapshot supabase/supabase@b53403e supplied to reviewers (advisory, not a style pass).

Confirmed all five Codex findings. The fallback can exhaust the lock table before dropping any schemas, and interrupting migration down --last can leave the user without the version-pinned recovery command. The reset reference overstates when a retry occurs, and the new tests leave PostgreSQL transaction behavior unverified. The prose finding is advisory. Claude reported zero findings. The supplied next context shows no fundamental conflict. Verification used source inspection.

Findings

Severity Location Category Sources Claim
🟠 MAJOR apps/cli/src/command-internal/drop-objects.ts:37 correctness codex The per-object fallback can exhaust the lock table before reaching the schema drops.
🟡 MINOR apps/cli/src/command-internal/drop-objects.ts:284 error-handling codex Interrupting migration down --last during the fallback can clear the history used to select the rollback version before the CLI prints the recovery command.
🟡 MINOR apps/cli/docs/supabase/db/reset.md:15 docs-drift codex The remote reset reference promises a retry for transaction lock exhaustion. A lock-exhaustion error during COMMIT returns immediately.
🟡 MINOR apps/cli/src/command-internal/drop-objects.unit.test.ts:246 test-coverage codex The new tests leave the fallback's per-operation commits unverified in PostgreSQL.
⚪ NIT apps/cli/docs/supabase/db/reset.md:15 docs-style codex The fallback sentence combines the retry condition, commit behavior, and scope of schema deletion.

Stats

Claude findings: 0 · Codex findings: 5 · Confirmed: 5 · Refuted: 0 · Uncertain: 0


Models: claude-opus-5-5 + gpt-6.2-sol · Trigger: manual · Workflow run

This review runs once per PR. A maintainer can request another with a /ai-review comment.

Comment on lines +37 to +46
const migrationsFirst = commitEach
? `
for rec in
select *
from pg_class c
where c.relnamespace::regnamespace::name = 'supabase_migrations' and c.relkind = 'r'
loop
raise notice 'truncating table: %.%', rec.relnamespace::regnamespace::name, rec.relname;
execute format('truncate %I.%I cascade', rec.relnamespace::regnamespace::name, rec.relname);${commit}
end loop;

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.

🟠 MAJOR · correctness · source: codex

The per-object fallback can exhaust the lock table before reaching the schema drops.

Evidence: apps/cli/src/command-internal/drop-objects.ts:37–55 runs TRUNCATE ... CASCADE on each supabase_migrations table before entering the schema loop. PostgreSQL locks foreign-key referencing tables in the same truncate transaction. References across several schemas can exhaust the lock table before the separate schema drops at line 68 run.

Suggested fix: Defer cascading truncation until after the user schema and table drops. Track an incomplete reset separately if migration history must be invalidated before the first drop.

Comment thread apps/cli/docs/supabase/db/reset.md Resolved
Comment on lines +246 to +259
describe("drop SQL", () => {
it("commits after every step only in the per-object block", () => {
expect(count(dropObjectsSql, "commit;")).toBe(0);
expect(count(dropObjectsEachSql, "commit;")).toBe(count(dropObjectsEachSql, "execute format("));
});

it("clears the migration history first only in the per-object block", () => {
const history = "'supabase_migrations'";
expect(dropObjectsEachSql.indexOf(history)).toBeLessThan(
dropObjectsEachSql.indexOf("drop schema if exists"),
);
expect(dropObjectsSql.indexOf(history)).toBeGreaterThan(
dropObjectsSql.indexOf("drop table if exists"),
);

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.

🟡 MINOR · test-coverage · source: codex

The new tests leave the fallback's per-operation commits unverified in PostgreSQL.

Evidence: apps/cli/src/command-internal/drop-objects.unit.test.ts:247–259 counts SQL tokens and checks substring positions. The fake session at lines 40–49 and the reset integration stub at apps/cli/src/commands/db/reset/reset.integration.test.ts:261–282 return outcomes from SQL-string matches. The fallback test at lines 3085–3104 checks the statements sent and migration progress output.

Suggested fix: Add a PostgreSQL-backed test that forces a later fallback operation to fail and verifies that an earlier drop and the migration-history truncation remain committed.

Comment thread apps/cli/docs/supabase/db/reset.md Resolved
- Treat a lock-exhaustion failure on COMMIT like one from the drop
  statement: roll back and retry in separate transactions.
- Print how to finish in the fallback warning, before anything can
  commit, so an interrupted migration down still shows the pinned
  db reset --version command.
- Split the reset.md fallback sentence.

This branch has not been deployed

No deployments
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.

db reset --remote fails with "out of shared memory" because every object is dropped in one transaction

1 participant