pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tim Cross <theophilusx@gmail.com>
To: hmidi slim <hmidi.slim2@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: When use triggers?
Date: Fri, 18 May 2018 08:15:31 +1000
Message-ID: <877eo2nd30.fsf@gmail.com> (raw)
In-Reply-To: <CAMsqVxtxHV39JAt8+_+bLDvVXnNJuLpWqv8=fUBapSXJyVg5rg@mail.gmail.com>
References: <CAMsqVxtxHV39JAt8+_+bLDvVXnNJuLpWqv8=fUBapSXJyVg5rg@mail.gmail.com>


hmidi slim <hmidi.slim2@gmail.com> writes:

> HI,
>
> I'm working on a microservice application and I avoid using triggers
> because they will not be easy to maintain and need an experimented person
> in database administration to manage them. So I prefer to manage the work
> in the application using ORM and javascript.
> However I want to get some opinions and advices about using triggers: when
> should I use them? How to manage them when there are some problems?

I think triggers are generally best avoided. They do have a purpose, but
like regular expressions and Lisp style macros, they are abused more
often than used appropriately. When used correctly, they can help to
ensure your code is robust, clear and easy to maintain. 

The big issue with triggers is that they are really a side effect of
some other action. As such, they are obscure, easily missed, difficult
to debug and often frustrating to maintain.

In nearly 30 years of working with different databases, I've rarely
found triggers necessary. As mentioned by others in this thread, they
can be useful when you need low level auditing and like all guidelines,
there are always exceptions, but in general, they should usually be the
last choice, not the first.

Database functions on the other hand are extremely useful and probably
something more developers should take advantage of. There are far too
many applications out there which are doing things within external
application code which could be handled far more efficiently and
consistently as a database function. The challenge is in getting the
right balance.

My rule of thumb is to develop under the assumption that someone else
will have this dumped on them to maintain. I want the code to be as easy
to understand and follow as possible and I want to make it as easy to
make changes and test those changes as possible. Therefore I prefer my
code to consist of simple units of functionality which can be tested in
isolation and have a purpose which can be understood without requiring a
knowledge of hidden actions or unexpected side effects. If a function
cannot be viewed in a single screen, it is probably too big and trying
to do too many different things which should be broken up into smaller
functions. 

regards,

Tim


-- 
Tim Cross




view thread (9+ messages)  latest in thread

Message-ID: <877eo2nd30.fsf@gmail.com>
Permalink:  ../877eo2nd30.fsf@gmail.com/
Also on:    postgresql.org/message-id/877eo2nd30.fsf@gmail.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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: theophilusx@gmail.com, hmidi.slim2@gmail.com
  Subject: Re: When use triggers?
  In-Reply-To: <877eo2nd30.fsf@gmail.com>
  References: <CAMsqVxtxHV39JAt8+_+bLDvVXnNJuLpWqv8=fUBapSXJyVg5rg@mail.gmail.com> <877eo2nd30.fsf@gmail.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