PostgreSQL "Cannot attach partition" error occurs when ALTER TABLE ... ATTACH PARTITION fails due to schema mismatches, constraint violations, or overlapping partition bounds. Ensure column compatibility, add matching CHECK constraints before attaching, and verify partition bounds do not overlap.
When using ALTER TABLE ... ATTACH PARTITION to add a table as a partition to a partitioned table, PostgreSQL validates that the table structure and data conform to strict requirements. The "Cannot attach partition" error indicates that one or more validation checks failed, preventing the attachment. This is a critical operation because improper partition attachment could violate data integrity of the partitioned table. PostgreSQL performs several validations during ATTACH PARTITION: it checks that columns exactly match the parent table, verifies that all existing data in the table satisfies the partition bounds, ensures partition bounds do not overlap with existing partitions, and validates constraint compatibility. If any of these checks fail, the operation is rejected.
Ensure the table being attached has identical columns to the parent partitioned table, including column names, data types, and constraints.
-- Check parent table structure
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'parent_table'
ORDER BY ordinal_position;
-- Check partition table structure
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'partition_table'
ORDER BY ordinal_position;If differences exist, alter the partition table to match or recreate it with the correct schema.
Create a CHECK constraint on the partition table that matches the partition bounds. This avoids a full table scan during attachment and ensures data validity.
ALTER TABLE measurement_y2008m02
ADD CONSTRAINT y2008m02_check
CHECK (logdate >= DATE '2008-02-01' AND logdate < DATE '2008-03-01');For RANGE partitions, use >= for lower bound and < for upper bound. For LIST partitions, specify the exact values. This constraint tells PostgreSQL the data is pre-validated, avoiding the expensive scan.
Check that the bounds you are specifying for the new partition do not overlap with any existing partitions.
-- For RANGE partitions, view existing bounds
SELECT relname, pg_get_partkeydef(oid) as partition_key
FROM pg_class
WHERE relispartition AND relname LIKE 'parent_table_%';
-- Manually verify your new partition bounds fall in a gap
-- Example: if 2008-01 covers Jan, Feb covers Feb, then Mar must start at 2008-03-01For LIST partitions, ensure the values do not duplicate existing partition values. Overlap causes the attachment to fail.
If the CHECK constraint reveals data outside partition bounds, you have two options:
Option 1: Remove out-of-range data
DELETE FROM measurement_y2008m02
WHERE logdate < DATE '2008-02-01' OR logdate >= DATE '2008-03-01';Option 2: Use a DEFAULT partition to catch misplaced rows
CREATE TABLE parent_table_default
PARTITION OF parent_table DEFAULT;After successful ATTACH PARTITION, you can drop the now-redundant CHECK constraint:
ALTER TABLE measurement_y2008m02 DROP CONSTRAINT y2008m02_check;Once constraints are in place and data is verified, execute ATTACH PARTITION:
ALTER TABLE measurement ATTACH PARTITION measurement_y2008m02
FOR VALUES FROM ('2008-02-01') TO ('2008-03-01');If the CHECK constraint was present from earlier steps, this operation completes quickly. Without it, PostgreSQL must scan the entire table, which can be slow on large tables and holds an ACCESS EXCLUSIVE lock.
Lock Behavior: ATTACH PARTITION requires an ACCESS EXCLUSIVE lock on the partitioned table and the partition being attached. During validation, if a DEFAULT partition exists and must be scanned, it is also locked. Plan ATTACH PARTITION operations during maintenance windows to avoid blocking queries.
Recursive Validation: If the DEFAULT partition is itself partitioned, PostgreSQL recursively validates all sub-partitions, which can take considerable time on deeply nested hierarchies. Pre-filtering data and using CHECK constraints is especially important for recursive partitions.
Automatic Partition Creation: PostgreSQL does not automatically create missing partitions. Some extensions like pg_partman or TimescaleDB can automate partition lifecycle management. Alternatively, schedule partition creation jobs using pg_cron.
Constraint Inheritance: When attaching a partition with indexes or foreign keys, ensure they are compatible with the parent table. PostgreSQL will reject attachment if incompatibilities exist—drop conflicting constraints before attaching.
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL
ERROR 42501: must be owner of table
How to fix "must be owner of table" in PostgreSQL
trigger cannot change partition destination
How to fix "Trigger cannot change partition destination" in PostgreSQL
SSL error: certificate does not match host name
SSL error: certificate does not match host name in PostgreSQL
No SSL connection
No SSL connection to PostgreSQL