| name | managing-database-partitions |
| description | Process use when you need to work with database partitioning.
This skill provides table partitioning strategies with comprehensive guidance and automation.
Trigger with phrases like "partition tables", "implement partitioning",
or "optimize large tables".
|
| allowed-tools | Read, Write, Edit, Grep, Glob, Bash(psql:*), Bash(mysql:*), Bash(mongosh:*) |
| version | 1.25.0 |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
| license | MIT |
| tags | ["database","database-partitions"] |
| compatibility | Designed for Claude Code, also compatible with Codex and OpenClaw |
Database Partition Manager
Overview
Implement and manage table partitioning for PostgreSQL and MySQL to improve query performance and simplify data lifecycle management on large tables. This skill covers range partitioning (by date or ID), list partitioning (by category or region), hash partitioning (for even distribution), and composite partitioning.
Prerequisites
- PostgreSQL 10+ (declarative partitioning) or MySQL 5.7+ (native partitioning)
- Database admin credentials with CREATE TABLE and ALTER TABLE permissions
psql or mysql CLI for executing partition DDL
- Table size metrics:
SELECT pg_size_pretty(pg_total_relation_size('table_name')) or SELECT data_length FROM information_schema.TABLES
- Query patterns on the target table (especially WHERE clause columns used for filtering)
- Maintenance window availability for initial partition migration on existing tables
Instructions
-
Identify partitioning candidates by finding tables that exceed 10GB or 100M rows, have time-based query patterns, or require periodic data purging. Query pg_stat_user_tables to find tables with high sequential scan counts on large row sets.
-
Select the partition key based on the most common query filter column. For time-series data, use the timestamp column. For multi-tenant data, use tenant_id. The partition key must appear in most WHERE clauses to enable partition pruning.
-
Choose the partitioning strategy:
- Range: Best for time-series data. Create monthly or daily partitions. Queries filtering by date range scan only relevant partitions.
- List: Best for categorical data. Create one partition per category, region, or status value.
- Hash: Best for even distribution when no natural range exists. Distribute rows across N partitions using hash of the partition key.
- Composite: Combine range + list for multi-dimensional partitioning (e.g., range by date, then list by region).
-
For PostgreSQL, create the partitioned parent table: CREATE TABLE orders (id bigint, created_at timestamptz, ...) PARTITION BY RANGE (created_at). Then create child partitions: CREATE TABLE orders_2024_01 PARTITION OF orders FOR VALUES FROM ('2024-01-01') TO ('2024-02-01').
-
For MySQL, define partitions inline: ALTER TABLE orders PARTITION BY RANGE (YEAR(created_at) * 100 + MONTH(created_at)) (PARTITION p202401 VALUES LESS THAN (202402), ...).