Skip to content

OpenConceptLab/ocl_online#452 | Two versions of one source kept apart, behind the per-evaluation switch - #1090

Open
paynejd wants to merge 27 commits into
fhir-452-b-versionsfrom
fhir-452-b-apart
Open

paynejd wants to merge 27 commits into
fhir-452-b-versionsfrom
fhir-452-b-apart

Conversation

@paynejd

@paynejd paynejd commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Not part of the Dec 1 run.

Stacked on #1087 (#1086 is merged): merge #1087 first. This PR is then retargeted to master, without a rebase.

Part of OpenConceptLab/ocl_online#452 (phase B, PR 5: two versions of one source kept apart).

What it does

Today an expansion's members are concept versions, and a build keeps only the newest version of each concept, so a value set whose references name two versions of a source shows each concept once, as the newer version has it. An exclusion removes every version of each concept it selects. FHIR's value sets keep two versions of one code system apart (the HL7 Terminology Ecosystem IG's overload cases). This PR adds that behaviour behind the per-evaluation switch PR 2 added (ExpansionEvaluation.fhir_rules). FHIR's request-time evaluations will turn the switch on. For API requests the EXPANSION_FHIR_RULES_TOGGLE toggle decides, and it isn't set anywhere, so nothing changes for API users until it is.

With the rules on (core/collections/member_versions.py):

  • Each concept member records the source version that selected it, in a new table beside the membership (ConceptMembership). A concept version that two releases share is a member of each release it was included from. The expansion's concepts (collection_expansions_concepts) are still filled, with each concept version once, so everything that reads them keeps working. A concept that a cascade reached in another source is recorded at that source's HEAD. Mappings are kept as they are today.

  • One member per concept per source version: the newest concept version that version selected.

  • Exclusions follow what they name. An exclusion that names codes or filters in a version it states removes them from that version only. Any other exclusion (a whole version, or one that states no version) removes what it holds from every version of its source. It is evaluated at a member's version when it can be (it states no version, or a wildcard that names that version, and force-system-version doesn't fix it); otherwise it uses what it holds in its own version. A reference to a concept version still removes only that concept version (REF-12).

  • versionsMatch, a new expansion parameter (the value set's own). false keeps every exclusion to its own version. true removes in every version and keeps each concept once, in the newest version that has it (by the source's declared version algorithm from OpenConceptLab/ocl_online#452 | Version rules in OCL's core: wildcards, a declared version algorithm and the version parameters, behind the per-evaluation switch #1087, else the most recently created). The API accepts this parameter only while the toggle is on, as with default-valueset-version.

  • Value sets hold a concept in a version. A value set that a reference with a system names (an intersection) holds a candidate in version V when:

    • its own members include that concept in V; or
    • one of its includes of that source can be evaluated in V (it names no version, or a wildcard that names V) and selects the concept there, and the value set's exclusions leave it.
    • When none of its includes of that source can be at V, it holds the candidate if it has that concept at all.

    That covers the design's two cases: an import whose 1.x.x include expands alone at 1.2.0 still holds a 1.0.0 candidate; and an import that lists X from version 1 still holds an outer version-2 X. An import's own force-system-version and expansion parameters apply to what its includes select at another version. An import of an import is read the same way, and a value set that leads back to one already being evaluated holds there only its own members, so evaluation always ends (refusing such loops is PR 6). A reference with value sets and no system takes the first value set's members, each in its version, that the others hold. A value set built without the rules holds each member in the versions its expansion recorded that contain it. Under the rules, every build evaluates its references again with the expansion's parameters, including an auto-generated expansion and the re-add after a reference is removed: saved results don't say which version selected what.

  • What reads these memberships:

    • Health judges a member by the references that resolved to a version it's a member of, so a member that a reference took from the older version it names isn't a version mismatch of another reference (HEALTH-2).
    • dry_run_rebuild's snapshot adds each concept's memberships (concept_versions).
    • Comparison is by code as before, with the newest version of each concept. It also lists, in concept_versions, the (concept, version) memberships each expansion has that the other hasn't. A version is named there only when it's a public release.
    • FHIR's stored $expand lists:
      • a member for each live release the caller is shown that selected a concept, reported in that release;
      • nothing for a concept that only other releases selected;
      • one member, reported as today, for a concept no release selected.
    • A member whose selecting release was deleted stays a member, of unknown version, until the next rebuild.
    • An expansion built before versions were kept apart, imported by a value set FHIR evaluates at request time, is evaluated again there under FHIR's rules (OpenConceptLab/ocl_online#452 | Imported value sets under the expansion's version parameters, and value set loops refused #1091), so FHIR reports what OCL evaluates: the concept in each version that selected it. Stored $expand of the value set itself serves its stored expansion as it is (each concept once) until it's rebuilt.

When a build asks the switch: only when it meets something the rules could change: a reference or exclusion evaluated in a second version of one source, a reference with value sets, versionsMatch, or an expansion that already records versions. Until then an exclusion removes the same either way. Any other build never reads the toggle, and with the switch off nothing reads or writes the new table (tested by capturing every SQL statement of builds, a reference delete, a dry-run rebuild and a concept delete).

When the switch changes for an expansion:

  • An add to an expansion built under the other rules (the switch turned on or off since) rebuilds it, keeping the versions it locked (keep_locks), because an add alone would keep what the other rules made.
  • A build without the rules never touches the memberships table: it clears versions_apart, and the next build under the rules clears the leftover rows.
  • Reference deletes touch memberships only while the switch is on.

So with the switch off, an expansion holds exactly today's members after its next build, whatever it was built under before.

Migration

collections.0078_concept_memberships:

SET LOCAL lock_timeout = '300ms';
CREATE TABLE "collection_expansions_concept_memberships" ("id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, "concept_id" bigint NOT NULL, "expansion_id" bigint NOT NULL, "source_version_id" bigint NOT NULL);
ALTER TABLE "collection_expansions_concept_memberships" ADD CONSTRAINT "expansion_concept_membership_unique" UNIQUE ("expansion_id", "concept_id", "source_version_id");
ALTER TABLE collection_expansions_concept_memberships ADD CONSTRAINT collection_expansions_concept_memberships_concept_id_fk FOREIGN KEY (concept_id) REFERENCES concepts (id) ON DELETE CASCADE;
ALTER TABLE collection_expansions_concept_memberships ADD CONSTRAINT collection_expansions_concept_memberships_source_version_id_fk FOREIGN KEY (source_version_id) REFERENCES sources (id) ON DELETE CASCADE;
LOCK TABLE collection_expansions IN ACCESS EXCLUSIVE MODE;
ALTER TABLE collection_expansions_concept_memberships ADD CONSTRAINT collection_expansions_concept_memberships_expansion_id_fk FOREIGN KEY (expansion_id) REFERENCES collection_expansions (id) ON DELETE CASCADE;
ALTER TABLE "collection_expansions" ADD COLUMN "versions_apart" boolean NULL;
CREATE INDEX "collection_expansions_concept_memberships_concept_id_5255d80b" ON "collection_expansions_concept_memberships" ("concept_id");
CREATE INDEX "collection_expansions_conc_source_version_id_3b446e17" ON "collection_expansions_concept_memberships" ("source_version_id");
  • The 6.1M-row membership table (collection_expansions_concepts) isn't touched. A column there couldn't hold a concept version that two releases share without replacing its unique index, so the memberships go in a new table instead.

  • The new table starts empty, so creating it and its indexes reads no existing rows. Its ids are 64-bit.

  • versions_apart is a nullable column with no default on collection_expansions (about 26,000 rows). PostgreSQL adds it by updating the catalog only. It takes a brief ACCESS EXCLUSIVE lock on that table, which reads wait for, so that table comes last: the lock isn't held while the migration waits for concepts or sources.

  • Each foreign key takes a brief SHARE ROW EXCLUSIVE lock on the table it references (collection_expansions, concepts, sources), and checks only the new, empty table. Writes there wait for it; reads don't.

  • Each lock wait is bounded at 0.3 s (SET LOCAL lock_timeout, the first operation applied and reversed). The three waits together stay below PostgreSQL's deadlock_timeout (1 s). So if this migration and another transaction wait for each other, the migration gives up and the other goes on.

  • If it fails during a deploy ("canceling statement due to lock timeout"), another transaction held one of these tables. Nothing has changed: the migration is one transaction, and all of it is rolled back. The container that runs the migrations stops there. Run it again (or let it start again) once that transaction is over, so it's still best to deploy when no import is running.

  • Measured on PostgreSQL 14.7, with another transaction holding a lock for 30 s:

    Held Before Now
    a write on concepts the migration waited, and reads of collection_expansions with it, for 25 s it gives up, and reads wait 0.1 s
    a read of collection_expansions the migration waited, and other reads of the table with it, for 25 s it gives up, and other reads wait 0.3 s

    Run again afterwards, it applies in 0.3 s.

  • The foreign keys cascade in the database (ON DELETE CASCADE), and Django's side is DO_NOTHING. So deleting a concept, source version or expansion through Django never queries the new table.

  • Code without this PR ignores the column and the table, so a database shared with an older deployment (staging's, when fhir-next deploys) is unaffected.

Tests

  • core/collections/tests/tests_member_versions.py, following the IG's overload cases on stored content (1.0.0: code1, code2, code3; 2.0.0: code1 unchanged, a new version of code2, code4):
    • includes in two versions, listed codes, a version and the default, versionsMatch true and false, the declared version algorithm;
    • exclusions of a whole version, of listed codes and of a filter in a stated version, one that states no version, a concept version and a value set;
    • imports: both worked cases, a wildcard that can't be at the version, an import built without the rules, value sets without a system;
    • the switch: off, left to the toggle, and when the toggle is read;
    • stored expansions: a reference added to an expansion built without the rules, deleted references and expressions, saved results evaluated again, HEAD rows swapped for their latest versions, nothing kept at request time;
    • changes of rules: an expansion built under one set of rules and added to under the other is rebuilt, its version locks kept, from the references it was given; deletes without the rules touch no membership;
    • health, the dry-run snapshot, comparison and FHIR $expand;
    • equivalence: each case built without the rules, left to the toggle, and with them. The first two are identical in members, mappings, recorded versions, unresolved entries, each reference's reason and what's written back. With the rules on they're identical too, unless a concept is selected in two versions.
  • core/collections/tests/tests_version_rules.py: OpenConceptLab/ocl_online#452 | Version rules in OCL's core: wildcards, a declared version algorithm and the version parameters, behind the per-evaluation switch #1087's toggle-read test now names the version its other reference resolves to, since two versions of one source also ask the toggle.
  • Full gate on 478ee77 (the one commit after it only renames two test classes), as CI runs it (--parallel=4 with TEST_ES=off, then the search-index tests): pylint 10.00; 4,264 + 803 tests OK; coverage 97% (member_versions.py 96%).

Still to come: a dry-run rebuild of real collections (switch off against #1087, then switch on). It belongs to B's final checks, before the whole goes to staging.

Review

Three Codex defect passes (10, 10 and 10 findings; every P0 and P1 fixed, with its class), a simplification review (1 of 6 cuts applied) and a Codex check of the later commits (5 findings, all fixed; those last fixes had no further pass, the loop's limit).

Expansions built before this change: with the switch on, a stored expansion recorded before per-version membership, read where its value set is imported, makes a concept row that two recorded releases share a member at both. So the importing expansion lists it once per version, while the stored expansion itself still lists it once until it's rebuilt. They converge on rebuild.

Follow-up: #1087's new head merged in (a version parameter naming a canonical URL applies whichever repository with it holds the version), with force-system-version found here the same way. A value set that an include with a system names now also holds, by code, a candidate whose code it has as a member in another public copy of that code system version (the same canonical URL and version), as when a code system a request carries is loaded beside a stored one. Codes compare as FHIR spells them (6b072be, ea88b82, 6c1d689, a1d6351; Codex passes and checks, every P0/P1 fixed, P2s fixed or accepted on OpenConceptLab/ocl_online#797).

Follow-up: #1087's new head merged in, with #1086's review answers (8a79df0). Adapted here: get_system_queryset, CodeReferenceBatch.found_by_version and ImportedValueSet.held_at_version read what the reference's viewer can view (can_view, viewer_id: its creator, or at request time the caller), and the versions the caller can't view are left out before member versions are kept. Codex check: three P1s answered, not changed: the core keeps the API's rule that a stored expansion's members are what anyone who can view its collection sees; FHIR leaves out members of code systems its caller isn't shown when it answers.

Follow-up: #1087's new head merged in (5a06ee5, no conflicts): a failed version check names the version it used by its id.

Follow-up: one rule for where a member of an expansion built without FHIR's rules goes when no version it recorded holds it (c8205a4, refined in 75d0f84 and 73fd5b4). It goes in its source's HEAD when HEAD holds it; else in each live release that holds it; else in each draft or retired release that holds it; else, when no active version holds it (an older row OCL keeps for what took it), in HEAD. Before, it always went to HEAD, even a row only a release holds. The rule is the same for the version comparisons (legacy_memberships) and for an imported value set's versions and members. At request time, an import's versions the caller can't view are left out, as a reference to a code system finds nothing in one (49da2c8). Builds outside a request are unchanged. Each step has a test that fails without it (f7a529e adds a row a live release and a newer draft share). Full suite on 73fd5b4: 4,322 + 803 tests OK, coverage 97%, pylint 10.00. Codex checks: three P1s fixed, and a missing test added. One P1 answered: a row held only by a public retired release goes in it and can win versionsMatch, then not be listed by FHIR, which lists live releases only. A recorded retired release already behaves that way, so it's a next step for versionsMatch's ranking.

🤖 Generated with Claude Code

https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm

paynejd and others added 9 commits November 29, 2026 13:55
…, behind the per-evaluation switch

Under FHIR's rules each concept member records the source version that selected it (a new table beside the
membership), one member per concept per source version; exclusions by what they name; versionsMatch; value sets that
hold a concept in a version; health, the rebuild snapshot, comparison and the stored $expand read them. Without the
rules nothing reads or writes the new table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
… a version, an import's namespace read once

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
…s, each case built without the rules, left to the toggle, and with them

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
…time, exclusions at other versions, import parameters, comparison by version, hidden releases

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
…ated with their parameters, imports' own rules and imports of imports, comparison apart, switch-off rebuilds

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
…ilds with the locks kept, every build under the rules re-evaluated, imports read as built, deletes gated on the switch

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
…rite in one place

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
…ules on late is rebuilt from the references given, exclusions of value sets in each version, stale memberships pruned

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
…ed by the behaviour they pin

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
paynejd and others added 4 commits November 29, 2026 18:27
…s a code of another copy of a code system version by code

Under FHIR's rules a value set that a reference with a system names holds a candidate concept by concept rows: its
members, or what its includes select there. A code system a request carries beside a stored one is loaded as another
source with the same canonical URL and versions, its concepts new rows, so an include naming the carried code system
intersected with a stored value set lost every code the two have in common.

A value set now also holds, by code, a candidate whose code it has as a member in another copy of that code system
version: a source version with the same canonical URL and version (never HEAD), as FHIR names a code by (system,
version, code).
- Only a copy the public can view is read (the version active, with an active HEAD, both public): who may view a
  private one depends on a caller the evaluation doesn't know. A member row deleted since doesn't count.
- Codes compare as FHIR's operations spell them: a member's and a candidate's code are both decoded, as
  core.fhir.ops.concepts.fhir_code decodes them, since OCL may store a code percent-encoded.
- Two source versions with different canonical URLs or versions never match, and nothing changes without FHIR's rules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qWnJcYXZ5geiBgML73
…52-b-versions) into versions kept apart

Brings in #1087's follow-up: a version parameter naming a canonical URL applies whichever
repository with it holds the version (version_rules keys parameters by repository and canonical URL). One import line
conflicted; both kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qWnJcYXZ5geiBgML73
…sion found by canonical URL too, as the version rules key it

The version rules now key a parameter by the repository its entry names and, for one naming a canonical URL, by that
URL (#1087). So force-system-version can send a reference to another repository with the URL.
- The two places here that ask whether force-system-version fixes a reference's version (MemberVersions.forced,
  ImportedValueSet.forced) ask the rules the same way, so they agree with the version the build gives the reference.
- An imported value set's reference is read as of the repository its own force-system-version sends it to, not the
  one its system resolves to, when the import is asked what it holds there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qWnJcYXZ5geiBgML73
…odes read from every row in its release, forced exclusions kept at their own source

Two fixes from the review of the last two commits:
- A candidate whose code OCL stores percent-encoded is matched by code (as FHIR spells it) when it's a row the release
  holds (one made in the release included) or the HEAD row of one, as an extensional member is. They're read in one
  indexed query on the release's concept links (on production, about the cost of reading LOINC 2.81's links once).
- An imported value set's exclusion that its own force-system-version sends to another source is read as of that
  source and of its own: from its own it takes away what it selects in the forced version, as the import's build did.
  An include is read as of the source its force sends it to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qWnJcYXZ5geiBgML73
paynejd added a commit that referenced this pull request Nov 30, 2026
…import evaluated again carries what reading it by version needs

Rebased onto the versions-kept-apart branch (#1090). An import evaluated again outside a request
(EvaluatedImport) now carries what reading it by version reads: its parameters, how it was built, whether it keeps
versions apart, the versions it recorded and its memberships, read before its rollback. ImportedValueSet reads
memberships through one helper that answers for both a stored expansion and such an import.

Tests: an intersection and an exclusion ask an import by version, as versions kept apart read it; a carried import's
marker covers its memberships too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qTSj4R6AHvrLWEVgWn
…52-b-versions) into versions kept apart

Brings in #1087's new head, which carries #1086's review
answers: a request-time evaluation evaluates only expansions built under it, for its caller, and a
version the caller can't view is answered as one not found, by the version rules' parameters too.

One conflict, in core/collections/models.py: this branch moved the system version's queryset into
get_system_queryset, and #1086 made the check there ask the evaluation's can_view (its viewer: the
creator, or at request time the caller). get_system_queryset now asks can_view.

Adapted to the new interface (each creator check goes through the evaluation):
- CodeReferenceBatch.found_by_version reads viewability by the reference's viewer, as the batch now
  records it (it was keyed by creator, so at request time it found nothing).
- member_versions.held_at_version groups plain codes by their reference's viewer and asks can_view.
- In add_references the versions a caller can't view are left out before kept.meet, so a kept member
  version is never one the caller can't view (as for the version rules' checks).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qWddiMQHvvG5BYWRfh
paynejd added a commit that referenced this pull request Nov 30, 2026
…r-452-b-apart) into imports

Brings in #1090's new head, which carries #1087's and
#1086's: a request-time evaluation evaluates only expansions built under it, for its
caller (what references find is what the caller can view) unless opened for none, a version the caller
can't view is answered as one not found, and a statement runs as it is while the evaluation has no
deadline.

Conflicts, in core/collections:
- evaluation.py: ExpansionEvaluation keeps both sides' state (the caller, its own expansions and what it
  may view; what it built and the imports it evaluated). at_request_time's description keeps both halves,
  the no-seconds sentence included. within_deadline is #1086's, which lets a statement through with no
  deadline, as this branch's did.
- models.py, build_at_request_time: both sides kept: refused for a version the evaluation may not build;
  locks_of's locks and the parameters it was built under recorded; the expansion registered as its own.

Adapted to the new interface: this branch opens an evaluation at request time outside a request too, so
that nothing a stored expansion's import evaluates again is kept (ValueSetImports.evaluate). An opening
answers its caller unless opened with for_caller False, which #1086 now offers, so
that path opens with for_caller False: the import is evaluated as its references' creators view it, as
the stored build is, rather than as an anonymous caller (which refused a private import).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qWddiMQHvvG5BYWRfh
paynejd added a commit that referenced this pull request Nov 30, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7

# Conflicts:
#	core/collections/models.py
paynejd and others added 2 commits November 30, 2026 02:56
… in the versions that hold it

An expansion built without FHIR's rules records its members as concept
versions, and the versions they're read in are the ones it recorded that hold
them. A member none of them holds was placed in its source's HEAD, even when
HEAD doesn't hold it and only a release does. It is now in its source's HEAD
when HEAD holds it or no active version does (an older row OCL keeps for what
took it), else in each active version that holds it. One rule, in
unrecorded_placements, for legacy_memberships (the version comparisons) and
an imported value set's versions and members. So an evaluation that leaves
out versions its viewer can't view leaves such a member out with its release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
… the caller can't view are left out

A reference with value sets and no system is evaluated in each version its
first value set has members in. Those versions weren't asked of the
evaluation, so at request time a release the caller can't view (one the
import recorded, or one that alone holds a member's row) still gave its
members, and with versionsMatch could outrank a release the caller is shown.
They're now left out as a reference to a code system finds nothing in one
(ExpansionEvaluation.hides). A build outside a request is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
paynejd added a commit that referenced this pull request Nov 30, 2026
paynejd added a commit that referenced this pull request Nov 30, 2026
paynejd and others added 4 commits November 30, 2026 03:35
… releases that hold it, else HEAD

A member no recorded version holds went in each active version that holds
it, drafts and retired ones too. Those aren't what OCL resolves a source to,
nor what FHIR lists members in, so such a member could rank above a live
release under versionsMatch and then not be listed, or make an expansion
look as if it mixed versions of its source. It now goes in each live release
(released, active, not retired) that holds it, else in its source's HEAD as
before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
…etired releases hold goes in them

A member no recorded version holds, that HEAD and every live release don't
hold, fell back to its source's HEAD. At request time a caller who can't view
the draft or retired release that alone holds it was then given it, at HEAD,
where versionsMatch could rank it above a live release's row of the same
concept. It now goes in each active version that holds it, so an evaluation
for the caller leaves it out with them; only a member no active version holds
goes in HEAD. A member a live release holds still goes there first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
…ease and a newer draft hold is the release's

A row HEAD has moved on from, held by a live release and a newer draft that
the expansion recorded neither of, is the live release's member only, and
versionsMatch keeps it there. It fails under the rule before 75d0f84, which
put it in both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
…given to the caller at HEAD either

The request-time test now also rules out the member only a private draft
holds coming back at its source's HEAD, as it did under 75d0f84's rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
paynejd added a commit that referenced this pull request Nov 30, 2026
paynejd added a commit that referenced this pull request Nov 30, 2026
paynejd added a commit that referenced this pull request Nov 30, 2026
paynejd added a commit that referenced this pull request Nov 30, 2026
paynejd added a commit that referenced this pull request Nov 30, 2026
paynejd added a commit that referenced this pull request Nov 30, 2026
…es), which the FHIR series builds on

fhir-452-b-codes ends the queue of OCL API PRs, so the FHIR series sits on all of them:
#1085, #1086, #1089,
#1088, #1087, #1090,
#1091, #1092 and #1093, and
the core changes that need them together. No conflicts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qV2gzsF5GhF5BwTJ6x
…ge into fhir-452-b-apart

Takes #1087's new head, which brings in master
(#1085, #1086 and
#1082). Merged cleanly; no change to this branch's
own code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qWUTom6BGgjQy6oehZ
@paynejd
paynejd marked this pull request as ready for review December 1, 2026 07:26
@paynejd
paynejd requested a review from snyaggarwal December 1, 2026 07:26
@paynejd

paynejd commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

A performance fix is coming to this PR before it merges. ImportedValueSet.members_at() in core/collections/member_versions.py combines two id subqueries with OR inside versioned_object_id IN (...). The primary key can't serve that, so on a large database Postgres scans the whole concepts table. It only runs under FHIR's rules, for a reference that names both a system and a value set, so API users on master's behaviour aren't affected. The next push replaces it with a UNION, so each side uses the primary key, keeps the same results, and adds a test on the query's shape. I'll say here when it's pushed.

…version are read by the primary key

ImportedValueSet.members_at read a value set's members in a version (built without the rules) as those recorded there
OR those placed there unrecorded: two IN subqueries joined by OR, which PostgreSQL can answer only by reading every
concept. Wrapped in versioned_object_id IN (...) for each code a reference with a system and value sets checks, it
read all of concepts per statement (on 1.5 M concepts, about 0.6 s each, nine a request). It's now one IN over their
UNION ALL (ids_in), answered by the primary key, with the same members; code_matches reads its copies the same way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qX3xher9sfoCLLKrTC
@paynejd

paynejd commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

The fix is pushed in 9c35a2e (+27/−8, two files). ImportedValueSet.members_at now reads both id sets as one IN over a UNION ALL, which the primary key answers, with the same members. A new test, test_an_import_s_members_in_a_version_are_read_by_the_primary_key, guards the shape: it fails on the previous head and passes now. All 678 core.collections tests pass, and pylint scores 10.

…into fhir-452-b-apart

Takes #1087's new head, which brings in master
(#1089, #1109 and
#1106): REFERENCE_SEARCH_OPTIONS_TOGGLE is on in every
environment, as on master. Merged cleanly; no change to this branch's own
code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
paynejd added a commit that referenced this pull request Dec 2, 2026
…o fhir-452-c-storage

Takes #1090's new head, which brings in master
(#1089, #1109 and
#1106): REFERENCE_SEARCH_OPTIONS_TOGGLE is on in every
environment, as on master. Merged cleanly; no change to this branch's own
code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed

@snyaggarwal snyaggarwal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: request changes. One small fix, in migration 0078: its lock waits need a bound (inline). The rest is ready.

Merge order: first #1087, then retarget this PR to master. Merge master into it and let CI run on that, including #1093 if it has landed by then. Then merge this PR, with #1103 right after it. #1088 conflicts with this stack in core/collections/evaluation.py and core/collections/models.py, so whichever of them merges second needs a master merge first.

I reviewed only this PR's own diff against fhir-452-b-versions.

With the switch off, nothing changes. I traced every new path:

  • MemberVersions reads the toggle only in these cases: an expansion already marked versions_apart, a switch value the caller passed, versionsMatch, a reference with value sets, or a second version of one source. When the answer is off, it never reads or writes collection_expansions_concept_memberships.
  • capture() iterates the queryset that rel.add() has already evaluated, so it runs no extra statement.
  • Health, comparison, the dry-run snapshot, reference deletes and stored $expand all check versions_apart first.
  • The API refuses versionsMatch while the toggle is off.

That matches the SQL-capture tests.

Non-blocking:

  • $expand of an expansion that keeps versions apart runs Expander.entries() three times: once for the count, once for the page and once in used(). Each run is a NOT IN plus a correlated Exists over members(), and once #1093 merges, members() also carries its check for rows the caller is shown. This only runs behind the switch, and PERF-18 says the draft series is reworking the same kind of statement (selected_shown, #1097). Before the toggle is turned on anywhere, please time it on a LOINC-sized expansion that keeps versions apart.
  • Interaction with #1093: git merge-tree shows no conflict, and the two rules agree: each shows only rows that a version the caller is shown holds. This PR's FHIR $expand tests were written against master's members(), though, so CI needs to run on the merge once #1093 lands.
  • The members_at UNION fix you raised (9c35a2e) is in and has a test on the query's shape. The ranking of retired releases under versionsMatch, which the description notes, is fine as a next step.

]

operations = [
migrations.AddField(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking. Django runs this migration in one transaction, so the ACCESS EXCLUSIVE lock that this AddField takes on collection_expansions is held until the migration ends. That includes the time the foreign keys below spend waiting for SHARE ROW EXCLUSIVE on concepts and sources.

That wait lasts as long as any open write transaction on concepts, a bulk import for example. All that time, every read of collection_expansions queues behind this migration: collection pages, $expand, builds. The description's "deploy when no import is running" is the only guard.

#1103 found the same problem in its own migrations and fixed it by making SET LOCAL lock_timeout = '5s' the first operation, applying and reversing (a53b7ff, a744936). Please do the same here. Moving this AddField after the RunSQL that adds the foreign keys would also shorten how long collection_expansions is locked against reads.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, you're right. Fixed in 6b3a81a: every lock wait is now bounded, applying and reversing, and the column is added after the foreign keys. Two things differ from what you suggested:

  • The bound is 0.3 s for each lock, not 5 s. The migration takes locks on three tables, so it can end up in a deadlock with another transaction. With 5 s, PostgreSQL's deadlock check (after 1 s) can cancel the other one: I got it to cancel a transaction that writes sources and then concepts. Three waits of 0.3 s end before that check, so it's always the migration that gives up.
  • collection_expansions comes last for its foreign key too, and its ACCESS EXCLUSIVE lock is the first one taken there. With only the column moved, the foreign key took a weaker lock first and the column a stronger one later. A build that had read the table and then wrote to it was cancelled at once.

Tried on PostgreSQL 14.7:

  • A write held on concepts for 30 s: before, reads of collection_expansions waited 25 s. Now the migration gives up and they wait 0.1 s.
  • A read held on collection_expansions: other reads waited 25 s before, and now 0.3 s.
  • Six deadlock cases: the other transaction went on in all of them.

A try that gives up changes nothing. Run it again once the other transaction is over; it then applies in 0.3 s. The description's Migration section says what that means for a deploy.

Merging master, and $expand's three reads of the entries, come separately: we're timing those now on sets kept apart of up to 110,000 members.

@snyaggarwal

Copy link
Copy Markdown
Contributor

Merge call: merge after the 0078 fix and after #1087, as the description says: merge #1087 first, then retarget this PR to master without a rebase. That fits the Dec 8–12 slot in the road (ocl_online#797).

My non-blocking note about Expander.entries() was already yours. You found stored $expand of an expansion that keeps versions apart running past 57 s for 38,363 members, and fixed it in the draft #1097 (ocl_online#797). That doesn't block this PR, since the switch stays off. It does mean EXPANSION_FHIR_RULES_TOGGLE shouldn't be turned on for API users, which the road plans for Dec 15–19, until #1097's fix is on master.

… and locks the expansions last and once

Applied or reversed, it waits at most 0.3 s for each lock, then fails and
changes nothing. Its three waits together are below PostgreSQL's
deadlock_timeout (1 s), so in a deadlock this migration gives up and the other
transaction goes on.

collection_expansions, whose lock reads wait for, comes after the foreign keys
to concepts and sources, so it isn't held while they wait for a write there.
Its strongest lock is the first taken: a weaker one made stronger later is a
deadlock with a build that has read the table and then writes to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed

@snyaggarwal snyaggarwal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: approve. 6b3a81a fixes the blocking point on 0078.

  • Every lock wait is bounded. SET LOCAL lock_timeout = '300ms' runs first when the migration is applied, and the closing no-op/reverse pair makes it run first when it's reversed. It lasts until the migration's own transaction ends. Your reason for 0.3 s rather than my 5 s is right: the three waits together stay under deadlock_timeout, so in a deadlock the migration is the one that gives up. I hadn't thought of the deadlock check.
  • collection_expansions is locked last. It takes ACCESS EXCLUSIVE before its foreign key, so reads of it no longer queue while the migration waits for writers on concepts and sources. It also never asks for a stronger lock on a table it already locked.
  • Reversed, the migration drops the column before the foreign keys, so it locks collection_expansions first. Each later wait is still capped at 0.3 s, so reads queue for under a second. That's fine for a rollback.
  • Tests: the new MigrationTest applies, reverses and applies again, and pins the order of the operations. CI is green at 6b3a81a.

Merge order is unchanged: first #1087, then retarget this PR to master without a rebase. Merge master into it and rerun CI, then merge it, with #1103 right after. EXPANSION_FHIR_RULES_TOGGLE stays off for API users until the $expand fix you're timing is on master.

@paynejd

paynejd commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

On your timing point, from the review's non-blocking list: you were right. I timed stored $expand on expansions that keep versions apart, with this PR and #1093, #1091 and #1094 together, as it would be when the rules are switched on. Seconds for count=0 / count=10 / count=10 with a text filter, where a request may take 5 s (the times before were measured with that limit raised):

Members, selected at HEAD Before With the fix
5,000 2.2 / 3.2 / 2.4 0.2 / 0.2 / 0.2
38,363 4.2 / 5.4 / 15.6 1.0 / 0.9 / 1.4
110,000 6.9 / 9.1 / 14.3 1.9 / 2.0 / 3.0

The fix is in #1094, so this PR doesn't change. Expander.entries() now reads the rows and what selected each once, in plain statements, and compares them in Python. The total, the page, the filter and used-codesystem all come from that one reading. #1094 has to be on master before the toggle is on anywhere, and its description has the details.

paynejd added a commit that referenced this pull request Dec 4, 2026
…o fhir-452-c-storage

Takes #1090's new head. Merged cleanly; no change to this
branch's own code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
paynejd added a commit that referenced this pull request Dec 4, 2026
…or _apply_filters takes any further arguments

#1088 gives CollectionReference._apply_filters a further argument, so the
stand-in that #1090's exclusion test patches in takes it too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qV2gzsF5GhF5BwTJ6x
paynejd added a commit that referenced this pull request Dec 4, 2026
…o fhir-452-c-storage

Takes #1090's new head. Merged cleanly; no change to this
branch's own code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
paynejd added a commit that referenced this pull request Dec 4, 2026
…nto fhir-452-b-codes

Takes #1091's new head: #1090 now
has the test stand-in's fix this branch had (f990fd0), and an imported value
set whose expansion recorded no versions is read by those its references
resolve to, or evaluated again. No change to this branch's own code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
paynejd added a commit that referenced this pull request Dec 4, 2026
…o fhir-452-c-storage

Takes #1090's new head. Merged cleanly; no change to this
branch's own code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
@paynejd

paynejd commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

@snyaggarwal, please don't merge this one before our next push to it, although GitHub shows it as mergeable and you've approved it.

#1121, now on master, names each member of a comparison by its source and code. This PR has its own version of that step for expansions that keep versions apart, and it still goes by code alone. The lines don't clash, so git merges them, but then comparing such an expansion fails with an error, and three of this PR's tests fail on master.

The fix is two lines, with tests, and it comes in the push we make after #1087 is merged. It'll be a commit of its own, so you can look at just that.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants