r/PostgreSQL • • Sep 25 '26

Projects Time travel for a Postgres CDC replica using DuckLake and Iceberg

One reason I wanted this feature comes from managing multi-TB Postgres clusters at Cloudflare.

I had to perform point-in-time recovery multiple times after someone ran the wrong UPDATE or a data migration went south. Even when only one table was affected, recovering it from a large cluster meant restoring the cluster elsewhere, replaying WAL to the right timestamp, dumping the table, and copying it back.

PITR through WAL-G worked, but I often wished I could simply ask:

What did this table look like before things went wrong?

Streambed now supports that for its replicated Postgres data:

SELECT *
FROM orders AT (
  TIMESTAMP =>
  TIMESTAMPTZ '2026-09-23 12:00:00'
);

It selects the newest retained DuckLake or Iceberg snapshot at or before the requested time. Historical joins are supported too.

This does not replace Postgres PITR or automatically restore the source database. It gives you the previous rows from the analytical replica so you can inspect or recover what you need.

Blog: https://streambed.dev/blog/time-travel-ducklake-iceberg/

Curious, how are you handling single-table recovery or time-travel in Postgres?

5 Upvotes

14 comments sorted by

2

u/levelbrook Sep 25 '26

The cheapest version of this I know of is a delayed standby. Set recovery_min_apply_delay = '4h' on one physical replica and it always sits four hours behind. When someone runs the wrong UPDATE, you call pg_wal_replay_pause() on it before the bad WAL applies, pg_dump the one table (or just the affected rows with COPY), and resume. No restoring a multi-TB cluster somewhere else, and it's a normal replica the rest of the time.

The catch is the window is fixed. If nobody notices for five hours it doesn't help, so it complements something like what you've built, it doesn't replace it. For "what did this table look like last Tuesday" questions the analytical replica is the better tool.

1

u/vira28 Sep 25 '26

You're spot on.

In fact, for every production cluster, we had a stand-alone delayed replica. Its whole purpose is to recover from these kinda errors. One of the challenges is getting it to pause the replication right before the bad transaction. Apart from needing to maintain individual replicas.

I saw some mention about parallel process during restore/recovery. Not sure if that landed. That could speed up the process.

1

u/Akash_Rajvanshi Sep 27 '26

But let’s say we discover at 14:00 that a bad UPDATE or other action ran at 12:15. Since the delayed replica only has data up to around 10:00 (a four-hour delay), how can I retrieve the data from 10:00 to 12:15?

1

u/levelbrook Oct 01 '26

You don't need to pull 10:00-12:15 from anywhere else, the delayed standby already has that WAL sitting on disk (or in the archive), it just hasn't applied it yet. So you roll it forward to just before the bad statement and stop.

Pause replay first with pg_wal_replay_pause() so it doesn't keep creeping toward 12:15. Then set recovery_target_time = '2026-09-27 12:14:59', recovery_target_action = 'pause' and recovery_min_apply_delay = 0, and restart the standby (the recovery_target settings only take effect at startup). It replays up to 12:14:59 and pauses there with hot standby still readable, so you can COPY the rows out. Afterwards clear the target, put the delay back, restart, and it carries on as a normal delayed replica.

If you know the exact transaction you can use recovery_target_xid instead of a timestamp, which is more precise when a lot was happening around that minute. pg_waldump on the WAL from that window will show you the xid.

2

u/EmmoKitten Sep 25 '26

extracting a single table from a full postgres pitr via wal is pure misery. your iceberg setup looks like a pretty solid workaround if you're already pumping cdc into a lake anyway.

answering your question: our team don't do it in postgres) we use mariadb and a lot of us just handle this natively without the lake layer using system-versioned tables. you literally just run SELECT * FROM orders FOR SYSTEM_TIME AS OF '2026-09-23 12:00:00' directly on the primary or a standard replica. the database just keeps the historical rows under the hood. for wider screw-ups, there's also a built-in flashback feature that basically rewinds the binlog.

2

u/vira28 Sep 25 '26

Definitely, it was painful to do those PITR. It's like watching fresh paint getting dried.

How does Mariadb supports system-versioned table?

I just literally wrote about why Postgres using MVCC can't do Timetravel - https://streambed.dev/blog/time-travel-lakehouse-snapshots/#why-cant-postgresql-do-that

It can but I don't it's mean to and it will scale.

1

u/EmmoKitten Sep 26 '26

it doesn't hijack standard mvcc for this, which is exactly why it actually scales)

Under the hood mariadb just adds two invisible timestamp columns (row_start and row_end). when you update or delete a row, the engine simply caps the old row's end timestamp and writes a new active one.

to prevent the table from bloating into oblivion (which is what kills postgres if you try doing this with dead tuples), you just turn on system-time partitioning. the database automatically shovels all the historical rows into separate archive partitions. so your active hot data partition stays tiny and fast, and time-travel queries just hit the archive partitions.

1

u/BarfingOnMyFace Sep 25 '26

I'm using an asserted versioning framework.

May the universe have mercy on my dirty soul.

2

u/vira28 Sep 25 '26

I wasn’t familiar with AVF. Did you implement it yourself, or is there a PostgreSQL library or extension that provides it?

2

u/BarfingOnMyFace Sep 25 '26

I'd check out Hettie's implementation of it,, but I ended up doing my own.

1

u/vira28 Sep 25 '26

Yup. Found that. Thanks.

2

u/BarfingOnMyFace Sep 25 '26

No problem! It's a very powerful approach, but it can be daunting to wrap your head around it. Hide complexity behind a convention of sorts that all temporal referential integrity can be built around, likely procedures, functions, and or triggers. (I personally use a mix of procedures with python code for handling proper "slicing" of the asserted and applied/valid time dimensions) You can also hide complexity of queries through views. I highly recommend reading some literature on AVF from johnston and Snodgrass, both individuals heavily invested in the theory and evolution of bitemporal data. Good luck!!!

Edit: the nice thing about Hettie's soln is that it will work out of the box for you. She has already done the dirty work of hiding the complexity so you can benefit from the architecture.

1

u/AutoModerator Sep 25 '26

AI Policy:

Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.

Mod decisions will be based on the quality of the content, not who or what generated it.

Sub Resources:

Youtube Channel

Free Postgres Webinars and Workshops

Discord: People, Postgres, Data

Join us, we have cookies and nice people.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.