Anil
Anil

Reputation: 1108

Looping over query result versus looping over explicitly declared cursor in Postgres

What is the difference in looping over query result vs looping over explicitly declared cursor in Postgres? Is looping over query result implicitly creating a cursor?

Looping over query result:

FOR v_employee IN
    SELECT *
    FROM employee
    WHERE ...
LOOP
    -- Do something with v_employee
END LOOP;

Looping over explicitly declared cursor:

FOR v_employee IN my_cursor
LOOP
    -- Do something with v_employee
END LOOP;

Upvotes: 1

Views: 82

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 247270

Yes, looping through a query result will use a cursor implicitly.

The performance should be the same, but the first syntax is often clearer.

Upvotes: 1

Related Questions