[Refactor/Chore] Workflow Logs tab reads every log row of the app per page view: workflow_app_logs has no index that includes created_at
Self Checks
- [x] I have read the Contributing Guide and Language Policy.
- [x] This is only for refactors or chores; if you would like to ask a question, please head to Discussions.
- [x] I have searched for existing issues search for existing issues, including closed ones.
- [x] I confirm that I am using English to submit this report, otherwise it will be closed.
- [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
- [x] Please do not modify this template :) and fill in all the required fields.
Description
Opening the Logs tab of a workflow app (GET /console/api/apps/{app_id}/workflow-app-logs) reads every log row of that app to return the newest page.
WorkflowAppLogQueryRepository.get_paginated filters on tenant_id and app_id and orders by workflow_app_logs.created_at DESC with LIMIT/OFFSET. The only index that helps is workflow_app_log_app_idx (tenant_id, app_id), which has no created_at, so PostgreSQL fetches all of the app's rows, sorts them, and keeps ten. With 500,000 logs the plan is a bitmap heap scan of 500,801 rows followed by an external merge sort on disk (three 13 MB temp files).
The conversations table, which backs the Logs tab of chat apps, already has (app_id, created_at DESC) and (app_id, updated_at DESC); the workflow log table was left without the equivalent.
Motivation
Measured on 1.19.0 (repository and model identical to main), PostgreSQL 15 from the Compose file, one workflow app, log rows added in steps (real runs first, then copies of a finished run spread over 180 days). Each cell is the median of three requests to the console endpoint with page=1&limit=10.
| Logs of the app | No filter | status=succeeded |
status=failed (no match) |
keyword=abc |
created_at__after (last day) |
|---|---|---|---|---|---|
| 801 | 21 ms | 17 ms | 15 ms | 13 ms | 17 ms |
| 100,801 | 116 ms | 226 ms | 114 ms | 1,524 ms | 33 ms |
| 500,801 | 404 ms | 1,374 ms | 477 ms | 7,486 ms | 149 ms |
500,801 with an index on (tenant_id, app_id, created_at) |
98 ms | 796 ms | 503 ms | 7,578 ms | 27 ms |
Every column grows linearly with the number of logs the app has. An app that runs a few times a second passes 500,000 logs in two days.
With the index, the page query itself becomes an Index Scan Backward that touches ten rows (0.26 ms in EXPLAIN ANALYZE, down from about 300 ms). What is left in the "no filter" and date-range columns is the exact total, which still counts every matching row.
Proposal:
- Add an index on
workflow_app_logs (tenant_id, app_id, created_at), created concurrently on PostgreSQL. It is a superset ofworkflow_app_log_app_idx, which can be dropped in a later step once the new one is in place. I will open a pull request for this. - Not covered by the index, listed so the numbers above are not read as solved:
statusfilters joinworkflow_runsfor every log row to computetotal(the page itself stops early when the status is common). A(tenant_id, app_id, status, created_at)index onworkflow_runs, or driving the filtered query fromworkflow_runs, would be needed.keywordrunsILIKE '%…%'overworkflow_runs.inputsandoutputsof every run of the app. That is inherent to substring search without a trigram or full-text index.totalis exact on every page view. Returninghas_morefromlimit + 1rows and computing the total only when asked would remove the last per-row cost of the default view.
Additional Context
- Runtime statements (the ones a workflow run issues) were all index lookups in
pg_stat_statements, 0.02–0.3 ms each; this issue is about the console list only. - Scripts and raw numbers are available if useful.
Related reports, found after filing:
- #40036 (open) describes the same Logs tab on a 740 GB database. Its reporter added
workflow_app_logs (tenant_id, app_id, created_at DESC)by hand and confirms it makes the first page fast; this issue is about shipping that index so that every installation has it. The per-row loading ofworkflow_runsdescribed there is no longer present onmain:WorkflowAppLogQueryRepositoryselects the run summary columns in the same statement. The keyword search cost described there is the same one listed under point 2 here. - #14752 (closed) raised the index for the count query of the earlier
get_paginate_workflow_app_logs.
If you prefer to track this under #40036, I can move the numbers there and close this one.