Re: [GENERAL] Config for fast huge cascaded updates - Mailing list pgsql-general

From Tom Lane
Subject Re: [GENERAL] Config for fast huge cascaded updates
Date
Msg-id 24005.1498516616@sss.pgh.pa.us
Whole thread Raw
In response to [GENERAL] Config for fast huge cascaded updates  (Craig de Stigter <craig.destigter@koordinates.com>)
List pgsql-general
Craig de Stigter <craig.destigter@koordinates.com> writes:
> We're doing a large migration on our site which involves changing most of
> the primary key values. We've noticed this is a *very* slow process.

> Firstly we've set up all the foreign keys to use `on update cascade`. Then
> we essentially do this on every table:

> UPDATE TABLE users SET id = id + 1000000;

> Since this cascades via about 40 foreign keys to most of the other tables
> in the database, this update on our fairly small table takes about five
> hours.

Do you have indexes on all the referencing columns?

The core problem here is that the updates will be cascaded one row at a
time.  As long as the referencing rows can be found by an indexscan,
that might be tolerable, but it's certainly not as fast as a bulk
update.

If you can guarantee no other updates while you're doing the migration,
it might be practical to drop the foreign key constraints, run all the
bulk updates by hand (on referencing tables too!), and then re-establish
the constraints.  Of course there's a lot of potential for errors of
omission here, but if you can script it and test the script in advance,
it's worth considering.

            regards, tom lane


pgsql-general by date:

Previous
From: Craig de Stigter
Date:
Subject: [GENERAL] Config for fast huge cascaded updates
Next
From: Jim Fulton
Date:
Subject: Re: [GENERAL] When inserting from a SELECT with an ORDER BY, are theinserts (and associated triggers) applied in order?