Normal cursors return data in text format, the same as a
would produce. The
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
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).
Binary cursors should be used carefully. Many applications, including
psql, are not prepared to handle binary cursors and expect data to come back in the text format.
When the client application uses the
protocol to issue a
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 any cursor can be treated as either text or binary.
is specified, the cursor created by this command can only be used within the current transaction. Thus,
is useless outside a transaction block: the cursor would survive only to the completion of the statement. Therefore
reports an error if such a command is used outside a transaction block. Use
ROLLBACK(7)) to define a transaction block.
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
is closed when an explicit
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.
may not be specified when the query includes
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,
will allow backward fetches without
SCROLL, if the cursors 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
is specified, then backward fetches are disallowed in any case.
Backward fetches are also disallowed when the query includes
FOR SHARE; therefore
may not be specified in this case.
cursors may give unexpected results if they invoke any volatile functions (see
Section 35.6, Function Volatility Categories, in the documentation). When a previously fetched row is re-fetched, the functions might be re-executed, perhaps leading to results different from the first time. One workaround for such cases is to declare the cursor
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.
If the cursors query includes
FOR SHARE, then returned rows are locked at the time they are first fetched, in the same way as for a regular
command with these options. In addition, the returned rows will be the most up-to-date versions; therefore these options provide the equivalent of what the SQL standard calls a
sensitive cursor. (Specifying
is an error.)
It is generally recommended to use
if the cursor is intended to be used with
UPDATE ... WHERE CURRENT OF
DELETE ... WHERE CURRENT OF. Using
prevents other sessions from changing the rows between the time they are fetched and the time they are updated. Without
FOR UPDATE, a subsequent
WHERE CURRENT OF
command will have no effect if the row was changed since the cursor was created.
Another reason to use
is that without it, a subsequent
WHERE CURRENT OF
might fail if the cursor query does not meet the SQL standards rules for being
(in particular, the cursor must reference just one table and not use grouping or
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.
The main reason not to use
WHERE CURRENT OF
is if you need the cursor to be scrollable, or to be insensitive to the subsequent updates (that is, continue to show the old data). If this is a requirement, pay close heed to the caveats shown above.
The SQL standard only makes provisions for cursors in embedded
server does not implement an
statement for cursors; a cursor is considered to be open when it is declared. However,
ECPG, the embedded SQL preprocessor for
PostgreSQL, supports the standard SQL cursor conventions, including those involving
You can see all available cursors by querying the