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 1itDop-00017V-AF for pgsql-general@arkaria.postgresql.org; Sun, 19 Jan 2020 16:53:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1itDon-0005FP-9T for pgsql-general@arkaria.postgresql.org; Sun, 19 Jan 2020 16:52:57 +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 1itDol-00057D-DU for pgsql-general@lists.postgresql.org; Sun, 19 Jan 2020 16:52:56 +0000 Received: from [66.111.4.29] (helo=out5-smtp.messagingengine.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1itDof-0005Zy-8X for pgsql-general@postgresql.org; Sun, 19 Jan 2020 16:52:53 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 64B49216CA; Sun, 19 Jan 2020 11:52:37 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Sun, 19 Jan 2020 11:52:37 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:cc:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=L A8k0sYmZJLvQgXeTLsKcjegtDw2BmzU7OmW1KF34j0=; b=GxbjhQIMEY43UIcXT ttx61LclAyOsf5P8CgKLaJ1Nt3nmsYdKE7vpnuWqRXK88hVZw2Hy2NC9RQR2k2gY laYSQadZ8vzEoBhdx1vdrnFOZut7Hzu9q9Snt+9ZHx96OVP3fYwsLXhVsnnVIZPl U9jQdd96Vg2njLVumLfJJNtK3hE68cps88BtEkO8qKk88FXyF9hpeFKMeMgqoTsx vxSnwHhhFmgmmANlyYut+Tb0Yo/MYk6rP6Y3z3M/1NiUBafPNGEHeGP5wJ9plpgS F6VKWS+xCjJUtmbZuq82e4qlhrsqUw4zy4HiZXlg4jgUcYO/SRIaswKkivNm5Ts+ 8DnTg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=LA8k0sYmZJLvQgXeTLsKcjegtDw2BmzU7OmW1KF34 j0=; b=R4dfKSzvlwZs+uYh4n+4p3aY3l/6TQBY+WjDfXzWJsXAdQMPNmXpd0Zc5 rxH/2QrP/xId3s8dI+CmT0q8Rf/Ae8n3RshgkUKtADOkfN/Jw7gxuDE37mezpPLS lJZCPMLUnZKTh4iaD77KlwG1p2t58Ej/iE6gtumz9X62hhlFRcV9lGz2kM9e97cJ JEg4AkH8JeBBPRT7I0layCfSwrSBa2if4bb8NPYUbFMY4Qw/qsWbUKEGxKdRwL4b TwZKlXnGGSJE47IpBfClToAiutJmDGW0egON5RriGQJQqmZ6eQhLNCxiAZEB2J3C AfNnLxRp2/VAYbTgBRU1WA50N9www== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrudefgdelfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthejre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucfkphepjedurddvuddvrdduvdelrddule efnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhl rghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.10] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id D9CB63060B16; Sun, 19 Jan 2020 11:52:36 -0500 (EST) Subject: Re: A question about rules To: stan References: <20200118165325.GB21472@panix.com> <20200119103200.GA25001@panix.com> Cc: pgsql-general From: Adrian Klaver Message-ID: Date: Sun, 19 Jan 2020 08:52:36 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.3.1 MIME-Version: 1.0 In-Reply-To: <20200119103200.GA25001@panix.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 66.111.4.29 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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. -- Adrian Klaver adrian.klaver@aklaver.com