chore(npm): add the npm remote repository entity count (S17 repository statistics on remote repositories plan: 2/8)
Summary
- Problem: the repository statistics route answers
404for a remote repository. The monolith's remote sidebar needs package counts from it (gitlab#632276). Before the npm handler can serve remote figures, it needs a count query over the npm remote cache. - Impact: nobody sees a change yet. No code calls the new query until Step 6.
- Change: adds
NpmPackageStore.AggregateNpmRemoteRepositoryEntityCounts. It is oneSELECTwith oneCOUNT(*)subquery each for the packages, versions and files under onenpm_remote_repositoriesrow. It uses the same visibility as the remote lists: each level checks its ownsoft_deleted_at, because an eviction marks only the row it names. Cached metadata documents (npm_remote_metadata_files) never count. The result is a newNpmRemoteRepositoryEntityCountstype with no dist-tag field. One query name joins thenpm_remote_packagesgroup. - Downsides: the MR is 588 reviewable lines of Go (587 added, 1 removed): 125 source and 462 test. That is over 500. The plan keeps the query and its tests in one MR, because splitting would only move the tests away from the code.
duplreports both ends of a twin pair, so the hosted twins innpm_packages.goandnpm_packages_test.goeach get a one-linedupldirective. Nothing in this MR reaches the handler (Step 4 and Step 6), the API text, or the on-call note indocs/dev/database-query-patterns.md(Step 8).
This MR also records its own row in the plan's Status table.
Checklist
- Ran it: n/a for a client, since no route calls the query yet. I ran the integration suite on PostgreSQL 16 and the plans below on PostgreSQL 17.
- Interfaces and data: no API, configuration, schema or storage format changes. The new method and type are internal to the service.
- Failure: a database error comes back wrapped, with no ids in the text, so the caller logs its own scope and answers
500. A nil context or a zero UUID returns a sentinel error before any SQL runs. - Rollout: no schema change, and nothing calls the query, so a mixed-version deploy cannot reach it.
- Hot path: none today. Once Step 6 wires it, the statistics route makes one round trip, pruned to one partition per table. As with the hosted count, a large repository makes the child legs read the whole partition. Repository statistics live counts cost the partition, not the repository records why that cost is accepted.
- Database: one new query, no migration. Every leg uses an index that exists today. The evidence is below.
- Security: no new input, route or permission. Every partitioned table in the query is bound to the caller's
namespace_id, and a unit test checks that. - ADRs: ADR-007 governs the remote cache tables: they are partitioned by
HASH(namespace_id), and the query bindsnamespace_idon each one. ADR-004 does not cap packages per repository, and the spec accepts that the statistics read is rare rather than bounded.scripts/adr-freshness.shreports the mirror one upstream commit behind: the Go modules amendment to ADR-004, ADR-007, ADR-009 and ADR-010. It adds the Go format and rewords some text. None of its changes touch the tables this query reads. - E2E: none. No route changes. The plan's Step 5 changes the Maven statistics row, and npm has no statistics row.
- Simplicity: I rejected reusing
NpmRepositoryEntityCounts, because itsDistTagsCountwould read0, which means "no dist-tags". I rejected one builder shared with the hosted query: the two use different jet table types, so a shared builder would need every column passed in as an argument. I also rejected joiningblob_storage_blobsas the file list does.fk_npm_remote_files_ns_id_blob_sha256_blobsmeans that join drops no row, and no namespace literal prunes a table partitioned by digest.
Testing
npm_packages_remote_statistics_test.go: the three input checks, and the SQL text of each arm. It checks the aliases, thesoft_deleted_atfilters, the repository scope, the join namespace equalities, thatnpm_remote_metadata_filesandblob_storage_blobsare absent,assertBindsNamespaceOnfor every table, and the bound arguments by position.npm_packages_remote_statistics_integration_test.go: 10 subtests. The counts equal a walk of the remote package, version and file lists. They cover an evicted row at each level, metadata documents, a second remote repository and a hosted repository in the same namespace, another namespace, a remote repository id shared across two namespaces, and an id with no rows.- I broke the query and the guards 16 ways in a scratch copy: each of the six
soft_deleted_atfilters, each of the three join namespace equalities, each arm's repository scope, an alias swap, and each of the three guards. Every change turned the suite red. The version-to-file join's namespace equality is caught by the SQL-text test only, as on hosted. - No query-plan test, per
docs/dev/go-testing.md. - The
hosted repositorysubtest is from the plan. No one-line change to the query can make it fail, because the query reads no hosted table.
Database Review Evidence
Collected by hand at 71da107475, with the /db-review-prep query-mode method.
Queries
Note
Plans are from EXPLAIN (ANALYZE, BUFFERS) against an ephemeral PostgreSQL 17.11 container (matching GL_PG_CURR_VERSION from .gitlab-ci-other-versions.yml), loaded from structure.sql, with seed data rolled back after the runs. Each shape ran three times, and the table shows the slowest execution. Numbers reflect moderate cardinality and do not capture production-scale effects. See Database review evidence for the methodology.
One seed serves both shapes: one namespace with two npm remote repositories. Repository a holds 1,000 packages with 5 versions each and 1 file per version. Repository b holds 5 packages with 2 versions each and 1 file per version. Repository a also has 10 evicted packages, 10 evicted versions under live packages, and 10 evicted files under live versions, so every soft_deleted_at filter has rows to drop.
| Method | Plan node | Index | Rows (plan / actual) | Cost | Time | Buffers (hit / read) | Partitions |
|---|---|---|---|---|---|---|---|
datastore.AggregateNpmRemoteRepositoryEntityCounts (repository a, the whole partition) |
Result over three Aggregates (Seq Scan, Hash Join) | n/a | 1 / 1 | 639.04 | 7.714ms | 252 / 0 | 1/64, 1/64, 1/64 |
datastore.AggregateNpmRemoteRepositoryEntityCounts (repository b, beside a large sibling) |
Result over three Aggregates (Index Scan, Nested Loop) | index_npm_remote_packages_on_ns_id_repo_id, index_npm_remote_versions_on_ns_id_pkg_id_created_at_id, index_npm_remote_files_on_ns_id_ver_id |
1 / 1 | 206.70 | 0.133ms | 69 / 0 | 1/64, 1/64, 1/64 |
datastore.AggregateNpmRemoteRepositoryEntityCounts, repository a
Summary: Plan matches the intent. Every leg prunes to partition p23 of its table. Because repository a owns almost the whole partition, the planner reads each leg with a Seq Scan and joins by hash, which is the same flip Repository statistics live counts cost the partition, not the repository records for the hosted counts. The counts are right: 990 packages, 4,940 versions and 4,930 files. No anomalies.
Seed shape: namespaces=1, repositories=2, npm_remote_repositories=2, npm_remote_packages=1005, npm_remote_versions=5010, blob_storage_blobs=5010, blob_storage_attachments=5010, npm_remote_files=5010
Rendered SQL:
SELECT (
SELECT COUNT(*)
FROM public.npm_remote_packages
WHERE ((npm_remote_packages.namespace_id = $1::uuid) AND (npm_remote_packages.npm_remote_repository_id = $2::uuid)) AND (npm_remote_packages.soft_deleted_at IS NULL)
) AS "packages_count",
(
SELECT COUNT(*)
FROM public.npm_remote_versions
INNER JOIN public.npm_remote_packages ON ((npm_remote_packages.id = npm_remote_versions.npm_remote_package_id) AND (npm_remote_packages.namespace_id = npm_remote_versions.namespace_id))
WHERE (((npm_remote_versions.namespace_id = $3::uuid) AND (npm_remote_packages.npm_remote_repository_id = $4::uuid)) AND (npm_remote_versions.soft_deleted_at IS NULL)) AND (npm_remote_packages.soft_deleted_at IS NULL)
) AS "versions_count",
(
SELECT COUNT(*)
FROM public.npm_remote_files
INNER JOIN public.npm_remote_versions ON ((npm_remote_versions.id = npm_remote_files.npm_remote_version_id) AND (npm_remote_versions.namespace_id = npm_remote_files.namespace_id))
INNER JOIN public.npm_remote_packages ON ((npm_remote_packages.id = npm_remote_versions.npm_remote_package_id) AND (npm_remote_packages.namespace_id = npm_remote_versions.namespace_id))
WHERE ((((npm_remote_files.namespace_id = $5::uuid) AND (npm_remote_packages.npm_remote_repository_id = $6::uuid)) AND (npm_remote_files.soft_deleted_at IS NULL)) AND (npm_remote_versions.soft_deleted_at IS NULL)) AND (npm_remote_packages.soft_deleted_at IS NULL)
) AS "files_count";Bound args: [01900000-0000-7000-8000-0000000000a1, 01900000-0000-7000-8000-0000000000c1, 01900000-0000-7000-8000-0000000000a1, 01900000-0000-7000-8000-0000000000c1, 01900000-0000-7000-8000-0000000000a1, 01900000-0000-7000-8000-0000000000c1]
Plan (EXPLAIN (ANALYZE, BUFFERS) output, slowest of three runs):
Result (cost=639.03..639.04 rows=1 width=24) (actual time=7.600..7.603 rows=1 loops=1)
Buffers: shared hit=252
InitPlan 1
-> Aggregate (cost=29.55..29.56 rows=1 width=8) (actual time=0.092..0.093 rows=1 loops=1)
Buffers: shared hit=12
-> Seq Scan on npm_remote_packages_p23 npm_remote_packages (cost=0.00..27.07 rows=990 width=0) (actual time=0.006..0.064 rows=990 loops=1)
Filter: ((soft_deleted_at IS NULL) AND (namespace_id = '01900000-0000-7000-8000-0000000000a1'::uuid) AND (npm_remote_repository_id = '01900000-0000-7000-8000-0000000000c1'::uuid))
Rows Removed by Filter: 15
Buffers: shared hit=12
InitPlan 2
-> Aggregate (cost=189.59..189.60 rows=1 width=8) (actual time=1.337..1.339 rows=1 loops=1)
Buffers: shared hit=74
-> Hash Join (cost=39.45..177.26 rows=4935 width=0) (actual time=0.259..1.188 rows=4940 loops=1)
Hash Cond: (npm_remote_versions.npm_remote_package_id = npm_remote_packages_1.id)
Buffers: shared hit=74
-> Seq Scan on npm_remote_versions_p23 npm_remote_versions (cost=0.00..124.62 rows=5000 width=32) (actual time=0.003..0.385 rows=5000 loops=1)
Filter: ((soft_deleted_at IS NULL) AND (namespace_id = '01900000-0000-7000-8000-0000000000a1'::uuid))
Rows Removed by Filter: 10
Buffers: shared hit=62
-> Hash (cost=27.07..27.07 rows=990 width=32) (actual time=0.250..0.250 rows=990 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 70kB
Buffers: shared hit=12
-> Seq Scan on npm_remote_packages_p23 npm_remote_packages_1 (cost=0.00..27.07 rows=990 width=32) (actual time=0.001..0.125 rows=990 loops=1)
Filter: ((soft_deleted_at IS NULL) AND (namespace_id = '01900000-0000-7000-8000-0000000000a1'::uuid) AND (npm_remote_repository_id = '01900000-0000-7000-8000-0000000000c1'::uuid))
Rows Removed by Filter: 15
Buffers: shared hit=12
InitPlan 3
-> Aggregate (cost=419.85..419.86 rows=1 width=8) (actual time=6.166..6.167 rows=1 loops=1)
Buffers: shared hit=166
-> Hash Join (cost=226.57..407.52 rows=4935 width=0) (actual time=2.432..4.009 rows=4930 loops=1)
Hash Cond: (npm_remote_versions_1.npm_remote_package_id = npm_remote_packages_2.id)
Buffers: shared hit=166
-> Hash Join (cost=187.12..354.89 rows=5000 width=32) (actual time=2.208..3.222 rows=4990 loops=1)
Hash Cond: (npm_remote_files.npm_remote_version_id = npm_remote_versions_1.id)
Buffers: shared hit=154
-> Seq Scan on npm_remote_files_p23 npm_remote_files (cost=0.00..154.62 rows=5000 width=32) (actual time=0.007..0.377 rows=5000 loops=1)
Filter: ((soft_deleted_at IS NULL) AND (namespace_id = '01900000-0000-7000-8000-0000000000a1'::uuid))
Rows Removed by Filter: 10
Buffers: shared hit=92
-> Hash (cost=124.62..124.62 rows=5000 width=48) (actual time=2.173..2.173 rows=5000 loops=1)
Buckets: 8192 Batches: 1 Memory Usage: 455kB
Buffers: shared hit=62
-> Seq Scan on npm_remote_versions_p23 npm_remote_versions_1 (cost=0.00..124.62 rows=5000 width=48) (actual time=0.004..1.766 rows=5000 loops=1)
Filter: ((soft_deleted_at IS NULL) AND (namespace_id = '01900000-0000-7000-8000-0000000000a1'::uuid))
Rows Removed by Filter: 10
Buffers: shared hit=62
-> Hash (cost=27.07..27.07 rows=990 width=32) (actual time=0.181..0.181 rows=990 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 70kB
Buffers: shared hit=12
-> Seq Scan on npm_remote_packages_p23 npm_remote_packages_2 (cost=0.00..27.07 rows=990 width=32) (actual time=0.012..0.076 rows=990 loops=1)
Filter: ((soft_deleted_at IS NULL) AND (namespace_id = '01900000-0000-7000-8000-0000000000a1'::uuid) AND (npm_remote_repository_id = '01900000-0000-7000-8000-0000000000c1'::uuid))
Rows Removed by Filter: 15
Buffers: shared hit=12
Planning Time: 0.744 ms
Execution Time: 7.714 msTimings: planning 0.744ms, execution 7.714ms, total 8.458ms.
datastore.AggregateNpmRemoteRepositoryEntityCounts, repository b
Summary: Plan matches the intent. Every leg prunes to partition p23 and probes an existing index: the packages leg seeks index_npm_remote_packages_on_ns_id_repo_id, and the child legs reach their rows through the per-parent indexes. The 5,000-row sibling repository in the same partition is never read. The counts are right: 5 packages, 10 versions and 10 files. No anomalies.
Seed shape: as for repository a; it is the same seed.
Rendered SQL: as for repository a.
Bound args: [01900000-0000-7000-8000-0000000000a1, 01900000-0000-7000-8000-0000000000c2, 01900000-0000-7000-8000-0000000000a1, 01900000-0000-7000-8000-0000000000c2, 01900000-0000-7000-8000-0000000000a1, 01900000-0000-7000-8000-0000000000c2]
Plan (EXPLAIN (ANALYZE, BUFFERS) output, slowest of three runs):
Result (cost=206.69..206.70 rows=1 width=24) (actual time=0.088..0.089 rows=1 loops=1)
Buffers: shared hit=69
InitPlan 1
-> Aggregate (cost=15.39..15.40 rows=1 width=8) (actual time=0.015..0.016 rows=1 loops=1)
Buffers: shared hit=3
-> Index Scan using npm_remote_packages_p23_namespace_id_npm_remote_repository_idx1 on npm_remote_packages_p23 npm_remote_packages (cost=0.28..15.38 rows=5 width=0) (actual time=0.013..0.014 rows=5 loops=1)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-0000000000a1'::uuid) AND (npm_remote_repository_id = '01900000-0000-7000-8000-0000000000c2'::uuid))
Filter: (soft_deleted_at IS NULL)
Buffers: shared hit=3
InitPlan 2
-> Aggregate (cost=89.10..89.11 rows=1 width=8) (actual time=0.025..0.025 rows=1 loops=1)
Buffers: shared hit=18
-> Nested Loop (cost=0.56..89.04 rows=25 width=0) (actual time=0.013..0.025 rows=10 loops=1)
Buffers: shared hit=18
-> Index Scan using npm_remote_packages_p23_namespace_id_npm_remote_repository_idx1 on npm_remote_packages_p23 npm_remote_packages_1 (cost=0.28..15.38 rows=5 width=32) (actual time=0.004..0.004 rows=5 loops=1)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-0000000000a1'::uuid) AND (npm_remote_repository_id = '01900000-0000-7000-8000-0000000000c2'::uuid))
Filter: (soft_deleted_at IS NULL)
Buffers: shared hit=3
-> Index Only Scan using npm_remote_versions_p23_namespace_id_npm_remote_package_id_idx2 on npm_remote_versions_p23 npm_remote_versions (cost=0.28..14.68 rows=5 width=32) (actual time=0.003..0.004 rows=2 loops=5)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-0000000000a1'::uuid) AND (npm_remote_package_id = npm_remote_packages_1.id))
Heap Fetches: 10
Buffers: shared hit=15
InitPlan 3
-> Aggregate (cost=102.17..102.18 rows=1 width=8) (actual time=0.046..0.047 rows=1 loops=1)
Buffers: shared hit=48
-> Nested Loop (cost=0.84..102.11 rows=25 width=0) (actual time=0.016..0.046 rows=10 loops=1)
Buffers: shared hit=48
-> Nested Loop (cost=0.56..89.04 rows=25 width=32) (actual time=0.008..0.014 rows=10 loops=1)
Buffers: shared hit=18
-> Index Scan using npm_remote_packages_p23_namespace_id_npm_remote_repository_idx1 on npm_remote_packages_p23 npm_remote_packages_2 (cost=0.28..15.38 rows=5 width=32) (actual time=0.003..0.004 rows=5 loops=1)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-0000000000a1'::uuid) AND (npm_remote_repository_id = '01900000-0000-7000-8000-0000000000c2'::uuid))
Filter: (soft_deleted_at IS NULL)
Buffers: shared hit=3
-> Index Only Scan using npm_remote_versions_p23_namespace_id_npm_remote_package_id_idx2 on npm_remote_versions_p23 npm_remote_versions_1 (cost=0.28..14.68 rows=5 width=48) (actual time=0.001..0.002 rows=2 loops=5)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-0000000000a1'::uuid) AND (npm_remote_package_id = npm_remote_packages_2.id))
Heap Fetches: 10
Buffers: shared hit=15
-> Index Scan using npm_remote_files_p23_namespace_id_npm_remote_version_id_idx on npm_remote_files_p23 npm_remote_files (cost=0.28..0.51 rows=1 width=32) (actual time=0.003..0.003 rows=1 loops=10)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-0000000000a1'::uuid) AND (npm_remote_version_id = npm_remote_versions_1.id))
Filter: (soft_deleted_at IS NULL)
Buffers: shared hit=30
Planning Time: 0.465 ms
Execution Time: 0.133 msTimings: planning 0.465ms, execution 0.133ms, total 0.598ms.
Closes #1668 (closed)