Skip to main content
Version: 1.1.0
Date: 2026-01-17 (Updated)
Status: ✅ Phase 1 Complete
Database: PostgreSQL 15 (Supabase)

Executive Summary

This document reviews currently installed PostgreSQL extensions and provides recommendations for additional extensions that would benefit the Encore OS platform based on:
  1. Current extension usage patterns
  2. Codebase analysis (full-text search, ILIKE queries, JSONB operations)
  3. Multi-tenant healthcare ERP requirements
  4. Performance optimization needs
Key Findings:
  • 10 extensions currently installed (pgcrypto, pg_stat_statements, pg_net, pg_cron, uuid-ossp, pg_graphql, pgmq, pg_trgm, unaccent, btree_gin)
  • 3 high-priority extensions enabled (pg_trgm, unaccent, btree_gin) - Phase 1 Complete
  • 📊 2 medium-priority extensions for future optimization (pg_partman, pg_jsonschema)
  • 🔍 Full-text search patterns optimized - trigram indexes added

Currently Installed Extensions

Total: 11 extensions installed

High-Priority Recommendations

1. pg_trgm - Trigram Text Search ⭐⭐⭐

Priority: HIGH
Impact: Performance improvement for text search
Effort: Low (enable extension, add indexes)
Why:
  • Current Pattern: Extensive use of ILIKE '%pattern%' queries (found 25+ instances)
  • Problem: ILIKE with leading wildcards cannot use indexes efficiently
  • Solution: Trigram indexes enable fast fuzzy text matching
Usage Patterns Found:
Tables That Would Benefit:
  • hr_employees (name, job_title searches)
  • hr_positions (title searches)
  • pf_documents (title, content searches)
  • fw_workflow_templates (name, description searches)
  • it_knowledge_base_articles (title, content searches)
  • All tables with full-text search indexes
Implementation:
Performance Impact:
  • Before: Sequential scan on ILIKE queries (slow on large tables)
  • After: Index scan with trigram matching (10-100x faster)
  • Storage: ~30% of indexed column size (acceptable trade-off)
Migration Path:
  1. Enable extension in migration
  2. Add trigram indexes for high-traffic search columns
  3. Update search queries to use % operator or similarity() function
  4. Monitor query performance improvement

2. unaccent - Accent-Insensitive Search ⭐⭐⭐

Priority: HIGH
Impact: Better user experience for name searches
Effort: Low (enable extension, update indexes)
Why:
  • Healthcare Context: Names often contain accents (José, François, etc.)
  • Current Problem: ILIKE searches won’t match “Jose” when searching for “José”
  • Solution: Unaccent extension removes accents for search
Usage Pattern:
Tables That Would Benefit:
  • hr_employees (first_name, last_name)
  • pf_profiles (first_name, last_name)
  • rh_residents (first_name, last_name)
  • Any table with person names
Performance Impact:
  • Minimal overhead (unaccent is fast)
  • Significantly better search results for international names

3. btree_gin - GIN Indexes for Standard Types ⭐⭐

Priority: HIGH
Impact: Better JSONB query performance
Effort: Low (enable extension, add indexes)
Why:
  • Current Pattern: 363 tables with custom_fields JSONB columns
  • Problem: Standard GIN indexes on JSONB can be large
  • Solution: btree_gin allows composite indexes mixing JSONB with other types
Usage Pattern:
Performance Impact:
  • Faster queries filtering by organization_id + custom_fields
  • Smaller indexes than separate GIN indexes
  • Better query planning for multi-tenant queries

4. pg_partman - Table Partitioning ⭐⭐

Priority: HIGH (Future)
Impact: Performance for large audit/log tables
Effort: Medium (requires planning and migration)
Why:
  • Data Archival Plan: Identified need to archive old audit logs, activity logs
  • Problem: Large tables (audit logs, workflow executions) will slow down over time
  • Solution: Partition tables by date for easier archival and better query performance
Tables That Would Benefit:
  • pf_audit_logs (partition by created_at monthly)
  • pf_activity_logs (partition by created_at monthly)
  • fw_workflow_executions (partition by created_at monthly)
  • pf_notifications (partition by created_at monthly)
Implementation:
Performance Impact:
  • Query Performance: Faster queries on recent data (only scan relevant partitions)
  • Maintenance: Easy to drop old partitions (vs. DELETE operations)
  • Storage: Better compression and index efficiency per partition
Migration Path:
  1. Review data archival plan (docs/migration/DATA_ARCHIVAL_PLAN.md)
  2. Plan partition strategy (monthly vs. quarterly)
  3. Test partitioning on staging environment
  4. Migrate production tables during maintenance window
Note: This is a future optimization - implement after monitoring shows performance issues with large tables.

5. pg_jsonschema - JSONB Validation ⭐

Priority: MEDIUM
Impact: Data quality for custom_fields
Effort: Low (enable extension, add check constraints)
Why:
  • Current Pattern: 363 tables with custom_fields JSONB DEFAULT '{}'
  • Problem: No validation of JSONB structure (could store invalid data)
  • Solution: Validate custom_fields against JSON Schema
Usage Pattern:
Performance Impact:
  • Minimal (validation only on INSERT/UPDATE)
  • Prevents invalid data from being stored
  • Better data quality for reporting
Consideration:
  • May be too restrictive if custom_fields structure varies significantly
  • Consider making validation optional per organization

Medium-Priority Recommendations

6. citext - Case-Insensitive Text Type

Priority: MEDIUM
Impact: Simpler queries (no need for LOWER()/UPPER())
Effort: Low (enable extension, migrate columns if desired)
Why:
  • Many queries use LOWER(column) = LOWER(value) or ILIKE
  • citext type handles case-insensitivity at the type level
Consideration:
  • Not recommended for existing tables (would require column type changes)
  • Consider for new tables where case-insensitivity is required
  • Current ILIKE pattern is acceptable and doesn’t require migration
Usage:

7. ltree - Hierarchical Tree Structures

Priority: MEDIUM (If Needed)
Impact: Efficient tree queries
Effort: Medium (requires schema changes)
Why:
  • Could be useful for organizational hierarchies, category trees
  • More efficient than recursive CTEs for tree queries
Consideration:
  • Only if tree structures are needed
  • Current patterns don’t show hierarchical data requirements
  • Defer until needed

8. tcn - Triggered Change Notifications

Priority: MEDIUM
Impact: Event-driven workflows
Effort: Low (enable extension, add triggers)
Why:
  • Current Pattern: Event-based integration for workflows (FW-16)
  • Problem: Manual event publishing via triggers
  • Solution: Automatic change notifications via tcn extension
Usage Pattern:
Consideration:
  • May duplicate existing event publishing logic
  • Review FW-16 event integration patterns before implementing
  • Defer until event system review

Extensions to Avoid


Implementation Plan

Phase 1: Immediate (Week 1-2) ✅ COMPLETE

Priority Extensions (All Enabled 2026-01-17):
  1. pg_trgm - Enabled with trigram indexes for high-traffic search columns
  2. unaccent - Enabled with unaccented search indexes for name columns
  3. btree_gin - Enabled for composite index optimization
Migration:
Testing:
  • Verify search performance improvement
  • Test accent-insensitive searches
  • Monitor index sizes

Phase 2: Optimization (Month 2-3)

Conditional Extensions: 4. pg_jsonschema - If custom_fields validation is needed 5. pg_partman - If audit/log tables exceed 1M rows Decision Criteria:
  • Monitor table sizes monthly
  • Review data archival plan progress
  • Assess custom_fields data quality issues

Phase 3: Future (As Needed)

Deferred Extensions: 6. citext - For new tables requiring case-insensitive text 7. ltree - If hierarchical data structures are needed 8. tcn - If event system review recommends it

Migration Script Template


Performance Monitoring

Before/After Metrics

Track these metrics after enabling extensions:
  1. Search Query Performance:
  2. Index Sizes:
  3. Index Usage:

Rollback Plan

If extensions cause issues:
Note: Extensions themselves don’t break existing functionality - only new indexes/queries use them.

References

Project Documentation

  • Performance Recommendations: docs/migration/SUPABASE_PERFORMANCE_RECOMMENDATIONS.md
  • Data Archival Plan: docs/migration/DATA_ARCHIVAL_PLAN.md
  • Search Framework: specs/pf/specs/PF-57-search-filter-sort-framework.md
  • Database Schema: docs/architecture/DATABASE_SCHEMA.md

External Resources


Summary

Recommended Extensions (Priority Order):
  1. pg_trgm - HIGH - Fast fuzzy text search (replaces slow ILIKE)
  2. unaccent - HIGH - Accent-insensitive name searches
  3. btree_gin - HIGH - Composite indexes for JSONB + standard types
  4. pg_partman - HIGH (Future) - Table partitioning for large audit logs
  5. pg_jsonschema - MEDIUM - JSONB validation (if needed)
Total New Extensions: 3-5 (depending on future needs) Expected Impact:
  • Search Performance: 10-100x faster text searches
  • User Experience: Better search results (accent-insensitive, fuzzy matching)
  • Database Performance: Better index utilization, smaller query times

Last Updated: 2026-01-16
Status: Ready for Implementation
Next Steps: Create migration script for Phase 1 extensions