| import psycopg2
|
| import os
|
| from dotenv import load_dotenv
|
|
|
|
|
| load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
|
|
| DB_HOST = "aws-1-ap-south-1.pooler.supabase.com"
|
|
|
|
|
|
|
|
|
| DB_USER = "postgres.erecrmjorkafmqwspytb"
|
|
|
|
|
| DB_PASS = "$Kanishka20"
|
|
|
|
|
|
|
|
|
| DB_PORT = 5432
|
|
|
|
|
|
|
| def build_index():
|
| conn = None
|
| try:
|
| print(f"🔌 Connecting to {DB_HOST} on Port {DB_PORT}...")
|
|
|
| conn = psycopg2.connect(
|
| host=DB_HOST,
|
| database="postgres",
|
| user=DB_USER,
|
| password=DB_PASS,
|
| port=DB_PORT
|
| )
|
| conn.autocommit = True
|
| cur = conn.cursor()
|
|
|
| print("🚀 Connection successful!")
|
|
|
|
|
| print("⚙️ Step 1: Disabling timeouts...")
|
| cur.execute("SET statement_timeout = 0;")
|
|
|
|
|
|
|
| print("⚙️ Step 2: Boosting memory to 150MB...")
|
| cur.execute("SET maintenance_work_mem = '150MB';")
|
|
|
|
|
| print("🧹 Step 3: Cleaning up old indexes...")
|
| cur.execute("DROP INDEX IF EXISTS child_vectors_embedding_idx;")
|
|
|
|
|
| print("🏗️ Step 4: Building IVFFlat Index (lists=100)...")
|
| print(" (This will take 1-3 minutes. Please wait...)")
|
|
|
|
|
| cur.execute("""
|
| CREATE INDEX child_vectors_embedding_idx
|
| ON child_vectors
|
| USING ivfflat (embedding vector_cosine_ops)
|
| WITH (lists = 100);
|
| """)
|
|
|
| print("✅ SUCCESS! Index built. Your backend should now be instant.")
|
|
|
| except Exception as e:
|
| print(f"\n❌ ERROR: {e}")
|
| print("Tip: Double check you copied the 'Pooler' Host and User correctly from Supabase Settings.")
|
|
|
| finally:
|
| if conn:
|
| conn.close()
|
| print("🔌 Connection closed.")
|
|
|
| if __name__ == "__main__":
|
| build_index() |