Diagnosing a slow query
/v1/query; entries carry no plan text), EXPLAIN ANALYZE, per-operator and lock-wait totals and the CPU profile. Statement statistics and their reset, the activity view, cancel, query_id, slow queries from every door and the module series are on develop. "On develop" means merged in the product repository but not in a release yet.1. Find the expensive shapes
relata_stat_statements has one row per query shape and tenant, like PostgreSQL's pg_stat_statements. Two statements that differ only in their literals share a row, keyed by query_id, a 16-hex-digit fingerprint.
SELECT query_id, query, calls, total_seconds, p99_seconds
FROM relata_stat_statements ORDER BY total_seconds DESC LIMIT 10
SELECT query, errors, errors_by_code FROM relata_stat_statements WHERE errors > 0| Column | Meaning |
|---|---|
query_id | The shape fingerprint; the same value as performance.query_id in a /v1/query answer, the slow-query list and the logs. |
query | The normalized text: every literal is ?, at most 2 KiB. Never a literal or bound value. |
tenant, door, purpose | Who ran it and how. door is http today. |
calls, rows, cache_hits | Executions, rows returned, answers served from the result cache. |
errors, errors_by_code | Error answers, and their count per code (at most 8 codes). |
total_seconds, mean_seconds, min_seconds, max_seconds, p50_seconds, p99_seconds | Wall-clock time. The percentiles come from power-of-two buckets. |
first_seen_ns, last_seen_ns | First and last execution, nanoseconds UTC. |
Every POST /v1/query answer is counted, errors included, except 401. The search routes that run their SQL through /v1/query are counted too. A tenant-scoped token sees only its own tenant's rows. The table keeps at most RELATA_STAT_STATEMENTS_MAX shapes (default 1000); a new shape then evicts the one with the least total time, and relata_stat_dropped_total{view="statements"} rises. Rows live for the process, or until a reset.
Reset the statistics
Like PostgreSQL's pg_stat_statements_reset(), on the admin listener:
# every row
curl -s -X POST -H "Authorization: Bearer $RELATA_ADMIN_TOKEN" \
http://127.0.0.1:9091/admin/stat/statements/reset
# one shape, for every tenant
curl -s -X POST -H "Authorization: Bearer $RELATA_ADMIN_TOKEN" \
"http://127.0.0.1:9091/admin/stat/statements/reset?query_id=<16 hex digits>"The answer is {entries_removed, reset_at_ns}. A malformed query_id answers 400 invalid-query-id and resets nothing. Every reset is audited. After a full reset, statements_stats_reset_ns in /v1/system/stats holds its time.
2. See what is running now, and cancel it
relata_stat_activity has one row per query running on this node through /v1/query (including GraphQL and the MCP tools that run SQL through it), /v1/query/stream (door query_stream) and /v1/query/arrow (door query_arrow). A streamed query stays listed until its last frame is written.
SELECT execution_id, door, tenant, elapsed_seconds, statement
FROM relata_stat_activity ORDER BY started_ns LIMIT 10Columns are execution_id, query_id, request_id, door, tenant, token_id, purpose, started_ns, elapsed_seconds, state (running or cancelling) and statement (literals replaced by ?, at most 1024 bytes). Operator and oversight keys only.
execution_id is a UUID per execution. A client can choose it up front with the X-Relata-Execution-Id header. Cancel with either route:
# data plane: the token that started the query, or a node-wide key
curl -s -X POST -H "Authorization: Bearer $RELATA_API_KEY" \
http://127.0.0.1:9090/v1/query/<execution_id>/cancel
# admin listener: any query on this node
curl -s -X POST -H "Authorization: Bearer $RELATA_ADMIN_TOKEN" \
http://127.0.0.1:9091/admin/queries/<execution_id>/cancelBoth answer 202 with state: cancelling, or 404 when no such query runs or the caller may not cancel it. The worker stops at its next check and releases its permit. The cancelled request answers 409 query-cancelled. Every cancel request is audited. Agents on MCP have a cancel_query tool with the same authorization.
Cancelling a stream
Both streaming doors accept X-Relata-Execution-Id and return it as a response header. A cancel seen before the first byte answers 409 query-cancelled. After that:
/v1/query/streamends with an error_metaframe carryingtruncated: trueand thequery-cancelledproblem. The supported SDKs raise it asIncompleteQueryStreamError./v1/query/arrowstops at a record-batch boundary without the IPC end-of-stream marker, and the HTTP body ends with an error. Arrow readers accept a stream without the marker, so treat a body error as an incomplete answer.
Rows sent before a cancel are not the whole answer.
3. Find the slow run
Start with the route-level series to see which route moved:
histogram_quantile(0.99,
sum(rate(relata_http_route_duration_seconds_bucket[5m])) by (le, endpoint))Then read the slow-query list:
curl -s -H "Authorization: Bearer $RELATA_API_KEY" \
http://127.0.0.1:9090/v1/system/stats | jq '.slow_queries'A query that takes longer than RELATA_SLOW_QUERY_MS (default 500) is added to a list of the last 20, newest first. On develop, every query door feeds it: http, graphql, mcp, stream, arrow and search. Each entry has:
door,query_id,request_id,traceparent,tenant_id;duration_ms,at_ns,sql_bytes;explain_plan: the plan the executor ran, with every literal replaced by?, at most 2,048 bytes.
The statement text is not kept; sql holds [REDACTED]. On develop each entry is also logged as RLM-QUERY-0002 on the perf log channel. Use query_id to find the shape in relata_stat_statements, and request_id to find the request in the logs and the audit log. A tenant-scoped token sees only its own entries.
4. Explain the query
Re-run the statement with EXPLAIN ANALYZE. It always instruments the one query, whatever the sample rate.
curl -s -H "Authorization: Bearer $RELATA_API_KEY" -H 'Content-Type: application/json' \
-d '{"sql":"EXPLAIN ANALYZE SELECT * FROM Person WHERE age > 30 ORDER BY name LIMIT 10"}' \
http://127.0.0.1:9090/v1/query | jq .The reply has one row per stage (Scan, Filter, Sort, and so on) with actual_rows, ns_elapsed and bytes_scanned, plus a scan total and a spill count. Compare est_rows with actual_rows: a large gap means the estimate, and so the plan, is wrong. A non-zero spill.runs means a GROUP BY or ORDER BY wrote to disk. EXPLAIN without ANALYZE shows the plan without running it.
5. See which operator dominates across all queries
relata_module_duration_seconds{module="query"} and {module="planner"} time every execution and plan. For a finer split, a sample of queries (RELATA_PROFILE_SAMPLE_RATE, default one in 100) feeds per-operator time into /metrics. The operators are scan, filter, join, aggregate, sort, acl and serialize.
sum(rate(relata_query_operator_seconds_total[5m])) by (operator)
sum(rate(relata_query_door_operator_seconds_total[5m])) by (door, operator)door is http, mcp or graphql. Setting the rate to 0.0 turns sampling off.
6. Check for waiting
If the time is not in an operator, check lock waits. Only an acquisition that finds the lock held is timed.
rate(relata_lock_contended_total{lock="per_type_table"}[5m])
/ rate(relata_lock_acquisitions_total{lock="per_type_table"}[5m])The locks are audit_log, ingest_queue, per_type_table, fts_index, store_read and store_write. The store lock is the known concurrency ceiling; the runbook "Requests are waiting on the store lock" covers it. RELATA_LOCK_WAIT_METRICS=false switches the accounting off.
A request that waits for an in-flight permit is shed with 503 server-overloaded after RELATA_HTTP_QUEUE_WAIT_MS (default 2000 ms). A query that passes RELATA_QUERY_TIMEOUT_SECS (default 30) is aborted. Both appear in relata_problem_responses_total{code}.
7. Take a CPU profile
If the cause is still unclear, capture a profile on the admin listener (see Memory, caches and profiling).
Caches can mislead
A repeated statement may be served from the result cache. /v1/query returns cached, performance.cache_temperature and performance.cache_verdict. Add WITH CACHE BYPASS to time the real execution.
What is coming
Wait events, the current stage and wait in the activity view and statement statistics for the other doors are tracked in #7849.