{
    "content": [
        {
            "type": "text",
            "text": "# FETCH (man)\n\n## NAME\n\nFETCH - retrieve rows from a query using a cursor\n\n## SYNOPSIS\n\nFETCH [ direction ] [ FROM | IN ] cursorname\nwhere direction can be one of:\nNEXT\nPRIOR\nFIRST\nLAST\nABSOLUTE count\nRELATIVE count\ncount\nALL\nFORWARD\nFORWARD count\nFORWARD ALL\nBACKWARD\nBACKWARD count\nBACKWARD ALL\n\n## DESCRIPTION\n\nFETCH retrieves rows using a previously-created cursor.\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS**\n- **DESCRIPTION**\n- **PARAMETERS**\n- **OUTPUTS**\n- **NOTES**\n- **EXAMPLES**\n- **COMPATIBILITY**\n- **SEE ALSO**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "FETCH",
        "section": "",
        "mode": "man",
        "summary": "FETCH - retrieve rows from a query using a cursor",
        "synopsis": "FETCH [ direction ] [ FROM | IN ] cursorname\nwhere direction can be one of:\nNEXT\nPRIOR\nFIRST\nLAST\nABSOLUTE count\nRELATIVE count\ncount\nALL\nFORWARD\nFORWARD count\nFORWARD ALL\nBACKWARD\nBACKWARD count\nBACKWARD ALL",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [
            "The following example traverses a table using a cursor:",
            "BEGIN WORK;",
            "-- Set up a cursor:",
            "DECLARE liahona SCROLL CURSOR FOR SELECT * FROM films;",
            "-- Fetch the first 5 rows in the cursor liahona:",
            "FETCH FORWARD 5 FROM liahona;",
            "code  |          title          | did | dateprod  |   kind   |  len",
            "-------+-------------------------+-----+------------+----------+-------",
            "BL101 | The Third Man           | 101 | 1949-12-23 | Drama    | 01:44",
            "BL102 | The African Queen       | 101 | 1951-08-11 | Romantic | 01:43",
            "JL201 | Une Femme est une Femme | 102 | 1961-03-12 | Romantic | 01:25",
            "P301 | Vertigo                 | 103 | 1958-11-14 | Action   | 02:08",
            "P302 | Becket                  | 103 | 1964-02-03 | Drama    | 02:28",
            "-- Fetch the previous row:",
            "FETCH PRIOR FROM liahona;",
            "code  |  title  | did | dateprod  |  kind  |  len",
            "-------+---------+-----+------------+--------+-------",
            "P301 | Vertigo | 103 | 1958-11-14 | Action | 02:08",
            "-- Close the cursor and end the transaction:",
            "CLOSE liahona;",
            "COMMIT WORK;"
        ],
        "see_also": [
            {
                "name": "CLOSE",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/CLOSE/7/json"
            },
            {
                "name": "DECLARE",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/DECLARE/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": 19,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 27,
                "subsections": []
            },
            {
                "name": "PARAMETERS",
                "lines": 58,
                "subsections": []
            },
            {
                "name": "OUTPUTS",
                "lines": 7,
                "subsections": []
            },
            {
                "name": "NOTES",
                "lines": 15,
                "subsections": []
            },
            {
                "name": "EXAMPLES",
                "lines": 29,
                "subsections": []
            },
            {
                "name": "COMPATIBILITY",
                "lines": 10,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 5,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "FETCH - retrieve rows from a query using a cursor\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "FETCH [ direction ] [ FROM | IN ] cursorname\n\nwhere direction can be one of:\n\nNEXT\nPRIOR\nFIRST\nLAST\nABSOLUTE count\nRELATIVE count\ncount\nALL\nFORWARD\nFORWARD count\nFORWARD ALL\nBACKWARD\nBACKWARD count\nBACKWARD ALL\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "FETCH retrieves rows using a previously-created cursor.\n\nA cursor has an associated position, which is used by FETCH. The cursor position can be\nbefore the first row of the query result, on any particular row of the result, or after the\nlast row of the result. When created, a cursor is positioned before the first row. After\nfetching some rows, the cursor is positioned on the row most recently retrieved. If FETCH\nruns off the end of the available rows then the cursor is left positioned after the last row,\nor before the first row if fetching backward.  FETCH ALL or FETCH BACKWARD ALL will always\nleave the cursor positioned after the last row or before the first row.\n\nThe forms NEXT, PRIOR, FIRST, LAST, ABSOLUTE, RELATIVE fetch a single row after moving the\ncursor appropriately. If there is no such row, an empty result is returned, and the cursor is\nleft positioned before the first row or after the last row as appropriate.\n\nThe forms using FORWARD and BACKWARD retrieve the indicated number of rows moving in the\nforward or backward direction, leaving the cursor positioned on the last-returned row (or\nafter/before all rows, if the count exceeds the number of rows available).\n\nRELATIVE 0, FORWARD 0, and BACKWARD 0 all request fetching the current row without moving the\ncursor, that is, re-fetching the most recently fetched row. This will succeed unless the\ncursor is positioned before the first row or after the last row; in which case, no row is\nreturned.\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.3.\n",
                "subsections": []
            },
            "PARAMETERS": {
                "content": "direction\ndirection defines the fetch direction and number of rows to fetch. It can be one of the\nfollowing:\n\nNEXT\nFetch the next row. This is the default if direction is omitted.\n\nPRIOR\nFetch the prior row.\n\nFIRST\nFetch the first row of the query (same as ABSOLUTE 1).\n\nLAST\nFetch the last row of the query (same as ABSOLUTE -1).\n\nABSOLUTE count\nFetch the count'th row of the query, or the abs(count)'th row from the end if count\nis negative. Position before first row or after last row if count is out of range; in\nparticular, ABSOLUTE 0 positions before the first row.\n\nRELATIVE count\nFetch the count'th succeeding row, or the abs(count)'th prior row if count is\nnegative.  RELATIVE 0 re-fetches the current row, if any.\n\ncount\nFetch the next count rows (same as FORWARD count).\n\nALL\nFetch all remaining rows (same as FORWARD ALL).\n\nFORWARD\nFetch the next row (same as NEXT).\n\nFORWARD count\nFetch the next count rows.  FORWARD 0 re-fetches the current row.\n\nFORWARD ALL\nFetch all remaining rows.\n\nBACKWARD\nFetch the prior row (same as PRIOR).\n\nBACKWARD count\nFetch the prior count rows (scanning backwards).  BACKWARD 0 re-fetches the current\nrow.\n\nBACKWARD ALL\nFetch all prior rows (scanning backwards).\n\ncount\ncount is a possibly-signed integer constant, determining the location or number of rows\nto fetch. For FORWARD and BACKWARD cases, specifying a negative count is equivalent to\nchanging the sense of FORWARD and BACKWARD.\n\ncursorname\nAn open cursor's name.\n",
                "subsections": []
            },
            "OUTPUTS": {
                "content": "On successful completion, a FETCH command returns a command tag of the form\n\nFETCH count\n\nThe count is the number of rows fetched (possibly zero). Note that in psql, the command tag\nwill not actually be displayed, since psql displays the fetched rows instead.\n",
                "subsections": []
            },
            "NOTES": {
                "content": "The cursor should be declared with the SCROLL option if one intends to use any variants of\nFETCH other than FETCH NEXT or FETCH FORWARD with a positive count. For simple queries\nPostgreSQL will allow backwards fetch from cursors not declared with SCROLL, but this\nbehavior is best not relied on. If the cursor is declared with NO SCROLL, no backward fetches\nare allowed.\n\nABSOLUTE fetches are not any faster than navigating to the desired row with a relative move:\nthe underlying implementation must traverse all the intermediate rows anyway. Negative\nabsolute fetches are even worse: the query must be read to the end to find the last row, and\nthen traversed backward from there. However, rewinding to the start of the query (as with\nFETCH ABSOLUTE 0) is fast.\n\nDECLARE is used to define a cursor. Use MOVE to change cursor position without retrieving\ndata.\n",
                "subsections": []
            },
            "EXAMPLES": {
                "content": "The following example traverses a table using a cursor:\n\nBEGIN WORK;\n\n-- Set up a cursor:\nDECLARE liahona SCROLL CURSOR FOR SELECT * FROM films;\n\n-- Fetch the first 5 rows in the cursor liahona:\nFETCH FORWARD 5 FROM liahona;\n\ncode  |          title          | did | dateprod  |   kind   |  len\n-------+-------------------------+-----+------------+----------+-------\nBL101 | The Third Man           | 101 | 1949-12-23 | Drama    | 01:44\nBL102 | The African Queen       | 101 | 1951-08-11 | Romantic | 01:43\nJL201 | Une Femme est une Femme | 102 | 1961-03-12 | Romantic | 01:25\nP301 | Vertigo                 | 103 | 1958-11-14 | Action   | 02:08\nP302 | Becket                  | 103 | 1964-02-03 | Drama    | 02:28\n\n-- Fetch the previous row:\nFETCH PRIOR FROM liahona;\n\ncode  |  title  | did | dateprod  |  kind  |  len\n-------+---------+-----+------------+--------+-------\nP301 | Vertigo | 103 | 1958-11-14 | Action | 02:08\n\n-- Close the cursor and end the transaction:\nCLOSE liahona;\nCOMMIT WORK;\n",
                "subsections": []
            },
            "COMPATIBILITY": {
                "content": "The SQL standard defines FETCH for use in embedded SQL only. The variant of FETCH described\nhere returns the data as if it were a SELECT result rather than placing it in host variables.\nOther than this point, FETCH is fully upward-compatible with the SQL standard.\n\nThe FETCH forms involving FORWARD and BACKWARD, as well as the forms FETCH count and FETCH\nALL, in which FORWARD is implicit, are PostgreSQL extensions.\n\nThe SQL standard allows only FROM preceding the cursor name; the option to use IN, or to\nleave them out altogether, is an extension.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "CLOSE(7), DECLARE(7), MOVE(7)\n\n\n\nPostgreSQL 14.23                                2026                                        FETCH(7)",
                "subsections": []
            }
        }
    }
}