pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Per Kaminsky <per.kaminsky@hawk-intech.com>
To: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: Re: Performance issues on FK Triggers after replacing a primary column
Date: Mon, 28 Mar 2022 09:49:22 -0700
Message-ID: <04bce16b-7bf4-e74a-e3c7-f6e369592cca@aklaver.com> (raw)
In-Reply-To: <DB8PR09MB3913EC4DBBDFAC619D119C93D81D9@DB8PR09MB3913.eurprd09.prod.outlook.com>
References: <DB8PR09MB3913300D0A5150D499048FA8D81C9@DB8PR09MB3913.eurprd09.prod.outlook.com>
	<8ed0c2ae-8a35-2628-4b41-68f9cb734297@aklaver.com>
	<DB8PR09MB3913EC4DBBDFAC619D119C93D81D9@DB8PR09MB3913.eurprd09.prod.outlook.com>

On 3/27/22 23:53, Per Kaminsky wrote:
> The table structure looks (roughly) like this:
> 
>   * Table "Base": (id, created, deleted, origin, ...) ~3m rows
>   * Table "A": (id as FK on "Base", ...) ~400k rows
>   * Table "B": (id, ref_a as FK on "A", type, ...) ~2m rows
> 
> Swapping the PK of "A" happens as following, the FK is dropped during 
> the process since otherwise the performance issues also happen here when 
> updating the PK. The update calls do normally utilize a file based 
> import into a temporary table from which i do the actual update:
> 
> ALTER TABLE "B" DROP CONSTRAINT "B_to_A_fkey";
> ALTER TABLE "A" ADD COLUMN id_temp BIGINT;
> // fill id_temp with new IDs
> UPDATE "B" SET ref_a = "A".id_temp WHERE "B".ref_a= "A".id;
> UPDATE "A" SET id = id_temp;
> ALTER TABLE "B" ADD CONSTRAINT "B_to_A_fkey" FOREIGN KEY (ref_a) 
> REFERENCES A(id);

Now that the morning coffee has taken effect, I'm wondering why the 
above is necessary at all?

If "B_to_A_fkey" where to have ON UPDATE CASCADE then you could 
eliminate the dropping/adding back of the FK. The process would be:

ALTER TABLE "A" ADD COLUMN id_temp BIGINT;
// fill id_temp with new IDs
UPDATE "A" SET id = id_temp;
UPDATE "B" SET type = 2 WHERE type ISNULL;

It might even be possible to further simplify depending on what '// fill 
id_temp with new IDs' actually does?

> 
> And then the new occuring step, in the same transaction, which then also 
> has shown the performance issues described if i would not remove the FK 
> temporarily:
> 
> ALTER TABLE "B" DROP CONSTRAINT "B_to_A_fkey";
> UPDATE "B" SET type = 2 WHERE type ISNULL;
> ALTER TABLE "B" ADD CONSTRAINT "B_to_A_fkey" FOREIGN KEY (ref_a) 
> REFERENCES A(id);
> 
> 
> 
> **
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (13+ messages)  latest in thread

Message-ID: <04bce16b-7bf4-e74a-e3c7-f6e369592cca@aklaver.com>
Permalink:  ../04bce16b-7bf4-e74a-e3c7-f6e369592cca@aklaver.com/
Also on:    postgresql.org/message-id/04bce16b-7bf4-e74a-e3c7-f6e369592cca@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, per.kaminsky@hawk-intech.com
  Subject: Re: Performance issues on FK Triggers after replacing a primary column
  In-Reply-To: <04bce16b-7bf4-e74a-e3c7-f6e369592cca@aklaver.com>
  References: <DB8PR09MB3913300D0A5150D499048FA8D81C9@DB8PR09MB3913.eurprd09.prod.outlook.com> <8ed0c2ae-8a35-2628-4b41-68f9cb734297@aklaver.com> <DB8PR09MB3913EC4DBBDFAC619D119C93D81D9@DB8PR09MB3913.eurprd09.prod.outlook.com> <04bce16b-7bf4-e74a-e3c7-f6e369592cca@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox