Upserts With MERGE INTO

run(ctx, spark, `
ALTER TABLE hms.db.orders SET TBLPROPERTIES (
  'write.delete.mode'='merge-on-read',
  'write.update.mode'='merge-on-read',
  'write.merge.mode'='merge-on-read'
)`)

run(ctx, spark, `
CREATE TABLE hms.db.orders_updates (
    order_id BIGINT, customer_id BIGINT, order_ts TIMESTAMP,
    amount DECIMAL(10,2), order_status STRING, country STRING
) USING iceberg`)

run(ctx, spark, `
INSERT INTO hms.db.orders_updates VALUES
  (1003, 501, TIMESTAMP '2026-01-16 08:03:00', 249.00, 'COMPLETED', 'US'),
  (1008, 506, TIMESTAMP '2026-01-19 13:00:00', 33.00,  'COMPLETED', 'FR')`)

run(ctx, spark, `
MERGE INTO hms.db.orders t
USING hms.db.orders_updates s
ON t.order_id = s.order_id
WHEN MATCHED THEN UPDATE SET t.order_status = s.order_status
WHEN NOT MATCHED THEN INSERT (order_id, customer_id, order_ts, amount, order_status, country)
  VALUES (s.order_id, s.customer_id, s.order_ts, s.amount, s.order_status, s.country)`)

Two things just happened: order 1003 flipped from PENDING to COMPLETED, and order 1008 was inserted. What matters is what Iceberg did not do — it never rewrote the Parquet file that order 1003 lives in. Under merge-on-read an update becomes two small writes instead: a new data file holding the corrected row, and a delete file marking the old row as superseded. The stale row is still physically on disk; it has just been annotated out of existence. The file list shows all of it:

run(ctx, spark, `
SELECT content, record_count, file_path
FROM hms.db.orders.files
ORDER BY content, file_path`)
# file_path trimmed here for width
+-------+------------+-------------------------------------------------------------------+
|content|record_count|file_path                                                          |
+-------+------------+-------------------------------------------------------------------+
|0      |2           |order_ts_day=2026-01-15/00000-20-1dcd4483-...-0-00001.parquet       |
|0      |2           |order_ts_day=2026-01-16/00000-20-1dcd4483-...-0-00002.parquet       |
|0      |1           |order_ts_day=2026-01-16/00000-46-41efbff0-...-00001.parquet         |
|0      |2           |order_ts_day=2026-01-17/00000-23-021b86bf-...-0-00001.parquet       |
|0      |1           |order_ts_day=2026-01-18/00000-27-cf269f5f-...-0-00001.parquet       |
|0      |1           |order_ts_day=2026-01-19/00000-46-41efbff0-...-00002.parquet         |
|1      |1           |order_ts_day=2026-01-16/00000-46-41efbff0-...-00001-deletes.parquet |
+-------+------------+-------------------------------------------------------------------+

The content column is the tell. Six rows still read 0 — ordinary data files — but one now reads 1, a position-delete file, and that is the value the manifest list and manifest layers, without decoding Avro by hand section said to watch for. Iceberg also gives delete files a -deletes.parquet suffix, so you can spot one by filename alone.

Three of these seven files are new. They are the ones sharing the 41efbff0 UUID, because a single commit stamps every file it writes with the same one:

New filecontentWhat it holds
2026-01-16/…-00001.parquet0order 1003, corrected to COMPLETED
2026-01-19/…-00002.parquet0order 1008, in a partition that didn't exist a moment ago
2026-01-16/…-00001-deletes.parquet1one entry: "in that data file, at row N, skip what you find"

What is actually inside the delete file

"Marks the row as superseded" is the kind of phrase that sounds like hand-waving until you open the file. A position-delete file is an ordinary Parquet file with a fixed two-column schema, and you don't have to decode it by hand — Iceberg exposes it as a metadata table:

run(ctx, spark, `
SELECT file_path, pos
FROM hms.db.orders.position_deletes`)
file_path (string)pos (bigint)
…/order_ts_day=2026-01-16/00000-20-1dcd4483-…-0-00002.parquet1

One row, and it reads as a sentence: "in that data file, the row at ordinal position 1 is dead — skip it." That is the entire delete record. No copy of the row, no key, no predicate — just a filename and an offset.

Notice which file it points at. The delete file was written into the 2026-01-16 partition and carries the new commit's 41efbff0 UUID in its name, but the file it targets is the older 1dcd4483 data file — the one the Lab: write data part wrote, still holding the original PENDING version of order 1003. The filename tells you it is a delete file; only the contents tell you what it deletes.

What a reader now has to do

Scanning order_ts_day=2026-01-16 is no longer "read the Parquet files." It is:

  1. Read every delete file for the partition first and build a set of dead positions — here, { (1dcd4483-file, 1) }.
  2. Scan 1dcd4483-…-00002.parquet, counting each row's ordinal as it goes. Row 0 → keep. Row 1 → present in the delete set, drop it.
  3. Scan 41efbff0-…-00001.parquet → keep its single row, the corrected 1003.
  4. Union the survivors and hand them upstream. The query sees one 1003, and it says COMPLETED.

That anti-join between data rows and delete positions runs on every single read of the partition, forever, until compaction (see Lab: compaction and cleanup) rewrites the data file with the delete already applied and retires the delete file. That is the merge-on-read bargain on one screen: writes get cheap, and you take on a read tax that never expires by itself. The Compaction and maintenance part is where that bill was described; this is where you can watch it start accruing.

Isn't three files a lot for two rows?

It is, and the instinct behind the question is the right one — small files are the operational failure mode of merge-on-read. But three is the floor for this merge, and it's worth separating the part that's a hard invariant from the part that's a tuning problem, because only one of them is yours to fix.

  • Can't be merged: A data file never spans partitions. Order 1003 is in 2026-01-16, order 1008 in 2026-01-19, and a manifest entry records exactly one partition tuple per file. That one-tuple-per-file fact is the entire basis of pruning — let a file straddle two days and Iceberg can no longer say "skip this one, it's outside your range," and the Lab: query and prove pruning part's proof collapses. Two partitions means two data files, always.
  • Can't be merged: A delete file can never be a data file. Different content type, and a different schema entirely — (file_path, pos) versus your table's six columns. It is a separate physical file by definition.
  • Already minimal: Note what Iceberg did not do: it wrote one delete file, not one per targeted data file. Position-delete files are scoped per partition, and their file_path column varies row to row — so 100 updates hitting rows scattered across 10 different data files in one partition still produce a single delete file with 100 entries spanning 10 paths.

That last point has a payoff at read time. The spec requires those entries to be sorted by file_path then pos, so a reader scanning one data file can use column bounds to skip the stretches of the delete file that concern other files — and when a delete file happens to reference exactly one data file, that fact is recorded in the manifest and readers skip it outright for everything else in the partition.

What actually drives the file count

Rows are almost irrelevant to it. The real formula is:

files ≈ partitions touched × writer tasks holding rows for them

The second factor is where production goes wrong, and it is entirely a config decision:

PropertyWhat it does to the count
write.distribution-mode = noneNo shuffle before the write. Every task that happens to hold a row for 2026-01-16 opens its own file for it. 100 rows spread across 20 tasks → 20 files of 5 rows. Scale that to 200 shuffle partitions across 5 dates and one merge emits a thousand files.
write.distribution-mode = hashShuffles rows by partition key first, so every row for a partition lands in exactly one task → one file per partition per commit. This is what you want for MERGE; current Iceberg versions default partitioned tables to it, but older tables may still carry none — check with SHOW TBLPROPERTIES rather than assuming.
write.distribution-mode = rangeRange-partitions and sorts. More shuffle cost, better clustering; worth it when the table has a declared sort order.
write.target-file-size-bytes512 MB by default. The writer rolls to a new file when it crosses this, so it bounds file size upward — it will never merge small writes for you.
write.spark.fanout.enabledLets one task keep writers open for several partitions at once instead of requiring partition-clustered input. Saves a sort; costs memory per open file.
One MERGE touching…Data filesDelete files
100 rows, 1 partition, all inserts10
100 rows, 1 partition, all updates1 (the new versions)1 (100 position entries)
100 rows, 5 partitions, updates55
the same, under distribution-mode=none, 20 tasksup to 100up to 100

The part configuration can't fix

Every commit also writes a manifest, a manifest list, and a fresh metadata.json — regardless of whether it changed one row or a billion. Merging once a minute is 1,440 commits a day of pure metadata accumulation, which is why the Lab: compaction and cleanup part has rewrite_manifests and expire_snapshots as jobs separate from rewrite_data_files: they clean up different piles.

So the durable mitigation is architectural, not a property setting: batch your merges. One MERGE carrying 100k rows produces a small fraction of the files — and a tiny fraction of the metadata — that a thousand merges of 100 rows do, for byte-identical results. Streaming a per-record upsert straight into an Iceberg table is the single most common way teams end up with a table that takes minutes to plan a query against, and no amount of compaction keeps ahead of it if the write pattern never changes.