Skip to content

Conversation

gurusainath
Copy link
Member

@gurusainath gurusainath commented Sep 29, 2025

Description

  • Updated index constraints for the notification table to improve query performance and data access efficiency.

Type of Change

  • Improvement (change that would cause existing functionality to not work as expected)
  • Code refactoring

References

[MOB-1199]


Note

Add multiple composite indexes on notifications to speed up common lookups by receiver, workspace, status, entity, state, sender, and entity lookup.

  • Backend/DB
    • db.models.notification.Notification and migration 0108_*:
      • Add composite index notif_receiver_status_idx on ['receiver', 'workspace', 'read_at', 'created_at'].
      • Add composite index notif_receiver_entity_idx on ['receiver', 'workspace', 'entity_name', 'read_at'].
      • Add composite index notif_receiver_state_idx on ['receiver', 'workspace', 'snoozed_till', 'archived_at'].
      • Add composite index notif_receiver_sender_idx on ['receiver', 'workspace', 'sender'].
      • Add composite index notif_entity_lookup_idx on ['workspace', 'entity_identifier', 'entity_name'].

Written by Cursor Bugbot for commit 339d584. This will update automatically on new commits. Configure here.

Summary by CodeRabbit

  • Performance Improvements

    • Faster loading and filtering of notifications; quicker lookups by receiver, sender, entity, and status across inbox, snoozed, and archived views. Improved asset lookup performance.
  • Chores

    • Applied a database migration that creates new indexes concurrently to speed queries while minimizing migration impact.

Copy link
Contributor

coderabbitai bot commented Sep 29, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Adds six new DB indexes (five on Notification, one on FileAsset) and a non-atomic Django migration that creates them concurrently (uses AddIndexConcurrently, atomic = False, depends on migration 0107).

Changes

Cohort / File(s) Summary
DB migration: notification & asset indexes
apps/api/plane/db/migrations/0108_notification_notif_receiver_status_idx_and_more.py
New non-atomic migration using AddIndexConcurrently to create six indexes concurrently: notif_receiver_status_idx (receiver, workspace, read_at, created_at); notif_receiver_entity_idx (receiver, workspace, entity_name, read_at); notif_receiver_state_idx (receiver, workspace, snoozed_till, archived_at); notif_receiver_sender_idx (receiver, workspace, sender); notif_entity_lookup_idx (workspace, entity_identifier, entity_name); and asset_asset_idx on FileAsset (asset).
Model: notification Meta.indexes
apps/api/plane/db/models/notification.py
Added five composite models.Index entries to Notification.Meta.indexes matching the migration.
Model: fileasset Meta.indexes
apps/api/plane/db/models/asset.py
Added models.Index entry asset_asset_idx on the asset field to match the migration.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

🌟improvement

Suggested reviewers

  • dheeru0198

Poem

I hop through rows where indexes sprout,
Six little paths to speed queries out.
Receiver, entity, asset in line,
Concurrent digs make lookups fine.
A rabbit’s clap — quick, neat, and spry! 🥕

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description Check ⚠️ Warning The PR description provides a clear summary of the index changes, categorizes the type of change, and includes relevant references but omits the required Test Scenarios section, leaving it unclear what steps were taken to verify the migration and index functionality. Please add a Test Scenarios section describing how you applied and verified the migration, how index creation was validated, and any performance measurements or tests you ran to ensure the changes work as intended.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title Check ✅ Passed The title succinctly identifies the main change—updating index constraints on the Notification model to optimize query performance—and includes the relevant issue ID for tracking, making it clear and specific without unnecessary detail.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch indexing-notification-fields

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3e27e55 and 8e2616f.

📒 Files selected for processing (2)
  • apps/api/plane/db/migrations/0108_notification_notif_receiver_status_idx_and_more.py (1 hunks)
  • apps/api/plane/db/models/asset.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (3)
apps/api/plane/db/migrations/0108_notification_notif_receiver_status_idx_and_more.py (2)

7-8: LGTM: Correct use of non-atomic migration.

Setting atomic = False is required when using AddIndexConcurrently to avoid table locks during index creation, which is essential for production databases with active traffic.


14-39: Notification model indexes verified
All five composite indexes from the migration are defined in Notification.Meta.indexes as expected. Run EXPLAIN ANALYZE on key notification queries in staging to confirm they’re leveraged effectively.

apps/api/plane/db/models/asset.py (1)

69-69: The asset index is justified and should be retained.

Multiple view functions perform filter(asset=…) and get(asset=…) queries on FileAsset (e.g., in apps/api/plane/app/views/asset/base.py), so the index on the asset FileField supports those lookups and improves performance.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🧪 Early access (Sonnet 4.5): enabled

We are currently testing the Sonnet 4.5 model, which is expected to improve code review quality. However, this model may lead to increased noise levels in the review comments. Please disable the early access features if the noise level causes any inconvenience.

Note:

  • Public repositories are always opted into early access features.
  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

makeplane bot commented Sep 29, 2025

Linked to Plane Work Item(s)

This comment was auto-generated by Plane

dheeru0198
dheeru0198 previously approved these changes Sep 30, 2025
@dheeru0198 dheeru0198 added the 🔄migrations Contains Migration changes label Sep 30, 2025
@dheeru0198 dheeru0198 marked this pull request as ready for review September 30, 2025 10:16
@Copilot Copilot AI review requested due to automatic review settings September 30, 2025 10:16
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR optimizes query performance for the notification table by adding strategic database indexes to support common query patterns.

  • Added 5 new composite indexes to the notification table
  • Created Django migration to implement the index changes
  • Focused on receiver-based queries, entity lookups, and notification state filtering

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
apps/api/plane/db/models/notification.py Added 5 new composite indexes to optimize notification queries
apps/api/plane/db/migrations/0108_notification_notif_receiver_status_idx_and_more.py Django migration to create the new indexes

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
apps/api/plane/db/migrations/0108_notification_notif_receiver_status_idx_and_more.py (1)

13-32: Consider concurrent index creation for production deployments.

The migration uses standard AddIndex operations, which will lock the table during index creation. For a large notifications table in production, this could cause downtime or blocking.

Django supports concurrent index creation via CREATE INDEX CONCURRENTLY (PostgreSQL). Consider modifying this migration to use migrations.AddIndexConcurrently if deploying to a production database with significant data volume.

Example modification for concurrent index creation:

from django.contrib.postgres.operations import AddIndexConcurrently

class Migration(migrations.Migration):
    atomic = False  # Required for concurrent operations
    
    operations = [
        AddIndexConcurrently(
            model_name='notification',
            index=models.Index(fields=['receiver', 'workspace', 'read_at', 'created_at'], name='notif_receiver_status_idx'),
        ),
        # ... repeat for other indexes
    ]

Note: This requires PostgreSQL and adds complexity to rollback scenarios.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c45151d and 339d584.

📒 Files selected for processing (2)
  • apps/api/plane/db/migrations/0108_notification_notif_receiver_status_idx_and_more.py (1 hunks)
  • apps/api/plane/db/models/notification.py (1 hunks)
🔇 Additional comments (5)
apps/api/plane/db/migrations/0108_notification_notif_receiver_status_idx_and_more.py (2)

1-33: Migration matches model definitions correctly.

The migration operations correctly mirror the index definitions in the model file. All five indexes have matching field orders and names.


8-10: Dependency chain verified. Migration 0107 exists and directly precedes 0108—no conflicts detected.

apps/api/plane/db/models/notification.py (3)

50-52: Index usage confirmed. Multiple ORM queries filter on snoozed_till and archived_at (e.g., in apps/api/plane/app/views/notification/base.py around lines 72–87), so the composite index on receiver, workspace, snoozed_till, archived_at is appropriately utilized.


57-60: Unable to locate usage of composite key filters
I couldn’t find any code filtering notifications by workspace, entity_identifier, and entity_name together. Please confirm such queries exist to justify the composite index or consider removing it.


41-44: Confirm index field order matches actual query filters.
No occurrences of queries filtering by both receiver and workspace were found—please manually verify that ["receiver", "workspace", "read_at", "created_at"] aligns with your most common WHERE clauses and sort patterns.

Copy link

cursor bot commented Sep 30, 2025

You have run out of free Bugbot PR reviews for this billing cycle. This will reset on October 20.

To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
apps/api/plane/db/migrations/0108_notification_notif_receiver_status_idx_and_more.py (1)

14-35: Consider index maintenance overhead and monitoring.

Adding five composite indexes will improve read performance but increases:

  • Write operation overhead (INSERT/UPDATE/DELETE on notifications)
  • Storage requirements
  • Index maintenance during vacuuming

Recommendations:

  1. Monitor index usage after deployment using PostgreSQL's pg_stat_user_indexes view to identify unused indexes
  2. Track index bloat and schedule periodic REINDEX operations if needed
  3. Measure query performance before/after to quantify improvements
  4. Consider index-only scans by including frequently-selected columns in indexes if query patterns support it

Example monitoring query:

SELECT schemaname, tablename, indexname, idx_scan, idx_tup_read, idx_tup_fetch
FROM pg_stat_user_indexes
WHERE tablename = 'notification'
ORDER BY idx_scan;
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 339d584 and 3e27e55.

📒 Files selected for processing (1)
  • apps/api/plane/db/migrations/0108_notification_notif_receiver_status_idx_and_more.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (2)
apps/api/plane/db/migrations/0108_notification_notif_receiver_status_idx_and_more.py (2)

1-12: LGTM! Proper setup for concurrent index creation.

The migration correctly imports AddIndexConcurrently and sets atomic = False, which is required for concurrent index operations in PostgreSQL to avoid table locking during index creation.


14-35: Composite indexes align with model schema and expected query patterns.

All five indexes use non‐nullable leading columns (receiver, workspace), nullable fields (read_at, snoozed_till, archived_at) appear later, and no redundant coverage exists given existing single‐ and two‐column indexes. Migrations match the Meta.indexes in Notification. No changes needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚙️backend 🔄migrations Contains Migration changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants