Bulk/batch update/upsert in PostgreSQL
Execute a batch upsert in PostgreSQL with INSERT ... ON CONFLICT
, which either inserts new rows or updates existing ones based on a unique constraint like id. Here's a concise example:
As highlighted here, if an id already exists, the corresponding column1 and column2 will be updated with the new values. New rows are inserted when there isn't any conflict. The EXCLUDED pseudo-table signifies the propositioned insert.
The unnest()
method for bulk inserts
PostgreSQL 8.4 and above support unnest()
function, a handy tool for unpacking arrays into rows for bulk inserts. Let's dive into a simple illustration:
This array trick reduces SQL operations, improving performance with large datasets.
Employ UPDATE...FROM
for various conditions
For a more verbose update scenario where various conditions have to be met, UPDATE...FROM
has got your back:
This UPDATE...FROM
technique minimizes unnecessary operations and accelerates your work.
Optimizing with Hibernate's batch
When coupling JDBC with Hibernate, strategically flushing your session and configuring Hibernate’s @BatchSize
can do wonders:
By doing this, we are dramatically improving the SQL operations’ efficiency.
JSON data with batch updates
For the PostgreSQL 9.3 users, json_array_elements()
is a gift you should unwrap to perform batch updates with JSON data:
This command converts a JSON array into rows, making your update life easier.
Multiple rows insertion with VALUES
The VALUES
keyword encourages multiple row insertions in one query:
The keyword streamlines your bulk operations and keeps your database smiling.
Complicated batch? Use DO
block
PostgreSQL brings ‘DO’ block to the rescue whenever a more complicated update involving loops is required:
But remember, tucking logic in database might turn your maintenance into a hurdle.
Hibernate's 'IN' operator to reduce SELECTs
Instead of executing many SELECT statements, get smart and use IN
operator:
This lowers query count and matters when id list is already known.
Specific column update with column-list syntax
PostgreSQL's column-list syntax permits targeted updates via row-valued expressions, which optimizes SQL execution:
This feature works wonders when you intend to update several but not all columns.
Was this article helpful?