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 1fSJxW-0005jM-KJ for pgsql-general@arkaria.postgresql.org; Mon, 11 Jun 2018 10:21:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fSJxU-0001Hv-AX for pgsql-general@arkaria.postgresql.org; Mon, 11 Jun 2018 10:21:56 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fSJxT-0001Hl-UV for pgsql-general@lists.postgresql.org; Mon, 11 Jun 2018 10:21:56 +0000 Received: from mx.nsu.ru ([84.237.50.39]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fSJxM-00058v-Uk for pgsql-general@postgresql.org; Mon, 11 Jun 2018 10:21:54 +0000 Received: from [84.237.50.47] (helo=regency.nsu.ru) by mx.nsu.ru with esmtp (Exim 4.72) (envelope-from ) id 1fSJxI-00007y-M6 for pgsql-general@postgresql.org; Mon, 11 Jun 2018 17:21:44 +0700 Received: from regency.nsu.ru (localhost [127.0.0.1]) by regency.nsu.ru (8.14.2/8.14.2) with ESMTP id w5BAfhTG045027 for ; Mon, 11 Jun 2018 16:41:43 +0600 (NOVT) (envelope-from danfe@regency.nsu.ru) Received: (from danfe@localhost) by regency.nsu.ru (8.14.2/8.14.2/Submit) id w5BAfc1E044992 for pgsql-general@postgresql.org; Mon, 11 Jun 2018 17:41:38 +0700 (+07) (envelope-from danfe) Date: Mon, 11 Jun 2018 17:41:38 +0700 From: Alexey Dokuchaev To: pgsql-general@postgresql.org Subject: Re: Catching unique_violation exception on specific column/index Message-ID: <20180611104138.GA43920@regency.nsu.ru> References: <20180611101033.GA40332@regency.nsu.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180611101033.GA40332@regency.nsu.ru> User-Agent: Mutt/1.4.2.1i X-KLMS-Rule-ID: 3 X-KLMS-Message-Action: skipped X-KLMS-AntiSpam-Status: not scanned, whitelist X-KLMS-AntiPhishing: not scanned, whitelist X-KLMS-AntiVirus: Kaspersky Security 8.0 for Linux Mail Server, version 8.0.1.705, not scanned, whitelist List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, Jun 11, 2018 at 05:10:33PM +0700, Alexey Dokuchaev wrote: > The usual approach ("EXCEPTION WHEN unique_violation THEN ... END") does > not really cut it because I want to catch unique_violation only when it > happens on "foo_key", and still rightfully complain on others. However, > there is no "EXCEPTION WHEN unique_violation ON (foo_key)" or something. > Is there a way to do this without using triggers and in a less ugly way > than the code below? Oh, I completely forgot about rules: CREATE OR REPLACE RULE ignore_duplicate_foo AS ON INSERT TO table_bar WHERE EXISTS ( SELECT 1 FROM table_bar WHERE foo = NEW.foo) DO INSTEAD NOTHING; Sorry for the noise. ./danfe