[ST-4.1] feat: Add optimized partial index for auth.users.locked_until

- Create migration 2026-02-03_add_auth_locked_index.sql
- Add partial index on locked_until with active lockout condition
- Replace previous generic locked_until index with optimized version
- Index filters for locked_until IS NOT NULL AND locked_until > NOW()
- Improves query performance for account unlock validation
- Resolves GAP-009

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Adrian Flores Cortes 2026-02-03 23:56:26 -06:00
parent 6da2786590
commit 82d6e608b1
4 changed files with 58 additions and 1 deletions

View File

@ -73,7 +73,7 @@ CREATE INDEX idx_users_role ON auth.users(role);
CREATE INDEX idx_users_last_login ON auth.users(last_login_at DESC);
CREATE INDEX idx_users_created_at ON auth.users(created_at DESC);
CREATE INDEX idx_users_email_verified ON auth.users(email_verified) WHERE email_verified = false;
CREATE INDEX idx_users_locked ON auth.users(locked_until) WHERE locked_until IS NOT NULL;
CREATE INDEX idx_users_locked_until ON auth.users(locked_until) WHERE locked_until IS NOT NULL AND locked_until > NOW();
CREATE INDEX idx_users_phone ON auth.users(phone_number) WHERE phone_number IS NOT NULL;
-- Table Comments

View File

@ -53,6 +53,7 @@ CREATE INDEX idx_conversations_active ON llm.conversations(user_id, last_message
WHERE status = 'active';
CREATE INDEX idx_conversations_tags ON llm.conversations USING GIN(tags);
CREATE INDEX idx_conversations_symbols ON llm.conversations USING GIN(related_symbols);
CREATE INDEX idx_conversations_topics ON llm.conversations USING GIN(related_topics);
-- Comentarios
COMMENT ON TABLE llm.conversations IS 'Chat conversations between users and LLM agent';

View File

@ -0,0 +1,29 @@
-- ============================================================================
-- Migration: Add index on locked_until for account lockout queries
-- Task: ST-4.1, GAP-009
-- Description: Creates a more optimized partial index for active account
-- lockouts, improving query performance on account unlock checks
-- ============================================================================
-- Drop the previous index if it exists (less specific)
DROP INDEX IF EXISTS auth.idx_users_locked CASCADE;
-- Create optimized partial index for active lockouts
CREATE INDEX idx_users_locked_until
ON auth.users(locked_until)
WHERE locked_until IS NOT NULL AND locked_until > NOW();
-- Add index comment
COMMENT ON INDEX auth.idx_users_locked_until IS 'Partial index for active account lockouts - optimized for unlock validation queries';
-- ============================================================================
-- Verification
-- ============================================================================
-- This index improves queries like:
-- SELECT id, email FROM auth.users WHERE locked_until > NOW();
--
-- Benefits:
-- 1. Smaller index size (only rows with active locks)
-- 2. Faster scans for checking unlocked accounts
-- 3. Automatic pruning as locks expire
-- ============================================================================

View File

@ -0,0 +1,27 @@
-- =====================================================
-- Migration: Add GIN indexes for llm schema
-- Task: ST-4.4, GAP-012
-- Date: 2026-02-03
-- Description: Add GIN indexes for array columns in llm.conversations
-- for optimized full-text tag searches
-- =====================================================
-- GIN index on tags array for fast tag searches
CREATE INDEX IF NOT EXISTS idx_conversations_tags_gin
ON llm.conversations USING GIN(tags);
-- GIN index on related_symbols for symbol searches
CREATE INDEX IF NOT EXISTS idx_conversations_symbols_gin
ON llm.conversations USING GIN(related_symbols);
-- GIN index on related_topics for topic searches
CREATE INDEX IF NOT EXISTS idx_conversations_topics_gin
ON llm.conversations USING GIN(related_topics);
-- =====================================================
-- Verification
-- =====================================================
-- To verify indexes were created:
-- SELECT indexname FROM pg_indexes
-- WHERE tablename='conversations' AND schemaname='llm';
-- =====================================================