Guarded updates with expect

Create a keyed row once, advance it with compare-and-set on its version, and enqueue a send only when a guard finds no suppression row.

table Schema: schema.yaml
tableexpectassertguard
docs/site/examples/src/guarded-update/1-create-chunk.yamlyaml
# Admission chunk 3 of run r1. A second admitter gets key_conflict.
request_id: r1-chunk-3
issued_at: 2026-11-16T09:31:00Z
statements:
  - insert:
      into: chunks
      rows: [{run: r1, chunk: 3, cursor: 0}]
      on_conflict: reject

The first program inserts the row. Another program inserting the same key is rejected with key_conflict, and none of its statements commit.

docs/site/examples/src/guarded-update/2-advance-cursor.yamlyaml
# Compare-and-set: move the cursor only if the row still has the version
# this admitter read. Anything else rejects with expect_unmet.
request_id: r1-chunk-3-advance-1
issued_at: 2026-11-16T09:31:05Z
params:
  seen_version: 7
statements:
  - update:
      target: chunks
      keys: [{run: r1, chunk: 3}]
      where: {eq: [version, {param: seen_version}]}
      set: {cursor: 1200}
      expect: {min: 1, max: 1}

Returns matched: 1 and met: true. If another writer changed the row first, its version differs, nothing matches, and the program is rejected with expect_unmet. The cursor is non_decreasing, so setting it lower is rejected with merge_violation.

docs/site/examples/src/guarded-update/3-guarded-send.yamlyaml
# Enqueue a send only if the address is not suppressed. The assert and the
# insert commit together, or neither does.
request_id: c7-enqueue-dee
issued_at: 2026-11-16T09:31:10Z
statements:
  - assert:
      target: suppression
      keys: [{address: dee@example.com}]
      expect: {max: 0}
  - insert:
      into: sends
      rows: [{key: c7-dee, campaign: c7, address: dee@example.com, body: {template: welcome}}]

With no suppression row, the assert matches 0 rows and the insert commits. Once dee@example.com is in suppression, the same program is rejected at statement 0 with expect_unmet, and no send is enqueued.

Programs that write carry a fixed issued_at here. Set it to the time you create the request id, and resend it unchanged on every retry. Ids and tokens come from earlier responses and are passed as params.

All examples · Next: Indexed counts