FETCH

An SQL command for retrieving rows from a cursor

FETCH is a utility for command for retrieving rows from a previously defined cursor.

FETCH has always been present in PostgreSQL.

Change history

Work-in-progress

FETCH has been present in every PostgreSQL version.

Examples

Basic usage example for FETCH:

postgres=# BEGIN ;
BEGIN

postgres=*# DECLARE foo_cursor CURSOR
                FOR SELECT * FROM foo;
DECLARE CURSOR

postgres=*# FETCH 1 FROM foo_cursor;
 id |             val              
----+------------------------------
  1 | 2022-10-25 08:21:20.67438+01
(1 row)

Attempting to retrieve rows from a non-existent cursor:

postgres=*# CLOSE foo_cursor ;
CLOSE CURSOR

postgres=*# FETCH 1 FROM foo_cursor;
ERROR:  cursor "foo_cursor" does not exist
  • PostgreSQL documentation: FETCH

Categories

Cursors, SQL command

See also

CLOSE, DECLARE, MOVE, pg_cursors