From RBENITEZ@houston.rr.com Tue Nov 5 03:05:30 2002 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 0E5E5475EF0 for ; Mon, 4 Nov 2002 22:05:30 -0500 (EST) Received: from txsmtp03.texas.rr.com (smtp3.texas.rr.com [24.93.36.231]) by postgresql.org (Postfix) with ESMTP id 50EF1475E4A for ; Mon, 4 Nov 2002 22:05:29 -0500 (EST) Received: from SUZIE (cs6669177-54.houston.rr.com [66.69.177.54]) by txsmtp03.texas.rr.com (8.12.5/8.12.2) with SMTP id gA533nUq024755 for ; Mon, 4 Nov 2002 22:03:50 -0500 (EST) Message-ID: <000801c28478$2c97e810$6401a8c0@SUZIE> From: "Roberto Benitez" To: Subject: insert trigger Date: Mon, 4 Nov 2002 21:05:17 -0600 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0005_01C28445.E0B20C50" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/84 X-Sequence-Number: 6272 This is a multi-part message in MIME format. ------=_NextPart_000_0005_01C28445.E0B20C50 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable I have the following situation: when a new record is inserted in to a psql table, i want to be able to = update certain fields. the way i'm doing it right now..to find the last re= cord inserted...is by sorting the primary key (serial) in descending order = and taking the first item in the list...the one at the top should be the on= e i just entered...and of course, i then procede to update the fieldsd i ne= ed to update.. So, basically would like to know if this will always work. will the one at= the top (the one w/ the highest primary key [serial type]) always be the o= ne i just entered. AND does psql ALWAYS execute the trigger(s) after EVERY = single record that is inerted. what would happen if [ n ] different users t= ry to insert records simultaniously? will psql insert one-execute the trigg= er, insert the next-execute the trigger..and so on? or will it insert ALL [= n] records AND THEN execute the trigger? i would appreciate any advice--if this would work, or if there is a better = way. thanks rbenitez22@yahoo.com ------=_NextPart_000_0005_01C28445.E0B20C50 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
I have the following situation:
 
    when a new record is in= serted in=20 to a psql table, i want to be able to update certain fields.  the way = i'm=20 doing it right now..to find the last record inserted...is by sorting the pr= imary=20 key (serial) in descending order and taking the first item in the list...th= e one=20 at the top should be the one i just entered...and of course, i then procede= to=20 update the fieldsd i need to update..
So, basically would like to know if this w= ill=20 always work.  will the one at the top (the one w/ the highest primary = key=20 [serial type]) always be the one i just entered. AND does psql ALWAYS execu= te=20 the trigger(s) after EVERY single record that is inerted. what would happen= =20 if [ n ] different users try to insert records simultaniously? will ps= ql=20 insert one-execute the trigger, insert the next-execute the trigger..and so= on?=20 or will it insert ALL [n] records AND THEN  execute the=20 trigger?
 
i would appreciate any advice--if this wou= ld work,=20 or if there is a better way.
 
thanks
rbenitez22@yahoo.com
 
 
 
 
 
------=_NextPart_000_0005_01C28445.E0B20C50-- From marcm@eisolution.com Thu Nov 7 16:18:46 2002 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 210F4476B34 for ; Thu, 7 Nov 2002 11:18:46 -0500 (EST) Received: from dg.eisolution.com (dsl-232-15.speedsite.com [206.126.232.15]) by postgresql.org (Postfix) with ESMTP id C82A3476F7A for ; Thu, 7 Nov 2002 11:07:32 -0500 (EST) Received: from marcmdelltop (sylvester.crosscountrycourier.com [63.160.190.114]) (authenticated) by dg.eisolution.com (8.11.6/8.11.6) with ESMTP id gA7G7ao32716; Thu, 7 Nov 2002 10:07:36 -0600 Message-ID: <004301c28677$b40ee360$e802010a@crosscountrycourier.com> From: "Marc Mitchell" To: "Roberto Benitez" , References: <000801c28478$2c97e810$6401a8c0@SUZIE> Subject: Re: insert trigger Date: Thu, 7 Nov 2002 10:06:58 -0600 MIME-Version: 1.0 Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4807.1700 X-MIMEOLE: Produced By Microsoft MimeOLE V5.50.4807.1700 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/88 X-Sequence-Number: 6276 It would appear to me that you are really doing this the hard way. If you are trying to set/change fields in the same row that you are inserting (say to force an add date or userid) you could use an INSTEAD rule to transform the insert passing through the fields you don't wish to change and transforming the ones that you do. You turn what would be 3 statements into one. I think an alternative to this would also be a BEFORE INSERT trigger and function as I believe the function can simply change the current values of the row before the insert takes place. If you are trying to set/change fields in another table based on the values of the row inserted, then the rule should have access to NEW. attributes for the columns and their inserted values and there is no need for you to go out searching (SELECTing) for them. Hope this helps, Marc Mitchell - Senior Application Architect Enterprise Information Solutions, Inc. Downers Grove, IL 60515 marcm@eisolution.com ----- Original Message ----- From: "Roberto Benitez" To: Sent: Monday, November 04, 2002 9:05 PM Subject: [ADMIN] insert trigger I have the following situation: when a new record is inserted in to a psql table, i want to be able to update certain fields. the way i'm doing it right now..to find the last record inserted...is by sorting the primary key (serial) in descending order and taking the first item in the list...the one at the top should be the one i just entered...and of course, i then procede to update the fieldsd i need to update.. So, basically would like to know if this will always work. will the one at the top (the one w/ the highest primary key [serial type]) always be the one i just entered. AND does psql ALWAYS execute the trigger(s) after EVERY single record that is inerted. what would happen if [ n ] different users try to insert records simultaniously? will psql insert one-execute the trigger, insert the next-execute the trigger..and so on? or will it insert ALL [n] records AND THEN execute the trigger? i would appreciate any advice--if this would work, or if there is a better way. thanks rbenitez22@yahoo.com From mallah@trade-india.com Thu Nov 7 16:13:18 2002 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 9846D47728C for ; Thu, 7 Nov 2002 11:13:18 -0500 (EST) Received: from rtlocal.trade-india.com (mail-relay.trade-india.com [203.196.129.235]) by postgresql.org (Postfix) with SMTP id 549E2476CF4 for ; Thu, 7 Nov 2002 11:08:50 -0500 (EST) Received: (qmail 2355 invoked from network); 7 Nov 2002 15:56:52 -0000 Received: from news1.trade-india.com (HELO www.trade-india.com) (130.94.20.27) by infocom-236-129-del.trade-india.com with SMTP; 7 Nov 2002 15:56:52 -0000 Received: (qmail 20423 invoked by uid 99); 7 Nov 2002 16:08:45 -0000 Received: from 203.145.130.142 (SquirrelMail authenticated user mallah) by mail.trade-india.com with HTTP; Thu, 7 Nov 2002 21:38:45 +0530 (IST) Message-ID: <1101.203.145.130.142.1036685325.squirrel@mail.trade-india.com> Date: Thu, 7 Nov 2002 21:38:45 +0530 (IST) Subject: Re: insert trigger From: To: In-Reply-To: <000801c28478$2c97e810$6401a8c0@SUZIE> References: <000801c28478$2c97e810$6401a8c0@SUZIE> X-Priority: 3 Importance: Normal X-MSMail-Priority: Normal Cc: X-Mailer: SquirrelMail (version 1.2.6) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/89 X-Sequence-Number: 6277 Yes Roberto , Trigger are very relaible and are fired in intutive manner. (trigger is fired for every insert ) read documentation of create trigger regds mallah. regds mallah. > I have the following situation: > > when a new record is inserted in to a psql table, i want to be able to update certain > fields. the way i'm doing it right now..to find the last record inserted...is by sorting > the primary key (serial) in descending order and taking the first item in the list...the one > at the top should be the one i just entered...and of course, i then procede to update the > fieldsd i need to update.. > So, basically would like to know if this will always work. will the one at the top (the one w/ > the highest primary key [serial type]) always be the one i just entered. AND does psql ALWAYS > execute the trigger(s) after EVERY single record that is inerted. what would happen if [ n ] > different users try to insert records simultaniously? will psql insert one-execute the trigger, > insert the next-execute the trigger..and so on? or will it insert ALL [n] records AND THEN > execute the trigger? > > i would appreciate any advice--if this would work, or if there is a better way. > > thanks > rbenitez22@yahoo.com ----------------------------------------- Get your free web based email at trade-india.com. "India's Leading B2B eMarketplace.!" http://www.trade-india.com/ From sszabo@megazone23.bigpanda.com Thu Nov 7 16:47:04 2002 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id AD887477534 for ; Thu, 7 Nov 2002 11:47:04 -0500 (EST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id DCE3C476B55 for ; Thu, 7 Nov 2002 11:32:20 -0500 (EST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 39F5AD613; Thu, 7 Nov 2002 08:32:25 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 2FA375C04; Thu, 7 Nov 2002 08:32:25 -0800 (PST) Date: Thu, 7 Nov 2002 08:32:25 -0800 (PST) From: Stephan Szabo To: Roberto Benitez Cc: Subject: Re: insert trigger In-Reply-To: <000801c28478$2c97e810$6401a8c0@SUZIE> Message-ID: <20021107082731.R97075-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/92 X-Sequence-Number: 6280 On Mon, 4 Nov 2002, Roberto Benitez wrote: > when a new record is inserted in to a psql table, i want to be > able to update certain fields. the way i'm doing it right now..to > find the last record inserted...is by sorting the primary key (serial) > in descending order and taking the first item in the list...the one at > the top should be the one i just entered...and of course, i then > procede to update the fieldsd i need to update.. As was said, if you're modifying the same row you can use a before trigger and directly change the values by assigning them. This happens before the row is inserted so any changes made there are in the row that's actually written. You can also make the system ignore some row by returning NULL. Rules have a problem that you need to be careful to set things up so that you don't end up with a recursive rule. > So, basically would like to know if this will always work. will the > one at the top (the one w/ the highest primary key [serial type]) > always be the one i just entered. AND does psql ALWAYS execute the > trigger(s) after EVERY single record that is inerted. what would > happen if [ n ] different users try to insert records simultaniously? > will psql insert one-execute the trigger, insert the next-execute the > trigger..and so on? or will it insert ALL [n] records AND THEN execute > the trigger? It may interleave them. However, I think in an after trigger, the value of the key given to this row might be set, so you probably want to use that in any case (esp since you can have a statement that inserts multiple rows and you'll want to do each row individually). From bruno@wolff.to Thu Nov 7 16:25:19 2002 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 1E608476E4C for ; Thu, 7 Nov 2002 11:25:19 -0500 (EST) Received: from wolff.to (unknown [66.93.197.35]) by postgresql.org (Postfix) with SMTP id 4C36A4773F6 for ; Thu, 7 Nov 2002 11:23:55 -0500 (EST) Received: (qmail 17198 invoked by uid 500); 7 Nov 2002 16:33:34 -0000 Date: Thu, 7 Nov 2002 10:33:34 -0600 From: Bruno Wolff III To: Roberto Benitez Cc: pgsql-admin@postgresql.org Subject: Re: insert trigger Message-ID: <20021107163334.GA17058@wolff.to> Mail-Followup-To: Roberto Benitez , pgsql-admin@postgresql.org References: <000801c28478$2c97e810$6401a8c0@SUZIE> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <000801c28478$2c97e810$6401a8c0@SUZIE> User-Agent: Mutt/1.3.25i X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/90 X-Sequence-Number: 6278 On Mon, Nov 04, 2002 at 21:05:17 -0600, Roberto Benitez wrote: > I have the following situation: > > when a new record is inserted in to a psql table, i want to be able to update certain fields. the way i'm doing it right now..to find the last record inserted...is by sorting the primary key (serial) in descending order and taking the first item in the list...the one at the top should be the one i just entered...and of course, i then procede to update the fieldsd i need to update.. > So, basically would like to know if this will always work. will the one at the top (the one w/ the highest primary key [serial type]) always be the one i just entered. AND does psql ALWAYS execute the trigger(s) after EVERY single record that is inerted. what would happen if [ n ] different users try to insert records simultaniously? will psql insert one-execute the trigger, insert the next-execute the trigger..and so on? or will it insert ALL [n] records AND THEN execute the trigger? If you are writing an insert trigger the "new" record should have the value being inserted into serial column. You don't want to do a select for the highest value to get this value. Besides being less efficient, you have a more complicated locking situation to worry about.