3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "FETCH" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 FETCH \- retrieve rows from a query using a cursor
35 FETCH [ \fIdirection\fR ] [ FROM | IN ] \fIcursor_name\fR
37 where \fIdirection\fR can be one of:
57 retrieves rows using a previously\-created cursor\&.
59 A cursor has an associated position, which is used by
60 \fBFETCH\fR\&. The cursor position can be before the first row of the query result, on any particular row of the result, or after the last row of the result\&. When created, a cursor is positioned before the first row\&. After fetching some rows, the cursor is positioned on the row most recently retrieved\&. If
62 runs off the end of the available rows then the cursor is left positioned after the last row, or before the first row if fetching backward\&.
65 \fBFETCH BACKWARD ALL\fR
66 will always leave the cursor positioned after the last row or before the first row\&.
75 fetch a single row after moving the cursor appropriately\&. If there is no such row, an empty result is returned, and the cursor is left positioned before the first row or after the last row as appropriate\&.
81 retrieve the indicated number of rows moving in the forward or backward direction, leaving the cursor positioned on the last\-returned row (or after/before all rows, if the
83 exceeds the number of rows available)\&.
88 all request fetching the current row without moving the cursor, that is, re\-fetching the most recently fetched row\&. This will succeed unless the cursor is positioned before the first row or after the last row; in which case, no row is returned\&.
94 .nr an-no-space-flag 1
102 This page describes usage of cursors at the SQL command level\&. If you are trying to use cursors inside a
104 function, the rules are different \(em see
113 defines the fetch direction and number of rows to fetch\&. It can be one of the following:
117 Fetch the next row\&. This is the default if
124 Fetch the prior row\&.
129 Fetch the first row of the query (same as
135 Fetch the last row of the query (same as
142 \fIcount\fR\*(Aqth row of the query, or the
143 abs(\fIcount\fR)\*(Aqth row from the end if
145 is negative\&. Position before first row or after last row if
147 is out of range; in particular,
149 positions before the first row\&.
155 \fIcount\fR\*(Aqth succeeding row, or the
156 abs(\fIcount\fR)\*(Aqth prior row if
160 re\-fetches the current row, if any\&.
168 FORWARD \fIcount\fR)\&.
173 Fetch all remaining rows (same as
179 Fetch the next row (same as
189 re\-fetches the current row\&.
194 Fetch all remaining rows\&.
199 Fetch the prior row (same as
207 rows (scanning backwards)\&.
209 re\-fetches the current row\&.
214 Fetch all prior rows (scanning backwards)\&.
221 is a possibly\-signed integer constant, determining the location or number of rows to fetch\&. For
225 cases, specifying a negative
227 is equivalent to changing the sense of
235 An open cursor\*(Aqs name\&.
239 On successful completion, a
241 command returns a command tag of the form
255 is the number of rows fetched (possibly zero)\&. Note that in
256 psql, the command tag will not actually be displayed, since
258 displays the fetched rows instead\&.
261 The cursor should be declared with the
263 option if one intends to use any variants of
269 with a positive count\&. For simple queries
271 will allow backwards fetch from cursors not declared with
272 SCROLL, but this behavior is best not relied on\&. If the cursor is declared with
273 NO SCROLL, no backward fetches are allowed\&.
276 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
277 FETCH ABSOLUTE 0) is fast\&.
280 is used to define a cursor\&. Use
282 to change cursor position without retrieving data\&.
285 The following example traverses a table using a cursor:
293 \-\- Set up a cursor:
294 DECLARE liahona SCROLL CURSOR FOR SELECT * FROM films;
296 \-\- Fetch the first 5 rows in the cursor liahona:
297 FETCH FORWARD 5 FROM liahona;
299 code | title | did | date_prod | kind | len
300 \-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-
301 BL101 | The Third Man | 101 | 1949\-12\-23 | Drama | 01:44
302 BL102 | The African Queen | 101 | 1951\-08\-11 | Romantic | 01:43
303 JL201 | Une Femme est une Femme | 102 | 1961\-03\-12 | Romantic | 01:25
304 P_301 | Vertigo | 103 | 1958\-11\-14 | Action | 02:08
305 P_302 | Becket | 103 | 1964\-02\-03 | Drama | 02:28
307 \-\- Fetch the previous row:
308 FETCH PRIOR FROM liahona;
310 code | title | did | date_prod | kind | len
311 \-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-+\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-
312 P_301 | Vertigo | 103 | 1958\-11\-14 | Action | 02:08
314 \-\- Close the cursor and end the transaction:
323 The SQL standard defines
325 for use in embedded SQL only\&. The variant of
327 described here returns the data as if it were a
329 result rather than placing it in host variables\&. Other than this point,
331 is fully upward\-compatible with the SQL standard\&.
338 BACKWARD, as well as the forms
347 The SQL standard allows only
349 preceding the cursor name; the option to use
350 IN, or to leave them out altogether, is an extension\&.
352 \fBCLOSE\fR(7), \fBDECLARE\fR(7), \fBMOVE\fR(7)