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 1rycO3-00HAHV-WB for pgsql-general@arkaria.postgresql.org; Sun, 21 Apr 2024 18:58:04 +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 1rycO0-00Fi1Y-JF for pgsql-general@arkaria.postgresql.org; Sun, 21 Apr 2024 18:58:00 +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 1rycO0-00Fi1M-7D for pgsql-general@lists.postgresql.org; Sun, 21 Apr 2024 18:58:00 +0000 Received: from mail-oo1-xc2f.google.com ([2607:f8b0:4864:20::c2f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rycNx-002Cxt-W2 for pgsql-general@lists.postgresql.org; Sun, 21 Apr 2024 18:57:59 +0000 Received: by mail-oo1-xc2f.google.com with SMTP id 006d021491bc7-5aa26990d5aso2449017eaf.1 for ; Sun, 21 Apr 2024 11:57:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713725876; x=1714330676; 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=r0eHcQD4hLrpm+SpgpLYCvYu30L9N9uAHZzupMZSMZQ=; b=Hng2W97tCQp2qfndxIzN7TMeoRB4bNaeEeuZvvHsvVACJxg4+VOAZY/82w6YrKpB8i RHewT2UhmN5E73smqr8Vh1PApTszuO6gSoANKhTP4b2iN452LVx/pEesQmUi3GGJHk4i YphAqy4ijezps/XyMZwymIL4gsEFhlKxNXu+o8BGxnm6uBrcq3b/bG1CGDnCmg7oBLVI yNrPA05OMSjC+0yDybqG95Kq4sCkxCVfhOlfKMqVOqn3jjuUtMzq48GkwyIYrPIvCKV5 C8QBHesKRyM5pXALtFNVYZPTKp5hfwOli6+kB5I5nQfqhC3lUj4kTyy4+0Dg0M1gxx2T FyNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713725876; x=1714330676; 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=r0eHcQD4hLrpm+SpgpLYCvYu30L9N9uAHZzupMZSMZQ=; b=rFRazZ+FDnM2CFSsvKgKWT2gHS9KFOqwzZB95iS5u4vUVV79QpD5CE2e26fpHOiSqJ ldMLfuQd4TgEzrs/cR6Wn72sZ8qaICBfhLa7qwOPENtnU2l/evn3NiQnH4442LdwYjVI 7+kC/yaIRklM9ZMr/wQRkTkzWj/YDXny9ot0uSaHsmuWPhXMYNLPBquiK+MBogsCpFNz JYHF7r4tXj39/2YIZFhfn9FSRYOrSkuueAMcHNUBqCWiUXPM+6v/f3dmLKBf7/NaroDu LzrC2Zhyai8OZ+IX1LCwFL1k/Qct71xeSdbZeeycVesByXtdcXlvpaQGNxVW11k0UvJW lakA== X-Forwarded-Encrypted: i=1; AJvYcCV0FqIJPVc1rU1XLY24vgXZkfLI2IhVvnx02QDS7kr2PmKhTh+A9NVydvd7QekA27rs6yIni8KP6HhuK59r8z2Edw/jMDTMJB5MQBG3Szxjs8Am X-Gm-Message-State: AOJu0YywP4Ur6jIMXtyU1wqEGg/ToGnBvSYYW6kJuLsP0GxNYzlrSCVS nocrIy8P2euOWDpYF+mmsjhro0d8/Wgy92SE/gF4AelJaIvehjn29I7eD8pK4p74CT5OoOT6wb0 ZytfVs/1sSxPDyeXkINSEdb03BPM= X-Google-Smtp-Source: AGHT+IESzoulK4zS7L/GmrkBbIiOKDo4DJN/0KhmmHBS6OI2Z7/cAklrHXkh8kTQsmznWdV8akqnfBeKnQ6UDGyxJuU= X-Received: by 2002:a05:6358:5d8e:b0:186:18a7:6b01 with SMTP id s14-20020a0563585d8e00b0018618a76b01mr10480358rwm.23.1713725876010; Sun, 21 Apr 2024 11:57:56 -0700 (PDT) MIME-Version: 1.0 References: <2720974.1713710606@sss.pgh.pa.us> In-Reply-To: From: yudhi s Date: Mon, 22 Apr 2024 00:27:44 +0530 Message-ID: Subject: Re: error in trigger creation To: "David G. Johnston" Cc: Tom Lane , pgsql-general Content-Type: multipart/alternative; boundary="0000000000001508da06169fe6ed" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000001508da06169fe6ed Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Apr 22, 2024 at 12:02=E2=80=AFAM David G. Johnston < david.g.johnston@gmail.com> wrote: > > I suggest you share a script that demonstrates exactly what you are tryin= g > to accomplish. Which event triggers you need to create from the > application and what the functions those triggers call do. > > We are using pg_partman for automatic partition maintenance however as we have foreign keys created on the tables, so the partition drop from parent is taking longer as it scans all the partitions of the child table and also locks the full child table for that duration(even SELECT query not allowed during that period). So we are thinking of creating foreign keys on partitions rather than on tables however there is no direct option for that to happen through pg_partman. So we are thinking of first creating the table without any foreign keys and creating the partitions using pg_partman, then create the below event trigger which will add the foreign key to the new partitions for all new future partitions. And we are planning to create such an event trigger for all such child tables that are partitioned and having FK's. CREATE OR REPLACE FUNCTION add_partition_foreign_key() RETURNS event_trigger LANGUAGE plpgsql AS $$ DECLARE partition_table TEXT; parent_table TEXT; partition_name TEXT; BEGIN IF TG_TAG =3D 'CREATE TABLE' THEN partition_table :=3D TG_TABLE_NAME; parent_table :=3D 'parent_table_' || to_char(NEW.partition_key, 'YYYY_MM_DD= '); EXECUTE format('ALTER TABLE %I ADD CONSTRAINT fk_%I_%I FOREIGN KEY (partition_key, id) REFERENCES %I (partition_key, id)', partition_table, partition_table, parent_table, parent_table); END IF; END; $$; CREATE EVENT TRIGGER add_partition_foreign_key_trigger ON ddl_command_end WHEN TAG IN ('CREATE TABLE') EXECUTE FUNCTION add_partition_foreign_key(); --0000000000001508da06169fe6ed Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Mon, Apr 22, 2024 at 12:02=E2=80=AFAM = David G. Johnston <david.g= .johnston@gmail.com> wrote:

=
I suggest you share a= script that demonstrates exactly what you are trying to accomplish.=C2=A0 = Which event triggers you need to create from the application and what the f= unctions those triggers call do.


We = are using=C2=A0pg_partman for automatic partition=C2=A0maintenance however = as we have foreign keys created on the tables, so the partition drop from p= arent is taking longer as it scans all the partitions of the child table an= d also locks the=C2=A0full child table for that duration(even SELECT query = not allowed during that period). So we are thinking of creating foreign key= s on partitions rather than on tables however there is no direct option for= that to happen through pg_partman.

So we are thin= king of first creating the table without any foreign keys and creating=C2= =A0the partitions using pg_partman, then create the below event trigger whi= ch will add the foreign key to the new partitions for all new future partit= ions. And we are planning to create=C2=A0such an event trigger for all such= child tables that are partitioned and having FK's.

<= /div>
CREATE OR REPLACE FUNCTION ad= d_partition_foreign_key()
RETURNS event_trigger
LANGUAGE plpgsql
A= S $$
DECLARE
partition_table TEXT;
parent_table TEXT;
partition= _name TEXT;
BEGIN
IF TG_TAG =3D 'CREATE TABLE' THEN
partit= ion_table :=3D TG_TABLE_NAME;
parent_table :=3D 'parent_table_' = || to_char(NEW.partition_key, 'YYYY_MM_DD');

EXECUTE format(= 'ALTER TABLE %I ADD CONSTRAINT fk_%I_%I FOREIGN KEY (partition_key, id)= REFERENCES %I (partition_key, id)', partition_table, partition_table, = parent_table, parent_table);
END IF;
END;
$$;

CREATE EVENT = TRIGGER add_partition_foreign_key_trigger
ON ddl_command_end
WHEN TAG= IN ('CREATE TABLE')
EXECUTE FUNCTION add_partition_foreign_key(= );

--0000000000001508da06169fe6ed--