Repository navigation
Add additive staleness to follower reads; fix SQLAlchemy session helper - #2
Merged
Merged
Conversation
- hours_ago/minutes_ago/seconds_ago on follower_read(), FollowerReadContext.scoped(), sqla session helper - build_staleness() single source of truth; ValueError on negative - Fix sqla session follower_read() runtime AttributeError (called non-existent FollowerReadContext.set/reset) - Add CHANGELOG and update README follower-reads section
felipeg48
force-pushed
the
fix/7-follower-reads
branch
from
October 28, 2026 12:59
1834fe4 to
def1d33
Compare
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.
PR: Follower-read staleness controls + fix SQLAlchemy session helper
Repo:
cockroachlabs/cockroachdb-python-libraryBranch:
fix/7-follower-reads→mainSuggested bump after merge: patch on
cockroachdb,cockroachdb-core,cockroachdb-sqlalchemy(additive feature + bug fix)Summary
Ports the follower-read contract from the Spring Boot starter and fixes a
SQLAlchemy session helper that raised
AttributeErrorat runtime.The bug (confirmed)
cockroachdb.sqla.session.follower_read()calledFollowerReadContext.set(True)/reset(token)— methods that do not exist (the class exposesis_active,current_staleness,scoped) — and passed aboolinto astr | Nonecontextvar.Any caller on that path got an
AttributeError. Live in the publishedcockroachdb-continuum 0.1.1. Now uses the realFollowerReadContext.scoped()contextmanager.
What changed
hours_ago/minutes_ago/seconds_agokeyword-only params added to thefollower_read()decorator,FollowerReadContext.scoped(), and the SQLAlchemysession helper. They add up and compose to
AS OF SYSTEM TIME '-Ns'; nothing setkeeps
follower_read_timestamp().cockroachdb.staleness.build_staleness()— single source of truth forcomposition + validation; exported from
cockroachdb. Reused by all three entrypoints so the rule (negative →
ValueError) lives in one place.cockroachdbpackage (not core): core depends onbase, so a base-only install can still import
@follower_readwithout anImportError.Breaking vs additive
Fully additive. New params are keyword-only with default 0;
stalenessretainedwith the same precedence when no parts are set. All pre-existing tests unchanged and
green.
Testing (verified locally)
test_follower_read,test_sqla_session,test_decorators,test_template) → 86 passed.cockroachdb/cockroach:v26.3.3node→ 543 passed, 0 failed.
uv run --package cockroachdb-sqlalchemy pytest tests/test_sqla_session.py→ 19 passed.Why the bug shipped: the old
TestFollowerReadcases@patchedFollowerReadContextand asserted.set(True)/.reset(token)— they encoded thenon-existent API and passed only because the mock faked it. Replaced with 6 tests
against the real
FollowerReadContext(no live DB): staleness activated + reset,additive composition, negative →
ValueErrorbefore any session opens, resultpassthrough, and context reset +
session.close()on exception.Environment note for CI
The
aiextras depend ontorch, which has no wheel for CPython 3.15. Run thesuite on Python ≤ 3.14 (3.13 recommended); on 3.15 the
ai/cdc/djangocollection fails to import because the sync aborts on torch. Not a code issue.
Known limitation (not fixed here)
MetricsCollector.record_follower_read()has zero call sites library-wide —follower-read metrics are never recorded. Wiring it needs the SQLAlchemy event /
psycopg execution layer; tracked separately.