Files
2nd/10_Wiki/Topics/Coding/DB_Partitioning_Patterns.md
T
2026-05-09 21:08:02 +09:00

4.3 KiB

id, title, category, status, source_trust_level, verification_status, created_at, updated_at, tags, tech_stack, applied_in, aliases
id title category status source_trust_level verification_status created_at updated_at tags tech_stack applied_in aliases
db-partitioning-patterns Partitioning — Range / List / Hash Coding draft B conceptual 2026-05-09 2026-05-09
database
partitioning
postgres
vibe-coding
language applicable_to
SQL / Postgres
Backend
partitioning
range partition
partition pruning
table inheritance

Table Partitioning

거대 테이블 (10M+ rows / 100GB+) 을 작은 파티션으로 분할. 인덱스 작아짐 + 오래된 파티션 통째 drop = 빠른 만료. Postgres 11+ declarative partitioning.

📖 핵심 개념

  • Range: 시간 / 숫자 범위 (events_2026_05).
  • List: 카테고리 / 지역 (users_kr, users_us).
  • Hash: 균일 분산 (write 부하 분산).
  • Pruning: WHERE 조건이 파티션 키와 맞으면 다른 파티션 skip.

💻 코드 패턴

Range partition (시간)

CREATE TABLE events (
  id BIGSERIAL,
  user_id UUID,
  event_type TEXT,
  created_at TIMESTAMPTZ NOT NULL,
  PRIMARY KEY (id, created_at)
) PARTITION BY RANGE (created_at);

CREATE TABLE events_2026_04 PARTITION OF events
  FOR VALUES FROM ('2026-04-01') TO ('2026-05-01');

CREATE TABLE events_2026_05 PARTITION OF events
  FOR VALUES FROM ('2026-05-01') TO ('2026-06-01');

-- default (필수: 안 맞는 row 의 안전망)
CREATE TABLE events_default PARTITION OF events DEFAULT;

Pruning 확인

EXPLAIN SELECT * FROM events WHERE created_at >= '2026-05-01' AND created_at < '2026-05-10';
-- "Append" 안에 events_2026_05 만 보여야. default 도 안 보여야.

자동 파티션 생성 (pg_partman)

SELECT partman.create_parent(
  p_parent_table => 'public.events',
  p_control => 'created_at',
  p_type => 'native',
  p_interval => 'monthly',
  p_premake => 3 -- 3개월 미리 생성
);

오래된 파티션 drop = 빠른 만료

-- 1년 지난 events 삭제 = DELETE 가 아니라 DROP
DROP TABLE events_2025_05;
-- vacuum 부담 X, 즉시.

List partition

CREATE TABLE orders (
  id UUID,
  region TEXT NOT NULL,
  ...
) PARTITION BY LIST (region);

CREATE TABLE orders_kr PARTITION OF orders FOR VALUES IN ('KR');
CREATE TABLE orders_us PARTITION OF orders FOR VALUES IN ('US');
CREATE TABLE orders_other PARTITION OF orders DEFAULT;

Hash partition (write 분산)

CREATE TABLE accounts (...) PARTITION BY HASH (id);

CREATE TABLE accounts_p0 PARTITION OF accounts FOR VALUES WITH (modulus 4, remainder 0);
CREATE TABLE accounts_p1 PARTITION OF accounts FOR VALUES WITH (modulus 4, remainder 1);
CREATE TABLE accounts_p2 PARTITION OF accounts FOR VALUES WITH (modulus 4, remainder 2);
CREATE TABLE accounts_p3 PARTITION OF accounts FOR VALUES WITH (modulus 4, remainder 3);

인덱스 — 자동으로 각 파티션에

CREATE INDEX events_user ON events (user_id);
-- Postgres 11+ : 자동으로 모든 파티션에 적용

Constraint exclusion 확인

SET enable_partition_pruning = on;
EXPLAIN ANALYZE SELECT count(*) FROM events WHERE user_id = $1 AND created_at >= '2026-05-01';

🤔 의사결정 기준

상황 파티션 종류
시계열 (이벤트, 로그, 메트릭) Range (날짜)
다국가 / 다지역 List (region)
단일 큰 테이블 균등 write Hash
TTL / 자동 만료 Range + drop old partitions
멀티테넌트 큰 차이 List (tenant_id)
천만 미만 파티션 X — 인덱스로 충분

안티패턴

  • PK 가 파티션 키 안 포함: 못 함 — PK 에 partition column 포함 (composite PK).
  • Default 파티션 누락: 범위 밖 INSERT 실패.
  • 모든 파티션 동시 query: pruning 안 됨 — WHERE 에 partition column 포함.
  • Cross-partition unique constraint: 안 됨. 앱 레벨에서.
  • 파티션 너무 많음 (1000+): planner 오버헤드. 적당히 (<100).
  • Partition pruning 환경 검사 안 함: 잘못 만들면 아무 효과 X.
  • 수동 파티션 생성 잊음: pg_partman 또는 cron.

🤖 LLM 활용 힌트

  • Postgres 11+ declarative partitioning 우선.
  • Range + 자동 생성 (pg_partman) + drop 으로 만료.
  • WHERE 에 partition column 항상.

🔗 관련 문서