DECLARE(7)
β¨ DECLARE(7) β¨
okay so bestie, this is the DECLARE page from the PostgreSQL 13.23 Documentation and it's literally all about defining cursors π
πΈ NAME πΈ
DECLARE β define a cursor, periodt.
π SYNOPSIS π
DECLARE name [ BINARY ] [ INSENSITIVE ] [ [ NO ] SCROLL ]
CURSOR [ { WITH | WITHOUT } HOLD ] FOR query
π¦ DESCRIPTION π¦
So DECLARE lets you create cursors, which are like, genuinely iconic for grabbing a small number of rows at a time out of a much bigger query. Once the cursor is created, you fetch rows from it using FETCH(7) β¨
Note: This page is about using cursors at the SQL command level. If you're trying to use cursors inside a PL/pgSQL function, the rules are different β see Section 42.7. iykyk π
π PARAMETERS π
nameβ the name of the cursor you're creating. slay, give it a cute name.BINARYβ makes the cursor return data in binary instead of text format. spicy π₯INSENSITIVEβ says data retrieved from the cursor should be unaffected by updates to the underlying table(s) that happen after the cursor is created. In PostgreSQL this is literally the default behavior, so this keyword has no effect and is only accepted for compatibility with the SQL standard. it's giving "just here for the vibes" πSCROLL/NO SCROLLβSCROLLmeans the cursor can retrieve rows in a nonsequential fashion (like, backward, bestie). Depending on how complex the query's execution plan is, specifyingSCROLLmight impose a performance penalty on the query's execution time.NO SCROLLmeans the cursor cannot retrieve rows nonsequentially. The default is to allow scrolling in some cases; this is not the same as specifyingSCROLL. See Notes below for the details, fr fr.WITH HOLD/WITHOUT HOLDβWITH HOLDmeans the cursor can keep being used after the transaction that created it successfully commits.WITHOUT HOLDmeans the cursor cannot be used outside of the transaction that created it. If neitherWITHOUT HOLDnorWITH HOLDis specified,WITHOUT HOLDis the default.queryβ aSELECT(7)orVALUES(7)command which will provide the rows to be returned by the cursor.
The key words BINARY, INSENSITIVE, and SCROLL can appear in any order. no cap π«Ά
π NOTES π
Normal cursors return data in text format, same as what a SELECT would produce. The BINARY 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. Like, for example: if a query returns a value of one from an integer column, you'd get a string of 1 with a default cursor, whereas with a binary cursor you'd get a 4-byte field containing the internal representation of the value (in big-endian byte order). iconic but also a lot π
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. so like, be mindful bestie.
Note: When the client application uses the βextended queryβ protocol to issue a FETCH 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. the way that this whole concept just became irrelevant π
Unless WITH HOLD is specified, the cursor created by this command can only be used within the current transaction. So DECLARE without WITH HOLD is useless outside a transaction block: the cursor would survive only to the completion of the statement. Therefore PostgreSQL reports an error if such a command is used outside a transaction block. Use BEGIN(7) and COMMIT(7) (or ROLLBACK(7)) to define a transaction block. lowkey important, don't skip this π«Ά
If WITH HOLD 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 WITH HOLD is closed when an explicit CLOSE 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. it's giving "prepared and ready" β¨
WITH HOLD may not be specified when the query includes FOR UPDATE or FOR SHARE. not me warning you about this, it's literally a rule π
The SCROLL 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, PostgreSQL will allow backward fetches without SCROLL, if the cursor's 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 SCROLL. If NO SCROLL is specified, then backward fetches are disallowed in any case. so like, don't even try it π
Backward fetches are also disallowed when the query includes FOR UPDATE or FOR SHARE; therefore SCROLL may not be specified in this case.
Caution: Scrollable cursors may give unexpected results if they invoke any volatile functions (see Section 37.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's best to specifyNO SCROLLfor a query involving volatile functions. If that's not practical, one workaround is to declare the cursorSCROLL WITH HOLDand 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. honestly this is so real, don't let volatile functions ruin your vibe π
If the cursor's query includes FOR UPDATE or FOR SHARE, then returned rows are locked at the time they are first fetched, in the same way as for a regular SELECT(7) 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 INSENSITIVE together with FOR UPDATE or FOR SHARE is an error.) π₯
Caution: It is generally recommended to useFOR UPDATEif the cursor is intended to be used withUPDATE ... WHERE CURRENT OForDELETE ... WHERE CURRENT OF. UsingFOR UPDATEprevents other sessions from changing the rows between the time they are fetched and the time they are updated. WithoutFOR UPDATE, a subsequentWHERE CURRENT OFcommand will have no effect if the row was changed since the cursor was created. bestie please useFOR UPDATE, it's for your own good π«Ά Another reason to useFOR UPDATEis that without it, a subsequentWHERE CURRENT OFmight fail if the cursor query does not meet the SQL standard's rules for being βsimply updatableβ (in particular, the cursor must reference just one table and not use grouping orORDER 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. IfFOR UPDATEis specified, the cursor is guaranteed to be updatable. not the "works in testing but fails in prod" pipeline ππ The main reason not to useFOR UPDATEwithWHERE CURRENT OFis 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 SQL. The PostgreSQL server does not implement an OPEN 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 DECLARE and OPEN statements. iykyk β¨
You can see all available cursors by querying the pg_cursors system view. so convenient bestie π
π EXAMPLES π
To declare a cursor:
DECLARE liahona CURSOR FOR SELECT * FROM films;
See FETCH(7) for more examples of cursor usage. ily FETCH π«Ά
π COMPATIBILITY π
The SQL standard says that it is implementation-dependent whether cursors are sensitive to concurrent updates of the underlying data by default. In PostgreSQL, cursors are insensitive by default, and can be made sensitive by specifying FOR UPDATE. Other products may work differently. not us being the main character, periodt π
The SQL standard allows cursors only in embedded SQL and in modules. PostgreSQL permits cursors to be used interactively. we're just built different β¨
Binary cursors are a PostgreSQL extension. lowkey iconic of us π
π¦ SEE ALSO π¦
CLOSE(7), FETCH(7), MOVE(7) β go read these too bestie, they're all connected π
π and that's the whole page! mwah! π
PostgreSQL 13.23 β dated 2025, like, literally the current vibe β¨ ngl this doc is kinda iconic, happy cursoring bestie! π¦πΈπ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms