Draft: Avoid loading the whole thread after saving a reply
After saving a reply, Notes::CreateService#update_discussions loaded every note in the thread just to check whether the first note needs converting. Everything after it reused that memoized load: TodoService#handle_note, the merge request check in when_saved, and, for diff threads, DiffNote#create_diff_file and clear_noteable_diffs_cache through Note#start_of_discussion?. On the 4,500-note thread from INC-15305, this was one of the whole-thread loads behind every reply.
With this change those callers use only the thread's first note, memoized on the note, and todos are resolved with a note_id IN (SELECT ...) subquery instead of an in-memory list of note IDs. It is behind the reply_post_save_loads_first_note flag, which is off by default.
Related to https://gitlab.com/gitlab-org/gitlab/-/work_items/634352 (the TodoService#handle_note item) and https://gitlab.com/gitlab-org/gitlab/-/work_items/634293
Detailed context for AI agents
Background
INC-15305 (S2, 2026-10-16): a project bot replied about 9.7 times per second into one MR discussion (about 4,529 notes) via POST /projects/:id/merge_requests/:noteable_id/discussions/:discussion_id/notes. Each reply cost O(N) in the thread length, so the thread cost O(N^2).
A probe (request spec that subscribes to sql.active_record and logs every notes query without LIMIT, with backtraces) on current master showed 4 whole-thread loads per reply:
Notes::BuildService#find_discussion- !263491 (closed)- and 3.
Note#ensure_confidentiality_discussion_compliance(invalid?and again insave) - !263097 (merged) Notes::CreateService#update_discussionsvianote.discussion- this MR
Load 4 is memoized on the note (strong_memoize_attr :discussion), and every later caller on the reply path reuses it:
TodoService#handle_note(note.discussion, listed in https://gitlab.com/gitlab-org/gitlab/-/work_items/634352)Note#start_of_discussion?, called fromNotes::CreateService#when_saved(merge requests),Notes::BaseService#clear_noteable_diffs_cache(diff notes),Notes::BaseService#increment_usage_counter(wiki pages) and theDiffNote#create_diff_fileafter_commitcallback
Fixing update_discussions alone would just move the load into whichever of those runs first, so they change together under one flag.
Changes
Notes::Discussion (the note concern):
- New
first_note_of_discussion, memoized: the discussion'sfirst_notewhen the full discussion is already loaded, the note itself when it is not part of a discussion (the same fallback as#discussion), otherwisenoteable.notes.with_discussion_ids(discussion_id).order_created_at_id_asc.first || self. start_of_discussion?with the flag on:first_note_of_discussion == self. With the flag off: unchanged,discussion.first_note == self. Both compare with ActiveRecord==, and an unsaved note that starts a discussion finds no rows and falls back to itself, as before.- The memoized first note survives
Note#ensure_confidentiality_discussion_compliance, which clears only:discussion, so the value found during validation is reused after save. A new note never becomes its thread's first note: a reply finds the existing first note, and a note that starts a thread finds no rows and falls back to itself. The one difference from the flag-off path is a reply whosecreated_atsorts before the first note's (clock skew, orfreeze_timedropping microseconds in specs): flag off then treats the reply as the start of the thread, flag on does not. - Named differently from the private
Note#discussion_first_notethat !263097 (merged) adds, so the two cannot clash. Once both are in, that one can call this.
Notes::CreateService#update_discussions: with the flag on, builds the discussion from note.first_note_of_discussion. The discussion class, can_convert_to_discussion? (IndividualNoteDiscussion: can_reply_to_system_note? && noteable.supports_replying_to_individual_notes?) and convert_to_discussion! (first_note.becomes!(DiscussionNote)) depend only on the first note and noteable. After converting, it clears both memoizations. when_saved calls note.start_of_discussion? as on master.
TodoService#handle_note:
- With the flag on, for a persisted note that is part of a discussion, it builds
note.discussion_class.new(relation, inverse_relations: false), whererelationis the thread's notes, unloaded and unordered. Replies copy the first note's type (BuildServicemergesreply_attributes, includingtype, over the params, and the individual note is converted inupdate_discussionsbeforewhen_saved), so the class matches whatDiscussion.buildwould pick. PendingTodosFinder#by_discussioncallstodos.for_note(discussion.notes), which becomestodos.note_id IN (SELECT notes.id ...). The relation has noORDER BY, so Postgres can turn theINinto a join instead of sorting the thread.- The discussion's
first_note(read forprojectand the GlobalID, which are the same for every note in the thread) becomesORDER BY id LIMIT 1onindex_notes_on_noteable_id_noteable_type_and_id. - Individual notes and unsaved notes (for example
TodoService#new_notecalled with a built note, as the Designs spec does) keepnote.discussion.
Queries
Plans from a gitlab-production-main thin clone (postgres.ai, 2026-10-21), with the SQL exactly as Rails sends it and literal values. Each query ran twice and the warm run is quoted. "Typical" is a 5-note thread on a gitlab-org/gitlab merge request. "Incident" is the 4,529-note thread from INC-15305, on a private project, so its IDs are only in the linked plans. The replier in the todo query is the person replying: a team member on the typical thread, the incident's project bot on the incident thread.
-- Note#first_note_of_discussion (once per reply, memoized; same SQL as !263097's validation query)
SELECT "notes".* FROM "notes" WHERE "notes"."noteable_id" = 515490439 AND "notes"."noteable_type" = 'MergeRequest' AND "notes"."discussion_id" = '555af213e9cd9235edc487541e4ff5c74c5400e1' ORDER BY "notes"."created_at" ASC, "notes"."id" ASC LIMIT 1
-- TodoService: Discussion#first_note on the unordered thread relation
SELECT "notes".* FROM "notes" WHERE "notes"."noteable_id" = 515490439 AND "notes"."noteable_type" = 'MergeRequest' AND "notes"."discussion_id" = '555af213e9cd9235edc487541e4ff5c74c5400e1' ORDER BY "notes"."id" ASC LIMIT 1
-- PendingTodosFinder via Todo.batch_update (this SELECT, then UPDATE ... WHERE id IN (it))
SELECT "todos"."id" FROM "todos" LEFT JOIN banned_users ON todos.author_id = banned_users.user_id WHERE "todos"."state" = 'pending' AND "banned_users"."user_id" IS NULL AND "todos"."user_id" = 5918904 AND "todos"."project_id" = 278964 AND "todos"."note_id" IN (SELECT "notes"."id" FROM "notes" WHERE "notes"."noteable_id" = 515490439 AND "notes"."noteable_type" = 'MergeRequest' AND "notes"."discussion_id" = '555af213e9cd9235edc487541e4ff5c74c5400e1')| Query | Typical | Incident |
|---|---|---|
| first note | 0.23 ms, 22 buffers | 17.2 ms, 4,222 buffers |
| todo first note | 0.21 ms, 19 buffers | 17.7 ms, 4,219 buffers |
| pending todos | 0.32 ms, 56 buffers | 0.08 ms, 4 buffers |
master's update_discussions load, the whole-thread load this replaces |
0.24 ms, 22 buffers | 27.2 ms, 4,222 buffers |
- Both first-note queries use a BitmapAnd of
index_notes_on_discussion_idandindex_notes_on_noteable_id_noteable_type_and_id, then a top-N sort. On the incident thread they read the same buffers as master's whole-thread load (about 4,220), because Postgres still visits every row of the thread, but Rails gets 1 row instead of 4,529. - Pending todos on the typical thread: the thread's 5 note IDs drive a nested loop into
index_todos_on_note_idandindex_on_todos_user_project_target_and_state. On the incident thread the bot has no pending todos, so the plan starts fromindex_on_todos_user_project_target_and_stateand never reachesnotes(4 buffers). - Without the
ORDER BY(dropped after review), theINsubquery is flattened into a join instead of sorting the thread first. - An index on
(discussion_id, created_at, id)would make the first-note query a single index probe. It is not part of this MR.
Query count
Old: one whole-thread load per reply, reused after save. New: one memoized first-note query on the note, one first-note query in TodoService, and the todo subqueries, which replace in-memory ID lists. spec/services/draft_notes/publish_service_spec.rb "reduce N+1 queries" (5 replies, limit 116): 107 queries with the flag off, 111 with it on. That path is the exception: DraftNotes::PublishService#set_discussion_resolve_status calls note.discussion after save, which used to reuse the memoized load from update_discussions and now loads the thread itself, so per published reply the flag adds the two first-note queries without removing a thread load. On the REST reply path from INC-15305 it removes one whole-thread load per reply. A first version without memoization measured up to 122 and failed that spec.
Feature flag
reply_post_save_loads_first_note,gitlab_com_derisk,group::code review, milestone 19.6, actorproject(nil for group-level noteables, which switch over only when the flag is fully on).- Off: unchanged behaviour.
- Rollout issue: #634763
Tests
spec/models/concerns/notes/discussion_spec.rb(new), flag on and off:start_of_discussion?for the first note, a reply, an individual note, an unsaved reply and an unsaved new thread. Flag on: no whole-thread load, and nonotesquery when the discussion is already loaded. Flag off:find_discussionis called.spec/services/notes/create_service_spec.rb, "reply to a discussion with several notes" (merge request), flag on and off: a reply does not triggermerge_request_merge_status_updatedand a new discussion does. Flag on: noNote.find_discussioncall comes fromupdate_discussions,when_savedorDiffNote#create_diff_file, for a discussion reply and for a diff thread reply. Flag off: the load is there.- A local break matrix (27 scenarios, not in the MR) ran each reply with the flag off in a rolled-back savepoint, then on, and compared the persisted notes, todos, GraphQL triggers and diff position captures: MR discussion, diff and resolved threads, a confidential thread, individual issue and MR notes (converted), commit notes, a personal snippet, a group-level epic, a design, a quick-action-only reply, a deleted first note, a reply older than its first note, a 60-note thread, and new threads. All match.
- "reply to individual note": a flag-off conversion example; the existing conversion examples run with the flag on, because specs enable flags by default.
spec/services/todo_service_spec.rb, "when the note is a reply in a discussion": a todo on another note of the thread is marked done. Flag on:Note.find_discussionis not called. Flag off: it is.spec/models/concerns/notes/discussion_spec.rb, flag on: a reply saved after validation reuses the first note found while validating, with no furthernotesquery.- Results (local, GDK):
notes/discussion_spec15/0,notes/create_service_spec86/0,todo_service_spec215/0, EEtodo_service_spec32/0 (one example deadlocked with another process on the local test database and passed on rerun),draft_notes/publish_service_spec52/0,notes/update_service_spec55/0,notes/post_process_service_spec5/0,diff_note_spec112/0,note_spec345/0,projects/notes_controller_spec100/0,api/discussions_spec450/0 (41 pending). RuboCop clean.
Out of scope
NotesActions#createwithreturn_discussion=truere-renders the whole thread for the web UI (another https://gitlab.com/gitlab-org/gitlab/-/work_items/634352 item).- Unpaginated single-discussion GETs and the work items API check (also in https://gitlab.com/gitlab-org/gitlab/-/work_items/634352).
DraftNotes::PublishService#set_discussion_resolve_statusloads the whole thread to resolve or unresolve it after each published reply.NewNoteWorkerparticipants walk (https://gitlab.com/gitlab-org/gitlab/-/work_items/634351, !263500).