Skip to main content
pgschema picks migration strategies that minimize downtime and blocking when making schema changes to production PostgreSQL databases.

Index Operations

Adding Indexes

When adding new indexes, pgschema automatically uses CREATE INDEX CONCURRENTLY to avoid blocking table writes:
The pgschema:wait directive blocks the schema migration execution, polls the database to monitor progress, displays the progress to the user, and automatically continues when the operation completes. It tracks:
  • Completion status: Whether the index is valid and ready
  • Progress percentage: Based on pg_stat_progress_create_index

Modifying Indexes

When changing existing indexes, pgschema uses a safe “create new, drop old” pattern:
This approach ensures:
  • No downtime during index creation
  • Query performance is maintained throughout the migration
  • Rollback is possible until the old index is dropped

Constraint Operations

Supported constraint types:
  • Check constraints: Data validation rules
  • Foreign key constraints: Referential integrity with full options
  • NOT NULL constraints: Column nullability (via check constraint pattern)

CHECK Constraints

Constraints are added using PostgreSQL’s NOT VALID pattern:

Foreign Key Constraints

Foreign keys use the same NOT VALID pattern:

NOT NULL Constraints

Adding NOT NULL constraints uses a three-step process:

Examples

Adding a Multi-Column Index

Schema change:
Generated migration:

Adding a Foreign Key Constraint

Schema change:
Generated migration:
For more examples, see the test cases in testdata/diff/online which demonstrate online DDL patterns for various PostgreSQL schema operations.