C.W.K.
Stream
Lesson 05 of 05 · published

마이그레이션, 재임베딩, zero downtime

~20 min · pgvector, operations, migrations

Level 0Scout
0 XP0/41 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

pgvector ops 가 다른 두 시나리오

  1. Schema 마이그레이션 — vector 컬럼 있는 테이블에. Postgres 가 fine — 같은 마이그레이션 도구 (Alembic, dbmate, sqlx) 작동. 단지 populated 테이블에 vector(N) NOT NULL 컬럼 추가는 backfill plan 필요한 거 알아둬.
  2. 재임베딩 — 모델 바꿀 때. 차원 변경이라 ALTER COLUMN 안 됨 — 새 컬럼 또는 테이블 추가.

zero-downtime 재임베딩 패턴

  1. ALTER TABLE chunks ADD COLUMN embedding_v2 vector(1024) NULL;
  2. 배치로 backfill: text 읽고, 임베딩, embedding_v2 IS NULL 인 곳 update.
  3. v2 인덱스 빌드.
  4. 코드에서 read 를 embedding_v2 로 전환.
  5. 일주일 뒤 embedding (v1 컬럼) drop.

Backup

pg_dump + PITR 평소대로 작동. Vector 컬럼이 default 로 text 직렬화 — verbose 함; 매우 큰 테이블은 logical dump 보다 base backup + WAL archiving 선호.

Code

병렬 vector 컬럼 추가·sql
ALTER TABLE chunks ADD COLUMN embedding_v2 vector(1024);

-- 옵션: backfill 끝날 때까지 partial 인덱스
CREATE INDEX chunks_embedding_v2_hnsw
    ON chunks USING hnsw (embedding_v2 vector_cosine_ops)
    WHERE embedding_v2 IS NOT NULL;
Python 에서 batched backfill·python
from itertools import islice

BATCH = 500
with conn.cursor() as cur:
    while True:
        cur.execute(
            'SELECT id, text FROM chunks WHERE embedding_v2 IS NULL LIMIT %s',
            (BATCH,),
        )
        rows = cur.fetchall()
        if not rows:
            break
        ids   = [r[0] for r in rows]
        texts = [r[1] for r in rows]
        new_vecs = new_model.encode(texts, normalize_embeddings=True).tolist()
        cur.executemany(
            'UPDATE chunks SET embedding_v2 = %s WHERE id = %s',
            list(zip(new_vecs, ids)),
        )
        conn.commit()
        print(f'backfilled {len(ids)} rows')

External links

Exercise

10k row 테스트 테이블에 병렬 vector 컬럼 추가, 500 배치로 backfill, v2 인덱스 빌드, 코드에서 read 전환, v1 컬럼 drop. 총 wall-clock 시간 + 모든 에러 트래킹 — 그게 프로덕션 마이그레이션 playbook.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.