Skip to content

Commit 661c06f

Browse files
authored
fix: reverse index to journals to speed up triggers (#19056)
When a journal entry changes, the `update_project_last_serial` trigger fires, which runs `maintain_project_last_serial()`. This function looks for the `max(id)` for a given `name`, meaning it first has to find all records for `name`, and then find the highest `id` (at the "end" of the records). By creating a compound index that has the name first, and then the most recent `id` at the **top** of the index should return much faster, speeding up any trigger/function executions, which are sadly somewhat opaque to our instrumentation. Signed-off-by: Mike Fiedler <miketheman@gmail.com>
1 parent 5428407 commit 661c06f

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
"""
3+
Add reverse ID index for journals
4+
5+
Revision ID: 7cf64da2632a
6+
Revises: 4c20f2342bba
7+
Create Date: 2025-11-14 18:14:43.440919
8+
"""
9+
10+
import sqlalchemy as sa
11+
12+
from alembic import op
13+
14+
revision = "7cf64da2632a"
15+
down_revision = "4c20f2342bba"
16+
17+
18+
def upgrade():
19+
# CREATE INDEX CONCURRENTLY cannot happen inside a transaction. We'll close
20+
# our transaction here and issue the statement.
21+
op.get_bind().commit()
22+
23+
with op.get_context().autocommit_block():
24+
op.create_index(
25+
"journals_name_id_idx",
26+
"journals",
27+
["name", sa.literal_column("id DESC")],
28+
unique=False,
29+
postgresql_concurrently=True,
30+
)
31+
32+
33+
def downgrade():
34+
op.drop_index("journals_name_id_idx", table_name="journals")

warehouse/packaging/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,12 @@ def __table_args__(cls): # noqa
10861086
cls._submitted_by,
10871087
cls.submitted_date.desc(),
10881088
),
1089+
# Reverse index on ID, most recent project's journal entry for triggers
1090+
Index(
1091+
"journals_name_id_idx",
1092+
cls.name,
1093+
cls.id.desc(),
1094+
),
10891095
)
10901096

10911097
id: Mapped[int] = mapped_column(primary_key=True)

0 commit comments

Comments
 (0)