-
Notifications
You must be signed in to change notification settings - Fork 15
docs(subscription): document multi-row cursor fetch and non-blocking short-read behavior #1071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
3
commits into
main
Choose a base branch
from
copilot/docs-update-multi-row-cursor-fetch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+43
−6
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,13 +96,19 @@ FETCH from cursor function is supported in the PSQL simple query mode and extend | |
|
|
||
| #### Non-blocking data fetch | ||
|
|
||
| Use `FETCH NEXT` to retrieve a single row, or `FETCH n` to retrieve up to `n` rows in one call: | ||
|
|
||
| ```sql | ||
| -- Fetch one row | ||
| FETCH NEXT FROM cursor_name; | ||
|
|
||
| -- Fetch up to n rows at once | ||
| FETCH n FROM cursor_name; | ||
| ``` | ||
|
|
||
| Fetch the next row from the cursor. If there are no rows available (i.e., the latest data has been reached), an empty result is returned immediately. | ||
| `FETCH NEXT` is equivalent to `FETCH 1`. | ||
|
|
||
| To fetch multiple rows, call `FETCH NEXT` repeatedly in a loop. | ||
| Both forms are non-blocking: if fewer than `n` rows are available, only the available rows are returned immediately. If no rows are available, an empty result is returned immediately. | ||
|
|
||
| ```sql | ||
| FETCH NEXT FROM cur; | ||
|
|
@@ -114,19 +120,35 @@ t1.v1 | t1.v2 | t1.v3 | t1.op | rw_timestamp | |
| (1 row) | ||
| ``` | ||
|
|
||
| In the example above, the `op` column in the result indicates the type of change operations. There are four options: `Insert`, `UpdateInsert`, `Delete`, and `UpdateDelete`. For a single UPDATE statement, the subscription log will contain two separate rows: one with `UpdateInsert` and another with `UpdateDelete`. This is because RisingWave treats an UPDATE as a delete of the old value followed by an insert of the new value. As for `rw_timestamp`, it corresponds to the Unix timestamp in milliseconds when the data was written. | ||
| ```sql | ||
| -- Fetch up to 10 rows at once | ||
| FETCH 10 FROM cur; | ||
|
|
||
| ----RESULT | ||
| t1.v1 | t1.v2 | t1.v3 | t1.op | rw_timestamp | ||
| -------+-------+-------+--------------+--------------- | ||
| 1 | 1 | 1 | UpdateDelete | 1715669376304 | ||
| 1 | 1 | 10 | UpdateInsert | 1715669376304 | ||
| (2 rows) | ||
| ``` | ||
|
|
||
| In the example above, the `op` column in the result indicates the type of change operations. There are four options: `Insert`, `UpdateInsert`, `Delete`, and `UpdateDelete`. For a single UPDATE statement, the subscription log will contain two separate rows: one with `UpdateInsert` and another with `UpdateDelete`. This is because RisingWave treats an UPDATE as a delete of the old value followed by an insert of the new value. As for `rw_timestamp`, it corresponds to the Unix timestamp in milliseconds when the data was written. | ||
|
|
||
| #### Blocking data fetch | ||
|
|
||
| ```sql | ||
| -- Block until 1 row arrives or the timeout elapses | ||
| FETCH NEXT FROM cursor_name WITH (timeout = '1s'); | ||
|
|
||
| -- Block until up to n rows arrive or the timeout elapses | ||
| FETCH n FROM cursor_name WITH (timeout = '1s'); | ||
| ``` | ||
|
|
||
| Fetch up to N rows from the cursor with a specified timeout. The `timeout` value should be a string in the interval format. In this case, the fetch statement will return when either N rows have been fetched or the timeout occurs. If the timeout occurs, whatever has been read up to that point will be returned. Here are two scenarios to trigger the timeout: | ||
| Fetch up to `n` rows from the cursor with a specified timeout. The `timeout` value should be a string in the interval format. The fetch statement returns when either `n` rows have been fetched or the timeout occurs. If the timeout occurs, whatever has been read up to that point will be returned. Here are two scenarios to trigger the timeout: | ||
|
|
||
| 1. The cursor has reached the latest data and has been waiting too long for new data to arrive. | ||
|
|
||
| 2. At least N rows are available for the cursor to read, but retrieving all of them takes an extended period. | ||
| 2. At least `n` rows are available for the cursor to read, but retrieving all of them takes an extended period. | ||
|
|
||
| To avoid polling for new data frequently with the non-blocking `FETCH`, you can set a longer timeout to simulate a scenario where you want the `FETCH` to block until new data arrives. | ||
|
|
||
|
|
@@ -179,10 +201,25 @@ fetch next from cur; | |
| (1 row) | ||
| ``` | ||
|
|
||
| Then we can update table `t1` and fetch again to view the changes: | ||
| Then we can update table `t1` and fetch again to view the changes. Use `FETCH NEXT` to consume one row at a time, or `FETCH n` to retrieve multiple rows in a single call: | ||
|
|
||
| ```sql | ||
| update t1 set v3 = 10 where v1 = 1; | ||
|
|
||
| -- Fetch both change rows in one call | ||
| FETCH 10 FROM cur; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verified with v2.7.2, can you help confirm the use case @hzxa21
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM. @xxhZs can double confirm. |
||
|
|
||
| ----RESULT | ||
| t1.v1 | t1.v2 | t1.v3 | t1.op | rw_timestamp | ||
| -------+-------+-------+---------------+--------------- | ||
| 1 | 1 | 1 | UpdateDelete | 1715669376304 | ||
| 1 | 1 | 10 | UpdateInsert | 1715669376304 | ||
| (2 rows) | ||
| ``` | ||
|
|
||
| Alternatively, use `FETCH NEXT` to fetch one row at a time: | ||
|
|
||
| ```sql | ||
| fetch next from cur; | ||
|
|
||
| ----RESULT | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.