Bulk Inserts in Postgres
I would convert this into a single INSERT statement using on conflict do deal with the &quot;insert or update&quot; part:
insert into test1(id, name)
select id, name 
from test
on conflict (id) do update
  set name = excluded.name;

This assumes that there is a unique index (or constraint) on test1.id