From stephenlfoster@comcast.net Sun Feb 12 22:27:31 2006 X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 07F449DCA27 for ; Sun, 12 Feb 2006 18:27:31 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 39856-02 for ; Sun, 12 Feb 2006 18:27:32 -0400 (AST) X-Greylist: delayed 00:05:01.781956 by SQLgrey- Received: from sccrmhc11.comcast.net (sccrmhc11.comcast.net [204.127.200.81]) by postgresql.org (Postfix) with ESMTP id ACDFC9DC9E6 for ; Sun, 12 Feb 2006 18:27:28 -0400 (AST) Received: from cfgod (c-69-137-84-227.hsd1.tn.comcast.net[69.137.84.227]) by comcast.net (sccrmhc11) with SMTP id <2006021222222801100fnf8ue>; Sun, 12 Feb 2006 22:22:28 +0000 From: "Foster, Stephen" To: Subject: Trigger/Sequence headache Date: Sun, 12 Feb 2006 16:27:48 -0600 Message-ID: <000801c63023$8e0af830$2101a8c0@cfgod> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0009_01C62FF1.43708830" X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.2616 Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=1.091 required=5 tests=[AWL=-0.242, HTML_MESSAGE=0.001, RCVD_IN_BL_SPAMCOP_NET=1.332] X-Spam-Score: 1.091 X-Spam-Level: * X-Archive-Number: 200602/128 X-Sequence-Number: 24138 This is a multi-part message in MIME format. ------=_NextPart_000_0009_01C62FF1.43708830 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit This is going to be one of those stupid problems of mine. I have an insert trigger setup to verify that duplicate or repeating information isn't storage in the table. If trigger function finds the information as a duplicate it returns a NULL and the information isn't added; that works. The problem I'm having is that it is incrementing the sequence counter even when the data isn't added. Is this something that I have to live with or should I be returning something other than a NULL? I have a function to re-sequence the table and reset the sequence counter in another application. Normal for that application because of the constant adding and deleting. So if there isn't another answer then I can write a version of that for this application and run it weekly/monthly depending on need and use. It just drives me nuts with large holes in the table. Thanks Lee Foster/ ------=_NextPart_000_0009_01C62FF1.43708830 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

This is going to be one of those stupid problems of mine.  I have an insert = trigger setup to verify that duplicate or repeating information isn’t = storage in the table.  If trigger = function finds the information as a duplicate it returns a NULL and the information = isn’t added; that works.  The = problem I’m having is that it is incrementing the sequence counter even when the = data isn’t added.  Is this something = that I have to live with or should I be returning something other than a = NULL?

 

I have a function to re-sequence the table and reset = the sequence counter in another application.  Normal for that application because of the constant adding and deleting.  So if there isn’t another = answer then I can write a version of that for this application and run it = weekly/monthly depending on need and use.  = It just drives me nuts with large holes in the = table.

 

Thanks

 

Lee Foster/

------=_NextPart_000_0009_01C62FF1.43708830-- From sszabo@megazone.bigpanda.com Sun Feb 12 23:10:37 2006 X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 582A79DC85E for ; Sun, 12 Feb 2006 19:10:37 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 46225-04 for ; Sun, 12 Feb 2006 19:10:36 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by postgresql.org (Postfix) with ESMTP id 711AF9DC84D for ; Sun, 12 Feb 2006 19:10:32 -0400 (AST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 3930538043; Sun, 12 Feb 2006 15:10:33 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 3765E37F79; Sun, 12 Feb 2006 15:10:33 -0800 (PST) Date: Sun, 12 Feb 2006 15:10:33 -0800 (PST) From: Stephan Szabo To: "Foster, Stephen" Cc: pgsql-sql@postgresql.org Subject: Re: Trigger/Sequence headache In-Reply-To: <000801c63023$8e0af830$2101a8c0@cfgod> Message-ID: <20060212150559.S97294@megazone.bigpanda.com> References: <000801c63023$8e0af830$2101a8c0@cfgod> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.139 required=5 tests=[AWL=0.139] X-Spam-Score: 0.139 X-Spam-Level: X-Archive-Number: 200602/129 X-Sequence-Number: 24139 On Sun, 12 Feb 2006, Foster, Stephen wrote: > This is going to be one of those stupid problems of mine. I have an > insert trigger setup to verify that duplicate or repeating information > isn't storage in the table. If trigger function finds the information > as a duplicate it returns a NULL and the information isn't added; that > works. The problem I'm having is that it is incrementing the sequence > counter even when the data isn't added. Is this something that I have > to live with or should I be returning something other than a NULL? The sequence is going to increment upon getting the value. However, I think, if instead of using a default, you got the next value in the trigger after you determined that it wasn't a duplicate and set the field, it wouldn't increment for this case. This changes some other behaviors a little (for example DEFAULT in updates as well), so you'd need to see whether it'd be acceptable. Of course, errors, rollbacks and deletes will still leave holes. From stephenlfoster@comcast.net Sun Feb 12 23:20:08 2006 X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 902B29DCB8E for ; Sun, 12 Feb 2006 19:20:08 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 46134-09 for ; Sun, 12 Feb 2006 19:20:10 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sccrmhc12.comcast.net (sccrmhc12.comcast.net [63.240.77.82]) by postgresql.org (Postfix) with ESMTP id F015B9DCB80 for ; Sun, 12 Feb 2006 19:20:05 -0400 (AST) Received: from cfgod (c-69-137-84-227.hsd1.tn.comcast.net[69.137.84.227]) by comcast.net (sccrmhc12) with SMTP id <2006021223200001200sd7rke>; Sun, 12 Feb 2006 23:20:08 +0000 From: "Foster, Stephen" To: "'Stephan Szabo'" Cc: Subject: Re: Trigger/Sequence headache Date: Sun, 12 Feb 2006 17:25:20 -0600 Message-ID: <000f01c6302b$9be85760$2101a8c0@cfgod> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.2616 In-Reply-To: <20060212150559.S97294@megazone.bigpanda.com> Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=1.608 required=5 tests=[AWL=-0.711, DNS_FROM_RFC_POST=1.44, DNS_FROM_RFC_WHOIS=0.879] X-Spam-Score: 1.608 X-Spam-Level: * X-Archive-Number: 200602/130 X-Sequence-Number: 24140 That's what I thought was going to be the answer. I was just hoping I was making a mistake somehow. It's no big deal but I like things organized and hate giant holes. Ok, one more thing for one of the batch jobs. No problem I have a cleanup routine. Thanks for the help, Lee Foster/ -----Original Message----- From: Stephan Szabo [mailto:sszabo@megazone.bigpanda.com] Sent: Sunday, February 12, 2006 5:11 PM To: Foster, Stephen Cc: pgsql-sql@postgresql.org Subject: Re: [SQL] Trigger/Sequence headache On Sun, 12 Feb 2006, Foster, Stephen wrote: > This is going to be one of those stupid problems of mine. I have an > insert trigger setup to verify that duplicate or repeating information > isn't storage in the table. If trigger function finds the information > as a duplicate it returns a NULL and the information isn't added; that > works. The problem I'm having is that it is incrementing the sequence > counter even when the data isn't added. Is this something that I have > to live with or should I be returning something other than a NULL? The sequence is going to increment upon getting the value. However, I think, if instead of using a default, you got the next value in the trigger after you determined that it wasn't a duplicate and set the field, it wouldn't increment for this case. This changes some other behaviors a little (for example DEFAULT in updates as well), so you'd need to see whether it'd be acceptable. Of course, errors, rollbacks and deletes will still leave holes. From robeddielee@hotmail.com Mon Feb 13 07:04:04 2006 X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id DF8B59DC999 for ; Mon, 13 Feb 2006 03:04:04 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 46984-03 for ; Mon, 13 Feb 2006 03:04:04 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from floppy.pyrenet.fr (news.pyrenet.fr [194.116.145.2]) by postgresql.org (Postfix) with ESMTP id 07A349DC98F for ; Mon, 13 Feb 2006 03:04:01 -0400 (AST) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id CE7693093D; Mon, 13 Feb 2006 08:04:01 +0100 (MET) From: "rlee0001" X-Newsgroups: pgsql.sql Subject: Re: Trigger/Sequence headache Date: 12 Feb 2006 23:03:52 -0800 Organization: http://groups.google.com Lines: 40 Message-ID: <1139814232.642459.326740@g43g2000cwa.googlegroups.com> References: <20060212150559.S97294@megazone.bigpanda.com> <000f01c6302b$9be85760$2101a8c0@cfgod> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Complaints-To: groups-abuse@google.com In-Reply-To: <000f01c6302b$9be85760$2101a8c0@cfgod> User-Agent: G2/0.2 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: g43g2000cwa.googlegroups.com; posting-host=70.128.225.153; posting-account=W1HrJhMAAABKoEyjN0n9NdMFZKbfU7Ty3txgz4qxiVmANE6LfCsvZA To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.7 required=5 tests=[AWL=0.151, FORGED_HOTMAIL_RCVD2=0.549] X-Spam-Score: 0.7 X-Spam-Level: X-Archive-Number: 200602/225 X-Sequence-Number: 24235 Stephen, You don't need to use a seperate batch to clean up the table. As Stephan pointed out, you can call nextval after you determine that the new row isn't a duplicate. In case you misunderstood what Stephan had suggested let me try to explain what is happening. When PostgreSQL receives an INSERT request it aggregates field values for the new row from several sources. 1) The statement itself (INSERT INTO ... VALUES ...) 2) The fields' DEFAULT values (provided by the CREATE TABLE statement) 3) Any CONSTRANTs are taken into consideration 4) BEFORE triggers are called in alphabetical order 5) The new row is inserted 6) AFTER triggers are called in alphabetical order The sequence is being incrimented when NEXTVAL is evaluated. NEXTVAL can be evaluated in steps 1, 2 or 4 depending on how your application is written. Since you want to be able to cancel the operation in step 4 without NEXTVAL having been evaluated, this is where you should call NEXTVAL (instead of in steps 1 or 2). So in your trigger you want to do something like this: -- Check for duplicates here...RETURN NULL; NEW.ID = NEXTVAL('SEQ_MYTABLE_ID'); RETURN NEW; Also, are you sure you want to return NULL rather than raise an exception? Either way will work depending on what the desired behavour is. The thing is that silently dropping rows might be the source of a debugging nightmare later on where-as raising an exception is a bit easier to detect. Just me $0.02. To raise an exception see RAISE EXCEPTION in the postgresql manual. Its really easy but will cancel the current transaction so it may not be desirable for your uses. -Robert From schabi@logix-tt.com Tue Feb 14 08:44:02 2006 X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 8448B9DC822 for ; Tue, 14 Feb 2006 04:44:02 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 59256-03 for ; Tue, 14 Feb 2006 04:44:00 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mail.logix-tt.com (serval.logix-tt.com [213.239.221.42]) by postgresql.org (Postfix) with ESMTP id ADFFC9DC80C for ; Tue, 14 Feb 2006 04:43:57 -0400 (AST) Received: from kingfisher.intern.logix-tt.com (walfisch.in-ulm.de [217.10.3.203]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.logix-tt.com (Postfix) with ESMTP id DD14624407F; Tue, 14 Feb 2006 09:43:59 +0100 (CET) Received: from [127.0.0.1] (localhost [127.0.0.1]) by kingfisher.intern.logix-tt.com (Postfix) with ESMTP id 7AF9A1800024E; Mon, 13 Feb 2006 12:38:37 +0100 (CET) Message-ID: <43F06FBD.6000906@logix-tt.com> Date: Mon, 13 Feb 2006 12:38:37 +0100 From: Markus Schaber Organization: Logical Tracking and Tracing International AG, Switzerland User-Agent: Debian Thunderbird 1.0.7 (X11/20051017) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: Trigger/Sequence headache References: <000f01c6302b$9be85760$2101a8c0@cfgod> In-Reply-To: <000f01c6302b$9be85760$2101a8c0@cfgod> X-Enigmail-Version: 0.93.0.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.881 required=5 tests=[DATE_IN_PAST_12_24=0.881] X-Spam-Score: 0.881 X-Spam-Level: X-Archive-Number: 200602/148 X-Sequence-Number: 24158 Hi, Stephen, Foster, Stephen wrote: > That's what I thought was going to be the answer. I was just hoping I > was making a mistake somehow. It's no big deal but I like things > organized and hate giant holes. > > Ok, one more thing for one of the batch jobs. No problem I have a > cleanup routine. Out of curiosity: Could you explain what's the problem with the holes? Bigserial should provide enough number space that holes are no problem. Markus -- Markus Schaber | Logical Tracking&Tracing International AG Dipl. Inf. | Software Development GIS Fight against software patents in EU! www.ffii.org www.nosoftwarepatents.org