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 1fSJTW-00025m-Ms for pgsql-general@arkaria.postgresql.org; Mon, 11 Jun 2018 09:50: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 1fSJTU-0005cL-TN for pgsql-general@arkaria.postgresql.org; Mon, 11 Jun 2018 09:50: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 1fSJTU-0005cA-Ez for pgsql-general@lists.postgresql.org; Mon, 11 Jun 2018 09:50: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 1fSJTK-0004U6-Sb for pgsql-general@postgresql.org; Mon, 11 Jun 2018 09:50:54 +0000 Received: from [84.237.50.47] (helo=regency.nsu.ru) by mx.nsu.ru with esmtp (Exim 4.72) (envelope-from ) id 1fSJTD-0008Rs-UD for pgsql-general@postgresql.org; Mon, 11 Jun 2018 16:50:39 +0700 Received: from regency.nsu.ru (localhost [127.0.0.1]) by regency.nsu.ru (8.14.2/8.14.2) with ESMTP id w5BAAcm1042819 for ; Mon, 11 Jun 2018 16:10:38 +0600 (NOVT) (envelope-from danfe@regency.nsu.ru) Received: (from danfe@localhost) by regency.nsu.ru (8.14.2/8.14.2/Submit) id w5BAAXUl042811 for pgsql-general@postgresql.org; Mon, 11 Jun 2018 17:10:33 +0700 (+07) (envelope-from danfe) Date: Mon, 11 Jun 2018 17:10:33 +0700 From: Alexey Dokuchaev To: pgsql-general@postgresql.org Subject: Catching unique_violation exception on specific column/index Message-ID: <20180611101033.GA40332@regency.nsu.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 Hi there, I have a table with several UNIQUE and CHECK constraints. One of these UNIQUE constraints actually *can* be violated -- not on the table level, of course, but on the application level -- meaning, if the entry with particular foo_key is already in there, do not throw an exception, just silently do nothing. 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? IF SQLERRM = 'duplicate key value violates unique constraint' || ' "foo_key"' THEN RAISE NOTICE '%: %', SQLSTATE, SQLERRM; ELSE RAISE EXCEPTION '%: %', SQLSTATE, SQLERRM; END IF; ./danfe