Re: unique rows

From: Markus Schaber <schabi(at)logix-tt(dot)com>
To: TJ O'Donnell <tjo(at)acm(dot)org>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: unique rows
Date: 2006-09-21 07:46:11
Message-ID: 45124343.1060005@logix-tt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi, TJ,

TJ O'Donnell wrote:

> So, is there a way (a constraint, a check?) that will simply
> REJECT a duplicate when I insert the raw data directly into x
> rather than FAILING on an error as it does
> with the unique constraint above?

Failing on an error is exactly the way PostgreSQL (and the SQL standard)
uses to REJECT duplicates. :-)

You seem to think about silently dropping the duplicates. That could be
achieved with an BEFORE INSERT trigger, or with a rule on a view, as
both can silently drop the inserted rule.

The trigger body could even be in language SQL, along the lines of:

SELECT CASE WHEN EXISTS (SELECT keycol FROM table WHERE
table.keycol=NEW.keycol) THEN NULL ELSE NEW;

Nevertheless, expect the insert performance to drop a little, due to the
trigger overhead.

The alternative approaches (SELECT'ing from the application, using a
stored procedure that checks and then inserts the data, and using
subtransactions to roll back the failing inserts) all seem worse (uglier
and slower) to me, but don't hesitate to ask if you're interested.

HTH,
Markus

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Curtis Scheer 2006-09-21 22:18:48 Re: Help with optional parameters
Previous Message Kaloyan Iliev 2006-09-21 07:33:57 Re: unique rows