Skip to content

Commit e09d763

Browse files
committed
ATPrepAddPrimaryKey: ignore non-PK constraints
Because of lack of test coverage, this function added by b0e96f3 wasn't ignoring constraint types other than primary keys, which it should have. Add some lines to a test for it. Reported-by: Richard Guo <guofenglinux@gmail.com> Discussion: https://postgr.es/m/CAMbWs48bc-k_-1fh0dZpAhp_LiR5MfEX9haystmoBboR_4czCQ@mail.gmail.com
1 parent e8d74ad commit e09d763

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

src/backend/commands/tablecmds.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8907,7 +8907,14 @@ ATPrepAddPrimaryKey(List **wqueue, Relation rel, AlterTableCmd *cmd,
89078907
List *children;
89088908
List *newconstrs = NIL;
89098909
ListCell *lc;
8910-
IndexStmt *stmt;
8910+
IndexStmt *indexstmt;
8911+
8912+
/* No work if not creating a primary key */
8913+
if (!IsA(cmd->def, IndexStmt))
8914+
return;
8915+
indexstmt = castNode(IndexStmt, cmd->def);
8916+
if (!indexstmt->primary)
8917+
return;
89118918

89128919
/* No work if no legacy inheritance children are present */
89138920
if (rel->rd_rel->relkind != RELKIND_RELATION ||
@@ -8916,8 +8923,7 @@ ATPrepAddPrimaryKey(List **wqueue, Relation rel, AlterTableCmd *cmd,
89168923

89178924
children = find_inheritance_children(RelationGetRelid(rel), lockmode);
89188925

8919-
stmt = castNode(IndexStmt, cmd->def);
8920-
foreach(lc, stmt->indexParams)
8926+
foreach(lc, indexstmt->indexParams)
89218927
{
89228928
IndexElem *elem = lfirst_node(IndexElem, lc);
89238929
Constraint *nnconstr;

src/test/regress/expected/inherit.out

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2309,7 +2309,30 @@ alter table inh_child inherit inh_parent; -- nope
23092309
ERROR: column "a" in child table must be marked NOT NULL
23102310
alter table inh_child alter a set not null;
23112311
alter table inh_child inherit inh_parent; -- now it works
2312-
drop table inh_parent, inh_child;
2312+
-- don't interfere with other types of constraints
2313+
alter table inh_parent add constraint inh_parent_excl exclude ((1) with =);
2314+
alter table inh_parent add constraint inh_parent_uq unique (a);
2315+
alter table inh_parent add constraint inh_parent_fk foreign key (a) references inh_parent (a);
2316+
create table inh_child2 () inherits (inh_parent);
2317+
create table inh_child3 (like inh_parent);
2318+
alter table inh_child3 inherit inh_parent;
2319+
select conrelid::regclass, conname, contype, coninhcount, conislocal
2320+
from pg_constraint
2321+
where conrelid::regclass::text in ('inh_parent', 'inh_child', 'inh_child2', 'inh_child3')
2322+
order by 2, 1;
2323+
conrelid | conname | contype | coninhcount | conislocal
2324+
------------+-----------------------+---------+-------------+------------
2325+
inh_child2 | inh_child2_a_not_null | n | 1 | f
2326+
inh_child3 | inh_child3_a_not_null | n | 1 | t
2327+
inh_child | inh_child_a_not_null | n | 1 | t
2328+
inh_child | inh_child_pkey | p | 0 | t
2329+
inh_parent | inh_parent_excl | x | 0 | t
2330+
inh_parent | inh_parent_fk | f | 0 | t
2331+
inh_parent | inh_parent_pkey | p | 0 | t
2332+
inh_parent | inh_parent_uq | u | 0 | t
2333+
(8 rows)
2334+
2335+
drop table inh_parent, inh_child, inh_child2, inh_child3;
23132336
--
23142337
-- test multi inheritance tree
23152338
--

src/test/regress/sql/inherit.sql

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,20 @@ create table inh_child (a int primary key);
846846
alter table inh_child inherit inh_parent; -- nope
847847
alter table inh_child alter a set not null;
848848
alter table inh_child inherit inh_parent; -- now it works
849-
drop table inh_parent, inh_child;
849+
850+
-- don't interfere with other types of constraints
851+
alter table inh_parent add constraint inh_parent_excl exclude ((1) with =);
852+
alter table inh_parent add constraint inh_parent_uq unique (a);
853+
alter table inh_parent add constraint inh_parent_fk foreign key (a) references inh_parent (a);
854+
create table inh_child2 () inherits (inh_parent);
855+
create table inh_child3 (like inh_parent);
856+
alter table inh_child3 inherit inh_parent;
857+
select conrelid::regclass, conname, contype, coninhcount, conislocal
858+
from pg_constraint
859+
where conrelid::regclass::text in ('inh_parent', 'inh_child', 'inh_child2', 'inh_child3')
860+
order by 2, 1;
861+
862+
drop table inh_parent, inh_child, inh_child2, inh_child3;
850863

851864
--
852865
-- test multi inheritance tree

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy