{
    "content": [
        {
            "type": "text",
            "text": "# DECLARE (man)\n\n## NAME\n\nDECLARE - define a cursor\n\n## SYNOPSIS\n\nDECLARE name [ BINARY ] [ ASENSITIVE | INSENSITIVE ] [ [ NO ] SCROLL ]\nCURSOR [ { WITH | WITHOUT } HOLD ] FOR query\n\n## DESCRIPTION\n\nDECLARE allows a user to create cursors, which can be used to retrieve a small number of rows\nat a time out of a larger query. After the cursor is created, rows are fetched from it using\nFETCH.\n\n## TLDR\n\n> Declare variables and give them attributes.\n\n- Declare a string variable with the specified value:\n  `declare {{variable}}=\"{{value}}\"`\n- Declare an integer variable with the specified value:\n  `declare -i {{variable}}=\"{{value}}\"`\n- Declare an array variable with the specified value:\n  `declare -a {{variable}}=({{item_a item_b item_c}})`\n- Declare an associative array variable with the specified value:\n  `declare -A {{variable}}=({{key_a]=item_a [key_b]=item_b [key_c]=item_c}})`\n- Declare a readonly string variable with the specified value:\n  `declare -r {{variable}}=\"{{value}}\"`\n- Declare a global variable within a function with the specified value:\n  `declare -g {{variable}}=\"{{value}}\"`\n- Print a function definition:\n  `declare -f {{function_name}}`\n- Print a variable definition:\n  `declare -p {{variable_name}}`\n\n*Source: tldr-pages*\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS**\n- **DESCRIPTION**\n- **PARAMETERS**\n- **NOTES**\n- **EXAMPLES**\n- **COMPATIBILITY**\n- **SEE ALSO**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "DECLARE",
        "section": "",
        "mode": "man",
        "summary": "DECLARE - define a cursor",
        "synopsis": "DECLARE name [ BINARY ] [ ASENSITIVE | INSENSITIVE ] [ [ NO ] SCROLL ]\nCURSOR [ { WITH | WITHOUT } HOLD ] FOR query",
        "tldr_summary": "Declare variables and give them attributes.",
        "tldr_examples": [
            {
                "description": "Declare a string variable with the specified value",
                "command": "declare {{variable}}=\"{{value}}\""
            },
            {
                "description": "Declare an integer variable with the specified value",
                "command": "declare -i {{variable}}=\"{{value}}\""
            },
            {
                "description": "Declare an array variable with the specified value",
                "command": "declare -a {{variable}}=({{item_a item_b item_c}})"
            },
            {
                "description": "Declare an associative array variable with the specified value",
                "command": "declare -A {{variable}}=({{key_a]=item_a [key_b]=item_b [key_c]=item_c}})"
            },
            {
                "description": "Declare a readonly string variable with the specified value",
                "command": "declare -r {{variable}}=\"{{value}}\""
            },
            {
                "description": "Declare a global variable within a function with the specified value",
                "command": "declare -g {{variable}}=\"{{value}}\""
            },
            {
                "description": "Print a function definition",
                "command": "declare -f {{function_name}}"
            },
            {
                "description": "Print a variable definition",
                "command": "declare -p {{variable_name}}"
            }
        ],
        "tldr_source": "official",
        "flags": [],
        "examples": [
            "To declare a cursor:",
            "DECLARE liahona CURSOR FOR SELECT * FROM films;",
            "See FETCH(7) for more examples of cursor usage."
        ],
        "see_also": [
            {
                "name": "CLOSE",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/CLOSE/7/json"
            },
            {
                "name": "FETCH",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/FETCH/7/json"
            },
            {
                "name": "MOVE",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/MOVE/7/json"
            }
        ],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 3,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 8,
                "subsections": []
            },
            {
                "name": "PARAMETERS",
                "lines": 39,
                "subsections": []
            },
            {
                "name": "NOTES",
                "lines": 84,
                "subsections": []
            },
            {
                "name": "EXAMPLES",
                "lines": 6,
                "subsections": []
            },
            {
                "name": "COMPATIBILITY",
                "lines": 10,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 5,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "DECLARE - define a cursor\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "DECLARE name [ BINARY ] [ ASENSITIVE | INSENSITIVE ] [ [ NO ] SCROLL ]\nCURSOR [ { WITH | WITHOUT } HOLD ] FOR query\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "DECLARE allows a user to create cursors, which can be used to retrieve a small number of rows\nat a time out of a larger query. After the cursor is created, rows are fetched from it using\nFETCH.\n\nNote\nThis page describes usage of cursors at the SQL command level. If you are trying to use\ncursors inside a PL/pgSQL function, the rules are different — see Section 43.7.\n",
                "subsections": []
            },
            "PARAMETERS": {
                "content": "name\nThe name of the cursor to be created.\n\nBINARY\nCauses the cursor to return data in binary rather than in text format.\n\nASENSITIVE\nINSENSITIVE\nCursor sensitivity determines whether changes to the data underlying the cursor, done in\nthe same transaction, after the cursor has been declared, are visible in the cursor.\nINSENSITIVE means they are not visible, ASENSITIVE means the behavior is\nimplementation-dependent. A third behavior, SENSITIVE, meaning that such changes are\nvisible in the cursor, is not available in PostgreSQL. In PostgreSQL, all cursors are\ninsensitive; so these key words have no effect and are only accepted for compatibility\nwith the SQL standard.\n\nSpecifying INSENSITIVE together with FOR UPDATE or FOR SHARE is an error.\n\nSCROLL\nNO SCROLL\nSCROLL specifies that the cursor can be used to retrieve rows in a nonsequential fashion\n(e.g., backward). Depending upon the complexity of the query's execution plan, specifying\nSCROLL might impose a performance penalty on the query's execution time.  NO SCROLL\nspecifies that the cursor cannot be used to retrieve rows in a nonsequential fashion. The\ndefault is to allow scrolling in some cases; this is not the same as specifying SCROLL.\nSee Notes below for details.\n\nWITH HOLD\nWITHOUT HOLD\nWITH HOLD specifies that the cursor can continue to be used after the transaction that\ncreated it successfully commits.  WITHOUT HOLD specifies that the cursor cannot be used\noutside of the transaction that created it. If neither WITHOUT HOLD nor WITH HOLD is\nspecified, WITHOUT HOLD is the default.\n\nquery\nA SELECT or VALUES command which will provide the rows to be returned by the cursor.\n\nThe key words ASENSITIVE, BINARY, INSENSITIVE, and SCROLL can appear in any order.\n",
                "subsections": []
            },
            "NOTES": {
                "content": "Normal cursors return data in text format, the same as a SELECT would produce. The BINARY\noption specifies that the cursor should return data in binary format. This reduces conversion\neffort for both the server and client, at the cost of more programmer effort to deal with\nplatform-dependent binary data formats. As an example, if a query returns a value of one from\nan integer column, you would get a string of 1 with a default cursor, whereas with a binary\ncursor you would get a 4-byte field containing the internal representation of the value (in\nbig-endian byte order).\n\nBinary cursors should be used carefully. Many applications, including psql, are not prepared\nto handle binary cursors and expect data to come back in the text format.\n\nNote\nWhen the client application uses the “extended query” protocol to issue a FETCH command,\nthe Bind protocol message specifies whether data is to be retrieved in text or binary\nformat. This choice overrides the way that the cursor is defined. The concept of a binary\ncursor as such is thus obsolete when using extended query protocol — any cursor can be\ntreated as either text or binary.\n\nUnless WITH HOLD is specified, the cursor created by this command can only be used within the\ncurrent transaction. Thus, DECLARE without WITH HOLD is useless outside a transaction block:\nthe cursor would survive only to the completion of the statement. Therefore PostgreSQL\nreports an error if such a command is used outside a transaction block. Use BEGIN and COMMIT\n(or ROLLBACK) to define a transaction block.\n\nIf WITH HOLD is specified and the transaction that created the cursor successfully commits,\nthe cursor can continue to be accessed by subsequent transactions in the same session. (But\nif the creating transaction is aborted, the cursor is removed.) A cursor created with WITH\nHOLD is closed when an explicit CLOSE command is issued on it, or the session ends. In the\ncurrent implementation, the rows represented by a held cursor are copied into a temporary\nfile or memory area so that they remain available for subsequent transactions.\n\nWITH HOLD may not be specified when the query includes FOR UPDATE or FOR SHARE.\n\nThe SCROLL option should be specified when defining a cursor that will be used to fetch\nbackwards. This is required by the SQL standard. However, for compatibility with earlier\nversions, PostgreSQL will allow backward fetches without SCROLL, if the cursor's query plan\nis simple enough that no extra overhead is needed to support it. However, application\ndevelopers are advised not to rely on using backward fetches from a cursor that has not been\ncreated with SCROLL. If NO SCROLL is specified, then backward fetches are disallowed in any\ncase.\n\nBackward fetches are also disallowed when the query includes FOR UPDATE or FOR SHARE;\ntherefore SCROLL may not be specified in this case.\n\nCaution\nScrollable cursors may give unexpected results if they invoke any volatile functions (see\nSection 38.7). When a previously fetched row is re-fetched, the functions might be\nre-executed, perhaps leading to results different from the first time. It's best to\nspecify NO SCROLL for a query involving volatile functions. If that is not practical, one\nworkaround is to declare the cursor SCROLL WITH HOLD and commit the transaction before\nreading any rows from it. This will force the entire output of the cursor to be\nmaterialized in temporary storage, so that volatile functions are executed exactly once\nfor each row.\n\nIf the cursor's query includes FOR UPDATE or FOR SHARE, then returned rows are locked at the\ntime they are first fetched, in the same way as for a regular SELECT command with these\noptions. In addition, the returned rows will be the most up-to-date versions.\n\nCaution\nIt is generally recommended to use FOR UPDATE if the cursor is intended to be used with\nUPDATE ... WHERE CURRENT OF or DELETE ... WHERE CURRENT OF. Using FOR UPDATE prevents\nother sessions from changing the rows between the time they are fetched and the time they\nare updated. Without FOR UPDATE, a subsequent WHERE CURRENT OF command will have no\neffect if the row was changed since the cursor was created.\n\nAnother reason to use FOR UPDATE is that without it, a subsequent WHERE CURRENT OF might\nfail if the cursor query does not meet the SQL standard's rules for being “simply\nupdatable” (in particular, the cursor must reference just one table and not use grouping\nor ORDER BY). Cursors that are not simply updatable might work, or might not, depending\non plan choice details; so in the worst case, an application might work in testing and\nthen fail in production. If FOR UPDATE is specified, the cursor is guaranteed to be\nupdatable.\n\nThe main reason not to use FOR UPDATE with WHERE CURRENT OF is if you need the cursor to\nbe scrollable, or to be isolated from concurrent updates (that is, continue to show the\nold data). If this is a requirement, pay close heed to the caveats shown above.\n\nThe SQL standard only makes provisions for cursors in embedded SQL. The PostgreSQL server\ndoes not implement an OPEN statement for cursors; a cursor is considered to be open when it\nis declared. However, ECPG, the embedded SQL preprocessor for PostgreSQL, supports the\nstandard SQL cursor conventions, including those involving DECLARE and OPEN statements.\n\nYou can see all available cursors by querying the pgcursors system view.\n",
                "subsections": []
            },
            "EXAMPLES": {
                "content": "To declare a cursor:\n\nDECLARE liahona CURSOR FOR SELECT * FROM films;\n\nSee FETCH(7) for more examples of cursor usage.\n",
                "subsections": []
            },
            "COMPATIBILITY": {
                "content": "The SQL standard allows cursors only in embedded SQL and in modules.  PostgreSQL permits\ncursors to be used interactively.\n\nAccording to the SQL standard, changes made to insensitive cursors by UPDATE ... WHERE\nCURRENT OF and DELETE ... WHERE CURRENT OF statements are visible in that same cursor.\nPostgreSQL treats these statements like all other data changing statements in that they are\nnot visible in insensitive cursors.\n\nBinary cursors are a PostgreSQL extension.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "CLOSE(7), FETCH(7), MOVE(7)\n\n\n\nPostgreSQL 14.23                                2026                                      DECLARE(7)",
                "subsections": []
            }
        }
    }
}