Postgres Declarative Partitioning Design Prompt
Design a declarative partitioning scheme for a large table — pick the strategy and key, plan indexes and constraints, and lay out a safe migration from the existing monolith with automated partition maintenance.
- Target user
- DBAs and engineers whose biggest table has outgrown a single relation
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior PostgreSQL DBA who has partitioned multi-terabyte tables. You choose the partition strategy from the query and retention patterns — not by habit — and you plan the cutover so the live table is never offline. I will paste: - The table to partition: DDL, size, row count, growth rate: [TABLE DEFINITION] - The dominant query patterns (filters, ranges, joins) and retention policy (e.g. drop data older than N months): [QUERY PATTERNS / RETENTION] - Existing indexes, PK, and foreign keys (in and out): [CONSTRAINTS] - Postgres version: [VERSION] Produce a design: 1. **Choose strategy and key** — RANGE (time-series, retention by dropping old partitions), LIST (discrete categories like region/tenant), or HASH (even spread, no natural range). Justify from the query patterns. Note that the partition key must be part of the primary key / unique constraints, and discuss the implication for the existing PK and any FKs. 2. **Design the partition layout** — partition size/granularity (e.g. monthly vs weekly), index strategy per partition (indexes defined on the parent propagate), and how partition pruning will speed the dominant queries. Flag any query that can't prune (filters not on the key) and what that costs. 3. **Plan the migration from the monolith** — the safe path: create the partitioned table, backfill in batches (or attach the existing table as a partition where feasible), dual- write/cutover, and validate row counts — without a long lock on the live table. Give the exact DDL (PARTITION BY, CREATE TABLE ... PARTITION OF, ATTACH/DETACH PARTITION). 4. **Automate maintenance** — how new partitions get created ahead of time and old ones dropped/detached per retention (pg_partman or a scheduled function), so you never write into a missing partition. Output format: (a) chosen strategy + rationale, (b) the CREATE TABLE / partition DDL, (c) a batched migration runbook with row-count validation, (d) a partition-maintenance plan. Guardrails: rehearse the full migration on a snapshot with production volume and measure lock duration. Build partition indexes with CONCURRENTLY where applicable. ATTACH PARTITION takes a brief lock and validates the partition constraint — pre-add a matching CHECK to make attach fast. Never drop old partitions until retention and any compliance hold are confirmed. The output is a reviewed runbook, not an auto-applied change.
Why this prompt works
Partitioning is often reached for as a generic “make the big table faster” lever, but the wrong strategy or key delivers none of the benefit and a lot of new complexity. This prompt forces the choice to follow the query and retention patterns: RANGE earns its keep when you prune by time and drop old data, HASH when you just need even spread, LIST when there’s a natural category. Pinning strategy to evidence is what makes pruning actually fire.
It also confronts the constraints that trip people up — the partition key must live in every unique constraint, foreign keys behave differently across partitions, and a query that doesn’t filter on the key won’t prune. Surfacing these up front avoids designing a scheme that silently scans every partition.
The migration and maintenance sections turn a design into something operable: a batched, lock-aware cutover with row-count validation, plus automated partition creation so writes never hit a missing partition. The guardrails — production-volume rehearsal, fast ATTACH via pre-added CHECK, retention confirmation before dropping — keep the human reviewing a runbook rather than running a one-shot migration blind.
Related prompts
-
Postgres VACUUM, Bloat & Autovacuum Tuning Prompt
Diagnose table and index bloat, decide between VACUUM, autovacuum tuning, and a rebuild, and produce per-table autovacuum settings — so dead tuples and wraparound risk stop quietly degrading your database.
-
Postgres Zero-Downtime Schema Migration Planner Prompt
Turn a risky schema change into a lock-aware, multi-step migration plan that avoids long ACCESS EXCLUSIVE locks — with the exact statements, lock_timeout guards, and a back-out path for each phase.