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 1fBnZL-0004v3-01 for pgsql-general@arkaria.postgresql.org; Thu, 26 Apr 2018 20:32:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fBnZJ-0008B3-N2 for pgsql-general@arkaria.postgresql.org; Thu, 26 Apr 2018 20:32:41 +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 1fBnZJ-0008As-CU for pgsql-general@lists.postgresql.org; Thu, 26 Apr 2018 20:32:41 +0000 Received: from mail-yb0-x230.google.com ([2607:f8b0:4002:c09::230]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fBnZF-0005my-LR for pgsql-general@postgresql.org; Thu, 26 Apr 2018 20:32:40 +0000 Received: by mail-yb0-x230.google.com with SMTP id i69-v6so7513567ybg.2 for ; Thu, 26 Apr 2018 13:32:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=o7+KZwpe8MbavrU+Cjaq/6m42NibPKVqDc9INwbhkHY=; b=tQtH5p71ZJF+siloaJcmUgnKURhi7Uiykr8FGkq/mHhrVMzinGxuKW0SmxxuPIqjnC Ax3k7D6KGspk+V36FH/VFA6Za3GT9HtiuA4dJdbOcCzy1o0RICE5QJGqi/mtcfC0Ekw+ cWtFw5Z4DF1hs5EHyHMr8bkXuAmwEG2VyOobdlypaxm0scGtEj0spJnd7lsHwdquvhjc s+S000RocUfar1yc9G8AqfP2ap/vIB4KdAqHmsWVxW89SibvNvuViqIONBAlJn8godSd XYg9Q30i8QbmuHcH3pf38tSLbFyHejaP2RtMbA4uqvnRg8KBjOHclo1ShWc5mq43efXa Aq2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=o7+KZwpe8MbavrU+Cjaq/6m42NibPKVqDc9INwbhkHY=; b=OEvuFk9RMuyYtjJ1aOx/efF6vnGynCc2AFTrU6rdf/7bYXFiH9/xeFuKsmX13MvQ2w YITfD1p1tXe6xA3crSYHMyO1o2dYquozD533KYwte8Cd9x6JoEaO8xKLLwSVazLc0KBo UJYhrB9rdFccPNLTmgPjK7rKwglb4KlNZ5qcgLnIaCs2juxnP7Q0PSTdHwrF+eUkuLpW uUTvECkoiP/hI28YAaJ1lZLbEsH28RjujqyixA40JjtbXAqZl8n3NJu002ZCqvDAbefZ ropEwCQYw0M7MKZ+vqgMEXnJ2ToybTOku7dbEhNHEotiAvkz1hD6EPOsp4M4rV88gtLt XWTQ== X-Gm-Message-State: ALQs6tCtwod9Mgw6/6CpE8dj0lDnsAo855P1Ibcjxieyfq927npQH2Xa YRvGGY5yP951IliBYZV8KPYQxF07 X-Google-Smtp-Source: AB8JxZoBEQwxMXNjX4Zd5DU8qrSc/Dm2Io40mqoMcNBSINzJ4/1+davcAZBGwxTgbIIc42dSs+pxvA== X-Received: by 2002:a25:b949:: with SMTP id s9-v6mr7866762ybm.392.1524774755532; Thu, 26 Apr 2018 13:32:35 -0700 (PDT) Received: from [192.168.1.151] ([178.54.41.83]) by smtp.gmail.com with ESMTPSA id v125-v6sm3069910ywg.75.2018.04.26.13.32.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 Apr 2018 13:32:34 -0700 (PDT) Subject: Re: Long running INSERT+SELECT query To: Adrian Klaver , Postgres General References: <11ea9b74-243d-b275-be10-1004f51a4aa6@gmail.com> <39121e73-b04a-77c4-cb5f-55530e82f0d3@aklaver.com> <320a63d9-71b5-90d1-fd2c-9ed408ae1016@aklaver.com> From: Vitaliy Garnashevich Message-ID: <5893718d-4449-741f-671a-aea1ee654944@gmail.com> Date: Thu, 26 Apr 2018 23:32:33 +0300 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <320a63d9-71b5-90d1-fd2c-9ed408ae1016@aklaver.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > 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? Regards, Vitaliy