BanglaTech

© 2026 Bangla Technologies. সর্বস্বত্ব সংরক্ষিত.

একটিNerddevs Ltd-এর প্রোডাক্ট
হোমঅনুসন্ধানআমাদের সম্পর্কেটিউটোরিয়ালশিক্ষকদের জন্যকোচিং সেন্টারের জন্যগোপনীয়তা নীতিসেবার শর্তাবলি

In PostgreSQL, UPSERT

a
admin
March 21, 2026 · 136 views
~2 মিনিট পড়তে
16px

UPSERT means update if the row exists, otherwise insert it.

It combines INSERT + UPDATE into one operation.

In PostgreSQL this is done using:

INSERT ... ON CONFLICT


  1. Basic Idea

Suppose you have a table:

CREATE TABLE users (
  id INT PRIMARY KEY,
  name TEXT
);

If you try:

INSERT INTO users (id, name)

VALUES (1, 'Delwar');

If id = 1 already exists → PostgreSQL will throw an error.

UPSERT solves this.


  1. UPSERT Example

INSERT INTO users (id, name)

VALUES (1, 'Delwar')

ON CONFLICT (id)

DO UPDATE SET name = EXCLUDED.name;

Behavior:

CaseResultid does not existINSERTid existsUPDATE


  1. What is EXCLUDED?

EXCLUDED represents the new value you tried to insert.

Example:

INSERT INTO users (id, name)

VALUES (1, 'Rahim')

ON CONFLICT (id)

DO UPDATE SET name = EXCLUDED.name;

If row exists:

id | name

1 | Delwar

After UPSERT:

id | name

1 | Rahim


  1. Do Nothing Option

Sometimes you want to skip duplicates.

INSERT INTO users (id, name)

VALUES (1, 'Delwar')

ON CONFLICT (id)

DO NOTHING;

If id = 1 exists → nothing happens.


  1. Real Example (Similar to Your Migration Case)

Suppose you have:

OrganizationReportPermission

organizationId UNIQUE

You can UPSERT like this:

INSERT INTO "OrganizationReportPermission"

("organizationId", "permissionToggles", "version")
VALUES ('org-123', '{}', 1)

ON CONFLICT ("organizationId")
DO UPDATE
SET "permissionToggles" = EXCLUDED."permissionToggles",
    "updatedAt" = now();

Meaning:

CaseActionorganization not existscreate roworganization existsupdate permission


  1. Why UPSERT is Important

Without UPSERT you must do:

SELECT

IF EXISTS

UPDATE

ELSE

INSERT

That causes race conditions.

Example problem:

Process A -> SELECT (not found)

Process B -> SELECT (not found)

Process A -> INSERT

Process B -> INSERT -> ERROR

UPSERT is atomic and prevents this.


  1. Short Summary

UPSERT =

INSERT

if conflict

UPDATE

PostgreSQL syntax:

INSERT ...

ON CONFLICT (...)

DO UPDATE ...


✅ Simple rule

CommandMeaningINSERTalways insertUPDATEupdate existingUPSERTinsert or update

আরও দেখুন

🏆
কুইজ প্রতিযোগিতায় অংশ নিন
ফ্রি অনলাইন কুইজ, জিতুন পুরস্কার।
✏️
নিজে কুইজ তৈরি করুন
শিক্ষক ও টিউটরদের জন্য ফ্রি টুলস।
✨
BanglaTech সম্পর্কে
আমাদের গল্প ও মিশন।

Comments (0)

Login to leave a comment.