# 0023. Real time starts with an Organization change cursor

The repository's architectural decision, rationale and consequences.

# 0023. Real time starts with an Organization change cursor

Accepted · 2026-11-29 · [#279](https://github.com/CarlosZiegler/fofo/issues/279)

## Decision [#decision]

Each Organization has a cursor in `sync_cursor` and a feed in `sync_change`. Both tables use row-level security and the tenant grants cover reads and appends. A Member can advance the sequence and update the cursor timestamp. Only the scheduled job can advance the retention floor or remove feed rows. The feed contains the Todo collection key and the kind of change. A lane renumbering, a multi-Todo proposal or an Import Run records one resync marker.

A change record commits with the write. A single SQL statement advances the cursor and inserts the change. It runs after the handler, idempotency receipt and Audit Event at the outermost `committed` boundary. Successful savepoints pass their pending statements to that boundary. Rolled-back savepoints drop them. On pipelined tenant transactions the record and COMMIT use the same round trip. In explicit transaction mode the record takes its own round trip. Imports record at the end of their tenant step in the system transaction.

The cursor lock orders successful changes by commit within the Organization. Writers lock domain rows first, then the cursor. A lane renumbering takes the Organization advisory lock before locking any Todo rows (#292). A replay or no-op appends nothing. An applied Todo RPC response reads its sequence after COMMIT. The Board, Calendar and snapshot read their cursor before the rows so the client can catch up from that cursor.

`realtime.changesSince` reads the head, retention floor, page and current Todo rows in one read-only REPEATABLE READ transaction on the tenant handle. A client can ask for sequences alone. It reloads on a resync marker or when its cursor is outside the available range. The cron job locks each Organization's cursor and prunes at most 1,000 changes older than 24 hours, stopping at the first retained change within that window. The deletion and the floor update commit together. `realtime.sync` defaults to off and controls polling in clients that support it. The hub and its signals follow in Phase 2.

## Evidence [#evidence]

S3a measured a +0.120 ms median cost for one recorded write. The record adds one statement and shares COMMIT's round trip in default mode. With 20 writers in one Organization, pipelined throughput was 98% of the reference locally and 88% with 2 ms of round-trip delay. The corresponding p95 ratios were 0.99× and 1.74×. RT-1.7 gates these ratios in default mode. The explicit mode records about one write per round trip per Organization under contention. Its results are informational.

Run `scripts/measure-cursor-contention.mjs` through the disposable PostgreSQL harness with the integration worktree as the baseline. Integration tests cover sequence confirmation, rollback, replay, and Organization isolation. Run the RLS matrix in both modes, and run the preview proof before production.
