Third- for an analysis project, I had to look ahead to the next transaction. Tried a bunch of implementations, ranging from offset self joins to iterators to recursion.
Turns out the fastest and simplest solution, by far, was a mix of window functions and CTEs directly in the database.