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 1fBoWE-0001RJ-Pu for pgsql-general@arkaria.postgresql.org; Thu, 26 Apr 2018 21:33:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fBoWD-0006gi-Bf for pgsql-general@arkaria.postgresql.org; Thu, 26 Apr 2018 21:33:33 +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 1fBoWD-0006gX-3I for pgsql-general@lists.postgresql.org; Thu, 26 Apr 2018 21:33:33 +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 1fBoW9-00077h-E4 for pgsql-general@postgresql.org; Thu, 26 Apr 2018 21:33:32 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 50E062146D; Thu, 26 Apr 2018 17:33:28 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 26 Apr 2018 17:33:28 -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=0w/qY3yys9a3grCFRXasQMmFj3S/2 TzY+lZWYQ7wbb0=; b=KbxdoEatvS8MztxY94Qgpwa6FsDa0KHfqkr8WaG75QPl2 ChuDvRcuB/EFiu4k18b0T2fn5eZ06rO114JTgMRDq94yEWstsu+Rn7Zs+vEmf5+r 9FlIGKQmXtR90fEOSTZM11+RrpCNrImYL1KwV2IkdD3ivnXDW4REM6OPoX4cRWcc ApmhzmO472zzsoSEpL8C7/wfhjJR7DmFFPEH5QU6wnJZeAd1gFv7Ndgmp4KsYbdI k/24ya8euse8yvWEK5cL7C/g/aA4n61BQ7t9ub/AWx9BQ+UzQh2sckJcnn9Ji34N 1VMB6Tuw4yZ8Bfi4oFtWgIr8azMMy2ZsUkFxh0zKA== 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=0w/qY3 yys9a3grCFRXasQMmFj3S/2TzY+lZWYQ7wbb0=; b=gb3OCWfmqXqrXsAmJDEDl6 advjrIx4He9xvj5liNMt3Vy3GC5laqP8KfB01SyCsf31JZYFvnhl/tg6vJ/u0YAO /ECSA18ZGcjx+x0o3iifLEAgYafhaXXs5zW8SpPaTTw4WkQ90Manx0pWYzgWm6Tm oFKgcKv8EObLKAk4tVU2ZDR+CJuvNpyF4JlX1BM6QXtcVhvf5wDBCrk1EuownuA6 YSS/WeHawUq9KwE7S+XFiabpoH6IYA9+2yjBIs5PkCSrjIAdpus88xetFTgH0Uhz H/CFc0+UZH0vz2JZyus97iQCX1c8TQzEDGrmCFR+yhNYmLHAtuBEIzsvE4vyz0tg == X-ME-Sender: Received: from [192.168.1.2] (unknown [71.212.135.74]) by mail.messagingengine.com (Postfix) with ESMTPA id B3E1CE4122; Thu, 26 Apr 2018 17:33:27 -0400 (EDT) Subject: Re: Long running INSERT+SELECT query To: Vitaliy Garnashevich , Postgres General References: <11ea9b74-243d-b275-be10-1004f51a4aa6@gmail.com> <39121e73-b04a-77c4-cb5f-55530e82f0d3@aklaver.com> <320a63d9-71b5-90d1-fd2c-9ed408ae1016@aklaver.com> <5893718d-4449-741f-671a-aea1ee654944@gmail.com> From: Adrian Klaver Message-ID: <823b9271-02ba-c42a-ba90-71dfdf6ce5ed@aklaver.com> Date: Thu, 26 Apr 2018 14:33:26 -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: <5893718d-4449-741f-671a-aea1ee654944@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 04/26/2018 01:32 PM, Vitaliy Garnashevich wrote: > >> Have not worked through all of the above, but a first draft suggestion: >> >> Move the SELECT minus the aggregation functions into a sub-query that >> uses FOR UPDATE. Then do the aggregation on the results of the sub-query. > > The aggregated table has hundreds of millions of rows, and the query > runs for many hours (which is one of the reasons why it's better not to > fail). I really doubt that row level locking would work. That would be a > lot of RAM just to hold all the locks. > > On the other hand, I don't see something like FOR KEY SHARE kind of > locks at table level, so that the query would try not to block most of > other existing activity (e.g. SELECTs, UPDATEs). > > Maybe this could be solved by calculating results into a temporary > table, which would not check foreign key constraints, and then copy the > data into the actual results table, while checking each row for FK > consistency and skipping if necessary. But then I don't think it would > be possible for my transaction to see row deletions which other > transactions have done, and to check row existence (the transaction is > there, because the whole thing is implemented as a DO statement with > some local variables). > > Thoughts? The procedure seems to be fighting itself. There is an inherent conflict between trying to keep up with data changes and presenting a consistent result. Keeping up means constantly updating the aggregation calculations which in turn means the result will continually changing. As David and Alvaro have also suggested your best bet is to pick a point in time and work off that. > > Regards, > Vitaliy > > -- Adrian Klaver adrian.klaver@aklaver.com