From JSON to SQL
A request is a batch of named queries. Each one passes through the same stages, and validation, permission and static-cost failures reject the whole batch before any SQL runs:- Parameters. For saved queries,
{ "$param" }markers are replaced on the JSON itself — only in value, limit and time positions — so parameters are validated exactly like literals and never reach SQL text. - Schema. A strict schema checks the shape. Unknown keys are refused anywhere, including any attempt to pass a
workspaceId. - Validation. Every field path is resolved against the catalog (with “did you mean” hints), every operator is checked against the field’s type, and every limit is checked against the caller’s budget. The caller’s permissions decide which resources they may query.
- Static cost. A cost estimate from the query’s shape (includes, relation tests, unindexed searches) refuses obviously expensive requests early, with hints on how to narrow them.
- Planning. The planner builds the SQL: list or aggregate, capped totals, includes as lateral subqueries, relation tests as
EXISTSorCOUNTsubqueries.
The catalog
The catalog declares every resource — targets, hosts, services, HTTP responses, technologies, TLS certificates, URLs, IPs, DNS records, findings, rollups — with its fields, the SQL fragment behind each field, and the relations between resources. Only catalog names reach SQL; every value travels as a bound parameter. A caller can never name a table, a column or a function. The catalog is checked when the core API starts: a fragment that uses an undeclared alias, or a resource without a valid relation, stops the boot. What clients and the agent see from it (describe) never exposes table names or SQL.
Tenant scope is compiled, never supplied
The workspace id comes from the permission check or from the agent’s run, never from the query. The planner writes it into the SQL as its own term:- every statement’s
WHEREstarts with the root resource’s tenant term; - the caller’s filter, the time window, the project scope and the cursor are each separate, parenthesized
ANDterms, so anoror anotin the caller’s filter cannot escape the tenant term; - every join, include,
EXISTSandCOUNTsubquery re-applies the child’s tenant term.
asq_reader role exists, each query runs as that role with the workspace set on the transaction, and row-level security policies hide every other workspace’s rows. See Tenancy.
Execution
- Read-only units. Each query runs in its own
BEGIN … READ ONLYtransaction with astatement_timeout, and is always rolled back. A batch that asks for a consistent view runs in oneREPEATABLE READunit instead. - EXPLAIN gate. Interactive queries are first explained by PostgreSQL; a plan whose estimated cost is above the caller’s ceiling, or a bounded sort that would sort too many rows, is refused before it runs.
- Bounded concurrency. Each core API process runs at most three ASQ units at a time. A query that waits more than two seconds for a slot gets a
BUSYerror instead of piling up. - Errors stay inside. One query’s runtime failure is its own
{ error }result; the others in the batch still return. Database errors are mapped to ASQ error codes, never passed through.
Paging
Lists are paged with keyset cursors, not offsets: the cursor encodes the sort key of the last row, so deep pages cost the same as the first one and concurrent inserts do not shift results. Cursors are signed with an HMAC and bound to the query they came from, so a cursor cannot be edited or replayed against a different query.Report mode
Saved queries can run as reports. A report is queued on BullMQ and executed in the background with report budgets, and its result is kept for an hour. When a read replica is configured (ASQ_READ_REPLICA_URL), report mode runs there, keeping heavy reporting off the primary database.
Rollups
Distributions that every dashboard asks for — services per port, technologies, status codes and similar — are pre-computed per workspace every 15 minutes over the latest HTTP response of each service, and exposed as a queryable resource. A chart over a large inventory reads a few hundred rollup rows instead of scanning millions of responses.Related
Attack Surface Query
The query language and what you can ask
ASQ for the AI agent and MCP
How the agent learns and uses ASQ
Tenancy and access control
The layers of the workspace boundary
Data model
The tables ASQ resources map onto
