You're right, that is just an example.
I'm basically using a CTE to select the data and then, inserting some rows onto a new table.
I just don't know how to tell my function to perform 2000 records at once, and then when calling it again it will "know" where to start from
UPDATE tablea a SET migrated = yes WHERE a.id = row.id;
On my original select, the row will have migrated = false. Maybe All I need to put is a limit 2000 and the query will do the rest?
CREATE or REPLACE FUNCTION migrate_data()
RETURNS integer;
declare row record;
BEGIN
FOR row IN EXECUTE ' SELECT id FROM tablea WHERE migrated = false
'
LOOP
INSERT INTO tableb (id)
VALUES (row.id);
UPDATE tablea a SET migrated = yes WHERE a.id = row.id;
END LOOP;
RETURN num_rows; -- I want it to return the number of processed rows
END
$$ language 'plpgsql';