Skip to content

OpenConceptLab/ocl_online#452 | Stored $expand, $validate-code and $translate show a member only where a version the base shows holds its row - #1093

Open
paynejd wants to merge 5 commits into
masterfrom
fhir-452-expand-held
Open

paynejd wants to merge 5 commits into
masterfrom
fhir-452-expand-held

Conversation

@paynejd

@paynejd paynejd commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Part of OpenConceptLab/ocl_online#452.

What it does

FHIR's stored $expand, ValueSet $validate-code and the value set scope of ConceptMap $translate read a stored value set's default expansion. A member row was shown whenever the request's FHIR base showed its code system. Concept rows are held by source versions, though, and a public code system can have a release the base doesn't show. A row only such a release holds now leaves no trace in those answers, as FHIR's rule for members says (OpenConceptLab/ocl_online#451): not listed, not counted, not in scope, and with the same statements run as without it.

  • FhirResolver.shown_concepts: a row is shown when its code system is shown and an active version the base shows holds it. A row that no active version holds (an older row OCL keeps for the collections that took it, or one whose only release was deleted) still goes with its code system, as before. In production that's tens of thousands of expansion links, and they stay shown.

  • Cost: a page of a large value set takes longer than on master. SQL for a page of 1,000, measured on value sets built from a local copy of real data, with production's planner settings (medians):

    Value set master this PR
    38,363 members warm 0.31 s 0.52 s
    38,363 members cold start 0.40 s 0.67 s
    92,670 members warm, on a busy machine 1.7 s 4.0 s
    92,670 members cold start 1.5 s 2.6 s
    • The members of both sets are in no release themselves. That's the slowest case: $expand then also looks for another version of each concept.
    • The slowest request measured is a filter over the 92,670 members: 5.5 s of SQL against 3.5 s on master. Past 5 s, $expand answers 422 (too costly).
  • How the rule is checked: an expansion of up to 1,000 stored rows has each row checked by shown_concepts. A larger one uses the same rule written as joins (shown_members), so its statements keep their parallel plans: two workers for each statement over the members in production's plans, as on master. Only the number of rows stored in the expansion decides which, and counting them (up to 1,001) is one small statement more. Looking up one code always uses shown_concepts: HEAD holds nearly every row, so it is asked first.

  • The evaluation of content a request carries (the phase B work) reads stored members the same way, so stored and carried answers agree.

The OCL API is unchanged. It lists a collection's stored members to anyone who can view the collection, and it serves a concept's versions under its code system's access. These members were already listed there, so this restores FHIR's rule rather than closing an exposure.

What the rule decides (noted for review):

  • A row that left HEAD and is held only by a release the base doesn't show is hidden until that release is shown.
  • A row whose only holders were deleted goes with its code system, like any row no version holds.

Tests

  • core/fhir/tests_ops_expand.py:
    • a row only a release the caller isn't shown holds: an expansion with it answers byte for byte, and with the same statements, as one without it, at the global base for anonymous callers, outsiders, members and staff, and at an owner base for those it doesn't show it to. A deleted public release holding it too shows nothing. Staff at the owner base see it.
    • a row no version holds, or only a deleted one, stays a member.
  • core/fhir/tests_ops_validate_code.py: such a row's code answers as one the value set doesn't have, with the same statements.
  • core/fhir/tests_translate.py: such a row doesn't put its code in a source or target value set's scope (same answer and statements, forward and reverse).
  • Each of the three fails without the fix.
  • Both forms of the rule agree row by row, for every mix of holders, callers and bases. Every $expand case runs with each form.
  • A private release of a public code system changes no answer and no statement, with either form.
  • One test fails if the joins check rows one by one. Another fails if a small expansion can't be read where Postgres has recorded the tables as empty, which happens when autovacuum passes during a test and made the earlier form of the joins hang.
  • Full suite as CI runs it (--parallel=4, then the search-index tests) on fdc25ba: pylint 10.00; 4,111 + 804 tests OK; coverage 97%.

Review

  • Codex, pass 1: two P2s, both fixed. A deleted release no longer counts as holding a row. The $validate-code test now compares the statements run.
  • Codex, pass 2: clean.
  • Codex, a full pass on the rule's final form (fdc25ba): no P0 or P1, and three P2s, each accepted with a reason in a comment below. The other reviews here were of the earlier commits.
  • Simplification review: nothing to cut.
  • Adversarial pass (a fresh reviewer):
    • It found $translate's value set scope still on the old rule. Fixed, with a test; a Codex check of that commit was clean.
    • It noted the two rule consequences above, for review.
    • It noted that a code system with a public HEAD and no public release lists its HEAD's rows. That was already so before this change, and is unchanged.

CI note (Nov 30): the first Tests run failed on ConceptParentsViewTest.test_get_200, which compares two parents that can come back in either order. That's an order-dependent test this PR doesn't touch; the rerun passed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7

paynejd and others added 3 commits November 30, 2026 02:07
…w a row only where a version the base shows holds it

A stored expansion's member was shown when the base showed its code system's
HEAD. A row that only a release the caller isn't shown holds (a private
release of a public code system) was then listed with its own content.
FhirResolver.shown_concepts adds that a version the base shows must hold the
row, as the evaluation of carried content already reads members. A row no
version holds (an older row OCL keeps for the value sets that took it) still
goes with its code system. HEAD, which holds nearly every row, is asked first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
…ing a member's row

A deleted release the base shows no longer makes a row it holds shown, and a
row only deleted versions hold goes with its code system, as one no version
holds. The $validate-code test compares the statements run too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
…code only in a row the base shows

ConceptMap $translate's source or target value set held a code when its
default expansion had any active row of it in a code system the caller is
shown, so a row only a release the caller isn't shown holds still put the
code in scope. holds() now reads the rows as stored $expand lists members
(FhirResolver.shown_concepts), so the code answers as one the value set
doesn't hold, as ValueSet $validate-code answers it.

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
…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
@paynejd
paynejd marked this pull request as ready for review December 1, 2026 05:34
@paynejd
paynejd requested a review from snyaggarwal December 1, 2026 05:34
@paynejd

paynejd commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Please hold this one: a performance fix is coming before it merges. With the check added here, two queries of a stored $expand (the count, and the code systems used) can no longer run in parallel on a large database. The answers are right, but we estimate that the largest public value sets, about 36,000 to 40,000 members, would take around 50% longer and come close to the 5 s limit. That's an estimate from query plans, not a measurement. The next push keeps the same answers and lets those queries run in parallel again. I'll say here when it's pushed.

…ound with joins, so its statements can run in parallel

The check that a version the base shows holds a row was an OR of correlated subqueries. Postgres checks those row by row
and keeps the statement out of parallel workers, so the total and the code systems used of a large expansion lost theirs.

FhirResolver.shown_members is the same rule over a whole expansion, as joins: not among the expansion's rows that active
versions hold and none of them is shown. $expand uses it. shown_concepts stays for the rows of one code
($validate-code, $translate).

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

paynejd commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

The performance fix is pushed (2078274), so this is ready for review again. The answers are the same, and every earlier test passes unchanged. The rule is now written as joins for a whole expansion, so the large statements run in parallel again, with no index and no migration. The Cost paragraph above has the new numbers. Please merge this and #1092 before #1094, which contains both.

@paynejd

paynejd commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Please hold this one again, sorry. The new form of the check (2078274) can hang in a test database: when autovacuum records the tables as empty in the middle of a test run, Postgres picks a plan for it that doesn't finish. A server with real statistics plans it well, so this is about CI failing or hanging at random, not about production's answers or speed. We're testing a form that avoids it, and I'll say here when it's pushed.

…ws one by one, and finds a large one's with joins

Postgres nests shown_members' joins without end where it has recorded the tables as empty. A test's are, once autovacuum
passes: its rows are in an open transaction, and a hundred of them are enough.

An expansion of up to 1000 rows has them checked one by one again, by shown_concepts: it is no slower there, and can't be
nested. A larger one keeps the joins. Row by row, an expansion of 90,000 rows takes $expand past the time an answer has
when members are in no public release.

Which of the two depends on the expansion's stored rows alone (one small statement more), so a private release changes no
statement. The $expand cases now run for a large expansion too, with ANALYZE in each test's transaction: Postgres then
knows its rows, and autovacuum keeps off the tables until it ends.

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

paynejd commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Codex review, full pass on the rule's final form (codex-cli 0.169.0, commit fdc25ba)

Verdict: no P0 or P1. Three P2s, all accepted.

  • F1.1 (P2), a hidden row can take an expansion from 1,000 stored rows to 1,001, and so to the joins; with bad statistics those might pass the time limit. Accepted. With 999, 1,000 and 1,001 stored rows, and 0, 1 or 10 of them hidden, both forms gave the same bytes with the same number of statements, and no request's median was over 0.8 s. With the tables never analyzed, a page at 1,001 rows takes 1.5 s with the joins and 1.3 s row by row (3.3 s on master).
  • F1.2 (P2), the plan test checks that the joins check nothing row by row, not that the plan is parallel. Accepted. Whether Postgres uses parallel workers depends on the size of the tables and on settings a test database doesn't have. What the test checks is what kept the statements out of them, and production's plans show two workers for each.
  • F1.3 (P2), the test that records the tables as empty needs a database superuser. Accepted. CI and the compose files use one.
findings.json
{
 "reviewer": "codex-cli 0.169.0",
 "perspective": "defects",
 "mode": "full",
 "commit": "fdc25ba282182bf7915804014b1d9367572024b6",
 "verdict": "findings",
 "prior": [],
 "findings": [
  {
   "id": "F1.1",
   "severity": "P2",
   "tag": null,
   "file": "core/fhir/resolve.py",
   "line": 342,
   "claim": "A hidden member can switch the SQL at the cutoff and make its presence observable in the HTTP status.",
   "scenario": "An anonymous caller expands a value set with 1,000 stored rows. Adding one row held only by a private release leaves the visible members unchanged but selects the join path. After a bulk load, PostgreSQL can still estimate the relevant tables as empty. If the resulting nested joins hit the request deadline while the row-by-row path finishes, the caller sees 422 too-costly instead of 200 without measuring latency. Creating a private release alone leaves the branch unchanged when stored membership stays fixed. [PostgreSQL planner statistics](https://www.postgresql.org/docs/14/planner-stats.html)",
   "action": "Keep hidden rows from selecting a different execution path. Test equivalent requests at 1,000 and 1,001 stored rows with stale statistics and the operation deadline active.",
   "lines_delta": null,
   "confidence": "medium"
  },
  {
   "id": "F1.2",
   "severity": "P2",
   "tag": null,
   "file": "core/fhir/tests_ops_expand.py",
   "line": 477,
   "claim": "The plan test can pass while the actual total and used-codesystem scans run serially.",
   "scenario": "The test forces the join branch on a four-row expansion, replaces the captured SELECT lists with SELECT 1, and accepts any plan without a SubPlan. A serial join plan passes. The rewrite also removes the release lookups projected by the real used-codesystem query. [PostgreSQL parallel plans](https://www.postgresql.org/docs/14/parallel-plans.html)",
   "action": "Explain both captured statements intact on a fixture and planner configuration that permit parallel scans, then assert that the main scans can run below a Gather or Gather Merge.",
   "lines_delta": null,
   "confidence": "high"
  },
  {
   "id": "F1.3",
   "severity": "P2",
   "tag": null,
   "file": "core/fhir/tests_ops_expand.py",
   "line": 489,
   "claim": "The stale-statistics regression test fails for an ordinary PostgreSQL test database owner.",
   "scenario": "A developer runs the FHIR suite with a non-superuser DB_USER that has CREATEDB and owns the test database. PostgreSQL rejects UPDATE pg_class with a permission error before the test sends an expansion request. [PostgreSQL database creation](https://www.postgresql.org/docs/14/sql-createdatabase.html) \u00b7 [PostgreSQL privileges](https://www.postgresql.org/docs/14/ddl-priv.html)",
   "action": "Create stale statistics with an ANALYZE before inserting the fixture, or skip the catalog-specific test when the test role lacks the required privilege.",
   "lines_delta": null,
   "confidence": "high"
  }
 ]
}

@paynejd

paynejd commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

This one is ready for review again, at fdc25ba. The hold is over: the new form of the check is tested against what hung the old one (tables recorded as empty), and its test run passed (4,331 and 816 tests). The description has what changed and what it costs: a page of a large value set takes longer than on master.

Please merge it before oclapi2#1094, with a merge commit.

@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.

Merge order: this PR doesn't depend on the #1087 → #1090 → #1103 stack and can merge to master now. You asked for it before #1094, with a merge commit. git merge-tree shows no textual conflict with #1090 or #1103. Once this lands, #1090 should merge master and rerun CI, since its Expander.entries() builds on members().

What I checked:

  • Both forms of the rule give the same answer. shown_members drops the "HEAD holds it" clause safely: the row's parent HEAD must already be shown and active, so that clause is a special case of "a version the caller is shown holds it". Your row-by-row equivalence tests cover holders, callers and bases.
  • $validate-code and $translate's value set scope use the same rule. Their per-code lookups stay on shown_concepts.
  • There is no migration and no index. The OCL API is unchanged.
  • Cost: the slowdown on large value sets is in the description. The 92,670-member filter goes from 3.5 s to 5.5 s, past the 5 s limit. The FHIR lead accepted it on Dec 3, as you recorded on the ticket, so I'm not treating it as blocking. One cheaper form is suggested inline as a follow-up.

Comment thread core/fhir/resolve.py
Exists(held.filter(Exists(shown.filter(id=OuterRef('source_id'))))) |
~Exists(held.filter(source__is_active=True)))

def shown_members(self, expansion_id):

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.

Suggestion for a follow-up, non-blocking. The hidden rows are the same for every statement of one request. They depend on the caller and the base, not on activeOnly, filter or the offset.

You could read them once per request: hidden is normally empty, because nearly every member is held by its HEAD or by a release the caller is shown, and that's true of the 92,670-member case too. Then apply ~Q(id__in=hidden_ids), or no condition at all when the list is empty. The count, the page and used() would go back to master's plans, and the anti-join would run once instead of in each of them.

That would likely bring the 5.5 s filter back under the 5 s limit. Expansions of 1,000 rows or fewer can still compute it row by row, to avoid the plan for tables recorded as empty.

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. Noted as a follow-up, not for this PR.

We measured something close to it: listing the hidden rows first, then leaving them out by id. It was fast while the list was short, but a large private release makes the list long, and the cap that needed made small value sets slower. Reading them once per request, only for expansions of more than 1,000 rows, is different enough to be worth measuring. It would go with another saving we've noted: when some members are in no public release, $expand works out the members twice, on master too.

paynejd added a commit that referenced this pull request Dec 4, 2026
$expand finds the members of a large expansion that a caller is shown with
parallel joins (#1093). PostgreSQL keeps the hash tables
those joins share between workers in shared memory, about 45 MB a request. This
repository's compose files give the database 128 MB, so a few requests at once
failed. Its statements no longer share hash tables: each worker builds its own,
about 1 MB a request. The setting ends with $expand's transaction; searches and
other operations keep theirs.

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

Copy link
Copy Markdown
Contributor

Merge call update: merge this together with #1092, not on its own.

#1092's description (9ac0986) shows that this PR's parallel joins share hash tables in shared memory. That's 38 to 46 MB per $expand on a large value set, and the compose files give the database 128 MB. Under a few concurrent requests, $expand fails. #1092 turns parallel hash off inside $expand's own transaction, and Jon asks that no release have this PR without it.

So merge this, with a merge commit and before #1094, in the same deploy as #1092. My approval of the code stands. The two conflict only in imports and a helper in core/fhir/tests_ops_expand.py, where both sides are kept.

paynejd added a commit that referenced this pull request Dec 4, 2026
A stored expansion's shown rows are found with joins (FhirResolver.shown_members), so
$expand's statements over a whole expansion can run in parallel. 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
$expand checks the rows of a small expansion one by one, and finds a large one's with
joins. 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

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