Pathfinder Docs

Documentation Preview

Fix: Properties Fetch Timeout Error ✅

Source: `docs/operations/FIX_PROPERTIES_TIMEOUT.md`View on GitHub

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

  1. Large Dataset: 1680+ properties being fetched at once
  2. Missing Indexes: Database performing full table scans
  3. Batching Overhead: Multiple sequential queries in a loop
  4. 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:

  1. idx_properties_created_at - For ORDER BY sorting ⭐ Most Important
  2. idx_properties_name - For property name searches
  3. idx_properties_city - For city filtering
  4. idx_properties_address - For address searches
  5. idx_properties_status - For status filtering
  6. idx_properties_management_type - For management type filtering
  7. idx_properties_state_city - Composite index for common patterns
  8. idx_properties_pm_company_id - Foreign key lookups
  9. idx_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 message
  • Code 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)

  1. Open Supabase Dashboard
  2. Go to SQL Editor
  3. Copy contents of database/fixes/FIX_PROPERTIES_TIMEOUT.sql
  4. Run the script
  5. Verify indexes were created:
SELECT indexname, indexdef 
FROM pg_indexes 
WHERE tablename = 'properties';

Step 3: Test

  1. Refresh the application
  2. Navigate to Properties tab
  3. Check browser console for success message
  4. Properties should load within 2-5 seconds

Expected Performance Improvements

MetricBeforeAfterImprovement
Query Time60+ seconds (timeout)2-5 seconds92% faster
Number of QueriesMultiple (batch loop)Single query1 query
Columns SelectedAll (SELECT *)Only neededSmaller payload
Index UsageSequential scanIndex scanMuch 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

  1. Increase Supabase Statement Timeout:

    • Go to Supabase Dashboard → Database Settings
    • Increase statement timeout to 60 seconds
  2. Check Index Creation:

    SELECT indexname 
    FROM pg_indexes 
    WHERE tablename = 'properties';
    

    Should see all 9 indexes listed above

  3. Reduce Limit: In fetchProperties, change:

    .limit(5000)  // Try .limit(2000)
    
  4. 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

  1. Run the SQL script in Supabase SQL Editor
  2. Test the application - refresh and navigate to Properties tab
  3. Monitor performance - check console logs
  4. 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.