Two Writers, One Table
func fireInsert(ctx context.Context, id int, results chan<- error) {
s, err := sql.NewSessionBuilder().Remote("sc://localhost:15002").Build(ctx)
if err != nil {
results <- fmt.Errorf("writer %d connect: %w", id, err)
return
}
defer s.Stop()
// seven values — retry_count was added back in §7
_, err = s.Sql(ctx, fmt.Sprintf(`
INSERT INTO hms.db.orders VALUES
(%d, 999, TIMESTAMP '2026-01-21 00:00:00', 1.00, 'COMPLETED', 'US', NULL)`,
2000+id))
results <- err
}
// in main():
results := make(chan error, 2)
var wg sync.WaitGroup
for i := 0; i < 2; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
fireInsert(ctx, i, results)
}(i)
}
wg.Wait()
close(results)
for err := range results {
if err != nil {
fmt.Println("writer failed:", err)
} else {
fmt.Println("writer succeeded")
}
}Run it a handful of times. Both writers should report success every time, and both rows should land — but the reason is worth being precise about, because it is not the reason a filesystem catalog gives you the same outcome.
What actually happens inside the commit
Every Iceberg commit has the same two phases: write all the new files (data, manifest, manifest list, a fresh metadata.json), then swap one pointer to make them visible. Only the second phase can race. Here that phase runs inside HiveTableOperations.doCommit() on the Spark server's JVM, and by default it looks like this:
- Acquire a metastore lock. Iceberg asks HMS for an exclusive lock on
db.orders. HMS records it as a row in theHIVE_LOCKStable in Postgres. Writer B's request waits. - Re-read the table under the lock. Fetch the current
metadata_locationfrom HMS and confirm it still matches what this writer based its work on. If another writer moved it, this writer rebuilds against the new base rather than clobbering it. alter_tableto swap the pointer. Setmetadata_locationto the new file and slide the old value intoprevious_metadata_location. One row, one database transaction. This is the commit.- Release the lock, and only now does
.Sql()returnnilto your goroutine.
Writer B genuinely blocked on writer A, in a database, and then committed correctly on top of A's result. Contention costs latency, not correctness — which is exactly the property the Concurrency control part said a filesystem rename cannot provide, because there is nothing in a POSIX rename that any second writer can wait on.
You can watch the locks exist. They are short-lived, so poll in a second terminal while a burst is running:
bash — second terminal, during the stress run below
$ while true; do
docker compose exec -T postgres psql -U hive -d metastore_db -c \
'SELECT "HL_LOCK_EXT_ID","HL_DB","HL_TABLE","HL_LOCK_STATE","HL_LOCK_TYPE" FROM "HIVE_LOCKS";'
doneAnd in a third, watch the pointer itself advance — the same query from the pointer, as a database row section, run in a loop. Every row that lands in the table is one UPDATE to one column in TABLE_PARAMS.
Trading the lock for a real compare-and-swap
Locks have a cost that has nothing to do with speed: a writer that dies mid-commit leaves one behind, and somebody has to time it out. Iceberg's alternative removes the lock entirely and pushes the atomicity down into the metastore's own database transaction:
Default — engine.hive.lock-enabled = true
The four steps above. A genuine HMS lock, persisted in Postgres, held across the read-modify-write.
This is the default in Iceberg 1.11 (TableProperties.HIVE_LOCK_ENABLED_DEFAULT = true), and it is why HiveCatalog is safe even on metastores far older than the one you just started.
Opt-in — lock-enabled = false
No lock at all. Iceberg sends alter_table carrying expected_parameter_key=metadata_location and expected_parameter_value=<what I read>, and HMS applies the change only if the row still holds that value.
One round trip, no lock table, nothing to leak on a crash — a true CAS inside the metastore's DB transaction. This is the mechanism the Concurrency control part contrasts the filesystem catalog against.
Set it server-wide — the flag is read from the Hadoop Configuration, so it's --conf spark.hadoop.iceberg.engine.hive.lock-enabled=false — or per table, which is the version worth preferring because it travels with the table instead of the cluster:
main.go
run(ctx, spark, "ALTER TABLE hms.db.orders SET TBLPROPERTIES ('engine.hive.lock-enabled' = 'false')")Re-run the two-goroutine test with that set and nothing observable changes from Go — both writers still succeed. What changed is that HIVE_LOCKS stays empty throughout, because no lock was ever taken. The serialization moved from a lock Iceberg holds to a conditional UPDATE Postgres evaluates.
Push it until something actually fails
Wrap fireInsert in a loop that fires 20 goroutines instead of 2, all at once, with no stagger. Under enough contention you will eventually see an error come back from .Sql() wrapping a CommitFailedException — the retry budget around the lock or the CAS is finite, and exhausting it is how the server tells you to back off.
That looks like the same failure the filesystem catalog produces, and it is worth being clear that it is not the same thing at all:
- Filesystem catalog: A lost race means writer B's speculative
vN.metadata.jsonlost an atomicrenameto a name both writers were guessing. The retry loop re-reads and tries again, and it usually works — but nothing serialized the writers, and on object storage, where that rename isn't atomic in the first place, nothing was protecting the table either. The retry is papering over a missing guarantee. - HMS + Postgres: A lost race means the lock was already held, or the conditional
UPDATEfound a value it didn't expect and refused. Both writers were correctly serialized; the loser simply ran out of attempts. Same error class, opposite meaning — this is honest backpressure, not a near miss.
That distinction is the entire reason the Concurrency control part draws such a hard line between "works on a laptop" and "safe on S3 with real concurrent writers," and it's why this lab spent two containers to make the pointer a row. The Lab: the filesystem catalog part sets up the other side if you want to run the same twenty-goroutine burst against it and compare.