Cursors
Every cursor implementation in SynergyDB, at the SQL level and on each wire, works the same way: the full result is computed and held in memory when the cursor is opened, and fetches hand out slices of that buffer. No cursor reads from storage lazily, and none keeps a snapshot open between fetches (source: src/query/engine.rs, CursorState; src/protocol/mssql/tsql.rs, CursorState; src/protocol/oracle/query.rs, append_rows_and_status).
SQL cursors in the engine
Statements
The parser accepts the PostgreSQL forms (source: src/query/sql/parser.rs, parse_declare_cursor, parse_cursor_direction_count; see parser):
| Statement | Accepted syntax | Plan node |
|---|---|---|
DECLARE |
DECLARE name [BINARY] [INSENSITIVE \| ASENSITIVE] [[NO] SCROLL] CURSOR [WITH HOLD \| WITHOUT HOLD] FOR query |
DeclareCursor { name, query } |
FETCH |
FETCH [direction] [FROM \| IN] name |
FetchCursor { name, forward, count } |
MOVE |
MOVE [direction] [FROM \| IN] name |
MoveCursor |
CLOSE |
CLOSE name, CLOSE ALL
|
CloseCursor { name: Option<String> } |
The option words between the name and FOR are skipped; they do not change behaviour. Recognised directions:
| Direction | Meaning |
|---|---|
none, NEXT
|
forward 1 |
PRIOR |
backward 1 |
FORWARD, FORWARD n, FORWARD ALL
|
forward 1, n, or all |
BACKWARD, BACKWARD n, BACKWARD ALL
|
backward 1, n, or all |
n, ALL
|
forward n, or all |
FIRST, LAST, ABSOLUTE n and RELATIVE n are not recognised (source: src/query/sql/parser.rs, parse_cursor_direction_count).
State and execution
Each QueryEngine (one per session) holds cursors: HashMap<String, CursorState>, where CursorState is { columns, rows, pos } and pos is the index of the next row a forward fetch returns (source: src/query/engine.rs, CursorState).
| Plan | Behaviour |
|---|---|
DeclareCursor |
runs execute_plan_inner(query) once, with the session's current bound parameters, and stores the complete result. A non-SELECT query stores an empty cursor. A DECLARE with a name already in use replaces the old cursor without an error. Returns an empty result |
FetchCursor forward |
count None (ALL) returns all remaining rows; a count of 0 or less returns none; n returns up to n; pos advances |
FetchCursor backward |
moves pos back and returns the rows in reverse order |
MoveCursor |
moves pos like FETCH without returning rows |
CloseCursor |
removes one cursor, or all with CLOSE ALL
|
| unknown name | error cursor "name" does not exist
|
(source: src/query/engine.rs, execute_plan_inner)
Differences from PostgreSQL:
- Cursors are not closed at
COMMITorROLLBACK, andDISCARD ALLdoes not close them. A cursor lives untilCLOSE, a protocol-level portalClose, or the end of the session.WITH HOLDandWITHOUT HOLDtherefore behave the same (source:src/query/engine.rs,cursor_close; theDISCARDfast path inexecute_with_timeout_innerdrops temporary tables and resets the role but not cursors). - Because the result is materialized at
DECLARE, later changes in the same transaction are never visible through the cursor, and the whole result must fit in memory. - Re-declaring an open name silently replaces it instead of raising
42P03. - The
pg_cursorsview always returns no rows (source:src/query/engine.rs, thepg_cursorsvirtual relation).
LogicalPlan::DeclareCursor's doc comment states the read-only, fully buffered design (source: src/plan/logical.rs, LogicalPlan::DeclareCursor).
Not verified: no engine or integration test runs DECLARE, FETCH, MOVE or CLOSE.
src/query/cursor.rs is not compiled
src/query/cursor.rs describes a CursorRegistry with Materialized and Lazy modes, and has 10 tests. src/query/mod.rs has no mod cursor, so the file is not part of the build, its tests never run, and it refers to a crate::plan::FetchDirection that does not exist (source: src/query/mod.rs; src/query/cursor.rs). Nothing in this page relies on it.
Wire hooks
The engine exposes four functions that wire handlers use (source: src/query/engine.rs, has_cursor, cursor_columns, cursor_take, cursor_close):
| Hook | Use |
|---|---|
has_cursor(name) |
is there a SQL cursor with this name |
cursor_columns(name) |
column list, for a portal Describe
|
cursor_take(name, max_rows) |
up to max_rows rows forward (0 means all remaining), returns (columns, rows, has_more)
|
cursor_close(name) |
drop the cursor |
Per wire
| Wire | Server-side paging | Notes |
|---|---|---|
| PostgreSQL | SQL cursors, and portals named after a SQL cursor | see below |
| MySQL | none |
COM_STMT_FETCH returns ERR 1235 "Cursors are not supported" (source: src/protocol/mysql/mod.rs) |
| SQL Server | T-SQL cursors inside the batch interpreter |
sp_cursor* RPCs are not implemented; see below |
| Oracle | per-cursor result held in the session, fetched in slices | see below |
| MongoDB |
CursorManager with getMore
|
see MongoDB |
| Cassandra | none |
page_size and paging_state are decoded but not used; every result is returned in one page (source: src/protocol/cassandra/cql.rs). See Cassandra
|
| Arrow Flight SQL | none | the whole result is converted to one Arrow RecordBatch (crate arrow) and sent as slices of 65,536 rows (BATCH_ROWS) (source: src/protocol/arrow/flight_grpc.rs, BATCH_ROWS) |
| SQLite / libSQL (Hrana) | none | Hrana v3 cursors are not supported; see SQLite and libSQL |
PostgreSQL extended protocol
Execute carries a max_rows count (0 means all). The handler distinguishes two cases (source: src/protocol/postgres/extended.rs, handle_execute):
| Case | Behaviour |
|---|---|
| No bound portal of that name, but a SQL cursor of that name exists |
cursor_take(name, max_rows); rows are sent in text format (no Bind was sent), flushed every 256 KiB; if rows remain, PortalSuspended is sent and the client may Execute again; otherwise CommandComplete with tag FETCH n
|
| Normal bound portal | the whole statement is executed (or the result prefetched by Describe through prefetch_portal_for_describe is used); the first max_rows rows are sent, then CommandComplete with SELECT n. The remaining rows are dropped |
Source: src/protocol/postgres/extended.rs, handle_execute.
Only the cursor case emits PortalSuspended. For a normal portal with max_rows > 0, PostgreSQL would suspend and let the client fetch the rest; SynergyDB completes the portal and discards the rest, and a second Execute on the same portal runs the query again because the portal is not removed until Close. Drivers that page a plain prepared statement by max_rows (for example the PostgreSQL JDBC driver with a fetch size inside a transaction) therefore see only the first page. Close of a portal ('P') removes the portal and its prefetched result (source: src/protocol/postgres/extended.rs). This matches the note in PostgreSQL wire.
Not verified: neither portal path has a test that sends Execute with max_rows > 0.
SQL Server
T-SQL cursors are implemented by the T-SQL interpreter that runs batches and procedures. CursorState holds the declaring query's tokens, the columns, the rows (None until OPEN) and a position. OPEN executes the query and stores all rows (source: src/protocol/mssql/tsql.rs, CursorState, exec_cursor).
| Statement | Behaviour |
|---|---|
DECLARE c CURSOR FOR ... |
error 16915 if the name exists |
OPEN c |
runs the query; error 16905 if already open, 16916 if not declared |
FETCH NEXT \| PRIOR \| FIRST \| LAST \| ABSOLUTE n \| RELATIVE n FROM c [INTO @v, ...] |
moves the position, sets @@FETCH_STATUS
|
CLOSE c |
error 16917 if not open |
DEALLOCATE c |
error 16916 if not declared |
Resetting the session clears all cursors. The API server cursors that drivers use through sp_cursoropen, sp_cursorfetch and related RPCs are not implemented and fail with error 2812 (see SQL Server wire).
Oracle
Each executed query's rows are stored as an OraResultSet { columns, rows, pos } in the session's real_results, keyed by cursor id. The execute response and each later fetch return up to the requested number of rows (the execute's iteration count, or the fetch's array size; 0 means all) from pos, and the last batch carries ORA-01403 (no data found) to mark the end. REF cursors returned from PL/SQL are fetched the same way (source: src/protocol/oracle/query.rs, append_rows_and_status, build_fetch_response, ERR_NO_DATA_FOUND; src/protocol/oracle/mod.rs). See Oracle wire.