Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1itZZ2-0003WN-Q3 for pgsql-general@arkaria.postgresql.org; Mon, 20 Jan 2020 16:06:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1itZZ1-0003o6-H9 for pgsql-general@arkaria.postgresql.org; Mon, 20 Jan 2020 16:06:07 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1itZZ1-0003nz-7c for pgsql-general@lists.postgresql.org; Mon, 20 Jan 2020 16:06:07 +0000 Received: from mailbackend.panix.com ([166.84.1.89]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1itZYu-0008Jj-Kr for pgsql-general@lists.postgresql.org; Mon, 20 Jan 2020 16:06:06 +0000 Received: from panix5.panix.com (panix5.panix.com [166.84.1.5]) by mailbackend.panix.com (Postfix) with ESMTP id 481c393mj3zqfL; Mon, 20 Jan 2020 11:05:57 -0500 (EST) Received: by panix5.panix.com (Postfix, from userid 17622) id 481c392g8ZzfYh; Mon, 20 Jan 2020 11:05:57 -0500 (EST) Date: Mon, 20 Jan 2020 11:05:57 -0500 From: stan To: Adrian Klaver Cc: pgsql-general@lists.postgresql.org Subject: Re: A question about rules Message-ID: <20200120160557.GA25258@panix.com> References: <20200118165325.GB21472@panix.com> <20200119103200.GA25001@panix.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Editor: gVim List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sun, Jan 19, 2020 at 08:52:36AM -0800, Adrian Klaver wrote: > On 1/19/20 2:32 AM, stan wrote: > Please post to list also. > Ccing list. > > > On Sat, Jan 18, 2020 at 08:56:06AM -0800, Adrian Klaver wrote: > > > On 1/18/20 8:53 AM, stan wrote: > > > > So, I just discovered the rules system. As I understand it, it can be used > > > > to rewrite queries before they are passed to the query processor. > > > > > > > > I was wondering if I could use this to resolve a long standing frustration > > > > of mine. I often need to declare a column as NON NULL, and create a > > > > trigger/function to auto populate it with something more complex than a > > > > simple sequence. This need arises fairly often for me, and I would like to > > > > come up with a good way to implement this. > > > > > > Stay away from rules, they will only drive you to distraction. Stick with > > > triggers they are a lot easier to understand and implement. > > > > > > > I use triggers quite a bit. My issue is that if I have a column defined as > > NOT NULL, and assign a trigger that fires a function to auto populate this > > column if the user (input form) does not. The NOT NULL constraint is > > checked BEFORE the trigger can fire, which means that I cannot properly > > provide data integrity. > > > > I don't know that this changes with rules. In other words I believe > constraints are checked first with either rules/triggers. Someone else will > need to confirm this. > > A work around is to supply a dummy DEFAULT for the the NOT NULL column e.g. > for a varchar column the string 'NOT NULL'. Test for this in the trigger and > take the appropriate action. Or accept the reality which is, you are > allowing NULL input to the column and take away the NOT NULL constraint and > look for NULL or empty string input and take the appropriate action. > Thanks, the dea of a default is an interesting one. I will consider that solution. -- "They that would give up essential liberty for temporary safety deserve neither liberty nor safety." -- Benjamin Franklin