The 30 second answer
Inserting rows one at a time pays a network round trip and often a commit for each row. A multi-row INSERT or a batch sends many rows in one statement, and COPY is the fastest way to bulk load in PostgreSQL. For large loads I also wrap everything in one transaction, and sometimes drop and recreate indexes afterwards, which the PostgreSQL docs recommend.
The PostgreSQL "Populating a Database" page recommends COPY for bulk loads, since it has much less overhead than many INSERTs. If you use INSERT, batch many rows per statement and per transaction. In Java, JDBC batching (addBatch) plus the pgJDBC setting reWriteBatchedInserts=true turns batches into multi-row INSERTs.
INSERT INTO events (id, type) VALUES (1,'a'), (2,'b'), (3,'c');Alternate interview scenarios
"Batched multi-row INSERTs top out well below your target write rate. What do you change next?"
I switch the bulk path to COPY and remove per-row work from the target table. In a public load test that pushed a URL shortener to 1 million requests per second, a consumer writing big multi-row INSERT batches into Postgres capped at around 700,000 rows per second. Switching to COPY FROM STDIN with CSV data let it keep up. The same test found that a foreign key from the views table to the links table roughly halved throughput, so it was dropped and the app deletes child rows itself, and the ID moved from a 32-bit to a 64-bit integer because the table was heading for billions of rows. That matches the PostgreSQL Populating a Database page: COPY is almost always faster than INSERT even when inserts are batched into one transaction, and a foreign key can be checked in bulk more efficiently than row by row. Dropping the foreign key gives up that integrity check, so I only do it on an append-only table, with orphan cleanup and a periodic check.
"You need to update a list of records efficiently, but the list contains duplicates. How do you do it?"
INSERT ... ON CONFLICT DO UPDATE, the command may not affect the same existing row twice, so a key that appears twice in one statement raises a cardinality violation error. With UPDATE ... FROM a list of values, a target row that matches two input rows gets updated from only one of them, and the docs say which one "is not readily predictable". So I collapse the list by id with an explicit rule. Usually the last occurrence wins, which a LinkedHashMap keyed by id gives me for free; sometimes the highest version wins.Then I send one set-based statement per chunk instead of one UPDATE per row. I pass the ids and values as two arrays and expand them with unnest, so the whole chunk is one round trip. I also sort by id before sending, so two concurrent batches lock rows in the same order and can't deadlock each other.
Map<Long, BigDecimal> latestPrice = new LinkedHashMap<>();
for (PriceChange change : changes) {
latestPrice.put(change.productId(), change.price()); // last one wins
}UPDATE product p
SET price = v.price
FROM unnest(?::bigint[], ?::numeric[]) AS v(id, price)
WHERE p.id = v.id;Sources (7)
- PostgreSQL 17: Populating a Databasepostgresql.org
- COPYpostgresql.org
- INSERTpostgresql.org
- pgJDBC connection propertiesjdbc.postgresql.org
- PostgreSQL 17 docs: UPDATE (FROM clause)postgresql.org
- PostgreSQL 17 docs: Array functions (unnest)postgresql.org
- PostgreSQL 17 docs: Explicit locking, deadlockspostgresql.org