Report
ALTER COLUMN TYPE on a partitioned table drops REPLICA IDENTITY, CLUSTER ON and comments of partitions' attached indexes and constraints
- Severity
- 45
- Blamed commit
- 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99
On 10 branches
REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
ALTER TABLE ... ALTER COLUMN TYPE on a partitioned table rebuilds a partitioned index by replaying the parent's definition, which recreates every attached partition index. Properties set on the partition indexes themselves are not remembered and are lost:
- REPLICA IDENTITY USING INDEX on a partition's attached index (indisreplident goes false, pg_class.relreplident stays 'i');
- CLUSTER ON a partition's attached index (indisclustered false);
- COMMENT ON INDEX of the attached partition index;
- COMMENT ON CONSTRAINT of a partition's inherited unique or CHECK constraint.
The same properties on a plain table, on the parent, and on a partition's own (non-attached) indexes are kept.
Reproduction
CREATE TABLE p (k int NOT NULL, a int NOT NULL, b text)
PARTITION BY LIST (k);
CREATE TABLE p1 PARTITION OF p FOR VALUES IN (1);
CREATE UNIQUE INDEX p_ka ON p (k, a);
ALTER TABLE p1 REPLICA IDENTITY USING INDEX p1_k_a_idx;
CREATE INDEX p_b ON p (b);
ALTER TABLE p1 CLUSTER ON p1_b_idx;
COMMENT ON INDEX p1_k_a_idx IS 'p1 ri index';
CREATE PUBLICATION pub FOR TABLE p;
INSERT INTO p VALUES (1, 1, 'x');
ALTER TABLE p ALTER a TYPE bigint;
ALTER TABLE p ALTER b TYPE varchar(10);
SELECT c.relreplident, i.indexrelid::regclass, i.indisreplident,
i.indisclustered,
obj_description(i.indexrelid, 'pg_class')
FROM pg_class c JOIN pg_index i ON i.indrelid = c.oid
WHERE c.relname = 'p1';
-- before: i | p1_k_a_idx | t | f | p1 ri index
-- i | p1_b_idx | f | t |
-- after: i | p1_k_a_idx | f | f |
-- i | p1_b_idx | f | f |
UPDATE p SET b = 'z';
ERROR: cannot update table "p1" because it does not have a
replica identity and publishes updates
It also happens with COMMENT ON CONSTRAINT <partition unique constraint> ON p1, and with a comment on an inherited CHECK on a partition. Changing the same column type on a partition directly is refused (inherited column), so there is no workaround other than re-running the ALTERs afterwards.
pg_dump does not emit a REPLICA IDENTITY clause for the stale relreplident = 'i', so plain, -Fc and -j restores and pg_upgrade all turn p1 into REPLICA IDENTITY DEFAULT.
Analysis
RememberIndexForRebuilding() calls RememberReplicaIdentityForRebuilding() and RememberClusterOnForRebuilding() (1cc9c2412cc9, a40caf5f862c), and index and constraint comments are carried in the rebuilt statements. All of these act only on the index or constraint whose definition is replayed. For a partitioned index, that is the parent, and its recreation cascades into the partitions. The attached partition indexes and inherited constraints (skipped by "if (!conislocal) continue" in ATPostAlterTypeCleanup) are recreated by recursion from the parent, so per-partition replident, cluster markings and comments are never captured. Since 8b08f7d4820f (local partitioned indexes), attached partition indexes have been rebuilt through the parent.
Notes
Tested on master at ad33a140c76f53d66f37330edfa47c5d96e2d6ce. A fix could remember these properties for each attached child index and constraint before the drop, and reapply them by OID/name after the recursive recreation.
Related: 01a1cbd6-3047 (ALTER COLUMN TYPE rebuilds an invalid ON ONLY partitioned index) uses the same rebuild path but concerns indisvalid. 01a11f5c-052d (DROP of the replica identity index leaves relreplident 'i') reaches the same stale end state through DROP. 01a13c6b-23df (ALTER COLUMN TYPE drops DEPENDS ON EXTENSION) is a sibling property.
Branches: REL_14 through master (reproduced on master; REL_14_STABLE through REL_19_STABLE rebuild attached partition indexes through the parent and remember replica identity and cluster marks only for the replayed index, by code inspection).