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 8B1D99FB2D5 for ; Wed, 12 Jul 2006 10:51:04 -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 30911-07 for ; Wed, 12 Jul 2006 10:50:57 -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 A70209FB28F for ; Wed, 12 Jul 2006 10:50:58 -0300 (ADT) Received: from list by ciao.gmane.org with local (Exim 4.43) id 1G0f6e-0003cZ-AJ for pgsql-sql@postgresql.org; Wed, 12 Jul 2006 15:50:24 +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 ; Wed, 12 Jul 2006 15:50:24 +0200 Received: from mark by simba.summersault.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 12 Jul 2006 15:50:24 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Mark Stosberg Subject: Avoiding RI failure with INSERT ... SELECT FROM Date: Wed, 12 Jul 2006 09:48:37 -0400 Lines: 31 Message-ID: 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) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/191 X-Sequence-Number: 25765 Hello! I got an unexpected SQL failure last night, and want to see how to prevent it in the future. This is with 8.1.3. I have a couple of insert statements that run in a transaction block, initializing rows that will be used the next day: INSERT into item_hit_log SELECT item_id, CURRENT_DATE + CAST('1 day' AS interval), 0 FROM items where item_state = 'available'; INSERT into item_view_log 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, so this takes a bit a run. 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. What's an alternate design that avoids this possible error? Thanks! Mark