Cursors — Row-by-Row Processing for Large Result Sets
Cursors — Row-by-Row Processing for Large Result Sets
Most PL/pgSQL should use set-based operations — they are faster and simpler. But sometimes you genuinely need to process rows one at a time: complex business logic that cannot be expressed as a single SQL statement, streaming large result sets to avoid loading millions of rows into memory, or coordinating multi-step workflows per row. Cursors are the PostgreSQL mechanism for controlled row-by-row iteration.