feat(managementapi): list a remote repository's idle manifests

Summary

  • Problem: a cleanup job that frees a remote container repository's cache has no way to find the manifests nobody pulled for a while. Manifests list only per image, so it has to walk every image first.
  • Impact: API clients get a new read-only endpoint. Nothing changes for existing routes or the UI.
  • Change: adds GET /api/v1/{slug}/repositories/{repository_name}/{format}/manifests?last_downloaded_before=<RFC 3339 time>. It returns one keyset page of the repository's cached manifests, across all its images, that are idle at that time, in an order the spec leaves unspecified (image name today, coldest first after #1752): cached before it and not downloaded since (created_at < cutoff AND (last_downloaded_at IS NULL OR last_downloaded_at < cutoff)). Each item is the usual manifests list item plus image_id and image_name, so a caller can group a page into the existing per-image manifest bulk deletes.
  • Downsides: remote repositories only; a hosted repository answers 400 (see Simplicity). The idle rule is fixed in the service; a knob can come later as an optional parameter.

Checklist

  • Ran it: against a local instance (the run-artifact-registry driver) with curl, on a remote oci repository seeded with five cached manifests across two images: two idle in one image, one idle and one recent in the other, and one pulled a day ago. With the cutoff at now minus 30 days the list returned exactly the three idle manifests, newest first, each with its image_id and image_name (run before the query moved to image-name order; the filters, the walk and the 400s did not change); a limit=1 walk followed rel="next" across three pages that kept last_downloaded_before and ended with no next. A future cutoff, a missing cutoff, include_referrers and the hosted repository answered 400, a missing repository 404. The existing per-image bulk_delete on the first image's two idle digests answered 202, and the list then returned only the other image's idle manifest.
  • Interfaces and data: one new route, specified in api/openapi/v1.yaml as listContainerRepositoryManifests (x-gitlab-lifecycle: experiment). No schema, configuration or storage change, so existing clients and data are untouched.
  • Failure: these answer 400 before any store call: a missing, non-RFC 3339, zero or future last_downloaded_before; sort, order or include_referrers, which this list does not read; and a hosted repository. A pull bumps last_downloaded_at at most once per staleness window (1h by default), so a listed manifest may have been pulled up to one window after any cutoff, and a cutoff less than one window old can list one pulled minutes ago. That is accepted and stated in the spec: retention counts in days, and the cache re-fetches what it drops from the upstream. A virtual or missing repository answers 404. Store errors are a logged 500.
  • Rollout: no schema or configuration change, so mixed versions are fine; the previous release simply has no such route.
  • Hot path: no. It is a management listing, not a pull or push path.
  • Database: adds one query, container_remote_manifests_select_idle_page. It states namespace_id on both joined tables, so each table only reads one of its 64 hash partitions.
    • Why not page by created_at: the obvious query lists the repository's idle manifests ordered by (created_at, id), with sort and order like the per-image manifest list. Manifests carry no repository column, so the only index in that order is the namespace-wide (namespace_id, created_at DESC, id DESC) one. The repository can only be checked through the join to images. A page then also reads every other repository's old manifests in the namespace, and a small repository next to a big cache pays for the big cache.
    • This version: instead, the query starts from the repository's live images (unique_container_remote_images_ns_id_repo_id_name) and reads each image's manifests through the per-image partial index. A page reads only this repository's rows. On the same seed, a 5k-manifest repository next to a 45k-manifest sibling, vs paging by created_at:
      • first page: 190 buffers vs 1,203
      • cursor page: 243 vs 916
      • every manifest recently pulled: 5,582 buffers vs 9,091 (2,600 rows vs 26,327)
    • The trade-off is the order: image name, then created_at, and no sort or order. Cleanup deletes every idle manifest, so the order does not matter to it.
    • Still accepted: manifests pulled since the cutoff are read and then discarded (see the query notes).
    • Target state (#1752): a repository column on manifests, and an index on (namespace_id, repository, GREATEST(created_at, last_downloaded_at), id). A page then reads only the rows it returns, coldest first. The created_at order is not part of it, so sort and order would go then anyway.
    • Paging: the keyset is the immutable (image name, created_at, id). With a fixed past cutoff the matching set only shrinks, so a walk always ends. The next link carries last_downloaded_before unchanged. Evidence below.
  • Security: one new read route, bound like every artifact read: read_artifact on the repository (requirePermission(..., scopeRepository)), checked before the handler runs. TestAuthorizeRoute_EveryBoundRouteEnforcesItsPermission covers it (a denied caller gets the masked 404 and the handler never runs), and the golden bindings table pins the binding. The query is scoped by namespace and the URL's repository; TestContainerRemoteReadsIntegration_IdleManifestsAreRepositoryScoped proves a sibling repository and a same-named repository in another namespace never show up, and fails if the repository predicate is dropped.
  • ADRs: ADR-009 (an image is the OCI repository name, so an image's manifests stay together for the per-image delete). Holds. ADR-009 keeps hosted and remote on the same surface; remote ships first, and hosted follows in #1761 once hosted pulls record last_downloaded_at (#1040). S17 names the route and adds it to its ADR-009 items.
  • E2E: none. The endpoint is experiment, and the handler and integration tests cover its behavior; rows come when it leaves experiment.
  • Simplicity: no hosted support. Hosted pulls do not write container_manifests.last_downloaded_at today, so every hosted manifest past the cutoff would read as idle, even one pulled every minute. Hosted manifests are also the source of truth rather than a re-fetchable cache, so their cleanup needs retention rules (tags, latest N, index children). Adding hosted later turns a 400 into a 200 and breaks no one. No sort or order (both 400); see Database. Also rejected: a caller-supplied knob for never-pulled manifests.

Query options

Option One composite index, at most two sort fields? Cost per page
This MR: start from the repository's images No: three sort fields across two tables Up to the repository's size
Page by created_at on the namespace index No: the repository is only checked through the join Up to the namespace's size
LATERAL top-N per image No Images × limit, on every page
#1752: repository column and a GREATEST(created_at, last_downloaded_at) index Yes: two fields on one index Only the rows returned

Only #1752 meets the query guide, and it needs a migration and a backfill. This MR ships the repository-bounded shape as a stopgap while the endpoint is experiment. #1752 replaces it with no spec change, because the order is unspecified.

Database Review Evidence

Queries

Note

Plans are from EXPLAIN (ANALYZE, BUFFERS) against an ephemeral PostgreSQL 17 container (matching GL_PG_CURR_VERSION from .gitlab-ci-other-versions.yml), with synthesized seed data rolled back per query and the container torn down at the end of the run. Numbers reflect moderate cardinality and do not capture production-scale effects. See Database review evidence for seed sizing, methodology, and the anomalies the skill flags. Expand each row's details for the seed shape, rendered SQL, bound args, and raw plan.

Method Plan node Index Rows (plan / actual) Cost Time Buffers (hit / read) Partitions
datastore.ListIdleContainerRemoteManifests.CursorBackward Limit (Incremental Sort over Nested Loop) unique_container_remote_images_ns_id_repo_id_name (backward), index_container_remote_manifests_on_ns_id_cri_id_created_at_id 101 / 101 357.06 0.182ms 210 / 0 1/64, 1/64
datastore.ListIdleContainerRemoteManifests.CursorForward Limit (Incremental Sort over Nested Loop) unique_container_remote_images_ns_id_repo_id_name, index_container_remote_manifests_on_ns_id_cri_id_created_at_id 101 / 101 293.32 0.175ms 243 / 0 1/64, 1/64
datastore.ListIdleContainerRemoteManifests.FirstPage Limit (Incremental Sort over Nested Loop) unique_container_remote_images_ns_id_repo_id_name, index_container_remote_manifests_on_ns_id_cri_id_created_at_id 101 / 101 184.49 0.171ms 190 / 0 1/64, 1/64
datastore.ListIdleContainerRemoteManifests.FirstPage (nothing in the namespace before the cutoff) Limit (Sort over Hash Join) index_container_remote_manifests_on_ns_id_created_at_id, container_remote_images seq scan (200 rows) 1 / 0 21.95 0.030ms 5 / 0 1/64, 1/64
datastore.ListIdleContainerRemoteManifests.FirstPage (target repository all hot) Limit (Incremental Sort over Nested Loop) unique_container_remote_images_ns_id_repo_id_name, index_container_remote_manifests_on_ns_id_cri_id_created_at_id 101 / 0 402.01 1.896ms 5582 / 0 1/64, 1/64

Query notes:

  • Same data through the previous query (namespace-wide (namespace_id, created_at DESC, id DESC) scan): first page 2,686 rows read and 1,203 buffers, cursor page 1,543 rows and 916 buffers, target-all-hot 26,327 rows and 9,091 buffers. The new path never reads the sibling repository's 45,000 manifests.
  • ListIdleContainerRemoteManifests (target all hot): an empty page reads every old manifest of the target repository that was pulled since the cutoff (about 2,600 rows, 5,582 buffers, 1.9ms), because the last_downloaded_at check runs on the row after the per-image index read. A busy cache is mostly in this state, so this is the common cost of a cleanup run that finds nothing. It grows with the repository, never the namespace: about 75ms at 100k such manifests, about 0.75s at 1M. Accepted for now; #1752 removes it with a repository column and an index on GREATEST(created_at, last_downloaded_at), which makes a page read only the rows it returns.
  • ListIdleContainerRemoteManifests (cursor inside an image): the cursor's (created_at, id) bound runs as a filter, so a page re-reads the start of the image it resumes in. One request still costs at most that image, inside the repository bound above; only a walk through one very large image grows faster than its row count. #1752's single-index cursor removes it. Accepted.
  • ListIdleContainerRemoteManifests (nothing in the namespace before the cutoff): the planner takes the namespace created_at index, which is cheapest there (5 buffers, 0 rows). With old rows present it takes the repository path, as the all-hot row shows; the plan choice rests on statistics, not on the query shape. Accepted.
datastore.ListIdleContainerRemoteManifests.CursorBackward

Summary: Plan matches the intent: a backward index scan over the repository's images from the cursor's name, a nested loop into the per-image partial index bounded by created_at < cutoff, and an incremental sort within each image. Both tables prune to one of 64 partitions. No anomalies.

Seed shape: container_remote_images=200 (100 in the target repository, 100 in a sibling), container_remote_manifests=50000 (5000 target, 45000 sibling) in one namespace, created 2026-08-01 to late September, a third never pulled; 1,578 idle in the target at the cutoff. FK triggers were off for the seed only (session_replication_role = replica): the query reads no other ancestor table.

Rendered SQL:

SELECT container_remote_manifests.namespace_id AS "container_remote_manifests.namespace_id",
     container_remote_manifests.id AS "container_remote_manifests.id",
     container_remote_manifests.container_remote_image_id AS "container_remote_manifests.container_remote_image_id",
     container_remote_manifests.blob_storage_attachment_id AS "container_remote_manifests.blob_storage_attachment_id",
     container_remote_manifests.size AS "container_remote_manifests.size",
     container_remote_manifests.created_at AS "container_remote_manifests.created_at",
     container_remote_manifests.last_downloaded_at AS "container_remote_manifests.last_downloaded_at",
     container_remote_manifests.soft_deleted_at AS "container_remote_manifests.soft_deleted_at",
     container_remote_manifests.media_type AS "container_remote_manifests.media_type",
     container_remote_manifests.digest AS "container_remote_manifests.digest",
     container_remote_manifests.blob_sha256 AS "container_remote_manifests.blob_sha256",
     container_remote_images.name AS "container_remote_images.name"
FROM public.container_remote_images
     INNER JOIN public.container_remote_manifests ON ((container_remote_manifests.namespace_id = container_remote_images.namespace_id) AND (container_remote_manifests.container_remote_image_id = container_remote_images.id))
WHERE (((((((container_remote_manifests.namespace_id = $1::uuid) AND (container_remote_images.namespace_id = $2::uuid)) AND (container_remote_images.container_remote_repository_id = $3::uuid)) AND (container_remote_images.soft_deleted_at IS NULL)) AND (container_remote_manifests.soft_deleted_at IS NULL)) AND ((container_remote_manifests.created_at < $4::timestamp with time zone) AND ((container_remote_manifests.last_downloaded_at IS NULL) OR (container_remote_manifests.last_downloaded_at < $5::timestamp with time zone)))) AND (container_remote_images.name <= $6::text)) AND ((container_remote_images.name < $7::text) OR ((container_remote_manifests.created_at, container_remote_manifests.id) < ($8::timestamp with time zone, $9::uuid)))
ORDER BY container_remote_images.name DESC, container_remote_manifests.created_at DESC, container_remote_manifests.id DESC
LIMIT $10;

Bound args: [01900000-0000-7000-8000-000000000001, 01900000-0000-7000-8000-000000000001, 01900000-0000-7000-8000-0000000000aa, 2026-09-01 00:00:00+00, 2026-09-01 00:00:00+00, img-0050, img-0050, 2026-08-15 17:53:00+00, 9fa09e70-3423-7002-9b91-fb8d1640f729, 101]

Plan (EXPLAIN (ANALYZE, BUFFERS) output):

 Limit  (cost=149.26..357.06 rows=101 width=206) (actual time=0.098..0.182 rows=101 loops=1)
   Buffers: shared hit=210
   ->  Incremental Sort  (cost=149.26..3737.35 rows=1744 width=206) (actual time=0.097..0.176 rows=101 loops=1)
         Sort Key: container_remote_images.name DESC, container_remote_manifests.created_at DESC, container_remote_manifests.id DESC
         Presorted Key: container_remote_images.name
         Full-sort Groups: 3  Sort Method: quicksort  Average Memory: 35kB  Peak Memory: 35kB
         Buffers: shared hit=210
         ->  Nested Loop  (cost=0.69..3661.65 rows=1744 width=206) (actual time=0.026..0.126 rows=113 loops=1)
               Buffers: shared hit=210
               ->  Index Scan Backward using container_remote_images_p36_namespace_id_container_remote_r_idx on container_remote_images_p36 container_remote_images  (cost=0.27..12.89 rows=50 width=41) (actual time=0.015..0.015 rows=8 loops=1)
                     Index Cond: ((namespace_id = '01900000-0000-7000-8000-000000000001'::uuid) AND (container_remote_repository_id = '01900000-0000-7000-8000-0000000000aa'::uuid) AND (name <= 'img-0050'::text))
                     Buffers: shared hit=3
               ->  Index Scan using container_remote_manifests_p_namespace_id_container_remo_idx106 on container_remote_manifests_p36 container_remote_manifests  (cost=0.41..72.70 rows=28 width=197) (actual time=0.005..0.012 rows=14 loops=8)
                     Index Cond: ((namespace_id = '01900000-0000-7000-8000-000000000001'::uuid) AND (container_remote_image_id = container_remote_images.id) AND (created_at < '2026-09-01 00:00:00+00'::timestamp with time zone))
                     Filter: (((last_downloaded_at IS NULL) OR (last_downloaded_at < '2026-09-01 00:00:00+00'::timestamp with time zone)) AND ((container_remote_images.name < 'img-0050'::text) OR (ROW(created_at, id) < ROW('2026-08-15 17:53:00+00'::timestamp with time zone, '9fa09e70-3423-7002-9b91-fb8d1640f729'::uuid))))
                     Rows Removed by Filter: 9
                     Buffers: shared hit=207
 Planning:
   Buffers: shared hit=10
 Planning Time: 0.374 ms
 Execution Time: 0.213 ms

Timings: planning 0.374ms, execution 0.213ms, total 0.587ms.

datastore.ListIdleContainerRemoteManifests.CursorForward

Summary: Plan matches the intent: the images index starts at name >= 'img-0050', the per-image partial index carries the cutoff, and the cursor tie-break is a filter inside the boundary image. Both tables prune to one of 64 partitions. No anomalies.

Seed shape: same seed as the CursorBackward row.

Rendered SQL:

SELECT container_remote_manifests.namespace_id AS "container_remote_manifests.namespace_id",
     container_remote_manifests.id AS "container_remote_manifests.id",
     container_remote_manifests.container_remote_image_id AS "container_remote_manifests.container_remote_image_id",
     container_remote_manifests.blob_storage_attachment_id AS "container_remote_manifests.blob_storage_attachment_id",
     container_remote_manifests.size AS "container_remote_manifests.size",
     container_remote_manifests.created_at AS "container_remote_manifests.created_at",
     container_remote_manifests.last_downloaded_at AS "container_remote_manifests.last_downloaded_at",
     container_remote_manifests.soft_deleted_at AS "container_remote_manifests.soft_deleted_at",
     container_remote_manifests.media_type AS "container_remote_manifests.media_type",
     container_remote_manifests.digest AS "container_remote_manifests.digest",
     container_remote_manifests.blob_sha256 AS "container_remote_manifests.blob_sha256",
     container_remote_images.name AS "container_remote_images.name"
FROM public.container_remote_images
     INNER JOIN public.container_remote_manifests ON ((container_remote_manifests.namespace_id = container_remote_images.namespace_id) AND (container_remote_manifests.container_remote_image_id = container_remote_images.id))
WHERE (((((((container_remote_manifests.namespace_id = $1::uuid) AND (container_remote_images.namespace_id = $2::uuid)) AND (container_remote_images.container_remote_repository_id = $3::uuid)) AND (container_remote_images.soft_deleted_at IS NULL)) AND (container_remote_manifests.soft_deleted_at IS NULL)) AND ((container_remote_manifests.created_at < $4::timestamp with time zone) AND ((container_remote_manifests.last_downloaded_at IS NULL) OR (container_remote_manifests.last_downloaded_at < $5::timestamp with time zone)))) AND (container_remote_images.name >= $6::text)) AND ((container_remote_images.name > $7::text) OR ((container_remote_manifests.created_at, container_remote_manifests.id) > ($8::timestamp with time zone, $9::uuid)))
ORDER BY container_remote_images.name ASC, container_remote_manifests.created_at ASC, container_remote_manifests.id ASC
LIMIT $10;

Bound args: [01900000-0000-7000-8000-000000000001, 01900000-0000-7000-8000-000000000001, 01900000-0000-7000-8000-0000000000aa, 2026-09-01 00:00:00+00, 2026-09-01 00:00:00+00, img-0050, img-0050, 2026-08-15 17:53:00+00, 9fa09e70-3423-7002-9b91-fb8d1640f729, 101]

Plan (EXPLAIN (ANALYZE, BUFFERS) output):

 Limit  (cost=146.22..293.32 rows=101 width=206) (actual time=0.092..0.175 rows=101 loops=1)
   Buffers: shared hit=243
   ->  Incremental Sort  (cost=146.22..3816.51 rows=2520 width=206) (actual time=0.092..0.169 rows=101 loops=1)
         Sort Key: container_remote_images.name, container_remote_manifests.created_at, container_remote_manifests.id
         Presorted Key: container_remote_images.name
         Full-sort Groups: 3  Sort Method: quicksort  Average Memory: 34kB  Peak Memory: 34kB
         Buffers: shared hit=243
         ->  Nested Loop  (cost=0.69..3701.34 rows=2520 width=206) (actual time=0.033..0.134 rows=105 loops=1)
               Buffers: shared hit=243
               ->  Index Scan using container_remote_images_p36_namespace_id_container_remote_r_idx on container_remote_images_p36 container_remote_images  (cost=0.27..16.92 rows=51 width=41) (actual time=0.009..0.012 rows=9 loops=1)
                     Index Cond: ((namespace_id = '01900000-0000-7000-8000-000000000001'::uuid) AND (container_remote_repository_id = '01900000-0000-7000-8000-0000000000aa'::uuid) AND (name >= 'img-0050'::text))
                     Buffers: shared hit=3
               ->  Index Scan using container_remote_manifests_p_namespace_id_container_remo_idx106 on container_remote_manifests_p36 container_remote_manifests  (cost=0.41..71.77 rows=47 width=197) (actual time=0.005..0.012 rows=12 loops=9)
                     Index Cond: ((namespace_id = '01900000-0000-7000-8000-000000000001'::uuid) AND (container_remote_image_id = container_remote_images.id) AND (created_at < '2026-09-01 00:00:00+00'::timestamp with time zone))
                     Filter: (((last_downloaded_at IS NULL) OR (last_downloaded_at < '2026-09-01 00:00:00+00'::timestamp with time zone)) AND ((container_remote_images.name > 'img-0050'::text) OR (ROW(created_at, id) > ROW('2026-08-15 17:53:00+00'::timestamp with time zone, '9fa09e70-3423-7002-9b91-fb8d1640f729'::uuid))))
                     Rows Removed by Filter: 12
                     Buffers: shared hit=240
 Planning:
   Buffers: shared hit=8
 Planning Time: 0.363 ms
 Execution Time: 0.229 ms

Timings: planning 0.363ms, execution 0.229ms, total 0.592ms.

datastore.ListIdleContainerRemoteManifests.FirstPage

Summary: Plan matches the intent: 7 of the repository's images and 161 of their manifests read to fill a page of 101, through the images index and the per-image partial index, with an incremental sort per image. Both tables prune to one of 64 partitions. No anomalies.

Seed shape: same seed as the CursorBackward row.

Rendered SQL:

SELECT container_remote_manifests.namespace_id AS "container_remote_manifests.namespace_id",
     container_remote_manifests.id AS "container_remote_manifests.id",
     container_remote_manifests.container_remote_image_id AS "container_remote_manifests.container_remote_image_id",
     container_remote_manifests.blob_storage_attachment_id AS "container_remote_manifests.blob_storage_attachment_id",
     container_remote_manifests.size AS "container_remote_manifests.size",
     container_remote_manifests.created_at AS "container_remote_manifests.created_at",
     container_remote_manifests.last_downloaded_at AS "container_remote_manifests.last_downloaded_at",
     container_remote_manifests.soft_deleted_at AS "container_remote_manifests.soft_deleted_at",
     container_remote_manifests.media_type AS "container_remote_manifests.media_type",
     container_remote_manifests.digest AS "container_remote_manifests.digest",
     container_remote_manifests.blob_sha256 AS "container_remote_manifests.blob_sha256",
     container_remote_images.name AS "container_remote_images.name"
FROM public.container_remote_images
     INNER JOIN public.container_remote_manifests ON ((container_remote_manifests.namespace_id = container_remote_images.namespace_id) AND (container_remote_manifests.container_remote_image_id = container_remote_images.id))
WHERE (((((container_remote_manifests.namespace_id = $1::uuid) AND (container_remote_images.namespace_id = $2::uuid)) AND (container_remote_images.container_remote_repository_id = $3::uuid)) AND (container_remote_images.soft_deleted_at IS NULL)) AND (container_remote_manifests.soft_deleted_at IS NULL)) AND ((container_remote_manifests.created_at < $4::timestamp with time zone) AND ((container_remote_manifests.last_downloaded_at IS NULL) OR (container_remote_manifests.last_downloaded_at < $5::timestamp with time zone)))
ORDER BY container_remote_images.name ASC, container_remote_manifests.created_at ASC, container_remote_manifests.id ASC
LIMIT $6;

Bound args: [01900000-0000-7000-8000-000000000001, 01900000-0000-7000-8000-000000000001, 01900000-0000-7000-8000-0000000000aa, 2026-09-01 00:00:00+00, 2026-09-01 00:00:00+00, 101]

Plan (EXPLAIN (ANALYZE, BUFFERS) output):

 Limit  (cost=97.96..184.49 rows=101 width=206) (actual time=0.095..0.171 rows=101 loops=1)
   Buffers: shared hit=190
   ->  Incremental Sort  (cost=97.96..4936.27 rows=5648 width=206) (actual time=0.090..0.160 rows=101 loops=1)
         Sort Key: container_remote_images.name, container_remote_manifests.created_at, container_remote_manifests.id
         Presorted Key: container_remote_images.name
         Full-sort Groups: 3  Sort Method: quicksort  Average Memory: 35kB  Peak Memory: 35kB
         Buffers: shared hit=190
         ->  Nested Loop  (cost=0.69..4672.08 rows=5648 width=206) (actual time=0.023..0.118 rows=119 loops=1)
               Buffers: shared hit=190
               ->  Index Scan using container_remote_images_p36_namespace_id_container_remote_r_idx on container_remote_images_p36 container_remote_images  (cost=0.27..18.33 rows=100 width=41) (actual time=0.010..0.011 rows=7 loops=1)
                     Index Cond: ((namespace_id = '01900000-0000-7000-8000-000000000001'::uuid) AND (container_remote_repository_id = '01900000-0000-7000-8000-0000000000aa'::uuid))
                     Buffers: shared hit=3
               ->  Index Scan using container_remote_manifests_p_namespace_id_container_remo_idx106 on container_remote_manifests_p36 container_remote_manifests  (cost=0.41..45.98 rows=56 width=197) (actual time=0.005..0.013 rows=17 loops=7)
                     Index Cond: ((namespace_id = '01900000-0000-7000-8000-000000000001'::uuid) AND (container_remote_image_id = container_remote_images.id) AND (created_at < '2026-09-01 00:00:00+00'::timestamp with time zone))
                     Filter: ((last_downloaded_at IS NULL) OR (last_downloaded_at < '2026-09-01 00:00:00+00'::timestamp with time zone))
                     Rows Removed by Filter: 6
                     Buffers: shared hit=187
 Planning:
   Buffers: shared hit=34
 Planning Time: 0.533 ms
 Execution Time: 0.205 ms

Timings: planning 0.533ms, execution 0.205ms, total 0.738ms.

datastore.ListIdleContainerRemoteManifests.FirstPage (nothing in the namespace before the cutoff)

Summary: With a cutoff before every seeded row, the planner estimates almost nothing below it and takes the namespace created_at index with a hash join, which is the cheapest path here (5 buffers, 0 rows). See Query notes.

Seed shape: same seed as the CursorBackward row.

Rendered SQL: same as the FirstPage row.

Bound args: [01900000-0000-7000-8000-000000000001, 01900000-0000-7000-8000-000000000001, 01900000-0000-7000-8000-0000000000aa, 2026-07-01 00:00:00+00, 2026-07-01 00:00:00+00, 101]

Plan (EXPLAIN (ANALYZE, BUFFERS) output):

 Limit  (cost=21.95..21.95 rows=1 width=206) (actual time=0.030..0.030 rows=0 loops=1)
   Buffers: shared hit=5
   ->  Sort  (cost=21.95..21.95 rows=1 width=206) (actual time=0.029..0.030 rows=0 loops=1)
         Sort Key: container_remote_images.name, container_remote_manifests.created_at, container_remote_manifests.id
         Sort Method: quicksort  Memory: 25kB
         Buffers: shared hit=5
         ->  Hash Join  (cost=15.55..21.94 rows=1 width=206) (actual time=0.024..0.025 rows=0 loops=1)
               Hash Cond: (container_remote_images.id = container_remote_manifests.container_remote_image_id)
               Buffers: shared hit=5
               ->  Seq Scan on container_remote_images_p36 container_remote_images  (cost=0.00..6.00 rows=100 width=41) (actual time=0.009..0.009 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-0000000000aa'::uuid))
                     Buffers: shared hit=2
               ->  Hash  (cost=15.53..15.53 rows=2 width=197) (actual time=0.009..0.010 rows=0 loops=1)
                     Buckets: 1024  Batches: 1  Memory Usage: 8kB
                     Buffers: shared hit=3
                     ->  Index Scan using container_remote_manifests_p36_namespace_id_created_at_id_idx on container_remote_manifests_p36 container_remote_manifests  (cost=0.41..15.53 rows=2 width=197) (actual time=0.009..0.009 rows=0 loops=1)
                           Index Cond: ((namespace_id = '01900000-0000-7000-8000-000000000001'::uuid) AND (created_at < '2026-07-01 00:00:00+00'::timestamp with time zone))
                           Filter: ((soft_deleted_at IS NULL) AND ((last_downloaded_at IS NULL) OR (last_downloaded_at < '2026-07-01 00:00:00+00'::timestamp with time zone)))
                           Buffers: shared hit=3
 Planning:
   Buffers: shared hit=2
 Planning Time: 0.286 ms
 Execution Time: 0.057 ms

Timings: planning 0.286ms, execution 0.057ms, total 0.343ms.

datastore.ListIdleContainerRemoteManifests.FirstPage (target repository all hot)

Summary: Worst case for the new path: every target manifest pulled after the cutoff, so nothing is idle there while the sibling holds thousands of idle rows. The plan walks all 100 target images and filters about 2,600 hot rows, and never touches the sibling. See Query notes.

Seed shape: the CursorBackward row's seed, then every target manifest's last_downloaded_at set to 2026-10-20.

Rendered SQL: same as the FirstPage row.

Bound args: same as the FirstPage row.

Plan (EXPLAIN (ANALYZE, BUFFERS) output):

 Limit  (cost=204.53..402.01 rows=101 width=206) (actual time=1.895..1.896 rows=0 loops=1)
   Buffers: shared hit=5582
   ->  Incremental Sort  (cost=204.53..10258.27 rows=5142 width=206) (actual time=1.895..1.895 rows=0 loops=1)
         Sort Key: container_remote_images.name, container_remote_manifests.created_at, container_remote_manifests.id
         Presorted Key: container_remote_images.name
         Full-sort Groups: 1  Sort Method: quicksort  Average Memory: 25kB  Peak Memory: 25kB
         Buffers: shared hit=5582
         ->  Nested Loop  (cost=0.69..10021.15 rows=5142 width=206) (actual time=1.886..1.886 rows=0 loops=1)
               Buffers: shared hit=5582
               ->  Index Scan using container_remote_images_p36_namespace_id_container_remote_r_idx on container_remote_images_p36 container_remote_images  (cost=0.27..26.39 rows=100 width=41) (actual time=0.009..0.022 rows=100 loops=1)
                     Index Cond: ((namespace_id = '01900000-0000-7000-8000-000000000001'::uuid) AND (container_remote_repository_id = '01900000-0000-7000-8000-0000000000aa'::uuid))
                     Buffers: shared hit=4
               ->  Index Scan using container_remote_manifests_p_namespace_id_container_remo_idx106 on container_remote_manifests_p36 container_remote_manifests  (cost=0.41..99.44 rows=51 width=197) (actual time=0.019..0.019 rows=0 loops=100)
                     Index Cond: ((namespace_id = '01900000-0000-7000-8000-000000000001'::uuid) AND (container_remote_image_id = container_remote_images.id) AND (created_at < '2026-09-01 00:00:00+00'::timestamp with time zone))
                     Filter: ((last_downloaded_at IS NULL) OR (last_downloaded_at < '2026-09-01 00:00:00+00'::timestamp with time zone))
                     Rows Removed by Filter: 26
                     Buffers: shared hit=5578
 Planning:
   Buffers: shared hit=127
 Planning Time: 0.679 ms
 Execution Time: 1.927 ms

Timings: planning 0.679ms, execution 1.927ms, total 2.606ms.

Related to #1750 (closed)

Edited by João Pereira

Merge request reports

Loading
Loading