pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Piergiorgio Buongiovanni <piergiorgio.buongiovanni@netspa.it>
To: pgsql-bugs@postgresql.org
Subject: BUG #5894: Rules' behaviour when SERIAL data types are used
Date: Fri, 18 Feb 2011 15:47:24 GMT
Message-ID: <201102181547.p1IFlOsE056574@wwwmaster.postgresql.org> (raw)


The following bug has been logged online:

Bug reference:      5894
Logged by:          Piergiorgio Buongiovanni
Email address:      piergiorgio.buongiovanni@netspa.it
PostgreSQL version: 8.4.4
Operating system:   RedHat Centos
Description:        Rules' behaviour when SERIAL data types are used
Details: 

We are experiencing a problem on using rules and serial data types with
PostgreSQL.
We are trying to align two tables in two different schemas through rules and
the main table has a column of data type SERIAL.
The case is the following:

CREATE table business.prova
(
	iSId SERIAL,
	cName varchar,
	CONSTRAINT _prova_PK PRIMARY KEY (iSId)
);
CREATE table l10n.Prova
(
	iSId integer,
	cName varchar,
	CONSTRAINT _Prova_PK PRIMARY KEY (iSId),
	CONSTRAINT _Prova_FK FOREIGN KEY (iSId) REFERENCES business.Prova (iSId)
);

Now we create a rule in order to align l10n.prova when a row is inserted in
business.prova:

CREATE OR REPLACE RULE _Prova_Ins AS ON INSERT
  TO business.Prova
  DO ALSO INSERT INTO l10n.Prova VALUES (NEW.iSId, NEW.cName);

We are now ready to insert a value in business.Prova and we expect to see a
copy of that row in l10n.Prova, so we execute the following statement:

INSERT INTO business.Prova (cName) VALUES ('Prova_2');

We obtain the following error message:

ERROR:  insert or update on table "prova" violates foreign key constraint
"_prova_fk"
DETAIL:  Key (isid)=(384) is not present in table "prova".


********** Errore **********

ERROR: insert or update on table "prova" violates foreign key constraint
"_prova_fk"
Stato SQL: 23503
Dettaglio: Key (isid)=(384) is not present in table "prova".

To analyze the problem we have dropped the constraint _prova_fk on
l10n.Prova table; After this we can re-execute the previous statement and
this time we have no errors. If we now look at the two tables we found the
following situation:

SELECT * from business.Prova;
385;"Prova_2"
SELECT * from l10n.Prova;
386;"Prova_2"

Why the value of the iSId column is different?
it seems that a new sequence value is used when the INSERT in l10n.Prova
occurs.

We tried the same SQL statement on PostgreSQL 9.0 and we obtain the same
behaviour.

Is there a limit in the use of rules with SERIAL data types? This is not
clear from your documentation.



view thread (2+ messages)  latest in thread

Message-ID: <201102181547.p1IFlOsE056574@wwwmaster.postgresql.org>
Permalink:  ../201102181547.p1IFlOsE056574@wwwmaster.postgresql.org/
Also on:    postgresql.org/message-id/201102181547.p1IFlOsE056574@wwwmaster.postgresql.org

 · 

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-bugs@postgresql.org
  Cc: piergiorgio.buongiovanni@netspa.it
  Subject: Re: BUG #5894: Rules' behaviour when SERIAL data types are used
  In-Reply-To: <201102181547.p1IFlOsE056574@wwwmaster.postgresql.org>
  References: <201102181547.p1IFlOsE056574@wwwmaster.postgresql.org>

* 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