UPSERT statement inserts rows in cases where specified values do not violate uniqueness constraints and updates rows in cases where values do violate uniqueness constraints. UPSERT considers uniqueness only for primary key columns.
UPSERT vs. INSERT ON CONFLICT
Assuming that columnsa and b are the primary key, the following UPSERT and INSERT ... ON CONFLICT statements are equivalent:
UPSERT does not let you specify columns to infer a unique constraint as an arbiter. UPSERT always uses the primary key as the arbiter. You must therefore use INSERT ... ON CONFLICT ... DO UPDATE if your statement considers uniqueness for columns other than primary key columns.
Considerations
-
An
UPSERTstatement affecting a proper subset of columns behaves differently depending on whether or not you specify the target columns:- If you specify target columns (e.g.,
UPSERT INTO accounts (id, name) VALUES (2, 'b2');), the values of columns that do not have new values will not be updated. - If you do not specify target columns (e.g.,
UPSERT INTO accounts VALUES (2, 'b2');), the value of columns that do not have new values will be updated to their default values.
- If you specify target columns (e.g.,
-
A single multi-row
UPSERTstatement is faster than multiple single-rowUPSERTstatements. Whenever possible, use multi-rowUPSERTinstead of multiple single-rowUPSERTstatements.
Required privileges
You must have theINSERT, SELECT, and UPDATE privileges on the table.
Syntax
Parameters
See Common Table Expressions.
The name of the table.
An alias for the table name. When an alias is provided, it completely hides the actual table name.
The name of a column to populate during the upsert.
A selection query. Each value must match the data type of its column. If column names are listed after
INTO, values must be in corresponding order; otherwise, they must follow the declared order of the columns in the table.To fill all columns with their default values, use
DEFAULT VALUES in place of select_stmt. To fill a specific column with its default value, leave the value out of the select_stmt or use DEFAULT at the appropriate position.Return values based on rows upserted, where
target_list can be specific column names from the table, * for all columns, or computations using scalar expressions.Within a transaction, use RETURNING NOTHING to return nothing in the response, not even the number of rows affected.Examples
Upsert a row (no conflict)
In this example, theid column is the primary key. Because the inserted id value does not conflict with any existing row, the UPSERT statement inserts a new row.
Upsert multiple rows
Upsert that updates a row (conflict on primary key)
In this example, theid column is the primary key. Because the inserted id value is not unique, the UPSERT statement updates the row with the new balance.
Upsert that fails (conflict on non-primary key)
UPSERT will not update rows when the uniqueness conflict is on columns not in the primary key. In this example, the a column is the primary key, but the b column also has the UNIQUE constraint.
INSERT ON CONFLICT statement:
Upsert a proper subset of columns
See also
- INSERT
- UPDATE
- DELETE
- SELECT