DBAtlas · 데이터베이스 운영 아틀라스

partition pruning 동작 확인(계획 시점 및 실행 시점)

PostgreSQL파티셔닝

명령어

SHOW enable_partition_pruning;

-- Plan-time pruning: only the relevant partition should appear in the plan
EXPLAIN (COSTS OFF)
SELECT count(*) FROM sales WHERE created_at >= DATE '2026-03-01' AND created_at < DATE '2026-04-01';

-- Run-time pruning: generic plan / parameterized query, "Subplans Removed: N"
PREPARE q_sales(date) AS SELECT count(*) FROM sales WHERE created_at = $1;
SET plan_cache_mode = force_generic_plan;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF) EXECUTE q_sales('2026-03-15');
RESET plan_cache_mode;
DEALLOCATE q_sales;

-- Pruning killer: a function on the key column scans every partition
EXPLAIN (COSTS OFF)
SELECT count(*) FROM sales WHERE date_trunc('month', created_at) = DATE '2026-03-01';

하는 일

계획 시점 pruning, generic plan에서의 실행 시점 pruning('Subplans Removed: N'), 파티션 키에 함수를 적용할 때 pruning이 사라지는 경우를 비교한다.

사용 시점

파티션 테이블의 쿼리가 예상보다 느리거나, 계획이 모든 파티션을 스캔하거나, 애플리케이션이 ORM/prepared statement를 사용할 때.

주의

파티션 키에 함수나 cast를 적용하면(date_trunc(created_at), created_at::text) pruning이 비활성화되므로 조건을 키에 대한 범위로 작성한다. generic plan에서는 pruning이 executor 시작 시 일어나며 'Subplans Removed'로만 나타난다. now() 같은 STABLE 표현식은 ANALYZE 없는 EXPLAIN에서도 보인다. enable_partition_pruning = off이면 pruning이 전혀 일어나지 않는다. 파티션이 많으면 Planning Time도 측정한다.

DBAtlas에서 열기 →

관련 항목