Don't let replica-based cache updates race real writes

What does this MR do and why?

Don't let replica-based cache updates race real writes.

... and split mention calculation from store so we can minimise the time spent with a row lock.

See #628134 (comment 3945257084) for background on the new query, and that WI in general for what this work is driving towards.

Sample query

UPDATE "notes" SET "note_html" = '<p data-sourcepos="1:1-1:21" dir="auto">Note mentioning <a href="/root" title="Administrator" class="gfm gfm-project_member js-user-link" data-user="1" data-original="@root" data-container="body" data-placement="top" data-reference-type="user">@root</a></p>', "cached_markdown_version" = 2228224, "namespace_id" = NULL WHERE "notes"."id" = 33 AND (COALESCE(cached_markdown_version, 0) <= 2228224) AND "notes"."note" = 'Note mentioning @root'

Explain: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/62312/commands/167454

 Update on public.notes  (cost=0.58..3.60 rows=0 width=0) (actual time=8.906..8.906 rows=0 loops=1)
   Buffers: shared hit=3 read=5
   I/O Timings: read=8.839 write=0.000
   ->  Index Scan using notes_pkey on public.notes  (cost=0.58..3.60 rows=1 width=50) (actual time=8.904..8.904 rows=0 loops=1)
         Index Cond: (notes.id = 33)
         Filter: ((COALESCE(notes.cached_markdown_version, 0) <= 2228224) AND (notes.note = 'Note mentioning @root'::text))
         Rows Removed by Filter: 1
         Buffers: shared hit=3 read=5
         I/O Timings: read=8.839 write=0.000
Settings: random_page_cost = '1.5', seq_page_cost = '4', effective_cache_size = '472585MB', work_mem = '230MB', jit = 'off'
Statistics:
Time: 26.653 ms
  - planning: 17.627 ms
  - execution: 9.026 ms
    - I/O read: 8.839 ms
    - I/O write: 0.000 ms

Shared buffers:
  - hits: 3 (~24.00 KiB) from the buffer pool
  - reads: 5 (~40.00 KiB) from the OS file cache, including disk I/O
  - dirtied: 0
  - writes: 0

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Asherah Connor

Merge request reports

Loading
Loading