Home Explore Blog CI



postgresql

4th chunk of `doc/src/sgml/ref/fetch.sgml`
dbab536e9268c617215ba4b03d15aa90b6505c331b10630b000000010000099a
 class="parameter">count</replaceable> is a
      possibly-signed integer constant, determining the location or
      number of rows to fetch.  For <literal>FORWARD</literal> and
      <literal>BACKWARD</literal> cases, specifying a negative <replaceable
      class="parameter">count</replaceable> is equivalent to changing
      the sense of <literal>FORWARD</literal> and <literal>BACKWARD</literal>.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="parameter">cursor_name</replaceable></term>
    <listitem>
     <para>
      An open cursor's name.
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>

 <refsect1>
  <title>Outputs</title>

  <para>
   On successful completion, a <command>FETCH</command> command returns a command
   tag of the form
<screen>
FETCH <replaceable class="parameter">count</replaceable>
</screen>
   The <replaceable class="parameter">count</replaceable> is the number
   of rows fetched (possibly zero).  Note that in
   <application>psql</application>, the command tag will not actually be
   displayed, since <application>psql</application> displays the fetched
   rows instead.
  </para>
 </refsect1>

 <refsect1>
  <title>Notes</title>

  <para>
   The cursor should be declared with the <literal>SCROLL</literal>
   option if one intends to use any variants of <command>FETCH</command>
   other than <command>FETCH NEXT</command> or <command>FETCH FORWARD</command> with
   a positive count.  For simple queries
   <productname>PostgreSQL</productname> will allow backwards fetch
   from cursors not declared with <literal>SCROLL</literal>, but this
   behavior is best not relied on. If the cursor is declared with
   <literal>NO SCROLL</literal>, no backward fetches are allowed.
  </para>

  <para>
   <literal>ABSOLUTE</literal> fetches are not any faster than
   navigating to the desired row with a relative move: the underlying
   implementation must traverse all the intermediate rows anyway.
   Negative absolute fetches are even worse: the query must be read to
   the end to find the last row, and then traversed backward from
   there.  However, rewinding to the start of the query (as with
   <literal>FETCH ABSOLUTE 0</literal>) is fast.
  </para>

  <para>
   <link linkend="sql-declare"><command>DECLARE</command></link>
   is used to define a cursor.  Use
   <link linkend="sql-move"><command>MOVE</command></link>
   to change

Title: FETCH Command Outputs, Notes on SCROLL Option, and Absolute Fetches
Summary
This section explains the output of a successful FETCH command, which returns a command tag indicating the number of rows fetched. It also notes that in psql, the command tag isn't displayed as the fetched rows are shown instead. Additionally, it discusses the importance of declaring a cursor with the SCROLL option for using FETCH variants other than NEXT or FORWARD with positive counts, and the performance implications of using ABSOLUTE fetches compared to relative moves. Finally, it mentions using DECLARE to define a cursor.