chore(maven): add the Maven remote repository entity count (S17 repository statistics on remote repositories plan: 1/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 Maven handler can serve remote figures, it needs a count query over the Maven remote cache. - Impact: nobody sees a change yet. No code calls the new query until Step 5.
- Change: adds
MavenPackageStore.AggregateMavenRemoteRepositoryEntityCounts. It is oneSELECTwith oneCOUNT(*)subquery each for the packages, versions and files under onemaven_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. A package-levelmaven-metadata.xmlnever counts. It returns the existingMavenRepositoryEntityCounts, because the three fields mean the same thing. One query name joins themaven_remote_packagesgroup. One sentinel joins themaven_packages:block, for a zero remote repository id. - Downsides: the MR is 549 reviewable lines of Go (548 added, 1 removed): 108 source and 440 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 builder inmaven_packages.gogets a one-linedupldirective. Nothing in this MR reaches the handler (Step 4 and Step 5), 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. Step 2 (!3541 (merged)) records the row next to it, so whichever of the two merges second needs a one-line rebase. No other merge order matters: nothing calls either query yet.
Checklist
- Ran it: n/a for a client, since no route calls the query yet. I ran the integration suite and the plans below on PostgreSQL 17.11.
- Interfaces and data: no API, configuration, schema or storage format changes. The new method is 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 amaven_packages: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 5 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 versions leg 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 can use 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, anddocs/dev/database-query-patterns.mdaccepts the cost because the statistics read is rare.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 to lists and tables, and names nomaven_remote_*table. - E2E: none. No route changes. Step 5 changes the Maven statistics row.
- Simplicity: I rejected a new result type, because the three fields mean the same thing as on hosted. 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 joining
blob_storage_blobsas the file list does.fk_maven_remote_files_ns_id_blob_sha256_blobsmeans that join drops no row.
Testing
maven_packages_remote_statistics_test.go: the three input checks, as a table, and the SQL text. It checks the three counts in one statement, thesoft_deleted_atfilter on every level of every subquery,assertBindsNamespaceOnfor every table, the repository scope, the joins, themaven_remote_version_id IS NOT NULLleg, and that no hosted table appears.maven_packages_remote_statistics_integration_test.go: 12 subtests, the hosted suite's 11 plus a hosted repository. The counts equal a walk of the remote package, version and file lists. They cover an evicted row at each level, a package-level metadata file, a second remote repository and a hosted repository in the same namespace, another namespace, a remote repository id shared across two namespaces, a canceled query, and an id with no rows.- I broke the query 11 ways in a scratch copy: each of the three
soft_deleted_atfilters, each of the three namespace binds, the repository scope, theIS NOT NULLleg, the version level of the files count, the package level of the versions count, and the versions-to-packages join's namespace equality. 10 turned the suite red. Deleting each of the three guards turned it red too. The join's namespace equality stayed green, because both sides already bind the namespace. It stays, to match the composite key and the hosted query. The namespace binds on versions and files and theIS NOT NULLleg are caught by the SQL-text test only, because the joins imply them. The binds move its argument count. - No query-plan test, per
docs/dev/go-testing.md.
Database Review Evidence
Collected by hand at a4f5b223db, with the /db-review-prep query-mode method.
Queries
Note
Plans are from EXPLAIN (ANALYZE, BUFFERS) EXECUTE of the statement as rendered, against an ephemeral PostgreSQL 17.11 container (matching GL_PG_CURR_VERSION from .gitlab-ci-other-versions.yml) with CI's .pg-service-options flags, 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.
The seed matches the Maven rungs in Repository statistics live counts cost the partition, not the repository, so the numbers compare. One namespace holds two Maven remote repositories with 10,000 packages between them. Each package has 10 versions, each version has 5 files, and each package has one package-level maven-metadata.xml. So the partition holds 10,000 packages, 100,000 versions and 510,000 files. The target repository holds 100 packages (1%) in one shape and 1,000 packages (10%) in the other. The seed has no evicted rows; the integration suite covers those.
| Method | Plan node | Index | Rows (plan / actual) | Cost | Time | Buffers (hit / read) | Partitions |
|---|---|---|---|---|---|---|---|
datastore.AggregateMavenRemoteRepositoryEntityCounts (1% of the partition) |
Result over three Aggregates (Index Scan, Nested Loop) | index_maven_remote_packages_on_ns_id_repo_id, index_maven_remote_versions_on_ns_id_pkg_id, index_maven_remote_files_on_ns_id_ver_id |
1 / 1 | 5376.82 | 3.17ms | 10613 / 0 | 1/64, 1/64, 1/64 |
datastore.AggregateMavenRemoteRepositoryEntityCounts (10% of the partition) |
Result over three Aggregates (Seq Scan on versions, Hash Join, Nested Loop) | index_maven_remote_packages_on_ns_id_repo_id, index_maven_remote_files_on_ns_id_ver_id |
1 / 1 | 15560.44 | 34.164ms | 82553 / 0 | 1/64, 1/64, 1/64 |
Notes:
- Same cost as hosted. On the same container, with the same seed and literal binds, the hosted
AggregateMavenRepositoryEntityCountsread 10,613 buffers at 1% and 82,754 at 10%. The remote statement read 10,613 and 82,553. The plan shapes match too. The documented hosted rungs are 10,613 and 82,751 buffers. - The
IS NOT NULLleg does not change the plan. Without it, both shapes pick the same indexes and the same plan shape. The version join already drops package-level files. The leg stays because it states the same predicate as the remote file list and the hosted count.
datastore.AggregateMavenRemoteRepositoryEntityCounts, 1% of the partition
Summary: Plan matches the intent. Every leg prunes to partition p57 of its table and probes an existing index: the packages leg seeks (namespace_id, maven_remote_repository_id), and the child legs reach their rows through the per-parent indexes. The other repository's 9,900 packages in the same partition are never read. The counts are right: 100 packages, 1,000 versions and 5,000 files. No anomalies.
Seed shape: namespaces=1, repositories=2, maven_remote_repositories=2, blob_storage_blobs=1, blob_storage_attachments=1, maven_remote_packages=10000, maven_remote_versions=100000, maven_remote_files=510000
Rendered SQL:
SELECT (
SELECT COUNT(*)
FROM public.maven_remote_packages
WHERE ((maven_remote_packages.namespace_id = $1::uuid) AND (maven_remote_packages.maven_remote_repository_id = $2::uuid)) AND (maven_remote_packages.soft_deleted_at IS NULL)
) AS "packages_count",
(
SELECT COUNT(*)
FROM public.maven_remote_versions
INNER JOIN public.maven_remote_packages ON ((maven_remote_packages.id = maven_remote_versions.maven_remote_package_id) AND (maven_remote_packages.namespace_id = maven_remote_versions.namespace_id))
WHERE ((maven_remote_versions.namespace_id = $3::uuid) AND (maven_remote_versions.soft_deleted_at IS NULL)) AND (((maven_remote_packages.namespace_id = $4::uuid) AND (maven_remote_packages.maven_remote_repository_id = $5::uuid)) AND (maven_remote_packages.soft_deleted_at IS NULL))
) AS "versions_count",
(
SELECT COUNT(*)
FROM public.maven_remote_files
INNER JOIN public.maven_remote_versions ON ((maven_remote_versions.id = maven_remote_files.maven_remote_version_id) AND (maven_remote_versions.namespace_id = maven_remote_files.namespace_id))
INNER JOIN public.maven_remote_packages ON ((maven_remote_packages.id = maven_remote_versions.maven_remote_package_id) AND (maven_remote_packages.namespace_id = maven_remote_versions.namespace_id))
WHERE ((((maven_remote_files.namespace_id = $6::uuid) AND (maven_remote_files.soft_deleted_at IS NULL)) AND (maven_remote_files.maven_remote_version_id IS NOT NULL)) AND ((maven_remote_versions.namespace_id = $7::uuid) AND (maven_remote_versions.soft_deleted_at IS NULL))) AND (((maven_remote_packages.namespace_id = $8::uuid) AND (maven_remote_packages.maven_remote_repository_id = $9::uuid)) AND (maven_remote_packages.soft_deleted_at IS NULL))
) AS "files_count";Bound args: [01900000-0000-7000-8000-00000000aaaa, 01900000-0000-7000-8000-00000000bbbb, 01900000-0000-7000-8000-00000000aaaa, 01900000-0000-7000-8000-00000000aaaa, 01900000-0000-7000-8000-00000000bbbb, 01900000-0000-7000-8000-00000000aaaa, 01900000-0000-7000-8000-00000000aaaa, 01900000-0000-7000-8000-00000000aaaa, 01900000-0000-7000-8000-00000000bbbb]
Plan (EXPLAIN (ANALYZE, BUFFERS) output, slowest of three runs):
Result (cost=5376.81..5376.82 rows=1 width=24) (actual time=2.981..3.134 rows=1 loops=1)
Buffers: shared hit=10613
InitPlan 1
-> Aggregate (cost=137.10..137.11 rows=1 width=8) (actual time=0.014..0.014 rows=1 loops=1)
Buffers: shared hit=4
-> Index Scan using maven_remote_packages_p57_namespace_id_maven_remote_reposi_idx1 on maven_remote_packages_p57 maven_remote_packages (cost=0.29..136.85 rows=100 width=0) (actual time=0.005..0.011 rows=100 loops=1)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-00000000aaaa'::uuid) AND (maven_remote_repository_id = '01900000-0000-7000-8000-00000000bbbb'::uuid))
Filter: (soft_deleted_at IS NULL)
Buffers: shared hit=4
InitPlan 2
-> Aggregate (cost=2059.35..2059.36 rows=1 width=8) (actual time=0.286..0.286 rows=1 loops=1)
Buffers: shared hit=1304
-> Nested Loop (cost=0.70..2056.85 rows=1002 width=0) (actual time=0.008..0.263 rows=1000 loops=1)
Buffers: shared hit=1304
-> Index Scan using maven_remote_packages_p57_namespace_id_maven_remote_reposi_idx1 on maven_remote_packages_p57 maven_remote_packages_1 (cost=0.29..136.85 rows=100 width=32) (actual time=0.003..0.009 rows=100 loops=1)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-00000000aaaa'::uuid) AND (maven_remote_repository_id = '01900000-0000-7000-8000-00000000bbbb'::uuid))
Filter: (soft_deleted_at IS NULL)
Buffers: shared hit=4
-> Index Scan using maven_remote_versions_p57_namespace_id_maven_remote_packag_idx3 on maven_remote_versions_p57 maven_remote_versions (cost=0.42..19.10 rows=10 width=32) (actual time=0.001..0.002 rows=10 loops=100)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-00000000aaaa'::uuid) AND (maven_remote_package_id = maven_remote_packages_1.id))
Filter: (soft_deleted_at IS NULL)
Buffers: shared hit=1300
InitPlan 3
-> Finalize Aggregate (cost=3180.33..3180.34 rows=1 width=8) (actual time=2.680..2.832 rows=1 loops=1)
Buffers: shared hit=9305
-> Gather (cost=3180.22..3180.33 rows=1 width=8) (actual time=2.679..2.831 rows=2 loops=1)
Workers Planned: 1
Workers Launched: 1
Buffers: shared hit=9305
-> Partial Aggregate (cost=2180.22..2180.23 rows=1 width=8) (actual time=1.278..1.278 rows=1 loops=2)
Buffers: shared hit=9305
-> Nested Loop (cost=1.12..2173.00 rows=2889 width=0) (actual time=0.010..1.216 rows=2500 loops=2)
Buffers: shared hit=9305
-> Nested Loop (cost=0.70..1269.24 rows=589 width=32) (actual time=0.007..0.149 rows=500 loops=2)
Buffers: shared hit=1305
-> Parallel Index Scan using maven_remote_packages_p57_namespace_id_maven_remote_reposi_idx1 on maven_remote_packages_p57 maven_remote_packages_2 (cost=0.29..136.44 rows=59 width=32) (actual time=0.005..0.009 rows=50 loops=2)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-00000000aaaa'::uuid) AND (maven_remote_repository_id = '01900000-0000-7000-8000-00000000bbbb'::uuid))
Filter: (soft_deleted_at IS NULL)
Buffers: shared hit=5
-> Index Scan using maven_remote_versions_p57_namespace_id_maven_remote_packag_idx3 on maven_remote_versions_p57 maven_remote_versions_1 (cost=0.42..19.10 rows=10 width=48) (actual time=0.001..0.002 rows=10 loops=100)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-00000000aaaa'::uuid) AND (maven_remote_package_id = maven_remote_packages_2.id))
Filter: (soft_deleted_at IS NULL)
Buffers: shared hit=1300
-> Index Scan using maven_remote_files_p57_namespace_id_maven_remote_version_i_idx1 on maven_remote_files_p57 maven_remote_files (cost=0.42..1.48 rows=5 width=32) (actual time=0.001..0.002 rows=5 loops=1000)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-00000000aaaa'::uuid) AND (maven_remote_version_id = maven_remote_versions_1.id) AND (maven_remote_version_id IS NOT NULL))
Filter: (soft_deleted_at IS NULL)
Buffers: shared hit=8000
Planning:
Buffers: shared hit=5
Planning Time: 0.446 ms
Execution Time: 3.170 msTimings: planning 0.446ms, execution 3.17ms, total 3.616ms.
datastore.AggregateMavenRemoteRepositoryEntityCounts, 10% of the partition
Summary: Plan matches the intent. Every leg prunes to partition p57. At this share the planner reads maven_remote_versions whole, once for the versions count and once more as the files leg's probe feed. That is the same flip Repository statistics live counts cost the partition, not the repository records for the hosted Maven count at its 10% rung. The files leg still probes (namespace_id, maven_remote_version_id) per version. The counts are right: 1,000 packages, 10,000 versions and 50,000 files. No anomalies.
Seed shape: as for the 1% shape, with the target repository holding 1,000 of the 10,000 packages.
Rendered SQL: as for the 1% shape.
Bound args: as for the 1% shape.
Plan (EXPLAIN (ANALYZE, BUFFERS) output, slowest of three runs):
Result (cost=15560.43..15560.44 rows=1 width=24) (actual time=32.193..34.093 rows=1 loops=1)
Buffers: shared hit=82553
InitPlan 1
-> Aggregate (cost=182.03..182.04 rows=1 width=8) (actual time=0.104..0.105 rows=1 loops=1)
Buffers: shared hit=16
-> Bitmap Heap Scan on maven_remote_packages_p57 maven_remote_packages (cost=30.54..179.53 rows=1000 width=0) (actual time=0.019..0.081 rows=1000 loops=1)
Recheck Cond: ((namespace_id = '01900000-0000-7000-8000-00000000aaaa'::uuid) AND (maven_remote_repository_id = '01900000-0000-7000-8000-00000000bbbb'::uuid))
Filter: (soft_deleted_at IS NULL)
Heap Blocks: exact=14
Buffers: shared hit=16
-> Bitmap Index Scan on maven_remote_packages_p57_namespace_id_maven_remote_reposi_idx1 (cost=0.00..30.29 rows=1000 width=0) (actual time=0.014..0.014 rows=1000 loops=1)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-00000000aaaa'::uuid) AND (maven_remote_repository_id = '01900000-0000-7000-8000-00000000bbbb'::uuid))
Buffers: shared hit=2
InitPlan 2
-> Aggregate (cost=2964.67..2964.68 rows=1 width=8) (actual time=10.089..10.091 rows=1 loops=1)
Buffers: shared hit=1251
-> Hash Join (cost=192.03..2939.65 rows=10011 width=0) (actual time=0.123..9.821 rows=10000 loops=1)
Hash Cond: (maven_remote_versions.maven_remote_package_id = maven_remote_packages_1.id)
Buffers: shared hit=1251
-> Seq Scan on maven_remote_versions_p57 maven_remote_versions (cost=0.00..2485.00 rows=100000 width=32) (actual time=0.003..5.425 rows=100000 loops=1)
Filter: ((soft_deleted_at IS NULL) AND (namespace_id = '01900000-0000-7000-8000-00000000aaaa'::uuid))
Buffers: shared hit=1235
-> Hash (cost=179.53..179.53 rows=1000 width=32) (actual time=0.117..0.117 rows=1000 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 71kB
Buffers: shared hit=16
-> Bitmap Heap Scan on maven_remote_packages_p57 maven_remote_packages_1 (cost=30.54..179.53 rows=1000 width=32) (actual time=0.013..0.062 rows=1000 loops=1)
Recheck Cond: ((namespace_id = '01900000-0000-7000-8000-00000000aaaa'::uuid) AND (maven_remote_repository_id = '01900000-0000-7000-8000-00000000bbbb'::uuid))
Filter: (soft_deleted_at IS NULL)
Heap Blocks: exact=14
Buffers: shared hit=16
-> Bitmap Index Scan on maven_remote_packages_p57_namespace_id_maven_remote_reposi_idx1 (cost=0.00..30.29 rows=1000 width=0) (actual time=0.010..0.011 rows=1000 loops=1)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-00000000aaaa'::uuid) AND (maven_remote_repository_id = '01900000-0000-7000-8000-00000000bbbb'::uuid))
Buffers: shared hit=2
InitPlan 3
-> Finalize Aggregate (cost=12413.69..12413.70 rows=1 width=8) (actual time=21.997..23.893 rows=1 loops=1)
Buffers: shared hit=81286
-> Gather (cost=12413.58..12413.69 rows=1 width=8) (actual time=21.840..23.891 rows=2 loops=1)
Workers Planned: 1
Workers Launched: 1
Buffers: shared hit=81286
-> Partial Aggregate (cost=11413.58..11413.59 rows=1 width=8) (actual time=20.349..20.350 rows=1 loops=2)
Buffers: shared hit=81286
-> Nested Loop (cost=181.13..11341.56 rows=28806 width=0) (actual time=0.249..19.697 rows=25000 loops=2)
Buffers: shared hit=81286
-> Parallel Hash Join (cost=180.71..2305.48 rows=5889 width=32) (actual time=0.233..5.950 rows=5000 loops=2)
Hash Cond: (maven_remote_versions_1.maven_remote_package_id = maven_remote_packages_2.id)
Buffers: shared hit=1285
-> Parallel Seq Scan on maven_remote_versions_p57 maven_remote_versions_1 (cost=0.00..1970.29 rows=58824 width=48) (actual time=0.005..3.157 rows=50000 loops=2)
Filter: ((soft_deleted_at IS NULL) AND (namespace_id = '01900000-0000-7000-8000-00000000aaaa'::uuid))
Buffers: shared hit=1235
-> Parallel Hash (cost=173.36..173.36 rows=588 width=32) (actual time=0.086..0.087 rows=500 loops=2)
Buckets: 1024 Batches: 1 Memory Usage: 72kB
Buffers: shared hit=16
-> Parallel Bitmap Heap Scan on maven_remote_packages_p57 maven_remote_packages_2 (cost=30.54..173.36 rows=588 width=32) (actual time=0.037..0.107 rows=1000 loops=1)
Recheck Cond: ((namespace_id = '01900000-0000-7000-8000-00000000aaaa'::uuid) AND (maven_remote_repository_id = '01900000-0000-7000-8000-00000000bbbb'::uuid))
Filter: (soft_deleted_at IS NULL)
Heap Blocks: exact=14
Buffers: shared hit=16
-> Bitmap Index Scan on maven_remote_packages_p57_namespace_id_maven_remote_reposi_idx1 (cost=0.00..30.29 rows=1000 width=0) (actual time=0.028..0.028 rows=1000 loops=1)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-00000000aaaa'::uuid) AND (maven_remote_repository_id = '01900000-0000-7000-8000-00000000bbbb'::uuid))
Buffers: shared hit=2
-> Index Scan using maven_remote_files_p57_namespace_id_maven_remote_version_i_idx1 on maven_remote_files_p57 maven_remote_files (cost=0.42..1.48 rows=5 width=32) (actual time=0.002..0.002 rows=5 loops=10000)
Index Cond: ((namespace_id = '01900000-0000-7000-8000-00000000aaaa'::uuid) AND (maven_remote_version_id = maven_remote_versions_1.id) AND (maven_remote_version_id IS NOT NULL))
Filter: (soft_deleted_at IS NULL)
Buffers: shared hit=80001
Planning:
Buffers: shared hit=5
Planning Time: 0.516 ms
Execution Time: 34.164 msTimings: planning 0.516ms, execution 34.164ms, total 34.68ms.
Closes #1667 (closed)