How the `df.seq()` Function and the `~>` Operator Implement Sequencing in the pg_durable DSL
The ~> operator in the pg_durable DSL is pure syntactic sugar that directly invokes the df.seq() PostgreSQL function, which builds a THEN execution node guaranteeing left-to-right workflow order.
The Microsoft pg_durable extension embeds a Rust-based DSL inside PostgreSQL for composing durable workflows. Grasping the relationship between the df.seq() function and the ~> operator is key to writing clean, sequential logic in this environment. According to the pg_durable source code, the operator is nothing more than a SQL-level wrapper around the underlying Rust implementation.
How df.seq() Builds the Sequence Node
In src/dsl.rs, the then_fn function implements the actual sequencing logic and is exported under the name seq in the df schema.
#[pg_extern(name = "seq", schema = "df")]
pub fn then_fn(a: &str, b: &str) -> String {
let a_fut = Durofut::ensure(a);
let b_fut = Durofut::ensure(b);
Durofut {
node_type: "THEN".to_string(),
left_node: Some(Box::new(a_fut)),
right_node: Some(Box::new(b_fut)),
..Default::default()
}
.to_json()
}
This function takes two string arguments, ensures each is wrapped as a Durofut, and constructs a parent node with node_type set to "THEN". The left and right branches are stored in left_node and right_node, respectively, and the entire structure is serialized to JSON for the execution engine.
How the ~> Operator Maps to df.seq()
The operator definition lives in the extension SQL install script at sql/pg_durable--0.1.1.sql.
CREATE OPERATOR ~> (
FUNCTION = df.seq,
LEFTARG = text,
RIGHTARG = text
);
Because the operator maps FUNCTION = df.seq, any infix expression a ~> b is rewritten by the PostgreSQL parser into df.seq(a, b). This design means the ~> operator contains zero independent logic of its own.
Practical pg_durable DSL Sequencing Examples
You can write sequential workflows using the concise operator or the explicit function call. Both forms emit an identical THEN node and enforce that the left side finishes before the right side starts.
- Use the
~>operator for readability:
SELECT df.sql('SELECT 1') ~> df.sql('SELECT 2');
- Call
df.seq()directly when you need explicit function syntax:
SELECT df.seq(df.sql('SELECT 1'), df.sql('SELECT 2'));
- Sequence raw SQL strings:
SELECT 'SELECT 1' ~> 'INSERT INTO t VALUES (2)';
- Mix with other DSL helpers like
df.sleep():
SELECT df.sleep(1) ~> df.sql('SELECT now()');
Summary
df.seq()is the Rust-backed PostgreSQL function defined insrc/dsl.rsthat creates aTHENnode from two workflow strings.~>is a PostgreSQL operator defined insql/pg_durable--0.1.1.sqlthat acts as a thin wrapper arounddf.seq().- Together they provide two syntax options for the same behavior: sequenced, left-to-right execution in the pg_durable graph.
Frequently Asked Questions
Is ~> exactly equivalent to calling df.seq()?
Yes. The PostgreSQL operator definition binds ~> directly to df.seq in sql/pg_durable--0.1.1.sql. There is no additional logic in the operator itself; it is purely syntactic sugar that makes workflow definitions more readable.
What node type does df.seq() produce in the workflow graph?
It produces a Durofut node with node_type set to the string "THEN", as implemented in src/dsl.rs. This node explicitly links a left_node and right_node to enforce execution order.
Can I use df.seq() directly instead of the ~> operator?
Absolutely. Any expression written with ~> can be rewritten as a direct call to df.seq(). Both approaches generate identical JSON plan output and behave the same at runtime according to the pg_durable source.
Where is the ~> operator registered in the pg_durable source?
The operator is registered in the extension SQL install script found at sql/pg_durable--0.1.1.sql. In that file, the CREATE OPERATOR ~> statement binds the infix symbol to FUNCTION = df.seq. Consequently, PostgreSQL resolves any ~> usage by calling the Rust-backed df.seq implementation.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →