Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Add composite (status, next_retry_at) index on webhook_delivery to back the dispatcher's main 'due deliveries' query and avoid full-table scans once a few thousand deliveries accumulate; declared on the model and applied via init_db migration
Murtaza Nasir3f3dccc70d58
diff --git a/src/init_db.py b/src/init_db.pyindex 8b9c9a8c..98372fb2 100644--- a/src/init_db.py+++ b/src/init_db.py@@ -560,6 +560,16 @@ def initialize_database(app):except Exception as e:app.logger.warning(f"Could not create index on recording (user_id, file_hash): {e}")+ # Composite index that backs the webhook dispatcher's main query:+ # `status IN ('pending','failed') AND next_retry_at <= now`.+ # Without this the dispatcher scans the full webhook_delivery+ # table every poll once a few thousand deliveries accumulate.+ try:+ if create_index_if_not_exists(engine, 'idx_delivery_status_retry', 'webhook_delivery', 'status, next_retry_at'):+ app.logger.info("Created composite index idx_delivery_status_retry on webhook_delivery (status, next_retry_at)")+ except Exception as e:+ app.logger.warning(f"Could not create composite index on webhook_delivery: {e}")+# Add folder_id column to recording table for folders featureif add_column_if_not_exists(engine, 'recording', 'folder_id', 'INTEGER'):app.logger.info("Added folder_id column to recording table")diff --git a/src/models/webhook.py b/src/models/webhook.pyindex 4d73a6d8..b4090cc5 100644--- a/src/models/webhook.py+++ b/src/models/webhook.py@@ -199,6 +199,15 @@ class WebhookDelivery(db.Model):webhook = db.relationship('Webhook', back_populates='deliveries')+ # Composite index for the dispatcher's main query: due deliveries+ # are those with status in ('pending', 'failed') AND+ # next_retry_at <= now (or null). The single-column indexes don't+ # help much on a multi-thousand-row delivery table; this composite+ # makes the dispatcher sweep O(log n) instead of a scan.+ __table_args__ = (+ db.Index('idx_delivery_status_retry', 'status', 'next_retry_at'),+ )+def __repr__(self): # pragma: no coverreturn f'<WebhookDelivery {self.id} webhook={self.webhook_id} status={self.status}>'