Repository navigation
Description
What type of issue is this?
- Bug report
- Feature request
Prerequisites
- I have written a descriptive issue title
- I have verified that I am running the latest version of the SysML2.NET
- I have searched open and closed issues to ensure it has not already been reported
Description
resolve_commit_state returns the whole state of a commit. A consumer that needs a bounded slice of it in a stable order has to resolve the whole state and then discard everything outside the slice. A bounded slice here means the next N identities after a given one, optionally limited to some metaclasses:
SELECT …
FROM sysml2.commit AS state_commit
CROSS JOIN LATERAL sysml2.resolve_commit_state(state_commit.project_id, state_commit.id) AS state
JOIN sysml2.element_version AS data_version ON … = state.version_id
WHERE state_commit.id = @commit_id AND data_version.identity_id > @after AND data_version.class_kind = ANY(@class_kinds)
ORDER BY data_version.identity_id
LIMIT @limit;resolve_commit_state folds the walked ancestry with DISTINCT ON (identity_id), plus the checkpoint arm, before the bound and the LIMIT can apply. A slice therefore costs O(state of the commit), and reading a whole commit slice by slice costs O(state × slices), which is quadratic in the model size.
Any API layer built on the schema needs this. The Systems Modeling API PIM returns whole collections, for example getElements( project : Project, commit : Commit ) : Element [0..*] (Systems Modeling API and Services 1.0 §7.2.2). Each PSM bounds them in its own way: the REST/HTTP PSM with cursor pagination (§8.1.3), and the OSLC PSM through OSLC Query (§8.2). The schema should not know about either mechanism. It is still the only place a slice can be resolved efficiently: resolving one outside the schema means reimplementing the checkpoint arm of resolve_commit_state.
Measured on PostgreSQL 18 (Testcontainers), a commit of 100,001 elements over 2 commits, read in 100 slices of 1,000:
| Strategy | 100 slices | Per slice |
|---|---|---|
resolve_commit_state + bound (today) |
8.2–8.6 s | ~85 ms, grows with the commit |
DISTINCT ON fold with the bound pushed inside |
1.9–2.4 s | sorts everything after the bound |
| Ordered skip scan (proposal below) | 0.7–0.9 s | ~8 ms, flat |
All three returned identical identity sequences.
Proposal
-
A range resolver next to
resolve_commit_state. It has the same semantics: the newest version in the walked ancestry, tombstones removed, checkpoint-bounded. It also returns the same output columns:sysml2.resolve_commit_state_range( p_project_id uuid, p_commit_id uuid, p_from uuid, -- exclusive bound in walk order, NULL to start at the first identity p_limit int, -- the most rows returned p_class_kinds smallint[], -- NULL for every kind p_descending boolean DEFAULT false) RETURNS TABLE (identity_id uuid, version_id uuid, derived_id uuid) -- ordered by identity_id, descending when p_descending
Without a checkpoint in the walked ancestry, walk
element_version (project_id, identity_id, commit_id)one identity at a time fromp_from. Resolve each identity with one indexed lookup over the ancestry (ORDER BY created DESC, id DESC LIMIT 1), skip tombstones, and stop atp_limit:walk (identity_id) AS ( (SELECT identity_id FROM sysml2.element_version WHERE project_id = p_project_id AND identity_id > p_from ORDER BY identity_id LIMIT 1) UNION ALL SELECT (SELECT identity_id FROM sysml2.element_version WHERE project_id = p_project_id AND identity_id > walk.identity_id ORDER BY identity_id LIMIT 1) FROM walk WHERE walk.identity_id IS NOT NULL) SELECT … FROM walk CROSS JOIN LATERAL (newest version of walk.identity_id in the ancestry) AS latest WHERE NOT latest.tombstone LIMIT p_limit;
With a checkpoint, merge in identity order the
commit_checkpointrows of the checkpoint commit with the versions written after it, as the checkpoint arm ofresolve_commit_statedoes today. The checkpoint's primary key(project_id, commit_id, identity_id)already gives that order. -
An index that lets a kind-restricted range walk only that kind, in identity order. A version cannot change its identity's metaclass (the composite FK to
data_identity (id, class_kind)), so filtering on the identity's kind is exact. The existingix_data_identity_project_kind (project_id, class_kind)carries no identity order, though. A range over a rare kind, for exampleProjectUsage, therefore still walks every identity of the project. Proposed:CREATE INDEX ix_data_identity_project_kind_id ON sysml2.data_identity (project_id, class_kind, id);
When
p_class_kindsis set, walk the identities of those kinds through this index and probe the ancestry (andcommit_checkpoint) per identity.resolve_kind_at_commitalready drives its checkpoint arm the same way.
Reading the whole state stays with resolve_commit_state; the range resolver is for bounded reads only.
Acceptance criteria
- Concatenated, the ranges of
resolve_commit_state_rangereturn exactly the rows ofresolve_commit_state, filtered by kind and ordered by identity. This holds ascending and descending, over histories with deletions, merges and sibling commits sharing a timestamp (id DESCtie-break), and with and without a checkpoint in the walked ancestry. - A range touches O(limit × walked ancestry) rows, not O(state). An
EXPLAIN (ANALYZE)on a 100k-element commit shows no full scan or sort of the commit's versions. - The schema change ships as a migration consumers can apply on top of
0001_initial.sql. The script is generated, so the generator needs the same change.
Steps to Reproduce
- Create a project and a commit with 50,000
Namespaces owned through 50,000OwningMemberships (100,001 elements). - Read the commit in slices of 1,000 identities with the query above.
- Each slice costs about the time of resolving the whole state, and the total grows quadratically with the commit size.
System Configuration
- SysML2.NET version: 0.27.0 (
SysML2.NET.Dal) - Environment: PostgreSQL 18 (Testcontainers, podman on Windows 11)
- .NET Framework version: net10.0
- Additional information: found while implementing Mycelium Fabric, a server of the Systems Modeling API REST/HTTP PSM, which serves its paged responses from
resolve_commit_state
Activity
[-][Feature]: Keyset-paged state resolution at a commit (sysml2.resolve_commit_state_page)[/-][+]Resolve the state of a commit as an ordered, bounded range of identities[/+]