Query with SQL
Everything cairnflow knows about your environment can be read with SQL: invocations and their state, journals, timers, promises, streams, deployments, schedules and usage. Queries are read-only, run across every node of the cell, and see only your environment’s data.
Use it from the console’s Query page, or over HTTP:
curl -X POST "$CAIRN_URL/query" \ -H "authorization: Bearer $CAIRN_KEY" \ -H "content-type: application/json" \ -d '{"sql": "SELECT service, class, sum(count) AS n FROM sys_invocation_counts GROUP BY service, class"}'You need a key or console role that can read the environment: an Admin key, or a console session of an owner, admin, member or viewer. An Invoke only key cannot query.
Request and response
Section titled “Request and response”| Field | Meaning |
|---|---|
sql |
One read-only statement |
params |
Values for $1 … $n: strings, integers, booleans or null |
allow_partial |
Default true. false turns a partial answer into a 503 |
timeout_ms |
Default 10,000; at most 30,000 |
{ "columns": [{ "name": "service", "type": "…" }, { "name": "n", "type": "…" }], "rows": [["Chat", 42], ["Greeter", 7]], "partial": false, "warnings": [], "read": 12, "ms": 3}Times come back as ISO-8601 strings; numbers and booleans keep their type. read is the number of
read units the query used.
Parameters
Section titled “Parameters”Pass values separately instead of writing them into the SQL text. They are bound before the query is planned and never become SQL:
{ "sql": "SELECT id, class, created_at FROM sys_invocation WHERE service = $1 AND key = $2", "params": ["Chat", "chat-42"]}Parameters are positional and consecutive ($1, $2, …), with exactly one value each. Named
parameters, floats, arrays and objects are refused.
The tables
Section titled “The tables”| Group | Tables |
|---|---|
| Invocations | sys_invocation (one row per invocation), sys_invocation_counts (counts per service, handler and state) |
| Inside one invocation | sys_journal, sys_timer, sys_attachment, sys_log, sys_span, sys_stream, sys_stream_record, sys_artifact, sys_outbox |
| Objects and workflows | sys_state, sys_value, sys_promise, sys_promise_waiter, sys_object, sys_vqueue_entry |
| Scheduled work | sys_schedule, sys_delayed |
| Code | sys_deployment, sys_service, sys_handler |
| Usage and performance | sys_metering, sys_artifact_bytes, sys_handler_stats, sys_handler_history, sys_slow_run |
| History | sys_archive, sys_audit |
Columns and meanings are in the table reference.
Some tables need a key. Tables that read inside one invocation or one object require an
equality filter on their key, so a query never scans everything: id = … for sys_journal,
sys_log, sys_artifact and the other per-invocation tables; service = … AND key = … for
sys_state, sys_value, sys_promise and sys_promise_waiter. Without it the query is refused
with 400.
Questions operators ask
Section titled “Questions operators ask”What is stuck?
Section titled “What is stuck?”Invocations still running or waiting after an hour:
SELECT id, service, handler, status, created_atFROM sys_invocationWHERE class = 'active' AND created_at < now() - INTERVAL '1 hour'ORDER BY created_atLIMIT 50Paused invocations (out of attempts, or stopped by an error that needs a person), with the last error:
SELECT id, service, handler, last_failure_message, transitioned_atFROM sys_invocationWHERE class = 'paused'ORDER BY transitioned_at DESC, idLIMIT 50Invocations waiting for a retry, and why the last attempt failed:
SELECT id, service, handler, attempt, retry_at, last_failure_code, last_failure_messageFROM sys_invocationWHERE retry_at IS NOT NULLORDER BY retry_atLIMIT 50What is one invocation waiting for?
Section titled “What is one invocation waiting for?”SELECT index, name, entry_liteFROM sys_journalWHERE id = $1ORDER BY indexIn entry_lite, a command with "completed": false is still waiting for its result: a call, a
promise, a sleep. Pending sleeps are also in sys_timer:
SELECT completion_id, due_at_ms FROM sys_timer WHERE id = $1The console’s Why it is in this state panel answers the same question in words.
What is failing?
Section titled “What is failing?”SELECT id, service, handler, failure_code, failure_message, transitioned_atFROM sys_invocationWHERE class = 'failed'ORDER BY transitioned_at DESC, idLIMIT 50Failed invocations per handler, from in-memory counters (reads nothing from storage):
SELECT service, handler, sum(count) AS nFROM sys_invocation_countsWHERE class = 'failed'GROUP BY service, handlerORDER BY n DESCLIMIT 10Common failure codes: 572 cancelled, 575 out of attempts, 578 journal too long. See
Errors.
What is slow?
Section titled “What is slow?”Handlers by turn latency over the recent window:
SELECT service, handler, sum(turns) AS turns, sum(failed) AS failed, max(p99_ms) AS p99_msFROM sys_handler_statsGROUP BY service, handlerORDER BY p99_ms DESCLIMIT 10Invocations that took longer than ten seconds end to end:
SELECT id, service, handler, duration_msFROM sys_invocationWHERE duration_ms >= 10000ORDER BY created_at DESC, idLIMIT 50Long journals, which cost more on every resume in request-response mode:
SELECT id, service, handler, journal_lenFROM sys_invocationWHERE journal_len >= 100ORDER BY created_at DESC, idLIMIT 50How much is used?
Section titled “How much is used?”The current hour:
SELECT hour, invocations, steps, run_ms, input_tokens, output_tokens, unitsFROM sys_meteringCompleted, failed and compute units per hour, from a start time in milliseconds:
SELECT hour_ms, sum(completed) AS completed, sum(failed) AS failed, sum(cu) AS cuFROM sys_handler_historyWHERE hour_ms >= $1GROUP BY hour_msORDER BY hour_msTokens of one agent turn, per model call:
SELECT name, input_tokens, output_tokensFROM sys_spanWHERE id = $1 AND input_tokens IS NOT NULLFind an invocation
Section titled “Find an invocation”By object key, idempotency key, tag, parent or whole call tree:
SELECT id, handler, class, created_at FROM sys_invocationWHERE service = 'Chat' AND key = 'chat-42'ORDER BY created_at DESC, id LIMIT 50;
SELECT id, service, handler, class FROM sys_invocationWHERE idempotency_key = 'order-42' LIMIT 10;
SELECT id, parent_id, service, handler, class FROM sys_invocationWHERE root_id = $1 ORDER BY id;Send one statement per request; the block above shows three.
Consistency
Section titled “Consistency”- Only durable data. A query sees rows that are committed to storage. It never waits for, or slows down, the invocations it reads. When the node ships the query worker, each statement runs in a separate worker process, apart from the engine.
- Counts are exact as of the last commit of each key range:
sys_invocation_countsis kept in memory and updated with each commit. - Partial answers are flagged. If part of the data cannot be read (a node does not answer, or
an index is still being built after a change), the answer has
partial: trueand awarningsentry per affected range (reasonsuch asindexing). It never silently drops rows. Sendallow_partial: falseto get a503instead. - Live tables come from the API. Deployments, services, schedules, statistics, usage, archive and audit tables are read through the same API the console uses, with your key’s permissions.
Limits and errors
Section titled “Limits and errors”| Answer | Cause |
|---|---|
400 |
SQL syntax error, unknown table or column, a statement that is not read-only (DDL, DML, COPY, SET), table functions, recursive CTEs, file reads, a keyed table without its key, or a statement too large to plan: over 64 KiB of text, more than 500 operators, more than 64 joined tables or more than 64 query blocks (SELECT, UNION, …) |
422, error starting with query_budget: |
The query would read more than 200,000 read units, use more than 128 MiB, run longer than its timeout, or return more than 10,000 rows. Add a LIMIT or filter on an indexed column (service, class, created_at, id) |
429 |
More than 4 queries at once on a node or 2 of your environment’s, or your environment used up its read units for now (error starting with query_budget:). Retry shortly |
A query either completes or fails: an error never comes back as a truncated 200.
sys_invocation is stored in created_at DESC, id order, so “newest first” lists with a LIMIT
are cheap. Filters on id, service, handler, key, kind, class, root, parent_id,
root_id, schedule, deployment, restarted_from, created_at, transitioned_at and
duration_ms are applied during the scan; filters on other columns are applied after reading,
so combine them with one of these.