-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(findings): Optimize findings endpoint (#7019)
- Loading branch information
1 parent
78877c4
commit 80e24b9
Showing
11 changed files
with
362 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
api/src/backend/api/migrations/0010_findings_performance_indexes_partitions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from functools import partial | ||
|
||
from django.db import connection, migrations | ||
|
||
|
||
def create_index_on_partitions( | ||
apps, schema_editor, parent_table: str, index_name: str, index_details: str | ||
): | ||
with connection.cursor() as cursor: | ||
cursor.execute( | ||
""" | ||
SELECT inhrelid::regclass::text | ||
FROM pg_inherits | ||
WHERE inhparent = %s::regclass; | ||
""", | ||
[parent_table], | ||
) | ||
partitions = [row[0] for row in cursor.fetchall()] | ||
# Iterate over partitions and create index concurrently. | ||
# Note: PostgreSQL does not allow CONCURRENTLY inside a transaction, | ||
# so we need atomic = False for this migration. | ||
for partition in partitions: | ||
sql = ( | ||
f"CREATE INDEX CONCURRENTLY IF NOT EXISTS {partition.replace('.', '_')}_{index_name} ON {partition} " | ||
f"{index_details};" | ||
) | ||
schema_editor.execute(sql) | ||
|
||
|
||
def drop_index_on_partitions(apps, schema_editor, parent_table: str, index_name: str): | ||
with schema_editor.connection.cursor() as cursor: | ||
cursor.execute( | ||
""" | ||
SELECT inhrelid::regclass::text | ||
FROM pg_inherits | ||
WHERE inhparent = %s::regclass; | ||
""", | ||
[parent_table], | ||
) | ||
partitions = [row[0] for row in cursor.fetchall()] | ||
|
||
# Iterate over partitions and drop index concurrently. | ||
for partition in partitions: | ||
partition_index = f"{partition.replace('.', '_')}_{index_name}" | ||
sql = f"DROP INDEX CONCURRENTLY IF EXISTS {partition_index};" | ||
schema_editor.execute(sql) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
atomic = False | ||
|
||
dependencies = [ | ||
("api", "0009_increase_provider_uid_maximum_length"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
partial( | ||
create_index_on_partitions, | ||
parent_table="findings", | ||
index_name="findings_tenant_and_id_idx", | ||
index_details="(tenant_id, id)", | ||
), | ||
reverse_code=partial( | ||
drop_index_on_partitions, | ||
parent_table="findings", | ||
index_name="findings_tenant_and_id_idx", | ||
), | ||
), | ||
migrations.RunPython( | ||
partial( | ||
create_index_on_partitions, | ||
parent_table="findings", | ||
index_name="find_tenant_scan_idx", | ||
index_details="(tenant_id, scan_id)", | ||
), | ||
reverse_code=partial( | ||
drop_index_on_partitions, | ||
parent_table="findings", | ||
index_name="find_tenant_scan_idx", | ||
), | ||
), | ||
migrations.RunPython( | ||
partial( | ||
create_index_on_partitions, | ||
parent_table="findings", | ||
index_name="find_tenant_scan_id_idx", | ||
index_details="(tenant_id, scan_id, id)", | ||
), | ||
reverse_code=partial( | ||
drop_index_on_partitions, | ||
parent_table="findings", | ||
index_name="find_tenant_scan_id_idx", | ||
), | ||
), | ||
migrations.RunPython( | ||
partial( | ||
create_index_on_partitions, | ||
parent_table="findings", | ||
index_name="find_delta_new_idx", | ||
index_details="(tenant_id, id) where delta = 'new'", | ||
), | ||
reverse_code=partial( | ||
drop_index_on_partitions, | ||
parent_table="findings", | ||
index_name="find_delta_new_idx", | ||
), | ||
), | ||
] |
49 changes: 49 additions & 0 deletions
49
api/src/backend/api/migrations/0011_findings_performance_indexes_parent.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("api", "0010_findings_performance_indexes_partitions"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddIndex( | ||
model_name="finding", | ||
index=models.Index( | ||
fields=["tenant_id", "id"], name="findings_tenant_and_id_idx" | ||
), | ||
), | ||
migrations.AddIndex( | ||
model_name="finding", | ||
index=models.Index( | ||
fields=["tenant_id", "scan_id"], name="find_tenant_scan_idx" | ||
), | ||
), | ||
migrations.AddIndex( | ||
model_name="finding", | ||
index=models.Index( | ||
fields=["tenant_id", "scan_id", "id"], name="find_tenant_scan_id_idx" | ||
), | ||
), | ||
migrations.AddIndex( | ||
model_name="finding", | ||
index=models.Index( | ||
condition=models.Q(("delta", "new")), | ||
fields=["tenant_id", "id"], | ||
name="find_delta_new_idx", | ||
), | ||
), | ||
migrations.AddIndex( | ||
model_name="resourcetagmapping", | ||
index=models.Index( | ||
fields=["tenant_id", "resource_id"], name="resource_tag_tenant_idx" | ||
), | ||
), | ||
migrations.AddIndex( | ||
model_name="resource", | ||
index=models.Index( | ||
fields=["tenant_id", "service", "region", "type"], | ||
name="resource_tenant_metadata_idx", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.