Files
satellite-provider/SatelliteProvider.DataAccess/Migrations/015_AddTilesLeafletPathIndex.sql
T
Oleksandr Bezdieniezhnykh 909f69cb3a [AZ-505] Tile inventory endpoint + HTTP/2 + Leaflet covering index
Production code:
- POST /api/satellite/tiles/inventory (XOR body, 5000-cap,
  most-recent-per-location_hash select, present/absent shaping).
- Kestrel HttpProtocols.Http1AndHttp2 on every listener (AC-5).
- Migration 015 creates tiles_leaflet_path covering index over
  (location_hash, captured_at DESC, updated_at DESC, id DESC)
  INCLUDE (file_path, source); drops superseded idx_tiles_location_hash.
- TileRepository.GetByTileCoordinatesAsync rewired to filter by
  location_hash (Index Only Scan via tiles_leaflet_path).
- TileRepository.GetTilesByLocationHashesAsync added with Npgsql-
  direct ANY($1::uuid[]) binding (Dapper IEnumerable expansion is
  incompatible with the array form).
- Uuidv5.LocationHashForTile centralises the UUIDv5(TileNamespace,
  "{z}/{x}/{y}") formula — single source of truth for the cross-repo
  invariant (gps-denied-onboard parity).

Contracts:
- New: contracts/api/tile-inventory.md v1.0.0.
- Bumped: contracts/data-access/tile-storage.md to v2.0.0 (joint
  ownership by AZ-503-foundation + AZ-505: schema + covering index +
  GetByTileCoordinatesAsync rewrite).

Tests:
- TileInventoryTests covers AC-1, AC-2 (DB-level), AC-4, AC-6.
- Http2MultiplexingTests covers AC-5 (20 concurrent multiplexed GETs
  over h2c via SocketsHttpHandler + AppContext Http2Unencrypted switch).
- LeafletPathIndexOnlyTests covers AC-3 (EXPLAIN (ANALYZE, BUFFERS)
  asserts Index Only Scan over tiles_leaflet_path with heap_blocks=0).

Docs:
- architecture.md, system-flows.md, data_model.md, module-layout.md,
  glossary.md, modules/api_program.md, modules/dataaccess_tile_repository.md,
  components/02_data_access/description.md all updated to reference the
  v2.0.0 tile-storage contract + new tile-inventory contract + AC-7.

Reports:
- batch_01_cycle6_report.md, batch_01_cycle6_review.md,
  implementation_completeness_cycle6_report.md (PASS),
  implementation_report_tile_inventory_cycle6.md.

Task spec moved todo/ -> done/.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-12 21:16:37 +03:00

42 lines
2.0 KiB
PL/PgSQL

-- AZ-505: Leaflet covering index on `tiles` keyed by location_hash.
--
-- Forward migration:
-- 1. Create `tiles_leaflet_path` covering index over (location_hash,
-- captured_at DESC, updated_at DESC, id DESC) with INCLUDE (file_path, source).
-- The leading column matches the equality predicate used by the AZ-505
-- Leaflet hot path (`SELECT file_path FROM tiles WHERE location_hash = $1
-- ORDER BY captured_at DESC, updated_at DESC, id DESC LIMIT 1`); the INCLUDE
-- columns make that exact projection an index-only scan once VACUUM ANALYZE
-- has set the visibility map.
-- 2. Drop the lightweight `idx_tiles_location_hash` introduced by migration
-- 014 — it is superseded because equality lookups by `location_hash` use
-- the leading column of the new covering index.
--
-- Back-migration (manual):
-- DROP INDEX IF EXISTS tiles_leaflet_path;
-- CREATE INDEX IF NOT EXISTS idx_tiles_location_hash ON tiles (location_hash);
--
-- INCLUDE columns are intentionally narrow (`file_path, source`). The richer
-- inventory endpoint legitimately requires extra columns that are NOT in the
-- INCLUDE list (`id, captured_at, flight_id, image_type, tile_size_meters,
-- tile_size_pixels, location_hash`); inventory queries therefore trigger a
-- bounded heap fetch, which is acceptable per the AZ-505 NFR-Perf-2 budget
-- (≤ 1000 ms p95 / 2500 tiles). See AZ-505 Risk 1 in the task spec.
--
-- Lock window: this migration runs inside DbUp's per-script transaction, which
-- is incompatible with `CREATE INDEX CONCURRENTLY`. On a populated `tiles`
-- table the `CREATE INDEX` takes an `ACCESS SHARE` + `SHARE` lock on the table
-- for the duration of the build, blocking writes. Schedule deploys to a
-- low-traffic window or pre-build the index out-of-band before running this
-- migration. See AZ-505 Risk 2.
BEGIN;
CREATE INDEX IF NOT EXISTS tiles_leaflet_path
ON tiles (location_hash, captured_at DESC, updated_at DESC, id DESC)
INCLUDE (file_path, source);
DROP INDEX IF EXISTS idx_tiles_location_hash;
COMMIT;