Intelligent Compaction

LakeOps includes a compaction engine for Apache Iceberg, written in Rust on Apache DataFusion. It merges small data files into files near a target size, with a binpack or sort strategy, and commits the result as a new Iceberg snapshot without blocking readers or writers.

Why compaction matters

Apache Iceberg tables accumulate small files over time from streaming ingestion, frequent appends, and partial updates. These small files degrade query performance because engines must open and scan each file individually, increasing I/O overhead and planning time.

Compaction merges these files into optimally-sized data files. The result: fewer files to scan, less metadata to plan over, and dramatically faster queries across every connected engine.

Symptoms of poor compaction

  • •Thousands of small files per partition (well below target file size)
  • •Query planning time growing with each ingestion cycle
  • •High object storage request costs from excessive LIST and GET operations
  • •LakeOps Insights flagging PARTITION_SMALL_FILES

How LakeOps compaction works

LakeOps compaction operates directly on your Iceberg metadata and storage. It:

  • •Reads table metadata to plan which data files to rewrite, scoped by an optional predicate, and groups them by partition.
  • •Resolves the strategy and target size — binpack or sort, and a target file size from your settings, the table's write.target-file-size-bytes, or 512 MB.
  • •Rewrites data files using the Rust compaction engine — merging small files into target-sized outputs without blocking concurrent readers or writers.
  • •Commits atomically as a new Iceberg snapshot, so readers see either the old files or the new ones, never a mix.
  • •Logs every run in the table's Events tab, with input and output file counts, status, and duration.

Compaction strategies

LakeOps supports two compaction strategies. You choose one in a Compact Data Files policy. The per-table File Compaction card has no strategy setting.

Binpack

Optimize for size

Combines small files into files near the target size (default: the table's write.target-file-size-bytes, otherwise 512 MB). Does not reorder rows. This is the default strategy.

Best for

  • • Tables with heavy append workloads (streaming ingestion)
  • • Reducing file count as the primary goal
  • • Fast execution with minimal compute overhead

Sort

Optimize for queries

Rewrites files in the table's Iceberg sort order. Clustered rows give engines tighter column statistics, so they skip more files and read less data per query.

Best for

  • • Tables with a sort order on columns queries filter by
  • • Large analytical tables where scan reduction matters
  • • Read-heavy tables where a slower rewrite pays off

When a run doesn't specify a strategy, the engine uses sort if the table has an Iceberg sort order and binpack otherwise.

Execute on the per-table card runs once with the card's predicate. Strategy and target size come from the engine defaults (sort if the table has a sort order, otherwise binpack; target from write.target-file-size-bytes or 512 MB), not from any Compact Data Files policy.

Choosing a strategy

Start with Binpack to eliminate small-file pressure quickly. Then evaluate whether Sort would benefit query-heavy tables: the Simulations tab estimates how candidate partition specs and sort orders would change query cost.

Using the compaction UI

Navigate to Data > Explore in the sidebar, select a table, then open the Optimization tab. The File Compaction card provides compaction controls for the selected table.

Step-by-step

1Optionally set a Predicate to scope compaction to specific files. Supported fields include file_path, file_size, record_count, delete_file_count, sequence_number, and partition['col']. For example: file_size < 134217728 OR delete_file_count > 0.
2Configure the Cron Expression to control when compaction runs (e.g. 0 2 * * * for daily at 2:00 AM). The schedule display shows a human-readable interpretation.
3Click Save (Save table override when the table inherits a policy). The schedule starts and the Enabled switch appears.
4Use Enabled to pause or resume the schedule.
5Click Execute to run compaction once now with the predicate on the card, saved or not.

If Adaptive Maintenance is active for the table, compaction runs as part of its maintenance. The File Compaction card's Enabled switch and Save are locked, but Execute still works.

Configuration reference

Settings on the table's File Compaction card:

SettingDefaultDescription
Predicate(none)Optional WHERE clause to scope compaction to specific files. Supports file_path, file_size, record_count, delete_file_count, sequence_number, and partition['col'].
Schedule (cron)0 2 * * *5-field Unix cron expression controlling execution frequency. Default: daily at 2:00 AM.
EnabledOn after SavePauses or resumes scheduled compaction for this table. Appears once the card is saved.

Compact Data Files policy

To compact many tables with one configuration, go to Manage > Policies > Create Policy > Compact Data Files, pick a scope (catalog, namespaces, or tables), and choose a schedule: a cron expression, a fixed interval, or manual only. The policy adds the strategy and target size controls the per-table card doesn't have:

SettingDefaultDescription
StrategyBinpackBinpack combines small files to the target size. Sort rewrites files in the table's sort order.
Target file size (MB)512Leave it empty to use each table's write.target-file-size-bytes, falling back to 512 MB.
Predicate(none)The same file-metadata filter as the per-table card.
Use table propertiesOffRead target size and strategy from each table instead: sort when the table has a sort order, binpack otherwise. The strategy, target size, and predicate above are ignored.

Conflict handling

Compaction commits through Iceberg's optimistic concurrency. If another writer commits while compaction runs:

  • •The commit is validated against the snapshot compaction started from, so a conflicting change is detected rather than overwritten.
  • •Transient commit conflicts are retried a few times with backoff. If the commit still fails, the run fails and the table is unchanged; the next run picks the files up again.
  • •No data is lost or corrupted — the worst case is a delayed compaction cycle.

Monitoring compaction

After compaction runs, verify results through multiple observability surfaces:

  • •Events tab — each compaction run with input and output file counts, status, and duration.
  • •Metrics tab — Total Files and the File Size Distribution chart show files converging toward the target size.
  • •Insights tab — PARTITION_SMALL_FILES insights should clear on the next table scan after a successful compaction.
  • •Dashboard — Efficiency Gain (file reduction) and Data Optimized (volume compacted) over the last 30 days.

Compare layouts first

Two previews help before you change how a table is compacted. Neither changes the table.

  • •Preview a compaction plan. In the table's Query tab, switch the mode from SQL Query to Compaction, enter a predicate, and click Simulate to see the plan for the matching files. The REST equivalent is POST /api/compaction/{catalogName}/{namespace}/{tableName}.
  • •Compare layouts. The Simulations tab (organization admins) estimates how candidate partition specs, sort orders, and target file sizes would change files scanned, data read, and execution time.

Compaction via policies

Instead of configuring each table individually, use a Compact Data Files policy, or Adaptive Maintenance to bundle compaction with other operations and run it only when a table needs it. Policies support:

  • •Scoping to a catalog, namespaces, or tables
  • •Their own schedule and an enable/disable toggle
  • •Next Run and Last Run on the Policies screen, and each run in Events

Run it from the API or an agent

Run the operation once on one table, without saving a policy, through the REST API. The key needs tables:write.

curl -X POST https://api.lakeops.dev/api/tables/production-glue/analytics/page_events/policies/COMPACT_DATA_FILES/execute \
  -H "Authorization: Bearer $LAKEOPS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"whereClause": "1=1"}'

An agent does the same with the MCP tool execute_optimization, passing type COMPACT_DATA_FILES and the same config. whereClause is the same file-metadata predicate as the card; 1=1 (the default) compacts all files. Strategy and target size come from the engine defaults described under Compaction strategies.

Both return once the run is submitted. Follow it in the table's Events tab, or with get_table_events.

Troubleshooting

What you seeWhat to do
No Enabled switch on the cardIt appears once the card is saved, or when the table inherits a policy. Click Save first.
Save is missing and Enabled is lockedAdaptive Maintenance is on for the table. Execute still runs the operation once.
A run shows FAILED in EventsExpand the event in Data > Events to read its error. A common cause is catalog credentials without write or delete access to the table's storage; see Connect a Catalog.
A run failed with a commit conflictAnother writer committed first. The table is unchanged and the next run picks the files up again; see Conflict handling above.