Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sgIzX-000VSX-Pv for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2024 07:09:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sgIzV-00AhHa-49 for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2024 07:09:17 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sgIzU-00AhFG-Je for pgsql-general@lists.postgresql.org; Tue, 20 Aug 2024 07:09:17 +0000 Received: from mail-ua1-x933.google.com ([2607:f8b0:4864:20::933]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sgIzS-000Wor-Lb for pgsql-general@lists.postgresql.org; Tue, 20 Aug 2024 07:09:15 +0000 Received: by mail-ua1-x933.google.com with SMTP id a1e0cc1a2514c-842f1dd60deso1685961241.2 for ; Tue, 20 Aug 2024 00:09:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724137753; x=1724742553; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=gd7yk3vtc+1mo+VL3J2TkqOevjFwgt1l1F1gQtU+Ep8=; b=fR9YM7hvcS49hyCUsbC/D2DRwTJ6ojOeWik2kUlGr4Hbi2fZwzBmsfJ4Wh/m65WsFY nKc1EhfHqy3VJr7SaGpsTbmRweDSxLiV9rIirznCIkQK8Doi8svLWn74AoBewFaXMCqP vOJzw8eX1HnlyKEi3Zk2vPcaunFj+fW4iq6NJDnMq+adfPcLcwRkUCi/SJOaBt22OPnJ 2oXZ/ydASwoeb0vEn6rBNjbgo8/5rzaJOzviDM7+1cKviAk24pVdaqFqt/VwsJ030oF4 0AaGmawgLGNlaLrqtNAewFZbt1cBvkzTO14beKzgx6zerZIN4YlTWbX42iWsTYvpEKU0 9HtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724137753; x=1724742553; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gd7yk3vtc+1mo+VL3J2TkqOevjFwgt1l1F1gQtU+Ep8=; b=cLbKLAjn13FjVzuNISq+IQSMTuVzt3AUPHETSjvwOzFytc77A8qk2f3uXxZen6BM5n rxJJ3nTZAitb2HyA2+NYQW4NUSEnfeCt7Lxhuw9T5nzmEL1K0N5y33BZ6OcPsXuj0c99 W3SGOJReqqz6wcmzf3ij18FtI2rcMbr60+FGImiGS1eLaDGznLKu3rxMz0SsIeChIcpr Zi35ecWUfUzfkWlrQhdceDNHbYF8bJRroX0Mj7/SoD0548I5rBrtykbGKb6JvK+olV+d VeFR/cJnPPNhnikd4rB9KtUDQx42bN4hSMZctR+fAMO7obYUeZsLD4yNdzeEa/xrjtSx pTRQ== X-Gm-Message-State: AOJu0Yx9PI1H8Z0H3Uxvzj5lBHKSdI+e/b+bZNUHyQwbgtd4DXTesFBh vqTyaINiepAzgME47GKkQZaXjDrY0ucOmGkQgGlplitvRhyioRtQRY3Xv8QX8l5PZnaG3AYmHNz 8g4HtUw1DzFC8XNUsE2MeVK3xNfk= X-Google-Smtp-Source: AGHT+IF+0WdlMOGG7w3lciAKtfUBFTtWmi3jVFqd62toh4fjatvuT7PUSSN7l0rbTruCos7XPvEZQSkdm8e389qNGqk= X-Received: by 2002:a05:6102:dcb:b0:48f:82c5:90e7 with SMTP id ada2fe7eead31-49779905e30mr15522694137.14.1724137753547; Tue, 20 Aug 2024 00:09:13 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: sud Date: Tue, 20 Aug 2024 12:39:01 +0530 Message-ID: Subject: Re: Insert query performance To: David Rowley Cc: pgsql-general Content-Type: multipart/alternative; boundary="000000000000581a9c0620181a77" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000581a9c0620181a77 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Aug 19, 2024 at 4:33=E2=80=AFPM David Rowley = wrote: > On Mon, 19 Aug 2024 at 19:48, sud 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 the= m, > insert into the child table can cause slowness if we don't have foreign k= ey > 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 que= ry > 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. --000000000000581a9c0620181a77 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Mon, Aug 19, 2024 at 4:33=E2=80=AFPM D= avid Rowley <dgrowleyml@gmail.co= m> wrote:
On Mon, 19 Aug 2024 at 19:48, sud <suds1434@gmail.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 foreig= n 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).=C2=A0 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=C2=A0 row to the child table already exists in th= e 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 main= ly helps improve the deletes/updates performance of the parent table , if t= he same FK column gets impacted from the parent table. (This might be the r= eason why our detach partition in the parent table runs long and never comp= letes as we have no foreign key indexed).

However, my initial unders= tanding of "having the FK index will improve the insert performance= in the child table" is not accurate it seems. Rather as you menti= oned 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 , = =C2=A0it just scans the parent by the Primary key of the parent table (whic= h 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 tabl= e won't help the constraint validation faster. Please correct me if my = understanding is wrong here.

Additionally as you mentioned &quo= t;explain analyze" will show a section on how much time it really take= s 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= 9;t really do in production as that will physically insert data into the ta= ble. So do you mean to just do the "explain analyze" for the INSE= RT query and capture the plan and then do the rollback?=C2=A0 And in our ca= se it's a row by row insert=C2=A0happening , so we will see if we can c= lub/sum that "constraint validation" time for a handful=C2=A0if i= nsert somehow to get a better idea on the percentage of time we really spen= t in the constraint validation.
--000000000000581a9c0620181a77--