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 1sOXhu-00EkhB-Vg for pgsql-general@arkaria.postgresql.org; Tue, 02 Jul 2024 07:13:43 +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 1sOXho-006ApD-My for pgsql-general@arkaria.postgresql.org; Tue, 02 Jul 2024 07:13:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sOXho-006Ap5-Av for pgsql-general@lists.postgresql.org; Tue, 02 Jul 2024 07:13:36 +0000 Received: from mail-lf1-x134.google.com ([2a00:1450:4864:20::134]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sOXhn-0000hr-3w for pgsql-general@lists.postgresql.org; Tue, 02 Jul 2024 07:13:35 +0000 Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-52e829086f3so3692665e87.3 for ; Tue, 02 Jul 2024 00:13:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719904413; x=1720509213; 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=Z5PTy6V3Lf+B1KYSTixCh80BmgWjhdsN9CfKaVSVmJg=; b=Yb5+r9byt/kiRIUkFi9Vl2kXvPIsow2nf2jBX5FcoCNMgqSz1WZv3ZAdfz+Em6hFA+ Mcr4Q4QNUVSNPERrnVSdeoxWvYfFpZdFJEANmsjTyxM108vZHLNoofyDUUrogHt4x/0k oLe/Emt+tsFDFeoZ7UtskDSAwSQJ7HN9h4GMkuBn2UKZe8GEQ3lDpiJfufU1hN4sN6B3 pGIhhZIZazPLy+OCzn9Vgj+0aDhO7sYkmyvdGRlzQcL9A+WX6bMGSmV/NF0u86NqEFG3 E8cfNWpl1n+Qyit+vGuEli4S1Aa3c7jXhAeFSVVLrprKdNZ2MUYvNGmbSMhFEoc5+q8Q xJgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719904413; x=1720509213; 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=Z5PTy6V3Lf+B1KYSTixCh80BmgWjhdsN9CfKaVSVmJg=; b=J3QN+pGEbAs4P557FSpZAuVOZUMxo04bAUp/I3Woe+2Xy9aWbi+v20V//J+SeC3dN4 O0S1Ve4pQ2oKLsAoM4f7tAOagt7SXjvTP6KVtdOpFFDMsWPzuTW6op1XjJCfd/oAAYBb yO4ST4c/slJgRkZSwY6SbAZmXAn2xh8BEhC8s9C/vb2xsU6tFqdpZUOrkSOiDTy0lecn N9AP+0xBeJI1MX3kRiNGkczccdbb9Bc7RZQqlCeK/jQwidzIN10oPYm1AbvmL7JppBKy b9eKoRdLfOonYmtM5Leyb8vwcxZzD2l6yS/gM+tHiKxe+oDz+d2AvmMS1LRI0uJ4XneP GZ9A== X-Gm-Message-State: AOJu0Yy+VZkqF3p+XUPasExmnBMd38MC8hB5dyoEkat+X5npE8Yaqn/x p/tTDv089vm8oz3QZlIV2UwVKGbpkWlnPMYzBp1pDRJ0VOqLYMkS1rfs7a/nv3EQo0tnvGSQtkK 7FFk8uRsgI6cnFRx3mokidNk/Dbc= X-Google-Smtp-Source: AGHT+IFICoTg07TMSqCBNiQGKYcn/hyXatc6nQI2MOl69eaIT9VLEk8A4/fnVxs4YcZBhlGo2YEJjRFdpqWNW8c5bQI= X-Received: by 2002:a05:6512:e97:b0:52c:cd7d:4e7d with SMTP id 2adb3069b0e04-52e8264bcc0mr4453864e87.5.1719904413210; Tue, 02 Jul 2024 00:13:33 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Muhammad Ikram Date: Tue, 2 Jul 2024 12:13:21 +0500 Message-ID: Subject: Re: Question on partman extension while relation exist To: yudhi s Cc: pgsql-general Content-Type: multipart/alternative; boundary="00000000000098e153061c3e73cd" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000098e153061c3e73cd Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Yudhi, I think disabling foreign keys before maintenance will help. Regards, Muhammad Ikram Bitnine global On Tue, Jul 2, 2024 at 11:41=E2=80=AFAM yudhi s wrote: > Hello All, > In postgres we are seeing issues during automatic partition maintenance > using pg_partman extension. So basically it automatically creates one new > partition and drops one historical partition each day based on the set > retention period in part_config. We just call it like > partman.run_maintenance_proc('table_name'); > > While there exists foreign key relationships between the partitioned > tables, Mainly during dropping the parent partitions it takes a lot of > time, as it validates every child table partitions record and also is > taking lock longer. Ideally it should check only the respective parent > partition, but it's not doing that because the foreign key is defined in > table level rather than partition level. So we are planning to create the > foreign keys on the partition level but not at table level. > > And we were thinking of doing it dynamically by having an "event trigger" > which will fire on "create statement" i.e while the "create new partition= " > statement will be triggered by the Pg_partman. It will try to also create > the foreign key constraints on the new child partition referring to the > respective parent partition during the same time. So that things will be > automated. > > But now we are stuck in one scenario , say for example if we execute the > pg_partman for the parent table first then it will create the new partiti= on > independently which is fine, but when it will try to drop the historical > partition, it will complain stating the child partition already exists. > > On the other hand, > > If we run the pg_partman for the child table first, then it will drop the > historical child partition without any issue , however it will throw an > error while creating the foreign key , as because the respective parent > partition has not yet been created. > > Need advice, how we should handle this scenario. Basically in which order > we should call the "pg_partman.run_maintenance_proc" for the parent and > child tables? > --=20 Muhammad Ikram --00000000000098e153061c3e73cd Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi Yudhi,

I think dis= abling foreign keys before maintenance will help.
Regards,
Muhammad Ikram=C2= =A0
Bitnine global

On Tue, Jul 2, 20= 24 at 11:41=E2=80=AFAM yudhi s <learnerdatabase99@gmail.com> wrote:
Hello = All,
In postgres we are seeing issues during automatic partition mainte= nance using pg_partman extension. So basically it automatically creates one= new partition and drops one historical partition each day based on the set= retention period in part_config. We just call it like partman.run_maintena= nce_proc('table_name');

While there exists foreign key relat= ionships between the partitioned tables, Mainly during dropping the parent = partitions it takes a lot of time, as it validates every child table partit= ions record and also is taking lock longer. Ideally it should check only th= e respective parent partition, but it's not doing that because the fore= ign key is defined in table level rather than partition level. So we are pl= anning to create the foreign keys on the partition level but not at table l= evel.

And we were thinking of doing it dynamically by having an &qu= ot;event trigger" which will fire on "create statement" i.e = while the "create new partition" statement will be triggered by t= he Pg_partman. It will try to also create the foreign key constraints on th= e new child partition referring to the respective parent partition during t= he same time. So that things will be automated.

But now we are stuck= in one scenario , say for example if we execute the pg_partman for the par= ent table first then it will create the new partition independently which i= s fine, but when it will try to drop the historical partition, it will comp= lain stating the child partition already exists.

On the other hand,=

If we run the pg_partman for the child table first, then it will dr= op the historical child partition without any issue , however it will throw= an error while creating the foreign key , as because the respective parent= partition has not yet been created.

Need advice, how we should hand= le this scenario. Basically in which order we should call the "pg_part= man.run_maintenance_proc" for the parent and child tables?


--
Muhammad Ikram

--00000000000098e153061c3e73cd--