Thread: How to add constraints without validating data.

How to add constraints without validating data.

From
Amit jain
Date:
Hi Gurus

Is there any way in postgreSQL to apply constraints forcefully without checking/validating Data ? It should validate only those records which comes after constraints are applied.

I need this help as data is already loaded in database but "ALTER TABLE" sql for adding CONSTRAINTS is taking enormous time.

Thanks for ideas/suggestion in Advance !!


--
Regards,

Amit Jain 
Synechron Technologies Pvt. Ltd. 4th  Floor, B Wing, Block 1.2, Embassy Techzone,
Rajiv Gandhi Infotech Park, Hinjewadi Phase II, Pune 411 057. 
Phone : +91.20.40262000 Ext 2163 | Mobile:+91.9561298565 | VoIP : +1.732.415.0894
http://www.synechron.com  MailTo: amitjain@synechron.com



Re: How to add constraints without validating data.

From
Nilesh Govindarajan
Date:
On Thu, May 27, 2010 at 5:55 PM, Amit jain <amitjain@synechron.com> wrote:
> Hi Gurus
>
> Is there any way in postgreSQL to apply constraints forcefully without
> checking/validating Data ? It should validate only those records which comes
> after constraints are applied.
>
> I need this help as data is already loaded in database but "ALTER TABLE" sql
> for adding CONSTRAINTS is taking enormous time.
>
> Thanks for ideas/suggestion in Advance !!
>
>
> --
>
> Regards,
>
> Amit Jain
> Synechron Technologies Pvt. Ltd. 4th  Floor, B Wing, Block 1.2, Embassy
> Techzone,
> Rajiv Gandhi Infotech Park, Hinjewadi Phase II, Pune 411 057.
> Phone : +91.20.40262000 Ext 2163 | Mobile:+91.9561298565 | VoIP :
> +1.732.415.0894
> http://www.synechron.com  MailTo: amitjain@synechron.com
>
>


I don't think there's any method to do that. If you add a constraint
using ALTER TABLE it will check existing data also. One option is you
create a new table with the constraints and same column names then:

insert into newtable values ( select * from oldtable)

but again this will check for old data, but may be faster.

--
Nilesh Govindarajan
Facebook: nilesh.gr
Twitter: nileshgr
Website: www.itech7.com

Re: How to add constraints without validating data.

From
Iñigo Martinez Lasala
Date:
You have to disable triggers.

Two options:

- Export table data, truncate table (if your table is fk source you will have a problem), add constraint and import exported data with --disable-triggers flag in pg_restore.

- Perhaps you could add manually triger via pg_trigger table in pg_catalog whith an insert into this table.

-----Original Message-----
From: Amit jain <amitjain@synechron.com>
To: pgsql-admin@postgresql.org <pgsql-admin@postgresql.org>
Subject: [ADMIN] How to add constraints without validating data.
Date: Thu, 27 May 2010 17:55:56 +0530

Hi Gurus

Is there any way in postgreSQL to apply constraints forcefully without checking/validating Data ? It should validate only those records which comes after constraints are applied.

I need this help as data is already loaded in database but "ALTER TABLE" sql for adding CONSTRAINTS is taking enormous time.

Thanks for ideas/suggestion in Advance !!


--
Regards,

Amit Jain 
Synechron Technologies Pvt. Ltd. 4th  Floor, B Wing, Block 1.2, Embassy Techzone,
Rajiv Gandhi Infotech Park, Hinjewadi Phase II, Pune 411 057. 
Phone : +91.20.40262000 Ext 2163 | Mobile:+91.9561298565 | VoIP : +1.732.415.0894
http://www.synechron.com  MailTo: amitjain@synechron.com




Re: How to add constraints without validating data.

From
Nilesh Govindarajan
Date:
On Thu, May 27, 2010 at 9:30 PM, Iñigo Martinez Lasala
<imartinez@vectorsf.com> wrote:
> You have to disable triggers.
>
> Two options:
>
> - Export table data, truncate table (if your table is fk source you will
> have a problem), add constraint and import exported data with
> --disable-triggers flag in pg_restore.
>
> - Perhaps you could add manually triger via pg_trigger table in pg_catalog
> whith an insert into this table.
>
> -----Original Message-----
> From: Amit jain <amitjain@synechron.com>
> To: pgsql-admin@postgresql.org <pgsql-admin@postgresql.org>
> Subject: [ADMIN] How to add constraints without validating data.
> Date: Thu, 27 May 2010 17:55:56 +0530
>
> Hi Gurus
>
> Is there any way in postgreSQL to apply constraints forcefully without
> checking/validating Data ? It should validate only those records which comes
> after constraints are applied.
>
> I need this help as data is already loaded in database but "ALTER TABLE" sql
> for adding CONSTRAINTS is taking enormous time.
>
> Thanks for ideas/suggestion in Advance !!
>
>
> --
>
> Regards,
>
> Amit Jain
> Synechron Technologies Pvt. Ltd. 4th  Floor, B Wing, Block 1.2, Embassy
> Techzone,
> Rajiv Gandhi Infotech Park, Hinjewadi Phase II, Pune 411 057.
> Phone : +91.20.40262000 Ext 2163 | Mobile:+91.9561298565 | VoIP :
> +1.732.415.0894
> http://www.synechron.com  MailTo: amitjain@synechron.com
>
>
>
>
>


How would disabling triggers help that ? He just wants to add
constraints without loss of data. I don't think he has any triggers.

--
Nilesh Govindarajan
Facebook: nilesh.gr
Twitter: nileshgr
Website: www.itech7.com

Re: How to add constraints without validating data.

From
Iñigo Martinez Lasala
Date:
Constraints are special triggers, but triggers anyway.

So, If you truncate the table, create the contraint and then restore with triggers disabled, it could be faster that create the contraint and wait until it checks all table data... or not... :)



-----Original Message-----
From: Nilesh Govindarajan <lists@itech7.com>
To: Iñigo Martinez Lasala <imartinez@vectorsf.com>
Cc: Amit jain <amitjain@synechron.com>, pgsql-admin@postgresql.org
Subject: Re: [ADMIN] How to add constraints without validating data.
Date: Thu, 27 May 2010 21:45:25 +0530

How would disabling triggers help that ? He just wants to add
constraints without loss of data. I don't think he has any triggers.


Re: How to add constraints without validating data.

From
Nilesh Govindarajan
Date:
On Thu, May 27, 2010 at 9:49 PM, Iñigo Martinez Lasala
<imartinez@vectorsf.com> wrote:
> Constraints are special triggers, but triggers anyway.
>
> So, If you truncate the table, create the contraint and then restore with
> triggers disabled, it could be faster that create the contraint and wait
> until it checks all table data... or not... :)
>
>
>
> -----Original Message-----
> From: Nilesh Govindarajan <lists@itech7.com>
> To: Iñigo Martinez Lasala <imartinez@vectorsf.com>
> Cc: Amit jain <amitjain@synechron.com>, pgsql-admin@postgresql.org
> Subject: Re: [ADMIN] How to add constraints without validating data.
> Date: Thu, 27 May 2010 21:45:25 +0530
>
> How would disabling triggers help that ? He just wants to add
> constraints without loss of data. I don't think he has any triggers.
>
>
>


I agree constraints are triggers. But as far as I know, truncating
table doesn't disable the constraints.
Its same as deleteing all rows and then altering the table.
In that manner, even my solution would be appropriate because it
creates a duplicate table with the constraints.
Whatever be the method, I don't think its possible to avoid the
constraints. Correct me if I'm wrong.

--
Nilesh Govindarajan
Facebook: nilesh.gr
Twitter: nileshgr
Website: www.itech7.com

Re: How to add constraints without validating data.

From
Robert Voinea
Date:
Why don't you use a partition?

Create another table that inherits from the table and add your new constraints
to the new table.


On Thursday 27 May 2010 19:32:33 Nilesh Govindarajan wrote:
> On Thu, May 27, 2010 at 9:49 PM, Iñigo Martinez Lasala
>
> <imartinez@vectorsf.com> wrote:
> > Constraints are special triggers, but triggers anyway.
> >
> > So, If you truncate the table, create the contraint and then restore with
> > triggers disabled, it could be faster that create the contraint and wait
> > until it checks all table data... or not... :)
> >
> > How would disabling triggers help that ? He just wants to add
> > constraints without loss of data. I don't think he has any triggers.
>
> I agree constraints are triggers. But as far as I know, truncating
> table doesn't disable the constraints.
> Its same as deleteing all rows and then altering the table.
> In that manner, even my solution would be appropriate because it
> creates a duplicate table with the constraints.
> Whatever be the method, I don't think its possible to avoid the
> constraints. Correct me if I'm wrong.

--
Robert Voinea <robert (dot) voinea (at) topex (dot) ro>
Software Developer
Phone: +40 21 408 38 00 / ext. 343
Fax: +40 21 408 38 08
Local time: GMT+2
http://www.topex.ro