Overview
The PostgreSQL persistence adapter provides distributed locks, circuit breaker state management, caching, and checkpoint support using PostgreSQL as the backend. It uses application-level locks with TTL tracking and automatic table creation.Installation
Features
- Distributed Locks: Application-level locks with TTL and automatic expiration
- Circuit Breaker State: Persistent failure tracking with JSONB storage
- Caching: Database-backed cache with TTL support
- Checkpoints: Full checkpoint support for task recovery
- Idempotency: Prevent duplicate operations (via
PostgresIdempotencyAdapter) - Auto Schema: Automatic table creation on connect
- Transactional Safety: ACID guarantees for lock operations
Basic Usage
Configuration
pg Pool instance for PostgreSQL connections
Configuration options
Database Schema
The adapter automatically creates the following tables onconnect():
go_goscope_locks
go_goscope_circuit
go_goscope_cache
go_goscope_checkpoints
Lock Provider Methods
acquire(key, ttl, owner?)
Acquires a distributed lock with automatic TTL-based expiration. Uses transactions withFOR UPDATE for strong consistency.
Lock identifier
Time-to-live in milliseconds
Optional lock owner identifier (auto-generated if not provided)
Promise<LockHandle | null> - Lock handle or null if already locked
extend(key, ttl, owner)
Extends the TTL of an existing lock.Lock identifier
New time-to-live in milliseconds
Lock owner identifier
Promise<boolean> - True if extended successfully
forceRelease(key)
Forces release of a lock regardless of owner.Lock identifier
Cache Provider Methods
get(key)
Retrieves a cached value using JSONB storage.Cache key
Promise<T | null> - Cached value or null if not found/expired
set(key, value, ttl?)
Stores a value in the cache using JSONB.Cache key
Value to cache (must be JSON-serializable)
Time-to-live in milliseconds (optional)
delete(key)
Removes a cached value.Cache key
clear()
Clears all cached values (respects key prefix if set).keys(pattern?)
Lists all cache keys matching an optional pattern.Optional SQL LIKE pattern (e.g.,
'user:%')Promise<string[]> - Array of matching keys
Checkpoint Provider Methods
save(checkpoint)
Saves a checkpoint for task recovery.Checkpoint data including taskId, sequence, progress, and data
load(checkpointId)
Loads a specific checkpoint by ID.Checkpoint identifier
Promise<Checkpoint<T> | undefined>
loadLatest(taskId)
Loads the most recent checkpoint for a task.Task identifier
Promise<Checkpoint<T> | undefined>
list(taskId)
Lists all checkpoints for a task.Task identifier
Promise<Checkpoint<unknown>[]>
cleanup(taskId, keepCount)
Deletes old checkpoints, keeping only the most recent N.Task identifier
Number of checkpoints to keep
deleteAll(taskId)
Deletes all checkpoints for a task.Task identifier
Connection Example
Best Practices
Lock acquisition uses
FOR UPDATE which provides strong consistency but may serialize concurrent lock attempts. This is ideal for critical sections.Performance Considerations
- Lock operations use transactions with row-level locking
- Expired locks are cleaned up on each
acquire()call - JSONB indexes recommended for cache queries on large datasets
- Connection pooling is handled by the
pgPool
Related
Redis Adapter
Redis-based persistence
MySQL Adapter
MySQL-based persistence