{
    "mode": "info",
    "parameter": "FETCH",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/info/FETCH/json",
    "generated": "2026-07-07T02:38:50Z",
    "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",
    "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\nposition can be before the first row of the query result, on any\nparticular row of the result, or after the last row of the result. When\ncreated, a cursor is positioned before the first row. After fetching\nsome rows, the cursor is positioned on the row most recently retrieved.\nIf FETCH runs off the end of the available rows then the cursor is left\npositioned after the last row, or before the first row if fetching\nbackward.  FETCH ALL or FETCH BACKWARD ALL will always leave the cursor\npositioned after the last row or before the first row.\n\nThe forms NEXT, PRIOR, FIRST, LAST, ABSOLUTE, RELATIVE fetch a single\nrow after moving the cursor appropriately. If there is no such row, an\nempty result is returned, and the cursor is left positioned before the\nfirst row or after the last row as appropriate.\n\nThe forms using FORWARD and BACKWARD retrieve the indicated number of\nrows moving in the forward or backward direction, leaving the cursor\npositioned on the last-returned row (or after/before all rows, if the\ncount exceeds the number of rows available).\n\nRELATIVE 0, FORWARD 0, and BACKWARD 0 all request fetching the current\nrow without moving the cursor, that is, re-fetching the most recently\nfetched row. This will succeed unless the cursor is positioned before\nthe first row or after the last row; in which case, no row is returned.\n\nNote\nThis page describes usage of cursors at the SQL command level. If\nyou are trying to use cursors inside a PL/pgSQL function, the rules\nare different -- see Section 43.7.3.\n",
            "subsections": []
        },
        "PARAMETERS": {
            "content": "direction\ndirection defines the fetch direction and number of rows to fetch.\nIt can be one of the following:\n\nNEXT\nFetch the next row. This is the default if direction is\nomitted.\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\nfrom the end if count is negative. Position before first row or\nafter last row if count is out of range; in particular,\nABSOLUTE 0 positions before the first row.\n\nRELATIVE count\nFetch the count'th succeeding row, or the abs(count)'th prior\nrow if count is negative.  RELATIVE 0 re-fetches the current\nrow, 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\nrow.\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\nre-fetches the current row.\n\nBACKWARD ALL\nFetch all prior rows (scanning backwards).\n\ncount\ncount is a possibly-signed integer constant, determining the\nlocation or number of rows to fetch. For FORWARD and BACKWARD\ncases, specifying a negative count is equivalent to changing the\nsense 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\nform\n\nFETCH count\n\nThe count is the number of rows fetched (possibly zero). Note that in\npsql, the command tag will not actually be displayed, since psql\ndisplays the fetched rows instead.\n",
            "subsections": []
        },
        "NOTES": {
            "content": "The cursor should be declared with the SCROLL option if one intends to\nuse any variants of FETCH other than FETCH NEXT or FETCH FORWARD with a\npositive count. For simple queries PostgreSQL will allow backwards\nfetch from cursors not declared with SCROLL, but this behavior is best\nnot relied on. If the cursor is declared with NO SCROLL, no backward\nfetches are allowed.\n\nABSOLUTE fetches are not any faster than navigating to the desired row\nwith a relative move: the underlying implementation must traverse all\nthe intermediate rows anyway. Negative absolute fetches are even worse:\nthe query must be read to the end to find the last row, and then\ntraversed backward from there. However, rewinding to the start of the\nquery (as with FETCH ABSOLUTE 0) is fast.\n\nDECLARE is used to define a cursor. Use MOVE to change cursor position\nwithout retrieving data.\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\nvariant of FETCH described here returns the data as if it were a SELECT\nresult rather than placing it in host variables. Other than this point,\nFETCH is fully upward-compatible with the SQL standard.\n\nThe FETCH forms involving FORWARD and BACKWARD, as well as the forms\nFETCH count and FETCH ALL, in which FORWARD is implicit, are PostgreSQL\nextensions.\n\nThe SQL standard allows only FROM preceding the cursor name; the option\nto use IN, or to leave them out altogether, is an extension.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "CLOSE(7), DECLARE(7), MOVE(7)\n\nPostgreSQL 14.23                     2026                             FETCH(7)",
            "subsections": []
        }
    },
    "summary": "FETCH - retrieve rows from a query using a cursor",
    "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"
        }
    ]
}