Yes, there will be a huge speed difference, because COMMIT
is expensive. It requires a flush to disk. Unless you have fast write-back caching (battery-backed write-back caching RAID controller, or a good SSD), this takes quite a while. pg_test_fsync
will help you tell how long.
Batch the work into large transactions. If you need to rollback individual inserts you can use SAVEPOINT
s - though they come at somewhat of a performance cost, they're not as expensive as full transactions, especially if you RELEASE SAVEPOINT
them once the next is created.
There are diminishing returns with transaction size, so there's no point doing millions and millions in a single tx unless you need that for atomicity. Batch into decent sized chunks. The optimum size depends mostly on how expensive disk flushes are on your storage, but I'd aim for a few thousand rows to start with.
Better yet, use COPY
, which will be much faster, especially if network round-trips are involved.
For more detail see How to speed up insertion performance in PostgreSQL