# microsoft/pg_durable architecture

> Architecture diagram of the GitHub repository microsoft/pg_durable, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/microsoft/pg_durable
- Repository: https://github.com/microsoft/pg_durable

## Overview

#### 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: [`sql/pg_durable--0.2.2.sql`](https://github.com/microsoft/pg_durable/blob/main/sql/pg_durable--0.2.2.sql) (df schema), [`src/orchestrations/execute_function_graph.rs`](https://github.com/microsoft/pg_durable/blob/main/src/orchestrations/execute_function_graph.rs) (Function graph orchestrator), [`src/worker.rs`](https://github.com/microsoft/pg_durable/blob/main/src/worker.rs) (Postgres background worker), [`src/lib.rs`](https://github.com/microsoft/pg_durable/blob/main/src/lib.rs) (pgrx extension)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_sql["SQL authoring &amp; state"]
  node_clients(("SQL clients<br/>caller"))
  node_dsl["Graph DSL<br/>SQL API<br/>[dsl.rs]"]
  node_extension{{"pgrx extension<br/>extension entrypoint<br/>[lib.rs]"}}
  node_df_state[("df schema<br/>application persistence")]
  node_sql_migrations["Install &amp; upgrades<br/>SQL scripts"]
end

subgraph group_runtime["Durable runtime"]
  node_worker["Postgres background worker<br/>in-process worker<br/>[worker.rs]"]
  node_orchestrator["Function graph orchestrator<br/>deterministic orchestrator"]
  node_duroxide["duroxide runtime<br/>external Rust library<br/>[Cargo.toml]"]
  node_duroxide_state[("duroxide schema<br/>runtime persistence")]
end

subgraph group_activities["Activity adapters"]
  node_load_graph["Load graph activity"]
  node_status_activities["Status activities<br/>activity"]
  node_sql_activity["SQL execution activity<br/>activity adapter<br/>[execute_sql.rs]"]
  node_http_activity["HTTP activities<br/>activity adapter<br/>[execute_http.rs]"]
  node_external_http(("External HTTP services<br/>optional dependency"))
end

subgraph group_security["Security &amp; visibility"]
  node_ssrf["Egress policy<br/>SSRF enforcement<br/>[ssrf.rs]"]
  node_access["Authorization &amp; registry<br/>access boundary<br/>[registry.rs]"]
  node_observability["Explain &amp; monitoring<br/>SQL visibility API<br/>[monitoring.rs]"]
end

subgraph group_operations["Deployment"]
  node_postgres[("PostgreSQL server<br/>database runtime")]
  node_release["Release packaging<br/>deployment artifacts<br/>[Dockerfile.release]"]
end

node_clients -->|"compose graph"| node_dsl
node_dsl -->|"SQL exports"| node_extension
node_extension -->|"df.start persists instance"| node_df_state
node_sql_migrations -->|"creates and upgrades"| node_df_state
node_sql_migrations -->|"installs grants and RLS"| node_access
node_extension -->|"registers lifecycle"| node_worker
node_worker -->|"runs instances"| node_orchestrator
node_worker -->|"hosts runtime"| node_duroxide
node_duroxide -->|"persists history and queues"| node_duroxide_state
node_orchestrator -->|"loads structure"| node_load_graph
node_load_graph -->|"reads graph state"| node_df_state
node_orchestrator -->|"schedules effects"| node_sql_activity
node_orchestrator -->|"schedules effects"| node_http_activity
node_orchestrator -->|"checkpoints progress"| node_status_activities
node_status_activities -->|"updates statuses and results"| node_df_state
node_sql_activity -->|"executes SQL"| node_postgres
node_http_activity -->|"enforces egress policy"| node_ssrf
node_ssrf -->|"permits requests"| node_external_http
node_access -->|"scopes user access"| node_df_state
node_observability -->|"inspects state"| node_df_state
node_worker -->|"runs in server"| node_postgres
node_release -.->|"deploys extension"| node_postgres

click node_dsl "https://github.com/microsoft/pg_durable/blob/main/src/dsl.rs"
click node_extension "https://github.com/microsoft/pg_durable/blob/main/src/lib.rs"
click node_df_state "https://github.com/microsoft/pg_durable/blob/main/sql/pg_durable--0.2.2.sql"
click node_sql_migrations "https://github.com/microsoft/pg_durable/blob/main/sql/pg_durable--0.2.5--0.2.6.sql"
click node_worker "https://github.com/microsoft/pg_durable/blob/main/src/worker.rs"
click node_orchestrator "https://github.com/microsoft/pg_durable/blob/main/src/orchestrations/execute_function_graph.rs"
click node_duroxide "https://github.com/microsoft/pg_durable/blob/main/Cargo.toml"
click node_load_graph "https://github.com/microsoft/pg_durable/blob/main/src/activities/load_function_graph.rs"
click node_status_activities "https://github.com/microsoft/pg_durable/blob/main/src/activities/update_node_status.rs"
click node_sql_activity "https://github.com/microsoft/pg_durable/blob/main/src/activities/execute_sql.rs"
click node_http_activity "https://github.com/microsoft/pg_durable/blob/main/src/activities/execute_http.rs"
click node_ssrf "https://github.com/microsoft/pg_durable/blob/main/src/ssrf.rs"
click node_access "https://github.com/microsoft/pg_durable/blob/main/src/registry.rs"
click node_observability "https://github.com/microsoft/pg_durable/blob/main/src/monitoring.rs"
click node_release "https://github.com/microsoft/pg_durable/blob/main/Dockerfile.release"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_clients,node_dsl,node_extension,node_df_state,node_sql_migrations toneBlue
class node_worker,node_orchestrator,node_duroxide,node_duroxide_state toneAmber
class node_load_graph,node_status_activities,node_sql_activity,node_http_activity,node_external_http toneMint
class node_ssrf,node_access,node_observability toneRose
class node_postgres,node_release toneIndigo
```

## Components

### SQL authoring & state

- **SQL clients**: caller · Authors and starts workflows through df.
- [Graph DSL](https://github.com/microsoft/pg_durable/blob/main/src/dsl.rs): SQL API · Composes SQL, control-flow, signal, schedule, and HTTP graph nodes. · `src/dsl.rs`
- [pgrx extension](https://github.com/microsoft/pg_durable/blob/main/src/lib.rs): extension entrypoint · Exports SQL functions and manages extension lifecycle. · `src/lib.rs`
- [df schema](https://github.com/microsoft/pg_durable/blob/main/sql/pg_durable--0.2.2.sql): application persistence · Stores instances, nodes, variables, graph state, and exposed status. · `sql/pg_durable--0.2.2.sql`
- [Install & upgrades](https://github.com/microsoft/pg_durable/blob/main/sql/pg_durable--0.2.5--0.2.6.sql): SQL scripts · Defines schema compatibility, grants, and RLS policies. · `sql/pg_durable--0.2.5--0.2.6.sql`

### Durable runtime

- [Postgres background worker](https://github.com/microsoft/pg_durable/blob/main/src/worker.rs): in-process worker · Discovers runnable instances and hosts the orchestration runtime. · `src/worker.rs`
- [Function graph orchestrator](https://github.com/microsoft/pg_durable/blob/main/src/orchestrations/execute_function_graph.rs): deterministic orchestrator · Loads graph progression and schedules replay-safe activities. · `src/orchestrations/execute_function_graph.rs`
- [duroxide runtime](https://github.com/microsoft/pg_durable/blob/main/Cargo.toml): external Rust library · Provides orchestration replay and timers. · `Cargo.toml`
- **duroxide schema**: runtime persistence · PostgreSQL-backed history and work queues managed by duroxide-pg.

### Activity adapters

- [Load graph activity](https://github.com/microsoft/pg_durable/blob/main/src/activities/load_function_graph.rs): activity · `src/activities/load_function_graph.rs`
- [Status activities](https://github.com/microsoft/pg_durable/blob/main/src/activities/update_node_status.rs): activity · Checkpoints node and instance transitions. · `src/activities/update_node_status.rs`
- [SQL execution activity](https://github.com/microsoft/pg_durable/blob/main/src/activities/execute_sql.rs): activity adapter · Runs workflow SQL inside PostgreSQL. · `src/activities/execute_sql.rs`
- [HTTP activities](https://github.com/microsoft/pg_durable/blob/main/src/activities/execute_http.rs): activity adapter · Executes HTTP and multipart effects with normalized responses. · `src/activities/execute_http.rs`
- **External HTTP services**: optional dependency · Workflow-provided endpoints, including optional Azure services.

### Security & visibility

- [Egress policy](https://github.com/microsoft/pg_durable/blob/main/src/ssrf.rs): SSRF enforcement · Applies outbound HTTP allowlist and trust-boundary checks. · `src/ssrf.rs`
- [Authorization & registry](https://github.com/microsoft/pg_durable/blob/main/src/registry.rs): access boundary · Ordinary users are scoped to owned df state; worker bypasses RLS. · `src/registry.rs`
- [Explain & monitoring](https://github.com/microsoft/pg_durable/blob/main/src/monitoring.rs): SQL visibility API · Exposes execution state and results through PostgreSQL. · `src/monitoring.rs`

### Deployment

- **PostgreSQL server**: database runtime · Hosts the extension, schemas, worker, and workflow effects.
- [Release packaging](https://github.com/microsoft/pg_durable/blob/main/Dockerfile.release): deployment artifacts · Provides Docker and Debian-oriented deployment artifacts. · `Dockerfile.release`

## Connections

- 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
