pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexey Dokuchaev <danfe@nsu.ru>
To: pgsql-general@postgresql.org
Subject: Re: Catching unique_violation exception on specific column/index
Date: Mon, 11 Jun 2018 17:41:38 +0700
Message-ID: <20180611104138.GA43920@regency.nsu.ru> (raw)
In-Reply-To: <20180611101033.GA40332@regency.nsu.ru>
References: <20180611101033.GA40332@regency.nsu.ru>

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




view thread (8+ messages)  latest in thread

Message-ID: <20180611104138.GA43920@regency.nsu.ru>
Permalink:  ../20180611104138.GA43920@regency.nsu.ru/
Also on:    postgresql.org/message-id/20180611104138.GA43920@regency.nsu.ru

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: danfe@nsu.ru
  Subject: Re: Catching unique_violation exception on specific column/index
  In-Reply-To: <20180611104138.GA43920@regency.nsu.ru>
  References: <20180611101033.GA40332@regency.nsu.ru> <20180611104138.GA43920@regency.nsu.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox