[PostgreSQL/TimescaleDB] Hypertable과 Chunk를 포함한 최소 크기 덤프 방법

2025. 2. 17. 00:47·개발 (Development)/PostgreSQL

PostgreSQL에서 Hypertable과 Chunk를 포함한 최소 크기 덤프 방법

PostgreSQL에서 TimescaleDB의 Hypertable과 Chunk를 모두 포함하면서 덤프 파일 크기를 최소화하는 방법을 소개합니다. 효율적인 덤프를 위해 압축, 불필요한 데이터 제외, 사전 데이터 최적화 등의 전략을 사용합니다.

1. 압축을 최대화한 pg_dump 사용

TimescaleDB의 Hypertable과 Chunk를 포함하면서 덤프 크기를 최소화하려면 pg_dump의 압축 기능을 적극 활용해야 합니다.

pg_dump -U <username> -h <host> -p <port> -d <database> \
  --format=custom \
  --compress=9 \
  --file=backup.dump
  • --format=custom : 가장 효율적인 압축이 가능한 Custom Format을 사용합니다.
  • --compress=9 : 최대 압축 (9 = 최고 압축률) 적용

이렇게 하면 hypertable과 chunk도 함께 백업됩니다.

2. pg_dump 시 불필요한 데이터 제외

덤프 크기를 더 줄이기 위해 불필요한 인덱스, 시퀀스 값, 통계 정보 등을 제외할 수도 있습니다.

pg_dump -U <username> -h <host> -p <port> -d <database> \
  --format=custom \
  --compress=9 \
  --no-owner --no-acl --no-comments \
  --exclude-table="timescaledb_information.*" \
  --exclude-table="timescaledb_experimental.*" \
  --exclude-table="*_compressed" \
  --file=backup.dump
  • --no-owner, --no-acl : 소유권, 권한 정보 제외 → 불필요한 메타데이터 제거
  • --no-comments : 테이블 설명 등 주석 제외
  • --exclude-table="timescaledb_information.*" : TimescaleDB 내부 메타데이터 제외
  • --exclude-table="timescaledb_experimental.*" : 실험적 기능 관련 테이블 제외
  • --exclude-table="*_compressed" : 기존 압축된 Chunk를 백업에서 제외 가능 (나중에 다시 압축 가능)

3. 덤프 전에 데이터 압축 & 정리 (Vacuum + Compression)

TimescaleDB에서 압축을 활성화하고, VACUUM FULL을 수행하면 덤프 크기를 더 줄일 수 있습니다.

-- 모든 hypertable의 오래된 데이터 압축
SELECT compress_chunk(show_chunks('your_hypertable'));

-- 테이블 정리 (공간 최적화)
VACUUM FULL;
  • compress_chunk(show_chunks('your_hypertable')) : 기존 chunk들을 압축하여 덤프 크기 감소
  • VACUUM FULL : 불필요한 공간 정리 (단, 테이블이 잠길 수 있음)

4. 최적의 압축률을 위한 추가 압축(gzip)

pg_dump에서 압축을 했더라도, gzip으로 추가 압축하면 덤프 크기를 더 줄일 수 있습니다.

pg_dump -U <username> -h <host> -p <port> -d <database> \
  --format=custom --compress=9 | gzip -9 > backup.dump.gz
  • gzip -9 : 최대 압축률 적용

이 방식은 데이터 크기에 따라 50~90%까지 압축률을 향상시킬 수 있습니다.

5. 모든 데이터베이스를 백업해야 할 경우

만약 모든 데이터베이스를 백업해야 한다면, pg_dumpall을 사용할 수도 있습니다.

하지만 pg_dumpall은 --compress 옵션이 없으므로, gzip을 추가하는 것이 좋습니다.

pg_dumpall -U <username> -h <host> -p <port> | gzip -9 > all_databases.sql.gz

이 방법은 모든 데이터베이스를 백업할 때만 필요합니다.

정리: 가장 작은 크기로 Hypertable + Chunk 포함 덤프하는 최적의 명령어

pg_dump -U <username> -h <host> -p <port> -d <database> \
  --format=custom --compress=9 \
  --no-owner --no-acl --no-comments \
  --exclude-table="timescaledb_information.*" \
  --exclude-table="timescaledb_experimental.*" \
  | gzip -9 > backup.dump.gz
반응형

'개발 (Development) > PostgreSQL' 카테고리의 다른 글

[PostgreSQL] SQL Error Handling  (0) 2025.02.23
[PostgreSQL] JSON 컬럼에서 Key-Value 추출 및 MyBatis 연동  (0) 2025.02.23
[PostgreSQL] 데이터베이스 용량 조회 방법  (0) 2025.02.17
[PostgreSQL] WAL(Write-Ahead Logging) 로그 관리 가이드  (0) 2025.02.17
[PostgreSQL] 데이터베이스를 물리적으로 다른 서버에 있는 동일 데이터베이스로 옮기는 방법  (0) 2025.02.17
'개발 (Development)/PostgreSQL' 카테고리의 다른 글
  • [PostgreSQL] SQL Error Handling
  • [PostgreSQL] JSON 컬럼에서 Key-Value 추출 및 MyBatis 연동
  • [PostgreSQL] 데이터베이스 용량 조회 방법
  • [PostgreSQL] WAL(Write-Ahead Logging) 로그 관리 가이드
LoopThinker
LoopThinker
모르는 것을 알아가고, 아는 것을 더 깊게 파고드는 공간
  • LoopThinker
    CodeMemoir
    LoopThinker
  • 전체
    오늘
    어제
    • 분류 전체보기 (238) N
      • 개발 (Development) (171) N
        • Algorithm (1)
        • Angular (1)
        • AWS (7)
        • DeepSeek (2)
        • Docker (7)
        • Git (3)
        • Java (36)
        • JavaScript (4)
        • Kafka (5)
        • Kubernetes (4)
        • Linux (7)
        • PostgreSQL (38)
        • Python (34) N
        • React (3)
        • TypeScript (3)
        • Vue.js (5)
        • General (11)
      • 데이터 분석 (Data Analysis) (1)
      • 알고리즘 문제 풀이 (Problem Solving.. (27)
      • 자격증 (Certifications) (24)
        • ADsP (14)
        • 정보처리기사 (4)
        • Linux Master (5)
        • SQLD (1)
      • 기술 동향 (Tech Trends) (12)
      • 기타 (Others) (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    javascript
    pandas
    springboot
    PostgreSQL
    Linux master
    오답노트
    deepseek
    docker
    MyBatis
    java
    백준자바
    AWS
    리눅스 마스터 2급
    백준
    백준온라인저지
    DevOps
    Linux
    JPA
    리눅스 마스터 2급 2차
    자바
    timescaledb
    파이썬
    백준알고리즘
    JSON
    python
    데이터분석
    ADsP
    Kubernetes
    Vue.js
    Kafka
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
LoopThinker
[PostgreSQL/TimescaleDB] Hypertable과 Chunk를 포함한 최소 크기 덤프 방법
상단으로

티스토리툴바