Skip to main content
The 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

Examples

Basic Partition Generation

Output:

Custom Schema

Write to File

List Format

Get just the partition names (useful for scripts):
Output:

Docker

Workflow

1. Create Parent Table

First, define your parent table with PARTITION BY HASH in your schema files:

2. Generate Partitions

3. Include in Schema

The generated partition file is automatically discovered when you run pgtofu 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}:

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
Example warning:

Modulus Recommendations

Choosing a modulus larger than 1024 will display a warning. Very high partition counts can negatively impact query planning and DDL operations.

Generated SQL Format

The generated SQL follows PostgreSQL’s declarative partition syntax:
This syntax is fully compatible with pgtofu’s parser and will be correctly detected in subsequent diff and generate operations.

Use Cases

High-Volume Transactional Tables

Multi-Tenant Applications

Analytics Tables

Troubleshooting

Ensure the partition file is in the --desired directory path and has a .sql extension.
The parent table must be defined with PARTITION BY HASH (column) in your schema files before pgtofu can validate partitions.
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