X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 11153DAF19 for ; Wed, 26 Oct 2005 05:12:30 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 85729-04 for ; Wed, 26 Oct 2005 08:12:27 +0000 (GMT) Received: from smtp3.anpe.fr (unknown [80.118.163.18]) by svr1.postgresql.org (Postfix) with ESMTP id 0773ADAEF9 for ; Wed, 26 Oct 2005 05:12:23 -0300 (ADT) Received: from homere02.anpe.fr (localhost.localdomain [127.0.0.1]) by smtp3.anpe.fr (antivirus) with ESMTP id 08DEF73DA3; Wed, 26 Oct 2005 10:12:03 +0200 (CEST) Received: from HOMERE02.anpe.fr ([10.0.6.138]) by homere02.anpe.fr with Microsoft SMTPSVC(6.0.3790.1830); Wed, 26 Oct 2005 09:53:38 +0200 Received: from [10.0.32.170] ([10.0.32.170]) by HOMERE02.anpe.fr with Microsoft SMTPSVC(6.0.3790.1830); Wed, 26 Oct 2005 09:53:38 +0200 Message-ID: <435F3610.3020303@anpe.fr> Date: Wed, 26 Oct 2005 09:53:52 +0200 From: Patrick JACQUOT User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: tobbe Cc: pgsql-sql@postgresql.org Subject: Re: automatic update or insert References: <1129790184.351579.302550@g47g2000cwa.googlegroups.com> In-Reply-To: <1129790184.351579.302550@g47g2000cwa.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 26 Oct 2005 07:53:38.0351 (UTC) FILETIME=[5FE43BF0:01C5DA02] X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 required=5 tests=[none] X-Spam-Level: X-Archive-Number: 200510/312 X-Sequence-Number: 23165 tobbe wrote: >Hi. > >I have a little problem. > >In a system of mine i need to insert records into table [tbStat], and >if the records exist i need to update them instead and increase a >column [cQuantity] for every update. > >I.e. the first insert sets cQuantity to 1, and for every other run >cQuantity is increased. > >Currently i have implemented this as a stored procedure in the plpgsql >language. This means that in my stored procedure i first do a select to >find out if the row exists or not, then i do a insert or update >depending if the row existed. > >Unfortunately, stored procedures seems awfully slow. And i need the >application to go faster. > >One solution could be to implement the stored procedure in my program >instead. I think that this will be atleast 50% faster than my stored >procedure, so that would be ok. > >However, this has made me thinking. Couldn't this be done directly in >SQL? > > >Brgds Robert > > >---------------------------(end of broadcast)--------------------------- >TIP 3: Have you checked our extensive FAQ? > > http://www.postgresql.org/docs/faq > > > Maybe would it be better to insert always, and to use grouping and summation when using the table. That would enable you to preserve the history of events. That's how I almost always work hth P.Jacquot