Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iNf4H-0000pY-JB for pgsql-general@arkaria.postgresql.org; Thu, 24 Oct 2019 15:30:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iNf4G-0002WA-2Q for pgsql-general@arkaria.postgresql.org; Thu, 24 Oct 2019 15:30:28 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iNf4F-0002W1-Bo for pgsql-general@lists.postgresql.org; Thu, 24 Oct 2019 15:30:27 +0000 Received: from post.visena.com ([46.226.10.50]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.92) (envelope-from ) id 1iNf4B-0000HG-MK for pgsql-general@lists.postgresql.org; Thu, 24 Oct 2019 15:30:26 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=visena.com; s=20141101.wh; h=Content-Type:MIME-Version:Subject:In-Reply-To:Message-ID:To:From:Date; bh=m3XfUZ5xx+TctyDVajnUi3blr+lzNYby/UBn6XDszXk=; b=bbpR58dDGWJhhS9YJQWch2Xpa97PzTXlUlMfhEFuDb/sGLh41f/qZXgWQfM7VTxUCyGssDqMlnmjIj6eoW0KyLq9kheZ6NkOVAClqeVAF4NhRk3K3Sk5B3EE4m3KPFdahCu6aX5LEi3mPkMZRn0+ZZMhmGobwjmbrGArXzD/i0E=; Received: from [10.0.1.10] (helo=tc7-visena.wh.internal.visena.com) by post.visena.com with esmtp (Exim 4.82) (envelope-from ) id 1iNf44-00026F-ER for pgsql-general@lists.postgresql.org; Thu, 24 Oct 2019 17:30:19 +0200 Received: from localhost ([127.0.0.1] helo=tc7-visena.wh.internal.visena.com) by tc7-visena.wh.internal.visena.com with esmtp (Exim 4.90_1) (envelope-from ) id 1iNf44-0007aK-CI for pgsql-general@lists.postgresql.org; Thu, 24 Oct 2019 17:30:16 +0200 Date: Thu, 24 Oct 2019 17:30:16 +0200 (CEST) From: Andreas Joseph Krogh To: pgsql-general@lists.postgresql.org Message-ID: In-Reply-To: <33ad2642-ae94-29f6-cbc6-0122894ddf26@aklaver.com> Subject: Re: Having more than one constraint trigger on a table MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_380_1887643511.1571931016241" X-Mailer: Visena Mail 2.2.153 X-Spam-Score: -1.0 X-Spam-Report: SpamAssasin (score=-1.0, required 5.0 ALL_TRUSTED=-1,HTML_MESSAGE=0.001) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_Part_380_1887643511.1571931016241 Content-Type: multipart/related; boundary="----=_Part_381_1307567252.1571931016241" ------=_Part_381_1307567252.1571931016241 Content-Type: multipart/alternative; boundary="----=_Part_382_89798839.1571931016257" ------=_Part_382_89798839.1571931016257 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable P=C3=A5 torsdag 24. oktober 2019 kl. 16:59:42, skrev Adrian Klaver < adrian.klaver@aklaver.com >:=20 On 10/22/19 8:26 AM, Andreas Joseph Krogh wrote: > P=C3=A5 tirsdag 22. oktober 2019 kl. 17:12:59, skrev Adrian Klaver > >: > > [snip] > No. > When I sort the triggers I get: > > test=3D# create table trg_str(fld_1 varchar); > CREATE TABLE > test=3D# insert into trg_str values ('trigger_1_update_fts'), > ('trigger_2'), ('trigger_3'), ('trigger_1_check_nocycle'); > INSERT 0 4 > test=3D# select * from trg_test order by fld_1 ; > id | fld_1 > ----+------- > (0 rows) > > test=3D# select * from trg_str order by fld_1 ; > fld_1 > ------------------------- > trigger_1_check_nocycle > trigger_1_update_fts > trigger_2 > trigger_3 > > Is this how you want them to fire as it does not match what you say > above?: > > (I know they were not /declared/ in that order, but..) > Yes, all "trigger_1_*" are the "actuall triggers triggering the logic", > trigger_2 and trigger_3 are only there as part of the "make > constraint-triggers fire only once"-mechanism, in which the function in > the first trigger is the function performing the actual logic. > So, being I want 2 "logical chunks" to happen I have two > "trigger_1"-triggers (there is no established terminilogy for this > AFAIK), each calling a function performing the logick which is to happen > only once (per row). > > "The first "main" trigger-function is update_company_fts_tf() ... The > second "main" trigger-function is company_parent_no_cycle()" > > It might be easier to understand if sketch out a schematic version of > what you are trying to achieve. > > The point is; I want to functions to be called > - update_company_fts_tf() > - company_parent_no_cycle() > , each only once, as constraint-triggers on the same table. So they are > called by the "level 1 triggers" which must fire first. To be clear the order they fire relative to each other is not important?= =20 Correct, these main functions may fire in any order.=20 > Is it clearer now what I'm trying to achieve? Sort of, though I am still not entirely what the whole process is trying to achieve. What the mix of deferred and un-deferred triggers and 'logical' and housekeeping functions are doing is not clear to me. That is why I suggested a schematic representation of the trigger flow would be helpful. Leave out the fine details and create a flow chart of what you want to happen.=20 Normally, CONSTRAINT TRIGGERs will fire once for each UPDATE. That means, i= f=20 you do CREATE, the 2 UPDATES, then the trigger(s) will fire 3 times for eac= h=20 row. I'm trying to make these triggers fire only ONCE per row, and at COMMI= T=20 (being CONSTRAINT TRIGGER).=20 I'm using the trick mentioned here to achieve this:=20 https://stackoverflow.com/questions/8937203/execute-deferred-trigger-only-o= nce-per-row-in-postgresql#answer-8954058 =20 But I'm trying to have more than one CONSTRAINT TRIGGER on the same table,= =20 each one doing dirfferent things and reacting (triggering) on different=20 columns, and I'm wondering if I can "re-use" the "cleanup-triggers" 2 and 3= as=20 I mentioned, having trigger 2 firing on the sum of all involved COLUMNS (na= me,=20 duns_number, parent_id) ?=20 --=20 Andreas Joseph Krogh=20 CTO / Partner - Visena AS=20 Mobile: +47 909 56 963=20 andreas@visena.com =20 www.visena.com =20 ------=_Part_382_89798839.1571931016257 Content-Type: text/html;charset=UTF-8 Content-Transfer-Encoding: quoted-printable
P=C3=A5 torsdag 24. oktober 2019 kl. 16:59:42, skrev Adrian Klaver <= ;adrian.klaver@aklaver.com= >:
On 10/22/19 8:26 AM= , Andreas Joseph Krogh wrote:
> P=C3=A5 tirsdag 22. oktober 2019 kl. 17:12:59, skrev Adrian Klaver
> <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>= >:
>
>=C2=A0 =C2=A0 =C2=A0[snip]
>=C2=A0 =C2=A0 =C2=A0No.
>=C2=A0 =C2=A0 =C2=A0When I sort the triggers I get:
>
>=C2=A0 =C2=A0 =C2=A0test=3D# create table trg_str(fld_1 varchar);
>=C2=A0 =C2=A0 =C2=A0CREATE TABLE
>=C2=A0 =C2=A0 =C2=A0test=3D# insert into trg_str values ('trigger_1_upd= ate_fts'),
>=C2=A0 =C2=A0 =C2=A0('trigger_2'), ('trigger_3'), ('trigger_1_check_noc= ycle');
>=C2=A0 =C2=A0 =C2=A0INSERT 0 4
>=C2=A0 =C2=A0 =C2=A0test=3D# select * from trg_test order by fld_1 ; >=C2=A0 =C2=A0 =C2=A0 =C2=A0 id | fld_1
>=C2=A0 =C2=A0 =C2=A0----+-------
>=C2=A0 =C2=A0 =C2=A0(0 rows)
>
>=C2=A0 =C2=A0 =C2=A0test=3D# select * from trg_str order by fld_1 ;
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0fld_1
>=C2=A0 =C2=A0 =C2=A0-------------------------
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 trigger_1_check_nocycle
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 trigger_1_update_fts
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 trigger_2
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 trigger_3
>
>=C2=A0 =C2=A0 =C2=A0Is this how you want them to fire as it does not ma= tch what you say
>=C2=A0 =C2=A0 =C2=A0above?:
>
> (I know they were not /declared/ in that order, but..)
> Yes, all "trigger_1_*" are the "actuall triggers trigge= ring the logic",
> trigger_2 and trigger_3 are only there as part of the "make
> constraint-triggers fire only once"-mechanism, in which the funct= ion in
> the first trigger is the function performing the actual logic.
> So, being I want 2 "logical chunks" to happen I have two
> "trigger_1"-triggers (there is no established terminilogy fo= r this
> AFAIK), each calling a function performing the logick which is to happ= en
> only once (per row).
>
>=C2=A0 =C2=A0 =C2=A0"The first "main" trigger-function i= s update_company_fts_tf() ... The
>=C2=A0 =C2=A0 =C2=A0second "main" trigger-function is company= _parent_no_cycle()"
>
>=C2=A0 =C2=A0 =C2=A0It might be easier to understand if sketch out a sc= hematic version of
>=C2=A0 =C2=A0 =C2=A0what you are trying to achieve.
>
> The point is; I want to functions to be called
> - update_company_fts_tf()
> - company_parent_no_cycle()
> , each only once, as constraint-triggers on the same table. So they ar= e
> called by the "level 1 triggers" which must fire first.

To be clear the order they fire relative to each other is not important?
=C2=A0
=C2=A0
Correct, these main= functions may fire in any order.
=C2=A0
=C2=A0
=C2=A0
> Is it clearer = now what I'm trying to achieve?

Sort of, though I am still not entirely what the whole process is trying to achieve. What the mix of deferred and un-deferred triggers and
'logical' and housekeeping functions are doing is not clear to me. That
is why I suggested a schematic representation of the trigger flow would
be helpful. Leave out the fine details and create a flow chart of what
you want to happen.
=C2=A0
=C2=A0
Normally, CONSTRAINT TRIGGERs will fire once=C2=A0for each=C2=A0UPDATE. That means, if you do CREATE, the 2 UPDATES, then the trigger(s)= will fire 3 times for each row. I'm trying to make these triggers fire onl= y ONCE per row, and at COMMIT (being CONSTRAINT TRIGGER).
=C2=A0
I'm using the trick mentioned here to achieve this:
=C2=A0
">
But I'm trying to have more than one CONSTRAINT TRIGGER on the same ta= ble, each one doing dirfferent things and reacting (triggering)=C2=A0on dif= ferent columns,=C2=A0and I'm wondering if I can "re-use" the &quo= t;cleanup-triggers" 2 and 3 as I mentioned, having trigger 2 firing on= the sum of all involved COLUMNS (name, duns_number, parent_id)=C2=A0?
=C2=A0
--
And= reas Joseph Krogh
CTO / Partn= er=C2=A0- Visena AS
Mobile: +47= 909 56 963
------=_Part_382_89798839.1571931016257-- ------=_Part_381_1307567252.1571931016241 Content-Type: image/png Content-Transfer-Encoding: base64 Content-Disposition: inline Content-ID: iVBORw0KGgoAAAANSUhEUgAAAIUAAAAYCAYAAADUIj6hAAAABHNCSVQICAgIfAhkiAAABzBJREFU aEPtmNFxHDcMhmVP3i1VECpvnjzkVIHWFfhcgVcVRKrAUgWRK/C6Al8H3lTgy0PGbzFdQc4VJP/H ADs43q6kROeJNbOYgQACIAgCWJKng4MZ5gxUGXh0U0b+SIuF9G+EWXj2Q15vbrE/NPtk9uub7Gfd t5mBx1NhqSEo8HshjbEUtlO2yIM9tsx5dZP9rPt2MzDZFAqZE4LGcFjdsg3saQaHt7fY70X949On aS+OZidDBkavD33157L4xaw2os+Mp/DAC10l2XhOCeStj0W5ajrJL8WfCi80Xgf9vVlrhg9yRONe /f7xI2vNsIcM7JwU9o6oGyJrLT8JOA1aX9sKP4wl94bAniukEbo/n7YPmuTETzIab4Y9ZWCrKexd 8M58lxPCvvD6aihfvexbEQoPYO8NQRO0JnddGO6FJYaVsBde7cXj7KRk4LsqDxQzCYeGsJNgaXZe +JXkyGgWINq3Gp+bHNILz+wEYs5qH1eJrouNrpDXtk4O6x1IvtD40GWyJYYdkF0jIbYZlN16xygI gl9smbMDsmFdfAKb23xGB8H/mv3tOJfArs1kukk79DEPdQ5u0g1vCvvqKXIW8mZYW+F3Tg4r8HvZ kQCCLydK8EFMQCe5N8RgL9mRG0SqQFuNvdE6beSs0qPDBuCdg0+gvClso8SbTO4ki7mQzQqBrcMH QPwReg2wW8umEe/+L8T/LEzBGF9nXjzZ46s+ITHvhegW8LL39xm6App7KYL/GE+vcYnFbJiP/0aY hdiCnRC7jaj7eiK2ETInC5PRF6LYkSN0+HabF75WuT6syCyI0YkVGGMvUC33AmfZTDUEj8u6IVhu EhRUJ2U2g6UlugyNb03Hl9obHwlxJbcRzcYjO4QPjVfGFTQav4vrmp7cpMp2qbHnBxWJbisbho2Q XI6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