Fix: Properties Fetch Timeout Error ✅
Fix: Properties Fetch Timeout Error ✅
Problem
Users experiencing database timeout error when loading properties:
Error: {
code: '57014',
message: 'canceling statement due to statement timeout'
}
Root Causes
- Large Dataset: 1680+ properties being fetched at once
- Missing Indexes: Database performing full table scans
- Batching Overhead: Multiple sequential queries in a loop
- Statement Timeout: Default Supabase timeout too short for query
Solutions Implemented
1. ✅ Optimized fetchProperties Function
Changes Made:
- Removed batch fetching loop (was causing multiple slow queries)
- Changed to single optimized query with
.limit(5000) - Explicitly selected only needed columns (no
SELECT *) - Added 30-second client-side timeout with AbortController
- Better error handling for timeout scenarios
- Added fallback empty state if query fails
Before:
// Old approach - multiple queries in a loop
while (hasMore) {
const { data, error } = await supabase
.from('properties')
.select('*')
.range(from, from + batchSize - 1);
// ... accumulate results
}
After:
// New approach - single optimized query
const { data, error } = await supabase
.from('properties')
.select(`
id,
property_name,
address,
city,
state,
// ... specific columns
`)
.order('created_at', { ascending: false })
.limit(5000)
.abortSignal(abortController.signal);
2. ✅ Database Indexes (SQL Script)
Created database/fixes/FIX_PROPERTIES_TIMEOUT.sql with strategic indexes:
Critical Indexes:
idx_properties_created_at- For ORDER BY sorting ⭐ Most Importantidx_properties_name- For property name searchesidx_properties_city- For city filteringidx_properties_address- For address searchesidx_properties_status- For status filteringidx_properties_management_type- For management type filteringidx_properties_state_city- Composite index for common patternsidx_properties_pm_company_id- Foreign key lookupsidx_properties_owner_contact_id- Foreign key lookups
Run the SQL script:
# In Supabase SQL Editor, run:
# database/fixes/FIX_PROPERTIES_TIMEOUT.sql
3. ✅ Error Handling Improvements
Added specific error messages:
AbortError→ User-friendly timeout messageCode 57014→ Database timeout with support contact info- Graceful fallback to empty state
4. ✅ Performance Monitoring
Added console logging:
🔄 Starting property fetch...
✅ Fetched 1680 properties from Supabase
How to Apply the Fix
Step 1: Code Changes (Already Applied)
The optimized fetchProperties function is now in app/access-crm/page.tsx
Step 2: Database Indexes (YOU NEED TO RUN THIS)
- Open Supabase Dashboard
- Go to SQL Editor
- Copy contents of
database/fixes/FIX_PROPERTIES_TIMEOUT.sql - Run the script
- Verify indexes were created:
SELECT indexname, indexdef
FROM pg_indexes
WHERE tablename = 'properties';
Step 3: Test
- Refresh the application
- Navigate to Properties tab
- Check browser console for success message
- Properties should load within 2-5 seconds
Expected Performance Improvements
| Metric | Before | After | Improvement |
|---|---|---|---|
| Query Time | 60+ seconds (timeout) | 2-5 seconds | 92% faster |
| Number of Queries | Multiple (batch loop) | Single query | 1 query |
| Columns Selected | All (SELECT *) | Only needed | Smaller payload |
| Index Usage | Sequential scan | Index scan | Much faster |
Verification
Check if Indexes are Working
Run this in Supabase SQL Editor:
EXPLAIN ANALYZE
SELECT id, property_name, address, city, state,
created_at
FROM properties
ORDER BY created_at DESC
LIMIT 5000;
Expected Output:
- Should say:
Index Scan using idx_properties_created_at - Should NOT say:
Seq Scan on properties
Check Query Performance
SELECT
query,
calls,
total_time,
mean_time,
max_time
FROM pg_stat_statements
WHERE query LIKE '%properties%'
ORDER BY mean_time DESC
LIMIT 10;
Troubleshooting
If Timeout Still Occurs
-
Increase Supabase Statement Timeout:
- Go to Supabase Dashboard → Database Settings
- Increase statement timeout to 60 seconds
-
Check Index Creation:
SELECT indexname FROM pg_indexes WHERE tablename = 'properties';Should see all 9 indexes listed above
-
Reduce Limit: In
fetchProperties, change:.limit(5000) // Try .limit(2000) -
Add Pagination: Consider loading properties in pages rather than all at once
Alternative Solutions (If Still Slow)
Option A: Server-Side Caching
Add Redis cache to store property data for 5 minutes
Option B: Incremental Loading
Load initial 500 properties, then load more on scroll
Option C: Database Views
Create a materialized view for faster reads
Option D: Edge Functions
Move heavy queries to Supabase Edge Functions
Files Modified
- ✅
app/access-crm/page.tsx- Optimized fetchProperties function - ✅
database/fixes/FIX_PROPERTIES_TIMEOUT.sql- Database indexes script - ✅
docs/troubleshooting/FIX_PROPERTIES_TIMEOUT.md- This documentation
Status
- ✅ Code optimization complete
- ⚠️ Database indexes need to be run in Supabase
- ⏳ Testing required after index creation
Next Steps
- Run the SQL script in Supabase SQL Editor
- Test the application - refresh and navigate to Properties tab
- Monitor performance - check console logs
- Report back if timeout still occurs
Note: The most critical fix is creating the database indexes. The code optimization helps, but without indexes, large queries will always be slow.
Use links in each imported doc to open its source.