Skip to content

Commit

Permalink
🐛 fix: fix embeddings multi-insert when there is issues with async ta…
Browse files Browse the repository at this point in the history
…sk (lobehub#3530)
  • Loading branch information
arvinxx committed Aug 21, 2024
1 parent 5568e3f commit e2cfff7
Show file tree
Hide file tree
Showing 5 changed files with 3,044 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/database/server/migrations/0007_fix_embedding_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- step 1: create a temporary table to store the rows we want to keep
CREATE TEMP TABLE embeddings_temp AS
SELECT DISTINCT ON (chunk_id) *
FROM embeddings
ORDER BY chunk_id, random();

-- step 2: delete all rows from the original table
DELETE FROM embeddings;

-- step 3: insert the rows we want to keep back into the original table
INSERT INTO embeddings
SELECT * FROM embeddings_temp;

-- step 4: drop the temporary table
DROP TABLE embeddings_temp;

-- step 5: now it's safe to add the unique constraint
ALTER TABLE "embeddings" ADD CONSTRAINT "embeddings_chunk_id_unique" UNIQUE("chunk_id");
Loading

0 comments on commit e2cfff7

Please sign in to comment.