Spanner point-in-time recovery (PITR) provides protection against
accidental deletion or writes. For example, if an operator inadvertently writes
data or an application rollout corrupts the database, with PITR you can recover
the data from a point-in-time in the past (up to a maximum of seven days)
seamlessly. If you need longer-term retention of data, you can use either
[Backup and Restore](https://docs.cloud.google.com/spanner/docs/backup.md.txt) or [Export and Import](https://docs.cloud.google.com/spanner/docs/export.md.txt).

By default, your database retains all versions of its data and schema for one
hour. You can increase this time limit to as long as seven days through the
[`version_retention_period`](https://docs.cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.md.txt#Database.FIELDS.version_retention_period)
option. For instructions, see [Set the retention period](https://docs.cloud.google.com/spanner/docs/use-pitr.md.txt#set-period).
Spanner stores earlier versions of data at microsecond granularity and the
database maintains an [`earliest_version_time`](https://docs.cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases.md.txt#Database.FIELDS.earliest_version_time),
which represents the earliest time in the past that you can recover earlier versions
of the data.

> [!NOTE]
> **Note:** PITR provides additional insurance against logical data corruption, but does not protect you in case a user accidentally deletes the database. Make sure that you have other recovery options in place and that access to roles that include the [`spanner.databases.drop`](https://docs.cloud.google.com/spanner/docs/iam.md.txt#databases) permission are set appropriately. For more information, see [Using IAM securely](https://docs.cloud.google.com/iam/docs/using-iam-securely.md.txt). You can also enable [database deletion protection](https://docs.cloud.google.com/spanner/docs/prevent-database-deletion.md.txt) to prevent the accidental deletions of databases.

## Ways to recover data

There are three ways to recover data:

- To **recover a portion of the database** , perform a [stale read](https://docs.cloud.google.com/spanner/docs/reads.md.txt#perform-stale-read)
  specifying a query-condition and timestamp in the past, and then write the
  results back into the live database. This is typically used for surgical
  operations on a live database. For example, if you accidentally delete a
  particular row or incorrectly update a subset of data, you can recover it
  with this method. For instructions, see [recovering a portion of your database](https://docs.cloud.google.com/spanner/docs/use-pitr.md.txt#recover-portion).

- To **recover a specific table** , export the table from an earlier timestamp
  within the same database and then import it back. For instructions, see
  [recovering a specific table](https://docs.cloud.google.com/spanner/docs/use-pitr.md.txt#recover-table).

- To **recover the entire database** , [backup](https://docs.cloud.google.com/spanner/docs/backup.md.txt) or
  [export](https://docs.cloud.google.com/spanner/docs/export.md.txt) the database specifying a timestamp in the
  past and then restore or import it to a new database. This is typically used
  to recover from data corruption issues when you have to revert the
  database to a point-in-time before the corruption occurred. Note that
  backing up or exporting a database could take several hours and that you
  cannot restore or import to an existing database. For instructions, see
  [recovering the entire database](https://docs.cloud.google.com/spanner/docs/use-pitr.md.txt#recover-entire).

## Performance considerations

Databases with longer retention periods and, in particular, those that
frequently overwrite data, use more system resources. This can affect how your
database performs, especially if your instance is not provisioned with enough
[compute capacity](https://docs.cloud.google.com/spanner/docs/compute-capacity.md.txt). If your database has a very high overwrite rate
(for example, if your database is overwritten multiple times per day), you might
consider increasing the retention period gradually and
[monitoring the system](https://docs.cloud.google.com/spanner/docs/monitoring-cloud.md.txt#storage).
Here are some things to be aware of:

- **Increased storage utilization** . We recommend setting up
  [storage alerts](https://docs.cloud.google.com/spanner/docs/storage-utilization.md.txt#alerts) to ensure that you
  don't exceed the [storage limit](https://docs.cloud.google.com/spanner/quotas.md.txt#database_limits). When you
  increase the retention period, keep in mind that storage usage will increase
  gradually as the database accumulates earlier versions of data. This is because
  the old data that would have expired under the previous retention period, is no
  longer expired. So, for example, if you increase the retention period from 3
  days to 7 days, you need to wait for 4 days for database storage usage to
  stabilize. We also provide instructions for
  [estimating the storage increase](https://docs.cloud.google.com/spanner/docs/use-pitr.md.txt#estimate-storage).

- **Increased CPU usage and latency** . Spanner uses additional computing
  resources to compact and maintain earlier versions of data.
  [Monitor your instance and database](https://docs.cloud.google.com/spanner/docs/monitoring-console.md.txt#view-current-status)
  to ensure that latency and CPU utilization remain at acceptable levels.

- **Increased time to perform schema updates** . An increased retention period
  means that [schema versions](https://docs.cloud.google.com/spanner/docs/schema-updates.md.txt#schema_versions_created_during_schema_updates)
  must be retained for longer durations potentially causing schema updates to be
  [`throttled`](https://docs.cloud.google.com/spanner/docs/reference/rest/v1/UpdateDatabaseDdlMetadata.md.txt#FIELDS.throttled) while
  waiting for server resources. Make sure that you are following
  [best practices for schema updates](https://docs.cloud.google.com/spanner/docs/schema-updates.md.txt#best-practices)
  and staying within the [limits for schema updates](https://docs.cloud.google.com/spanner/docs/schema-updates.md.txt#frequency).

## Pricing

There is no additional charge for using PITR. However, if you
increase the version retention period of your database from the default one hour,
your database storage and compute capacity costs might increase. Your on-demand
[backup](https://docs.cloud.google.com/spanner/docs/backup.md.txt) cost is unaffected because only a single version
of your database is stored. For more information, see the [Performance
considerations](https://docs.cloud.google.com/spanner/docs/pitr#performance) section. Before increasing a database's version
retention period, you can [estimate the expected increase in database storage](https://docs.cloud.google.com/spanner/docs/use-pitr.md.txt#estimate-storage).

For general information about how Spanner is charged, see
[Spanner pricing](https://cloud.google.com/spanner/pricing).

## What's next

- Learn more about how to [recover data with PITR](https://docs.cloud.google.com/spanner/docs/use-pitr.md.txt).