@@ -170,6 +170,7 @@ class BackgroundJobRow(Base):
170170 __table_args__ = (
171171 UniqueConstraint ("idempotency_key" , name = "uq_background_job_idempotency" ),
172172 Index ("ix_background_job_claim" , "status" , "available_at" , "leased_until" , "id" ),
173+ Index ("ix_background_job_delivery_claim" , "kind" , "status" , "finished_at" ),
173174 )
174175
175176 id : Mapped [str ] = mapped_column (Text , primary_key = True )
@@ -735,6 +736,18 @@ def _ensure_pg_context_answer_message_reply_index(connection) -> None:
735736 )
736737
737738
739+ def _ensure_pg_background_job_delivery_claim_index (connection ) -> None :
740+ """background_job 表补已完成视觉投递领取索引。"""
741+ insp = inspect (connection )
742+ if not insp .has_table ("background_job" ):
743+ return
744+ connection .execute (
745+ text (
746+ "CREATE INDEX IF NOT EXISTS ix_background_job_delivery_claim ON background_job (kind, status, finished_at)"
747+ )
748+ )
749+
750+
738751def _ensure_pg_stat_statements_extension (connection ) -> None :
739752 """启用 pg_stat_statements(仅应在独立 autocommit 连接中调用)。
740753
@@ -840,6 +853,7 @@ async def get_session(*, read_only: bool = False):
840853 ("ddl.message_timeline_metadata" , _ensure_pg_message_timeline_metadata ),
841854 ("ddl.context_answer_reply_index" , _ensure_pg_context_answer_reply_index ),
842855 ("ddl.context_answer_message_reply_index" , _ensure_pg_context_answer_message_reply_index ),
856+ ("ddl.background_job_delivery_claim_index" , _ensure_pg_background_job_delivery_claim_index ),
843857]
844858
845859
0 commit comments