Skip to content

Commit 717754c

Browse files
committed
Fixups for bad queries
1 parent f32d66a commit 717754c

File tree

3 files changed

+53
-32
lines changed

3 files changed

+53
-32
lines changed

sql/2025-05-29_scoped-definition-search.sql

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ BEGIN
9393
FROM causals c
9494
JOIN projects p ON p.id = NEW.project_id
9595
WHERE c.id = NEW.causal_id
96-
;
97-
NOTIFY 'definition_sync';
96+
ON CONFLICT DO NOTHING;
97+
NOTIFY definition_sync;
9898
END IF;
9999
RETURN NEW;
100100
END;
@@ -114,9 +114,11 @@ BEGIN
114114
p.owner_user_id AS codebase_user_id
115115
FROM causals c
116116
JOIN projects p ON p.id = NEW.project_id
117-
WHERE c.id = NEW.squashed_causal_id
118-
;
119-
NOTIFY 'definition_sync';
117+
WHERE c.id = NEW.squashed_causal_id
118+
ON CONFLICT DO NOTHING;
119+
-- Log message to console
120+
RAISE NOTICE 'Added scoped definition search queue entry for release % in project %', NEW.id, NEW.project_id;
121+
NOTIFY definition_sync;
120122
RETURN NEW;
121123
END;
122124
$$ LANGUAGE plpgsql;

src/Share/BackgroundJobs/Search/DefinitionSync.hs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,27 @@ syncRoot ::
106106
AuthZ.AuthZReceipt ->
107107
(Maybe ReleaseId, BranchHashId, UserId) ->
108108
PG.Transaction e [DefnIndexingFailure]
109-
syncRoot authZReceipt (mayReleaseId, rootBranchHashId, codebaseOwner) = fmap (fromMaybe []) . runMaybeT $ do
110-
lift $ do
111-
namesPerspective <- NLOps.namesPerspectiveForRootAndPath rootBranchHashId (NL.PathSegments [])
112-
let nlReceipt = NL.nameLookupReceipt namesPerspective
113-
let codebaseLoc = Codebase.codebaseLocationForProjectRelease codebaseOwner
114-
let codebase = Codebase.codebaseEnv authZReceipt codebaseLoc
115-
errs <- Codebase.codebaseMToTransaction codebase $ do
116-
termsCursor <- lift $ NLOps.projectTermsWithinRoot nlReceipt rootBranchHashId
109+
syncRoot authZReceipt (mayReleaseId, rootBranchHashId, codebaseOwner) = do
110+
-- Only index it if it's not already indexed.
111+
errs <-
112+
(DDQ.isRootIndexed rootBranchHashId) >>= \case
113+
False -> do
114+
DDQ.markRootAsIndexed rootBranchHashId
115+
namesPerspective <- NLOps.namesPerspectiveForRootAndPath rootBranchHashId (NL.PathSegments [])
116+
let nlReceipt = NL.nameLookupReceipt namesPerspective
117+
let codebaseLoc = Codebase.codebaseLocationForProjectRelease codebaseOwner
118+
let codebase = Codebase.codebaseEnv authZReceipt codebaseLoc
119+
Codebase.codebaseMToTransaction codebase $ do
120+
termsCursor <- lift $ NLOps.projectTermsWithinRoot nlReceipt rootBranchHashId
117121

118-
termErrs <- syncTerms namesPerspective rootBranchHashId termsCursor
119-
typesCursor <- lift $ NLOps.projectTypesWithinRoot nlReceipt rootBranchHashId
120-
typeErrs <- syncTypes namesPerspective rootBranchHashId typesCursor
121-
pure (termErrs <> typeErrs)
122-
-- Copy relevant index rows into the global search index as well
123-
mayReleaseId & foldMapM (syncRelease rootBranchHashId)
124-
pure errs
122+
termErrs <- syncTerms namesPerspective rootBranchHashId termsCursor
123+
typesCursor <- lift $ NLOps.projectTypesWithinRoot nlReceipt rootBranchHashId
124+
typeErrs <- syncTypes namesPerspective rootBranchHashId typesCursor
125+
pure (termErrs <> typeErrs)
126+
True -> pure mempty
127+
-- Copy relevant index rows into the global search index as well
128+
for mayReleaseId (syncRelease rootBranchHashId)
129+
pure errs
125130

126131
syncRelease :: BranchHashId -> ReleaseId -> PG.Transaction e ()
127132
syncRelease rootBranchHashId releaseId = fmap (fromMaybe ()) . runMaybeT $ do

src/Share/Postgres/Search/DefinitionSearch/Queries.hs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module Share.Postgres.Search.DefinitionSearch.Queries
99
defNameCompletionSearch,
1010
definitionTokenSearch,
1111
definitionNameSearch,
12+
isRootIndexed,
13+
markRootAsIndexed,
1214
DefnNameSearchFilter (..),
1315
-- Exported for logging/debugging
1416
searchTokensToTsQuery,
@@ -74,16 +76,37 @@ claimUnsynced = do
7476
RETURNING chosen.release_id, chosen.root_namespace_hash_id, chosen.codebase_user_id
7577
|]
7678

79+
isRootIndexed :: BranchHashId -> Transaction e Bool
80+
isRootIndexed rootBranchHashId = do
81+
queryExpect1Col
82+
[sql|
83+
SELECT EXISTS (
84+
SELECT FROM indexed_definition_search_doc_roots
85+
WHERE root_namespace_hash_id = #{rootBranchHashId}
86+
)
87+
|]
88+
89+
markRootAsIndexed :: BranchHashId -> Transaction e ()
90+
markRootAsIndexed rootBranchHashId = do
91+
execute_
92+
[sql|
93+
INSERT INTO indexed_definition_search_doc_roots(root_namespace_hash_id)
94+
VALUES(#{rootBranchHashId})
95+
ON CONFLICT DO NOTHING
96+
|]
97+
-- We don't return anything, so we can use `execute_` here.
98+
pure ()
99+
77100
-- | Save definition documents to be indexed for search.
78101
insertDefinitionDocuments :: [DefinitionDocument Name (Name, ShortHash)] -> Transaction e ()
79102
insertDefinitionDocuments docs = pipelined $ do
80103
let docsTable = docRow <$> docs
81104
execute_ $
82105
[sql|
83-
WITH docs(project_id, release_id, name, token_text, arity, tag, metadata) AS (
106+
WITH docs(root_namespace_hash_id, name, token_text, arity, tag, metadata) AS (
84107
SELECT * FROM ^{toTable docsTable}
85-
) INSERT INTO global_definition_search_docs (project_id, release_id, name, search_tokens, arity, tag, metadata)
86-
SELECT d.project_id, d.release_id, d.name, tsvector(d.token_text::text), d.arity, d.tag::definition_tag, d.metadata
108+
) INSERT INTO scoped_definition_search_docs (root_namespace_hash_id, name, search_tokens, arity, tag, metadata)
109+
SELECT d.root_namespace_hash_id, d.name, tsvector(d.token_text::text), d.arity, d.tag::definition_tag, d.metadata
87110
FROM docs d
88111
ON CONFLICT DO NOTHING
89112
|]
@@ -120,15 +143,6 @@ copySearchDocumentsForRelease rootBranchHashId projectId releaseId = do
120143
FROM scoped_definition_search_docs doc
121144
WHERE doc.root_namespace_hash_id = #{rootBranchHashId}
122145
|]
123-
-- Update the release id for the copied documents.
124-
execute_
125-
[sql|
126-
UPDATE global_definition_search_docs
127-
SET release_id = #{releaseId}
128-
WHERE root_namespace_hash_id = #{rootBranchHashId}
129-
AND project_id = #{projectId}
130-
AND release_id IS NULL
131-
|]
132146

133147
-- | Wipe out any rows for the given project, useful when re-indexing and we only want to
134148
-- have records for the latest release.

0 commit comments

Comments
 (0)