diff --git a/services/horizon/internal/db2/history/account_loader.go b/services/horizon/internal/db2/history/account_loader.go index f246eb2291..11acab8a85 100644 --- a/services/horizon/internal/db2/history/account_loader.go +++ b/services/horizon/internal/db2/history/account_loader.go @@ -166,6 +166,7 @@ func bulkGetOrCreate(ctx context.Context, q *Q, table string, fields []columnVal } columns := strings.Join(insertFieldsPart, ",") + preExistingRowsSuffix := "ORDER BY id ASC FOR KEY SHARE" sql := ` WITH rows AS (SELECT ` + strings.Join(unnestPart, ",") + `), @@ -175,11 +176,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 * FROM inserted_rows UNION ALL - SELECT * FROM ` + table + ` WHERE (` + columns + `) IN - (SELECT * FROM rows)` + SELECT * FROM pre_existing_rows` return q.SelectRaw( ctx,