cockroachdb / cockroachdb/cockroach
plpgsql: make PLpgSQL cursors lazy
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
#110709 added initial support for the PLpgSQL `OPEN` statement, which allows a PLpgSQL routine to open a cursor that can be interacted with in the same ways as one created by SQL `DECLARE ... CURSOR` syntax. One of the reasons to use a cursor is that they materialize results on-demand; however, a cursor opened by PLpgSQL `OPEN` executes its entire query and stores the result in a row container. This container tracks memory usage and can spill to disk when needed, but this could still result in worse performance and resource usage than users expect.
This issue tracks adding support for lazy execution of PLpgSQL cursors. The difficulty lies in the fact that the SQL statement that is bound to the cursor can reference PLpgSQL variables. This makes it difficult to use the internal executor for lazy execution, as SQL cursors do. It may be possible to replace PLpgSQL variable references with placeholders, which the internal executor supports. It may also be possible to avoid going through the internal executor entirely, and modify the `runPlanInsidePlan` logic to allow for lazy execution.
Jira issue: CRDB-31917
Contributor guide
Assessment
This issue has not been assessed yet.