Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XC4zD-0000gA-FI for pgsql-sql@arkaria.postgresql.org; Tue, 29 Jul 2014 10:50:27 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XC4zC-00038e-EZ for pgsql-sql@arkaria.postgresql.org; Tue, 29 Jul 2014 10:50:26 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XC4zB-000383-AC for pgsql-sql@postgresql.org; Tue, 29 Jul 2014 10:50:25 +0000 Received: from post.officenet.no ([195.225.13.103]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XC4z7-0000Az-3i for pgsql-sql@postgresql.org; Tue, 29 Jul 2014 10:50:22 +0000 Received: from [10.47.1.10] (helo=tc7-on) by post.officenet.no with esmtp (Exim 4.76) (envelope-from ) id 1XC4z2-0002Hg-Ux; Tue, 29 Jul 2014 12:50:19 +0200 Received: from localhost ([127.0.0.1] helo=tc7-on) by tc7-on with esmtp (Exim 4.76) (envelope-from ) id 1XC4ye-000CZz-9s; Tue, 29 Jul 2014 12:49:52 +0200 Date: Tue, 29 Jul 2014 12:49:52 +0200 (CEST) From: Andreas Joseph Krogh To: Pavel Stehule Cc: pgsql-sql@postgresql.org Message-ID: In-Reply-To: Subject: Re: Update columns in the same table in a deferred constraint trigger MIME-Version: 1.0 X-Mailer: Visena Mail 1.9.0-SNAPSHOT X-Spam-Score: -1.0 X-Spam-Report: SpamAssasin (score=-1.0, required 5.0 ALL_TRUSTED=-1, HTML_MESSAGE=0.001) X-Pg-Spam-Score: 1.4 (+) Content-Type: multipart/related; boundary="----=_Part_36_1262428188.1406630992112" List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org ------=_Part_35_77888973.1406630992112 Content-Type: multipart/related; boundary="----=_Part_36_1262428188.1406630992112" ------=_Part_36_1262428188.1406630992112 Content-Type: multipart/alternative; boundary="----=_Part_37_786495301.1406630992158" ------=_Part_37_786495301.1406630992158 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable P=C3=A5 tirsdag 29. juli 2014 kl. 12:40:21, skrev Pavel Stehule < pavel.stehule@gmail.com >: =C2=A0 =C2=A0 20= 14-07-29 12:36=20 GMT+02:00 Andreas Joseph Krogh>:=20 P=C3=A5 tirsdag 29. juli 2014 kl. 12:27:32, skrev Pavel Stehule < pavel.stehule@gmail.com >: =C2=A0 =C2=A0 20= 14-07-29 12:21=20 GMT+02:00 Andreas Joseph Krogh>:=20 P=C3=A5 tirsdag 29. juli 2014 kl. 12:12:17, skrev Pavel Stehule < pavel.stehule@gmail.com >: =C2=A0 =C2=A0 20= 14-07-29 12:05=20 GMT+02:00 Andreas Joseph Krogh>:=20 P=C3=A5 tirsdag 29. juli 2014 kl. 12:01:48, skrev Pavel Stehule < pavel.stehule@gmail.com >: =C2=A0 =C2=A0 20= 14-07-29 11:59=20 GMT+02:00 Andreas Joseph Krogh>:=20 P=C3=A5 tirsdag 29. juli 2014 kl. 11:56:17, skrev Pavel Stehule < pavel.stehule@gmail.com >: Hi =C2=A0 2014-0= 7-29=20 11:52 GMT+02:00 Andreas Joseph Krogh>: Hi all. =C2=A0 I have this simple schema: =C2= =A0 create=20 table fisk( =C2=A0=C2=A0=C2=A0 name varchar primary key, =C2=A0=C2=A0=C2=A0 autofisk varchar ); =C2=A0 I want to update the column "autofisk" on commit based the value= of=20 "name", so I created this trigger: =C2=A0 CREATE OR REPLACE FUNCTION fisk_t= f()=20 returns TRIGGER AS $$ BEGIN =C2=A0=C2=A0=C2=A0 raise notice 'name %', NEW.name ; =C2=A0=C2=A0=C2=A0 NEW.autofisk =3D NEW.name || CURRENT_= TIMESTAMP::text; =C2=A0=C2=A0=C2=A0 RETURN NEW; END; $$ LANGUAGE plpgsql; =C2=A0 CREATE CONSTRAINT TRIGGER fisk_t AFTER INSERT = OR=20 UPDATE ON fisk DEFERRABLE INITIALLY DEFERRED =C2=A0 It should be BEFORE INS= ERT OR=20 UPDATE trigger =C2=A0 He he, yes - I know that will work, but I need the tr= igger to=20 be run as a constraint-trigger, on commit, after all the data is populated = in=20 other tables and this table. =C2=A0 It is not possible - Postgres can chang= e data=20 only before writing =C2=A0 Is there a work-around, so I in the trigger can = issue for=20 example: =C2=A0 update fisk set autofisk =3D NEW.name ||= =20 CURRENT_TIMESTAMP::text where name =3D NEW.name; =C2=A0 without it also tri= ggering the=20 trigger? =C2=A0 theoretically yes - you can disable triggers via ALTER TABL= E DISABLE=20 TRIGGER =C2=A0 but then the code will be unmaintainable. Anything else is better t= han=20 dependency in triggers. You should to think about different solution. =C2=A0 Sometimes triggers can be replaced by functions directly called fro= m=20 applications instead DML statements. =C2=A0 =C2=A0 I have tried this but th= e commit never=20 returns, I think because it recursively triggers the trigger again for that= =20 modification. =C2=A0 Will temporarily disabeling the trigger inside the tri= gger (in=20 a transaction) work? =C2=A0 I really afraid of this strategy =C2=A0 I see, = so it boils=20 down to this being impossible at the moment. I really want this to be at th= e=20 DML-level so any modification done also updates the "autofisk"-column. =C2= =A0 Are=20 there any plans to make this work, that being modifying the same table in a= =20 trigger running on it where the modification (comming form statements insid= e=20 the trigger-functino) like what I'm trying will not trigger the trigger? = =C2=A0 you=20 can use a auxiliary column with information where are from a UPDATE. This= =20 information should be used for breaking recursion. But it is not a good=20 solution. You do some too complex. =C2=A0 Why you need it? =C2=A0 Pavel =C2=A0 How would I use this auxiliary= column? As I=20 understand the WHERE-condition in the trigger-definition is not deferred, a= nd=20 evaled only once, or is this not what you propose? Can you make an example = of=20 how to use such an auxiliary-column? =C2=A0 The reason I need this is that = I will=20 concat information from different tables based on information in the table = the=20 trigger is installed on. This information is to be updated in a column in t= he=20 same table of type "tsvector" and used for searching later. I want the=20 tsvector-column to be in the same table to be able to have a multicolumn in= dex=20 and avoid unnecessary JOIN'ing. =C2=A0 I am thinking so correct solution fo= r this=20 solution is using a function instead trigger or redesign a schem =C2=A0 I n= eed this=20 function to be called whenever *any* modification (insert or update) is don= e on=20 the main table, how do I accomplish that without using a trigger? =C2=A0 Th= anks. =C2=A0 -- Andreas Joseph Krogh CTO / Partner - Visena AS Mobile: +47 909 56 963=20 andreas@visena.com www.visena.com=20 =C2=A0 ------=_Part_37_786495301.1406630992158 Content-Type: text/html;charset=UTF-8 Content-Transfer-Encoding: quoted-printable
P=C3=A5 tirsdag 29. juli 2014 kl. 12:40:21, skrev Pavel Stehule <pavel.stehule@gmail.com>:
=C2=A0
=C2=A0
2014-07-29 12:36 GMT+02:00 Andreas Joseph Krogh = <andreas@visena.com>:
P=C3=A5 tirsdag 29. juli 2014 kl. 12:27:32, skrev Pavel Stehule <pavel.stehule@gm= ail.com>:
=C2=A0
=C2=A0
2014-07-29 12:21 GMT+02:00 Andreas Joseph Krogh = <andreas@visena.com>:
P=C3=A5 tirsdag 29. juli 2014 kl. 12:12:17, skrev Pavel Stehule <pavel.stehule@gm= ail.com>:
=C2=A0
=C2=A0
2014-07-29 12:05 GMT+02:00 Andreas Joseph Krogh = <andreas@visena.com>:
P=C3=A5 tirsdag 29. juli 2014 kl. 12:01:48, skrev Pavel Stehule <pavel.stehule@gm= ail.com>:
=C2=A0
=C2=A0
2014-07-29 11:59 GMT+02:00 Andreas Joseph Krogh = <andreas@visena.com>:
P=C3=A5 tirsdag 29. juli 2014 kl. 11:56:17, skrev Pavel Stehule <pavel.stehule@gm= ail.com>:
Hi
=C2=A0
2014-07-29 11:52 GMT+02:00 Andreas Joseph Krogh = <andreas@visena.com>:
Hi all.
=C2=A0
I have this simple schema:
=C2=A0
create table fisk(
=C2=A0=C2=A0=C2=A0 name varchar primary key,
=C2=A0=C2=A0=C2=A0 autofisk varchar
);
=C2=A0
I want to update the column "autofisk" on commit based the v= alue of "name", so I created this trigger:
=C2=A0
CREATE OR REPLACE FUNCTION fisk_tf() returns TRIGGER AS $$
BEGIN
=C2=A0=C2=A0=C2=A0 raise notice 'name %', NEW.name;
=C2=A0=C2=A0=C2=A0 NEW.autofisk =3D NEW.name || CURRENT_TIMESTAMP::text;
=C2=A0=C2=A0=C2=A0 RETURN NEW;
END;
$$ LANGUAGE plpgsql;
=C2=A0
CREATE CONSTRAINT TRIGGER fisk_t AFTER INSERT OR UPDATE ON fisk DEFERR= ABLE INITIALLY DEFERRED
=C2=A0
It should be BEFORE INSERT OR UPDATE trigger
=C2=A0
He he, yes - I know that will work, but I need the trigger to be run a= s a constraint-trigger, on commit, after all the data is populated in other= tables and this table.
=C2=A0
It is not possible - Postgres can change data only before writing
=C2=A0
Is there a work-around, so I in the trigger can issue for example:
=C2=A0
update fisk set autofisk =3D NEW.name || CURRENT_TIMESTAMP::text where name =3D NEW.name;
=C2=A0
without it also triggering the trigger?
=C2=A0
theoretically yes - you can disable triggers via ALTER TABLE DISABLE T= RIGGER
=C2=A0
but then the code will be unmaintainable. Anything else is better than= dependency in triggers. You should to think about different solution.
=C2=A0
Sometimes triggers can be replaced by functions directly called from a= pplications instead DML statements.
=C2=A0
=C2=A0
I have tried this but the commit never returns, I think because it rec= ursively triggers the trigger again for that modification.
=C2=A0
Will temporarily disabeling the trigger inside the trigger (in a trans= action) work?
=C2=A0
I really afraid of this strategy
=C2=A0
I see, so it boils down to this being impossible at the moment.
I really want this to be at the DML-level so any modification done als= o updates the "autofisk"-column.
=C2=A0
Are there any plans to make this work, that being modifying the same t= able in a trigger running on it where the modification (comming form statem= ents inside the trigger-functino) like what I'm trying will not trigger the= trigger?
=C2=A0
you can use a auxiliary column with information where are from a UPDAT= E. This information should be used for breaking recursion. But it is not a = good solution. You do some too complex.
=C2=A0
Why you need it?
=C2=A0
Pavel
=C2=A0
How would I use this auxiliary column? As I understand the WHERE-condi= tion in the trigger-definition is not deferred, and evaled only once, or is= this not what you propose? Can you make an example of how to use such an a= uxiliary-column?
=C2=A0
The reason I need this is that I will concat information from differen= t tables based on information in the table the trigger is installed on. Thi= s information is to be updated in a column in the same table of type "= tsvector" and used for searching later. I want the tsvector-column to = be in the same table to be able to have a multicolumn index and avoid unnec= essary JOIN'ing.
=C2=A0
I am thinking so correct solution for this solution is us= ing a function instead trigger or redesign a schem
=C2=A0
I need this function to be called whenever *any* modification (insert = or update) is done on the main table, how do I accomplish that without usin= g a trigger?
=C2=A0
Thanks.
=C2=A0
--
Andrea= s Joseph Krogh
CTO / Partner<= /span> - Visena AS
Mobile: +47 90= 9 56 963
=3D""
=C2=A0
------=_Part_37_786495301.1406630992158-- ------=_Part_36_1262428188.1406630992112 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 gl9smbMDsmFd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