How microsoft/pg_durable works
Purpose and Runtime
pg_durable is a Rust/pgrx PostgreSQL extension for SQL-authored, long-running workflows. It persists workflow graphs, progress, results, and runtime history in PostgreSQL so work can resume after failures or restarts. PostgreSQL 17 and 18 are supported; the extension must be preloaded and its in-process background worker executes instances.
Core extension entry points are src/lib.rs (pgrx SQL exports and extension lifecycle), src/worker.rs (background worker), and pg_durable.control (PostgreSQL extension metadata).
SQL Authoring and Public State
The df SQL surface lets callers compose graph nodes—SQL, sequencing, parallelism, conditions, loops, scheduling, signals, and HTTP—then launch a graph with df.start(). The graph DSL and SQL-visible types are implemented in src/dsl.rs and src/types.rs; lifecycle, instance/node status, and durable state exposure are rooted in src/lib.rs.
The extension’s application-facing persistence boundary is df.* (notably instances, nodes, and per-owner variables). SQL install and upgrade scripts define this schema and compatibility path: sql/pg_durable--0.2.2.sql, sql/pg_durable--0.2.5--0.2.6.sql, and sql/00_init.sql.
Launch-to-Execution Flow
1. A client invokes DSL functions and df.start, creating an instance and graph state transactionally.
2. The worker discovers runnable durable work and hosts the duroxide orchestration runtime inside the PostgreSQL server.
3. The function-graph orchestrator loads graph structure, evaluates orchestration progression deterministically, and schedules activities: src/orchestrations/execute_function_graph.rs, src/worker.rs.
4. Each activity runs independently, updates checkpointed node/instance status, and records results. A failure or database restart replays from durable state rather than rerunning already completed nodes.
The runtime persistence boundary is the dedicated duroxide.* schema, managed by the external duroxide-pg state provider. The extension relies on external Rust libraries duroxide (orchestration/replay/timers) and duroxide-pg (PostgreSQL-backed history and queues), declared through Cargo.toml.
Activity Execution Boundary
Activities are the execution adapters between graph semantics and effects:
• SQL steps execute within PostgreSQL through src/activities/execute_sql.rs.
• HTTP and multipart calls execute through src/activities/execute_http.rs and src/activities/execute_multipart.rs.
• Graph loading and status transitions are isolated in src/activities/load_function_graph.rs, src/activities/update_node_status.rs, and src/activities/update_instance_status.rs.
This separation makes orchestration deterministic while treating SQL and network calls as durable, retryable activities. HTTP responses are normalized in src/activities/http_response.rs.
Security and Multi-Tenant Boundaries
The worker role is a superuser and bypasses RLS to manage all work; ordinary users are constrained to their own df instances, nodes, and variables. SQL grants/RLS policy are installed with the extension scripts, while runtime-facing authorization and registry behavior reside in src/registry.rs and src/client.rs.
Outbound HTTP is an explicit trust boundary. SSRF and egress policy enforcement live in src/ssrf.rs; deployments may build with restricted allowlists. External HTTP endpoints—including Azure Functions and Azure services in examples—are optional workflow dependencies, not required platform infrastructure.
Observability and Operations
Users inspect execution state and results through PostgreSQL tables and explain/monitoring APIs rather than a separate control plane. src/monitoring.rs, src/explain.rs, and src/node_status.rs implement this visibility layer.
Operational deployment is PostgreSQL plus the extension: development uses pgrx scripts such as scripts/pg-start.sh; packaged releases provide Debian and Docker artifacts via Dockerfile.release and docker-compose.yml. A configured database, normally postgres, is the worker’s target and must align with extension installation.
Key Design Constraints
Workflows are intentionally SQL-shaped: control flow is graph-based and effects should be SQL or HTTP. Arbitrary application logic must be encapsulated in a SQL function or external HTTP service. Durability depends on PostgreSQL persistence and the worker/runtime state schemas, not external queues, cron, or an application worker fleet.
Main entry points: pg_durable--0.2.2.sql df schema · execute_function_graph.rs Function graph orchestrator · worker.rs Postgres background worker · lib.rs pgrx extension
Components 19
SQL authoring & state
- SQL clients · Authors and starts workflows through df.
- Graph DSL · Composes SQL, control-flow, signal, schedule, and HTTP graph nodes.
src/dsl.rs - pgrx extension · Exports SQL functions and manages extension lifecycle.
src/lib.rs - df schema · Stores instances, nodes, variables, graph state, and exposed status.
sql/pg_durable--0.2.2.sql - Install & upgrades · Defines schema compatibility, grants, and RLS policies.
sql/pg_durable--0.2.5--0.2.6.sql
Durable runtime
- Postgres background worker · Discovers runnable instances and hosts the orchestration runtime.
src/worker.rs - Function graph orchestrator · Loads graph progression and schedules replay-safe activities.
src/orchestrations/execute_function_graph.rs - duroxide runtime · Provides orchestration replay and timers.
Cargo.toml - duroxide schema · PostgreSQL-backed history and work queues managed by duroxide-pg.
Activity adapters
- Load graph activity
src/activities/load_function_graph.rs - Status activities · Checkpoints node and instance transitions.
src/activities/update_node_status.rs - SQL execution activity · Runs workflow SQL inside PostgreSQL.
src/activities/execute_sql.rs - HTTP activities · Executes HTTP and multipart effects with normalized responses.
src/activities/execute_http.rs - External HTTP services · Workflow-provided endpoints, including optional Azure services.
Security & visibility
- Egress policy · Applies outbound HTTP allowlist and trust-boundary checks.
src/ssrf.rs - Authorization & registry · Ordinary users are scoped to owned df state; worker bypasses RLS.
src/registry.rs - Explain & monitoring · Exposes execution state and results through PostgreSQL.
src/monitoring.rs
Deployment
- PostgreSQL server · Hosts the extension, schemas, worker, and workflow effects.
- Release packaging · Provides Docker and Debian-oriented deployment artifacts.
Dockerfile.release
Connections 22
- SQL clients → Graph DSL: compose graph
- Graph DSL → pgrx extension: SQL exports
- pgrx extension → df schema: df.start persists instance
- Install & upgrades → df schema: creates and upgrades
- Install & upgrades → Authorization & registry: installs grants and RLS
- pgrx extension → Postgres background worker: registers lifecycle
- Postgres background worker → Function graph orchestrator: runs instances
- Postgres background worker → duroxide runtime: hosts runtime
- duroxide runtime → duroxide schema: persists history and queues
- Function graph orchestrator → Load graph activity: loads structure
- Load graph activity → df schema: reads graph state
- Function graph orchestrator → SQL execution activity: schedules effects
- Function graph orchestrator → HTTP activities: schedules effects
- Function graph orchestrator → Status activities: checkpoints progress
- Status activities → df schema: updates statuses and results
- SQL execution activity → PostgreSQL server: executes SQL
- HTTP activities → Egress policy: enforces egress policy
- Egress policy → External HTTP services: permits requests
- Authorization & registry → df schema: scopes user access
- Explain & monitoring → df schema: inspects state
- Postgres background worker → PostgreSQL server: runs in server
- Release packaging → PostgreSQL server: deploys extension