Re: Shorthand for foreign key indices

From: Scott Marlowe <smarlowe(at)g2switchworks(dot)com>
To: "John D(dot) Burger" <john(at)mitre(dot)org>
Cc: "Jim C(dot) Nasby" <decibel(at)decibel(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: Shorthand for foreign key indices
Date: 2005-05-09 17:58:33
Message-ID: 1115661512.3868.134.camel@state.g2switchworks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, 2005-05-09 at 12:40, John D. Burger wrote:
> >> By the way, I presume foreign key indices are used to check for
> >> referential integrity on insert. Can the query planner also use then
> >> somehow?
> >
> > It can use them the same way it can use any other index.
>
> Hmm, I guess I thought that "foreign key indices" were some special
> kind of index. For instance, I thought that Postgresql might
> effectively pre-compute part of the information it needed to do a join
> on the two relevant columns.
>
> I'm guessing now that we're just talking about a regular index on a
> column that happens to have a foreign key reference to another column.
> Is that the case?

Yes. And no.

PostgreSQL, by default, in a normal fk setup, requires a pk or unique
index on the column that IS the foreign key. i.e. the column being
pointed to.

The column that points at that column requires no index. So, in an
initial setup of FK->PK, only the master table has an index. And yes,
it is just a plain old index, although primary key indexes have a few
extra bits in the catalog to identify them as such and do some automagic
joining, they are otherwise plain old indexes.

Note that pgsql does not support indexes across multiple tables, so
there's really no way to have an index that has all the join data needed
in it alread, since it would need data from both tables.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Brendan Jurd 2005-05-09 18:03:53 Re: Shorthand for foreign key indices
Previous Message John D. Burger 2005-05-09 17:40:01 Re: Shorthand for foreign key indices