Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id DA7859FB6C0 for ; Thu, 3 May 2007 14:04:39 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 07780-01-2 for ; Thu, 3 May 2007 14:04:29 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from wr-out-0506.google.com (wr-out-0506.google.com [64.233.184.226]) by postgresql.org (Postfix) with ESMTP id 87B069FBCDC for ; Thu, 3 May 2007 14:04:28 -0300 (ADT) Received: by wr-out-0506.google.com with SMTP id 70so575306wra for ; Thu, 03 May 2007 10:04:27 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=MlSaqpgsYgz+4joSsooiOsZPkyc+HIUmyK8PfGhXO4A/r8hw3Upx+RWi0dZBhoeNEF2xGhXmUNpiyfPNzNkB/buvTXaEq8oNmeU56Kdxb9DEHa59wKNBajwGLIhm79PnfKuKCGzoHGi+Bg13AYFFIxO8BLDa6D7kOdoNUS2pdYM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=SuxCwHEatFK/PkSiHsu2xWcd7kMj8eIZSoe3AeSvaJF7VJw0wSsEAtX/PrcNnz2f/q3aQFYQql3Rc8YdmvLOlMQ8sUaPDj/On297tSbFsSpAu9amGOdze1qQ1bsloznsGSvuQbqJLMP00K88wUKkRAaDSxppczWZ2MszuytSZ2I= Received: by 10.115.55.1 with SMTP id h1mr737158wak.1178211867049; Thu, 03 May 2007 10:04:27 -0700 (PDT) Received: by 10.114.37.3 with HTTP; Thu, 3 May 2007 10:04:26 -0700 (PDT) Message-ID: Date: Thu, 3 May 2007 10:04:26 -0700 From: "Collin Peters" To: pgsql-sql@postgresql.org Subject: Insert into VIEW using RULE. Not possible to use nextval()? MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/17 X-Sequence-Number: 28298 I am having the same problem that is documented elsewhere in the archives. Namely when you have a INSERT RULE on a VIEW the nextval() function doesn't behave properly (or it doesn't behave how I'd like it to). http://archives.postgresql.org/pgsql-sql/2003-07/msg00333.php I'm wondering if anything has changed for this in regards to best practices? Suggested solutions are to change to a trigger or use currval() for your secondary INSERTS inside the RULE. A trigger does not apply to my case as I am basically using this as a shortcut to manually doing two INSERTs. Is there any problems with using the currval() approach? If I use that within the same call is there any chance of that not returning the correct value? (e.g. if this INSERT RULE is being called a 1000 times at once, is it guaranteed to be correct? Another option I see is to place the INSERT inside a LOOP. For example instead of: INSERT INTO user_activity_single(user_activity_id, activity_date, user_activity_type_id, user_activity_action_id, user_id, div1) SELECT nextval('user_activity_user_activity_id_seq'), etc.... have: FOR mviews IN SELECT nextval('user_activity_user_activity_id_seq') as id, CURRENT_DATE, 1, 2, 27, 'foo' LOOP INSERT INTO user_activity_single(mviews.id, etc...) END LOOP; Performance wise this doesn't seem as good. In my case the SELECT statement would be around 4000 records. Any tips for me? Regards, Collin Peters