-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
111329: plpgsql: add support for unnamed cursors r=DrewKimball a=DrewKimball #### plpgsql: add builtin to generate unique cursor names This patch adds a builtin function `crdb_internal.plpgsql_gen_cursor_name` that generates a unique name for a PLpgSQL cursor if the supplied name is NULL. It then returns the resulting name to be used when opening a cursor. This will be used in a following commit to implement unnamed PLpgSQL cursors. Informs #109709 Release note: None #### plpgsql: add support for unnamed cursors This patch adds support for opening an "unnamed" cursor in a PLpgSQL routine. A PLpgSQL cursor is unnamed when the value for the cursor variable is `NULL`. When an unnamed cursor is opened, a name will be generated for it like `<unnamed portal 1>` that is guaranteed not to conflict with an existing cursor or portal name. The PLpgSQL variable that represents the cursor's name is updated to reflect the generated name. Informs #109709 Release note (sql change): Added support for unnamed PLpgSQL cursors, which generate a unique name when no cursor name was specified. Co-authored-by: Drew Kimball <[email protected]>
- Loading branch information
Showing
18 changed files
with
692 additions
and
137 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Testing crdb_internal.plpgsql_gen_cursor_name. | ||
query T | ||
SELECT crdb_internal.plpgsql_gen_cursor_name(NULL); | ||
---- | ||
<unnamed portal 1> | ||
|
||
query T | ||
SELECT crdb_internal.plpgsql_gen_cursor_name(NULL); | ||
---- | ||
<unnamed portal 2> | ||
|
||
statement ok | ||
BEGIN; | ||
DECLARE "<unnamed portal 3>" CURSOR FOR SELECT 1; | ||
|
||
query T | ||
SELECT name FROM pg_cursors; | ||
---- | ||
<unnamed portal 3> | ||
|
||
# Skip manually generated duplicate names. | ||
query T | ||
SELECT crdb_internal.plpgsql_gen_cursor_name(NULL); | ||
---- | ||
<unnamed portal 4> | ||
|
||
query T | ||
SELECT crdb_internal.plpgsql_gen_cursor_name(NULL); | ||
---- | ||
<unnamed portal 5> | ||
|
||
statement ok | ||
CLOSE "<unnamed portal 3>"; | ||
|
||
# Keep incrementing after a "gap" opens. | ||
query T | ||
SELECT crdb_internal.plpgsql_gen_cursor_name(NULL); | ||
---- | ||
<unnamed portal 6> | ||
|
||
statement ok; | ||
ABORT; | ||
|
||
# Continue incrementing over transaction boundaries. | ||
query T | ||
SELECT crdb_internal.plpgsql_gen_cursor_name(NULL); | ||
---- | ||
<unnamed portal 7> | ||
|
||
# If the name is already set, don't generate a new one. | ||
query T | ||
SELECT crdb_internal.plpgsql_gen_cursor_name('foo'); | ||
---- | ||
foo | ||
|
||
query T | ||
SELECT crdb_internal.plpgsql_gen_cursor_name('bar'); | ||
---- | ||
bar | ||
|
||
query T | ||
SELECT crdb_internal.plpgsql_gen_cursor_name(''); | ||
---- | ||
· | ||
|
||
query T | ||
SELECT crdb_internal.plpgsql_gen_cursor_name(NULL); | ||
---- | ||
<unnamed portal 8> | ||
|
||
# Starting a new session restarts the counter. | ||
user testuser | ||
|
||
query T | ||
SELECT crdb_internal.plpgsql_gen_cursor_name(NULL); | ||
---- | ||
<unnamed portal 1> | ||
|
||
query T | ||
SELECT crdb_internal.plpgsql_gen_cursor_name(NULL); | ||
---- | ||
<unnamed portal 2> | ||
|
||
# Returning to the old session continues the old counter. | ||
user root | ||
|
||
query T | ||
SELECT crdb_internal.plpgsql_gen_cursor_name(NULL); | ||
---- | ||
<unnamed portal 9> | ||
|
||
query T | ||
SELECT crdb_internal.plpgsql_gen_cursor_name(NULL); | ||
---- | ||
<unnamed portal 10> |
Oops, something went wrong.