Referencia · §8.47

Live queries

live, §M16.6

Ez az oldal a nucleus saját dokumentációjából készül, ezért angol nyelvű.

A session subscribes to a query and asks for what changed since:

SUBSCRIBE open_tasks AS SELECT id, title FROM tasks WHERE done = false;
CHANGES open_tasks WAIT 30000;   -- what the commits since changed
UNSUBSCRIBE open_tasks;          -- or UNSUBSCRIBE ALL

What they answer.

  • Both statements answer rows of (nc_ts, nc_diff, columns…). nc_ts is the commit the batch was read at. nc_diff is how many copies of the row came (+n) or went (−n). An update is a −1 and a +1 at the same nc_ts. This is Materialize’s SUBSCRIBE form (mz_timestamp, mz_diff).
  • SUBSCRIBE answers the first result, every row +1, in the query’s order. The client keeps the result by applying each batch. Order is the client’s to keep; ORDER BY … LIMIT still picks which rows are in.
  • CHANGES name answers at once: a batch, or no rows.
  • CHANGES name WAIT ms (a number or $n, at most an hour) waits for a commit that changes the result. While it waits it holds no transaction and no snapshot.
  • Parameters are bound at SUBSCRIBE, and so is the identity it runs as: the session’s role, its settings (SET) and its login’s claims then. A later SET ROLE or SET does not change it.
  • All three are refused in a transaction block (25001).

How it works.

  • The subscription keeps:
    • the query and its parameters;
    • its result, as a multiset of rows;
    • the snapshot it was read at;
    • a watch of writes (§M16.6 S1) on the key ranges it read. These are each table its privilege checks looked at, whole, with the table’s indexes; a partitioned table’s partitions; and the catalog’s definitions, which cover DDL, grants and policies.
  • CHANGES re-runs the query at a new snapshot once the watch’s mark is past the last run’s snapshot. It answers the difference between the two multisets, the rows gone first, each part in the rows’ key order.
  • A run that changes nothing it answers is no batch: the wait goes on.
  • Each run is made as that identity, with its privileges (as they are now: a REVOKE applies) and its row-level security, and with the subjects’ keys the session has presented. Each run has a statement budget of its own; the wait is not counted.
  • When the tables it reads change (a table dropped and made again under its name), the new ranges are watched from that run on.
  • A run that fails ends the subscription with its error: a privilege revoked (42501), a table dropped (42P01). So does a run whose columns differ from the first’s (55000, as SELECT * after ADD COLUMN).

What is refused.

  • Functions whose answer changes without a commit (0A000): volatile ones (random(), clock_timestamp(), nextval) and the time ones (now(), current_date, current_timestamp, localtime, localtimestamp, statement_timestamp, transaction_timestamp), subqueries included.

Limits.

  • 64 subscriptions per session (54000).
  • 10 000 in the database, every session’s together (53400).
  • 100 000 rows per result (54000).

Less work per commit (§M16.6 S4).

  • Rows applied, not the query run. This applies to a query over one table’s rows, each output row of one table row. It may have filters, row-level security and a projection (a sort is dropped: a result is a multiset). It has no aggregate, DISTINCT, limit, window, join or subquery, and its table is a row table with a primary key, not partitioned. Such a subscription:
    • keeps its result by the rows’ keys;
    • has its watch keep the keys each commit writes in the table’s rows (Kv::watch_writes_keyed), before the commit is visible;
    • after a commit, reads those rows by key and runs the query’s filters, policies and projection on them alone. Each key’s output rows replace those it had.
  • When it runs whole instead. A commit that is not known key by key makes the query run whole, as other queries do. Such a commit:
    • wrote the catalog (DDL, a grant, a policy) or another table the query reads;
    • wrote more than 4096 keys since the last run;
    • spilled, or loaded a fresh range or a stream.
  • Shared runs. Subscriptions to a query that runs whole share its runs (Hasura’s multiplexing). This needs the same query, parameters and identity, in the same database. After a commit the first subscription to look runs it; the others take its result, and its batch where they held the result it started from.
    • A session that presented subjects’ keys shares nothing.
    • A query applying rows does not share: applying them costs each subscription less than copying a shared result would.
  • Metrics. nucleus_live_runs_total counts whole runs, nucleus_live_applied_total runs that applied rows, and nucleus_live_shared_total results taken from another subscription’s run.

Measured on a table of 10 000 rows (id, g = id % 100, an md5 text), 200 commits each, in the process: CHANGES after a commit, against the same query run after a write.

query CHANGES after a commit the query after a write
a point read (applied) 0.007 ms 0.008 ms
a 1% filter (applied) 0.007 ms 0.50 ms
all 10 000 rows (applied) 0.006 ms 2.1 ms
GROUP BY g (run whole) 0.46 ms 0.44 ms

On 1 000 000 rows, a 1% filter costs 0.02 ms after a commit, against 12.6 ms to run it (7.2 ms through an index on its column). Ten subscriptions to a query that runs whole (the same queries with LIMIT 100000) take one run a commit: all ten CHANGES cost 0.57 ms, against 5.0 ms each running its own, for the 1% filter, and 2.9 ms against 29 ms for all 10 000 rows. Over the network, with 1 to 100 subscribers: docs/benchmarks.md, “Live queries”.

In the drivers (§M16.6 S3). Every driver has subscribe(sql, params). Its subscription is named by the driver (nc_live_…) and keeps the result on the client’s side: the first result in the query’s order, rows that come appended, a row gone taken out by equality.

  • changes(timeout) sends CHANGES name WAIT $1, applies the batch, and returns it as (diff, row) pairs.
  • close() sends UNSUBSCRIBE.
  • C answers the raw (nc_ts, nc_diff, …) results (nc_subscribe, nc_changes, nc_unsubscribe) for the caller to apply.

Across a restart (§M16.6 S5). A subscription lives in its session: when the server stops or the connection is lost, it is gone, and the client subscribes again and gets the whole result. Nothing a commit made visible is lost to a subscriber that does so: the first result after it is the query’s at that commit, and each batch after it as before.

  • editions/nucleus/tests/live_kill.rs checks this against nucleus server killed (SIGKILL) three times while three writers commit inserts, updates (a primary key’s among them), deletes and upserts. Four subscribers keep a query each: one applying rows, two alike sharing their runs, a join run whole. After every batch the kept result is the query’s AS OF COMMIT the batch’s nc_ts, and at the end the query’s now.
  • examples/live/ (Python and TypeScript): a dashboard of orders by status, and an agent told of each task given to it.
  • The shell: \live [<batches> [<ms>]] <query> prints a query’s result, then its changes as they come (§6.2).
  • Latency from a commit to the delivered batch, and the cost of a commit with 1, 10 and 100 subscribers: docs/benchmarks.md, “Live queries”.