dbTalk Databases Forums  

"transfering" indexes to other table

comp.databases.postgresql comp.databases.postgresql


Discuss "transfering" indexes to other table in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Pet
 
Posts: n/a

Default "transfering" indexes to other table - 10-28-2009 , 07:53 AM






Hi,

since version 8.3 we can do following query:

create table newtable ( like oldtable INCLUDING DEFAULTS INCLUDING
CONSTRAINTS INCLUDING INDEXES );

I would prefer first create table without including index, because of
inserting big amount of data into newtable. And then I'd like
"transfer" all constraints and indexes and defaults to newtable.

Is it possible to do in SQL? May be create temp table where I insert
data without index and then select * insert into newtable?

Regards, Pet

Reply With Quote
  #2  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: "transfering" indexes to other table - 10-28-2009 , 10:05 AM






Pet wrote:
Quote:
since version 8.3 we can do following query:

create table newtable ( like oldtable INCLUDING DEFAULTS INCLUDING
CONSTRAINTS INCLUDING INDEXES );

I would prefer first create table without including index, because of
inserting big amount of data into newtable. And then I'd like
"transfer" all constraints and indexes and defaults to newtable.

Is it possible to do in SQL? May be create temp table where I insert
data without index and then select * insert into newtable?
I don't think that there is a one-command way of doing that.

But there are things that can help you.

There is pg_get_indexdef(index_oid) that can generate a CREATE INDEX
statement for a certain index, and pg_get_constraintdef(constraint_oid)
to get the definition of a constraint.

You could write an (advanced) PL/pgSQL function that extracts all
constraint and index definitions from a source table and modifies
and applies them for a target table. It would mean digging around in
the system catalogs and using dynamic SQL, but it can be done.

So you could first create the table without constraints and indexes,
then fill the table and finally call the function to create the
constraints and indexes on the new table.

Yours,
Laurenz Albe

Reply With Quote
  #3  
Old   
Pet
 
Posts: n/a

Default Re: "transfering" indexes to other table - 10-28-2009 , 10:31 AM



On Oct 28, 5:05*pm, "Laurenz Albe" <inv... (AT) spam (DOT) to.invalid> wrote:
Quote:
Pet wrote:
since version 8.3 we can do following query:

create table newtable ( like oldtable INCLUDING DEFAULTS INCLUDING
CONSTRAINTS INCLUDING INDEXES );

I would prefer first create table without including index, because of
inserting big amount of data into newtable. And then I'd like
"transfer" all constraints and indexes and defaults to newtable.

Is it possible to do in SQL? May be create temp table where I insert
data without index and then select * insert into newtable?

I don't think that there is a one-command way of doing that.

But there are things that can help you.

There is pg_get_indexdef(index_oid) that can generate a CREATE INDEX
statement for a certain index, and pg_get_constraintdef(constraint_oid)
to get the definition of a constraint.

You could write an (advanced) PL/pgSQL function that extracts all
constraint and index definitions from a source table and modifies
and applies them for a target table. It would mean digging around in
the system catalogs and using dynamic SQL, but it can be done.

So you could first create the table without constraints and indexes,
then fill the table and finally call the function to create the
constraints and indexes on the new table.
Hi Laurenz!

Thanks for your suggestions! Now I know where to start.

Pet

Quote:
Yours,
Laurenz Albe

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.