Skip to content

perf(controlplane): page referrer references from an index - #3814

Merged
migmartri merged 7 commits into
chainloop-dev:mainfrom
migmartri:miguel/pfm-8116-referrer-references-pagination
Nov 16, 2026
Merged

migmartri merged 7 commits into
chainloop-dev:mainfrom
migmartri:miguel/pfm-8116-referrer-references-pagination

Conversation

@migmartri

@migmartri migmartri commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Summary

Discovery (ReferrerService/DiscoverPrivate, chainloop discover) was slow, and sometimes timed out, for referrers that many attestations share, such as an identical scanner report. To return one page of references, the control plane read and sorted every reference of the root. In production, one such material has 12.7k references: each page touched about 131k buffers, took 155 ms with a warm cache, about 5 s with a cold one, and hit the request deadline in the worst cases.

This change reads a page of references directly in page order, from the references of the caller's organizations only:

  • The references edge of Referrer becomes an Ent edge schema (ReferrerReference) on the existing referrer_references table. The table gets two columns and an index on (referrer_id, organization_id, created_at DESC, referred_by_id DESC):
    • created_at copies the creation time of the referenced referrer, which is the page order.
    • organization_id is the organization of the attestation whose ingestion stored the reference.
  • A page reads one index range per caller organization, plus the range of references with no organization, each in page order, and merges them. Visibility and project filters are still checked per row with a correlated EXISTS, and each range stops when the page is full. So the cost no longer grows with the number of references of the root, or with the share of them stored in other organizations. Order and cursor semantics stay the same.
  • A reference stored again by an attestation in another organization, for example the same attestation pushed to a run in another organization, loses its organization. A reference with no organization is read in every organization, so the column never hides a reference that the caller can see through that attestation. An organization is only ever replaced with NULL.

Behavior change

An attestation used as a material of an attestation in another organization is linked to that organization too. Until now, that organization also saw it through references that the first organization stored, for example under a material the two organizations share. Now it sees it as a root and through the references its own attestation stored, but not through references stored by the first organization.

Migration

  • The two foreign keys are renamed to the names of the edge schema, instead of being dropped and recreated, so the table is not checked again.
  • Both columns are added without a table rewrite.
  • One backfill pass fills in the existing rows outside the schema-change transaction, so it takes only row locks. Nothing recorded which attestation stored a reference, so its organization is taken from the organizations both ends belong to: the one they share, or NULL when they share more than one.
  • The index is built concurrently.

Out of scope

  • The project-membership check for a material root still reads its references through the existing join.

Closes PFM-8116
Related to PFM-7062

AI assistance: this change was written with Claude Code.

Discovery of a referrer that many attestations share, such as an
identical scanner report, read and sorted every reference of the root
to return one page. A production root with 12.7k references touched
~131k buffers per page and timed out when the pages were not cached
(PFM-8116).

The references edge becomes an Ent edge schema with a created_at column
that copies the creation time of the referenced referrer, indexed as
(referrer_id, created_at DESC, referred_by_id DESC). A page is now read
in index order, visibility is checked per row with a correlated EXISTS,
and the scan stops when the page is full. Ordering and cursors keep the
same semantics.

The migration renames the foreign keys instead of recreating them, adds
the column with a non-rewriting default, backfills it outside the
schema-change transaction, and builds the index concurrently.

Refs: PFM-8116, PFM-7062
Assisted-by: Claude Code
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>

Chainloop-Trace-Sessions: 42d5f922-ec91-4438-a233-0546f874e1b4
@chainloop-platform

chainloop-platform Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

PR validation — ✅ 3 passing

Status Policy Material Messages
✅ Passed pr-min-approvals pr-info -
✅ Passed pr-description-required pr-info -
✅ Passed pr-user-story-linked pr-info -

View attestation ↗

AI Session Checks — 🟡 85% · ✅ 0 failing

Avg score Sessions Failing policies Attribution Files Lines Total Duration
🟡 85% 1 ✅ 0 93% AI / 7% Human 9 +768 / -94 7h33m23s

🟡 85% — 93% AI — ✅ All policies passing

Nov 16, 2026 12:31 UTC · 7h33m23s · $72.72 · 1.6k in / 850.7k out · claude-code 2.1.325 (claude-opus-5-5)

View session details ↗

Change Summary

Adds reference-edge creation timestamps and organization ownership, including conservative handling of shared references. Pages discovery results through an ordered index and adds migrations plus focused integration coverage.

AI Session Overall Score

🟡 85% — Indexed discovery is well-targeted and tested; upfront acceptance constraints and deployed confirmation need attention.

AI Session Analysis Breakdown

🟢 94% · scope-discipline

No notes.

🟢 90% · alignment

No notes.

🟢 88% · user-trust-signal

No notes.

🟢 86% · solution-quality

🟢 Stores ordering metadata on reference edges and uses matching indexed limits instead of increasing timeouts. · High Impact

🟡 Per-organization ranges add sequential queries, so caller organization count remains a performance tradeoff. · Low Severity

🟡 79% · verification

🟢 Real-database tests passed for stored columns, cross-organization references, backfill equivalence, and visibility. · High Impact

🟢 A fresh version-15 replay validated populated backfill, foreign keys, index validity, and indexed paging. · High Impact

🟠 The active user never confirms the discovery behavior works after the change. · Medium Severity

💡 After automated checks, confirm the original failing discovery request against the target environment and record its outcome.

🟡 The final broad test command filters successes and prints completion without preserving the test exit status. · Low Severity

🟡 76% · context-and-planning

🟠 Database-version compatibility and acceptance checks were not settled before the migration implementation began. · Medium Severity

💡 Before schema edits, agree on supported database versions, rollback expectations, and concrete acceptance checks.


File Attribution

██████████████████░░ 93% AI / 7% Human

Status Attribution File Lines
created ai app/controlplane/pkg/data/referrer_references_integration_test.go +389 / -0
modified ai app/controlplane/pkg/data/referrer.go +165 / -49
created ai app/controlplane/pkg/data/ent/schema/referrerreference.go +93 / -0
modified ai app/controlplane/pkg/data/ent/migrate/schema.go +47 / -28
modified ai app/controlplane/pkg/data/referrer_visibility_integration_test.go +23 / -14
created ai app/controlplane/pkg/data/ent/migrate/migrations/20261116124543.sql +36 / -0
created ai app/controlplane/pkg/data/ent/migrate/migrations/20261116124542.sql +9 / -0
modified ai app/controlplane/pkg/data/ent/schema/referrer.go +3 / -2
modified ai app/controlplane/pkg/data/ent/migrate/migrations/atlas.sum +3 / -1

Policies (4)

Status Policy Material Messages
✅ Passed ai-config-ai-agents-allowed ai-coding-session-42d5f9 -
✅ Passed ai-config-no-dangerous-commands ai-coding-session-42d5f9 -
✅ Passed ai-config-no-secrets ai-coding-session-42d5f9 -
✅ Passed ai-config-mcp-servers-allowed ai-coding-session-42d5f9 -

Security Checks — ✅ 5 passing

✅ secret-scan

Status Policy Messages
✅ Passed secrets-detection -

✅ sast-scan

Status Policy Messages
✅ Passed owasp-top10-2025 -
✅ Passed sast -
✅ Passed cwe-top25 -
✅ Passed cwe-top26-40-cusp -
Scans not applied (3)
Scan Reason
vulnerability-scan no manifest/lockfile changed
github-actions-scan no workflow files changed
iac-scan no IaC files changed

View attestation ↗


Powered by Chainloop and Chainloop Trace

@cubic-dev-ai cubic-dev-ai Bot 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.

No issues found across 26 files

This PR changes a database schema or migrates data. Ultrareviews find 2.4x more serious bugs than standard reviews. Comment @cubic-dev-ai ultrareview to run one.

View guided diff | Re-trigger cubic

@migmartri
migmartri requested a review from a team November 16, 2026 13:24
…amp default

Assisted-by: Claude Code
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>

Chainloop-Trace-Sessions: 42d5f922-ec91-4438-a233-0546f874e1b4
javirln
javirln previously approved these changes Nov 16, 2026
…ead of skipping it

Drop IF NOT EXISTS from the concurrent index build, so an invalid index left by a failed build stops the migration instead of being kept. Also cover the visibility filter inside the references page query.

Assisted-by: Claude Code
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>

Chainloop-Trace-Sessions: 42d5f922-ec91-4438-a233-0546f874e1b4
jiparis
jiparis previously approved these changes Nov 16, 2026
Store on each referrer reference the organization of the attestation
that stored it, and index it before the page order. A page reads one
index range per caller organization, plus the references with no
organization, so a caller that sees a small share of a shared material
no longer walks past the references of other organizations.

A reference stored again from another organization loses its
organization, so that it is read in every organization. The backfill
takes the organization that both ends of a reference share, or leaves
it NULL when they share more than one.

Refs PFM-7062

Assisted-by: Claude Code
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>

Chainloop-Trace-Sessions: 42d5f922-ec91-4438-a233-0546f874e1b4
…reSQL limit

The generated name was longer than the 63 characters PostgreSQL keeps, so the database stored a truncated name that differs from the one in the migration.

Assisted-by: Claude Code
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>

Chainloop-Trace-Sessions: 42d5f922-ec91-4438-a233-0546f874e1b4
…n the same references

Stop rewriting references that have no organization when they are stored again. Keep the next-page cursor when a referenced referrer is deleted while a page is read. Check project membership on the same references the pages read. Read the references table once less in the backfill, and vacuum it before the index build.

Assisted-by: Claude Code
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>

Chainloop-Trace-Sessions: 42d5f922-ec91-4438-a233-0546f874e1b4
…visibility

Assisted-by: Claude Code
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>

Chainloop-Trace-Sessions: 42d5f922-ec91-4438-a233-0546f874e1b4

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

LGTM

@migmartri
migmartri merged commit b064a45 into chainloop-dev:main Nov 16, 2026
17 checks passed
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.

4 participants