Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(workflow): avoid composite index scans when binding tasks (#45510)

zyssyz1230096737486c6
diff --git a/api/repositories/sqlalchemy_api_workflow_run_repository.py b/api/repositories/sqlalchemy_api_workflow_run_repository.py
index e111cf80dbc..00a520985b3 100644
--- a/api/repositories/sqlalchemy_api_workflow_run_repository.py
+++ b/api/repositories/sqlalchemy_api_workflow_run_repository.py
@@ -1246,8 +1246,9 @@ class DifyAPISQLAlchemyWorkflowRunRepository(APIWorkflowRunRepository):
update(WorkflowRun)
.where(
WorkflowRun.id == workflow_run_id,
- WorkflowRun.tenant_id == tenant_id,
- WorkflowRun.app_id == app_id,
+ # Keep owner checks together so PostgreSQL can use the run's primary key instead of
+ # underestimating the tenant/app composite index when their values are correlated.
+ and_(WorkflowRun.tenant_id == tenant_id, WorkflowRun.app_id == app_id).is_(True),
WorkflowRun.task_id.is_(None),
)
.values(task_id=task_id)
diff --git a/api/tests/unit_tests/repositories/test_sqlalchemy_api_workflow_run_repository.py b/api/tests/unit_tests/repositories/test_sqlalchemy_api_workflow_run_repository.py
index 25fba950425..077e2e6991a 100644
--- a/api/tests/unit_tests/repositories/test_sqlalchemy_api_workflow_run_repository.py
+++ b/api/tests/unit_tests/repositories/test_sqlalchemy_api_workflow_run_repository.py
@@ -305,6 +305,57 @@ def _workflow_run(*, run_id: str, tenant_id: str, status: WorkflowExecutionStatu
)
+ ("tenant_id", "app_id", "workflow_run_id", "should_bind"),
+ [
+ ("tenant-1", "app-1", "run-1", True),
+ ("tenant-2", "app-1", "run-1", False),
+ ("tenant-1", "app-2", "run-1", False),
+ ("tenant-1", "app-1", "missing-run", False),
+ ],
+)
+def test_bind_workflow_task_only_binds_the_matching_run(
+ sqlite_session: Session,
+ sqlite_session_factory: sessionmaker[Session],
+ tenant_id: str,
+ app_id: str,
+ workflow_run_id: str,
+ should_bind: bool,
+) -> None:
+ run = _workflow_run(run_id="run-1", tenant_id="tenant-1", status=WorkflowExecutionStatus.RUNNING)
+ other_run = _workflow_run(run_id="run-2", tenant_id="tenant-1", status=WorkflowExecutionStatus.RUNNING)
+ sqlite_session.add_all([run, other_run])
+ sqlite_session.commit()
+ repository = DifyAPISQLAlchemyWorkflowRunRepository(session_maker=sqlite_session_factory)
+
+ repository.bind_workflow_task(
+ tenant_id=tenant_id,
+ app_id=app_id,
+ workflow_run_id=workflow_run_id,
+ task_id="task-1",
+ )
+
+ sqlite_session.expire_all()
+ assert run.task_id == ("task-1" if should_bind else None)
+ assert other_run.task_id is None
+
+
+def test_bind_workflow_task_preserves_the_first_binding(
+ sqlite_session: Session,
+ sqlite_session_factory: sessionmaker[Session],
+) -> None:
+ run = _workflow_run(run_id="run-1", tenant_id="tenant-1", status=WorkflowExecutionStatus.RUNNING)
+ sqlite_session.add(run)
+ sqlite_session.commit()
+ repository = DifyAPISQLAlchemyWorkflowRunRepository(session_maker=sqlite_session_factory)
+
+ repository.bind_workflow_task(tenant_id="tenant-1", app_id="app-1", workflow_run_id="run-1", task_id="first-task")
+ repository.bind_workflow_task(tenant_id="tenant-1", app_id="app-1", workflow_run_id="run-1", task_id="second-task")
+
+ sqlite_session.expire_all()
+ assert run.task_id == "first-task"
+
+
def test_get_message_refs_filters_by_app_and_returns_lightweight_records(
sqlite_session: Session,
sqlite_session_factory: sessionmaker[Session],