![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
CREATE OR REPLACE FUNCTION public.func_job_status_upd() RETURNS trigger AS ' begin insert into x_job_status values ( OLD.job_id, OLD.job_status_type_id, OLD.status_date, OLD.notes, OLD.edit_person_id, OLD.edit_date); return new; end; ' LANGUAGE 'plpgsql' VOLATILE; |
#3
| |||
| |||
|
|
Hi, I am using postgres 7.4.5 on Redhat Enterprise Linux 3. My background is really on Oracle, and I am porting a largish database over to postgres. Here is my problem: On oracle, I had a table with an "on update or delete" trigger that copied the current row out to an audit table. Works like a champ. On postgres, when I try to delete a row, all it gives back to me is "DELETE 0" and does nothing. Here is the text of the trigger: ~~~~~~~~~~ CREATE OR REPLACE FUNCTION public.func_job_status_upd() RETURNS trigger AS ' begin insert into x_job_status values ( OLD.job_id, OLD.job_status_type_id, OLD.status_date, OLD.notes, OLD.edit_person_id, OLD.edit_date); return new; |
|
end; ' LANGUAGE 'plpgsql' VOLATILE; ~~~~~~~~~~ Any help would be appreciated! Thanks, naeem |
#4
| |||
| |||
|
|
I've made a test case, and setting the trigger BEFORE DELETE doesn't delete the rows from the table (but it does execute the trigger, and it does insert the rows in the audit table), I dont' know why . |
|
Anyway, setting the trigger AFTER DELETE works ok. On Mon, 2004-10-25 at 15:56, Naeem Bari wrote: Hi, I am using postgres 7.4.5 on Redhat Enterprise Linux 3. My background is really on Oracle, and I am porting a largish database over to postgres. Here is my problem: On oracle, I had a table with an ´on update or delete¡ trigger that copied the current row out to an audit table. Works like a champ. On postgres, when I try to delete a row, all it gives back to me is ´DELETE 0¡ and does nothing. Here is the text of the trigger: ~~~~~~~~~~ CREATE OR REPLACE FUNCTION public.func_job_status_upd() RETURNS trigger AS ' begin insert into x_job_status values ( OLD.job_id, OLD.job_status_type_id, OLD.status_date, OLD.notes, OLD.edit_person_id, OLD.edit_date); return new; end; ' LANGUAGE 'plpgsql' VOLATILE; ~~~~~~~~~~ Any help would be appreciated! Thanks, naeem |
#5
| |||
| |||
|
|
CREATE OR REPLACE FUNCTION public.func_job_status_upd() RETURNS trigger AS ' begin insert into x_job_status values ( OLD.job_id, OLD.job_status_type_id, OLD.status_date, OLD.notes, OLD.edit_person_id, OLD.edit_date); return new; end; ' LANGUAGE 'plpgsql' VOLATILE; |
#6
| |||
| |||
|
|
I've made a test case, and setting the trigger BEFORE DELETE doesn't delete the rows from the table (but it does execute the trigger, and it does insert the rows in the audit table), I dont' know why . |
|
Anyway, setting the trigger AFTER DELETE works ok. On Mon, 2004-10-25 at 15:56, Naeem Bari wrote: Hi, I am using postgres 7.4.5 on Redhat Enterprise Linux 3. My background is really on Oracle, and I am porting a largish database over to postgres. Here is my problem: On oracle, I had a table with an "on update or delete" trigger that copied the current row out to an audit table. Works like a champ. On postgres, when I try to delete a row, all it gives back to me is "DELETE 0" and does nothing. Here is the text of the trigger: ~~~~~~~~~~ CREATE OR REPLACE FUNCTION public.func_job_status_upd() RETURNS trigger AS ' begin insert into x_job_status values ( OLD.job_id, OLD.job_status_type_id, OLD.status_date, OLD.notes, OLD.edit_person_id, OLD.edit_date); return new; end; ' LANGUAGE 'plpgsql' VOLATILE; ~~~~~~~~~~ Any help would be appreciated! Thanks, naeem |
#7
| |||
| |||
|
|
I understand. Makes sense. Is there anyway for my trigger function to "know" that it is being called on a delete or on an update? Because I do need to "return new" on update... and I really don't want to write 2 different functions, one for update and one for delete... |
#8
| |||
| |||
|
|
"Naeem Bari" <naeem.bari (AT) agilissystems (DOT) com> writes: CREATE OR REPLACE FUNCTION public.func_job_status_upd() RETURNS trigger AS ' begin insert into x_job_status values ( OLD.job_id, OLD.job_status_type_id, OLD.status_date, OLD.notes, OLD.edit_person_id, OLD.edit_date); return new; end; ' LANGUAGE 'plpgsql' VOLATILE; If this is a BEFORE trigger, you probably need "RETURN OLD". "NEW" will be NULL in a delete trigger, so you're returning NULL which cancels the operation. |
|
regards, tom lane ---------------------------(end of broadcast)--------------------------- TIP 2: you can get off all lists at once with the unregister command (send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org) |
#9
| |||
| |||
|
|
I understand. Makes sense. Is there anyway for my trigger function to "know" that it is being called on a delete or on an update? Because I do need to "return new" on update... and I really don't want to write 2 different functions, one for update and one for delete... I would change the trigger to fire on "after" rather than before as Jan Weick suggests, but does that mean that if the trigger fails, the transaction would be committed anyways? |
|
Thanks for your help! naeem -----Original Message----- From: Tom Lane [mailto:tgl (AT) sss (DOT) pgh.pa.us] Sent: Monday, October 25, 2004 2:48 PM To: Naeem Bari Cc: pgsql-general (AT) postgresql (DOT) org Subject: Re: [GENERAL] ON DELETE trigger blocks delete from my table "Naeem Bari" <naeem.bari (AT) agilissystems (DOT) com> writes: CREATE OR REPLACE FUNCTION public.func_job_status_upd() RETURNS trigger AS ' begin insert into x_job_status values ( OLD.job_id, OLD.job_status_type_id, OLD.status_date, OLD.notes, OLD.edit_person_id, OLD.edit_date); return new; end; ' LANGUAGE 'plpgsql' VOLATILE; If this is a BEFORE trigger, you probably need "RETURN OLD". "NEW" will be NULL in a delete trigger, so you're returning NULL which cancels the operation. regards, tom lane ---------------------------(end of broadcast)--------------------------- TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faqs/FAQ.html |
#10
| |||
| |||
|
|
Ok, a really newbie question - I think I will switch to using "after" rather than "before" - but can I modify the trigger statement without dropping the trigger function? |
![]() |
| Thread Tools | |
| Display Modes | |
| |