Re: Foreign keys

From: Kevin Grittner <kgrittn(at)ymail(dot)com>
To: "Dean Gibson \(DB Administrator\)" <postgresql(at)ultimeth(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Foreign keys
Date: 2013-12-18 18:41:01
Message-ID: 1387392061.53728.YahooMailNeo@web162905.mail.bf1.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Dean Gibson (DB Administrator) <postgresql(at)ultimeth(dot)com> wrote:

> I have general question about FOREIGN KEYs:
>
>    1. Suppose I have table A with primary key X, and another
>       table B with field Y.
>    2. When I 'ALTER TABLE "B"  ADD FOREIGN KEY( "Y" )  REFERENCES
>       "A"  ON UPDATE CASCADE  ON DELETE CASCADE', that clearly
>       spends some time building a separate index.

No it doesn't.  If you are observing activity at that time, it is
probably from validating that the constraint is initially valid.

>    3. However, the PostgreSQL documentation seems to indicate
>       that it's a good idea to also separately create an index on
>       Y.

It *often* is, but there are various reasons you might not want
such an index, which is why its creation is not automatic.

>    5. If I need the separate index on Y, should it be built
>       before or after the FOREIGN KEY constraint?

In some cases it may allow faster initial validation of the
constraint; if I wanted the index I would probably build it before
adding the constraint.

--
Kevin Grittner
EDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

  • Foreign keys at 2013-12-18 18:02:04 from Dean Gibson (DB Administrator)

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Joe Van Dyk 2013-12-18 19:26:38 Replication failed after stalling
Previous Message Rob Sargent 2013-12-18 18:10:03 Re: Foreign keys