From gsmith@eurocom.od.ua Mon May 8 15:59:31 2000 Received: from Main.eurocom.od.ua (root@Main.EuroCom.Od.UA [212.15.128.6]) by hub.org (8.9.3/8.9.3) with ESMTP id LAA27619 for ; Mon, 8 May 2000 11:59:31 -0400 (EDT) (envelope-from gsmith@eurocom.od.ua) Received: from eurocom.od.ua (FarHost.EuroCom.Od.UA [212.15.128.42]) by Main.eurocom.od.ua (8.10.0/8.10.0) with ESMTP id e48FxEi31521 for ; Mon, 8 May 2000 18:59:15 +0300 Message-ID: <3916E46F.2E4DC655@eurocom.od.ua> Date: Mon, 08 May 2000 18:59:44 +0300 From: "Vladimir V. Zolotych" X-Mailer: Mozilla 4.51 [en] (X11; I; Linux 2.2.5-15 i686) X-Accept-Language: en MIME-Version: 1.0 To: pgsql-admin@postgresql.org Subject: rules problem Content-Type: multipart/alternative; boundary="------------224849EE308A255A97F4C080" X-Archive-Number: 200005/31 --------------224849EE308A255A97F4C080 Content-Type: text/plain; charset=koi8-r Content-Transfer-Encoding: 7bit Hello all, Encountered the problem with using RULEs. Cannot log (e.g. write some info about insertions into sepearate table) insertions properly. Detailed description (not long or sophisticated) follows: I do: 1) CREATE TABLE colors (id SERIAL, color TEXT); 2) Create table for log info: CREATE TABLE colors_log (color_id INT4, color TEXT); 3) Create RULE that actually makes log: CREATE RULE log_color AS ON INSERT TO colors DO INSERT INTO colors_log VALUES (NEW.id, NEW.color); 4) Make some insertions: INSERT INTO colors (color) VALUES ('red'); The same for 'green', 'blue'. 5) SELECT * FROM colors; id|color --+----- 2|red 4|green 6|blue Here appears the first question: why 'id' is 2, 4, 6, not 1, 2, 3? 7) SELECT * FROM colors_log; color_id|color --------+----- 1|red 3|green 5|blue The problem is: the 'id's differ. E.g., In colors_log table the saved 'id' are wrong. Thanks! -- Vladimir Zolotych gsmith@eurocom.od.ua --------------224849EE308A255A97F4C080 Content-Type: text/html; charset=koi8-r Content-Transfer-Encoding: 7bit Hello all,

Encountered the problem with using RULEs. Cannot log
(e.g. write some info about insertions into sepearate table)
insertions properly. Detailed description (not long or sophisticated)
follows:

I do:

1) CREATE TABLE colors (id SERIAL, color TEXT);

2) Create table for log info:

   CREATE TABLE colors_log (color_id INT4, color TEXT);

3) Create RULE that actually makes log:

   CREATE RULE log_color
   AS ON INSERT
   TO colors
   DO INSERT INTO colors_log VALUES (NEW.id, NEW.color);

4) Make some insertions:

   INSERT INTO colors (color) VALUES ('red');
 
   The same for 'green', 'blue'.
 
5) SELECT * FROM colors;

   id|color
   --+-----
    2|red
    4|green
    6|blue
 
   Here appears the first question:
   why 'id' is 2, 4, 6, not 1,  2, 3?
 
7) SELECT * FROM colors_log;

   color_id|color
   --------+-----
          1|red
          3|green
          5|blue
 
   The problem is: the 'id's differ. E.g.,
   In colors_log table the saved 'id' are wrong.

Thanks!
 
 

-- 
Vladimir Zolotych                         gsmith@eurocom.od.ua
  --------------224849EE308A255A97F4C080-- From nhuillard@ghs.fr Mon May 8 20:56:24 2000 Received: from mailhub1.isdnet.net (mailhub1.isdnet.net [195.154.209.21]) by hub.org (8.9.3/8.9.3) with ESMTP id QAA35321 for ; Mon, 8 May 2000 16:56:24 -0400 (EDT) (envelope-from nhuillard@ghs.fr) Received: from ppp54-cergy.isdnet.net (ppp54-cergy.isdnet.net [194.149.184.181]) by mailhub1.isdnet.net (8.9.3/8.9.3) with SMTP id WAA91598; Mon, 8 May 2000 22:56:11 +0200 (CEST) Received: by ppp54-cergy.isdnet.net with Microsoft Mail id <01BFB940.D1C41F80@ppp54-cergy.isdnet.net>; Mon, 8 May 2000 22:57:43 +0200 Message-ID: <01BFB940.D1C41F80@ppp54-cergy.isdnet.net> From: Nicolas Huillard To: "'Vladimir V. Zolotych'" , "pgsql-admin@postgresql.org" Subject: RE: rules problem Date: Mon, 8 May 2000 22:39:20 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by hub.org id QAA35339 X-Archive-Number: 200005/32 Here is my $0.02 : * when you create "id SERIAL", Postgres remembers to call function nextval on each insertion, * the rule's NEW.id item uses the function nextval itself instead of it's result This explains why the ID's are what you see : * first of all, you insert the log, calling nextval for the SERIAL (id=1 in the log) * then you actually insert the data into the colors table (first row has id=2) * then you insert a second time : first into the log (id=3) then into the actual table (id=4) This make me think about date constants : 'now' is a constant that have a different value each time you call it. In your case, the rule must use then constant 'nextval', which increments the actual sequence on each call. Either this is a bug... or a feature... I don't see any genral workaround here. Maybe there is another way of retreiving the actual inserted data (other than NEW.id) Yours, Nicolas Huillard G.H.S Directeur Technique Tél : +33 1 43 21 16 66 Fax : +33 1 56 54 02 18 mailto:nhuillard@ghs.fr http://www.ghs.fr -----Message d'origine----- De: Vladimir V. Zolotych [SMTP:gsmith@eurocom.od.ua] Date: lundi 8 mai 2000 18:00 À: pgsql-admin@postgresql.org Objet: [ADMIN] rules problem Hello all, Encountered the problem with using RULEs. Cannot log (e.g. write some info about insertions into sepearate table) insertions properly. Detailed description (not long or sophisticated) follows: I do: 1) CREATE TABLE colors (id SERIAL, color TEXT); 2) Create table for log info: CREATE TABLE colors_log (color_id INT4, color TEXT); 3) Create RULE that actually makes log: CREATE RULE log_color AS ON INSERT TO colors DO INSERT INTO colors_log VALUES (NEW.id, NEW.color); 4) Make some insertions: INSERT INTO colors (color) VALUES ('red'); The same for 'green', 'blue'. 5) SELECT * FROM colors; id|color --+----- 2|red 4|green 6|blue Here appears the first question: why 'id' is 2, 4, 6, not 1, 2, 3? 7) SELECT * FROM colors_log; color_id|color --------+----- 1|red 3|green 5|blue The problem is: the 'id's differ. E.g., In colors_log table the saved 'id' are wrong. Thanks! -- Vladimir Zolotych gsmith@eurocom.od.ua