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 1sFsQQ-006ycs-64 for pgsql-general@arkaria.postgresql.org; Sat, 08 Jun 2024 09:31:51 +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 1sFsQO-00GJl7-Jz for pgsql-general@arkaria.postgresql.org; Sat, 08 Jun 2024 09:31:49 +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 1sFsQO-00GJky-7o for pgsql-general@lists.postgresql.org; Sat, 08 Jun 2024 09:31:49 +0000 Received: from mail-qv1-xf29.google.com ([2607:f8b0:4864:20::f29]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sFsQM-000atR-Vq for pgsql-general@lists.postgresql.org; Sat, 08 Jun 2024 09:31:48 +0000 Received: by mail-qv1-xf29.google.com with SMTP id 6a1803df08f44-6b065d12e81so5586836d6.0 for ; Sat, 08 Jun 2024 02:31:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717839105; x=1718443905; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=AR3XkOcQFhL0ZGiq7kfLsBBvXJJ3Fa1AOSapg3S1+M8=; b=IQTg++nRsHEOuJFFUHN/icLNZwMK/MWUf296oCxhznBwnNot/scl3SBscI3GKUHg0m BI/piT/fVCP/ZaEJM1HSPhs6I7piwWPYLdF6M6tMX5miHHrQ+/vGQP7Jxp3QWM816pYr t5xb4DYDUKPUc69Aw/lM/JPn88qJy6Qx+8Ht/S+4Xe8qw70YnlH8DGYubOPkeiBm8pv1 vwqd/b3B3i2EG9YD2UqkWLcv7ZmtpT8PzMJabyLVCq4Ej3H4ljzkXCyndyhw2ql0PO3x EEW2Cv9NxEwUmM62gpIWVoSO8SFNVFi2M/WcVQZi7tgPjPG5dh4u2k7nxBoxxtb1K+Bg zEUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717839105; x=1718443905; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=AR3XkOcQFhL0ZGiq7kfLsBBvXJJ3Fa1AOSapg3S1+M8=; b=MNZ9o7oRQywzBZ5kSQBEi8ROMPllitwW00OAorwYS4vz0PEMRIQd9RwgBHh79XhF7v L1QXTxdGj9JuruoBdzRBB8Z8l7MQm79BjTDf+KZOg3z4V3FZfEHRH2F/5VxQoBr9Mdzy Y7bHeNbCb1Z4WL8616bqJoL8JZ+S/RylqRm7AcVaxLY6Ht6JQwbRGBEokVyo7x0EzDDq IoYHQYeYKYomJVObb/UaEiiDfq257vgoqPt9E8pAI7S5L0m+VcRNENkxlf6SQwISHDxL 1Z5BQAiLYkelgrqbgAmE8vllAY1gTPi4aRopyPZGnHefjOLb37+PBZ9CEhvrzvB4WTL+ 5VKA== X-Gm-Message-State: AOJu0YxMUd1s51Zc17pMqplnY/bKT9sD2b3I0YyhYGR+/L7rhdts7tr4 O7IZvBO2E+dI6y+b39Be6PW6cx83iKrRzHB0W9O+E9ZenhtR83aQ71VBbgf4Hjl4WN90ZF8Ud32 kEXfOon8lsTabKv0bdLiXGeFaPPFYSguw X-Google-Smtp-Source: AGHT+IHHxXGZxpqYNTSZfSLCbWcVQHgEg7/UNgk2i316suiBarR37gvjZ0lrw98C+UZoRCpl6YIFhuoAAHRoeMIVM3M= X-Received: by 2002:a05:6214:3189:b0:6af:bacb:5930 with SMTP id 6a1803df08f44-6b059cfa9fdmr53714216d6.50.1717839104568; Sat, 08 Jun 2024 02:31:44 -0700 (PDT) MIME-Version: 1.0 From: yudhi s Date: Sat, 8 Jun 2024 15:01:32 +0530 Message-ID: Subject: Question on pg_cron To: pgsql-general Content-Type: multipart/alternative; boundary="0000000000009bf5d4061a5d95e4" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000009bf5d4061a5d95e4 Content-Type: text/plain; charset="UTF-8" Hello All, We have around 10 different partition tables for which the partition maintenance is done using pg_partman extension. These tables have foreign key dependency between them. We just called partman.run_maintanance_proc() through pg_cron without any parameters and it was working fine. So we can see only one entry in the cron.job table. And it runs daily once. It was all working fine and we were seeing the historical partition being dropped and new partitions being created without any issue. But suddenly we started seeing, its getting failed with error "ERROR: can not drop schema1.tab1_part_p2023_12_01 because other objects depend on it" Then we realized , it may be the case that it's trying to run the partition maintenance for the Parent partition table first before the child partition table. So not sure how pg_partman handles the ordering of tables while doing the partition maintenance as we don't see any parameter to drive the ordering of the partition maintenance in part_config and we were under the impression pg_partman will take care of the sequence of partition maintenance automatically. So want to understand if anybody encountered such issues? And to handle the above issue , we are planning to call the partition maintenance of each of the TABLE by passing the table name explicitly to the run_maintanance_proc(), something as below. Is this advisable? SELECT cron.unschedule('run_maintenance_proc'); SELECT cron.schedule( 'daily_partition_maintenance', '0 2 * * *', $$ DO $$ BEGIN -- Run maintenance for child tables first PERFORM partman.run_maintenance_proc('schema1.child_table1'); PERFORM partman.run_maintenance_proc('schema1.child_table2'); -- Add more child tables as needed -- Run maintenance for parent tables next PERFORM partman.run_maintenance_proc('schema1.parent_table1'); PERFORM partman.run_maintenance_proc('schema1.parent_table2'); -- Add more parent tables as needed END; $$; $$ ); *Or else * create a function like below and then call/schedule it through pg_cron CREATE OR REPLACE FUNCTION run_partition_maintenance() RETURNS void AS $$ BEGIN -- Run maintenance for child tables first PERFORM partman.run_maintenance_proc('schema1.child_table1'); PERFORM partman.run_maintenance_proc('schema1.child_table2'); -- Add more child tables as needed -- Run maintenance for parent tables next PERFORM partman.run_maintenance_proc('schema1.parent_table1'); PERFORM partman.run_maintenance_proc('schema1.parent_table2'); -- Add more parent tables as needed END; $$ LANGUAGE plpgsql; SELECT cron.unschedule('run_maintenance_proc'); SELECT cron.schedule( 'daily_partition_maintenance', '0 2 * * *', 'CALL run_partition_maintenance()' ); --0000000000009bf5d4061a5d95e4 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello All,

We have around 10 different partition t= ables for which the partition maintenance is done using pg_partman extensio= n. These tables have foreign key dependency between them.=C2=A0 We just cal= led partman.run_maintanance_proc() through pg_cron without any parameters a= nd it was working fine. So we can see only one entry in the cron.job table.= And it runs daily once.

It was all working fine and we were seeing = the historical partition being dropped and new partitions being created wit= hout any issue. But suddenly we started seeing, its getting failed with err= or "ERROR: can not drop schema1.tab1_part_p2023_12_01 because other ob= jects depend on it"

Then we realized , it may be the case that = it's trying to run the partition maintenance for the Parent partition t= able first before the child partition table. So not sure how pg_partman han= dles the ordering of tables while doing the partition maintenance as we don= 't see any parameter to drive the ordering of the partition maintenance= in part_config and we were under the impression pg_partman will take care = of the sequence of partition maintenance automatically.

So want to u= nderstand if anybody encountered such issues?

And to handle the abov= e issue , we are planning to call the partition maintenance of each of the = TABLE by passing the table name explicitly to the run_maintanance_proc(), s= omething as below. Is this advisable?

SELEC= T cron.unschedule('run_maintenance_proc');
SELECT cron.schedule(=
=C2=A0 =C2=A0 'daily_partition_maintenance',
=C2=A0 =C2=A0 &= #39;0 2 * * *',
=C2=A0 =C2=A0 $$
=C2=A0 =C2=A0 DO $$
=C2=A0 = =C2=A0 BEGIN
=C2=A0 =C2=A0 =C2=A0 =C2=A0 -- Run maintenance for child ta= bles first
=C2=A0 =C2=A0 =C2=A0 =C2=A0 PERFORM partman.run_maintenance_p= roc('schema1.child_table1');
=C2=A0 =C2=A0 =C2=A0 =C2=A0 PERFORM= partman.run_maintenance_proc('schema1.child_table2');
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 -- Add more child tables as needed

=C2=A0 =C2= =A0 =C2=A0 =C2=A0 -- Run maintenance for parent tables next
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 PERFORM partman.run_maintenance_proc('schema1.parent_= table1');
=C2=A0 =C2=A0 =C2=A0 =C2=A0 PERFORM partman.run_maintenanc= e_proc('schema1.parent_table2');
=C2=A0 =C2=A0 =C2=A0 =C2=A0 -- = Add more parent tables as needed
=C2=A0 =C2=A0 END;
=C2=A0 =C2=A0 $$;=
$$
);


Or else

crea= te a function like below and then call/schedule it through pg_cron

C= REATE OR REPLACE FUNCTION run_partition_maintenance()
RETURNS void AS $$=
BEGIN
=C2=A0 =C2=A0 -- Run maintenance for child tables first
=C2= =A0 =C2=A0 PERFORM partman.run_maintenance_proc('schema1.child_table1&#= 39;);
=C2=A0 =C2=A0 PERFORM partman.run_maintenance_proc('schema1.ch= ild_table2');
=C2=A0 =C2=A0 -- Add more child tables as needed
=C2=A0 =C2=A0 -- Run maintenance for parent tables next
=C2=A0 =C2=A0 = PERFORM partman.run_maintenance_proc('schema1.parent_table1');
= =C2=A0 =C2=A0 PERFORM partman.run_maintenance_proc('schema1.parent_tabl= e2');
=C2=A0 =C2=A0 -- Add more parent tables as needed
END;
$= $ LANGUAGE plpgsql;

SELECT cron.unschedule('run_maintenance_proc= ');

SELECT cron.schedule(
=C2=A0 =C2=A0 'daily_partition_= maintenance',
=C2=A0 =C2=A0 '0 2 * * *',
=C2=A0 =C2=A0 &#= 39;CALL run_partition_maintenance()'
);

--0000000000009bf5d4061a5d95e4--