chore(datastore): count a remote container repository's cache (S17 repository statistics on remote repositories plan: 3/8)

Summary

  • Problem: the repository statistics route answers 404 for remote repositories. Before remote docker and oci repositories can get figures, we need a query that counts their cache. The spec also asks us to time the tag count first, because nothing limits how many tags a remote repository caches.
  • Impact: none yet. Nothing calls the new method until Step 7.
  • Change: adds ContainerImageStore.AggregateContainerRemoteRepositoryEntityCounts. One statement counts the live images, the live manifests under them, and the tags that the remote tags list shows. A tag counts only when its image is live and its manifest is live and under that image. So an evicted manifest's tags stop counting at once, before the purger deletes them.
  • Timing: 16.7 ms on 100,000 cached tags, inside the 100 ms budget. So the method keeps the tag count and returns the existing ContainerRepositoryEntityCounts, and Step 7 takes the container shape. The result is recorded in docs/dev/database-query-patterns.md.
  • Downsides: like the hosted counts, the tag count reads the whole container_remote_tags partition, so its cost grows with the partition, not the repository.

Size: 504 added Go lines, 113 source and 391 test. That is over 500 reviewable lines, as the plan forecast. It stays one MR because splitting would only move tests away from their code.

One small difference from the plan: the input checks are in the hosted guard test in container_image_test.go, not in the new test file. The two tables were near copies, so they are one table now.

No merge order. Steps 1 and 2 (!3543 (merged) and !3541 (merged)) also add a line to query_names.go, each in its own group, and that file merges cleanly. The plan's MR table is different: each step fills its own row, and the rows are adjacent lines. So this MR and !3541 (merged) (Step 2) conflict in the plan, and so do this MR and !3539 (merged) (Step 4). Whichever merges second needs a one-line rebase.

Checklist

  • Ran it: n/a, nothing calls the method yet. I ran the statement against PostgreSQL 17.10 for the timing below.
  • Interfaces and data: no API, configuration or schema change. One new query name, container_remote_images_select_aggregate_counts, on the query metrics' name label.
  • Failure: a nil context, a zero namespace or a zero remote repository id is refused before any SQL runs. A query error comes back wrapped, with no ids in the text, because the caller logs them.
  • Rollout: no schema change, so nothing to order.
  • Hot path: not on one yet. Step 7 puts it on the statistics route, a rare detail read.
  • Database: one new query. Evidence below.
  • Security: no route or permission change. Every table is filtered by namespace_id, the tenant boundary. A test gives another namespace's remote repository the same id and checks that its rows do not count.
  • ADRs: ADR-007 (the remote cache tables): the query binds namespace_id on all three tables and needs no new index. ADR-009 and ADR-021 govern the route, which Step 7 changes. The handbook mirror is one commit behind, a Go modules amendment that does not touch these tables.
  • E2E: none. Nothing calls the method yet.
  • Simplicity: the tag count reuses containerRemoteTagChainJoin, the join the remote tags list uses, so the count and the list cannot drift apart.

Database Review Evidence

Statement: aggregateContainerRemoteRepositoryEntityCountsStmt, at 4be5abf670.

Setup:

  • postgres:17.10-alpine with CI's .pg-service-options flags, migrated with goose to head.
  • One namespace, one remote docker repository, one image, 100 cached manifests, and 1,000 tag names on each: 100,000 container_remote_tags rows. Inserted directly with generate_series, the names in arbitrary order (md5), and the manifests interleaved on the heap.
  • ANALYZE on the three tables, then EXPLAIN (ANALYZE, BUFFERS) three times on the prepared statement.
Run Execution Buffers
1 15.878 ms 1,798 hit
2 16.667 ms 1,798 hit
3 12.423 ms 1,798 hit

All three tables prune to one partition (_p36). The tag count is a parallel sequential scan of that partition, hash-joined to the 100 live manifests. The namespace was alone in its partition. A production partition also holds other tenants' rows, which this shape does not measure.

Plan, run 2 (the slowest)
Result  (cost=4409.97..4409.98 rows=1 width=24) (actual time=11.047..16.609 rows=1 loops=1)
  Buffers: shared hit=1798
  InitPlan 1
    ->  Aggregate  (cost=1.02..1.03 rows=1 width=8) (actual time=0.006..0.007 rows=1 loops=1)
          Buffers: shared hit=1
          ->  Seq Scan on container_remote_images_p36 container_remote_images  (cost=0.00..1.01 rows=1 width=0) (actual time=0.004..0.005 rows=1 loops=1)
                Filter: ((soft_deleted_at IS NULL) AND (namespace_id = '01900000-0000-7000-8000-000000000001'::uuid) AND (container_remote_repository_id = '01900000-0000-7000-8000-000000000003'::uuid))
                Buffers: shared hit=1
  InitPlan 2
    ->  Finalize Aggregate  (cost=4402.16..4402.17 rows=1 width=8) (actual time=10.960..16.518 rows=1 loops=1)
          Buffers: shared hit=1793
          ->  Gather  (cost=4402.05..4402.16 rows=1 width=8) (actual time=10.821..16.510 rows=2 loops=1)
                Workers Planned: 1
                Workers Launched: 1
                Buffers: shared hit=1793
                ->  Partial Aggregate  (cost=3402.05..3402.06 rows=1 width=8) (actual time=9.426..9.430 rows=1 loops=2)
                      Buffers: shared hit=1793
                      ->  Parallel Hash Join  (cost=6.45..3254.99 rows=58824 width=0) (actual time=0.219..8.145 rows=50000 loops=2)
                            Hash Cond: ((container_remote_tags.container_remote_manifest_id = container_remote_manifests.id) AND (container_remote_tags.container_remote_image_id = container_remote_manifests.container_remote_image_id))
                            Buffers: shared hit=1793
                            ->  Parallel Seq Scan on container_remote_tags_p36 container_remote_tags  (cost=0.00..2460.29 rows=58824 width=48) (actual time=0.005..3.577 rows=50000 loops=2)
                                  Filter: (namespace_id = '01900000-0000-7000-8000-000000000001'::uuid)
                                  Buffers: shared hit=1725
                            ->  Parallel Hash  (cost=5.57..5.57 rows=59 width=80) (actual time=0.021..0.024 rows=50 loops=2)
                                  Buckets: 1024  Batches: 1  Memory Usage: 40kB
                                  Buffers: shared hit=4
                                  ->  Parallel Hash Join  (cost=1.02..5.57 rows=59 width=80) (actual time=0.009..0.028 rows=100 loops=1)
                                        Hash Cond: (container_remote_manifests.container_remote_image_id = container_remote_images_1.id)
                                        Buffers: shared hit=4
                                        ->  Parallel Seq Scan on container_remote_manifests_p36 container_remote_manifests  (cost=0.00..3.74 rows=59 width=48) (actual time=0.002..0.009 rows=100 loops=1)
                                              Filter: ((soft_deleted_at IS NULL) AND (namespace_id = '01900000-0000-7000-8000-000000000001'::uuid))
                                              Buffers: shared hit=3
                                        ->  Parallel Hash  (cost=1.01..1.01 rows=1 width=32) (actual time=0.003..0.004 rows=1 loops=1)
                                              Buckets: 1024  Batches: 1  Memory Usage: 40kB
                                              Buffers: shared hit=1
                                              ->  Parallel Seq Scan on container_remote_images_p36 container_remote_images_1  (cost=0.00..1.01 rows=1 width=32) (actual time=0.002..0.002 rows=1 loops=1)
                                                    Filter: ((soft_deleted_at IS NULL) AND (namespace_id = '01900000-0000-7000-8000-000000000001'::uuid) AND (container_remote_repository_id = '01900000-0000-7000-8000-000000000003'::uuid))
                                                    Buffers: shared hit=1
  InitPlan 3
    ->  Aggregate  (cost=6.76..6.77 rows=1 width=8) (actual time=0.077..0.078 rows=1 loops=1)
          Buffers: shared hit=4
          ->  Nested Loop  (cost=0.00..6.51 rows=100 width=0) (actual time=0.040..0.070 rows=100 loops=1)
                Join Filter: (container_remote_images_2.id = container_remote_manifests_1.container_remote_image_id)
                Buffers: shared hit=4
                ->  Seq Scan on container_remote_images_p36 container_remote_images_2  (cost=0.00..1.01 rows=1 width=32) (actual time=0.015..0.016 rows=1 loops=1)
                      Filter: ((soft_deleted_at IS NULL) AND (namespace_id = '01900000-0000-7000-8000-000000000001'::uuid) AND (container_remote_repository_id = '01900000-0000-7000-8000-000000000003'::uuid))
                      Buffers: shared hit=1
                ->  Seq Scan on container_remote_manifests_p36 container_remote_manifests_1  (cost=0.00..4.25 rows=100 width=32) (actual time=0.022..0.040 rows=100 loops=1)
                      Filter: ((soft_deleted_at IS NULL) AND (namespace_id = '01900000-0000-7000-8000-000000000001'::uuid))
                      Buffers: shared hit=3
Planning:
  Buffers: shared hit=5
Planning Time: 0.445 ms
Execution Time: 16.667 ms

Closes #1669 (closed)

Edited by Dzmitry (Dima) Meshcharakou

Merge request reports

Loading
Loading