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 1fSK5c-0006bF-KA for pgsql-general@arkaria.postgresql.org; Mon, 11 Jun 2018 10:30:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fSK5b-00074h-HW for pgsql-general@arkaria.postgresql.org; Mon, 11 Jun 2018 10:30:19 +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_SHA384:256) (Exim 4.89) (envelope-from ) id 1fSK5b-00074Q-9u for pgsql-general@lists.postgresql.org; Mon, 11 Jun 2018 10:30:19 +0000 Received: from mout.kundenserver.de ([212.227.126.135]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fSK5Y-0007Vn-Ae for pgsql-general@lists.postgresql.org; Mon, 11 Jun 2018 10:30:18 +0000 Received: from [10.8.0.146] ([185.40.248.10]) by mrelayeu.kundenserver.de (mreue007 [212.227.15.129]) with ESMTPSA (Nemesis) id 0LfLHp-1g4Avv03Zp-00p8XT for ; Mon, 11 Jun 2018 12:30:14 +0200 Subject: Re: Catching unique_violation exception on specific column/index To: pgsql-general@lists.postgresql.org Newsgroups: gmane.comp.db.postgresql.general References: <20180611101033.GA40332@regency.nsu.ru> From: Thomas Kellerer Message-ID: <0fa3bd50-3ce2-2038-29da-48fa0a1f6b19@gmx.net> Date: Mon, 11 Jun 2018 12:30:13 +0200 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 MIME-Version: 1.0 In-Reply-To: <20180611101033.GA40332@regency.nsu.ru> Content-Type: text/plain; charset=utf-8 Content-Language: de-DE Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K1:Ia758CyaxGXhUB5N4CUHDE2gWsfjPfzUie2h2+nyNBVRcACgmKr qYFLYwLrHDqNK0XLUZonOlKetWDiKfMBdz7vr6dKY/Yi7YGfs37VgXrDlikw8S/RizM11Uf MttyDkTpM6ditO7pj9xdOu+XY4Fu/TEru/sTLpcg2olbHy6QSBWwX57nDsAfPJesJ6hrgB7 fxiCzY8h4DTlPNHDwjqTQ== X-UI-Out-Filterresults: notjunk:1;V01:K0:oAlJLOy4XeI=:bZGuFIaU0TzxZDvCbMOT20 upV9pRiXOOvmZ7A/+EL6isiNfNjiMdotECjKWnPEfNwT0VEUyO3snMtJvGWRZ9sE4TGpB5vNq 7ILITwXKCO4+zfnU0UgxnRavnT2QfUorThOyhOwwZFHrEYagpudp9hLLoLbIp6ozGRbyHrdvt A7gRX43pQvhyt7EwcwVO2p3Lf51l51HmqbBF50TrSEsn+AWnCCr0tNaU+rdHOADWJjDbWm+lJ M1fRG2ndufehZ8/HyEkc+AaKFOwmfXJX+bJ7oZHWMS9Ihw6xrZJKedboaadTduqCNis5W8uxp EYcNbwANrvvENEWfAr90wSMc4rlohVGXdczqPL/gQ5otzXhAYMQ6HKWdIVz6qTcBxPHNPnfQA QkvNzZejcUhHAdrD0IpVeKossrMYUYIIzvA2DOvqGfWSkNPLo8O7ux8pVqiNMavghoVYmAOR5 eIZ/S5qSjJhmAC2rx8bnkKYr5YuNC9wP12L346JmzW7FP3Vekg3ipSKHiTsHk6kymseN0SFwq naSBfsNUkZVxh5WuJODWMDUy5fAhjBDG0Q94kW1kyDrk0mhBgfGEuIHMTH77kywMpvRdIFutL 9foiVhFNUDcpbGCFq93MF4QmCrVefQ31zT/JrCkyPdNIUn8YJjpo1j+uiT52qQEk1IZR/5oQY WsIQRwYfaRxIJ3s4g2XKn9PrIoQC/5EbzvdJp2FwWz7Sa8+BTiZFOx/4pE3Z9aCFSa4XGQGGF Al1E5nrluuiexE6z List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alexey Dokuchaev schrieb am 11.06.2018 um 12:10: > 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 What's wrong with: INSERT ... ON CONFLICT (foo_key) DO NOTHING