🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

Databases Intermediate

What is Upsert?

A database operation that inserts a new row if it does not exist, or updates the existing row if it does.

Upsert (update + insert) eliminates the need for separate check-then-insert-or-update logic. In PostgreSQL: INSERT INTO table (...) VALUES (...) ON CONFLICT (key) DO UPDATE SET column = EXCLUDED.column. MySQL uses INSERT ... ON DUPLICATE KEY UPDATE.

Upserts are atomic (no race conditions), simpler than alternatives, and efficient for sync operations, bulk imports, and idempotent data loading. The ON CONFLICT clause specifies which constraint determines whether to insert or update.

Related Terms

EXPLAIN ANALYZE
A PostgreSQL command that shows the execution plan of a query along with actual runtime statistics for performance tuning.
Migration
A version-controlled change to a database schema that can be applied and reversed systematically.
Materialized View
A database object that stores the precomputed result of a query, offering faster reads at the cost of periodic refresh.
EXPLAIN Plan
A database command that shows how the query planner will execute a SQL query, revealing join methods, scan types, and estimated costs.
Vacuum
A PostgreSQL maintenance operation that reclaims storage from dead tuples and updates statistics for the query planner.
Deadlock
A situation where two or more transactions permanently block each other by each holding locks that the other needs.
View All Databases Terms →