Short
Quote from the docs
The SET and WHERE clauses in ON CONFLICT DO UPDATE have access to the existing row using the table's name (or an alias), and to rows proposed for insertion using the special excluded table.
In your example, excluded.temperature
has the value 70.2
Details
See also DbFiddle example:
Let's assume, that the table already has a row with this primary key ('2017-07-28 11:42:42.846621+00'
, office
):
time |
location |
temperature |
humidity |
2017-07-28 12:42:42.846621+01 |
office |
60.1 |
50 |
Now we execute an insert statements (with the same primary key) and different ON CONFLICT
clauses
No ON CONFLICT
clause
INSERT INTO conditions VALUES ('2017-07-28 11:42:42.846621+00', 'office', 70.2, 50.1)
When we do not use ON CONFLICT
, we get an error:
ERROR: duplicate key value violates unique constraint "conditions_time_location_key"
DETAIL: Key ("time", location)=(2017-07-28 12:42:42.846621+01, office) already exists.
ON CONFLICT DO NOTHING
INSERT INTO conditions
VALUES ('2017-07-28 11:42:42.846621+00', 'office', 70.2, 50.1)
ON CONFLICT DO NOTHING
In this case the new data is ignored and the row remains the same as before:
time |
location |
temperature |
humidity |
2017-07-28 12:42:42.846621+01 |
office |
60.1 |
50 |
ON CONFLICT DO UPDATE
example:
INSERT INTO conditions
VALUES ('2017-07-28 11:42:42.846621+00', 'office', 70.2, 50.1)
ON CONFLICT (time, location) DO UPDATE
SET humidity = excluded.temperature;
will result in:
time |
location |
temperature |
humidity |
2017-07-28 12:42:42.846621+01 |
office |
60.1 |
70.2 |
NOTE: that we assign humidity
to excluded.temperature
- this does not really make sense in a production app, and is only used to illustrate how this works
When we use ON CONFLICT DO UPDATE
, we have access to a special table named exclude
.
This table contains the values from our insert statement that have been excluded (i.e. ignored), because other values already exist.
In our case:
excluded.temperature
is 70.2
excluded.humidity
is 50.1
When you do no use SET
for a column, the column keeps the old value: i.e. the temperature
will still be 60.1
after our insert statement.
When you do use SET
for a column you can assign any value you like: e.g. a constant value, an expression, or the values from the special excluded
table which contains the excluded row.