Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 93818e0

Browse files
fix: Try workaround for bad index choice when updating execution logs (#64328)
Under high contention, the updating of execution logs query: ``` UPDATE lsif_indexes SET execution_logs = execution_logs || $1::json WHERE id = $2 AND worker_hostname = $3 AND state = $4 RETURNING ARRAY_LENGTH(execution_logs, $5) ``` Was taking multiple seconds due to lock contention on the lsif_indexes_state index. ![image](https://github.com/user-attachments/assets/be58bc97-0995-4909-a032-69084ad995c5) Running `EXPLAIN ANALYZE` on Sourcegraph.com under lower contention uses the primary key index on id, so we don't have an easy way to test the high contention scenario. Try this alternate query form to see if that fixes the issue.
1 parent 4d6e894 commit 93818e0

File tree

1 file changed

+11
-7
lines changed
  • internal/workerutil/dbworker/store

1 file changed

+11
-7
lines changed

internal/workerutil/dbworker/store/store.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -791,14 +791,14 @@ func (s *store[T]) AddExecutionLogEntry(ctx context.Context, id int, entry execu
791791
}})
792792
defer endObservation(1, observation.Args{})
793793

794-
conds := []*sqlf.Query{
795-
s.formatQuery("{id} = %s", id),
796-
}
794+
conds := []*sqlf.Query{sqlf.Sprintf("%s", true)}
797795
conds = append(conds, options.ToSQLConds(s.formatQuery)...)
798796

799797
entryID, ok, err := basestore.ScanFirstInt(s.Query(ctx, s.formatQuery(
800798
addExecutionLogEntryQuery,
801799
quote(s.options.TableName),
800+
id,
801+
quote(s.options.TableName),
802802
entry,
803803
sqlf.Join(conds, "AND"),
804804
)))
@@ -825,11 +825,15 @@ func (s *store[T]) AddExecutionLogEntry(ctx context.Context, id int, entry execu
825825
}
826826

827827
const addExecutionLogEntryQuery = `
828-
UPDATE
829-
%s
828+
WITH candidate AS (
829+
-- Directly using id = blah in WHERE clause would sometimes
830+
-- trigger use of the state index under high contention, so
831+
-- try forcing the use of pkey on id
832+
SELECT id FROM %s WHERE id = %s FOR UPDATE
833+
)
834+
UPDATE %s
830835
SET {execution_logs} = {execution_logs} || %s::json
831-
WHERE
832-
%s
836+
WHERE id IN (SELECT id FROM candidate) AND %s
833837
RETURNING array_length({execution_logs}, 1)
834838
`
835839

0 commit comments

Comments
 (0)