# Database Issues Troubleshooting Guide **Last Updated**: 2025-10-22 --- ## Connection Issues ### Symptom: `could not connect to server` **Diagnosis**: ```bash # Check PostgreSQL is running kubectl get pods -n foxhunt -l app=postgresql # Check service endpoint kubectl get endpoints -n foxhunt foxhunt-postgresql # Test connectivity kubectl run -it --rm psql-test --image=postgres:15 --restart=Never -- \ psql -h foxhunt-postgresql.foxhunt.svc.cluster.local -U foxhunt -d foxhunt ``` **Resolution**: ```bash # Restart PostgreSQL kubectl delete pod foxhunt-postgresql-0 -n foxhunt # Verify secrets kubectl get secret foxhunt-secrets -n foxhunt -o yaml ``` --- ## Slow Queries ### Symptom: Queries taking >1s **Diagnosis**: ```sql -- Find slow queries SELECT query, mean_exec_time, calls FROM pg_stat_statements ORDER BY mean_exec_time DESC LIMIT 10; -- Analyze query plan EXPLAIN ANALYZE SELECT * FROM orders WHERE symbol='ES.FUT' ORDER BY created_at DESC LIMIT 100; ``` **Resolution**: ```sql -- Create index CREATE INDEX CONCURRENTLY idx_orders_symbol_created_at ON orders(symbol, created_at DESC); -- Update statistics ANALYZE orders; -- Increase work_mem for complex queries SET work_mem = '256MB'; ``` --- ## Deadlocks ### Symptom: `deadlock detected` **Diagnosis**: ```sql -- Check deadlock log SELECT * FROM pg_stat_activity WHERE wait_event_type = 'Lock'; -- Enable deadlock logging ALTER SYSTEM SET deadlock_timeout = '1s'; ALTER SYSTEM SET log_lock_waits = on; SELECT pg_reload_conf(); ``` **Resolution**: ```sql -- Access tables in consistent order BEGIN; SELECT * FROM orders WHERE id = 1 FOR UPDATE; -- Always lock orders first SELECT * FROM positions WHERE order_id = 1 FOR UPDATE; -- Then positions COMMIT; ``` --- **End of Database Issues Troubleshooting Guide**