@@ -5,9 +5,7 @@ keywords: [upsert, hypertables]
5
5
---
6
6
7
7
# Upsert data
8
-
9
8
Upserting is shorthand for:
10
-
11
9
* Inserting a new row if a matching row doesn't already exist
12
10
* Either updating the existing row, or doing nothing, if a matching row
13
11
already exists
@@ -22,11 +20,9 @@ If you have a primary key, you automatically have a unique index.
22
20
</highlight >
23
21
24
22
## Create a table with a unique constraint
25
-
26
23
The examples in this section use a ` conditions ` table with a unique constraint
27
24
on the columns ` (time, location) ` . To create a unique constraint, use `UNIQUE
28
25
(<COLUMNS >)` while defining your table:
29
-
30
26
``` sql
31
27
CREATE TABLE conditions (
32
28
time TIMESTAMPTZ NOT NULL ,
@@ -40,7 +36,6 @@ CREATE TABLE conditions (
40
36
You can also create a unique constraint after the table is created. Use the
41
37
syntax ` ALTER TABLE ... ADD CONSTRAINT ... UNIQUE ` . In this example, the
42
38
constraint is named ` conditions_time_location ` :
43
-
44
39
``` sql
45
40
ALTER TABLE conditions
46
41
ADD CONSTRAINT conditions_time_location
@@ -61,14 +56,12 @@ well. For more information, see the section on
61
56
</highlight >
62
57
63
58
## Insert or update data to a table with a unique constraint
64
-
65
59
You can tell the database to insert new data if it doesn't violate the
66
60
constraint, and to update the existing row if it does. Use the syntax `INSERT
67
61
INTO ... VALUES ... ON CONFLICT ... DO UPDATE`.
68
62
69
63
For example, to update the ` temperature ` and ` humidity ` values if a row with the
70
- specified ` time ` and ` location ` already exists, run:
71
-
64
+ specified ` time ` and ` location ` already exists, run:
72
65
``` sql
73
66
INSERT INTO conditions
74
67
VALUES (' 2017-07-28 11:42:42.846621+00' , ' office' , 70 .2 , 50 .1 )
@@ -78,19 +71,37 @@ INSERT INTO conditions
78
71
```
79
72
80
73
## Insert or do nothing to a table with a unique constraint
81
-
82
74
You can also tell the database to do nothing if the constraint is violated. The
83
75
new data is not inserted, and the old row is not updated. This is useful when
84
76
writing many rows as one batch, to prevent the entire transaction from failing.
85
77
The database engine skips the row and moves on.
86
78
87
79
To insert or do nothing, use the syntax `INSERT INTO ... VALUES ... ON CONFLICT
88
80
DO NOTHING`:
89
-
90
81
``` sql
91
82
INSERT INTO conditions
92
83
VALUES (' 2017-07-28 11:42:42.846621+00' , ' office' , 70 .1 , 50 .0 )
93
84
ON CONFLICT DO NOTHING;
94
85
```
95
86
87
+ <highlight type =" note " >
88
+ You cannot specify a constraint by name when using ` INSERT ... ON CONFLICT ` on
89
+ hypertables. Work around this by explicitly specifying the constrained columns.
90
+ For example, instead of this:
91
+
92
+ ``` sql
93
+ -- This does not work
94
+ INSERT INTO conditions
95
+ VALUES (' 2017-07-28 11:42:42.846621+00' , ' office' , 70 .1 , 50 .0 )
96
+ ON CONFLICT ON CONSTRAINT conditions_time_location DO NOTHING;
97
+ ```
98
+
99
+ Do this:
100
+ ``` sql
101
+ INSERT INTO conditions
102
+ VALUES (' 2017-07-28 11:42:42.846621+00' , ' office' , 70 .1 , 50 .0 )
103
+ ON CONFLICT (time , location) DO NOTHING;
104
+ ```
105
+ </highlight >
106
+
96
107
[ postgres-upsert ] : https://www.postgresql.org/docs/current/static/sql-insert.html#SQL-ON-CONFLICT
0 commit comments