[RFC] Database Index Schema Optimizations & Standardized Asynchronous Task Scheduling
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 25/100
Direzione di ricerca
Inizia leggendo backend/app/database/supabase/scripts/create_db.sql e individuando l’entry point del ciclo di vita di FastAPI e il codice cron o delle attività in background esistente. Confronta questi entry point con il design proposto di backend/app/core/scheduler.py, il job store PostgreSQL e i job pianificati elencati. Per considerare il lavoro completato sono necessarie una migrazione concordata e un’implementazione dello scheduler che coprano i requisiti indicati di indicizzazione e pianificazione delle attività.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
RFC: Database Index Schema Optimizations & Standardized Asynchronous Task Scheduling
Status: Draft
Author: Antigravity (AI Contributor)
Target Repository: AOSSIE-Org/Devr.AI
Date: June 26, 2026
1. Executive Summary
As an AI-powered Developer Relations Assistant, Devr.AI is built to process high-throughput event streams from platforms like Discord, Slack, and GitHub in real-time. To maintain high responsiveness, sub-second latency, and horizontal scalability under high workloads, the backend infrastructure must be optimized both at the data persistence layer and the asynchronous task scheduling layer.
This RFC proposes two major system enhancements:
- Database Index Schema Optimizations: Proactive B-Tree, GIN, and Partial indexes for PostgreSQL (Supabase) targeting high-read, high-write tables like
interactionsandrepositoriesto eliminate Sequential Scans (Seq Scan). - Standardized Asynchronous Task Scheduling: Migrating ad-hoc cron scripts to a unified, database-backed, clustered task scheduling framework utilizing APScheduler with a PostgreSQL Job Store integrated into our existing FastAPI/Lifespan event loop.
2. Part 1: Database Index Schema Optimizations
2.1 The Problem
An audit of the current database schema file (create_db.sql) reveals several index gaps:
- The
interactionsTable: This is the most active table in the system, storing every message, comment, and PR event. However, no indexes are defined onuser_id,repository_id,platform,created_at, ortopics_discussed. Any query listing user interactions, filtering by platform, searching topics, or sorting by time forces a costlySeq Scan(O(N) search complexity). - The
repositoriesTable: Frequently searched byfull_name(owner/repo) orowner/nameto look up indexing state or retrieve metadata. Lack of index coverage on these columns degrades performance of LLM tool retrievals. - The
organization_integrationsTable: Frequently queried by the Discord/Slack bots to verify which active organizations to serve. While it has single-column indexes, it lacks optimal composite index coverage for active platforms.
2.2 Proposed SQL Indexing Schema
We propose executing a database migration containing the following index declarations:
-- ====================================================================
-- DATABASE INDEX OPTIMIZATIONS FOR DEVR.AI
-- ====================================================================
-- --------------------------------------------------------------------
-- 1. Optimizations for 'interactions' table (High-Write/High-Read)
-- --------------------------------------------------------------------
-- Optimize foreign key joins and user history queries
CREATE INDEX IF NOT EXISTS idx_interactions_user_id
ON interactions(user_id);
CREATE INDEX IF NOT EXISTS idx_interactions_repository_id
ON interactions(repository_id)
WHERE repository_id IS NOT NULL;
-- Optimize temporal/activity-stream sorting (recent activities first)
CREATE INDEX IF NOT EXISTS idx_interactions_created_at_desc
ON interactions(created_at DESC);
-- Optimize lookup queries combining platform and platform-specific IDs (e.g. webhook deduplication)
CREATE INDEX IF NOT EXISTS idx_interactions_platform_lookup
ON interactions(platform, platform_specific_id);
-- Optimize thread and channel context retrieval (conversational memory queries)
CREATE INDEX IF NOT EXISTS idx_interactions_channel_thread
ON interactions(channel_id, thread_id)
WHERE channel_id IS NOT NULL;
-- Optimize classification and intent analytics
CREATE INDEX IF NOT EXISTS idx_interactions_classification
ON interactions(interaction_type, intent_classification)
WHERE interaction_type IS NOT NULL OR intent_classification IS NOT NULL;
-- Optimize search across topics_discussed array using GIN index (Generalized Inverted Index)
CREATE INDEX IF NOT EXISTS idx_interactions_topics_discussed_gin
ON interactions USING GIN (topics_discussed);
-- --------------------------------------------------------------------
-- 2. Optimizations for 'repositories' table (Metadata Lookups)
-- --------------------------------------------------------------------
-- Optimize fast case-insensitive full-name lookups from tools and agents
CREATE INDEX IF NOT EXISTS idx_repositories_full_name_lower
ON repositories(LOWER(full_name));
-- Optimize composite lookups by owner/name separately
CREATE INDEX IF NOT EXISTS idx_repositories_owner_name
ON repositories(owner, name);
-- Optimize partial index for indexing queues (fetching pending/failed repos)
CREATE INDEX IF NOT EXISTS idx_repositories_indexing_state
ON repositories(is_indexed, indexing_status)
WHERE is_indexed = false;
-- --------------------------------------------------------------------
-- 3. Optimizations for 'organization_integrations' table
-- --------------------------------------------------------------------
-- Optimize high-frequency active platform status checks by integrations
CREATE INDEX IF NOT EXISTS idx_org_integrations_platform_active
ON organization_integrations(platform, is_active)
WHERE is_active = true;
2.3 Query Performance Breakdown (Before & After)
Query 1: Fetching the latest 20 interactions for a user within a specific repository
SELECT * FROM interactions
WHERE user_id = 'c1234567-89ab-cdef-0123-456789abcdef'
AND repository_id = '98765432-10fe-dcba-ba09-876543210fed'
ORDER BY created_at DESC
LIMIT 20;
- Without Optimization: PostgreSQL performs a full
Seq Scanoninteractions, filtering out rows that do not matchuser_idandrepository_id, then performs an in-memory or on-disk sort (Quick Sort) oncreated_at. Cost: O(N) where N is total interactions. - With Optimization: PostgreSQL performs an
Index Scanonidx_interactions_user_idoridx_interactions_repository_id, intersecting the two bitmap indexes, and uses the pre-sorted temporal indexidx_interactions_created_at_descto retrieve the rows immediately. Cost: O(log N) + constant lookup.
Query 2: Searching interactions discussing a specific array of topics (e.g. 'auth', 'database')
SELECT * FROM interactions
WHERE topics_discussed @> ARRAY['auth', 'database']::TEXT[];
- Without Optimization: Forces a Sequential Scan and evaluates the array containment operator (
@>) on every single row. Highly CPU intensive. - With Optimization: Performs a
Bitmap Index Scanon the GIN indexidx_interactions_topics_discussed_gin, instantly pinpointing the matching rows without touching any unrelated disk pages. Performance gain is 100x to 1000x as data scales.
3. Part 2: Standardizing Asynchronous Task Scheduling
3.1 The Problem
Devr.AI needs to run scheduled background jobs:
- Periodic synchronization of repository metrics (stars, forks, open issues).
- Weekly compiler/cleanup of conversation summaries (
conversation_contextaggregation). - Automated health-checks of linked Discord and Slack integrations.
- Token expiration monitoring for OAuth tokens.
Currently, Devr.AI uses aio-pika (RabbitMQ) for real-time reactive event handling, but lacks a centralized, reliable background scheduler. Writing infinite while True: await asyncio.sleep(86400) loops inside the web server is an anti-pattern:
- If the FastAPI process restarts, the sleep timer is reset.
- If multiple worker instances are scaled horizontally, each worker will run the same background task simultaneously, causing race conditions and database locks.
- There is no visibility, error logging, or manual trigger capabilities for scheduled jobs.
3.2 Proposed Architecture: APScheduler with PostgreSQL (Supabase) Job Store
We propose standardizing on APScheduler 3.x/4.x with a SQLAlchemy/PostgreSQL Job Store. This integrates perfectly with Devr.AI's Supabase-backed database layer.
flowchart TD
subgraph FastAPI Web Process
A[FastAPI App Lifespan] -->|Initializes| B(AsyncScheduler)
B -->|Configures| C[(PostgreSQL Job Store)]
end
subgraph PostgreSQL Database
C --- D[Table: apscheduler_jobs]
end
subgraph Cluster Deployment
E[FastAPI Worker Instance 1] -->|Row Lock| D
F[FastAPI Worker Instance 2] -->|Row Lock| D
end
B -->|Triggers Job| G[Execute Sync Job]
G -->|Optionally Publishes Event| H[RabbitMQ Event Bus]
Key Benefits:
- Persistence: Scheduled tasks are stored in a database table. If the servers crash, the scheduler picks up exactly where it left off, catching up on missed triggers (
coalesce=True). - Distributed Locking (Cluster-Safe): If we scale to 3 FastAPI worker containers, APScheduler uses PostgreSQL's row-locking mechanisms to ensure that only one instance executes a scheduled job at a given time.
- Async Native: Fully supports
async/awaitsyntax, allowing jobs to query databases or invoke agents using non-blocking I/O.
3.3 Database Table Definition for Jobs
APScheduler automatically generates its required table, but for completeness and integration with migrations, the underlying table structure in our PostgreSQL/Supabase DB is:
-- DDL for APScheduler persistence (if manually managed or audited)
CREATE TABLE IF NOT EXISTS apscheduler_jobs (
id VARCHAR(191) PRIMARY KEY NOT NULL,
next_run_time DOUBLE PRECISION,
job_state BYTEA NOT NULL
);
COMMENT ON TABLE apscheduler_jobs IS 'Stores persistent scheduled tasks and their execution states for Devr.AI';
3.4 Pydantic Model for Job Declarations
To expose job states on our admin dashboards or monitor them programmatically, we declare a Pydantic representation:
from pydantic import BaseModel, Field
from typing import Optional
from datetime import datetime
class ScheduledJobResponse(BaseModel):
"""Schema representing a registered background task in Devr.AI."""
id: str = Field(..., description="Unique identifier of the background job")
name: str = Field(..., description="Human-readable name of the function being executed")
next_run_time: Optional[datetime] = Field(None, description="Timestamp of the next planned execution")
trigger: str = Field(..., description="Type of trigger (interval, cron, or date)")
coalesce: bool = Field(True, description="Whether to merge multiple missed executions into a single run")
is_paused: bool = Field(False, description="Indicates if the job is temporarily suspended")
3.5 Python Implementation: Centralized Scheduler Service
We propose adding a centralized scheduler module under backend/app/core/scheduler.py:
import logging
from apscheduler.schedulers.asyncio import AsyncScheduler
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from app.core.config import settings
from app.database.supabase.client import get_supabase_client
logger = logging.getLogger(__name__)
# Centralized Scheduler Instance
scheduler: Optional[AsyncScheduler] = None
def get_scheduler() -> AsyncScheduler:
global scheduler
if scheduler is None:
# Use our existing Supabase/PostgreSQL connection string
database_url = settings.database_url.replace("postgres://", "postgresql+psycopg2://")
job_stores = {
'default': SQLAlchemyJobStore(url=database_url, tablename="apscheduler_jobs")
}
job_defaults = {
'coalesce': True,
'max_instances': 1,
'misfire_grace_time': 300
}
scheduler = AsyncScheduler(
jobstores=job_stores,
job_defaults=job_defaults,
timezone="UTC"
)
return scheduler
# Example Scheduled Task: Periodic Repository Stats Sync
async def sync_repository_stats_job():
"""Fetches up-to-date star/fork/issue metrics from GitHub and updates PostgreSQL."""
logger.info("Starting scheduled job: sync_repository_stats_job")
try:
supabase = get_supabase_client()
# Fetch active repositories
response = await supabase.table("repositories").select("id, full_name").execute()
if not response.data:
logger.info("No repositories to sync.")
return
for repo in response.data:
repo_id = repo['id']
full_name = repo['full_name']
logger.debug(f"Syncing stats for {full_name}")
# 1. Fetch latest stats via GitHub Service (simulated/imported)
# star_count, fork_count, open_issues = await github_service.fetch_repo_stats(full_name)
# 2. Update database
# await supabase.table("repositories").update({
-- "stars_count": star_count,
-- "forks_count": fork_count,
-- "open_issues_count": open_issues,
-- "updated_at": datetime.utcnow().isoformat()
-- }).eq("id", repo_id).execute()
logger.info("Completed scheduled job: sync_repository_stats_job successfully")
except Exception as e:
logger.error(f"Error executing sync_repository_stats_job: {e}", exc_info=True)
def register_standard_jobs(sched: AsyncScheduler):
"""Registers standard, recurring system-wide jobs."""
# Sync GitHub metrics every 6 hours
sched.add_job(
sync_repository_stats_job,
trigger='interval',
hours=6,
id='sync_repository_stats',
replace_existing=True
)
# Example: Run conversation summarization compile daily at midnight UTC
# sched.add_job(
# compile_conversation_summaries_job,
# trigger='cron',
# hour=0,
# minute=0,
# id='daily_conversation_compile',
# replace_existing=True
# )
FastAPI Lifespan Integration (backend/app/main.py):
To start and stop the scheduler automatically with our API server:
from contextlib import asynccontextmanager
from fastapi import FastAPI
from app.core.scheduler import get_scheduler, register_standard_jobs
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup
logger.info("Initializing system scheduler...")
sched = get_scheduler()
register_standard_jobs(sched)
sched.start()
logger.info("Background scheduler started successfully.")
yield
# Shutdown
logger.info("Shutting down background scheduler...")
sched.shutdown()
logger.info("Scheduler shutdown complete.")
app = FastAPI(lifespan=lifespan)
4. Implementation Steps & Feedback
We request the community's and maintainers' feedback on:
- Index Coverage: Are there other high-frequency lookups performed by our LangGraph agents (e.g., Weaviate/FalkorDB sync scripts) that we should include in the SQL index migration?
- Scheduler Store: Is SQLAlchemy/PostgreSQL preferred for the persistent Job Store, or should we consider Redis if Devr.AI scales towards a larger event-driven topology? Using Supabase/PostgreSQL is currently the most lightweight path as we already run it.
Upon agreement, we will provide a Pull Request containing:
- Database migration script
02_optimize_indexes_and_jobs.sql. - Pyproject dependencies update (adding
apschedulerandsqlalchemy). - Implementation of
app/core/scheduler.pyand integration in the FastAPI lifespan.
- Lingua principale
- Python
- Stelle
- 103
- Fork
- 143
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Nessuna guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di AOSSIE-Org/Devr.AI
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 95/100
AOSSIE-Org/Devr.AI#315 · 1 commento ·
-
Bump Weaviate image to 1.39.13 (Go 1.26.10 / CVE-2026-111943)Forse già presa @dudanogueira l’ha presa 38 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
AOSSIE-Org/Devr.AI#309 · 1 commento ·
-
DOCS UPDATE: Incorrect Flask command path in backend installation guideForse già presa @Ashwin-Pillai-22 l’ha presa 321 giorni fa. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 68/100
AOSSIE-Org/Devr.AI#229 · 3 commenti ·
-
BUG:Devr.AI Agent loses user_profile for returning users and fails to persist conversation summaries & cumulative interaction countsForse già presa @shubhumre777 l’ha presa 34 giorni fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 55/100
AOSSIE-Org/Devr.AI#311 · 2 commenti ·
-
BUG: Local pytest suite fails to collect due to deprecated imports and mock schemasForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 25/100
AOSSIE-Org/Devr.AI#307 · 1 commento ·
Tutte le issue di AOSSIE-Org/Devr.AI
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno
-
enhancement good first issue
Difficoltà 1/5 1-3 ore Idoneità per principianti 89/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
smk762/argus-atlas#97 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
xdslproject/xdsl#6842 ·
I maintainer di solito rispondono entro 1 giorno