You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-`DECLARE` only supports forward cursors. Reverse cursors created with `DECLARE SCROLL` are not supported. [#77102](https://github.com/cockroachdb/cockroach/issues/77102)
4
4
-`FETCH` supports forward, relative, and absolute variants, but only for forward cursors. [#77102](https://github.com/cockroachdb/cockroach/issues/77102)
5
5
-`BINARY CURSOR`, which returns data in the Postgres binary format, is not supported. [#77099](https://github.com/cockroachdb/cockroach/issues/77099)
6
-
-`WITH HOLD`, which allows keeping a cursor open for longer than a transaction by writing its results into a buffer, is accepted as valid syntax within a single transaction but is not supported. It acts as a no-op and does not actually perform the function of `WITH HOLD`, which is to make the cursor live outside its parent transaction. Instead, if you are using `WITH HOLD`, you will be forced to close that cursor within the transaction it was created in. [#77101](https://github.com/cockroachdb/cockroach/issues/77101)
7
-
- This syntax is accepted (but does not have any effect):
8
-
{% include_cached copy-clipboard.html %}
9
-
~~~ sql
10
-
BEGIN;
11
-
DECLARE test_cur CURSOR WITH HOLD FOR SELECT * FROM foo ORDER BY bar;
12
-
CLOSE test_cur;
13
-
COMMIT;
14
-
~~~
15
-
- This syntax is not accepted, and will result in an error:
16
-
{% include_cached copy-clipboard.html %}
17
-
~~~sql
18
-
BEGIN;
19
-
DECLARE test_cur CURSOR WITH HOLD FOR SELECT*FROM foo ORDER BY bar;
20
-
COMMIT; -- This will fail with an error because CLOSE test_cur was not called inside the transaction.
21
-
~~~
22
6
- Scrollable cursor (also known as reverse `FETCH`) is not supported. [#77102](https://github.com/cockroachdb/cockroach/issues/77102)
23
7
-[`SELECT ... FOR UPDATE`]({% link {{ page.version.version }}/select-for-update.md %}) with a cursor is not supported. [#77103](https://github.com/cockroachdb/cockroach/issues/77103)
24
8
- Respect for [`SAVEPOINT`s]({% link {{ page.version.version }}/savepoint.md %}) is not supported. Cursor definitions do not disappear properly if rolled back to a `SAVEPOINT` from before they were created. [#77104](https://github.com/cockroachdb/cockroach/issues/77104)
Copy file name to clipboardExpand all lines: src/current/v25.2/cursors.md
+89-4
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,7 @@ Cursors differ from [keyset pagination]({% link {{ page.version.version }}/pagin
21
21
Cursors are declared and used with the following keywords:
22
22
23
23
-[`DECLARE`]({% link {{ page.version.version }}/sql-grammar.md %}#declare_cursor_stmt)
24
+
-[`WITH HOLD`](#use-a-holdable-cursor)
24
25
-[`FETCH`]({% link {{ page.version.version }}/sql-grammar.md %}#fetch_cursor_stmt)
25
26
-[`CLOSE`]({% link {{ page.version.version }}/sql-grammar.md %}#close_cursor_stmt)
26
27
@@ -75,6 +76,89 @@ CLOSE rides_cursor;
75
76
COMMIT;
76
77
~~~
77
78
79
+
### Use a holdable cursor
80
+
81
+
By default, a cursor closes when the transaction ends. The `WITH HOLD` clause defines a *holdable cursor*, which behaves as follows:
82
+
83
+
- A holdable cursor writes its results into a buffer, and stays open after a transaction commits.
84
+
- If a transaction aborts, all cursors opened within that transaction are also rolled back. However, holdable cursors from previously committed transactions remain open.
85
+
- A holdable cursor can be opened in both explicit and implicit transactions.
86
+
- If a holdable cursor results in an error as it is being persisted, it will cause the current transaction (implicit or explicit) to be rolled back.
87
+
88
+
Use `WITH HOLD` to access data across multiple transactions without redefining the cursor.
89
+
90
+
{{site.data.alerts.callout_info}}
91
+
The `WITHOUT HOLD` clause specifies the default non-holdable cursor behavior.
92
+
{{site.data.alerts.end}}
93
+
94
+
The following example uses a holdable cursor to return vehicles that are available for rides.
0 commit comments