Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XC5V9-0002iz-QR for pgsql-sql@arkaria.postgresql.org; Tue, 29 Jul 2014 11:23:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XC5V8-0005Tj-PH for pgsql-sql@arkaria.postgresql.org; Tue, 29 Jul 2014 11:23:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XC5V7-0005TP-AA for pgsql-sql@postgresql.org; Tue, 29 Jul 2014 11:23:25 +0000 Received: from mail-pa0-x230.google.com ([2607:f8b0:400e:c03::230]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XC5V2-00023O-7f for pgsql-sql@postgresql.org; Tue, 29 Jul 2014 11:23:23 +0000 Received: by mail-pa0-f48.google.com with SMTP id et14so12199688pad.21 for ; Tue, 29 Jul 2014 04:23:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=AqfcF8VdxYroT2pyXXgSuyCECiK0djUvfcLCr/TyJY8=; b=l7i/YprrqQtJ5hXJpQgMIelyBbya5qCyeblk+3luZoOL/jrPHlqLWMtBzrvNMwWvGg 8EUQTUFiSJYUPf+k8gjDCHLtP1IdG79PBLPmfTEAP6h748h0gewsDx5TMXQ56xRhA1BN azw8IgzBnvEX25PX17yk0w0ejM7k4VprwZODjaXm6G6Lw5ny0nG4lLOROasrgO0YiDnR jp5M/SOLvA084orfYpqM1zS4bn51Qt9wrR3EcW9z5lFss5at1vFNXgsBbNsnlIuhX622 o2o1lzo9fCPe9hClxAmd4budWgZAU4nQAbeW+/SGesx4q2ZlRInV45YMaIqGtMHVGSAP aT9g== X-Received: by 10.66.122.67 with SMTP id lq3mr1444734pab.84.1406632996980; Tue, 29 Jul 2014 04:23:16 -0700 (PDT) MIME-Version: 1.0 Received: by 10.70.104.243 with HTTP; Tue, 29 Jul 2014 04:22:36 -0700 (PDT) In-Reply-To: References: From: Pavel Stehule Date: Tue, 29 Jul 2014 13:22:36 +0200 Message-ID: Subject: Re: Update columns in the same table in a deferred constraint trigger To: Andreas Joseph Krogh Cc: postgres list Content-Type: multipart/related; boundary=047d7b2e4e701dca2304ff5344ce X-Pg-Spam-Score: 1.3 (+) 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 --047d7b2e4e701dca2304ff5344ce Content-Type: multipart/alternative; boundary=047d7b2e4e701dca2104ff5344cd --047d7b2e4e701dca2104ff5344cd Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 2014-07-29 12:49 GMT+02:00 Andreas Joseph Krogh : > P=C3=A5 tirsdag 29. juli 2014 kl. 12:40:21, skrev Pavel Stehule < > pavel.stehule@gmail.com>: > > > > 2014-07-29 12:36 GMT+02:00 Andreas Joseph Krogh : >> >> P=C3=A5 tirsdag 29. juli 2014 kl. 12:27:32, skrev Pavel Stehule < >> pavel.stehule@gmail.com>: >> >> >> >> 2014-07-29 12:21 GMT+02:00 Andreas Joseph Krogh : >>> >>> P=C3=A5 tirsdag 29. juli 2014 kl. 12:12:17, skrev Pavel Stehule < >>> pavel.stehule@gmail.com>: >>> >>> >>> >>> 2014-07-29 12:05 GMT+02:00 Andreas Joseph Krogh : >>>> >>>> P=C3=A5 tirsdag 29. juli 2014 kl. 12:01:48, skrev Pavel Stehule < >>>> pavel.stehule@gmail.com>: >>>> >>>> >>>> >>>> 2014-07-29 11:59 GMT+02:00 Andreas Joseph Krogh : >>>>> >>>>> P=C3=A5 tirsdag 29. juli 2014 kl. 11:56:17, skrev Pavel Stehule < >>>>> pavel.stehule@gmail.com>: >>>>> >>>>> Hi >>>>> >>>>> 2014-07-29 11:52 GMT+02:00 Andreas Joseph Krogh : >>>>>> >>>>>> Hi all. >>>>>> >>>>>> I have this simple schema: >>>>>> >>>>>> create table fisk( >>>>>> name varchar primary key, >>>>>> autofisk varchar >>>>>> ); >>>>>> >>>>>> I want to update the column "autofisk" on commit based the value of >>>>>> "name", so I created this trigger: >>>>>> >>>>>> CREATE OR REPLACE FUNCTION fisk_tf() returns TRIGGER AS $$ >>>>>> BEGIN >>>>>> raise notice 'name %', NEW.name; >>>>>> NEW.autofisk =3D NEW.name || CURRENT_TIMESTAMP::text; >>>>>> RETURN NEW; >>>>>> END; >>>>>> $$ LANGUAGE plpgsql; >>>>>> >>>>>> CREATE CONSTRAINT TRIGGER fisk_t AFTER INSERT OR UPDATE ON fisk >>>>>> DEFERRABLE INITIALLY DEFERRED >>>>>> >>>>> >>>>> It should be BEFORE INSERT OR UPDATE trigger >>>>> >>>>> >>>>> He he, yes - I know that will work, but I need the trigger to be run >>>>> as a constraint-trigger, on commit, after all the data is populated i= n >>>>> other tables and this table. >>>>> >>>> >>>> It is not possible - Postgres can change data only before writing >>>> >>>> >>>> Is there a work-around, so I in the trigger can issue for example: >>>> >>>> update fisk set autofisk =3D NEW.name || CURRENT_TIMESTAMP::text where >>>> name =3D NEW.name; >>>> >>>> without it also triggering the trigger? >>>> >>> >>> theoretically yes - you can disable triggers via ALTER TABLE DISABLE >>> TRIGGER >>> >>> but then the code will be unmaintainable. Anything else is better than >>> dependency in triggers. You should to think about different solution. >>> >>> Sometimes triggers can be replaced by functions directly called from >>> applications instead DML statements. >>> >>> >>>> >>>> I have tried this but the commit never returns, I think because it >>>> recursively triggers the trigger again for that modification. >>>> >>>> Will temporarily disabeling the trigger inside the trigger (in a >>>> transaction) work? >>>> >>> >>> I really afraid of this strategy >>> >>> >>> 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 also >>> updates the "autofisk"-column. >>> >>> Are there any plans to make this work, that being modifying the same >>> table in a trigger running on it where the modification (comming form >>> statements inside the trigger-functino) like what I'm trying will not >>> trigger the trigger? >>> >> >> you can use a auxiliary column with information where are from a UPDATE. >> This information should be used for breaking recursion. But it is not a >> good solution. You do some too complex. >> >> Why you need it? >> >> Pavel >> >> >> How would I use this auxiliary column? As I understand the >> WHERE-condition in the trigger-definition is not deferred, and evaled on= ly >> once, or is this not what you propose? Can you make an example of how to >> use such an auxiliary-column? >> >> The reason I need this is that I will concat information from different >> tables based on information in the table the trigger is installed on. Th= is >> 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 b= e >> in the same table to be able to have a multicolumn index and avoid >> unnecessary JOIN'ing. >> > > I am thinking so correct solution for this solution is using a function > instead trigger or redesign a schem > > > 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 using= a > trigger? > you will call function instead DML from application everywhere and you will prohibit direct DML There are not too much possibilities :( Pavel > > Thanks. > > -- > *Andreas Joseph Krogh* > CTO / Partner - Visena AS > Mobile: +47 909 56 963 > andreas@visena.com > www.visena.com > > > --047d7b2e4e701dca2104ff5344cd Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable



2014-07-29 12:49 GMT+02:00 Andreas Joseph Krogh &= lt;andreas@visena.c= om>:
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 using a functio= n instead trigger or redesign a schem
=C2=A0
I need this function to be called whenever *any* modificat= ion (insert or update) is done on the main table, how do I accomplish that = without using a trigger?

you will cal= l function instead DML from application everywhere and you will prohibit di= rect DML

There are not too much possibilities :(

Pa= vel
=C2=A0
=C2=A0
Thanks.
=C2=A0
--
Andreas Jose= ph Krogh
CTO / Partner - Visena AS
3D""
=C2=A0

--047d7b2e4e701dca2104ff5344cd-- --047d7b2e4e701dca2304ff5344ce Content-Type: image/png Content-Disposition: inline Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: eafd0d10fbb5ad70_0.0.1 iVBORw0KGgoAAAANSUhEUgAAAIUAAAAYCAYAAADUIj6hAAAABHNCSVQICAgIfAhkiAAABzBJREFU aEPtmNFxHDcMhmVP3i1VECpvnjzkVIHWFfhcgVcVRKrAUgWRK/C6Al8H3lTgy0PGbzFdQc4VJP/H ADs43q6kROeJNbO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