CRUD & ACID Transactions

Inserts, updates, upserts, deletes, joins, SSI transactions, locking reads, and balance_delta ledgers.

2 min read SpydQL & Data Modeling

All inserts, updates, deletes, upserts, joins, and multi-statement interactive transactions share one snapshot + Serializable Snapshot Isolation (SSI) engine. Every secondary, full-text, vector, spatial, and blind index updates atomically at commit.

Inserting Rows#

spydql
-- Insert a single row
users | insert { email: "ada@example.test", name: "Ada Lovelace", active: true }

-- Batch insert multiple rows and return generated fields
users | insert [
  { email: "grace@example.test", name: "Grace Hopper", active: true },
  { email: "alan@example.test",  name: "Alan Turing",  active: true }
] | returning id, email, created_at

-- Bind parameterized JSON object rows cleanly
event_docs | insert $1

Updating & Merging Rows (set, upsert, merge)#

spydql
-- Filtered update
orders
| where id == $1 and status == "pending"
| set { status: "paid", paid_at: now() }

-- Atomic ledger / balance delta with overflow & bounds checking
accounts
| where id == 42
| set { balance: balance_delta(balance, 0, 10000, -25, 10) }

balance_delta(current, minimum, maximum, delta1, ...) validates starting balance and every intermediate step against overflow and allowed min/max bounds atomically inside the write transaction; a failed check aborts the statement and leaves the row untouched.

Upserts (upsert / merge) & Deleting#

spydql
-- Upsert on an explicit conflict key
sessions | upsert on session_id { session_id: $1, user_id: $2, last_seen: now() }

-- Atomic relational merge
merge into inventory as inv
using incoming_stock as inc
on inv.sku == inc.sku
when matched then update set { qty: inv.qty + inc.delta }
when not matched then insert { sku: inc.sku, qty: inc.delta }
| returning inv.sku, inv.qty

-- Delete with predicate and RETURNING
expired_tokens
| where expires_at < now()
| delete
| returning token_id

Joins#

spydql
-- Inner join
orders
| join customers on orders.customer_id == customers.id
| where orders.status == "paid"
| pick orders.id, customers.name, orders.total
| sort orders.total desc

-- Left outer join (also available: right_join, full_join, cross_join, semi_join, anti_join)
customers
| left_join orders on customers.id == orders.customer_id
| group by customers.id, customers.name
| agg count(orders.id) as orders_placed

Interactive Transactions#

Execute multiple reads and writes under one isolated SSI snapshot using begin, commit, and rollback:

spydql
begin
accounts | where id == 1 | set { balance: balance_delta(balance, 0, 100000, -100) }
accounts | where id == 2 | set { balance: balance_delta(balance, 0, 100000, +100) }
ledger   | insert { from_id: 1, to_id: 2, amount: 100, at: now() }
commit

Abort explicit changes cleanly with rollback. If an interactive client socket closes before commit, the server automatically rolls back the open transaction.

Handling Serialization Conflicts (40001)#

Under Serializable Snapshot Isolation, if two concurrent explicit transactions create a read/write conflict cycle, SpydDB aborts one transaction cleanly with SQLSTATE 40001 (serialization_failure) and code conflict:

  • Autocommit statements automatically retry within a bounded conflict budget inside the server.
  • Explicit multi-statement begin ... commit transactions should retry the entire transaction block with short exponential backoff + jitter on 40001 / SpydConflictError.

Locking Reads (lock_rows & SELECT ... FOR UPDATE / SHARE)#

Coordinate queue workers or inventory reservations explicitly inside a transaction using SpydQL lock_rows or SQL FOR UPDATE / FOR SHARE:

spydql
begin
job_queue
| where status == "queued"
| sort id asc
| take 1
| lock_rows update skip_locked
commit

And over PostgreSQL wire (pgwire):

sql
BEGIN;

-- Claim the next unclaimed job without blocking other workers
SELECT id, payload
FROM job_queue
WHERE status = 'queued'
ORDER BY id
LIMIT 1
FOR UPDATE SKIP LOCKED;

-- Or fail immediately with SQLSTATE 55P03 if contended
SELECT id, stock
FROM inventory
WHERE sku = 'NVME-4TB'
FOR NO KEY UPDATE NOWAIT;

COMMIT;

Supported locking clauses:

  • Exclusive locks: FOR UPDATE, FOR NO KEY UPDATE
  • Shared locks: FOR SHARE, FOR KEY SHARE
  • Specific joined table targets: FOR UPDATE OF o FOR SHARE OF c
  • Contention policies: NOWAIT (returns SQLSTATE 55P03) and SKIP LOCKED
  • Deadlock detection: lock wait cycles immediately abort with SQLSTATE 40P01.

Distributed Coordination: Interactive 2PC, Leases, Row Locks, HLC & Idempotency#

For multi-shard and multi-cluster workloads, SpydDB provides explicit coordination primitives over HTTP and the official SDKs:

  • Idempotency Keys: Attach an Idempotency-Key header on mutating cluster requests so retried writes deduplicate atomically against the commit journal.
  • Interactive Distributed Transactions (2PC): Run multi-step cross-shard transactions using /v1/cluster/tx/begin, /v1/cluster/tx/query, /v1/cluster/tx/stage, and /v1/cluster/tx/end (backed by /v1/cluster/tx/prepare, /commit, /abort, and /recover).
  • Regional Leases: Acquire, fence, and transfer authoritative region write leases via /v1/cluster/leases, /v1/cluster/leases/acquire, /v1/cluster/leases/fence, and /v1/cluster/leases/transfer.
  • Distributed Row Locks: Coordinate cross-node exclusive/shared row locks via /v1/cluster/locks, /v1/cluster/locks/acquire, and /v1/cluster/locks/release.
  • Hybrid Logical Clocks (HLC): Inspect and synchronize cluster-wide causal timestamps via /v1/cluster/hlc and /v1/cluster/hlc/sync for causally consistent multi-region reads.
  • In-Flight Read Coalescing & Statement Deadlines: Identical concurrent cache-eligible read queries are coalesced in-flight so thundering-herd spikes execute once, and statements enforce a default execution deadline to prevent runaway queries.