Repository navigation
fix(sql): Optimize SpendLogs queries to use timestamp filtering for index usage - #17504
Merged
2 commits merged intoDec 5, 2025
Merged
2 commits merged into
2 commits merged into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
ryan-crabbe-berri
added a commit
that referenced
this pull request
Apr 11, 2026
… queries The raw SQL queries in spend_management_endpoints.py, spend_tracking_utils.py, and analytics_endpoints.py cast date params to ::timestamptz while comparing against the plain-timestamp "startTime" column. Postgres resolves the type mismatch by promoting the column using the DB session timezone, which drifts the filter window and date_trunc buckets whenever session TZ is not UTC — silently dropping rows at UTC day boundaries and, for narrow windows, losing rows entirely. Wraps every such comparison with `AT TIME ZONE 'UTC'` so the param side resolves to a plain timestamp matching the column type. Both sides end up as plain timestamp, Postgres does no implicit conversion, and session TZ plays no role in the query. The fix is constant-foldable so the existing startTime index (PR #17504) remains usable. Also marks strptime-produced datetimes as tz-aware UTC at the call sites for intent clarity and consistency with parse_date. Fixes #22529 (Logs page missing recent rows under non-UTC session TZ) and the Global Usage single-day-returns-two-days symptom reported internally.
fzowl
pushed a commit
to fzowl/litellm
that referenced
this pull request
Jun 24, 2026
…ndex usage (BerriAI#17504) * fix: optimize SpendLogs queries to use timestamp filtering (BerriAI#17487) * use timestamptz & enhance test
fzowl
pushed a commit
to fzowl/litellm
that referenced
this pull request
Jun 24, 2026
… queries The raw SQL queries in spend_management_endpoints.py, spend_tracking_utils.py, and analytics_endpoints.py cast date params to ::timestamptz while comparing against the plain-timestamp "startTime" column. Postgres resolves the type mismatch by promoting the column using the DB session timezone, which drifts the filter window and date_trunc buckets whenever session TZ is not UTC — silently dropping rows at UTC day boundaries and, for narrow windows, losing rows entirely. Wraps every such comparison with `AT TIME ZONE 'UTC'` so the param side resolves to a plain timestamp matching the column type. Both sides end up as plain timestamp, Postgres does no implicit conversion, and session TZ plays no role in the query. The fix is constant-foldable so the existing startTime index (PR BerriAI#17504) remains usable. Also marks strptime-produced datetimes as tz-aware UTC at the call sites for intent clarity and consistency with parse_date. Fixes BerriAI#22529 (Logs page missing recent rows under non-UTC session TZ) and the Global Usage single-day-returns-two-days symptom reported internally.
This pull request was closed.
This branch was successfully deployed
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.
Title
fix: Optimize SpendLogs queries to use timestamp filtering for index usage
Relevant issues
Fixes #17487
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/directory, Addingat least 1 test is a hard requirement - see
details
make test-unitType
🐛 Bug Fix
🚄 Infrastructure
Changes
Problem
SQL queries using
BETWEEN $1::date AND $2::datecaused PostgreSQL to implicitly cast thestartTimetimestamp column to DATE, making queries non-sargable and preventing index usage.This resulted in:
Solution
Changed all date filtering queries from:
To index-friendly timestamp filtering:
This allows PostgreSQL to use indexes on the startTime column efficiently.
Performance Impact
Testing