Skip to content

Commit a04dd16

Browse files
committed
Add ix_jobs_pipeline_fetch_q index
1 parent 301f217 commit a04dd16

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Add ix_jobs_pipeline_fetch_q index
2+
3+
Revision ID: 8b6d5d8c1b9a
4+
Revises: 6026b29d78c7
5+
Create Date: 2026-03-10 11:30:00.000000+00:00
6+
7+
"""
8+
9+
import sqlalchemy as sa
10+
from alembic import op
11+
12+
# revision identifiers, used by Alembic.
13+
revision = "8b6d5d8c1b9a"
14+
down_revision = "6026b29d78c7"
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade() -> None:
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
with op.get_context().autocommit_block():
22+
op.drop_index(
23+
"ix_jobs_pipeline_fetch_q",
24+
table_name="jobs",
25+
if_exists=True,
26+
postgresql_concurrently=True,
27+
)
28+
op.create_index(
29+
"ix_jobs_pipeline_fetch_q",
30+
"jobs",
31+
[sa.literal_column("last_processed_at ASC")],
32+
unique=False,
33+
sqlite_where=sa.text("(status NOT IN ('TERMINATED', 'ABORTED', 'FAILED', 'DONE'))"),
34+
postgresql_where=sa.text(
35+
"(status NOT IN ('TERMINATED', 'ABORTED', 'FAILED', 'DONE'))"
36+
),
37+
postgresql_concurrently=True,
38+
)
39+
# ### end Alembic commands ###
40+
41+
42+
def downgrade() -> None:
43+
# ### commands auto generated by Alembic - please adjust! ###
44+
with op.get_context().autocommit_block():
45+
op.drop_index(
46+
"ix_jobs_pipeline_fetch_q",
47+
table_name="jobs",
48+
if_exists=True,
49+
postgresql_concurrently=True,
50+
)
51+
# ### end Alembic commands ###

src/dstack/_internal/server/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,15 @@ class JobModel(PipelineModelMixin, BaseModel):
515515
should be processed only one-by-one.
516516
"""
517517

518+
__table_args__ = (
519+
Index(
520+
"ix_jobs_pipeline_fetch_q",
521+
last_processed_at.asc(),
522+
postgresql_where=status.not_in(JobStatus.finished_statuses()),
523+
sqlite_where=status.not_in(JobStatus.finished_statuses()),
524+
),
525+
)
526+
518527

519528
class GatewayModel(PipelineModelMixin, BaseModel):
520529
__tablename__ = "gateways"

0 commit comments

Comments
 (0)