feat: cygnet.follow_many — batched FK navigation (N+1 → 1 round-trip) - #16
Merged
Conversation
…trip) Following a foreign key for a whole collection meant looping cygnet.follow, one round-trip per object (the classic N+1). Against real PG the per-query round-trip dominates read cost (a measured spike put hydration at <15% of end-to-end), so collapsing N lookups into one is the high-leverage, driver-agnostic read-throughput win. follow_many(db, objs, fk_column) issues a single WHERE pk = ANY($1) query (the whole FK-value list bound as one array parameter -- no IN-length limit) and returns the targets aligned to objs: input order, None for a NULL FK or a missing row, and a shared instance for a shared FK value. It issues no query when objs is empty or every FK is None. Validation mirrors follow() (FK column, object types), with object-type errors taking precedence. Tests: 11 unit (FakeDB) covering alignment, NULL/missing -> None, dedup, the no-query short-circuits, validation + error precedence; 1 real-PG integration test (passed on Dockerised PG). Docs: README recipe, THEORY (round-trips are the read ceiling; batching is the lever), ARCHITECTURE component map.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds
cygnet.follow_many(db, objs, fk_column)— the batched sibling ofcygnet.follow. Following an FK for a whole collection previously meant loopingfollow, one round-trip per object (the classic N+1).follow_manydoes it in oneWHERE pk = ANY($1)query and re-associates the targets back to the inputs.Why this, and why now
This is the driver-agnostic read-throughput lever. A prior real-PG spike measured that end-to-end read time is ~85–92% driver decode + network round-trip and the positional hydrator already captured the hydration slice — so no faster hydrator (or Rust driver) moves the ceiling. Fewer round-trips is where the win is, and N+1 FK-loading is the most common source of extra round-trips.
follow_manyis the explicit cure (it fits the "littlest ORM" stance — a helper, not magic lazy-loading).The whole FK-value list binds as a single array parameter (
= ANY($1)), so there's noIN-list length limit and it's plan-cache friendly.Behavior
objs(input order);Nonefor a NULL FK or a missing target row.objsis empty or every FK isNone.follow()(FK column, object types), with object-type errors taking precedence (matched tofollow).Tests & verification
FakeDB): alignment, NULL→None, missing→None, dedup/shared-instance, empty + all-None no-query short-circuits, all validation paths, and the error-precedence guarantee.TestFollowRoundtrip— passed locally on Dockerised PG.Docs
README recipe (features
follow_many, plus the underlying= ANYpattern and.stream()note), THEORY synthesis paragraph (round-trips are the read ceiling; batching is the lever), ARCHITECTURE component-map symbol.