Re: Can I create a trigger to add another record based on the inserted record in the same table?

Lists: pgsql-general
From: Mohd Shaiza Ibrahim <mohdshaiza(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Can I create a trigger to add another record based on the inserted record in the same table?
Date: 2012-07-18 00:59:56
Message-ID: CACOE1QyYrA8JFqWA62q=u4s3dCqq=DYFLvmnCiCTzLdM=70TEA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi,

Can you guys please help me? My question sounds like this.

When I insert a new record in a table, can I create a trigger to add
another record based on the inserted record in the same table?

For example,

INSERT INTO employee (emp_id, emp_name) VALUES (0001, 'Jack');

The result:

Select * from employee;

emp_id | emp_name
0001 | Jack
0002 | Bob

I've tried running the statement below but it doesn't work. Infinite
loop i'm guessing.

--CREATE FUNCTION AS .. RETURNS TRIGGER
CREATE OR REPLACE FUNCTION add_employee_trg()
RETURNS trigger AS
$BODY$ DECLARE

BEGIN

--DELETE STATEMENT
IF tg_op = 'DELETE' THEN
INSERT INTO employee_bak(emp_id, emp_name, emp_operation)
VALUES (old.emp_id, old.emp_name, tg_op);
RETURN old;
END IF;

--INSERT STATEMENT
IF tg_op = 'INSERT' THEN
INSERT INTO employee_bak(emp_id, emp_name, emp_operation)
VALUES (new.emp_id, new.emp_name, tg_op);
RETURN new;
END IF;

--UPDATE STATEMENT
IF tg_op = 'UPDATE' THEN
INSERT INTO employee_bak(emp_id, emp_name, emp_operation)
VALUES (old.emp_id, new.emp_name, tg_op);
RETURN new;
END IF;

END

; $BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION add_employee_trg()
OWNER TO postgres;

Any help or guide would really be appreciated.

Thanks.

Shai
--
Mohd Shaiza Ibrahim


From: David Johnston <polobo(at)yahoo(dot)com>
To: Mohd Shaiza Ibrahim <mohdshaiza(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Can I create a trigger to add another record based on the inserted record in the same table?
Date: 2012-07-18 12:37:30
Message-ID: 5174807D-CA35-4CE2-B03E-77BC4A515695@yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Jul 17, 2012, at 20:59, Mohd Shaiza Ibrahim <mohdshaiza(at)gmail(dot)com> wrote:

> Hi,
>
> Can you guys please help me? My question sounds like this.
>
> When I insert a new record in a table, can I create a trigger to add
> another record based on the inserted record in the same table?
>
> For example,
>
> INSERT INTO employee (emp_id, emp_name) VALUES (0001, 'Jack');
>
> The result:
>
> Select * from employee;
>
> emp_id | emp_name
> 0001 | Jack
> 0002 | Bob
>
> I've tried running the statement below but it doesn't work. Infinite
> loop i'm guessing.
>
>

Infinite loop is correct. You need to fix your logic to solve that problem or consider a new design. Maybe restrict inserts to the table to a security definer function and put you dual insert logic into it.

David J.


From: David Johnston <polobo(at)yahoo(dot)com>
To: David Johnston <polobo(at)yahoo(dot)com>
Cc: Mohd Shaiza Ibrahim <mohdshaiza(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Can I create a trigger to add another record based on the inserted record in the same table?
Date: 2012-07-18 12:46:35
Message-ID: 43BB3545-B35F-4E21-8A7E-DEC55E32885F@yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Jul 18, 2012, at 8:37, David Johnston <polobo(at)yahoo(dot)com> wrote:

> On Jul 17, 2012, at 20:59, Mohd Shaiza Ibrahim <mohdshaiza(at)gmail(dot)com> wrote:
>
>> Hi,
>>
>> Can you guys please help me? My question sounds like this.
>>
>> When I insert a new record in a table, can I create a trigger to add
>> another record based on the inserted record in the same table?
>>
>> For example,
>>
>> INSERT INTO employee (emp_id, emp_name) VALUES (0001, 'Jack');
>>
>> The result:
>>
>> Select * from employee;
>>
>> emp_id | emp_name
>> 0001 | Jack
>> 0002 | Bob
>>
>> I've tried running the statement below but it doesn't work. Infinite
>> loop i'm guessing.
>>
>>
>
> Infinite loop is correct. You need to fix your logic to solve that problem or consider a new design. Maybe restrict inserts to the table to a security definer function and put you dual insert logic into it.
>
>

It may not be infinite trigger but you do not show the CREATE TRIGGER statement you are using so it is impossible to know. You mention same table but it appears you are trying to do audit logging which uses different tables.

You also do not say what you mean by "it doesn't work".

David J.