File size: 2,878 Bytes
21480cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
-- Supabase pgvector 설정 SQL
-- 이 SQL을 Supabase Dashboard > SQL Editor에서 실행하세요

-- 1. pgvector 확장 활성화
CREATE EXTENSION IF NOT EXISTS vector;

-- 2. documents 테이블 생성
CREATE TABLE IF NOT EXISTS documents (
    id SERIAL PRIMARY KEY,
    content TEXT NOT NULL,
    metadata JSONB,
    embedding vector(1536),  -- OpenAI text-embedding-3-small 차원
    source_file VARCHAR(255),
    chunk_index INTEGER,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- 3. 벡터 유사도 검색을 위한 인덱스 생성
CREATE INDEX IF NOT EXISTS documents_embedding_idx
ON documents
USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 100);

-- 4. 전문 검색을 위한 인덱스 생성 (기본 영어 설정)
CREATE INDEX IF NOT EXISTS documents_content_idx
ON documents
USING gin(to_tsvector('english', content));

-- 5. 소스 파일별 검색 인덱스
CREATE INDEX IF NOT EXISTS documents_source_file_idx
ON documents (source_file);

-- 6. 벡터 유사도 검색 함수 생성
CREATE OR REPLACE FUNCTION search_similar_documents(
    query_embedding vector(1536),
    match_threshold float DEFAULT 0.5,
    match_count int DEFAULT 10
)
RETURNS TABLE (
    id int,
    content text,
    metadata jsonb,
    source_file varchar(255),
    similarity float
) AS $$
BEGIN
    RETURN QUERY
    SELECT
        d.id,
        d.content,
        d.metadata,
        d.source_file,
        1 - (d.embedding <=> query_embedding) as similarity
    FROM documents d
    WHERE 1 - (d.embedding <=> query_embedding) > match_threshold
    ORDER BY similarity DESC
    LIMIT match_count;
END;
$$ LANGUAGE plpgsql;

-- 7. 자동 타임스탬프 업데이트 함수
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = NOW();
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- 8. documents 테이블에 트리거 추가
CREATE TRIGGER update_documents_updated_at
    BEFORE UPDATE ON documents
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column();

-- 9. RLS (Row Level Security) 설정 (선택사항)
ALTER TABLE documents ENABLE ROW LEVEL SECURITY;

-- 10. 읽기 권한 정책 (인증된 사용자에게 허용)
CREATE POLICY "Enable read access for all authenticated users" ON documents
    FOR SELECT USING (auth.role() = 'authenticated');

-- 11. 쓰기 권한 정책 (서비스 롤에게 허용)
CREATE POLICY "Enable write access for service role" ON documents
    FOR ALL USING (auth.role() = 'service_role')
    WITH CHECK (auth.role() = 'service_role');

-- 12. 초기 데이터 확인용 쿼리
SELECT 'Setup completed successfully!' as status;

-- 13. 테이블 정보 확인
SELECT
    column_name,
    data_type,
    is_nullable
FROM information_schema.columns
WHERE table_name = 'documents'
ORDER BY ordinal_position;