Query optimisation: share initial snapshots across LIMIT variants of the same query (donor windows)

Open
#58 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@sprihaanand-juspay is already working on this.

Since Nov 30, 2026.

Assessment

This issue has not been assessed yet.

Description

Problem

Two subscriptions that differ only in LIMIT each issue their own
PostgreSQL snapshot read:

SELECT * FROM tickets WHERE status = 'OPEN'
ORDER BY created_at DESC, id ASC LIMIT 50;   -- read LIMIT 100 → PG round-trip

SELECT * FROM tickets WHERE status = 'OPEN'
ORDER BY created_at DESC, id ASC LIMIT 20;   -- read LIMIT 40  → PG round-trip (redundant)

The second read fetches rows the first already holds in memory. Today twin sharing only matches structurally identical queries (by_query is keyed on the full SingleTableReadQuery, which includes limit — src/model/query.rs), so LIMIT 20 and LIMIT 50 never meet.

Proposal

When a new subscription has the same table, filter, and ORDER BY as an existing one but a smaller limit, serve it from the existing subscription's held rows (share_rows) instead of reading storage again.

OPEN tickets LIMIT 50 → SELECT ... LIMIT 100 → PG round-trip
OPEN tickets LIMIT 20 → reuse the 50's rows → 0 PG reads

This is the donor model: registration-time only. After registration the child is independent — own Window, own frontier, own refills. The engine
already works this way for twins (shared row images via subscriber tags; per-sub window in windows: IdMap<SubId, Window>), so Phase 1 mostly relaxes the lookup.

Dominant language
Rust
Stars
129
Forks
7
Avg merge
1d 19h
Merged PRs (30d)
5

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from juspay/streamgres

All issues in juspay/streamgres

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.