Re: rule which unpredictable modify a sequence number

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Armand Turpel <geocontexter(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: rule which unpredictable modify a sequence number
Date: 2010-10-26 13:40:05
Message-ID: AANLkTimptV_Fb6_XFJKdc4toT63VnaKuZNg-4x2O_xpa@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, Oct 23, 2010 at 9:53 AM, Armand Turpel <geocontexter(at)gmail(dot)com> wrote:
>
> I have a problem with a rule which unpredictable modify a sequence
> number. When I add a new table entry, the new id_keyword hasnt the value
> as expected. ex.: 1000000000000000, 1000000000000001,
> 1000000000000002,...... If i remove the rule it works.
>
> Here the table, rule and sequence definitions:
>
> CREATE TABLE geocontexter.gc_keyword(
>  id_keyword bigint NOT NULL DEFAULT
> nextval('geocontexter.seq_gc_keyword'::regclass),
>  id_parent bigint NOT NULL DEFAULT 0,
>  id_attribute_group bigint,
>  id_status smallint NOT NULL DEFAULT 100,
>  update_time timestamp without time zone NOT NULL,
>  preferred_order smallint,
>  lang character varying(30) NOT NULL DEFAULT 'en'::character varying,
>  title character varying(126) NOT NULL,
>  description character varying(10000) NOT NULL DEFAULT ''::character
> varying,
>  attribute_value text,
>  CONSTRAINT gc_keyword_id_keyword PRIMARY KEY (id_keyword)
> )
> WITH (
>  OIDS=FALSE
> );
>
> CREATE OR REPLACE RULE keyword_insert_or_replace AS
>    ON INSERT TO geocontexter.gc_keyword
>   WHERE (EXISTS ( SELECT 1 FROM geocontexter.gc_keyword
>                   WHERE gc_keyword.id_keyword = new.id_keyword))
>   DO INSTEAD
>          UPDATE geocontexter.gc_keyword SET lang = new.lang, description =
> new.description,
>                                             title = new.title, update_time =
> new.update_time, id_parent = new.id_parent,
>                                             preferred_order =
> new.preferred_order, id_attribute_group = new.id_attribute_group,
>                                             attribute_value =
> new.attribute_value
>          WHERE gc_keyword.id_keyword = new.id_keyword AND
> gc_keyword.update_time<   new.update_time;
>
> CREATE SEQUENCE geocontexter.seq_gc_keyword
>  INCREMENT 1
>  MINVALUE -1999999999999999
>  MAXVALUE 1999999999999999
>  START           1000000000000000
>  CACHE 1
>  CYCLE;

default values unfortunately don't play well with rules. rules in
fact are a giant headache and your strategy of upsert in rule is
probably going to need a rethink if you want to expose sql-like
behaviors to the calling query. I would advise moving your upsert
into a function call, or doing it in the application.

merlin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David Fetter 2010-10-26 14:21:58 Re: what can depend on index
Previous Message Alban Hertroys 2010-10-26 10:01:39 Re: Slow connection once the PC is network connected