Peek Under the Hood: The Metadata Tree, for Real

Walk the raw files

A small recursive walker prints the tree exactly as it sits on disk — genuinely useful as a standalone Go utility any time you're debugging a table's physical layout, not just for this lab:

func printTree(root string) error {
	return filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		rel, _ := filepath.Rel(root, path)
		depth := strings.Count(rel, string(os.PathSeparator))
		if rel == "." {
			depth = 0
		}
		fmt.Printf("%s%s\n", strings.Repeat("  ", depth), d.Name())
		return nil
	})
}

// in main(), after the writes in §3:
printTree("warehouse/db.db/orders")
orders
  data
    order_ts_day=2026-01-15
      00000-4-8f1c...-00001.parquet
    order_ts_day=2026-01-16
      00000-6-2a9d...-00001.parquet
    order_ts_day=2026-01-17
      00000-9-e701...-00001.parquet
    order_ts_day=2026-01-18
      00000-11-b34f...-00001.parquet
  metadata
    00000-2f8a1c6d-....metadata.json
    00001-7b41e0aa-....metadata.json
    00002-c93d5f18-....metadata.json
    00003-e5027bb4-....metadata.json
    snap-1938319986946564586-1-3c209181-....avro
    snap-759497885138563489-1-8a94d5ad-....avro
    snap-6239830616431609416-1-13040fd0-....avro
    3c209181-eaef-4563-aafa-fd88ea00ff22-m0.avro
    8a94d5ad-564d-4e9e-ae2e-f695e86a97b1-m0.avro
    13040fd0-6f4d-4461-a321-8580d153c27b-m0.avro

Count them carefully, because the numbers deliberately do not match: four *.metadata.json files but only three snap-*.avro manifest lists and three *-m0.avro manifests. That asymmetry is the single most useful thing in this listing.

Read the leading counter, not the UUID. 00000 is the CREATE TABLE from the Lab: create the table part — DDL, which bumps metadata.json without ever producing a snapshot. Then see Literal INSERT ... VALUES, From a Go struct slice, the way a real ingestion job would, and INSERT INTO ... SELECT, the way a real ETL step reads from one table into another each commit one snapshot, giving 00001, 00002, 00003 and one manifest list apiece. So the Iceberg's metadata tree part's rule is more precisely stated as: every data commit writes new files at every layer above the data and swaps one pointer, while a metadata-only change swaps the pointer alone. A table's metadata counter is therefore not a count of its snapshots, and the gap between them is exactly the number of DDL statements it has seen.

Note also what is not in that listing: no pointer file. Four metadata files sit there as equals, and the only thing that distinguishes 00003 as current is a row in Postgres. Delete that row and this directory becomes an unreadable pile of Avro and JSON — which is a fair summary of why catalog choice is a real architectural decision and not a config detail.

Read metadata.json straight, in Go

You cannot construct that path by hand any more — the filenames carry UUIDs and nothing on disk says which one is live. Ask Iceberg.

The newest row of metadata_log_entries by timestamp is the file the catalog currently points at:

df, err := spark.Sql(ctx, `
SELECT file FROM hms.db.orders.metadata_log_entries
ORDER BY timestamp DESC LIMIT 1`)
if err != nil {
	log.Fatal(err)
}
rows, err := df.Collect(ctx)
if err != nil {
	log.Fatal(err)
}
path := strings.TrimPrefix(rows[0].Value("file").(string), "file:")

That string is byte-for-byte the metadata_location you read out of Postgres in the pointer, as a database row section — same pointer, asked for through the engine instead of behind its back. It's also the portable way to do it: this query works identically against a REST catalog, Glue, or the filesystem catalog in the Lab: the filesystem catalog part, none of which agree on where the pointer lives. Now open it:

raw, err := os.ReadFile(path)
if err != nil {
	log.Fatal(err)
}
var pretty bytes.Buffer
if err := json.Indent(&pretty, raw, "", "  "); err != nil {
	log.Fatal(err)
}
fmt.Println(pretty.String())
{
  "format-version": 2,
  "table-uuid": "a1b2c3d4-...",
  "location": "file:/.../warehouse/db.db/orders",
  "current-schema-id": 0,
  "schemas": [ { "schema-id": 0, "fields": [
    { "id": 1, "name": "order_id",    "type": "long",           "required": false },
    { "id": 2, "name": "customer_id", "type": "long",           "required": false },
    { "id": 3, "name": "order_ts",    "type": "timestamptz",    "required": false },
    { "id": 4, "name": "amount",      "type": "decimal(10, 2)", "required": false },
    { "id": 5, "name": "status",      "type": "string",         "required": false },
    { "id": 6, "name": "country",     "type": "string",         "required": false }
  ] } ],
  "current-snapshot-id": 6239830616431609416,
  "snapshots": [
    { "snapshot-id": 1938319986946564586, "sequence-number": 1, "manifest-list": "file:/.../snap-1938319986946564586-1-....avro" },
    { "snapshot-id": 759497885138563489,  "sequence-number": 2, "parent-snapshot-id": 1938319986946564586, "manifest-list": "file:/.../snap-759497885138563489-1-....avro" },
    { "snapshot-id": 6239830616431609416, "sequence-number": 3, "parent-snapshot-id": 759497885138563489,  "manifest-list": "file:/.../snap-6239830616431609416-1-....avro" }
  ]
}

Three snapshots for four metadata files, for the reason the Walk the raw files section just laid out. Field id in every schema entry is what the Schema evolution and time travel part calls "the real identity" of a column — order_id is name order_id today only because nothing has renamed it yet. Note that status is field 5; the Lab: schema evolution part's proof turns on that exact number.

The manifest list and manifest layers, without decoding Avro by hand

The manifest list and manifest files are Avro, not JSON — nobody hand-decodes those in practice. Iceberg exposes them as ordinary queryable tables instead, and Go reads them exactly like any other query result:

run(ctx, spark, `
SELECT snapshot_id, parent_id, operation, summary
FROM hms.db.orders.snapshots
ORDER BY committed_at`)

run(ctx, spark, `
SELECT path, added_data_files_count, existing_data_files_count, deleted_data_files_count
FROM hms.db.orders.manifests`)

run(ctx, spark, `
SELECT content, file_path, partition, record_count, file_size_in_bytes
FROM hms.db.orders.files
ORDER BY file_path`)

content in the files table is the field to watch across the rest of this lab: 0 means a data file, 1 a position-delete file, 2 an equality-delete file. Right now every row reads 0 — that changes the moment the Lab: upserts with MERGE INTO part's MERGE INTO runs.

  • You've now seen: Every layer of the Iceberg's metadata tree part's tree exists as either a real file you opened directly (metadata.json) or a real queryable table backed by files you didn't have to decode by hand (.snapshots, .manifests, .files) — driven entirely from Go, with no PySpark or Scala involved anywhere.
  • Still ahead: What happens to this tree under a rename (see Lab: schema evolution), a delete-heavy merge (see Lab: upserts with MERGE INTO), a partition-spec change (see Lab: partition evolution), and a genuine two-writer race (see Lab: two writers, one table) — the parts of the theory that are easy to nod along to and much more convincing once you've watched the file count change yourself.