Repository navigation
feat(proxy): refuse migrations that existing rows would make fail, before the first one runs - #48493
Merged
Merged
Conversation
…fore the first one runs Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…ase without the api_key index Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Contributor
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
Merging this PR will not alter performance1 warning
Comparing |
…he type gate holds Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…e may be absent Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…ort variables Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…s unknown Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
… flag for what it does The index check now lays out each row's entry the way index_form_tuple does (tuple header, null bitmap, every indexed column at its alignment, rounded to 8) and compares it with BTMaxItemSize for the server's block_size, so a key that passes octet_length > 2704 but still fails the (api_key, startTime) index is caught. --db_preflight becomes --check_migrations_against_existing_data, db_preflight.py becomes migration_data_checks.py and the timeout env var LITELLM_MIGRATION_DATA_CHECK_TIMEOUT Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
yuneng-berri
approved these changes
Oct 27, 2026
This was referenced Oct 27, 2026
Merged
Merged
3 of 6 tasks
Closed
3 of 6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Before any migration runs, the proxy now reads the rows a pending schema change depends on and refuses with a named report when they would make it fail, instead of applying the migrations and dying halfway with a Postgres error. The checks are derived from the statement text (an index over a text column, UNIQUE, SET NOT NULL, a NOT NULL column with no default, FOREIGN KEY, CHECK), so a future migration of one of those kinds gets its check without anyone writing one.
litellm --check_migrations_against_existing_dataruns the same checks against a database and exits without applying anything, so an operator can check production a week before the upgradeDetails
TLDR
Problem this solves:
(api_key, startTime)indexHow it solves it:
migration_data_checks.pyturns each pending statement into one read-only count query from its kind and the catalog column typesindex_form_tuplelays it out (tuple header, null bitmap, every indexed column at its alignment, rounded to 8) againstBTMaxItemSizefor the server'sblock_size, so the line falls where Postgres draws it (2684 bytes ofapi_keynext tostartTime), not atoctet_length > 2704setup_databaseruns it beforeprisma migrate deploy; a hit raises the existing "Database migration cannot proceed" exit 2--check_migrations_against_existing_dataruns the checks alone, exit 0 or 2, writes nothingLITELLM_MIGRATION_DATA_CHECK_TIMEOUT(600 s) is reported as unverified, never passedUser Flow
Before: an operator upgrading a 1.101 deployment with old spend-log rows gets a failed migration job and no useful message
--skip_server_startup(or the Helm migration job) against their databaseindex row size 2976 exceeds btree version 4 maximum 2704 for index "LiteLLM_SpendLogs_api_key_startTime_idx"Database setup failed after multiple retries ... check your database connectionand exits 1, every rerun fails the same wayAfter: the same job stops before the first migration and tells them which rows to fix
"LiteLLM_SpendLogs".api_key, startTime: 2 row(s) with an index entry over 2704 bytes, largest 3232 bytes (api_key up to 3207 bytes), the index it blocks, and the hashing UPDATE that clears it, then exits 2 with the schema untouched--check_migrations_against_existing_dataagainst production first and get the same report or an all-clear, nothing appliedLinear ticket
Resolves LIT-9634
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreScreenshots / Proof of Fix
Rig: a database built by the
ghcr.io/berriai/litellm-database:v1.101.0image (165 migrations), then two spend-log rows inserted the way a pre-v1.99.0 proxy wrote them ('Bearer ' || random hex, 3207 and 2951 bytes), then the migration job from the commit under test run against it withpython litellm/proxy/proxy_cli.py --skip_server_startupBefore (2e27a02)
Migration job on a database with two legacy rows
DATABASE_URL=... python litellm/proxy/proxy_cli.py --skip_server_startupAll migrations have been successfully applied.thenWARNING Could not build the request-log indexes, leaving them for the next index build: index row size 2976 exceeds btree version 4 maximum 2704 for index "LiteLLM_SpendLogs_api_key_startTime_idx"Database setup failed after multiple retries. The proxy cannot start safely. Please check your database connection and migration status.exit 1_prisma_migrationsfinished rows: 165 -> 222.pg_indexforLiteLLM_SpendLogs_api_key_startTime_idx:indisvalid = f--check_migrations_against_existing_datapython litellm/proxy/proxy_cli.py --check_migrations_against_existing_dataError: No such option: --check_migrations_against_existing_dataexit 2After (f8d5ccd)
Migration job on a database with two legacy rows
_prisma_migrationsfinished rows: still 165.pg_indexesrows forLiteLLM_SpendLogs_api_key_startTime_idx: 0UPDATE 2. Same job again:Building index LiteLLM_SpendLogs_api_key_startTime_idx on LiteLLM_SpendLogs concurrently...Request-log indexes are all in place...Setup complete, exit 0. Finished rows 222,indisvalid = t--check_migrations_against_existing_datapython litellm/proxy/proxy_cli.py --check_migrations_against_existing_dataprints the same report, exit 2, ledger and indexes untouchedLiteLLM Proxy: the pending migrations and indexes can be applied to the rows this database has.exit 0Where the line is (Postgres 14, 8 KB page,
psql)create table bt (b text, "startTime" timestamp(3)); insert into bt select <2684 random bytes>, now(); create index bt1 on bt (b, "startTime");->CREATE INDEXERROR: index row size 2712 exceeds btree version 4 maximum 2704 for index "bt1"1 row(s) with an index entry over 2704 bytes, largest 2712 bytes (b up to 2685 bytes).octet_length > 2704would have passed both. Same for a 20-byte text column before the key (line at 2668) and a NULL before it (2684), intest_the_entry_size_check_draws_the_line_exactly_where_postgres_doesType
🆕 New Feature
Caveats (if any)
Medium
ALTER COLUMN ... TYPEgets no check: counting rows that fail a cast needspg_input_is_valid, which is Postgres 16+WHERE, so a row the predicate excludes can still refuse the migrationLow
LITELLM_MIGRATION_DATA_CHECK_TIMEOUT(default 600 s) and refuses as unverified_prisma_migrationstable treats every migration as pending; every check then skips because its table does not exist yet, which costs one catalog query per derived checkprecheck.sqlfor every new risky statement is a follow-up, not in this PRLink to Devin session: https://app.devin.ai/sessions/14b2bd229edf4b11a3220d90993f6708
Open in Devin Desktop: https://app.devin.ai/desktop/session/14b2bd229edf4b11a3220d90993f6708?variant=devin
Requested by: @kerry-berri