X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 226989FB35B for ; Fri, 14 Jul 2006 12:45:56 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 33602-07 for ; Fri, 14 Jul 2006 12:45:51 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by postgresql.org (Postfix) with ESMTP id 99FF59FB334 for ; Fri, 14 Jul 2006 12:45:50 -0300 (ADT) Received: from list by ciao.gmane.org with local (Exim 4.43) id 1G1Pqy-0002BU-S0 for pgsql-sql@postgresql.org; Fri, 14 Jul 2006 17:45:20 +0200 Received: from simba.summersault.com ([12.161.105.182]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 14 Jul 2006 17:45:20 +0200 Received: from mark by simba.summersault.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 14 Jul 2006 17:45:20 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Mark Stosberg Subject: INSERT ... SELECT FROM .... FOR UPDATE? Date: Fri, 14 Jul 2006 11:44:10 -0400 Lines: 34 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: simba.summersault.com User-Agent: Thunderbird 1.5 (X11/20060405) In-Reply-To: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/212 X-Sequence-Number: 25786 Hello, This is a re-phrasing of question I asked recently, inquiring about a an approach I'm considering. My query is: > INSERT into item_hit_log (item_id, hit_date, hit_count) > SELECT item_id, CURRENT_DATE + CAST('1 day' AS interval), 0 > FROM items where item_state = 'available'; The "items" table has a few hundred thousand rows in it, and is likely to be updated by other queries while this runs. The error we got last night was: ERROR: insert or update on table "item_hit_log" violates foreign key constraint "item_id_fk" DETAIL: Key (item_id)=(451226) is not present in table "items". Re-running the transaction block a few minutes later worked. From reading the docs, it sounds like "SELECT ... FOR UPDATE" might be the perfect solution here. http://www.postgresql.org/docs/8.1/static/sql-select.html#SQL-FOR-UPDATE-SHARE Since it's hard to test for this kind of failure, I wanted to get the opinions of others here if this would be a good idea. Thanks! Mark