---
title: "Time Travel with Serverless Postgres"
description: "Neon's copy-on-write database branching can recover from accidental data loss by creating branches at a past point in time or at a specific Postgres WAL Log Sequence Number (LSN)."
url: "https://swe.quest/articles/time-travel-with-serverless-postgres-ce3e12c8b6286e70"
canonical: "https://neon.com/blog/time-travel-with-postgres"
source: "Neon"
published: "2022-12-07"
difficulty: "medium"
kind: "How-to"
reading_minutes: 9
tags: ["postgres","serverless","databases","algorithms"]
---

# Time Travel with Serverless Postgres

A post on [Neon](https://swe.quest/sources/neon.md), published 2022-12-07, 9 min read, How-to. Original: [https://neon.com/blog/time-travel-with-postgres](https://neon.com/blog/time-travel-with-postgres)

## Summary

Neon's copy-on-write database branching can recover from accidental data loss by creating branches at a past point in time or at a specific Postgres WAL Log Sequence Number (LSN). When the exact moment of a destructive query is unknown, you can binary-search the LSN range between a known-good branch and the current state. A Python script uses the Neon API to create a branch at each midpoint, run a validation query, and delete the branch. It converges on the exact LSN where the data broke, and you then branch from that point to restore.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Serverless](https://swe.quest/tags/serverless.md) (Backend & APIs)
- [Databases](https://swe.quest/tags/databases.md) (Data & databases)
- [Algorithms](https://swe.quest/tags/algorithms.md) (Architecture & design)

## Prerequisites

- PostgreSQL basics ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- Write-ahead logging ([easy posts on Observability](https://swe.quest/tags/observability.md?difficulty=easy))
- Binary search ([easy posts on Search](https://swe.quest/tags/search.md?difficulty=easy))
- REST APIs ([easy posts on API design](https://swe.quest/tags/api-design.md?difficulty=easy))

## Keywords

Neon, PostgreSQL, Log Sequence Number, WAL, Neon API, psycopg2, database branching

## Related posts

- [Using Neon branching for instant Point in time recovery](https://swe.quest/articles/using-neon-branching-for-instant-point-in-time-recovery-9f84c3c887a11e14.md): Neon · 2023-07-28 · Medium · 4 min read · PostgreSQL, Serverless, TypeScript
  Neon's copy-on-write branching can restore a Postgres database to an earlier point in time by creating a branch from a timestamp or LSN within the retained WAL history window (seven days by default).
- [Announcing Point-in-Time Restore](https://swe.quest/articles/announcing-point-in-time-restore-fde22da262d059a9.md): Neon · 2024-02-20 · Medium · 5 min read · PostgreSQL, Serverless
  Neon's Branch Restore feature, now in the Neon Console, restores a Postgres branch to any LSN or timestamp within a configurable history retention window of up to 30 days.
- [I Dropped a Table in Production—Now What?](https://swe.quest/articles/i-dropped-a-table-in-production-now-what-e098e81a35749033.md): Neon · 2025-02-18 · Medium · 7 min read · PostgreSQL, Databases, Incidents & postmortems
  Neon's instant point-in-time recovery creates a new copy-on-write branch at a moment just before a dropped table.
- [Point In Time Recovery Under the Hood in Serverless Postgres](https://swe.quest/articles/point-in-time-recovery-under-the-hood-in-serverless-postgres-81b0a26592d229ce.md): Neon · 2024-02-22 · Medium · 7 min read · PostgreSQL, Storage engines, Serverless, Replication
  Neon implements point-in-time recovery by treating Postgres as its WAL.
- [Practical Guide to Database Branching](https://swe.quest/articles/practical-guide-to-database-branching-8aa2f63325bcfecb.md): Neon · 2025-12-04 · Medium · 9 min read · PostgreSQL, Databases, CI/CD, Schema migrations, Testing
  Neon's copy-on-write storage makes Postgres database branches instant and cheap: each branch shares the parent's base data, gets its own autoscaling compute that scales to zero, and can be created from any point within the history retention window.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
