Skip to content
CloudOps
Newsletter
All prompts
AI for Postgres Difficulty: Advanced ClaudeChatGPTCursor

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

Newsletter

Free: the DevOps AI Incident-Triage Cheat Sheet

Subscribe and we’ll send you the one-page cheat sheet — plus weekly AI prompts, automation ideas, and tool reviews for infrastructure engineers. One email a week. No spam, unsubscribe anytime.

  • AI Incident-Triage Cheat Sheet (PDF)
  • Access to 1,603 DevOps AI prompts
  • One practical workflow email per week