CREATE TABLE PARTITION OF fails when a local constraint name matches a cloned parent constraint =============================================================================================== AI-generated bug report from the AI Annex Report ID: 01a11f2b-1b95-7d6a-8f43-91db9ef05244 Filed: 2026-10-09 05:38:01 UTC Severity: 35/100 Blamed commit: 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99 ## Summary CREATE TABLE ... PARTITION OF installs indexes and foreign keys cloned from the parent before it installs CHECK and NOT NULL constraints written on the new partition. A local constraint whose name is the one the clone would choose is therefore rejected. ALTER TABLE ... ATTACH PARTITION does the opposite: the table's own constraints already exist, and the clone picks another name. Since PostgreSQL 18, the NOT NULL form of this clash is not a normal duplicate-object error. It is a unique violation on pg_constraint, because the NOT NULL creator never checks that an explicit name is free. The same unique violation happens on an ordinary table when a CHECK and a NOT NULL in one CREATE TABLE share a name. ## Reproduction Tested on PostgreSQL 20devel (412848dac37f2e4455e1505f83f0af0f3d28a67d). The foreign-key case: ```sql CREATE TABLE rk (a int PRIMARY KEY); CREATE TABLE fq ( a int, b int, CONSTRAINT fk1 FOREIGN KEY (a) REFERENCES rk ) PARTITION BY LIST (b); CREATE TABLE fq1 PARTITION OF fq ( CONSTRAINT fk1 CHECK (a > 0) ) FOR VALUES IN (1); ``` Expected: the statement succeeds. The CHECK keeps the name fk1 and the cloned foreign key is renamed, which is what ATTACH PARTITION does for the same names. Actual: ``` ERROR: constraint "fk1" for relation "fq1" already exists ``` SQLSTATE is 42710 (duplicate_object). A CHECK whose name does not collide is accepted, and the cloned foreign key keeps the name fk1. Creating the leaf first and then attaching it also succeeds: the local CHECK stays fk1 and the cloned foreign key is stored as fk1_1. ```sql CREATE TABLE fq_att (a int, b int, CONSTRAINT fk1 CHECK (a > 0)); ALTER TABLE fq ATTACH PARTITION fq_att FOR VALUES IN (3); ``` ``` relname | conname | contype ---------+---------+--------- fq_att | fk1 | c fq_att | fk1_1 | f ``` The NOT NULL case, where uq2_a_b_key is the name DefineIndex would generate for the cloned unique constraint: ```sql CREATE TABLE uq (a int, b int, UNIQUE (a, b)) PARTITION BY LIST (b); CREATE TABLE uq2 PARTITION OF uq ( CONSTRAINT uq2_a_b_key NOT NULL a ) FOR VALUES IN (2); ``` Expected: success. ATTACH PARTITION with those names keeps the NOT NULL constraint as uq_att_a_b_key and names the cloned unique constraint uq_att_a_b_key1. Actual: ``` ERROR: duplicate key value violates unique constraint "pg_constraint_conrelid_contypid_conname_index" DETAIL: Key (conrelid, contypid, conname)=(, 0, uq2_a_b_key) already exists. ``` SQLSTATE is 23505. No partition is required for that class of error. An explicit NOT NULL that reuses a CHECK name in the same statement fails the same way: ```sql CREATE TABLE plain ( a int, CONSTRAINT c1 CHECK (a > 0), CONSTRAINT c1 NOT NULL a ); ``` Actual: ``` ERROR: duplicate key value violates unique constraint "pg_constraint_conrelid_contypid_conname_index" DETAIL: Key (conrelid, contypid, conname)=(, 0, c1) already exists. ``` Two NOT NULL constraints that share a name still produce the normal "constraint ... already exists" error. An inherited CHECK with the same name and the same expression is merged. A different expression under the parent's CHECK name is a normal duplicate-object error, which is the right outcome for two different checks. ## Analysis DefineRelation stores the partition bound and partition key, then clones every parent index, row-level trigger, and foreign key, and only afterwards calls AddRelationNewConstraints for CHECK constraints named in the statement and AddRelationNotNullConstraints for NOT NULL. Index cloning was inserted in front of the existing CHECK step by 8b08f7d4820fd7a8ef6152a9dd8c6e3cb01e5f99 so that DefineIndex could see the partition key. Foreign-key cloning was added in that same slot by 3de241dba86f3dd000434f70aebba725fb928032. The comment that CHECK constraints come after partitioning was set up was added later, when generated-column defaults were moved earlier; it does not say that clones must run before the statement's own constraints. The clone paths are written to yield the name when it is already taken. addFkConstraint calls ConstraintNameIsUsed and, on a hit, ChooseConstraintName. ChooseRelationName does the same for an index that backs a constraint. Both look only at rows already visible in the catalogs. On PARTITION OF the local constraint has not been stored yet, so the clone takes the preferred name: the parent's foreign-key name, or the generated tab_cols_key or tab_pkey name. The later CHECK fails in MergeWithExistingConstraint because the existing row is not an identical check. ATTACH PARTITION works because the local row is already visible, so the clone is renamed. That is intentional behavior for a name that is in use, not a reason for PARTITION OF to reject the user's name. The catalog unique violation is newer. Commit 14e87ffa5c543b5f30ead7413084c25f7735039f began storing NOT NULL as pg_constraint rows and added AddRelationNotNullConstraints. The function comment says a clashing user-specified name should raise an error. The implementation compares that name only with other user-specified NOT NULL names in the same call. It does not call ConstraintNameIsUsed, and the existing_constraints list is consulted only when a name is being generated. StoreRelNotNull then inserts, and the unique index on (conrelid, contypid, conname) reports 23505. The older NOT NULL arm inside AddRelationNewConstraints does call ConstraintNameIsUsed before insert. This creator does not. On master, REL_19_STABLE, and REL_18_STABLE that gap is still present. Commit 0eeffd31bf9bb73446387cd7eebbbf43148aa281, for bug 19393, only taught generated NOT NULL names to avoid constraints already created in the same statement. Explicit names were not checked. REL_14_STABLE through REL_17_STABLE still clone indexes and foreign keys before local CHECK constraints, so the "already exists" failure is there. NOT NULL was only pg_attribute.attnotnull on those branches: a name written on NOT NULL was discarded and could not hit pg_constraint. This is not a security issue. It is a DDL failure. An unprivileged user cannot gain privileges, read ungranted data, or run arbitrary code through it. No report of this PARTITION OF ordering, or of an explicit NOT NULL name tripping the catalog unique index, showed up on pgsql-hackers or pgsql-bugs. Bug 19393 is the generated-name case and is already fixed on these branches: https://postgr.es/m/19393-6a82427485a744cf@postgresql.org Threads about ATTACH PARTITION renaming a cloned foreign key describe the behavior PARTITION OF fails to match; they do not report this CREATE TABLE failure. ## Fix Two changes belong together. Before storing a user-specified NOT NULL name, call ConstraintNameIsUsed and raise duplicate_object, as AddRelationNewConstraints already does for CHECK and for its own NOT NULL arm. That turns the catalog unique violation into a normal error when a CHECK and a NOT NULL share a name, and when a NOT NULL reuses an inherited CHECK name. Those are real conflicts and should keep failing. For PARTITION OF, create the statement's own CHECK and NOT NULL constraints after the partition key is stored and before indexes and foreign keys are cloned, then CommandCounterIncrement so the clone's catalog scan sees the new rows. ChooseRelationName and addFkConstraint will then rename the clone, matching ATTACH. attnotnull is already set from the column definition before any index is cloned, so a primary-key clone still observes NOT NULL columns. Do not rename an inherited CHECK to make room: a local constraint that reuses that name should remain a duplicate-object error. The NOT NULL name check is a few lines. Moving constraint creation is not. It must stay after generated columns are stored, because a CHECK may reference them, and after the partition key is stored, because DefineIndex requires that. It needs regression coverage for primary keys, multi-level partitions, and foreign keys on both sides. Shipping only the name check would be safe but would still make PARTITION OF fail where ATTACH succeeds, so it is not the whole fix. No patch is included. ## Testing The statements in this report were executed on a temporary server built from 412848dac37f2e4455e1505f83f0af0f3d28a67d. Non-colliding names, ATTACH PARTITION, merged identical checks, and a CHECK named like the cloned primary key (relation_pkey) were compared on that server. DefineRelation, AddRelationNotNullConstraints, addFkConstraint, and ChooseRelationName were read on master and on REL_19_STABLE through REL_14_STABLE. No source tree was modified.