Re: Insert query performance

From: sud <suds1434(at)gmail(dot)com>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Insert query performance
Date: 2024-08-20 07:09:01
Message-ID: CAD=mzVW2px2fQrtMYdDfkqgLYv-4iKHC_H1-D5UhmJ7BCBRcBg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Aug 19, 2024 at 4:33 PM David Rowley <dgrowleyml(at)gmail(dot)com> wrote:

> On Mon, 19 Aug 2024 at 19:48, sud <suds1434(at)gmail(dot)com> wrote:
> > In a version 15.4 postgres database, Is it possible that , if we have
> two big range partition tables with foreign key relationships between them,
> insert into the child table can cause slowness if we don't have foreign key
> index present in the child table? Basically it need to make sure the new
> row already added to parent partition table or not.
>
> Having an index on the referencing columns is only useful for DELETEs
> and UPDATEs affecting the foreign key column(s). For INSERTs to the
> referencing table, technically having indexes there would only slow
> down inserts due to the additional overhead of having to maintain the
> index, however, the overhead of having the index might be fairly
> minuscule when compared to performing a CASCADE UPDATE or DELETE to
> the referencing table when the DDL is performed on the referenced
> table.
>
> > And if there is any possible way(example query tracing etc) to get the
> underlying system queries which gets triggered as part of the main insert
> query? For example in above scenario, postgres must be executing some query
> to check if the incoming row to the child table already exists in the
> parent table or not?
>
> EXPLAIN ANALYZE will list the time it took to execute the foreign key
> trigger in the "Trigger for constraint" section.
>
> David
>

Thank you so much David.

If I get it correct , the index on the foreign key mainly helps improve the
deletes/updates performance of the parent table , if the same FK column
gets impacted from the parent table. (This might be the reason why our
detach partition in the parent table runs long and never completes as we
have no foreign key indexed).

However, my initial understanding of "*having the FK index will improve the
insert performance in the child table*" is not accurate it seems. Rather as
you mentioned it may negatively impact the loading/insert performance
because it has to now update the additional index in each insert. In case
of insert into child table, to ensure if the child row is already present
in the parent , it just scans the parent by the Primary key of the parent
table (which is be default indexed) and thus it doesn't need an index in
the child table foreign keys or having an index in the foreign key in the
child table won't help the constraint validation faster. Please correct me
if my understanding is wrong here.

Additionally as you mentioned "explain analyze" will show a section on how
much time it really takes for the constraint validation , I can see that
section now. But it seems it will really need that INSERT statement to be
executed and that we can't really do in production as that will physically
insert data into the table. So do you mean to just do the "explain analyze"
for the INSERT query and capture the plan and then do the rollback? And in
our case it's a row by row insert happening , so we will see if we can
club/sum that "constraint validation" time for a handful if insert somehow
to get a better idea on the percentage of time we really spent in the
constraint validation.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message sud 2024-08-20 07:19:12 Re: Insert query performance
Previous Message milist ujang 2024-08-20 00:41:03 Re: pg 16.4, logical replication, use case = data archiving