Skip to content

Commit 9aa644f

Browse files
committed
Doc: improve description of plpgsql's FETCH and MOVE commands.
We were not being clear about which variants of the "direction" clause are permitted in MOVE. Also, the text seemed to be written with only the FETCH/MOVE NEXT case in mind, so it didn't apply very well to other variants. Also, document that "MOVE count IN cursor" only works if count is a constant. This is not the whole truth, because some other cases such as a parenthesized expression will also work, but we want to push people to use "MOVE FORWARD count" instead. The constant case is enough to cover what we allow in plain SQL, and that seems sufficient to claim support for. Update a comment in pl_gram.y claiming that we don't document that point. Per gripe from Philipp Salvisberg. Discussion: https://postgr.es/m/172155553388.702.7932496598218792085@wrigleys.postgresql.org
1 parent 9695305 commit 9aa644f

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

doc/src/sgml/plpgsql.sgml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3358,13 +3358,16 @@ FETCH <optional> <replaceable>direction</replaceable> { FROM | IN } </optional>
33583358
</synopsis>
33593359

33603360
<para>
3361-
<command>FETCH</command> retrieves the next row from the
3361+
<command>FETCH</command> retrieves the next row (in the indicated
3362+
direction) from the
33623363
cursor into a target, which might be a row variable, a record
33633364
variable, or a comma-separated list of simple variables, just like
3364-
<command>SELECT INTO</command>. If there is no next row, the
3365+
<command>SELECT INTO</command>. If there is no suitable row, the
33653366
target is set to NULL(s). As with <command>SELECT
33663367
INTO</command>, the special variable <literal>FOUND</literal> can
3367-
be checked to see whether a row was obtained or not.
3368+
be checked to see whether a row was obtained or not. If no row is
3369+
obtained, the cursor is positioned after the last row or before the
3370+
first row, depending on the movement direction.
33683371
</para>
33693372

33703373
<para>
@@ -3416,11 +3419,25 @@ MOVE <optional> <replaceable>direction</replaceable> { FROM | IN } </optional> <
34163419

34173420
<para>
34183421
<command>MOVE</command> repositions a cursor without retrieving
3419-
any data. <command>MOVE</command> works exactly like the
3422+
any data. <command>MOVE</command> works like the
34203423
<command>FETCH</command> command, except it only repositions the
3421-
cursor and does not return the row moved to. As with <command>SELECT
3424+
cursor and does not return the row moved to.
3425+
The <replaceable>direction</replaceable> clause can be any of the
3426+
variants allowed in the SQL <xref linkend="sql-fetch"/>
3427+
command, including those that can fetch more than one row;
3428+
the cursor is positioned to the last such row.
3429+
(However, the case in which the <replaceable>direction</replaceable>
3430+
clause is simply a <replaceable>count</replaceable> expression with
3431+
no key word is deprecated in <application>PL/pgSQL</application>.
3432+
That syntax is ambiguous with the case where
3433+
the <replaceable>direction</replaceable> clause is omitted
3434+
altogether, and hence it may fail if
3435+
the <replaceable>count</replaceable> is not a constant.)
3436+
As with <command>SELECT
34223437
INTO</command>, the special variable <literal>FOUND</literal> can
3423-
be checked to see whether there was a next row to move to.
3438+
be checked to see whether there was a row to move to. If there is no
3439+
such row, the cursor is positioned after the last row or before the
3440+
first row, depending on the movement direction.
34243441
</para>
34253442

34263443
<para>

src/pl/plpgsql/src/pl_gram.y

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3218,11 +3218,11 @@ read_fetch_direction(void)
32183218
{
32193219
/*
32203220
* Assume it's a count expression with no preceding keyword.
3221-
* Note: we allow this syntax because core SQL does, but we don't
3222-
* document it because of the ambiguity with the omitted-direction
3223-
* case. For instance, "MOVE n IN c" will fail if n is a variable.
3224-
* Perhaps this can be improved someday, but it's hardly worth a
3225-
* lot of work.
3221+
* Note: we allow this syntax because core SQL does, but it's
3222+
* ambiguous with the case of an omitted direction clause; for
3223+
* instance, "MOVE n IN c" will fail if n is a variable, because the
3224+
* preceding else-arm will trigger. Perhaps this can be improved
3225+
* someday, but it hardly seems worth a lot of work.
32263226
*/
32273227
plpgsql_push_back_token(tok);
32283228
fetch->expr = read_sql_expression2(K_FROM, K_IN,

0 commit comments

Comments
 (0)