diff --git a/docs/query-language.md b/docs/query-language.md index 85c2c44dc..e89590b76 100644 --- a/docs/query-language.md +++ b/docs/query-language.md @@ -239,9 +239,12 @@ TRUNCATE users; ### RETURNING -Both `UPDATE` and `DELETE` support a `RETURNING` clause to read back affected rows in the same statement: +`INSERT`, `UPSERT`, `UPDATE`, `DELETE`, and `MERGE` accept a `RETURNING` clause that reads back the affected rows in the same statement: ```sql +-- INSERT RETURNING: returns the stored row +INSERT INTO users (id, name) VALUES ('u2', 'Bo') RETURNING id, name; + -- UPDATE RETURNING: returns the post-update image UPDATE users SET role = 'admin' WHERE id = 'u1' RETURNING id, role; UPDATE orders SET status = 'shipped' WHERE id = 'o1' RETURNING *; @@ -249,9 +252,67 @@ UPDATE orders SET status = 'shipped' WHERE id = 'o1' RETURNING *; -- DELETE RETURNING: returns the pre-delete image DELETE FROM users WHERE id = 'u1' RETURNING id, name; DELETE FROM orders WHERE status = 'cancelled' RETURNING *; + +-- Expressions: evaluated per returned row against the stored image +UPDATE orders SET qty = qty + 1 WHERE id = 'o1' RETURNING id, qty * price AS total; +INSERT INTO events (id, kind) VALUES ('e1', 'click') RETURNING id, nextval('event_seq') AS n; ``` -`RETURNING *` expands to all columns. Named columns are returned as bare values — arithmetic expressions in `RETURNING` are not supported. Works in both simple-query and extended-query (prepared statement) protocols. +`RETURNING *` expands to all columns. Every item is a scalar expression over the target collection: a bare column, a column under an alias (`col AS name`), arithmetic, a function call, or a sequence accessor (`nextval`, `currval`, `setval`). The Data Plane returns the base columns an expression reads, and the Control Plane evaluates the expression once per returned row. A sequence accessor advances once per row, in row order. The clause works in both the simple-query and extended-query (prepared statement) protocols, and `Describe` announces an expression under its alias. + +### Sequences + +A sequence is a named `bigint` counter, independent of any collection. + +```sql +CREATE SEQUENCE event_seq START WITH 1 INCREMENT BY 1 MINVALUE 1 CYCLE CACHE 20; +DROP SEQUENCE event_seq; +DROP SEQUENCE IF EXISTS event_seq; +SHOW SEQUENCES; +DESCRIBE SEQUENCE event_seq; +ALTER SEQUENCE event_seq RESTART WITH 100; +``` + +`CREATE SEQUENCE [IF NOT EXISTS] ` accepts these options, in any order: + +| Option | Effect | +|---|---| +| `START [WITH] n` | First value `nextval` returns | +| `INCREMENT [BY] n` | Step between successive values | +| `MINVALUE n` | Lower bound | +| `MAXVALUE n` | Upper bound | +| `CYCLE` / `NO CYCLE` | Wrap to the bound instead of erroring at exhaustion | +| `CACHE n` | Values a node pre-allocates per round-trip to the registry | +| `FORMAT 'template'` | Render template applied to the numeric value | +| `RESET period` | Period after which the counter restarts | +| `GAP_FREE` | Accepted and stored; allocation is not yet serialized or rolled back per transaction | +| `SCOPE name` | Named allocation scope | + +`DROP SEQUENCE [IF EXISTS] ` removes it. `SHOW SEQUENCES` lists every sequence. `DESCRIBE SEQUENCE ` reports one sequence's current state. `ALTER SEQUENCE RESTART [WITH n]` and `ALTER SEQUENCE FORMAT '