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 1h4ryv-00033a-JG for pgsql-general@arkaria.postgresql.org; Fri, 15 Mar 2019 18:55:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h4ryu-0004BR-21 for pgsql-general@arkaria.postgresql.org; Fri, 15 Mar 2019 18:55:00 +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 1h4ryt-0004BK-MV for pgsql-general@lists.postgresql.org; Fri, 15 Mar 2019 18:54:59 +0000 Received: from fiedler.schlittermann.de ([212.80.235.136] helo=ikarus.unix-solution.de) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h4ryq-0006A6-Rp for pgsql-general@lists.postgresql.org; Fri, 15 Mar 2019 18:54:58 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=unix-solution.de; s=mx; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:From:References:To:Subject:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=8EFWaaUwwYJtuw+hXLuSB96CyoKXtcZgfNoER4JMdjM=; b=OCYYq9oKk7R2fjeGt5GeeOtPxZ O0RMEItq8mpRkK6jL/hXRWlXqMphWR83yHR/hsdgPciXMfIuCLqeuIWO/+Q4frjLqdiNza3j52OD0 y9TqDJLHeeHQaFMERCbP5P1V1zfFV9HcnqQY6YacFaSGxaYCvTX9SC7QIR9oEmnEMIog=; Received: from x4e367480.dyn.telefonica.de ([78.54.116.128] helo=[192.168.30.100]) by ikarus.unix-solution.de with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1h4ryo-0006w1-5f for pgsql-general@lists.postgresql.org; Fri, 15 Mar 2019 19:54:54 +0100 Subject: Re: Conditional INSERT To: pgsql-general@lists.postgresql.org References: <68951fae-3281-c9ad-09b7-205e0bb1c5ae@unix-solution.de> From: basti Message-ID: <1ebb57da-c34b-6581-ea33-bdf78f6990f8@unix-solution.de> Date: Fri, 15 Mar 2019 19:54:52 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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: 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 ); On 15.03.19 19:17, Michael Lewis wrote: > On Fri, Mar 15, 2019 at 10:55 AM basti > wrote: > > Hello, > > I want to insert data into table only if condition is true. > For example: > > INSERT into  mytable (domainid, hostname, txtdata) >   VALUES (100,'_acme.challenge.example', 'somedata'); > >   > Alternative to a trigger implementation, if you are generating that > INSERT statement, you can change it to use a sub-select or CTE that > contains no values if the domainid isn't what you like. If you want it > to fail with error, you could add a check constraint. We might need more > context on what you are doing and why to give good advice.