-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07.upsert.sql
More file actions
140 lines (111 loc) · 3.64 KB
/
07.upsert.sql
File metadata and controls
140 lines (111 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
-- =====================================================
-- Basic UPSERT
-- Demonstrates: Insert a row or update if key exists
-- =====================================================
INSERT INTO customer_events (id, customer_id, event_data)
VALUES (1,
100,
'{
"event_type": "login",
"device": "mobile"
}')
ON CONFLICT (id)
DO UPDATE SET customer_id = EXCLUDED.customer_id,
event_data = EXCLUDED.event_data;
-- =====================================================
-- UPSERT Updating Only Specific Columns
-- Demonstrates: Update only event_data on conflict
-- =====================================================
INSERT INTO customer_events (id, customer_id, event_data)
VALUES (1,
100,
'{
"event_type": "logout",
"device": "desktop"
}')
ON CONFLICT (id)
DO UPDATE SET event_data = EXCLUDED.event_data;
-- =====================================================
-- UPSERT Merging JSON Data
-- Demonstrates: Combine existing JSON with new JSON
-- =====================================================
INSERT INTO customer_events (id, customer_id, event_data)
VALUES (2,
200,
'{
"amount": 300,
"currency": "USD"
}')
ON CONFLICT (id)
DO UPDATE SET event_data = customer_events.event_data || EXCLUDED.event_data;
-- =====================================================
-- UPSERT Only If Condition Is True
-- Demonstrates: Conditional updates using WHERE clause
-- =====================================================
INSERT INTO customer_events (id, customer_id, event_data)
VALUES (3,
300,
'{
"event_type": "purchase",
"amount": 500
}')
ON CONFLICT (id)
DO UPDATE SET event_data = EXCLUDED.event_data
WHERE (customer_events.event_data ->> 'event_type') != 'purchase';
-- =====================================================
-- UPSERT Incrementing Counters
-- Demonstrates: Atomic update pattern (common in analytics)
-- =====================================================
CREATE TABLE IF NOT EXISTS event_counters
(
event_type TEXT PRIMARY KEY,
total_events INT DEFAULT 0
);
INSERT INTO event_counters (event_type, total_events)
VALUES ('login', 1)
ON CONFLICT (event_type)
DO UPDATE SET total_events = event_counters.total_events + 1;
-- =====================================================
-- UPSERT with Timestamp Update
-- Demonstrates: Maintain last_updated metadata
-- =====================================================
CREATE TABLE IF NOT EXISTS customer_profiles
(
customer_id INT PRIMARY KEY,
profile JSONB,
last_updated TIMESTAMP
);
INSERT INTO customer_profiles (customer_id, profile, last_updated)
VALUES (1,
'{
"tier": "gold",
"preferences": {
"language": "en"
}
}',
now())
ON CONFLICT (customer_id)
DO UPDATE SET profile = EXCLUDED.profile,
last_updated = now();
-- =====================================================
-- UPSERT with Partial Unique Index
-- Demonstrates: Conflict resolution with business logic
-- =====================================================
CREATE TABLE IF NOT EXISTS user_sessions
(
user_id INT,
session_id TEXT,
active BOOLEAN,
created_at TIMESTAMP DEFAULT now()
);
CREATE UNIQUE INDEX IF NOT EXISTS unique_active_session
ON user_sessions (user_id)
WHERE active = TRUE;
INSERT INTO user_sessions (user_id, session_id, active)
VALUES (10, 'sess_abc123', TRUE)
ON CONFLICT (user_id)
WHERE active = TRUE
DO
UPDATE
SET session_id = EXCLUDED.session_id,
created_at = now();