The first way is slower than the second way.
First:
DO $$ DECLARE page int := 10000; min_id bigint; max_id bigint;BEGIN SELECT max(id),min(id) INTO max_id,min_id FROM opportunities; FOR j IN min_id..max_id BY page LOOP UPDATE opportunities SET sec_type = 'Unsec' FROM opportunities AS opp INNER JOIN accounts AS acc ON opp.account_id = acc.id WHERE acc.borrower = true AND opp.sec_type IS NULL AND opp.id >= j AND opp.id < j+page; COMMIT; END LOOP;END; $$;
Second:
DO $$ DECLARE page int := 10000; min_id bigint; max_id bigint;BEGIN SELECT max(id),min(id) INTO max_id,min_id FROM opportunities; FOR j IN min_id..max_id BY page LOOP UPDATE opportunities AS opp SET sec_type = 'Unsec' FROM accounts AS acc WHERE opp.account_id = acc.id AND opp.sec_type IS NULL AND acc.borrower = true AND opp.id >= j AND opp.id < j+page; COMMIT; END LOOP;END; $$;