-- Created on savesnippets.com · https://savesnippets.com/Ihnfv5aduvrbiZ -- Insert or bump the view count for a URL INSERT INTO page_views (url, views, last_seen) VALUES ('/about', 1, NOW()) ON CONFLICT (url) DO UPDATE SET views = page_views.views + 1, last_seen = EXCLUDED.last_seen; -- EXCLUDED.* refers to the row we tried to insert. -- Ignore the insert entirely on conflict (no-op upsert) INSERT INTO events (event_id, payload) VALUES ('evt_123', '{}'::jsonb) ON CONFLICT (event_id) DO NOTHING; -- Conditional update — only overwrite if newer INSERT INTO user_settings (user_id, theme, updated_at) VALUES (42, 'dark', NOW()) ON CONFLICT (user_id) DO UPDATE SET theme = EXCLUDED.theme, updated_at = EXCLUDED.updated_at WHERE user_settings.updated_at < EXCLUDED.updated_at; -- Composite unique constraint INSERT INTO favorites (user_id, post_id, created_at) VALUES (42, 9001, NOW()) ON CONFLICT (user_id, post_id) DO NOTHING;