From f2187ceb8f61004a5335701ff8aef65d68f00bae Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 7 May 2026 12:22:14 -0400 Subject: [PATCH] Closes #22102: Add a GIN index on CablePath to optimize filtering of cable paths by node (#22144) --- .../dcim/migrations/0234_cablepath_nodes_index.py | 15 +++++++++++++++ netbox/dcim/models/cables.py | 6 ++++++ 2 files changed, 21 insertions(+) create mode 100644 netbox/dcim/migrations/0234_cablepath_nodes_index.py diff --git a/netbox/dcim/migrations/0234_cablepath_nodes_index.py b/netbox/dcim/migrations/0234_cablepath_nodes_index.py new file mode 100644 index 000000000..af4f506a5 --- /dev/null +++ b/netbox/dcim/migrations/0234_cablepath_nodes_index.py @@ -0,0 +1,15 @@ +import django.contrib.postgres.indexes +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ('dcim', '0233_device_render_config_permission'), + ] + + operations = [ + migrations.AddIndex( + model_name='cablepath', + index=django.contrib.postgres.indexes.GinIndex(fields=['_nodes'], name='dcim_cablep__nodes_b23b96_gin'), + ), + ] diff --git a/netbox/dcim/models/cables.py b/netbox/dcim/models/cables.py index 06b5606bc..fb0d87067 100644 --- a/netbox/dcim/models/cables.py +++ b/netbox/dcim/models/cables.py @@ -5,6 +5,7 @@ from collections import Counter from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType from django.contrib.postgres.fields import ArrayField +from django.contrib.postgres.indexes import GinIndex from django.core.exceptions import ValidationError from django.core.validators import MaxValueValidator, MinValueValidator from django.db import models @@ -730,6 +731,11 @@ class CablePath(models.Model): _netbox_private = True class Meta: + indexes = ( + # GIN index supports @> operator used by `_nodes__contains` lookups, + # which fire on every cable/termination delete and path retrace. + GinIndex(fields=('_nodes',)), + ) verbose_name = _('cable path') verbose_name_plural = _('cable paths')