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 "DECLARE" "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 DECLARE \- define a cursor
35 DECLARE \fIname\fR [ BINARY ] [ ASENSITIVE | INSENSITIVE ] [ [ NO ] SCROLL ]
36 CURSOR [ { WITH | WITHOUT } HOLD ] FOR \fIquery\fR
41 allows a user to create cursors, which can be used to retrieve a small number of rows at a time out of a larger query\&. After the cursor is created, rows are fetched from it using
48 .nr an-no-space-flag 1
56 This page describes usage of cursors at the SQL command level\&. If you are trying to use cursors inside a
58 function, the rules are different \(em see
66 The name of the cursor to be created\&. This must be different from any other active cursor name in the session\&.
71 Causes the cursor to return data in binary rather than in text format\&.
78 Cursor sensitivity determines whether changes to the data underlying the cursor, done in the same transaction, after the cursor has been declared, are visible in the cursor\&.
80 means they are not visible,
82 means the behavior is implementation\-dependent\&. A third behavior,
83 SENSITIVE, meaning that such changes are visible in the cursor, is not available in
85 PostgreSQL, all cursors are insensitive; so these key words have no effect and are only accepted for compatibility with the SQL standard\&.
101 specifies that the cursor can be used to retrieve rows in a nonsequential fashion (e\&.g\&., backward)\&. Depending upon the complexity of the query\*(Aqs execution plan, specifying
103 might impose a performance penalty on the query\*(Aqs execution time\&.
105 specifies that the cursor cannot be used to retrieve rows in a nonsequential fashion\&. The default is to allow scrolling in some cases; this is not the same as specifying
116 specifies that the cursor can continue to be used after the transaction that created it successfully commits\&.
118 specifies that the cursor cannot be used outside of the transaction that created it\&. If neither
133 command which will provide the rows to be returned by the cursor\&.
141 can appear in any order\&.
144 Normal cursors return data in text format, the same as a
148 option specifies that the cursor should return data in binary format\&. This reduces conversion effort for both the server and client, at the cost of more programmer effort to deal with platform\-dependent binary data formats\&. As an example, if a query returns a value of one from an integer column, you would get a string of
150 with a default cursor, whereas with a binary cursor you would get a 4\-byte field containing the internal representation of the value (in big\-endian byte order)\&.
152 Binary cursors should be used carefully\&. Many applications, including
153 psql, are not prepared to handle binary cursors and expect data to come back in the text format\&.
159 .nr an-no-space-flag 1
167 When the client application uses the
168 \(lqextended query\(rq
171 command, the Bind protocol message specifies whether data is to be retrieved in text or binary format\&. This choice overrides the way that the cursor is defined\&. The concept of a binary cursor as such is thus obsolete when using extended query protocol \(em any cursor can be treated as either text or binary\&.
177 is specified, the cursor created by this command can only be used within the current transaction\&. Thus,
181 is useless outside a transaction block: the cursor would survive only to the completion of the statement\&. Therefore
183 reports an error if such a command is used outside a transaction block\&. Use
188 \fBROLLBACK\fR) to define a transaction block\&.
192 is specified and the transaction that created the cursor successfully commits, the cursor can continue to be accessed by subsequent transactions in the same session\&. (But if the creating transaction is aborted, the cursor is removed\&.) A cursor created with
194 is closed when an explicit
196 command is issued on it, or the session ends\&. In the current implementation, the rows represented by a held cursor are copied into a temporary file or memory area so that they remain available for subsequent transactions\&.
199 may not be specified when the query includes
206 option should be specified when defining a cursor that will be used to fetch backwards\&. This is required by the SQL standard\&. However, for compatibility with earlier versions,
208 will allow backward fetches without
209 SCROLL, if the cursor\*(Aqs query plan is simple enough that no extra overhead is needed to support it\&. However, application developers are advised not to rely on using backward fetches from a cursor that has not been created with
212 is specified, then backward fetches are disallowed in any case\&.
214 Backward fetches are also disallowed when the query includes
219 may not be specified in this case\&.
225 .nr an-no-space-flag 1
233 Scrollable cursors may give unexpected results if they invoke any volatile functions (see
234 Section\ \&36.7)\&. When a previously fetched row is re\-fetched, the functions might be re\-executed, perhaps leading to results different from the first time\&. It\*(Aqs best to specify
236 for a query involving volatile functions\&. If that is not practical, one workaround is to declare the cursor
238 and commit the transaction before reading any rows from it\&. This will force the entire output of the cursor to be materialized in temporary storage, so that volatile functions are executed exactly once for each row\&.
242 If the cursor\*(Aqs query includes
245 FOR SHARE, then returned rows are locked at the time they are first fetched, in the same way as for a regular
247 command with these options\&. In addition, the returned rows will be the most up\-to\-date versions\&.
253 .nr an-no-space-flag 1
261 It is generally recommended to use
263 if the cursor is intended to be used with
264 \fBUPDATE \&.\&.\&. WHERE CURRENT OF\fR
266 \fBDELETE \&.\&.\&. WHERE CURRENT OF\fR\&. Using
268 prevents other sessions from changing the rows between the time they are fetched and the time they are updated\&. Without
269 FOR UPDATE, a subsequent
271 command will have no effect if the row was changed since the cursor was created\&.
273 Another reason to use
275 is that without it, a subsequent
277 might fail if the cursor query does not meet the SQL standard\*(Aqs rules for being
278 \(lqsimply updatable\(rq
279 (in particular, the cursor must reference just one table and not use grouping or
280 ORDER BY)\&. Cursors that are not simply updatable might work, or might not, depending on plan choice details; so in the worst case, an application might work in testing and then fail in production\&. If
282 is specified, the cursor is guaranteed to be updatable\&.
284 The main reason not to use
288 is if you need the cursor to be scrollable, or to be isolated from concurrent updates (that is, continue to show the old data)\&. If this is a requirement, pay close heed to the caveats shown above\&.
292 The SQL standard only makes provisions for cursors in embedded
295 server does not implement an
297 statement for cursors; a cursor is considered to be open when it is declared\&. However,
298 ECPG, the embedded SQL preprocessor for
299 PostgreSQL, supports the standard SQL cursor conventions, including those involving
305 The server data structure underlying an open cursor is called a
306 portal\&. Portal names are exposed in the client protocol: a client can fetch rows directly from an open portal, if it knows the portal name\&. When creating a cursor with
307 \fBDECLARE\fR, the portal name is the same as the cursor name\&.
309 You can see all available cursors by querying the
320 DECLARE liahona CURSOR FOR SELECT * FROM films;
328 for more examples of cursor usage\&.
331 The SQL standard allows cursors only in embedded
335 permits cursors to be used interactively\&.
337 According to the SQL standard, changes made to insensitive cursors by
338 UPDATE \&.\&.\&. WHERE CURRENT OF
340 DELETE \&.\&.\&. WHERE CURRENT OF
341 statements are visible in that same cursor\&.
343 treats these statements like all other data changing statements in that they are not visible in insensitive cursors\&.
349 \fBCLOSE\fR(7), \fBFETCH\fR(7), \fBMOVE\fR(7)