Skip to content

Commit

Permalink
lock pre-existing rows
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms committed Aug 14, 2024
1 parent ee0a588 commit 37b3e5b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions services/horizon/internal/db2/history/account_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func bulkGetOrCreate(ctx context.Context, q *Q, table string, fields []columnVal
// will not see the effects of the inserted_rows CTE because of the snapshot
// isolation semantics of postgres CTEs (see
// https://www.postgresql.org/docs/12/queries-with.html ).
preExistingRowsSuffix := "ORDER BY id ASC FOR KEY SHARE"
sql := `
WITH rows AS
(SELECT ` + strings.Join(unnestPart, ",") + `),
Expand All @@ -213,11 +214,14 @@ func bulkGetOrCreate(ctx context.Context, q *Q, table string, fields []columnVal
SELECT * FROM rows
ON CONFLICT (` + columns + `) DO NOTHING
RETURNING *
),
pre_existing_rows AS (
SELECT * FROM ` + table + ` WHERE (` + columns + `) IN
(SELECT * FROM rows) ` + preExistingRowsSuffix + `
)
SELECT *, true as inserted FROM inserted_rows
UNION ALL
SELECT *, false as inserted FROM ` + table + ` WHERE (` + columns + `) IN
(SELECT * FROM rows)`
SELECT *, false as inserted FROM pre_existing_rows`

return q.SelectRaw(
ctx,
Expand Down

0 comments on commit 37b3e5b

Please sign in to comment.