Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit 32938c0

Browse files
authored
Add missing indices relevant for Repo Deletion (#490)
There are a few very slow `DELETE` queries as part of repo deletion. The worst of those is deleting the newly created "shadow owner", which takes >1 minute. Running all the `on_delete` hooks using django (which has since been fixes) has revealed that the `SET NULL` of `Commit.author` is the culprit, as that column did not have any index previously. That is similar to the `SET NULL` on `Repository.fork`, except that query is by far not as slow, probably because of a lot less records that need to be scanned. --- While at it, this also fixes a django warning related to the `default` not being a callable, and aligns the `Repository.fork` `SET NULL` constraint with the reality of the database.
1 parent ce6e1e9 commit 32938c0

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Generated by Django 4.2.16 on 2025-01-27 13:18
2+
3+
import django.db.models.deletion
4+
from django.contrib.postgres.operations import AddIndexConcurrently
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
atomic = False
10+
11+
dependencies = [
12+
("core", "0063_increment_version"),
13+
]
14+
15+
operations = [
16+
migrations.AlterField(
17+
model_name="repository",
18+
name="fork",
19+
field=models.ForeignKey(
20+
blank=True,
21+
db_column="forkid",
22+
null=True,
23+
on_delete=django.db.models.deletion.SET_NULL,
24+
to="core.repository",
25+
),
26+
),
27+
migrations.AlterField(
28+
model_name="repository",
29+
name="languages",
30+
field=django.contrib.postgres.fields.ArrayField(
31+
base_field=models.CharField(),
32+
blank=True,
33+
default=list,
34+
null=True,
35+
size=None,
36+
),
37+
),
38+
AddIndexConcurrently(
39+
model_name="commit",
40+
index=models.Index(fields=["author"], name="commits_author_cd2f50_idx"),
41+
),
42+
AddIndexConcurrently(
43+
model_name="repository",
44+
index=models.Index(fields=["fork"], name="repos_forkid_4cd440_idx"),
45+
),
46+
]

shared/django_apps/core/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ class Languages(models.TextChoices):
108108
language = models.TextField(
109109
null=True, blank=True, choices=Languages.choices
110110
) # Really an ENUM in db
111-
languages = ArrayField(models.CharField(), default=[], blank=True, null=True)
111+
languages = ArrayField(models.CharField(), default=list, blank=True, null=True)
112112
languages_last_updated = DateTimeWithoutTZField(null=True, blank=True)
113113
fork = models.ForeignKey(
114114
"core.Repository",
115115
db_column="forkid",
116-
on_delete=models.DO_NOTHING,
116+
on_delete=models.SET_NULL,
117117
null=True,
118118
blank=True,
119119
)
@@ -149,6 +149,7 @@ class Meta:
149149
app_label = CORE_APP_LABEL
150150
ordering = ["-repoid"]
151151
indexes = [
152+
models.Index(fields=["fork"]),
152153
models.Index(
153154
fields=["service_id", "author"],
154155
name="repos_service_id_author",
@@ -287,6 +288,7 @@ class Meta:
287288
)
288289
]
289290
indexes = [
291+
models.Index(fields=["author"]),
290292
models.Index(
291293
fields=["repository", "-timestamp"],
292294
name="commits_repoid_timestamp_desc",

0 commit comments

Comments
 (0)