pg_fraiseql_cache
Automatic cache invalidation for PostgreSQL through domain-based versioning
Overview
pg_fraiseql_cache is a PostgreSQL extension that provides automatic, efficient, and reliable cache invalidation through domain-based versioning. It eliminates manual cache invalidation code, prevents stale data, and dramatically improves performance for batch operations.
Key Features:
- ✅ Automatic invalidation: Zero manual cache clearing code
- ✅ Batch-safe: 10k row update = 1 cache invalidation (not 10k!)
- ✅ Multi-tenant: Built-in tenant isolation
- ✅ Framework-agnostic: Works with any GraphQL server or REST API
- ✅ Multi-node: PostgreSQL NOTIFY/LISTEN for distributed cache coordination
- ✅ Production-proven: Extracted from battle-tested printoptim_backend
Quick Start
Installation
-- Install extension
CREATE EXTENSION pg_fraiseql_cache;
-- Setup automatic invalidation for a table
SELECT fraiseql_cache.setup_table_invalidation('tv_contract');
-- Add cascade rules (optional)
INSERT INTO fraiseql_cache.cascade_rules (source_domain, target_domain)
VALUES ('contract', 'item'), ('contract', 'price');
Usage (Python/FraiseQL)
from fraiseql import FraiseQL
from fraiseql.caching import PostgresCache
# FraiseQL automatically detects and uses pg_fraiseql_cache
app = FraiseQL(
db_url="postgresql://localhost/mydb",
cache_backend=PostgresCache(...)
)
# Write mutations - cache invalidation is automatic!
@app.mutation
async def update_contract(id: UUID, price: Decimal):
return await Contract.update(id, price=price)
# Extension handles:
# 1. Version increment for 'contract' domain
# 2. Cascade to 'item' and 'price' domains
# 3. NOTIFY all app nodes
# 4. Caches automatically invalidate
Monitoring
-- Check current domain versions
SELECT * FROM fraiseql_cache.v_domain_versions
WHERE tenant_id = 'abc-123';
-- See which tables have invalidation triggers
SELECT * FROM fraiseql_cache.v_invalidation_status;
-- View recent invalidation activity
SELECT * FROM fraiseql_cache.v_recent_invalidations;
Performance
Real-world benchmarks from production (printoptim_backend):
| Operation |
Without Extension |
With Extension |
Improvement |
| Single row update |
5ms |
5.1ms |
+0.1ms overhead |
| 100 row batch |
500ms |
50ms |
10x faster |
| 10k row batch |
50s |
5s |
10x faster |
| Cache hit rate |
60% |
95% |
1.5x better |
How It Works
Domain-Based Versioning
Each (tenant_id, domain) pair has a version number:
SELECT * FROM fraiseql_cache.domain_version;
tenant_id | domain | version | last_modified
-----------+----------+---------+----------------------------
abc-123 | contract | 42 | 2025-10-11 10:30:15+00
abc-123 | item | 156 | 2025-10-11 10:30:15+00
Flow:
- App caches query with current domain version
- Data changes → trigger increments version
- App checks:
cached_version < current_version → re-query
- New result cached with updated version
Batch Safety
Problem: Naive trigger on 10k row update = 10k invalidations
Solution: Statement-level deduplication
-- Update 10,000 rows
UPDATE contracts SET price = price * 1.1;
-- Result: Only 1 version increment (not 10,000!)
-- Uses statement_version_tracker to deduplicate
Cascade Invalidation
-- Configure cascades
INSERT INTO fraiseql_cache.cascade_rules (source_domain, target_domain)
VALUES ('contract', 'item'), ('contract', 'price');
-- Update contract
UPDATE contracts SET name = 'Updated' WHERE id = 1;
-- Result: Automatically invalidates contract, item, AND price domains
Multi-Node Coordination
-- Extension sends PostgreSQL NOTIFY on version increment
-- All app nodes listen and invalidate their local caches
LISTEN cache_invalidation;
-- Receive: {"tenant_id": "abc-123", "domain": "contract", "version": 43}
Use Cases
1. FraiseQL (GraphQL Framework)
Auto-detects extension and uses domain versioning for all cached queries.
2. PostGraphile / Hasura
Add triggers to your tables, check domain versions before serving cache.
3. REST APIs
Use domain versioning for any cached endpoints.
4. Batch Operations
Efficiently handle bulk updates without cache invalidation overhead.
Documentation
Development
Requirements
- PostgreSQL 14+ (tested on 14, 15, 16, 17)
- pgTAP (for unit tests)
- Python 3.11+ with psycopg3 (for integration tests)
Setup
# Install extension locally
make install
# Run unit tests
make test-unit
# Run integration tests
make test-integration
# Run all tests
make test
# Run benchmarks
make benchmark
Testing PostgreSQL Versions
# Test on specific version
PG_VERSION=14 make test
# Test all supported versions
make test-all-versions
Project Status
Current Version: 1.0.0
Status: Production Ready ✅
- ✅ Phase 1: Core Infrastructure (COMPLETE)
- ✅ Phase 2: Advanced Features (COMPLETE)
- ✅ Phase 3: Polish & Tooling (COMPLETE)
- 🎯 Phase 4: FraiseQL Integration (PLANNED)
See STATUS.md for detailed progress and metrics.
Architecture
Core Components
- Domain Version Table - Tracks version per (tenant_id, domain)
- Statement Tracker - Prevents duplicate increments in batch operations
- Trigger Function - Automatically increments versions on data changes
- Cascade Rules - Propagates invalidations to related domains
- NOTIFY Integration - Coordinates multi-node cache invalidation
Schema
fraiseql_cache.domain_version
- tenant_id (UUID)
- domain (TEXT)
- version (BIGINT)
- last_modified (TIMESTAMPTZ)
fraiseql_cache.statement_version_tracker (UNLOGGED)
- backend_pid (INT)
- statement_timestamp (TIMESTAMPTZ)
- tenant_id (UUID)
- domain (TEXT)
- version_incremented (BOOLEAN)
fraiseql_cache.cascade_rules
- source_domain (TEXT)
- target_domain (TEXT)
- rule_type (TEXT)
Examples
See examples/ directory for:
- FraiseQL integration example
- PostGraphile integration example
- REST API usage example
- Batch operation patterns
Contributing
We follow a strict Phased TDD approach:
- RED: Write failing test
- GREEN: Minimal implementation
- REFACTOR: Clean up and optimize
- QA: Verify completion
See CLAUDE.md for detailed development guidelines.
Related Projects
- FraiseQL - GraphQL framework with automatic cache invalidation
- pg_jsonb_ivm - Incremental JSONB view maintenance (companion extension)
License
MIT License - see LICENSE file for details
Acknowledgments
- Extracted from production battle-tested code in printoptim_backend
- Inspired by PostgreSQL IVM research and Redis keyspace notifications
- Built with guidance from the PostgreSQL extension development community
Built with ❤️ for the PostgreSQL and GraphQL communities