Re: ADD FOREIGN KEY (was Re: [GENERAL] 7.4Beta)

From: Greg Stark <gsstark(at)mit(dot)edu>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: ADD FOREIGN KEY (was Re: [GENERAL] 7.4Beta)
Date: 2003-09-29 18:20:36
Message-ID: 87lls7e9sb.fsf@stark.dyndns.tv
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

> > > So a db designer made a bloody mistake.

Not necessarily. If I'm never going to update or delete from the parent table
the index would be useless. I find very few of my foreign key relationships
actually need indexes on the child table. I usually only have the unique index
on the parent table.

And often the child table is the big table. The index would be very large and
have awful selectivity. The last thing I want is a 5-million record table with
half a dozen indexes each with 10-20 unique values.

> > > The problem is there's no easy way to find out what's missing.
> > > I'd really like EXPLAIN to display all subsequent triggered queries
> > > also, to see the full scans caused by missing indexes.
> >
> > I'd sure second that!

I think the root of problem here is the same as the root of the problem with
foreign key checks being slow for large batch updates and inserts. Namely that
foreign key constraint checks are being handled as a million small queries.

To handle foreign key constraints optimally they would really have to be
merged into the plan in a kind of join. For most inserts/updates something
like a nested-loop join that is effectively the same as the current triggers
would be used. But for large batch updates/inserts it's quite possible that it
would look more like a hash join or even a merge join.

To do that would probably mean throwing out the whole trigger-based
implementation though, which seems like an awfully big project.

And being able to disable and reenable constraints would still be nice.
They're never going to be instantaneous. And besides, speed isn't the only
reason to want to disable constraints temporarily. The database is a tool, it
should be there to do the DBA's bidding, not the other way around :)

--
greg

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rasmus Aveskogh 2003-09-29 18:23:40 Re: Result set granularity..
Previous Message Robert Treat 2003-09-29 18:19:47 Re: Postgres vs. Progress performance

Browse pgsql-hackers by date

  From Date Subject
Next Message Greg Stark 2003-09-29 18:25:02 Re: Alter Table Column Datatype
Previous Message Bruce Momjian 2003-09-29 18:11:37 Re: ecpg doesn't compile (datetime.h/dtime_t)