Skip to main content

Overview

Iceberg provides stored procedures for table maintenance and management. Procedures are available when using Iceberg SQL extensions.
In Spark 4.0+, procedures are supported natively but are case-sensitive.

Using Procedures

Call procedures from any configured catalog using the CALL statement:

Argument Passing

Mixing positional and named arguments is not supported.

Snapshot Management

rollback_to_snapshot

Roll back a table to a specific snapshot:
Arguments:
  • table (required) - Table name
  • snapshot_id (required) - Target snapshot ID

rollback_to_timestamp

Roll back to a snapshot at a specific time:
Arguments:
  • table (required) - Table name
  • timestamp (required) - Target timestamp

set_current_snapshot

Set the current snapshot (not limited to ancestors):

cherrypick_snapshot

Apply changes from a snapshot without removing the original:
Only append and dynamic overwrite snapshots can be cherry-picked.

fast_forward

Fast-forward a branch to another branch’s head:

Metadata Management

expire_snapshots

Remove old snapshots and unreferenced data files:
Arguments:
  • table (required) - Table name
  • older_than - Expiration timestamp (default: 5 days ago)
  • retain_last - Minimum snapshots to keep (default: 1)
  • max_concurrent_deletes - Thread pool size for deletions
  • stream_results - Stream results to prevent driver OOM
  • snapshot_ids - Specific snapshot IDs to expire
Output:
  • deleted_data_files_count
  • deleted_position_delete_files_count
  • deleted_equality_delete_files_count
  • deleted_manifest_files_count
  • deleted_manifest_lists_count

remove_orphan_files

Remove files not referenced in table metadata:
Arguments:
  • table (required) - Table name
  • older_than - Remove files older than this (default: 3 days ago)
  • location - Specific directory to scan
  • dry_run - Preview without deleting (default: false)
  • max_concurrent_deletes - Thread pool size
  • stream_results - Stream results to prevent OOM
Orphan file removal is irreversible. Always run with dry_run => true first.

rewrite_data_files

Compact small files and optimize data layout:
Common Options:
  • target-file-size-bytes - Target output file size (default: 512 MB)
  • min-file-size-bytes - Files below this are rewritten (default: 75% of target)
  • max-file-size-bytes - Files above this are rewritten (default: 180% of target)
  • min-input-files - Minimum files to trigger rewrite (default: 5)
  • rewrite-all - Force rewrite all files (default: false)
  • remove-dangling-deletes - Remove orphaned delete files (default: false)

rewrite_manifests

Optimize manifest files for better scan planning:

rewrite_position_delete_files

Compact position delete files and remove dangling deletes:

Table Migration

snapshot

Create a lightweight copy for testing:
Snapshot tables share data files with the source table. Use DROP TABLE to clean up when done testing.

migrate

Replace a Hive/Spark table with an Iceberg table:
Arguments:
  • table (required) - Table to migrate
  • properties - Properties for the new Iceberg table
  • drop_backup - Don’t retain original table (default: false)
  • backup_table_name - Custom backup name (default: table_BACKUP_)

add_files

Add files from external sources:
Schema is not validated. Adding incompatible files will cause query failures.

register_table

Register an existing metadata file in a catalog:
Registering the same metadata in multiple catalogs can cause data loss and corruption.

Change Data Capture

create_changelog_view

Create a view showing table changes:
Query the changelog:
CDC Metadata Columns:
  • _change_type - INSERT, DELETE, UPDATE_BEFORE, UPDATE_AFTER
  • _change_ordinal - Order of changes
  • _commit_snapshot_id - Snapshot where change occurred

Table Statistics

compute_table_stats

Calculate NDV statistics for columns:

compute_partition_stats

Compute partition statistics incrementally:

Metadata Information

ancestors_of

Report snapshot ancestry:

Best Practices

Run maintenance procedures on a schedule:
  • Daily: expire_snapshots for active tables
  • Weekly: rewrite_data_files for frequently updated tables
  • Monthly: remove_orphan_files for all tables
For tables with streaming writes:
  • Use longer trigger intervals (1+ minutes)
  • Regularly run rewrite_data_files to compact small files
  • Run rewrite_manifests to optimize metadata
Always use dry run first:

Next Steps

Writes

Learn about write operations and distribution

Configuration

Configure Spark for optimal performance

Queries

Query tables and inspect metadata

Structured Streaming

Maintain streaming tables