Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fJRBn-00007P-MT for pgsql-general@arkaria.postgresql.org; Thu, 17 May 2018 22:15:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fJRBi-0002o9-Bh for pgsql-general@arkaria.postgresql.org; Thu, 17 May 2018 22:15:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fJRBi-0002nz-1F for pgsql-general@lists.postgresql.org; Thu, 17 May 2018 22:15:54 +0000 Received: from mail-pf0-x22f.google.com ([2607:f8b0:400e:c00::22f]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fJRBe-0003yg-DD for pgsql-general@postgresql.org; Thu, 17 May 2018 22:15:53 +0000 Received: by mail-pf0-x22f.google.com with SMTP id p14-v6so2738658pfh.9 for ; Thu, 17 May 2018 15:15:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:cc:subject:in-reply-to:date :message-id:mime-version; bh=MbqQ7SVkiI4Is+ZNhsOxkI+bORdMhSD0/oW2ovEzBPM=; b=PQVRyP1Uv1AQmD2LrM12McqmAGuqrjYFxDHqa/tUisdFKyHPvv1c7Ym9h+C8zJzVeR nYfuPLaccQ54fbgw3Sc7pW6Av4p4P6SZPF3WtrmGfjcrftRtpZFWnOZ/GIfR76jmLpDS yRHdnaRXB9Y6YANc+wYG5PtmOtYKL1rUYYNYPjslFIAyqkWiPBuYoZkEAifes+qe/f8J GyCj0dO/dgmHCP7p3owbVhpzkJsvUlgKAFacg8XJV0t+ShbcpLyJe104qhulSZJtSUyR LrUMyrsxKAVuXBMRXa5NQEJh6elj+Xg9nhhaumup7ghDioc11nsTfUc0p8cIkpW/c1OJ ZbEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:cc:subject :in-reply-to:date:message-id:mime-version; bh=MbqQ7SVkiI4Is+ZNhsOxkI+bORdMhSD0/oW2ovEzBPM=; b=n6cFsEu1lzjS+wAScS5SoVPi2Y/gmbXDhU0Zu7tAjiGh8VMh6cyKft2oTM10TzVjsO xaqy+FsqA6YyhCmkVGXFc8+gBmShQcPDMH1Q24+MqoJcT6msJfEgfQqfq+gVNhYb1zsh Ed46uOsTeal4WurJuLC511mVlryHcIMXvukEmgAoVkS32FvMHodfCmMDo7wvM5jWwI/0 sBsAGol2nR1d2sstXvCQ7Uxx1zA6wAVGsOlHRE3S29TG+6kvHXyOD79If7aPauev5EQV Q4tM+C5cA/c0diteZPDcj7VozXrHB2jKKu82yX1bKHMbESXKf1oC37QjyfLJW7ygEeUE OleA== X-Gm-Message-State: ALKqPwe2wHeVuTeHTPQ29cy4O5bTEcl4l/b0KMNf7IhLwlkmS20OYUgW 4qye+9hwI1772cLpFpB2tiSEvv2z X-Google-Smtp-Source: AB8JxZrZB1Md2aRbM+gYkSYJBwv+Kk1jNCIloQ/wKQxUMYMUTIb8ACI6NxK4KTf/sQ9mIej7SLd17w== X-Received: by 2002:a65:5003:: with SMTP id f3-v6mr5318639pgo.433.1526595346460; Thu, 17 May 2018 15:15:46 -0700 (PDT) Received: from tim-desktop (2001-44b8-31f2-bb00-3144-c91a-79c0-8dc3.static.ipv6.internode.on.net. [2001:44b8:31f2:bb00:3144:c91a:79c0:8dc3]) by smtp.gmail.com with ESMTPSA id a11-v6sm8632767pgn.64.2018.05.17.15.15.44 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 17 May 2018 15:15:45 -0700 (PDT) References: User-agent: mu4e 0.9.18; emacs 26.1 From: Tim Cross To: hmidi slim Cc: pgsql-general@postgresql.org Subject: Re: When use triggers? In-reply-to: Date: Fri, 18 May 2018 08:15:31 +1000 Message-ID: <877eo2nd30.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk hmidi slim 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