removing duplicated constraints

From: "Dan Langille" <dan(at)langille(dot)org>
To: pgsql-admin(at)postgresql(dot)org
Subject: removing duplicated constraints
Date: 2002-03-02 15:19:42
Message-ID: 200203021519.g22FJjk52460@lists.unixathome.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

I was looking through my database schema and noticed that some foreign key
declarations were duplicated. For example, I found this statement
occurred twice within my DDL:

alter table ports
add foreign key (category_id)
references categories (id) on update cascade on delete cascade;

I fixed up the DDL to remove the duplicates. Then I turned to the 7.2
PostgreSQL database to examine that situation. I noticed this
duplication:

CREATE CONSTRAINT TRIGGER "<unnamed>" AFTER INSERT OR UPDATE ON "ports"
FROM "categories" NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE
PROCEDURE "RI_FKey_check_ins"
('<unnamed>', 'ports', 'categories', 'UNSPECIFIED', 'category_id', 'id');

CREATE CONSTRAINT TRIGGER "<unnamed>" AFTER INSERT OR UPDATE ON "ports"
FROM "categories" NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE
PROCEDURE "RI_FKey_check_ins"
('<unnamed>', 'ports', 'categories', 'UNSPECIFIED', 'category_id', 'id');

To my eye, those two contraints are identical. Some may consider that as
a bug (i.e postgres allowing a FK to be duplicated). I see it as a
feature, which in this case, has been misused.

From what I see, I can easily delete either one of these two constraints
without affecting the relational integrity of the database. Do you agree?

This is how I'm going to identify the duplicate constraints.

$ pg_dump -s > fp2migration.ddl
$ grep "CREATE CONSTRAINT TRIGGER" ~/fp2migration.ddl > constraints.txt
$ cat constraints.txt | sort | uniq > constraints.sorted.txt
$ wc -l constraints.txt
93 constraints.txt
$ wc -l constraints.sorted.txt
78 constraints.sorted.txt

It looks like I have 15 duplicated constraints to fix up.

Thanks
--
Dan Langille
The FreeBSD Diary - http://freebsddiary.org/ - practical examples

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Ben 2002-03-02 17:40:01 Re: [GENERAL] where is my bottleneck?
Previous Message Jeff Fitzmyers 2002-03-02 14:44:02 Re: where is my bottleneck?