Re: FUNCTION, TRIGGER and best practices

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: pgsql-sql(at)postgresql(dot)org, KeithW(at)narrowpathinc(dot)com
Cc: "PostgreSQL Novice" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: FUNCTION, TRIGGER and best practices
Date: 2004-12-17 05:43:32
Message-ID: 200412162143.32610.josh@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice pgsql-sql

Keith,

> Can triggers and functions have the same name?

Yes.

> Is this a good practice?

It can be. If the function does nothing but power the trigger, sure. Or
you can use prefixes or suffixes to distinguish them. For example, if you
had an audit trigger on the companies table, you might call the function
audit_companies_tf, the trigger audit_companies_tg.

> Where should the function and trigger be stored?

Um, in the database?

> In the same schema as the table they are connected to?

I'm not sure you CAN put the trigger in a different schema from the table.
It's a dependant object.

> I will now be attempting to expand this function to move data from two
> source tables to four or more target tables. (Different source columns go
> to different tables.) Is it possible to do a transaction/commit/rollback
> inside a function that is driven by a trigger so that if any portion of the
> transfer fails it is all rolled back?

Well, if any portion of a trigger fails, everything is rolled back; so is the
insert, for a before trigger. So that might be the way to handle the
double-trigger situation .... except they'll be in two different tables, yes?
So that'll require an explicit transaction wrapping the two inserts.

--
Josh Berkus
Aglio Database Solutions
San Francisco

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Michael Fuhr 2004-12-17 06:52:09 Re: FUNCTION, TRIGGER and best practices
Previous Message Keith Worthington 2004-12-17 04:05:20 FUNCTION, TRIGGER and best practices

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Sydney-Smith 2004-12-17 06:23:32 Table History
Previous Message Keith Worthington 2004-12-17 04:05:20 FUNCTION, TRIGGER and best practices