From: | Per Kaminsky <per(dot)kaminsky(at)hawk-intech(dot)com> |
---|---|
To: | Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Performance issues on FK Triggers after replacing a primary column |
Date: | 2022-03-28 06:53:49 |
Message-ID: | DB8PR09MB3913EC4DBBDFAC619D119C93D81D9@DB8PR09MB3913.eurprd09.prod.outlook.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
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);
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);
________________________________
From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Sent: Sunday, March 27, 2022 23:22
To: Per Kaminsky <per(dot)kaminsky(at)hawk-intech(dot)com>; pgsql-general(at)postgresql(dot)org <pgsql-general(at)postgresql(dot)org>
Subject: Re: Performance issues on FK Triggers after replacing a primary column
On 3/27/22 09:30, Per Kaminsky wrote:
> Hi there,
>
> i recently stumbled upon a performance issue which i can't
> really understand.
> The issue occured when i (roughly) did the following without a commit in
> between:
>
> * Replace the PK column of a table A which has a referencing table B -
> I have removed the FK from the referencing tables B and have
> recreated them afterwards
> * Now following i am working in one of the referencing tables B,
> updating columns. This takes an extremely large amount of time. This
> means, e.g. updating 1000 rows would now need 35-40 seconds.
> * The "explain" tells, that the Foreign Key trigger in B referencing A
> causes this mishap.
Post the query and the explain.
Also have you run vacuum and/or analyze on the tables involved?
> * Re-creating the Index in B for the column referencing A does not
> cause any performance gain.
> * If i again remove the FK to A from B this again shrinks back to some
> milliseconds.
>
> The question is, what does cause the FK trigger to be less performant
> than recreating the FK constraint? If executed on 100k or even 1m rows
> the operation takes hours or even days.
>
> Thank you very much.
> Sincerely, Per Kaminsky
>
--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com
From | Date | Subject | |
---|---|---|---|
Next Message | Per Kaminsky | 2022-03-28 07:22:09 | Re: Performance issues on FK Triggers after replacing a primary column |
Previous Message | Peter J. Holzer | 2022-03-27 23:34:16 | Re: support for DIN SPEC 91379 encoding |