Skip to content

fix(db): partition runbook moves the call-id index off the legacy spend logs table - #46131

Merged
klu-berri merged 1 commit into
mainfrom
litellm_partition_runbook_call_id_idx
Oct 13, 2026
Merged

klu-berri merged 1 commit into
mainfrom
litellm_partition_runbook_call_id_idx

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • The partition runbook leaves the call-id index name on the legacy table
  • The migration job builds that index, so partitioning after it keeps the old name
  • Every later migration job then fails with "Index LiteLLM_SpendLogs_litellm_call_id_idx already exists on LiteLLM_SpendLogs_legacy rather than LiteLLM_SpendLogs"

How it solves it:

  • db_scripts/partition_spend_logs.sql renames the call-id index aside with the six others
  • A regression test runs the index job, partitions with the runbook, and runs the job again

User Flow

Before: an operator who partitions spend logs after upgrading cannot get the next upgrade's migration job through

  1. They run a version whose migration job has already built the call-id index on LiteLLM_SpendLogs
  2. They follow the partitioning runbook, psql -f db_scripts/partition_spend_logs.sql, and the proxy keeps serving
  3. On the next upgrade the migration job exits non-zero with "Index LiteLLM_SpendLogs_litellm_call_id_idx already exists on LiteLLM_SpendLogs_legacy rather than LiteLLM_SpendLogs" and the rollout stalls
  4. They have to rename the index by hand on the legacy table before the job passes

After: the same upgrade goes through without touching the database by hand

  1. They run a version whose migration job has already built the call-id index on LiteLLM_SpendLogs
  2. They follow the partitioning runbook, psql -f db_scripts/partition_spend_logs.sql, and the proxy keeps serving
  3. On the next upgrade the migration job builds the call-id index on every partition and exits 0, and the rollout completes

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. 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 more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

The symptom was first seen in the Buildkite perf lane (https://github.com/BerriAI/project-releaser/pull/310), which restores a Postgres volume where the index job ran before the runbook, and boots the released ghcr.io/berriai/litellm-migrations:v1.105.0 job on it: Job e2e-bk-412-rbo-litellm-migrations failed with the collision error below, and builds 413 to 415 passed once the rename was applied by hand

The run below repeats that operator flow on each commit with a fresh Postgres 18 database per leg. python3 migrations/run.py is the litellm-migrations image's entrypoint, the runbook is applied with psql -v ON_ERROR_STOP=1, and the proxy then boots with 2 uvicorn workers and DISABLE_SCHEMA_UPDATE=true (schema changes left to the Job, as in a Kubernetes rollout) and serves a real gpt-5.4-mini call

Before (cca06b2)

  1. The first upgrade's migration Job builds the call-id index on the plain table
$ python3 migrations/run.py
exit code 0
litellm_proxy_extras - INFO - Building index LiteLLM_SpendLogs_api_key_startTime_idx on LiteLLM_SpendLogs concurrently
litellm_proxy_extras - INFO - Building index LiteLLM_SpendLogs_litellm_call_id_idx on LiteLLM_SpendLogs concurrently
litellm_proxy_extras - INFO - Request-log indexes are all in place
litellm_proxy_extras - INFO - Migration job completed successfully.
  1. The operator runs the partitioning runbook, which renames six indexes aside
$ psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f db_scripts/partition_spend_logs.sql
BEGIN
ALTER TABLE
ALTER INDEX
ALTER INDEX
ALTER INDEX
ALTER INDEX
ALTER INDEX
ALTER INDEX
CREATE TABLE
ALTER TABLE
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE TABLE
COMMIT
exit code 0
  1. The next upgrade's migration Job fails, so the rollout stalls
$ python3 migrations/run.py
exit code 1
litellm_proxy_extras - WARNING - Index LiteLLM_SpendLogs_litellm_call_id_idx already exists on LiteLLM_SpendLogs_legacy rather than LiteLLM_SpendLogs, leaving it alone
litellm_proxy_extras - ERROR - Migration job failed after retries.
  1. The proxy itself still serves traffic and records spend
$ curl -s http://127.0.0.1:33422/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"gpt-5.4-mini","messages":[{"role":"user","content":"Reply with the word ok"}],"max_completion_tokens":20}'
{"id": "chatcmpl-EYNznmfbrN20TF87AQntsyIw3HRIg", "model": "gpt-5.4-mini", "content": "ok", "usage": 15, "error": null}

$ curl -s "http://127.0.0.1:33422/spend/logs?request_id=chatcmpl-EYNznmfbrN20TF87AQntsyIw3HRIg" -H "Authorization: Bearer $LITELLM_MASTER_KEY"
[{"request_id": "chatcmpl-EYNznmfbrN20TF87AQntsyIw3HRIg", "model": "openai/gpt-5.4-mini", "spend": 2.625e-05, "total_tokens": 15, "startTime": "2026-10-13T04:01:37.906000Z"}]

After (5937ad5)

  1. The first upgrade's migration Job builds the call-id index on the plain table
$ python3 migrations/run.py
exit code 0
litellm_proxy_extras - INFO - Building index LiteLLM_SpendLogs_api_key_startTime_idx on LiteLLM_SpendLogs concurrently
litellm_proxy_extras - INFO - Building index LiteLLM_SpendLogs_litellm_call_id_idx on LiteLLM_SpendLogs concurrently
litellm_proxy_extras - INFO - Request-log indexes are all in place
litellm_proxy_extras - INFO - Migration job completed successfully.
  1. The operator runs the partitioning runbook, which now renames seven indexes aside
$ psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f db_scripts/partition_spend_logs.sql
BEGIN
ALTER TABLE
ALTER INDEX
ALTER INDEX
ALTER INDEX
ALTER INDEX
ALTER INDEX
ALTER INDEX
ALTER INDEX
CREATE TABLE
ALTER TABLE
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE TABLE
COMMIT
exit code 0
  1. The next upgrade's migration Job builds the call-id index on the partition and passes, so the rollout completes
$ python3 migrations/run.py
exit code 0
litellm_proxy_extras - INFO - Building index LiteLLM_SpendLogs_pdefault_litellm_call_id_idx on LiteLLM_SpendLogs_pdefault concurrently
litellm_proxy_extras - INFO - Attached index LiteLLM_SpendLogs_pdefault_litellm_call_id_idx on partition LiteLLM_SpendLogs_pdefault to LiteLLM_SpendLogs_litellm_call_id_idx
litellm_proxy_extras - INFO - Request-log indexes are all in place
litellm_proxy_extras - INFO - Migration job completed successfully.
  1. The proxy serves traffic and records spend through the partitioned table
$ curl -s http://127.0.0.1:45700/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"gpt-5.4-mini","messages":[{"role":"user","content":"Reply with the word ok"}],"max_completion_tokens":20}'
{"id": "chatcmpl-EYNzcCHwFxTrcBgfmVfuRny3bh9Sk", "model": "gpt-5.4-mini", "content": "ok", "usage": 15, "error": null}

$ curl -s "http://127.0.0.1:45700/spend/logs?request_id=chatcmpl-EYNzcCHwFxTrcBgfmVfuRny3bh9Sk" -H "Authorization: Bearer $LITELLM_MASTER_KEY"
[{"request_id": "chatcmpl-EYNzcCHwFxTrcBgfmVfuRny3bh9Sk", "model": "openai/gpt-5.4-mini", "spend": 2.625e-05, "total_tokens": 15, "startTime": "2026-10-13T04:01:26.746000Z"}]

The regression test, test_partitioning_a_table_the_index_job_already_covered_moves_its_call_id_index_aside, fails on main with assert None == 'LiteLLM_SpendLogs_legacy' and passes at 5937ad5 against Postgres 18. A rollback with db_scripts/unpartition_spend_logs.sql after the index job ran was also checked, and needs no matching rename, since it drops the partitioned table and its index in the same transaction and the next Job rebuilds the index on the plain table

Observed during the run, none caused or made worse by this PR:

  • Before: only a WARNING names the Job's failure cause
  • Before: the proxy kept serving while the Job failed
  • Both legs: boot logs a drift ERROR suggesting DROP TABLE on legacy

Type

🐛 Bug Fix

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Link to Devin session: https://app.devin.ai/sessions/a12e287eab9e442d858a8ee418ba2897
Open in Devin Desktop: https://app.devin.ai/desktop/session/a12e287eab9e442d858a8ee418ba2897?variant=devin
Requested by: @kerry-berri


Note

Medium Risk
Touches the manual spend-logs partitioning runbook and upgrade ordering; wrong behavior blocks migration jobs, but the change mirrors existing index renames and is covered by an integration test.

Overview
Fixes a partition runbook gap where LiteLLM_SpendLogs_litellm_call_id_idx stayed on LiteLLM_SpendLogs_legacy after partitioning if the migration index job had already created it on the plain table. Later ensure_request_log_indexes / migration job runs then failed because the canonical index name was on the wrong table.

partition_spend_logs.sql now renames that index to LiteLLM_SpendLogs_legacy_litellm_call_id_idx, matching the six Prisma indexes already moved aside before recreating indexes on the partitioned parent.

A new integration test runs the index job, applies the partition script, asserts the legacy rename, and confirms a second index job succeeds with call-id coverage on every partition.

Reviewed by Cursor Bugbot for commit 5937ad5. Bugbot is set up for automated code reviews on this repo. Configure here.

…nd logs table

db_scripts/partition_spend_logs.sql renames the Prisma indexes aside before creating the partitioned table but not LiteLLM_SpendLogs_litellm_call_id_idx, which the request-log index job builds. A deployment that partitions after that job ran keeps the name on LiteLLM_SpendLogs_legacy, so every later index build reports the index as being on another table and the migration job fails

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration
devin-ai-integration Bot requested a review from a team October 13, 2026 01:35
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor

@litereview code-review

@litereview

litereview Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

✅ code-review

Scope: full · Full PR review.

READY · Test evidence: VERIFIED · 5/5

No correctness findings.

Checks and execution details
  • Passed · Real testing evidence (required): Not applicable: no LLM translation changes in the scoped paths. The PR changes only db_scripts/partition_spend_logs.sql and tests/integration/database/migrations/test_request_log_indexes.py. Neither file is under litellm/llms/** or litellm/litellm_core_utils/prompt_templates/**.
  • Passed · Request-log index integration suite on a real Postgres (optional): I installed Postgres 15 locally, ran uv sync, and then ran DATABASE_URL=postgresql://postgres:pw@​localhost:5432/postgres uv run pytest tests/integration/database/migrations/test_request_log_indexes.py. Result: 37 passed in 352.29s. The PR's own proof used Postgres 18.
  • Passed · New regression test fails without the fix (optional): I removed the added ALTER INDEX ... litellm_call_id_idx rename from db_scripts/partition_spend_logs.sql and reran -k moves_its_call_id. Result: 1 failed. I then restored the file. The test catches the regression.
  • Passed · Rollback script consistency (optional): Code inspection only. db_scripts/unpartition_spend_logs.sql does not rename LiteLLM_SpendLogs_litellm_call_id_idx. It still works because it runs DROP TABLE "LiteLLM_SpendLogs_partitioned" in the same transaction, which drops that index. The next index job then rebuilds it on the plain table. The PR description makes the same claim, and this matches it.

View review · Reviewed 5937ad570d52

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor 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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 5937ad5. Configure here.

@klu-berri
klu-berri merged commit 9f511e1 into main Oct 13, 2026
142 of 144 checks passed
@klu-berri
klu-berri deleted the litellm_partition_runbook_call_id_idx branch October 13, 2026 15:17
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.

3 participants