# How the `df.seq()` Function and the `~>` Operator Implement Sequencing in the pg_durable DSL

> Discover how the df.seq() function and the ~> operator in pg_durable implement sequencing, ensuring left-to-right workflow order with THEN execution nodes.

- Repository: [Microsoft/pg_durable](https://github.com/microsoft/pg_durable)
- Tags: internals
- Published: 2026-06-08

---

**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`](https://github.com/microsoft/pg_durable/blob/main/src/dsl.rs), the **`then_fn`** function implements the actual sequencing logic and is exported under the name **`seq`** in the `df` schema.

```rust
#[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`](https://github.com/microsoft/pg_durable/blob/main/sql/pg_durable--0.1.1.sql)**.

```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.

1. Use the `~>` operator for readability:

```sql
SELECT df.sql('SELECT 1') ~> df.sql('SELECT 2');

```

2. Call `df.seq()` directly when you need explicit function syntax:

```sql
SELECT df.seq(df.sql('SELECT 1'), df.sql('SELECT 2'));

```

3. Sequence raw SQL strings:

```sql
SELECT 'SELECT 1' ~> 'INSERT INTO t VALUES (2)';

```

4. Mix with other DSL helpers like **`df.sleep()`**:

```sql
SELECT df.sleep(1) ~> df.sql('SELECT now()');

```

## Summary

- **`df.seq()`** is the Rust-backed PostgreSQL function defined in [`src/dsl.rs`](https://github.com/microsoft/pg_durable/blob/main/src/dsl.rs) that creates a `THEN` node from two workflow strings.
- **`~>`** is a PostgreSQL operator defined in [`sql/pg_durable--0.1.1.sql`](https://github.com/microsoft/pg_durable/blob/main/sql/pg_durable--0.1.1.sql) that acts as a thin wrapper around `df.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`](https://github.com/microsoft/pg_durable/blob/main/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`](https://github.com/microsoft/pg_durable/blob/main/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`](https://github.com/microsoft/pg_durable/blob/main/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.