Re: cannot get CREATE TABLE AS to work

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Josh Berkus <josh(at)agliodbs(dot)com>
Cc: "Creager, Robert S" <CreagRS(at)LOUISVILLE(dot)STORTEK(dot)COM>, sqllist <pgsql-sql(at)postgresql(dot)org>
Subject: Re: cannot get CREATE TABLE AS to work
Date: 2001-03-09 20:15:30
Message-ID: Pine.BSF.4.21.0103091206370.81537-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Fri, 9 Mar 2001, Josh Berkus wrote:

> Robert,
>
> > I suspect that the INSERT INTO SELECT in this case will take longer than a
> > CREATE TABLE AS because of the referential integrity check needed on every
> > INSERT (per Tom Lane).
>
> In that case, what about:
>
> a) dropping the referential integrity check;

Unfortunately if he adds it back in with ALTER TABLE, that's going to be
slow as well. I did it in a fashion I felt was cleaner code, but in
practice, I think the implementation's performance is poor enough that
it might be worth doing in the less clean way (running a single select
looking for failing rows when possible on alter table rather than checking
each row -- less clean because it means keeping information on what the
fk check is in multiple places. :( )

> 2) making the referential integrity check deferrable (there's a way to
> do this, it was discussed a couple weeks ago - ask Tom).

Well, you can always add deferrable initially immediate to the constraint
and run a SET CONSTRAINTS ALL DEFERRED, but I'm not sure that'll actually
be much faster, it still does a check per row I believe.

It's hacky, but I'd say, if you don't have other triggers you care about,
twiddle pg_class.reltriggers for the class to 0, do the insert, set it
back to what it was before and then run selects to make sure the data is
valid (ie, would the constraint have failed).

[
assuming one column, something like:

select * from fktable where not exists
(select * from pktable where pktable.pkcol=fktable.fkcol);
]

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Creager, Robert S 2001-03-09 20:59:22 RE: cannot get CREATE TABLE AS to work
Previous Message Patrick Welche 2001-03-09 19:43:28 Re: [SQL] Re: MySQLs Describe emulator!