PostgreSQL pg_partman advanced 的核心責任是把 declarative partitioning 的日常維護自動化。pg_partman 可以協助建立未來 partition、管理 retention、執行 maintenance job,讓 time-based 或 serial-based partition 不再依賴人工 DDL。

本文的判讀錨點是:pg_partman 解決的是 partition lifecycle operation,而非 partition strategy 本身。Partition key、query pattern、retention、index、foreign key 與 migration 仍要先在 Declarative PartitioningPartition Redesign 做對。

Responsibility Boundary

Responsibility boundary 的核心責任是區分 PostgreSQL 原生 partition 和 pg_partman。

層級責任
PostgreSQL declarative partitioningpartition table、constraint、planner pruning
pg_partmanfuture partition premake、retention、maintenance
Scheduler / job runner定期執行 maintenance
DBA / platformmonitoring、backup、DDL review
Applicationquery pattern、partition key 使用

pg_partman 的價值在於減少重複 DDL。它不會替 application 選出正確 partition key,也不會自動修復跨 partition query 設計。

Core Concepts

Core concepts 的核心責任是理解 pg_partman operation vocabulary。

概念意義
Parent tablepartitioned table 的入口
Child table實際存放資料的 partition
Premake預先建立未來 partition
Retention自動 detach / drop 舊 partition
Maintenance建立新 partition、處理 retention 的 job
Templatechild partition 繼承 index / constraint 的模板

Premake 是防止 insert 打到不存在 partition 的保護。若 partition 建立落後於時間,application insert 會失敗或落到 default partition;production 要對 future partition count 設 alert。

Retention 是資料生命週期操作。Drop 舊 partition 速度快,但要先確認 legal retention、backup、analytics dependency 與 downstream CDC。

Setup Pattern

Setup pattern 的核心責任是把 pg_partman 導入流程放進 migration gate。

1CREATE EXTENSION IF NOT EXISTS pg_partman;
2
3CREATE TABLE events (
4  id bigserial,
5  tenant_id uuid NOT NULL,
6  created_at timestamptz NOT NULL,
7  payload jsonb NOT NULL
8) PARTITION BY RANGE (created_at);

實際建立 partman config 要依 pg_partman 版本與 provider 支援文件執行。Managed PostgreSQL 可能限制 extension version、background worker 或 scheduler,因此 setup 前要先確認 provider boundary。

最小 setup evidence:

  1. Extension version。
  2. Parent table DDL。
  3. Partition key 與 interval。
  4. Premake 數量。
  5. Retention policy。
  6. Maintenance job schedule。
  7. Test insert 到 current / future partition。

Maintenance Runbook

Maintenance runbook 的核心責任是讓 partition lifecycle 可觀測。

Signal意義反應
future partition countpremake 是否足夠手動跑 maintenance、修 scheduler
default partition rowsrouting 失敗或 partition 缺漏建 partition、搬資料、修 app timestamp
old partition countretention 是否執行檢查 policy、legal hold、job error
maintenance durationDDL / lock / catalog 壓力調整 schedule、拆 table
index build timechild index 建立成本template / concurrent strategy review

Maintenance job 要有 owner。Cron、pg_cron、background worker、Kubernetes job 或 managed scheduler 都可以;重點是 job failure 會告警,並且有人處理。

Migration and Backfill

Migration and backfill 的核心責任是把既有大表轉成 partman-managed partition。這通常比新表導入更高風險。

PhaseEvidence
Audittable size、query pattern、write rate
New schemaparent table、child partition、index
Backfillbatch size、lag、lock、checksum
Dual writeapp compatibility
Cutoverrename / view / routing switch
Cleanupold table retention、rollback

Backfill 要控制 WAL、replica lag、autovacuum、index bloat 與 lock。大型 table 應先用 shadow table 或 partition redesign playbook,避開 peak traffic 直接重建。

Failure Modes

Failure modes 的核心責任是列出 pg_partman 常見事故。

Failure mode判讀訊號修正方向
未建立未來 partitioninsert 失敗或 default partition 增長補 partition、修 maintenance schedule
retention drop 過早查詢缺歷史資料restore backup、調 policy、legal review
managed provider 不支援extension / worker 限制改 manual partition job 或 provider
index / constraint 漂移child partition schema 不一致template review、schema diff
planner pruning 失效query 未帶 partition keyquery rewrite、index review

pg_partman 事故通常是 lifecycle 事故。Runbook 要先看 maintenance job,再看 partition metadata 與 application query。

下一步路由

pg_partman advanced 完成後,partition 設計讀 Declarative Partitioning;重排策略讀 Partition Redesign;migration gate 讀 Online Schema Change