feat: monthly global spend litellm database materialized view migration#154
feat: monthly global spend litellm database materialized view migration#154noahpodgurski wants to merge 2 commits into
Conversation
|
[Critical] Don't DROP/CREATE the wrapper view on every refresh The CronJob in dataservices-infra#1968 invokes this script with
Both go away if the ACTION="${1:-migrate}"
if [[ "${ACTION}" == "refresh" ]]; then
if ! psql -d "${LiteLLM_DB_NAME}" -tAc \
"SELECT to_regclass('public.monthly_global_spend_cache') IS NOT NULL" \
| grep -qx t; then
echo "[mlpa-litellm-migrate] cache missing; run migrate first. Skipping."
exit 0
fi
psql -v ON_ERROR_STOP=1 -d "${LiteLLM_DB_NAME}" -c \
'REFRESH MATERIALIZED VIEW CONCURRENTLY public.monthly_global_spend_cache;'
exit 0
fi
# existing schema-setup block runs only on the migrate pathThe DROP/CREATE then runs once at deploy time instead of 8,640 times a month, and the dependency risk is limited to the controlled migrate window. |
|
[Medium] Postgres has no Suggest a comment in the script so the next person knows what's needed: -- NOTE: Postgres has no CREATE OR REPLACE MATERIALIZED VIEW. To change the
-- SELECT below, add a one-shot migration step that runs
-- DROP MATERIALIZED VIEW IF EXISTS public.monthly_global_spend_cache;
-- before this block, otherwise the new definition will be ignored.
CREATE MATERIALIZED VIEW IF NOT EXISTS public.monthly_global_spend_cache AS
...Pure documentation — no behavior change today, just saves a future debugging session. |
|
[Worth flagging — likely not an issue] Wrapper view column/row shape change The new wrapper view exposes a single
Probably fine if LiteLLM only uses the # from the LiteLLM source (and any internal callers)
git grep -i 'MonthlyGlobalSpend'If everything is summing, no action needed. If anything is selecting the daily rows, the MV would need to keep |
What's new
scripts/migrate-litellm-database.shto createmonthly_global_spend_cacheand recreateMonthlyGlobalSpendas a thin wrapper over the cache.start.shscript which makes starting LiteLLM, running migrations, init scripts all automatichttps://mozilla-hub.atlassian.net/browse/AIPLAT-696
https://github.com/mozilla/dataservices-infra/pull/1968
Results:
Summary (for 1GB of data in table):
Before migration, execution time:
104.687 msPost migration, execution time:
0.035 ms