pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Trigger
47+ messages / 30 participants
[nested] [flat]

* Trigger
@ 1999-12-22 15:17  soundar rajan <psrajan@yahoo.com>
  0 siblings, 0 replies; 47+ messages in thread

From: soundar rajan @ 1999-12-22 15:17 UTC (permalink / raw)
  To: Mike Beller <beller@tradeworx.com>; Ed Loehr <ELOEHR@austin.rr.com>; pgsql-general; +Cc: Thomas Reinke <reinke@e-softinc.com>; Jose Soares <jose@sferacarta.com>; pgsql-general

I'm trying to create a trigger.  The function I'm
using returns the count(*) from a table.  But, during
the creation of the trigger, I get an error stating
the function must return opaque.  I tried the same
with the function to be returning a varchar.  But,
again I get the same error. 

In simple words, it's this.  I'm trying to create a
simple trigger which just prints out a message (as
dbms_out.println() in oracle) before or after
insertion or updation on a table.  Even if it doesn't
print a message, some changes to be made on the table.
 Can some one give me a simple example which does it. 


Thanks in advance.
__________________________________________________
Do You Yahoo!?
Thousands of Stores.  Millions of Products.  All in one place.
Yahoo! Shopping: http://shopping.yahoo.com



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Trigger
@ 2000-03-07 12:49  Raigo Lukk <l950681@ttu.ee>
  0 siblings, 0 replies; 47+ messages in thread

From: Raigo Lukk @ 2000-03-07 12:49 UTC (permalink / raw)
  To: pgsql-general@hub.org

Hi

I am trying to create a trigger for a detail table to check that value
what
 is entered exists in master table.
For example into CustomerOrder table could not be enterd order for
Customer what does not exists.

How is it best done in PostgreSQL?

I believe a trigger is what I must do.
I looked the documentation in the part of triggers and there was an
example what had a part:
execute procedure procedurename
Does this mean that I should create a procedure for each trigger?
Going further I found no Create Procedure in the documentation ... so how
to create procedures in PostgreSQL?


Thanks
Raigo
raigo.lukk@ttu.ee




^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Trigger
@ 2000-03-07 12:54  Raigo Lukk <l950681@ttu.ee>
  0 siblings, 1 reply; 47+ messages in thread

From: Raigo Lukk @ 2000-03-07 12:54 UTC (permalink / raw)
  To: pgsql-general

Hi

I am trying to create a trigger for a detail table to check that value
what is entered exists in master table.
For example into CustomerOrder table could not be enterd order for
Customer what does not exists.

How is it best done in PostgreSQL?

I believe a trigger is what I must do.
I looked the documentation in the part of triggers and there was an
trigger example what had a part:
execute procedure procedurename
Does this mean that I should create a procedure for each trigger?
Going further I found no Create Procedure in the documentation ... so
 how to create procedures in PostgreSQL?


Maybe, somebody could send me some example trigger for checking detail
record value's existance in master table.

Sorry, for simple question, but I just started using PostgreSQL.


Thanks
Raigo
raigo.lukk@ttu.ee




^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* RE: [GENERAL] Trigger
@ 2000-03-07 13:11  Andrzej Mazurkiewicz <andrzej.mazurkiewicz@polkomtel.com.pl>
  0 siblings, 3 replies; 47+ messages in thread

From: Andrzej Mazurkiewicz @ 2000-03-07 13:11 UTC (permalink / raw)
  To: 'Raigo Lukk' <l950681@ttu.ee>; pgsql-general

Switch to v7.0 beta1 and use FOREIGN KEY. You will save a lot of time.
Before you really finish your development there will be 7.0 production (this
is my private estimation).
Andrzej Mazurkiewicz

> -----Original Message-----
> From:	Raigo Lukk [SMTP:l950681@ttu.ee]
> Sent:	7 marca 2000 13:55
> To:	pgsql-general@postgreSQL.org
> Subject:	[GENERAL] Trigger
> 
> Hi
> 
> I am trying to create a trigger for a detail table to check that value
> what is entered exists in master table.
> For example into CustomerOrder table could not be enterd order for
> Customer what does not exists.
> 
> How is it best done in PostgreSQL?
> 
> I believe a trigger is what I must do.
> I looked the documentation in the part of triggers and there was an
> trigger example what had a part:
> execute procedure procedurename
> Does this mean that I should create a procedure for each trigger?
> Going further I found no Create Procedure in the documentation ... so
>  how to create procedures in PostgreSQL?
> 
> 
> Maybe, somebody could send me some example trigger for checking detail
> record value's existance in master table.
> 
> Sorry, for simple question, but I just started using PostgreSQL.
> 
> 
> Thanks
> Raigo
> raigo.lukk@ttu.ee
> 
> 
> ************



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* RE: Trigger
@ 2000-03-07 13:38  Raigo Lukk <l950681@ttu.ee>
  parent: Andrzej Mazurkiewicz <andrzej.mazurkiewicz@polkomtel.com.pl>
  2 siblings, 0 replies; 47+ messages in thread

From: Raigo Lukk @ 2000-03-07 13:38 UTC (permalink / raw)
  To: ; +Cc: pgsql-general

Hi

On Tue, 7 Mar 2000, Andrzej Mazurkiewicz wrote:

> Switch to v7.0 beta1 and use FOREIGN KEY. You will save a lot of time.
> Before you really finish your development there will be 7.0 production (this
> is my private estimation).
> Andrzej Mazurkiewicz

I am now using PostgreSQL 6.5.3 in RedHat 5.2
In my case the upgrade to v.7 is not possible because I am doing it for
university project and in university computers I have no rights to 
install stuff :-(

Still, I would appreciate some examples on triggers and procedures as 
I will need to write some procedures during this project.


Raigo




^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: [GENERAL] Trigger
@ 2000-03-07 13:58  Alfred Perlstein <bright@wintelcom.net>
  parent: Raigo Lukk <l950681@ttu.ee>
  0 siblings, 0 replies; 47+ messages in thread

From: Alfred Perlstein @ 2000-03-07 13:58 UTC (permalink / raw)
  To: Raigo Lukk <l950681@ttu.ee>; +Cc: pgsql-general

* Raigo Lukk <l950681@ttu.ee> [000307 05:36] wrote:
> Hi
> 
> I am trying to create a trigger for a detail table to check that value
> what is entered exists in master table.
> For example into CustomerOrder table could not be enterd order for
> Customer what does not exists.
> 
> How is it best done in PostgreSQL?
> 
> I believe a trigger is what I must do.
> I looked the documentation in the part of triggers and there was an
> trigger example what had a part:
> execute procedure procedurename
> Does this mean that I should create a procedure for each trigger?
> Going further I found no Create Procedure in the documentation ... so
>  how to create procedures in PostgreSQL?
> 
> 
> Maybe, somebody could send me some example trigger for checking detail
> record value's existance in master table.
> 
> Sorry, for simple question, but I just started using PostgreSQL.

I'm pretty new to postgresql as well, but i think what you want to
look at is the RULES section of the documentation, it seems that
would be what you're looking for.

I haven't done it myself, but there's some decent examples there.

-- 
-Alfred Perlstein - [bright@wintelcom.net|alfred@freebsd.org]



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: [GENERAL] Trigger
@ 2000-03-07 14:36  Bruce Momjian <pgman@candle.pha.pa.us>
  parent: Andrzej Mazurkiewicz <andrzej.mazurkiewicz@polkomtel.com.pl>
  2 siblings, 0 replies; 47+ messages in thread

From: Bruce Momjian @ 2000-03-07 14:36 UTC (permalink / raw)
  To: Andrzej Mazurkiewicz <andrzej.mazurkiewicz@polkomtel.com.pl>; +Cc: 'Raigo Lukk' <l950681@ttu.ee>; pgsql-general; wieck@debis.com

> Switch to v7.0 beta1 and use FOREIGN KEY. You will save a lot of time.
> Before you really finish your development there will be 7.0 production (this
> is my private estimation).
> Andrzej Mazurkiewicz
> 
> > I am trying to create a trigger for a detail table to check that value
> > what is entered exists in master table.
> > For example into CustomerOrder table could not be enterd order for
> > Customer what does not exists.


    Not only that he'll save some time. Without the deferred trigger
    queue manager, watching that one and the same row cannot be modified
    twice inside of one transaction (WRT key changes), it's possible to
    fool the trigger and end up with inconsistent data in a multiuser
    environment.

    And don't forget that the INSERT of the FK and the DELETE of the PK
    can happen simultaneously. Due to MVCC, both transactions don't
    know about each other (thanks to Vadim).


Jan from Philly

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2000-03-07 18:04  Raigo Lukk <l950681@ttu.ee>
  parent: Andrzej Mazurkiewicz <andrzej.mazurkiewicz@polkomtel.com.pl>
  2 siblings, 0 replies; 47+ messages in thread

From: Raigo Lukk @ 2000-03-07 18:04 UTC (permalink / raw)
  To: pgsql-general

Hi

>     twice inside of one transaction (WRT key changes), it's possible to
>     fool the trigger and end up with inconsistent data in a multiuser
>     environment.

OK let's assume that customer is not deleted while order is 
enetered. At my work I have used InterBase quite a lot. Up to IB 5 
there was also no foreign keys, so it had to be done with triggers 
just like in PostgreSQL.  So when using IB 4 I usually did 
something like this:

CREATE EXCEPTION CHILD_INSERT_RESTRICT "Cannot 
INSERT ORDER because CUSTOMER does not exist in 
CUSTOMER table";


CREATE TRIGGER BI_ORDER FOR ORDER BEFORE INSERT 
AS
  
DECLARE VARIABLE numrows INTEGER;
BEGIN    
    select count(distinct CustomerID)
      from customer
      where CustomerID=NEW.CustomerID 
      into numrows;
    IF (numrows = 0 ) THEN
    BEGIN
      EXCEPTION CHILD_INSERT_RESTRICT;
    END
  
END

I think there is some small syntax differences in PostgreSQL. 
So, can somebody please put this into PostgreSQL syntax for me?
I searched the manuals but found no Exeption ... is it possible to 
use Exceptions in PostgreSQL?
Also, does there excist special aliases like NEW and OLD in 
PostgreSQL?


Thanks
Raigo



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2000-03-08 13:08  Raigo Lukk <l950681@ttu.ee>
  0 siblings, 0 replies; 47+ messages in thread

From: Raigo Lukk @ 2000-03-08 13:08 UTC (permalink / raw)
  To: pgsql-general@hub.org

Hi

I sent this message already, but did not get it back from list. So I 
aplogise if somebody gets it two times.

>     twice inside of one transaction (WRT key changes), it's possible to
>     fool the trigger and end up with inconsistent data in a multiuser
>     environment.

OK let's assume that customer is not deleted while order is 
enetered. At my work I have used InterBase quite a lot. Up to IB 5 
there was also no foreign keys, so it had to be done with triggers 
just like in PostgreSQL.  So when using IB 4 I usually did 
something like this:

CREATE EXCEPTION CHILD_INSERT_RESTRICT "Cannot 
INSERT ORDER because CUSTOMER does not exist in 
CUSTOMER table";


CREATE TRIGGER BI_ORDER FOR ORDER BEFORE INSERT 
AS
  
DECLARE VARIABLE numrows INTEGER;
BEGIN    
    select count(distinct CustomerID)
      from customer
      where CustomerID=NEW.CustomerID 
      into numrows;
    IF (numrows = 0 ) THEN
    BEGIN
      EXCEPTION CHILD_INSERT_RESTRICT;
    END
  
END

I think there is some small syntax differences in PostgreSQL. 
So, can somebody please put this into PostgreSQL syntax for me?
I searched the manuals but found no Exeption ... is it possible to 
use Exceptions in PostgreSQL?
Also, does there excist special aliases like NEW and OLD in 
PostgreSQL?


Thanks
Raigo



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* trigger
@ 2000-12-14 05:04  Sandeep Joshi <sjoshi@Zambeel.com>
  0 siblings, 0 replies; 47+ messages in thread

From: Sandeep Joshi @ 2000-12-14 05:04 UTC (permalink / raw)
  To: pgsql-general

Hi,
   Is it possible to turn off trigger?

Sandeep




^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Trigger
@ 2000-12-15 16:36  Manika dey <bithi@ipr.res.in>
  0 siblings, 1 reply; 47+ messages in thread

From: Manika dey @ 2000-12-15 16:36 UTC (permalink / raw)
  To: postgres-inter <pgsql-interfaces@postgresql.org>; +Cc: pgsql-hackers@postgresql.org; pgsql-general


Hi,
Can anyone help me .
I want to know how 
to write triggers.
i am using java and postgresql
 



 
-------------------------------------------------------------------

From:-                          | 
 Ms. Manika Dey.                |Ph.No:--
 Engineer-SC (Comp. Tech.)      | IPR -- 02712 - 69276 
 I.P.R                          |          EXT 336,315
 BHAT, GANDHINAGAR              | Residence -- 079 - 6619967
 Gujrat -- 382 428              | FAX --- 69017
  ------------------------------------------------------------------ 




                                       
                






^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: [HACKERS] Trigger
@ 2000-12-18 07:37  Karel Zak <zakkr@zf.jcu.cz>
  parent: Manika dey <bithi@ipr.res.in>
  0 siblings, 1 reply; 47+ messages in thread

From: Karel Zak @ 2000-12-18 07:37 UTC (permalink / raw)
  To: Manika dey <bithi@ipr.res.in>; +Cc: postgres-inter <pgsql-interfaces@postgresql.org>; pgsql-hackers@postgresql.org; pgsql-general


On Fri, 15 Dec 2000, Manika dey wrote:

> 
> Hi,
> Can anyone help me .
> I want to know how 
> to write triggers.
> i am using java and postgresql

 not probably in java.... you can write function in some
"internal-interpreted-language": C, Perl, Tcl, SQL, PL/SQL

 BTW, What is bad on PostgreSQL's docs?

					Karel

PS. -hackers: What happen with PL/Python? Before 1/2 of year I ask if 
    anyone works on this and answer was: "yes, but 'he' is waiting for new 
    fmgr design". Tom's fmgr is done... IMHO it's big worse - The Python 
    has very good design for integration to other programs.




^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Re: [HACKERS] Trigger
@ 2000-12-20 02:48  Bruce Momjian <pgman@candle.pha.pa.us>
  parent: Karel Zak <zakkr@zf.jcu.cz>
  0 siblings, 0 replies; 47+ messages in thread

From: Bruce Momjian @ 2000-12-20 02:48 UTC (permalink / raw)
  To: Karel Zak <zakkr@zf.jcu.cz>; +Cc: Manika dey <bithi@ipr.res.in>; postgres-inter <pgsql-interfaces@postgresql.org>; pgsql-hackers@postgresql.org; pgsql-general

> PS. -hackers: What happen with PL/Python? Before 1/2 of year I ask if 
>     anyone works on this and answer was: "yes, but 'he' is waiting for new 
>     fmgr design". Tom's fmgr is done... IMHO it's big worse - The Python 
>     has very good design for integration to other programs.

Good question.  I don't remember this old message, though.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* TRIGGER
@ 2001-01-18 03:11  HC Mercy T. de Vera <mercy@edsamail.com>
  0 siblings, 1 reply; 47+ messages in thread

From: HC Mercy T. de Vera @ 2001-01-18 03:11 UTC (permalink / raw)
  To: pgsql-general


I’m having a problem with creating triggers…

Here’s my code:

CREATE FUNCTION copy_email(varchar)
RETURNS varchar
AS ‘SELECT email AS email INTO temp_agegroup FROM basic_information;’
LANGUAGE ‘sql’;

OUPTPUT:  CREATE  <---  successful, right?



CREATE TRIGGER trig_test
AFTER INSERT OR UPDATE
ON basic_information
FOR EACH ROW
EXECUTE PROCEDURE copy_email(); <--- also tried copy_email(varchar),
copy_email(test), copy_email(‘test’)

OUTPUT:   ERROR: function copy_email does not exist


Please help me….
Thanks!

^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: TRIGGER
@ 2001-01-18 14:39  rob <rob@cabrion.com>
  parent: HC Mercy T. de Vera <mercy@edsamail.com>
  0 siblings, 0 replies; 47+ messages in thread

From: rob @ 2001-01-18 14:39 UTC (permalink / raw)
  To: HC Mercy T. de Vera <mercy@edsamail.com>; pgsql-general

The docs are not very clear, but I think that trigger functions must all
return opaque.

--rob

----- Original Message -----
From: "HC Mercy T. de Vera" <mercy@edsamail.com>
To: <pgsql-general@postgresql.org>
Sent: Wednesday, January 17, 2001 10:11 PM
Subject: TRIGGER


>
> I'm having a problem with creating triggers.
>
> Here's my code:
>
> CREATE FUNCTION copy_email(varchar)
> RETURNS varchar
> AS 'SELECT email AS email INTO temp_agegroup FROM basic_information;'
> LANGUAGE 'sql';
>
> OUPTPUT:  CREATE  <---  successful, right?
>
>
>
> CREATE TRIGGER trig_test
> AFTER INSERT OR UPDATE
> ON basic_information
> FOR EACH ROW
> EXECUTE PROCEDURE copy_email(); <--- also tried copy_email(varchar),
> copy_email(test), copy_email('test')
>
> OUTPUT:   ERROR: function copy_email does not exist
>
>
> Please help me..
> Thanks!
>
>




^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* trigger
@ 2001-04-17 20:17  mgermoni@uniautomation.it
  0 siblings, 1 reply; 47+ messages in thread

From: mgermoni@uniautomation.it @ 2001-04-17 20:17 UTC (permalink / raw)
  To: pgsql-general

Does anyone can tell where I can find doc on the plpgsql for trigger 
and function? 
TX
==============================
Marco GERMONI            E-mail:mgermoni@uniautomation.it
Field Engineering Manager
Uniautomation S.r.l.               tel.+39-06-514221
Via A. Di Bonaiuto, 39             fax +39-06-51422311
I-00142 Roma Italy                 GSM +39-348-9997186

Internet: www.uniautomation.it
==============================



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: trigger
@ 2001-04-18 06:54  Richard Huxton <dev@archonet.com>
  parent: mgermoni@uniautomation.it
  0 siblings, 0 replies; 47+ messages in thread

From: Richard Huxton @ 2001-04-18 06:54 UTC (permalink / raw)
  To: mgermoni@uniautomation.it; +Cc: pgsql-general

mgermoni@uniautomation.it wrote:
> 
> Does anyone can tell where I can find doc on the plpgsql for trigger
> and function?

See the section on procedural languages in the programmer's guide
(http://www.postgresql.org/users-lounge/docs/7.1/programmer/). Also
check Bruce Momijan's book (online at
http://www.postgresql.org/docs/aw_pgsql_book/) and perhaps look in
http://techdocs.postgresql.org/ at some examples in Roberto Mello's
cookbook or in my notes.

HTH

- Richard Huxton



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Trigger
@ 2001-06-29 05:38  Andy Samuel <andysamuel@geocities.com>
  0 siblings, 1 reply; 47+ messages in thread

From: Andy Samuel @ 2001-06-29 05:38 UTC (permalink / raw)
  To: pgsql-general

How do I create a trigger on a table if a certain row is updated ?
For example, a table called DefaultValue with :
ModuleId char(10),
FieldId char(10),
DefVal char(3)

If I update the table 'update DefaultValue set defval='OFF'  where
FieldId='MODULESTATUS' then it will fire the event.

Thank you in advance
Andy






^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2001-06-29 08:54  Feite Brekeveld <feite.brekeveld@osiris-it.nl>
  parent: Andy Samuel <andysamuel@geocities.com>
  0 siblings, 1 reply; 47+ messages in thread

From: Feite Brekeveld @ 2001-06-29 08:54 UTC (permalink / raw)
  To: Andy Samuel <andysamuel@geocities.com>; +Cc: pgsql-general

Andy Samuel wrote:

> How do I create a trigger on a table if a certain row is updated ?
> For example, a table called DefaultValue with :
> ModuleId char(10),
> FieldId char(10),
> DefVal char(3)
>
> If I update the table 'update DefaultValue set defval='OFF'  where
> FieldId='MODULESTATUS' then it will fire the event.

Here is an example of one I use

CREATE FUNCTION "update_seqno" ( ) RETURNS opaque AS '
BEGIN
   new.seqno = nextval(''cdrseqno'');
   new.status = ''U'';
   return new;
END;' LANGUAGE 'plpgsql';

CREATE TRIGGER "updseq_no" BEFORE INSERT ON "accounting"  FOR EACH ROW
EXECUTE PROCEDURE "update_seqno" ();

seqno and status are attributes of the accounting-table that do not get set
by the insert statement I use, so the trigger provides the sequence number
from the defined sequence 'cdrseqno' and sets a default status on it.

You can define triggers on [ BEFORE/AFTER ] [ INSERT/DELETE/UPDATE ] .
This all is documented in the docs that come with PostgreSQL. Pay attention
to the double ' when you create functions/triggers.

Regards,

Feite

>
>
> Thank you in advance
> Andy
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html

--
Feite Brekeveld
feite.brekeveld@osiris-it.nl
http://www.osiris-it.nl






^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* RE: Trigger
@ 2001-06-29 14:29  Jeff Eckermann <jeckermann@verio.net>
  0 siblings, 0 replies; 47+ messages in thread

From: Jeff Eckermann @ 2001-06-29 14:29 UTC (permalink / raw)
  To: 'Andy Samuel' <andysamuel@geocities.com>; pgsql-general

Use conditional logic in your function:

CREATE FUNCTION set_defval ()
RETURNS opaque AS '
BEGIN
IF NEW.fieldid = ''MODULESTATUS''
THEN NEW.defval := ''OFF'';
END IF;
RETURN NEW;
END;
' LANGUAGE 'plpgsql';

CREATE TRIGGER default_value_set_defval
BEFORE UPDATE ON default_value
FOR EACH ROW EXECUTE PROCEDURE set_defval();

The trigger will fire for all updates, but will be a no-op for all cases
which don't meet your criterion.
This will create a small performance penalty, but probably not enough to be
noticeable.  
I don't think there is a way to have a trigger fire selectively on a given
event.
HTH



> -----Original Message-----
> From:	Andy Samuel [SMTP:andysamuel@geocities.com]
> Sent:	Friday, June 29, 2001 12:39 AM
> To:	pgsql-general@postgresql.org
> Subject:	[GENERAL] Trigger
> 
> How do I create a trigger on a table if a certain row is updated ?
> For example, a table called DefaultValue with :
> ModuleId char(10),
> FieldId char(10),
> DefVal char(3)
> 
> If I update the table 'update DefaultValue set defval='OFF'  where
> FieldId='MODULESTATUS' then it will fire the event.
> 
> Thank you in advance
> Andy
> 
> 
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
> 
> http://www.postgresql.org/users-lounge/docs/faq.html



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2001-06-29 14:29  Thalis A. Kalfigopoulos <thalis@cs.pitt.edu>
  parent: Feite Brekeveld <feite.brekeveld@osiris-it.nl>
  0 siblings, 0 replies; 47+ messages in thread

From: Thalis A. Kalfigopoulos @ 2001-06-29 14:29 UTC (permalink / raw)
  To: Feite Brekeveld <feite.brekeveld@osiris-it.nl>; +Cc: Andy Samuel <andysamuel@geocities.com>; pgsql-general

On Fri, 29 Jun 2001, Feite Brekeveld wrote:

> Andy Samuel wrote:
> 
> > How do I create a trigger on a table if a certain row is updated ?
> > For example, a table called DefaultValue with :
> > ModuleId char(10),
> > FieldId char(10),
> > DefVal char(3)
> >
> > If I update the table 'update DefaultValue set defval='OFF'  where
> > FieldId='MODULESTATUS' then it will fire the event.
> 
> Here is an example of one I use
> 
> CREATE FUNCTION "update_seqno" ( ) RETURNS opaque AS '
> BEGIN
>    new.seqno = nextval(''cdrseqno'');
>    new.status = ''U'';
>    return new;
> END;' LANGUAGE 'plpgsql';
> 
> CREATE TRIGGER "updseq_no" BEFORE INSERT ON "accounting"  FOR EACH ROW
> EXECUTE PROCEDURE "update_seqno" ();

You don't have to use a trigger in your case. You could instead just set the default values for the fields that the trigger fills in. Since you didn't do it upon table creation, use ALTER TABLE to do it now and save on the trigger time.

cheers,
thalis

> 
> seqno and status are attributes of the accounting-table that do not get set
> by the insert statement I use, so the trigger provides the sequence number
> from the defined sequence 'cdrseqno' and sets a default status on it.
> 
> You can define triggers on [ BEFORE/AFTER ] [ INSERT/DELETE/UPDATE ] .
> This all is documented in the docs that come with PostgreSQL. Pay attention
> to the double ' when you create functions/triggers.
> 
> Regards,
> 
> Feite
> 
> >
> >
> > Thank you in advance
> > Andy
> >
> > ---------------------------(end of broadcast)---------------------------
> > TIP 5: Have you checked our extensive FAQ?
> >
> > http://www.postgresql.org/users-lounge/docs/faq.html
> 
> --
> Feite Brekeveld
> feite.brekeveld@osiris-it.nl
> http://www.osiris-it.nl
> 
> 
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
> 




^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Trigger
@ 2002-11-28 20:35  Adrian Klaver <aklaver@attbi.com>
  0 siblings, 1 reply; 47+ messages in thread

From: Adrian Klaver @ 2002-11-28 20:35 UTC (permalink / raw)
  To: pgsql-general

I have created the following function:

CREATE OR REPLACE FUNCTION validate_tag_number() 
	RETURNS OPAQUE AS '
    BEGIN
	IF new.tag_number=old.tag_number THEN
	RAISE EXCEPTION " number is already present";
	END IF;
	RETURN NEW;
    END;
	'LANGUAGE 'plpgsql';

 I used it to create a trigger as follows;

	CREATE TRIGGER  validate_tag_number BEFORE UPDATE ON tag_vendor FOR EACH ROW 		      	
	EXECUTE PROCEDURE validate_tag_number();

If I do an update with a tag_number that does not exist everything is fine.
If  I do an update with a preexisting number I get the following messages-
NOTICE: plpgsql:ERROR during compile of validate_tag_number line 3
ERROR: unterminated " in name "
I was expecting to get the "number is already present" error messsage.
Where is my error?

	Thank you
	Adrian Klaver



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2002-11-28 21:04  Tariq Muhammad <tmuhamma@libertyrms.com>
  parent: Adrian Klaver <aklaver@attbi.com>
  0 siblings, 1 reply; 47+ messages in thread

From: Tariq Muhammad @ 2002-11-28 21:04 UTC (permalink / raw)
  To: Adrian Klaver <aklaver@attbi.com>; +Cc: pgsql-general


On Thu, 28 Nov 2002, Adrian Klaver wrote:

>       RAISE EXCEPTION " number is already present";

Try it as under

        RAISE EXCEPTION ''number is already present'';

Tariq Muhammad
Liberty RMS
tariq@libertyrms.info
v:416-646-3304 x 111
c:416-993-1859
p:416-381-1457





^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2002-11-28 21:12  Adrian Klaver <aklaver@attbi.com>
  parent: Tariq Muhammad <tmuhamma@libertyrms.com>
  0 siblings, 0 replies; 47+ messages in thread

From: Adrian Klaver @ 2002-11-28 21:12 UTC (permalink / raw)
  To: Tariq Muhammad <tmuhamma@libertyrms.com>; +Cc: pgsql-general

On Thursday 28 November 2002 01:04 pm, Tariq Muhammad wrote:
Thank you. By focusing on the quotes you made me realize I was using double 
quotes instead of escaped single quotes. 
> On Thu, 28 Nov 2002, Adrian Klaver wrote:
> >       RAISE EXCEPTION " number is already present";
>
> Try it as under
>
>         RAISE EXCEPTION ''number is already present'';
>
> Tariq Muhammad
> Liberty RMS
> tariq@libertyrms.info
> v:416-646-3304 x 111
> c:416-993-1859
> p:416-381-1457
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)




^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Trigger
@ 2003-10-22 13:01  Peter Childs <blue.dragon@blueyonder.co.uk>
  0 siblings, 1 reply; 47+ messages in thread

From: Peter Childs @ 2003-10-22 13:01 UTC (permalink / raw)
  To: pgsql-general

	Is it possible to deferr a trigger until commit, Or to have the
trigger not occur if the transaction is rolled back? Like transaction.
	I think its possible since constraints use triggers and if so why
is this a standard feature.
	Also is there anyway of seeing what triggers exsist and what they
do? (psql \<somthing> or the like)

Peter Childs



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2003-10-22 19:07  scott.marlowe <scott.marlowe@ihs.com>
  parent: Peter Childs <blue.dragon@blueyonder.co.uk>
  0 siblings, 2 replies; 47+ messages in thread

From: scott.marlowe @ 2003-10-22 19:07 UTC (permalink / raw)
  To: Peter Childs <blue.dragon@blueyonder.co.uk>; +Cc: pgsql-general

On Wed, 22 Oct 2003, Peter Childs wrote:

> 	Is it possible to deferr a trigger until commit, Or to have the
> trigger not occur if the transaction is rolled back? Like transaction.
> 	I think its possible since constraints use triggers and if so why
> is this a standard feature.
> 	Also is there anyway of seeing what triggers exsist and what they
> do? (psql \<somthing> or the like)

A trigger inside a transaction should automagically roll back should the 
transaction fail, shouldn't it?




^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2003-10-22 22:43  Peter Eisentraut <peter_e@gmx.net>
  parent: scott.marlowe <scott.marlowe@ihs.com>
  1 sibling, 0 replies; 47+ messages in thread

From: Peter Eisentraut @ 2003-10-22 22:43 UTC (permalink / raw)
  To: scott.marlowe <scott.marlowe@ihs.com>; +Cc: Peter Childs <blue.dragon@blueyonder.co.uk>; pgsql-general

scott.marlowe writes:

> A trigger inside a transaction should automagically roll back should the
> transaction fail, shouldn't it?

The database actions that the trigger possibly executed are rolled back
just like any other database actions.  But if your trigger does something
external to the database, you have a problem (that borders on
unsolvable).

-- 
Peter Eisentraut   peter_e@gmx.net




^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2003-10-23 07:16  Peter Childs <blue.dragon@blueyonder.co.uk>
  parent: scott.marlowe <scott.marlowe@ihs.com>
  1 sibling, 3 replies; 47+ messages in thread

From: Peter Childs @ 2003-10-23 07:16 UTC (permalink / raw)
  To: scott.marlowe <scott.marlowe@ihs.com>; +Cc: pgsql-general



On Wed, 22 Oct 2003, scott.marlowe wrote:

> On Wed, 22 Oct 2003, Peter Childs wrote:
>
> > 	Is it possible to deferr a trigger until commit, Or to have the
> > trigger not occur if the transaction is rolled back? Like transaction.
> > 	I think its possible since constraints use triggers and if so why
> > is this a standard feature.
> > 	Also is there anyway of seeing what triggers exsist and what they
> > do? (psql \<somthing> or the like)
>
> A trigger inside a transaction should automagically roll back should the
> transaction fail, shouldn't it?
>
>

	Only if it only affects that database. If the trigger uses C to
tell an outside app whats going on, it will not get the truth.
	Background, we are trying to get the database to tell clients when
records get updated, deleted or inserted so that they can update there
on-screen displays without having to query the database every couple of
seconds which would put an unnessary strain on the database. Hence
producing quicker respose times.

Peter Childs



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2003-10-23 11:47  Alvaro Herrera <alvherre@dcc.uchile.cl>
  parent: Peter Childs <blue.dragon@blueyonder.co.uk>
  2 siblings, 2 replies; 47+ messages in thread

From: Alvaro Herrera @ 2003-10-23 11:47 UTC (permalink / raw)
  To: Peter Childs <blue.dragon@blueyonder.co.uk>; +Cc: scott.marlowe <scott.marlowe@ihs.com>; pgsql-general

On Thu, Oct 23, 2003 at 08:16:27AM +0100, Peter Childs wrote:
> 
> 
> On Wed, 22 Oct 2003, scott.marlowe wrote:
> 
> > On Wed, 22 Oct 2003, Peter Childs wrote:
> >
> > > 	Is it possible to deferr a trigger until commit, Or to have the
> > > trigger not occur if the transaction is rolled back? Like transaction.
> 
> 	Background, we are trying to get the database to tell clients when
> records get updated, deleted or inserted so that they can update there
> on-screen displays without having to query the database every couple of
> seconds which would put an unnessary strain on the database. Hence
> producing quicker respose times.

You should probably be using an AFTER trigger ... when those get
executed, the transaction is ready to commit and will not abort (barring
any major problems, like your server go nuts or something).

But why don't you use some notifications and set up appropiate listeners
on the OSDs?  See the NOTIFY/LISTEN reference pages ... these also get
delivered during transaction commit.

-- 
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"In fact, the basic problem with Perl 5's subroutines is that they're not
crufty enough, so the cruft leaks out into user-defined code instead, by
the Conservation of Cruft Principle."  (Larry Wall, Apocalypse 6)



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2003-10-23 12:06  Peter Childs <blue.dragon@blueyonder.co.uk>
  parent: Alvaro Herrera <alvherre@dcc.uchile.cl>
  1 sibling, 1 reply; 47+ messages in thread

From: Peter Childs @ 2003-10-23 12:06 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@dcc.uchile.cl>; +Cc: Peter Childs <blue.dragon@blueyonder.co.uk>; scott.marlowe <scott.marlowe@ihs.com>; pgsql-general



On Thu, 23 Oct 2003, Alvaro Herrera wrote:

> On Thu, Oct 23, 2003 at 08:16:27AM +0100, Peter Childs wrote:
> >
> >
> > On Wed, 22 Oct 2003, scott.marlowe wrote:
> >
> > > On Wed, 22 Oct 2003, Peter Childs wrote:
> > >
> > > > 	Is it possible to deferr a trigger until commit, Or to have the
> > > > trigger not occur if the transaction is rolled back? Like transaction.
> >
> > 	Background, we are trying to get the database to tell clients when
> > records get updated, deleted or inserted so that they can update there
> > on-screen displays without having to query the database every couple of
> > seconds which would put an unnessary strain on the database. Hence
> > producing quicker respose times.
>
> You should probably be using an AFTER trigger ... when those get
> executed, the transaction is ready to commit and will not abort (barring
> any major problems, like your server go nuts or something).

	Using an After trigger but the transactions may still rollback. a
subsquent query may fail before commit or a constraint may fail.

>
> But why don't you use some notifications and set up appropiate listeners
> on the OSDs?  See the NOTIFY/LISTEN reference pages ... these also get
> delivered during transaction commit.

	Great idea shame drivers to get at these are rare. Anyway you
still need a trigger to fire the notify and these get sent when the query
is done not when its commented. hmmm


Peter Childs

>
> --
> Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
> "In fact, the basic problem with Perl 5's subroutines is that they're not
> crufty enough, so the cruft leaks out into user-defined code instead, by
> the Conservation of Cruft Principle."  (Larry Wall, Apocalypse 6)
>



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2003-10-23 13:30  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Peter Childs <blue.dragon@blueyonder.co.uk>
  2 siblings, 0 replies; 47+ messages in thread

From: Tom Lane @ 2003-10-23 13:30 UTC (permalink / raw)
  To: Peter Childs <blue.dragon@blueyonder.co.uk>; +Cc: scott.marlowe <scott.marlowe@ihs.com>; pgsql-general

Peter Childs <blue.dragon@blueyonder.co.uk> writes:
> 	Background, we are trying to get the database to tell clients when
> records get updated, deleted or inserted so that they can update there
> on-screen displays without having to query the database every couple of
> seconds which would put an unnessary strain on the database.

Use LISTEN/NOTIFY.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2003-10-23 13:44  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Peter Childs <blue.dragon@blueyonder.co.uk>
  0 siblings, 1 reply; 47+ messages in thread

From: Tom Lane @ 2003-10-23 13:44 UTC (permalink / raw)
  To: Peter Childs <blue.dragon@blueyonder.co.uk>; +Cc: Alvaro Herrera <alvherre@dcc.uchile.cl>; scott.marlowe <scott.marlowe@ihs.com>; pgsql-general

Peter Childs <blue.dragon@blueyonder.co.uk> writes:
> 	Great idea shame drivers to get at these are rare. Anyway you
> still need a trigger to fire the notify and these get sent when the query
> is done not when its commented. hmmm

But the NOTIFY isn't delivered until and unless the transaction commits.
This gets around the AFTER-trigger-can-still-roll-back problem.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2003-10-23 13:54  Stephan Szabo <sszabo@megazone.bigpanda.com>
  parent: Alvaro Herrera <alvherre@dcc.uchile.cl>
  1 sibling, 0 replies; 47+ messages in thread

From: Stephan Szabo @ 2003-10-23 13:54 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@dcc.uchile.cl>; +Cc: Peter Childs <blue.dragon@blueyonder.co.uk>; scott.marlowe <scott.marlowe@ihs.com>; pgsql-general

On Thu, 23 Oct 2003, Alvaro Herrera wrote:

> On Thu, Oct 23, 2003 at 08:16:27AM +0100, Peter Childs wrote:
> >
> >
> > On Wed, 22 Oct 2003, scott.marlowe wrote:
> >
> > > On Wed, 22 Oct 2003, Peter Childs wrote:
> > >
> > > > 	Is it possible to deferr a trigger until commit, Or to have the
> > > > trigger not occur if the transaction is rolled back? Like transaction.
> >
> > 	Background, we are trying to get the database to tell clients when
> > records get updated, deleted or inserted so that they can update there
> > on-screen displays without having to query the database every couple of
> > seconds which would put an unnessary strain on the database. Hence
> > producing quicker respose times.
>
> You should probably be using an AFTER trigger ... when those get
> executed, the transaction is ready to commit and will not abort (barring
> any major problems, like your server go nuts or something).

This is not true, actually. After triggers generally happen at the end of
statement (with the exception of deferred constraint triggers and some
wierdness with functions) and can themselves throw an exception condition
so even barring internal problems, it's unsafe to assume that an exception
won't happen after your trigger runs.



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2003-10-23 15:30  Peter Childs <blue.dragon@blueyonder.co.uk>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 47+ messages in thread

From: Peter Childs @ 2003-10-23 15:30 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Peter Childs <blue.dragon@blueyonder.co.uk>; Alvaro Herrera <alvherre@dcc.uchile.cl>; scott.marlowe <scott.marlowe@ihs.com>; pgsql-general



On Thu, 23 Oct 2003, Tom Lane wrote:

> Peter Childs <blue.dragon@blueyonder.co.uk> writes:
> > 	Great idea shame drivers to get at these are rare. Anyway you
> > still need a trigger to fire the notify and these get sent when the query
> > is done not when its commented. hmmm
>
> But the NOTIFY isn't delivered until and unless the transaction commits.
> This gets around the AFTER-trigger-can-still-roll-back problem.
>
> 			regards, tom lane
>
	Notify is also not very flexable it tells you somthing has
triggerged it not the information that a trigger is supplied with, like
what has changed to what from what.

Peter Childs



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2003-10-23 15:32  scott.marlowe <scott.marlowe@ihs.com>
  parent: Peter Childs <blue.dragon@blueyonder.co.uk>
  2 siblings, 1 reply; 47+ messages in thread

From: scott.marlowe @ 2003-10-23 15:32 UTC (permalink / raw)
  To: Peter Childs <blue.dragon@blueyonder.co.uk>; +Cc: pgsql-general

On Thu, 23 Oct 2003, Peter Childs wrote:

> 
> 
> On Wed, 22 Oct 2003, scott.marlowe wrote:
> 
> > On Wed, 22 Oct 2003, Peter Childs wrote:
> >
> > > 	Is it possible to deferr a trigger until commit, Or to have the
> > > trigger not occur if the transaction is rolled back? Like transaction.
> > > 	I think its possible since constraints use triggers and if so why
> > > is this a standard feature.
> > > 	Also is there anyway of seeing what triggers exsist and what they
> > > do? (psql \<somthing> or the like)
> >
> > A trigger inside a transaction should automagically roll back should the
> > transaction fail, shouldn't it?
> >
> >
> 
> 	Only if it only affects that database. If the trigger uses C to
> tell an outside app whats going on, it will not get the truth.
> 	Background, we are trying to get the database to tell clients when
> records get updated, deleted or inserted so that they can update there
> on-screen displays without having to query the database every couple of
> seconds which would put an unnessary strain on the database. Hence
> producing quicker respose times.

It might be more efficient and transactionally safe to write it all to a 
temp table, and have a daemon suck that data out every now and then and 
put it into another database that the feeders can interrogate as often as 
they like.  that way you still get the ease of programming a transaction 
that's all or nothing, and since the daemon only runs every minute or two 
and batches up its access, the impace of the batching should be nominal.  
Or would that introduce other problems of its own?




^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2003-10-24 08:30  Peter Childs <blue.dragon@blueyonder.co.uk>
  parent: scott.marlowe <scott.marlowe@ihs.com>
  0 siblings, 0 replies; 47+ messages in thread

From: Peter Childs @ 2003-10-24 08:30 UTC (permalink / raw)
  To: scott.marlowe <scott.marlowe@ihs.com>; +Cc: Peter Childs <blue.dragon@blueyonder.co.uk>; pgsql-general



On Thu, 23 Oct 2003, scott.marlowe wrote:
> It might be more efficient and transactionally safe to write it all to a
> temp table, and have a daemon suck that data out every now and then and
> put it into another database that the feeders can interrogate as often as
> they like.  that way you still get the ease of programming a transaction
> that's all or nothing, and since the daemon only runs every minute or two
> and batches up its access, the impace of the batching should be nominal.
> Or would that introduce other problems of its own?
>
>
	Makes the whole trigger alot more complex. Also got to work out
when to remove rows. Plus a load of programming to get notify to work
properly. Yuk.

	I think the simplest way then is going to be. That your
surgesting.

Write Trigger to Write info to table and notify each row needs a unique
id.

Listening Program reads table and records another id to say its
listening and how far its got, Waits for notify then checks the table
again and records the id. Fortinally each transaction only emits one
notify no matter how many times notify got called.

Program three looks for all ids in table below the lowest id listen and
deletes them, this should keep the table small enough to use.

	Where as my orignal plan was using a deffered after trigger emit
the data down a pipe then all the program needs to do is listen to the
pipe.

	Both plans have advantages the main oneof mine being its simple.

Peter
Trying to follow a kiss policy.



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* trigger
@ 2005-11-18 11:18  Aftab Alam <aalam@tatashare.com>
  0 siblings, 2 replies; 47+ messages in thread

From: Aftab Alam @ 2005-11-18 11:18 UTC (permalink / raw)
  To: pgsql-general

Hello,

I want to create a trigger in PostgresSQL

In trigger, Before inserting the record, if data is already in the table,
the trigger fire the mesaage that data is already there, and after that
trigger ckeck for next insert statement.

Pleae help me in this regard , kindly  reply.

Regards,

_____

Aftab Alam

^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: trigger
@ 2005-11-18 13:35  Richard Huxton <dev@archonet.com>
  parent: Aftab Alam <aalam@tatashare.com>
  1 sibling, 1 reply; 47+ messages in thread

From: Richard Huxton @ 2005-11-18 13:35 UTC (permalink / raw)
  To: aalam@tatashare.com; +Cc: pgsql-general

Aftab Alam wrote:
> Hello,
> 
> I want to create a trigger in PostgresSQL
> 
> In trigger, Before inserting the record, if data is already in the table,
> the trigger fire the mesaage that data is already there, and after that
> trigger ckeck for next insert statement.

Aftab - can you explain in some more detail. Raising an error on an 
insert will happen automatically if you violate a unique constraing.

What is this "next insert statment" you're talking about?

-- 
   Richard Huxton
   Archonet Ltd



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: trigger
@ 2005-11-18 15:05  Stefan Balzter <Email@SteBaSoft.de>
  parent: Aftab Alam <aalam@tatashare.com>
  1 sibling, 0 replies; 47+ messages in thread

From: Stefan Balzter @ 2005-11-18 15:05 UTC (permalink / raw)
  To: aalam@tatashare.com; pgsql-general

Aftab Alam schrieb:
> I want to create a trigger in PostgresSQL
> 
> In trigger, Before inserting the record, if data is already in the 
> table, the trigger fire the mesaage that data is already there, and 
> after that trigger ckeck for next insert statement.

You write a function that returns the type "trigger" and checks whether 
the data is already there (i.e. by SELECTing INTO a variable the number 
of records with your given values and checking if the variable is >0). 
If it is, let it shout out your NOTICE and RETURN NULL; if it's not, 
RETURN NEW;
Afterwards, you create the actual trigger BEFORE INSERT ON your table 
FOR EACH ROW and let it execute your recently written function :-)

Good luck,

Stefan Balzter



^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: trigger
@ 2005-11-19 07:44  Aftab Alam <aalam@tatashare.com>
  parent: Richard Huxton <dev@archonet.com>
  0 siblings, 0 replies; 47+ messages in thread

From: Aftab Alam @ 2005-11-19 07:44 UTC (permalink / raw)
  To: 'Richard Huxton' <dev@archonet.com>; +Cc: pgsql-general

Hi there,

 CREATE TABLE emp_mas
(
  emp_com_cd char(2) NOT NULL,
  emp_loc_cd numeric NOT NULL,
  emp_cd numeric NOT NULL,
  emp_sal numeric,
  emp_m_name varchar(50),
  emp_full_name varchar(50),
  emp_sex varchar(1)
)

delete from emp_mas where emp_com_cd  = 'KB' and emp_loc_cd  = 1 and emp_cd
in (1,2);
insert into emp_mas values ('KB', 1, 1,1,'aftab','aftab alam','M');
insert into emp_mas values ('KB', 1, 1,2,'amit','amit sharma','M');

in the above statement if aftab is already available in the database then
the trigger does not allowing to delete the data for the database if not
insert statement is going to fire.

wating for reply.

thanks & regrds,
aftab








-----Original Message-----
From: pgsql-general-owner@postgresql.org
[mailto:pgsql-general-owner@postgresql.org]On Behalf Of Richard Huxton
Sent: Friday, November 18, 2005 7:06 PM
To: aalam@tatashare.com
Cc: pgsql-general@postgresql.org
Subject: Re: [GENERAL] trigger


Aftab Alam wrote:
> Hello,
>
> I want to create a trigger in PostgresSQL
>
> In trigger, Before inserting the record, if data is already in the table,
> the trigger fire the mesaage that data is already there, and after that
> trigger ckeck for next insert statement.

Aftab - can you explain in some more detail. Raising an error on an
insert will happen automatically if you violate a unique constraing.

What is this "next insert statment" you're talking about?

--
   Richard Huxton
   Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
       subscribe-nomail command to majordomo@postgresql.org so that your
       message can get through to the mailing list cleanly




^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Trigger
@ 2019-10-11 10:59  Sonam Sharma <sonams1209@gmail.com>
  0 siblings, 1 reply; 47+ messages in thread

From: Sonam Sharma @ 2019-10-11 10:59 UTC (permalink / raw)
  To: pgsql-general

Can someone please help me in how to list all the triggers with their
respective tables and the trigger body

^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2019-10-11 12:00  Charles Clavadetscher <clavadetscher@swisspug.org>
  parent: Sonam Sharma <sonams1209@gmail.com>
  0 siblings, 0 replies; 47+ messages in thread

From: Charles Clavadetscher @ 2019-10-11 12:00 UTC (permalink / raw)
  To: Sonam Sharma <sonams1209@gmail.com>; +Cc: pgsql-general

Hello

On 2019-10-11 12:59, Sonam Sharma wrote:
> Can someone please help me in how to list all the triggers with their
> respective tables and the trigger body

You can use the catalogs:

SELECT t.tgname, t.tgrelid::regclass, t.tgfoid::regprocedure as 
function_name, pg_get_functiondef(t.tgfoid) as function_body FROM 
pg_trigger t WHERE NOT tgisinternal;

There is additional information in the table such as when the trigger 
fire, etc.
Have a look at: 
https://www.postgresql.org/docs/12/catalog-pg-trigger.html

Another way is querying the information schema:

SELECT * FROM information_schema.triggers;

https://www.postgresql.org/docs/12/infoschema-triggers.html
However you won't get there the function body.

Regards
Charles

-- 
Charles Clavadetscher
Swiss PostgreSQL Users Group
Treasurer
Spitzackerstrasse 9
CH - 8057 Zürich

http://www.swisspug.org

+---------------------------+
|     ____  ______  ___     |
|    /    )/      \/   \    |
|   (     / __    _\    )   |
|    \    (/ o)  ( o)   )   |
|     \_  (_  )   \ ) _/    |
|       \  /\_/    \)/      |
|        \/ <//|  |\\>      |
|             _|  |         |
|             \|_/          |
|                           |
|     Swiss PostgreSQL      |
|       Users Group         |
|                           |
+---------------------------+





^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Trigger
@ 2020-02-25 08:01  Sonam Sharma <sonams1209@gmail.com>
  0 siblings, 2 replies; 47+ messages in thread

From: Sonam Sharma @ 2020-02-25 08:01 UTC (permalink / raw)
  To: pgsql-general

I have a trigger, like many other triggers that fire after

update and checks a field of the OLD set. For some reason this trigger
throw this error:



ERROR: record "old" has no field "ivo_sts_cd" CONTEXT: SQL statement

"UPDATE dbo.T8071_CAI_IVO_HDR SET IVO_STS_CD = 1 where
T616_VBU_NBR=old.T616_VBU_NBR

and T617_FNC_TYP_CD=old.T617_FNC_TYP_CD and
T8071_CAI_IVO_ID=old.T8071_CAI_IVO_ID and

T8071_ADD_DM= old. T8071_ADD_DM and old.ivo_sts_cd != 10 and old.ivo_sts_cd
!= 3" PL/pgSQL

function dbo.t8096_upd_func02() line 9 at SQL statement SQL state: 42703

this is the trigger :

t8096_upd_trig02 AFTER UPDATE ON dbo.t8096_cai_ivo_exc FOR EACH ROW WHEN
(old.exc_rsd_idc IS DISTINCT FROM new.exc_rsd_idc) EXECUTE PROCEDURE
dbo.t8096_upd_func02()



below is the function :



CREATE OR REPLACE FUNCTION pps.t8096_upd_func02()

RETURNS trigger

LANGUAGE plpgsql

AS $function$

DECLARE

exc_count int;

BEGIN

select count(*) into exc_count from pps.T8096_CAI_IVO_EXC WHERE
T616_VBU_NBR=old.T616_VBU_NBR

and T617_FNC_TYP_CD=old.T617_FNC_TYP_CD and
T8071_CAI_IVO_ID=old.T8071_CAI_IVO_ID and T8071_ADD_DM= old.T8071_ADD_DM
and EXC_RSd_IDC = 'N'

AND T8095_EXC_TYP_CD NOT IN(8,9,10,11) ;

if exc_count = 0 then

UPDATE pps.T8071_CAI_IVO_HDR SET IVO_STS_CD = 1 where
T616_VBU_NBR=old.T616_VBU_NBR and T617_FNC_TYP_CD=old.T617_FNC_TYP_CD and

T8071_CAI_IVO_ID=old.T8071_CAI_IVO_ID and T8071_ADD_DM= old. T8071_ADD_DM
and old.ivo_sts_cd != 10 and old.ivo_sts_cd != 3;

end if;

RETURN NEW;

END

$function$;


Can someone please help where I am missing ..


Thanks,

Sonam

^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2020-02-25 16:14  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Sonam Sharma <sonams1209@gmail.com>
  1 sibling, 0 replies; 47+ messages in thread

From: Adrian Klaver @ 2020-02-25 16:14 UTC (permalink / raw)
  To: Sonam Sharma <sonams1209@gmail.com>; pgsql-general

On 2/25/20 12:01 AM, Sonam Sharma wrote:
> I have a trigger, like many other triggers that fire after
> 
> update and checks a field of the OLD set. For some reason this trigger 
> throw this error:
> 
> ERROR: record "old" has no field "ivo_sts_cd" CONTEXT: SQL statement
> 
> "UPDATE dbo.T8071_CAI_IVO_HDR SET IVO_STS_CD = 1 where 
> T616_VBU_NBR=old.T616_VBU_NBR
> 
> and T617_FNC_TYP_CD=old.T617_FNC_TYP_CD and 
> T8071_CAI_IVO_ID=old.T8071_CAI_IVO_ID and
> 
> T8071_ADD_DM= old. T8071_ADD_DM and old.ivo_sts_cd != 10 and 
> old.ivo_sts_cd != 3" PL/pgSQL
> 
> function dbo.t8096_upd_func02() line 9 at SQL statement SQL state: 42703
> 
> this is the trigger :
> 
> t8096_upd_trig02 AFTER UPDATE ON dbo.t8096_cai_ivo_exc FOR EACH ROW WHEN 
> (old.exc_rsd_idc IS DISTINCT FROM new.exc_rsd_idc) EXECUTE PROCEDURE 
> dbo.t8096_upd_func02()
> 
> below is the function :
> 
> CREATE OR REPLACE FUNCTION pps.t8096_upd_func02()
> 
> RETURNS trigger
> 
> LANGUAGE plpgsql
> 
> AS $function$
> 
> DECLARE
> 
> exc_count int;
> 
> BEGIN
> 
> select count(*) into exc_count from pps.T8096_CAI_IVO_EXC WHERE 
> T616_VBU_NBR=old.T616_VBU_NBR
> 
> and T617_FNC_TYP_CD=old.T617_FNC_TYP_CD and 
> T8071_CAI_IVO_ID=old.T8071_CAI_IVO_ID and T8071_ADD_DM= old.T8071_ADD_DM 
> and EXC_RSd_IDC = 'N'
> 
> AND T8095_EXC_TYP_CD NOT IN(8,9,10,11) ;
> 
> if exc_count = 0 then
> 
> UPDATE pps.T8071_CAI_IVO_HDR SET IVO_STS_CD = 1 where 
> T616_VBU_NBR=old.T616_VBU_NBR and T617_FNC_TYP_CD=old.T617_FNC_TYP_CD and
> 
> T8071_CAI_IVO_ID=old.T8071_CAI_IVO_ID and T8071_ADD_DM= old. 
> T8071_ADD_DM and old.ivo_sts_cd != 10 and old.ivo_sts_cd != 3;
> 
> end if;
> 
> RETURN NEW;
> 
> END
> 
> $function$;
> 
> 
> Can someone please help where I am missing ..

Does the table dbo.t8096_cai_ivo_exc have the field ivo_sts_cd?

> 
> 
> Thanks,
> 
> Sonam
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2020-02-25 16:53  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Sonam Sharma <sonams1209@gmail.com>
  1 sibling, 1 reply; 47+ messages in thread

From: Adrian Klaver @ 2020-02-25 16:53 UTC (permalink / raw)
  To: Sonam Sharma <sonams1209@gmail.com>; pgsql-general

On 2/25/20 12:01 AM, Sonam Sharma wrote:
> I have a trigger, like many other triggers that fire after
> 
> update and checks a field of the OLD set. For some reason this trigger 
> throw this error:
> 
> ERROR: record "old" has no field "ivo_sts_cd" CONTEXT: SQL statement

> if exc_count = 0 then
> 
> UPDATE pps.T8071_CAI_IVO_HDR SET IVO_STS_CD = 1 where 
> T616_VBU_NBR=old.T616_VBU_NBR and T617_FNC_TYP_CD=old.T617_FNC_TYP_CD and
> 
> T8071_CAI_IVO_ID=old.T8071_CAI_IVO_ID and T8071_ADD_DM= old. 
> T8071_ADD_DM and old.ivo_sts_cd != 10 and old.ivo_sts_cd != 3;

Realized I went through the above to quickly. I do not see a SET, nor am 
I clear what table you are trying to UPDATE.

> 
> end if;
> 
> RETURN NEW;
> 
> END
> 
> $function$;
> 
> 
> Can someone please help where I am missing ..
> 
> 
> Thanks,
> 
> Sonam
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2020-02-25 17:08  Alban Hertroys <haramrae@gmail.com>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 47+ messages in thread

From: Alban Hertroys @ 2020-02-25 17:08 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: Sonam Sharma <sonams1209@gmail.com>; pgsql-general


> On 25 Feb 2020, at 17:53, Adrian Klaver <adrian.klaver@aklaver.com> wrote:
> 
> On 2/25/20 12:01 AM, Sonam Sharma wrote:
>> I have a trigger, like many other triggers that fire after
>> update and checks a field of the OLD set. For some reason this trigger throw this error:
>> ERROR: record "old" has no field "ivo_sts_cd" CONTEXT: SQL statement
> 
>> if exc_count = 0 then
>> UPDATE pps.T8071_CAI_IVO_HDR SET IVO_STS_CD = 1 where T616_VBU_NBR=old.T616_VBU_NBR and T617_FNC_TYP_CD=old.T617_FNC_TYP_CD and
>> T8071_CAI_IVO_ID=old.T8071_CAI_IVO_ID and T8071_ADD_DM= old. T8071_ADD_DM and old.ivo_sts_cd != 10 and old.ivo_sts_cd != 3;
> 
> Realized I went through the above to quickly. I do not see a SET, nor am I clear what table you are trying to UPDATE.

I’m pretty sure that if the OP were to format their query in a more readable and consistent way, they would spot their error pretty quickly. It’s a simple typo.

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.






^ permalink  raw  reply  [nested|flat] 47+ messages in thread

* Re: Trigger
@ 2020-02-25 17:13  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Alban Hertroys <haramrae@gmail.com>
  0 siblings, 0 replies; 47+ messages in thread

From: Adrian Klaver @ 2020-02-25 17:13 UTC (permalink / raw)
  To: Alban Hertroys <haramrae@gmail.com>; +Cc: Sonam Sharma <sonams1209@gmail.com>; pgsql-general

On 2/25/20 9:08 AM, Alban Hertroys wrote:
> 
>> On 25 Feb 2020, at 17:53, Adrian Klaver <adrian.klaver@aklaver.com> wrote:
>>
>> On 2/25/20 12:01 AM, Sonam Sharma wrote:
>>> I have a trigger, like many other triggers that fire after
>>> update and checks a field of the OLD set. For some reason this trigger throw this error:
>>> ERROR: record "old" has no field "ivo_sts_cd" CONTEXT: SQL statement
>>
>>> if exc_count = 0 then
>>> UPDATE pps.T8071_CAI_IVO_HDR SET IVO_STS_CD = 1 where T616_VBU_NBR=old.T616_VBU_NBR and T617_FNC_TYP_CD=old.T617_FNC_TYP_CD and
>>> T8071_CAI_IVO_ID=old.T8071_CAI_IVO_ID and T8071_ADD_DM= old. T8071_ADD_DM and old.ivo_sts_cd != 10 and old.ivo_sts_cd != 3;
>>
>> Realized I went through the above to quickly. I do not see a SET, nor am I clear what table you are trying to UPDATE.
> 
> I’m pretty sure that if the OP were to format their query in a more readable and consistent way, they would spot their error pretty quickly. It’s a simple typo.

Yeah, throwing it at:

http://sqlformat.darold.net/

returned:

UPDATE
     pps.T8071_CAI_IVO_HDR
SET
     IVO_STS_CD = 1
WHERE
     T616_VBU_NBR = old.T616_VBU_NBR
     AND T617_FNC_TYP_CD = old.T617_FNC_TYP_CD
     AND T8071_CAI_IVO_ID = old.T8071_CAI_IVO_ID
     AND T8071_ADD_DM = old. T8071_ADD_DM
     AND old.ivo_sts_cd != 10
     AND old.ivo_sts_cd != 3;

Found the SET:)

> 
> Alban Hertroys
> --
> If you can't see the forest for the trees,
> cut the trees and you'll find there is no forest.
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 47+ messages in thread


end of thread, other threads:[~2020-02-25 17:13 UTC | newest]

Thread overview: 47+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-12-22 15:17 Trigger soundar rajan <psrajan@yahoo.com>
2000-03-07 12:49 Trigger Raigo Lukk <l950681@ttu.ee>
2000-03-07 12:54 Trigger Raigo Lukk <l950681@ttu.ee>
2000-03-07 13:58 ` Alfred Perlstein <bright@wintelcom.net>
2000-03-07 13:11 RE: [GENERAL] Trigger Andrzej Mazurkiewicz <andrzej.mazurkiewicz@polkomtel.com.pl>
2000-03-07 13:38 ` Raigo Lukk <l950681@ttu.ee>
2000-03-07 14:36 ` Bruce Momjian <pgman@candle.pha.pa.us>
2000-03-07 18:04 ` Raigo Lukk <l950681@ttu.ee>
2000-03-08 13:08 Re: Trigger Raigo Lukk <l950681@ttu.ee>
2000-12-14 05:04 trigger Sandeep Joshi <sjoshi@Zambeel.com>
2000-12-15 16:36 Trigger Manika dey <bithi@ipr.res.in>
2000-12-18 07:37 ` Karel Zak <zakkr@zf.jcu.cz>
2000-12-20 02:48   ` Bruce Momjian <pgman@candle.pha.pa.us>
2001-01-18 03:11 TRIGGER HC Mercy T. de Vera <mercy@edsamail.com>
2001-01-18 14:39 ` Re: TRIGGER rob <rob@cabrion.com>
2001-04-17 20:17 trigger mgermoni@uniautomation.it
2001-04-18 06:54 ` Re: trigger Richard Huxton <dev@archonet.com>
2001-06-29 05:38 Trigger Andy Samuel <andysamuel@geocities.com>
2001-06-29 08:54 ` Feite Brekeveld <feite.brekeveld@osiris-it.nl>
2001-06-29 14:29   ` Thalis A. Kalfigopoulos <thalis@cs.pitt.edu>
2001-06-29 14:29 RE: Trigger Jeff Eckermann <jeckermann@verio.net>
2002-11-28 20:35 Trigger Adrian Klaver <aklaver@attbi.com>
2002-11-28 21:04 ` Tariq Muhammad <tmuhamma@libertyrms.com>
2002-11-28 21:12   ` Adrian Klaver <aklaver@attbi.com>
2003-10-22 13:01 Trigger Peter Childs <blue.dragon@blueyonder.co.uk>
2003-10-22 19:07 ` scott.marlowe <scott.marlowe@ihs.com>
2003-10-22 22:43   ` Peter Eisentraut <peter_e@gmx.net>
2003-10-23 07:16   ` Peter Childs <blue.dragon@blueyonder.co.uk>
2003-10-23 11:47     ` Alvaro Herrera <alvherre@dcc.uchile.cl>
2003-10-23 12:06       ` Peter Childs <blue.dragon@blueyonder.co.uk>
2003-10-23 13:44         ` Tom Lane <tgl@sss.pgh.pa.us>
2003-10-23 15:30           ` Peter Childs <blue.dragon@blueyonder.co.uk>
2003-10-23 13:54       ` Stephan Szabo <sszabo@megazone.bigpanda.com>
2003-10-23 13:30     ` Tom Lane <tgl@sss.pgh.pa.us>
2003-10-23 15:32     ` scott.marlowe <scott.marlowe@ihs.com>
2003-10-24 08:30       ` Peter Childs <blue.dragon@blueyonder.co.uk>
2005-11-18 11:18 trigger Aftab Alam <aalam@tatashare.com>
2005-11-18 13:35 ` Re: trigger Richard Huxton <dev@archonet.com>
2005-11-19 07:44   ` Re: trigger Aftab Alam <aalam@tatashare.com>
2005-11-18 15:05 ` Re: trigger Stefan Balzter <Email@SteBaSoft.de>
2019-10-11 10:59 Trigger Sonam Sharma <sonams1209@gmail.com>
2019-10-11 12:00 ` Charles Clavadetscher <clavadetscher@swisspug.org>
2020-02-25 08:01 Trigger Sonam Sharma <sonams1209@gmail.com>
2020-02-25 16:14 ` Adrian Klaver <adrian.klaver@aklaver.com>
2020-02-25 16:53 ` Adrian Klaver <adrian.klaver@aklaver.com>
2020-02-25 17:08   ` Alban Hertroys <haramrae@gmail.com>
2020-02-25 17:13     ` Adrian Klaver <adrian.klaver@aklaver.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox