Knowledge > Runbooks > Technical Ops > Refresh Supabase Views and Cached Aggregates
Refresh Supabase Views and Cached Aggregates
Refresh stale data in Supabase materialized views or cached aggregates when query results are outdated.
Prerequisites
- Access to Supabase SQL editor or the
mcp__plugin_supabase_supabase__execute_sqlMCP tool - Knowledge of which view needs refreshing and why (stale data complaint, scheduled refresh)
Key Views in the System
| View | Type | Owner | Purpose |
|---|---|---|---|
dir_illustrations | Regular view | unified_rag_content | Read-only illustration snapshot for ITW |
Important: dir_illustrations is a regular view (not materialized) — it always reflects live data. Refreshing it is not necessary; if data appears stale, the underlying unified_rag_content table has not been updated.
Steps
-
Determine if the view is regular or materialized
SELECT schemaname, viewname, definitionFROM pg_viewsWHERE viewname = 'dir_illustrations';-- For materialized views:SELECT schemaname, matviewname, ispopulatedFROM pg_matviewsWHERE matviewname = 'your_view_name';- Regular view: always live — no refresh needed. If data is wrong, fix the underlying table.
- Materialized view: cached snapshot — requires explicit refresh.
-
Refresh a materialized view
For non-blocking refresh (allows reads during refresh):
REFRESH MATERIALIZED VIEW CONCURRENTLY your_view_name;Note:
CONCURRENTLYrequires a unique index on the materialized view. If none exists:REFRESH MATERIALIZED VIEW your_view_name;-- This locks reads during refresh — use only for small views or off-peak hours -
Verify the view returns fresh data
After refreshing, confirm the data is up to date:
-- Check record count matches expectedSELECT count(*) FROM your_view_name;-- Check most recent record timestampSELECT max(updated_at) FROM your_view_name;-- Compare to the source tableSELECT max(updated_at) FROM unified_rag_content WHERE content_type = 'illustration'; -
Investigate stale data in a regular view
If
dir_illustrationsreturns outdated content, the issue is inunified_rag_content:-- Check if the source data was updatedSELECT id, title, updated_at FROM unified_rag_contentWHERE content_type = 'illustration'ORDER BY updated_at DESC LIMIT 5;-- Check if the view definition is correctSELECT definition FROM pg_views WHERE viewname = 'dir_illustrations'; -
Schedule recurring refresh if needed
For materialized views that need regular refresh, use a Supabase cron job (via
pg_cronextension) or the existing ops cron infrastructure at/api/ops/collect(runs every 15 minutes).Example pg_cron entry (run in SQL editor):
-- Refresh every hour at :00SELECT cron.schedule('refresh-my-view', '0 * * * *', 'REFRESH MATERIALIZED VIEW CONCURRENTLY my_view'); -
Never write directly to
dir_illustrationsdir_illustrationsis a read-only view. Any attempt to INSERT, UPDATE, or DELETE through it will fail. Always write tounified_rag_contentdirectly.
Verification
SELECT count(*) FROM view_namereturns the expected row countSELECT max(updated_at) FROM view_namematches the source table's most recent update- The application layer (ITW illustration pages, chatbot RAG) returns fresh content