Trigger function that works with both updates and deletes?

Started by novnovalmost 19 years ago4 messagesgeneral
Jump to latest
#1novnov
novnovice@gmail.com

Most of the trigger fuctions I've written work on new and updated records
referencing NEW. etc. I will need some of the trigger functions to work with
record deletions too.

First, when a record is being deleted, OLD refers to the rec just deleted
(or about to be deleted)?

Second, while I could write two trigger functions, one dealing with
add/update, the other with deletes, it's probably neater to have a single
trigger function and have it discriminate "am I being called for a delete,
or an add/update?" I don't know how to determine the type record change.
--
View this message in context: http://www.nabble.com/Trigger-function-that-works-with-both-updates-and-deletes--tf3943732.html#a11186941
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

#2Michael Fuhr
mike@fuhr.org
In reply to: novnov (#1)
Re: Trigger function that works with both updates and deletes?

On Mon, Jun 18, 2007 at 06:07:37PM -0700, novnov wrote:

First, when a record is being deleted, OLD refers to the rec just deleted
(or about to be deleted)?

Correct.

Second, while I could write two trigger functions, one dealing with
add/update, the other with deletes, it's probably neater to have a single
trigger function and have it discriminate "am I being called for a delete,
or an add/update?" I don't know how to determine the type record change.

In PL/pgSQL you can use TG_OP. See "Trigger Procedures" in the
documentation:

http://www.postgresql.org/docs/8.2/interactive/plpgsql-trigger.html

--
Michael Fuhr

#3Harvey, Allan AC
HarveyA@OneSteel.com
In reply to: novnov (#1)
Re: Trigger function that works with both updates and deletes?

Second, while I could write two trigger functions, one dealing with
add/update, the other with deletes, it's probably neater to
have a single
trigger function and have it discriminate "am I being called
for a delete,
or an add/update?" I don't know how to determine the type
record change.

I use trigger arguments and determine what to do by:

if TG_ARGV[0] = 'value'
then
....
end if;

Allan

The material contained in this email may be confidential, privileged or copyrighted. If you are not the intended recipient, use, disclosure or copying of this information is prohibited. If you have received this document in error, please advise the sender and delete the document. Neither OneSteel nor the sender accept responsibility for any viruses contained in this email or any attachments.

#4novnov
novnovice@gmail.com
In reply to: Michael Fuhr (#2)
Re: Trigger function that works with both updates and deletes?

Perfect, thanks

Michael Fuhr wrote:

On Mon, Jun 18, 2007 at 06:07:37PM -0700, novnov wrote:

First, when a record is being deleted, OLD refers to the rec just deleted
(or about to be deleted)?

Correct.

Second, while I could write two trigger functions, one dealing with
add/update, the other with deletes, it's probably neater to have a single
trigger function and have it discriminate "am I being called for a
delete,
or an add/update?" I don't know how to determine the type record change.

In PL/pgSQL you can use TG_OP. See "Trigger Procedures" in the
documentation:

http://www.postgresql.org/docs/8.2/interactive/plpgsql-trigger.html

--
Michael Fuhr

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

--
View this message in context: http://www.nabble.com/Trigger-function-that-works-with-both-updates-and-deletes--tf3943732.html#a11189206
Sent from the PostgreSQL - general mailing list archive at Nabble.com.