0Pricing
PostgreSQL Performance & Query Optimization · บทเรียน

การทำ Upsert ขนาดใหญ่ด้วย ON CONFLICT

นำตรรกะผสานข้อมูลสำหรับชุดข้อมูลขนาดใหญ่ไปใช้ให้มีประสิทธิภาพ พร้อมหลีกเลี่ยงการแข่งขันแย่งล็อกและการพองตัว

การทำ Upsert ขนาดใหญ่ด้วย ON CONFLICT เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Upserts Need Care at Scale

An upsert inserts a row, but if it would collide with an existing key, it updates the existing row instead. PostgreSQL spells this INSERT ... ON CONFLICT.

For small workloads it is trivial. For ETL-sized batches (tens of thousands to millions of rows) the naive approach causes three problems:

  • Lock contention — concurrent writers fighting over the same rows or index pages.
  • Table bloat — every UPDATE writes a new row version (dead tuple) that VACUUM must later reclaim.
  • WAL and round-trip overhead — row-by-row upserts multiply network and transaction cost.

This lesson builds a merge that is both correct and throughput-friendly.

The ON CONFLICT Shape

ON CONFLICT requires a conflict target: the column(s) or constraint that define a duplicate. PostgreSQL needs a unique or exclusion constraint on that target to arbitrate.

The two actions are DO NOTHING (skip the colliding row) and DO UPDATE (merge new values in).

Inside DO UPDATE, the incoming row is exposed through the special EXCLUDED pseudo-table.

INSERT INTO products (sku, name, price)
VALUES ('A-100', 'Widget', 9.99)
ON CONFLICT (sku) DO UPDATE
  SET name  = EXCLUDED.name,
      price = EXCLUDED.price;

One Statement, Many Rows

The single most important throughput rule: batch your rows into one statement. A multi-row VALUES list (or a feeding SELECT) is parsed, planned, and committed once instead of N times.

This collapses N network round-trips and N transaction commits into one, often a 10-100x speedup over row-by-row upserts.

INSERT INTO products (sku, name, price)
VALUES
  ('A-100', 'Widget', 9.99),
  ('A-101', 'Gadget', 14.50),
  ('A-102', 'Gizmo', 7.25)
ON CONFLICT (sku) DO UPDATE
  SET name  = EXCLUDED.name,
      price = EXCLUDED.price;

Staging Table + INSERT...SELECT

For real ETL, load raw data into an unlogged staging table first (often via COPY, the fastest bulk path), then merge from staging into the target with one INSERT ... SELECT ... ON CONFLICT.

Benefits:

  • COPY avoids per-row INSERT overhead.
  • An UNLOGGED staging table skips WAL for the load phase.
  • You can dedupe and transform in the SELECT before merging.
CREATE UNLOGGED TABLE products_stage (LIKE products);

-- bulk load: COPY products_stage FROM '/data/products.csv' CSV;

INSERT INTO products (sku, name, price)
SELECT sku, name, price FROM products_stage
ON CONFLICT (sku) DO UPDATE
  SET name  = EXCLUDED.name,
      price = EXCLUDED.price;

Deduplicate the Batch First

A subtle but fatal error: a single INSERT statement cannot update the same target row twice. If your batch contains two rows with the same conflict key, PostgreSQL raises:

ERROR: ON CONFLICT DO UPDATE command cannot affect row a second time

Fix it by collapsing duplicates in the source before merging. DISTINCT ON keeps one row per key — typically the newest.

INSERT INTO products (sku, name, price)
SELECT DISTINCT ON (sku) sku, name, price
FROM products_stage
ORDER BY sku, updated_at DESC
ON CONFLICT (sku) DO UPDATE
  SET name  = EXCLUDED.name,
      price = EXCLUDED.price;

Skip No-Op Updates to Cut Bloat

Every DO UPDATE writes a new row version, even when the new values are identical to the old ones. Those dead tuples bloat the table and create extra work for VACUUM.

Add a WHERE clause to the DO UPDATE so it fires only when something actually changed. Use IS DISTINCT FROM so NULLs compare correctly.

INSERT INTO products (sku, name, price)
SELECT sku, name, price FROM products_stage
ON CONFLICT (sku) DO UPDATE
  SET name  = EXCLUDED.name,
      price = EXCLUDED.price
WHERE products.name  IS DISTINCT FROM EXCLUDED.name
   OR products.price IS DISTINCT FROM EXCLUDED.price;

Order Batches to Tame Lock Contention

When several ETL workers run concurrently, deadlocks appear if they touch the same keys in different orders. Worker 1 locks key X then Y; worker 2 locks Y then X — both block forever until PostgreSQL kills one.

Defenses:

  • Sort each batch by the conflict key so all workers acquire locks in the same order.
  • Partition work by key range so no two workers share keys.
  • Keep transactions short — long-held row locks magnify contention.
INSERT INTO products (sku, name, price)
SELECT sku, name, price
FROM products_stage
ORDER BY sku            -- consistent lock acquisition order
ON CONFLICT (sku) DO UPDATE
  SET name  = EXCLUDED.name,
      price = EXCLUDED.price;

Chunk Giant Merges

One enormous transaction merging millions of rows holds locks for a long time, balloons WAL, and blocks autovacuum from cleaning up. Break the work into chunks (for example 10k-50k rows) and commit between them.

Smaller transactions release locks sooner, let autovacuum keep pace, and make retries cheap after a failure. The trade-off is slightly more commit overhead — tune the chunk size against your hardware.

-- Merge one bounded slice; loop over key ranges from the app side.
INSERT INTO products (sku, name, price)
SELECT sku, name, price
FROM products_stage
WHERE sku >= 'A-0000' AND sku < 'A-5000'
ON CONFLICT (sku) DO UPDATE
  SET name  = EXCLUDED.name,
      price = EXCLUDED.price;

Choosing the Right Conflict Target

The conflict target must match an actual unique/primary-key or exclusion constraint. You can target by column list ON CONFLICT (sku) or by constraint name ON CONFLICT ON CONSTRAINT products_sku_key.

For partial unique indexes, repeat the index predicate so PostgreSQL can pick the right one:

-- Unique only among active rows
CREATE UNIQUE INDEX products_active_sku
  ON products (sku) WHERE is_active;

INSERT INTO products (sku, name, is_active)
VALUES ('A-100', 'Widget', true)
ON CONFLICT (sku) WHERE is_active DO UPDATE
  SET name = EXCLUDED.name;

ON CONFLICT vs MERGE

PostgreSQL 15+ adds the SQL-standard MERGE, which can INSERT, UPDATE, and DELETE in one pass. For high-throughput upserts, INSERT ... ON CONFLICT is usually still preferred:

  • ON CONFLICT is atomic against concurrent inserts — it handles a race where another transaction inserts the same key, retrying internally.
  • Classic MERGE can raise a unique-violation under heavy concurrency because it does not have that built-in conflict arbitration.

Use MERGE when you need DELETE branches or complex conditional logic; use ON CONFLICT for plain, concurrency-safe upserts.

Maintenance: VACUUM and Indexes

Even an optimized merge produces dead tuples on the updated rows. Keep performance steady with maintenance discipline:

  • Ensure autovacuum keeps up; for hot ETL tables lower autovacuum_vacuum_scale_factor so it triggers more often.
  • After a massive one-off backfill, run VACUUM (ANALYZE) to reclaim space and refresh planner statistics.
  • Every extra index on the target slows the merge (each insert/update maintains all of them) — keep only the indexes you truly need.
VACUUM (ANALYZE) products;

Quick Check

Test your understanding of safe, high-throughput merges.

Recap

Efficient upserts at scale come from a few combined habits:

  • Batch rows into one statement; for ETL, stage with COPY then INSERT ... SELECT ... ON CONFLICT.
  • Deduplicate the batch (DISTINCT ON) so no key appears twice.
  • Skip no-op updates with a WHERE ... IS DISTINCT FROM clause to curb dead tuples and bloat.
  • Order by the conflict key and partition work to avoid deadlocks; chunk huge merges into short transactions.
  • Prefer ON CONFLICT for concurrency-safe upserts; keep autovacuum healthy and indexes lean.

Together these turn a fragile row-by-row merge into a fast, low-contention ETL load.

คำถามที่พบบ่อย

บทเรียน “การทำ Upsert ขนาดใหญ่ด้วย ON CONFLICT” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การทำ Upsert ขนาดใหญ่ด้วย ON CONFLICT” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การทำ Upsert ขนาดใหญ่ด้วย ON CONFLICT”

นำตรรกะผสานข้อมูลสำหรับชุดข้อมูลขนาดใหญ่ไปใช้ให้มีประสิทธิภาพ พร้อมหลีกเลี่ยงการแข่งขันแย่งล็อกและการพองตัว คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การทำ Upsert ขนาดใหญ่ด้วย ON CONFLICT” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม

ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. อัตราการส่งผ่านของ COPY เทียบกับ INSERT หลายแถว
  2. การเลื่อนการสร้างดัชนีและข้อจำกัดระหว่างการโหลด
  3. การปรับ WAL และจุดตรวจสอบสำหรับการนำเข้าข้อมูล
  4. การทำ Upsert ขนาดใหญ่ด้วย ON CONFLICT
← กลับไปที่ PostgreSQL Performance & Query Optimization