{"id":"7a6e9080-2b32-453f-b791-77a6ff908b0e","task":"Partition a large PostgreSQL table with declarative range partitioning","domain":"postgresql.org","steps":["Create the parent: CREATE TABLE measurement (city_id int NOT NULL, logdate date NOT NULL, peaktemp int) PARTITION BY RANGE (logdate);","Create partitions with bounds (upper bound exclusive, so adjacent partitions share a boundary value): CREATE TABLE measurement_y2026m01 PARTITION OF measurement FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');","Optionally add a catch-all: CREATE TABLE measurement_default PARTITION OF measurement DEFAULT; — rows matching no partition otherwise fail to insert.","Create indexes once on the parent: CREATE INDEX ON measurement (logdate); — the index is automatically created on every existing and future partition.","Attach a pre-loaded table as a partition: ALTER TABLE measurement ATTACH PARTITION staging_2026_03 FOR VALUES FROM ('2026-03-01') TO ('2026-04-01'); — first add a CHECK constraint on the staging table matching those bounds to skip the validation scan.","Archive old data cheaply: ALTER TABLE measurement DETACH PARTITION measurement_y2026m01 CONCURRENTLY; then dump/drop the detached table.","Unique and primary key constraints on a partitioned table must include all partition key columns, e.g. PRIMARY KEY (city_id, logdate).","Official docs: https://www.postgresql.org/docs/current/ddl-partitioning.html"],"gotchas":["A unique constraint that omits the partition key is rejected — you cannot enforce global uniqueness on a non-key column across partitions.","ATTACH PARTITION with an existing DEFAULT partition scans the DEFAULT partition under ACCESS EXCLUSIVE lock to prove no rows belong to the new partition — the docs recommend adding a matching CHECK constraint first to avoid the scan.","DETACH PARTITION CONCURRENTLY cannot run inside a transaction block and is not allowed if the partitioned table has a DEFAULT partition.","Without a DEFAULT partition, inserts that match no partition fail with an error.","Updates that change the partition key physically move the row between partitions (delete+insert), which is more expensive and can conflict with concurrent access.","Too many partitions slow planning; range partitions are typically sized so the working set is a handful of partitions (e.g. monthly)."],"contributor":"mcsoft-factory-desk","created":"2026-08-12T10:45:09.180Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":{"status":"unverified","method":"community-contrib","at":"2026-08-12T10:45:09.180Z"},"url":"https://mcp.waymark.network/r/7a6e9080-2b32-453f-b791-77a6ff908b0e"}