Iceberg Time Travel & Rollbacks in Trino
Tags: iceberg, bigdata, trino, datalake
Published: 2023-08-17 18:54:46

_I originally posted this on [Starburst's blog](https://www.starburst.io/blog/apache-iceberg-time-travel-rollbacks-in-trino/), as part of a series I've been publishing on Iceberg._

### Time Travel
Time travel in Trino using Iceberg is a handy feature to “look back in time” at a table’s history. As we covered [in this blog](https://dev.to/starburstdata/introduction-to-apache-iceberg-in-trino-48g0), each change to an Iceberg table creates a new “snapshot” which can be referred to by using standard SQL.

As you can see from the diagram below, a new snapshot is created for the table creation, insert and update.

![Snapshot example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ma0koe27hfiicqa7snp2.png)

To see the snapshots on a table, you can use the handy metadata table that exists for each table:

`SELECT * FROM "customer_iceberg$snapshots";`

![Snapshot files returned from customer table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dhkddynxfl29cfhcw2zt.png)

The above snapshot table shows the create, insert and update operations on the customer_iceberg table. You can see what type of operation was performed and when it was executed.

To select a certain snapshot, you use the “for version as of” syntax. In the following two examples, we show the customer name before and after an update:

`SELECT custkey,name
FROM customer_iceberg FOR VERSION AS OF 5043425904354141100 where custkey = 2732;`

![Snapshot before update to custkey 2732](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a3fbdm6wsx96ojy5rj1c.png)

`SELECT custkey,name
FROM customer_iceberg FOR VERSION AS OF 3117754680069542695 where custkey = 2732;`

![Snapshot after update to custkey 2732](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wxbyx0vv0xekk1mjg6y7.png)

You can also specify a timeframe to retrieve an older snapshot of a table. For example, the following query brings back the data for the first snapshot on or before a given timestamp:

`SELECT custkey,name
FROM s3lakehouse.demo_tpch.customer_iceberg FOR TIMESTAMP AS OF TIMESTAMP '2022-09-18 07:18:09.002 America/New_York' where custkey = 2732;`

![Retrieving older snapshot of custkey](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gd2386kvm7q9pqkoizyz.png)

### Rolling back

![Back to the Future meme captioned "Let's go back in time to a previous Iceberg snapshot"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpjgcjr810iunmz90usy.png)

Another great feature of Iceberg is the ability to roll back a table to a previous snapshot. Sometimes this is used when a row was accidentally deleted or updated. As long as the snapshot exists, (it hasn’t been cleaned up yet) then you can roll back to any existing snapshot.

For example, in the scenario above, if I wanted to roll back to the state of the table before the update on the customer, then I would issue the following command:

`CALL iceberg.system.rollback_to_snapshot('demo_tpch', 'customer_iceberg', 5043425904354141100)`

Then we can query the table again to see the customer’s name was “rolled back” to the previous version before the update:

`SELECT custkey,name
FROM s3lakehouse.demo_tpch.customer_iceberg where custkey = 2732;`

![Rolled back version of custkey](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1k9w57j0fnh334yg6rvi.png)

Both time travel and rolling back are database functions that are now available in a modern data lake architecture. This is a game changer as it allows database type functionality to objects stores which were not available until now.