Re: pgsql 'prefix' error

From: Dimitri Fontaine <dfontaine(at)hi-media(dot)com>
To: bino(at)indoakses-online(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: pgsql 'prefix' error
Date: 2009-11-25 16:20:26
Message-ID: 87tywid19x.fsf@hi-media-techno.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

Bino Oetomo <bino(at)indoakses-online(dot)com> writes:
> ERROR: duplicate key value violates unique constraint "myrecords_pkey"
> CONTEXT: COPY myrecords, line 2: "12"

I think I should add the following code comment to the documentation, if
not already done:

/*
* We invent a prefix_range ordering for convenience, but that's
* dangerous. Use the BTree opclass at your own risk.
*
* On the other hand, when your routing table does contain pretty static
* data and you test it carefully or know it will fit into the ordering
* simplification, you're good to go.
*
* Baring bug, the constraint is to have non-overlapping data.
*/

You have to remember that '12'::prefix_range could be spelled as the
regular expression '12.*'. So that '1'::prefix_range contains '12'.

The BTree opclass is not made to resist to overlapping data. Maybe in
this case though we could say that 12 contains less elements than 1 so
it's less than 1. Here's a test to redefine the pr_cmp() operator in
term of that, as a patch against current CVS (which is 1.0.0).

Can you test with this version and maybe better data set?

Note that as said earlier the indexing you need to speed up queries is
the GiST one, but it could be you want the PK constraint noneless.

prefix=# select prefix_range_cmp('1', '12');
prefix_range_cmp
------------------
1 -- it is 0 without the patch.
(1 row)

This means '1'::prefix_range > '12'::prefix_range and you're now able to
create your PRIMARY KEY on the example data. It's still not very useful
for the general case, but could be argued as better...

Of course changing that will discard any btree containing a prefix_range
column, so that's going to be 1.1.0 if workable.

Regards,
--
dim

PS: no worry about the operators themselves, they are defined atop cmp:

static inline
bool pr_lt(prefix_range *a, prefix_range *b, bool eqval) {
int cmp = pr_cmp(a, b);
return eqval ? cmp <= 0 : cmp < 0;
}

static inline
bool pr_gt(prefix_range *a, prefix_range *b, bool eqval) {
int cmp = pr_cmp(a, b);
return eqval ? cmp >= 0 : cmp > 0;
}

Attachment Content-Type Size
prefix-btree-contains.diff text/x-diff 1020 bytes

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Greg Smith 2009-11-25 16:42:42 Re: Re: [GENERAL] How duplicate data produce when a UNIQUE index exite ?
Previous Message melina386 2009-11-25 15:56:58 Re: Data Warehousing