ServerDatabase
System Audits & Indexes
Database tables governing user-action security logs and performant relational / pgvector HNSW indices.
System Audits & Indexes
This module holds audit trail logging and defines critical database-level optimization indexes to facilitate high-speed query execution.
Migrated by: 00008_create_audit_logs_and_indexing.sql
1. Security Audit Logging
audit_logs
Saves low-level user interactions and security-sensitive event histories.
- Schema:
Column Type Constraints Default Description iduuidPRIMARY KEYgen_random_uuid()Audit log identifier. actor_user_iduuidREFERENCES users(id) ON DELETE SET NULL- User trigger ID. session_iduuidREFERENCES sessions(id) ON DELETE SET NULL- Active session context ID. actiontextNOT NULL- Action descriptor (e.g. 'auth.login','recipe.create').resource_typetextNOT NULL- Target resource group (e.g. 'recipes').resource_iduuid- - UUID of the targeted resource. ip_addressinet- - Request client IP address. user_agenttext- - Request client browser user-agent. metadatajsonbNOT NULL'{}'Context metadata tracking modified parameters. created_attimestamptzNOT NULLnow()Log creation timestamp.
2. Performance Indexes
The system registers specific database indexes to accelerate critical query lookup paths.
Identity & Authentication
idx_sessions_user_id: Accelerates looking up sessions for a user.idx_sessions_active: Filters active, non-expired sessions for a user:CREATE INDEX idx_sessions_active ON sessions(user_id, expires_at) WHERE revoked_at IS NULL;idx_oauth_accounts_user_id: Accelerates checking linked OAuth accounts.idx_otp_verifications_email: Quick lookup of the latest OTP code dispatched to an email:CREATE INDEX idx_otp_verifications_email ON otp_verifications(email, purpose, created_at DESC);
Profiles & Historical Logs
idx_user_goals_active: Speeds up fetching the user's active goals:CREATE INDEX idx_user_goals_active ON user_goals(user_id) WHERE is_active = true;idx_biometric_logs_user_date: Accelerates loading historical biometric weight/height charts.idx_workout_logs_user_date: Speeds up loading historical workout logs.
Recipes, Nutrition & Lists
idx_recipes_owner: Fetches user-created recipes.idx_recipe_food_items_food: Tracks which recipes contain a specific food item.idx_food_logs_user_date: Speeds up query processing of daily user food logs.idx_meal_plan_items_date: Speeds up loading scheduled meal calendar pages.idx_pantry_items_user: Speeds up pulling pantry stock lists.idx_shopping_list_items_list: Speeds up loading list items.
3. Vector HNSW Search Indices
Cosine Similarity indices are configured using pgvector's Hierarchical Navigable Small World (HNSW) algorithm to enable high-speed vector searches:
CREATE INDEX idx_recipe_embeddings_vector ON recipe_embeddings USING hnsw (embedding vector_cosine_ops);
CREATE INDEX idx_agent_memory_embeddings_vector ON agent_memory_embeddings USING hnsw (embedding vector_cosine_ops);
CREATE INDEX idx_food_item_embeddings_vector ON food_item_embeddings USING hnsw (embedding vector_cosine_ops);