partition command generates SQL statements for hash-partitioned tables. This is useful when you need to create a large number of partition tables following PostgreSQL’s declarative partitioning syntax.
Usage
Flags
| Flag | Short | Description | Default |
|---|---|---|---|
--table | Table name (required) | ||
--schema | Schema name | public | |
--modulus | Number of partitions (MODULUS value) | 16 | |
--output | -o | Output file path (- for stdout) | stdout |
--format | Output format: sql or list | sql | |
--help | -h | Help for partition |
Examples
Basic Partition Generation
Custom Schema
Write to File
List Format
Get just the partition names (useful for scripts):Docker
Workflow
1. Create Parent Table
First, define your parent table withPARTITION BY HASH in your schema files:
2. Generate Partitions
3. Include in Schema
The generated partition file is automatically discovered when you runpgtofu diff or pgtofu generate:
4. Version Control
Commit the partition file to version control. This ensures consistent partition definitions across environments.Partition Naming Convention
Partitions are named using the pattern{tablename}_p{remainder}:
| Table | Modulus | Partition Names |
|---|---|---|
orders | 4 | orders_p0, orders_p1, orders_p2, orders_p3 |
events | 8 | events_p0 through events_p7 |
logs | 16 | logs_p0 through logs_p15 |
Partition Validation
When pgtofu parses schema files, it automatically validates partition completeness:- All REMAINDER values (0 to MODULUS-1) must be present
- Partition count must match the MODULUS
- Warnings are displayed for missing or duplicate partitions
Modulus Recommendations
| Use Case | Recommended Modulus |
|---|---|
| Small tables (under 1M rows) | 4-8 |
| Medium tables (1-100M rows) | 16-32 |
| Large tables (100M+ rows) | 32-64 |
| Very large tables (1B+ rows) | 64-128 |
Generated SQL Format
The generated SQL follows PostgreSQL’s declarative partition syntax:Use Cases
High-Volume Transactional Tables
Multi-Tenant Applications
Analytics Tables
Troubleshooting
Partitions not detected by diff
Partitions not detected by diff
Ensure the partition file is in the
--desired directory path and has a .sql extension.Parent table not found
Parent table not found
The parent table must be defined with
PARTITION BY HASH (column) in your schema files before pgtofu can validate partitions.Too many partitions warning
Too many partitions warning
If you see a warning about high partition counts (>1024), consider whether you really need that many partitions. High counts can impact performance.
See Also
- Partitioning Feature - Complete partitioning documentation
- PostgreSQL Features - All supported PostgreSQL features
generate- Generate migrations including partition changes