Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fBkYU-0007d6-Oh for pgsql-general@arkaria.postgresql.org; Thu, 26 Apr 2018 17:19:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fBkYT-0003Mp-D0 for pgsql-general@arkaria.postgresql.org; Thu, 26 Apr 2018 17:19:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fBkYR-0003Mf-Pi for pgsql-general@lists.postgresql.org; Thu, 26 Apr 2018 17:19:37 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fBkYM-0001F0-AP for pgsql-general@postgresql.org; Thu, 26 Apr 2018 17:19:35 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 6C80321448; Thu, 26 Apr 2018 13:19:26 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 26 Apr 2018 13:19:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=f4ZKrVYM4J+357J8M8BcvADOC6Jth yuh5RCCU38X6kY=; b=kqKVLq64nWILRg8+Oo/uJ1Nh7AhbGVUj2JsBGMTtbqWYn aYJFaP26Z8VqAN7CWGk1q5WkHFF1LCQA+RUYCLxVCMFeYGVkVKtj4kvAuMvpB8jB LNpRM93fThELgpsVjShlbUKf/gaa4Q90bazIZq8Ag20ZvxXoF3Jvgnmj/fKyGMot HSmiWwJtAjM9+JILajsPynhggD5reyEEbevo2+o8HnUOWCUFa8k9qHmMJqMNm53H fGFA/N19UwOtGKrw7ASQ6lKIAVUwPwWTkXJfzllff96G5HKScgC437Dbg0ANOuQD WpkxwIvpVewryeJ1oBNNPFOjseX3xP1v9yBB0sUcw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=f4ZKrV YM4J+357J8M8BcvADOC6Jthyuh5RCCU38X6kY=; b=mzA0TL2Jph5is2LYfLXKUg PH/Avat4cPwsUig+L4nEX88YnWga+S0hRwKSrHYISwtj79t4cOjcH/eFgIz6uc3Z N2lEfDUmcZH+RmMOjRZkJ8Ka/W7lIER4Beu94rHmrxodU0Vs46NgBHZX+C1/xAVx scC1Kefj9ShVNg6pfKc0sQRQi3zB4X/ESqwlJC2EzmsvU23E1Z0FGmC1P4m3pfUm /U1sByWRtmxcslrCbBOWpThEkV6BnJR4CcKijkctzd7b9XBXkdogymNdORdLGWEZ varHzKmq43RS3ejMcyzQcWuUVylvro5q01sG6N/amwxs7J36iKkd9r9BpulyCMjQ == X-ME-Sender: Received: from ranger.suse (c-73-239-117-238.hsd1.wa.comcast.net [73.239.117.238]) by mail.messagingengine.com (Postfix) with ESMTPA id B0411E4488; Thu, 26 Apr 2018 13:19:25 -0400 (EDT) Subject: Re: Long running INSERT+SELECT query To: Vitaliy Garnashevich , Postgres General References: <11ea9b74-243d-b275-be10-1004f51a4aa6@gmail.com> From: Adrian Klaver Message-ID: Date: Thu, 26 Apr 2018 10:19:24 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <11ea9b74-243d-b275-be10-1004f51a4aa6@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 04/26/2018 09:13 AM, Vitaliy Garnashevich wrote: > Hi, > > We're trying to populate a table with aggregated data from other tables. > For that we're running a huge INSERT+SELECT query which joins several > tables, aggregates values, and then inserts the results into another > table. The problem we're facing is that while the query is running , > some records in tables referenced by the results table may be deleted, > which causes the following error: > > ERROR:  insert or update on table "..." violates foreign key constraint > "..." > DETAIL:  Key (...)=(...) is not present in table "...". > SELECT .. FOR UPDATE?: https://www.postgresql.org/docs/10/static/sql-select.html#SQL-FOR-UPDATE-SHARE https://www.postgresql.org/docs/10/static/explicit-locking.html#LOCKING-ROWS > Who do we make sure that such aggregating query would not fail? > > Regards, > Vitaliy > > > -- Adrian Klaver adrian.klaver@aklaver.com