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 1h4wTC-0000y1-HS for pgsql-general@arkaria.postgresql.org; Fri, 15 Mar 2019 23:42:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h4wTB-0006Ti-8x for pgsql-general@arkaria.postgresql.org; Fri, 15 Mar 2019 23:42:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h4wTA-0006Tb-0n for pgsql-general@lists.postgresql.org; Fri, 15 Mar 2019 23:42:33 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h4wT6-0003yg-Te for pgsql-general@lists.postgresql.org; Fri, 15 Mar 2019 23:42:30 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 3B5F021FEB; Fri, 15 Mar 2019 19:42:28 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Fri, 15 Mar 2019 19:42:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=1 WB9L68kpr01319JAdWyiLZlcu5pFWdJSEKJGvG63dY=; b=Ka4cw34orrFwTLYdU n9whMboSDIdfo82QTARRJGSCWpygl7VRQaAfoe2Ps1Wj+sU/HFVEJtR2qneFoBQO m3w56xwfcBScyjg7Rf+xT3M50L4iwFgESVCqsbm2n45aaQjrODWHdTdomSCKtOZu mWAPcarO2/3eCT9qYVHyFuoRtIFA21toW8FkRiqhpHYvxZMCNCEyX5byASBOBvjY q8/cc2CDP0gYsv+2MzSh0wPnxnn+NPl2wC2WtnaoousjRcxRCGH6YsDiVDrEBGHh 2SrtRrO8GE2TmeHJc/YEKhc8FPTrcFDq8P2B9Ovd0HN7+Nazj9Pa70mJALuK8EfF 46/CQ== 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=fm2; bh=1WB9L68kpr01319JAdWyiLZlcu5pFWdJSEKJGvG63 dY=; b=3AL/lpfdIWqw3KDOgtqL/eHhHsMid/6wz/a5iXvYlhh0inKTjCZwE99de UlT0h/qXFaWhga6dbU5W8RPgIOAWwUof4UIliqb0le63jdRX3v3cAB1MuZbg2nCM 22Kxa5JYQ/9u6AH2SbISuHplPfl6clVMuCqW8nGJBpisfNJJdXoAD5LQA5t5UWPj BYVCHuVD6Hrz9ZbBpwaLPlIJvDAwR5eKmigQS0OPJrkJlbC+YWnjIhXJVNFTiKaB jffSj883QkYVT6qSRmbzjcw9W9Km3xe6VlDvUmvYkpgHv66lsVIeogaS++W+gUPK bOXXJUc+NxR1CCWNofAT+6GRFcwDg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedutddrheeigddvfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdqrghrtghhihhvvgdrohhrghdpphho shhtghhrvghsqhhlrdhorhhgpdgrghgvnhgthidqshhofhhtfigrrhgvrdhorhhgnecukf hppeejuddrvdduvddrudehtddrvddtvdenucfrrghrrghmpehmrghilhhfrhhomheprggu rhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvghrufhiii gvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id DFB9510288; Fri, 15 Mar 2019 19:42:26 -0400 (EDT) Subject: Re: Conditional INSERT To: Ken Tanzer Cc: basti , "pgsql-generallists.postgresql.org" References: <68951fae-3281-c9ad-09b7-205e0bb1c5ae@unix-solution.de> <1ebb57da-c34b-6581-ea33-bdf78f6990f8@unix-solution.de> <53dc55ae-506f-2aed-ea80-50f0978837ab@aklaver.com> From: Adrian Klaver Message-ID: Date: Fri, 15 Mar 2019 16:42:25 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 3/15/19 4:23 PM, Ken Tanzer wrote: > On Fri, Mar 15, 2019 at 11:59 AM Adrian Klaver > > wrote: > > On 3/15/19 11:54 AM, basti wrote: > > this is a dns database, and the client is update the > _acme-challenge for > > LE certificates. I don't want that the client can insert "any" > txt record. > > the client should only insert data if the hostname start with > > _acme-challenge. i have no control on client. > > > > i have try this rule but the server reject this with a endless loop: > > To borrow a quote: > > "I had a problem so I decided to use a rule, now I have two problems." > > Do not use a rule. As suggested upstream use a BEFORE INSERT trigger, > you will be a lot happier. > > > > > CREATE RULE insert_acme AS ON INSERT TO t_dnsadmin_records_txt > >      WHERE NEW.hostname like '_acme-challenge%' > >      DO INSERT INTO t_dnsadmin_records_txt VALUES ( > >                                      NEW.domainid, > >                                      NEW.hostname, > >                                      NEW.txtdata > >                                  ); > > > > > > > Just curious, but wanted to follow up on whether rules are > across-the-board discouraged?  I've seen disparaging comments about > them, but I don't see any indication of that on the create rule page. See here: https://www.postgresql-archive.org/Deprecating-RULES-td5727689.html The rumor crops periodically that they will be deprecated. I personally do not see that happening any time soon. My issue with rules is this: https://www.postgresql.org/docs/11/rules.html If you can understand what really goes on in the above you are ahead of me. Given that my experience is that they do the unexpected as often as the expected so I stay away from them. Triggers I understand even when they error, which is the important part. > > The other suggestion in this thread--a foreign key--will throw an > error.  Your suggestion of a before trigger might well be better (and if > so, why?), but is there anything particularly wrong or bad about using a > rule that would actually work?  Something along these lines: The trouble is simple rarely stays simple and following logic is a lot easier in a trigger function then a rule. This is down mostly to the query rewrite that goes on in a rule. That is border line, if not outright, black magic. Try to follow what happens here: https://www.postgresql.org/docs/11/rules-update.html 41.4.1.1. A First Rule Step by Step > > CREATE RULE insert_acme AS ON INSERT TO t_dnsadmin_records_txt > WHERE NOT NEW.hostname like '_acme-challenge%' > DO INSTEAD NOTHING; > > Thanks, > Ken > > > > -- > AGENCY Software > A Free Software data system > By and for non-profits > /http://agency-software.org// > /https://demo.agency-software.org/client/ > ken.tanzer@agency-software.org > (253) 245-3801 > > Subscribe to the mailing list >  to > learn more about AGENCY or > follow the discussion. -- Adrian Klaver adrian.klaver@aklaver.com