Postgres has received a significant upgrade with the introduction of PG Durable, an open-source extension developed by Microsoft. This extension brings durable, crash-proof functions directly inside PostgreSQL, allowing developers to manage scheduled jobs, automatic retries, and long-running workflows without relying on external services. Because execution steps are persisted to disk, workflows can reliably survive database restarts and crashes.
Core Features and Built-in Operations
PG Durable operates by representing workflows as a graph of steps. Developers can initiate workflows using df_start and track their execution via df_status or df_result. The extension includes several native capabilities to simplify complex database-level processes:
- Execution Control: Run tasks in sequence, parallel, or as a “race” where the fastest operation wins.
- Time-based Scheduling: Use
df.waitwith cron expressions to schedule database jobs directly inside Postgres. - Signals and Approvals: Implement human-in-the-loop workflows where execution pauses until an external signal is received.
Practical Use Cases
The extension excels in two major scenarios demonstrated in the video:
- Automated Scheduling: A background loop can wait for a set duration (e.g., one minute) and then run database procedures, such as refreshing materialized views, completely replacing third-party cron tools.
- Human-in-the-Loop Workflows: It can fetch a pending record, pause for up to 24 hours, and wait for an external
df_signalto proceed with either approving or rejecting an order.
Drastic Boilerplate Reduction
One of the most compelling advantages of PG Durable is code simplification. Complex systems requiring queue setups, polling, message handling, and retries often consume hundreds of lines of SQL boilerplate. PG Durable can reduce a 300-line workflow down to just a 7-line SQL statement, handling error recovery and state tracking automatically behind the scenes.
Getting Started
Developers can easily run PG Durable locally using Docker. The extension runs on Postgres 17 and requires adding pg_durable to the shared_preload_libraries configuration.
Mentoring question
How could migrating your current external background-worker or cron services into database-level durable functions simplify your architecture and reduce your overall system points of failure?
Source: https://youtube.com/watch?v=4Lmqvn_yz-c&is=9PFXUSRDu9PLM-Tq