Skip to content

Resolve the state of a commit as an ordered, bounded range of identities #468

Backlog

Description

@antoineatsoprasteria

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

  1. 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 from p_from. Resolve each identity with one indexed lookup over the ancestry (ORDER BY created DESC, id DESC LIMIT 1), skip tombstones, and stop at p_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_checkpoint rows of the checkpoint commit with the versions written after it, as the checkpoint arm of resolve_commit_state does today. The checkpoint's primary key (project_id, commit_id, identity_id) already gives that order.

  2. 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 existing ix_data_identity_project_kind (project_id, class_kind) carries no identity order, though. A range over a rare kind, for example ProjectUsage, 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_kinds is set, walk the identities of those kinds through this index and probe the ancestry (and commit_checkpoint) per identity. resolve_kind_at_commit already 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_range return exactly the rows of resolve_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 DESC tie-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

  1. Create a project and a commit with 50,000 Namespaces owned through 50,000 OwningMemberships (100,001 elements).
  2. Read the commit in slices of 1,000 identities with the query above.
  3. 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

  1. changed the title [-][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[/+] on Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions