Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Fix: tag/custom-field document_count scales badly with tag count (#13203)

Trenton H84a04301d1ad
diff --git a/src/documents/permissions.py b/src/documents/permissions.py
index 2faf36b18..06e1e271a 100644
--- a/src/documents/permissions.py
+++ b/src/documents/permissions.py
@@ -4,14 +4,14 @@ from django.contrib.auth.models import Group
from django.contrib.auth.models import Permission
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
+from django.db.models import Case
from django.db.models import Count
from django.db.models import IntegerField
-from django.db.models import OuterRef
from django.db.models import Q
from django.db.models import QuerySet
-from django.db.models import Subquery
+from django.db.models import Value
+from django.db.models import When
from django.db.models.functions import Cast
-from django.db.models.functions import Coalesce
from guardian.core import ObjectPermissionChecker
from guardian.models import GroupObjectPermission
from guardian.models import UserObjectPermission
@@ -204,20 +204,24 @@ def _permitted_document_ids(user):
).values_list("id", flat=True)
-def get_document_count_filter_for_user(user):
+def get_document_count_filter_for_user(user, related_name: str = "documents"):
"""
Return the Q object used to filter document counts for the given user.
The filter is expressed as an ``id__in`` against a small subquery of permitted
document IDs to keep the generated SQL simple and avoid large OR clauses.
+
+ ``related_name`` is the ORM path from the annotated model to Document (e.g.
+ ``"documents"`` for Tag's direct M2M, or ``"fields__document"`` for CustomField,
+ which only reaches Document via the CustomFieldInstance through-model).
"""
if getattr(user, "is_superuser", False):
# Superuser: no permission filtering needed
- return Q(documents__deleted_at__isnull=True)
+ return Q(**{f"{related_name}__deleted_at__isnull": True})
permitted_ids = _permitted_document_ids(user)
- return Q(documents__id__in=permitted_ids)
+ return Q(**{f"{related_name}__id__in": permitted_ids})
def annotate_document_count_for_related_queryset(
@@ -228,14 +232,33 @@ def annotate_document_count_for_related_queryset(
user: User | None = None,
) -> QuerySet[Any]:
"""
- Annotate a queryset with permissions-aware document counts using a subquery
- against a relation table.
+ Annotate a queryset with a permissions-aware document count for a relation
+ to Document that goes through an M2M/through-model table (e.g. Tag via
+ ``Document.tags.through``, or CustomField via ``CustomFieldInstance``).
+
+ Counts are computed via a single, independent GROUP BY over the relation
+ table -- with the permission filter expressed as a plain ``WHERE`` rather
+ than an aggregate ``FILTER`` -- then injected via ``Case``/``When``. This
+ deliberately avoids two slower alternatives found while building this:
+
+ - A per-outer-row correlated subquery (one execution per row of the
+ annotated queryset): fine at a handful of rows, catastrophic once the
+ queryset has hundreds/thousands of rows.
+ - ``Count(..., filter=Q(id__in=permitted_ids), distinct=True)`` applied
+ directly to the M2M relation: Postgres can fail to plan the ``id__in``
+ check as a semi-join and instead re-checks subquery membership once per
+ row of the (much larger) M2M join -- worse than the correlated subquery.
+
+ Aggregation is restricted to rows whose ``related_object_field`` is one of
+ ``queryset``'s pks, so passing a subset (e.g. a handful of tag descendants)
+ doesn't pay the cost of counting for every row matching the permission
+ filter.
Args:
queryset: base queryset to annotate (must contain pk)
through_model: model representing the relation (e.g., Document.tags.through
or CustomFieldInstance)
- source_field: field on the relation pointing back to queryset pk
+ related_object_field: field on the relation pointing back to queryset pk
target_field: field on the relation pointing to Document id
user: the user for whom to filter permitted document ids
"""
@@ -244,15 +267,27 @@ def annotate_document_count_for_related_queryset(
counts = (
through_model.objects.filter(
**{
- related_object_field: OuterRef("pk"),
+ f"{related_object_field}__in": queryset.values("pk"),
f"{target_field}__in": permitted_ids,
},
)
.values(related_object_field)
- .annotate(c=Count(target_field))
- .values("c")
+ .annotate(c=Count(target_field, distinct=True))
+ )
+ counts_by_pk = {row[related_object_field]: row["c"] for row in counts}
+
+ if not counts_by_pk:
+ return queryset.annotate(
+ document_count=Value(0, output_field=IntegerField()),
+ )
+
+ return queryset.annotate(
+ document_count=Case(
+ *(When(pk=pk, then=Value(count)) for pk, count in counts_by_pk.items()),
+ default=Value(0),
+ output_field=IntegerField(),
+ ),
)
- return queryset.annotate(document_count=Coalesce(Subquery(counts[:1]), 0))
def get_objects_for_user_owner_aware(
diff --git a/src/documents/views.py b/src/documents/views.py
index 10eaae00c..066f8fd8a 100644
--- a/src/documents/views.py
+++ b/src/documents/views.py
@@ -482,7 +482,15 @@ class BulkPermissionMixin:
class PermissionsAwareDocumentCountMixin(BulkPermissionMixin, PassUserMixin):
"""Mixin to add document count to queryset, permissions-aware if needed"""
- # Default is simple relation path, override for through-table/count specialization.
+ # Direct FK/M2M relation name from this model to Document, used for the
+ # cheap Count(filter=...) path (Correspondent, DocumentType, StoragePath).
+ document_count_related_name: str = "documents"
+
+ # Set both of these instead, for models that only reach Document through
+ # an M2M/through-model table (Tag, CustomField). A plain Count(filter=...)
+ # over such a relation is fine for a direct FK, but forces a much more
+ # expensive plan once an M2M bridge table is involved -- see
+ # annotate_document_count_for_related_queryset() for why.
document_count_through: type[Model] | None = None
document_count_source_field: str | None = None
@@ -498,12 +506,14 @@ class PermissionsAwareDocumentCountMixin(BulkPermissionMixin, PassUserMixin):
def get_document_count_filter(self):
request = getattr(self, "request", None)
user = getattr(request, "user", None) if request else None
- return get_document_count_filter_for_user(user)
+ return get_document_count_filter_for_user(
+ user,
+ related_name=self.document_count_related_name,
+ )
def get_queryset(self):
base_qs = super().get_queryset()
- # Use optimized through-table counting when configured.
if self.document_count_through:
user = getattr(getattr(self, "request", None), "user", None)
return annotate_document_count_for_related_queryset(
@@ -513,10 +523,13 @@ class PermissionsAwareDocumentCountMixin(BulkPermissionMixin, PassUserMixin):
user=user,
)
- # Fallback: simple Count on relation with permission filter.
filter = self.get_document_count_filter()
return base_qs.annotate(
- document_count=Count("documents", filter=filter),
+ document_count=Count(
+ self.document_count_related_name,
+ filter=filter,
+ distinct=True,
+ ),
)