postgresql_16:6.6.7 Cursors
PostgreSQL 16 > Server Programming > PL/pgSQL — SQL Procedural Language > Cursors
Rather than executing a whole query at once, it is possible to set up a *cursor* that encapsulates the query, and then read the query result a few rows at a time. One reason for doing this is to avoid memory overrun when the result contains a large number of rows. (However, PL/pgSQL users do not normally need to worry about that, since FOR loops automatically use a cursor internally to avoid memory problems.) A more interesting usage is to return a reference to a cursor that a function has created, allowing the caller to read the rows. This provides an efficient way to return large row sets from functions.
Subsections
- postgresql_16:6.6.7.1 Declaring Cursor Variables
- postgresql_16:6.6.7.2 Opening Cursors
- postgresql_16:6.6.7.3 Using Cursors
- postgresql_16:6.6.7.4 Looping through a Cursor's Result