pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: pgsql-sql@postgresql.org
To: KeithW@narrowpathinc.com
Cc: PostgreSQL Novice <pgsql-novice@postgresql.org>
Subject: Re: FUNCTION, TRIGGER and best practices
Date: Thu, 16 Dec 2004 21:43:32 -0800
Message-ID: <200412162143.32610.josh@agliodbs.com> (raw)
In-Reply-To: <20041217040520.M87972@narrowpathinc.com>
References: <20041217040520.M87972@narrowpathinc.com>

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



view thread (3+ messages)  latest in thread

Message-ID: <200412162143.32610.josh@agliodbs.com>
Permalink:  ../200412162143.32610.josh@agliodbs.com/
Also on:    postgresql.org/message-id/200412162143.32610.josh@agliodbs.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: josh@agliodbs.com, KeithW@narrowpathinc.com, pgsql-novice@postgresql.org
  Subject: Re: FUNCTION, TRIGGER and best practices
  In-Reply-To: <200412162143.32610.josh@agliodbs.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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