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 1fBo4h-0007Xa-Nc for pgsql-general@arkaria.postgresql.org; Thu, 26 Apr 2018 21:05:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fBo4f-0004Fv-PU for pgsql-general@arkaria.postgresql.org; Thu, 26 Apr 2018 21:05:05 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fBo4f-0004Fk-Ay for pgsql-general@lists.postgresql.org; Thu, 26 Apr 2018 21:05:05 +0000 Received: from mail-qt0-x241.google.com ([2607:f8b0:400d:c0d::241]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fBo4b-0004Y8-Tj for pgsql-general@postgresql.org; Thu, 26 Apr 2018 21:05:03 +0000 Received: by mail-qt0-x241.google.com with SMTP id z23-v6so33460965qti.5 for ; Thu, 26 Apr 2018 14:05:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=bLaEquDot41n+yqiu3ni7J5hZNvSJtSxsyVavGdeh9c=; b=syYnxp6aNDzeazvZq8j4Z2ZiLMJ2KwlcmfCgOJc6zk3JOM7strt/+G871NHTj2qs9v 5/RMypvYQs8oB93sJ6IB6zQEIdzE3hlagYQdFL/57kEUIxWXB2glLcNFqeA8TSVWglsh qdXpyoNYFHYyPboJqBQFp4rci5EQJE4XnmAQkzihPloXfo8yRenyGRRhmpa0mBQRzH2x A3klRbB0/WWvfsDHz61uADd6Jiye7N17zF6W3ZN5d0LCvsobScpNlIg2Y5abKASmBssq 9RfNtwpY0SmprMvTitxO9ByOv+EoFJrG71RTFq6kFaFigJxiYEblDrNhxszZ8S2/h386 dXZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=bLaEquDot41n+yqiu3ni7J5hZNvSJtSxsyVavGdeh9c=; b=dwzzH4qGCBn9o7kGdl1SzHmhdUOZ0L0o6t1AmOTKM9/yNr5AjOabF4nEvgY/hEQNWj jru0xxxsnnJwtsVusuFcRHvha8kRmG8Ofxqsn6KQM+DiwdvjaymyneFJ1IhLR06A6JMr 88bh5PbuQYP7nTySVJ8ohW/5VQh/d5uEKWVURY7bi8hJrOZrfwNCqJAmXG39jsKeqnMV oCGMlCNKxzfyhnd8b8kmNyiIoGTQSZKtyOrFI0zIhNhxaOqaHr2Uuw0KZ0//kPSIltTf Ao1xsv/tgwqTnBHhh8OMZVfFaW79w8EczpxXl4mPnUmV8KDGUclcktaM1DwrphZdbkuB NY5A== X-Gm-Message-State: ALQs6tDKVPL1ZKb58/l6eGvEvHSUnYqmxw9dQHqWqbw2qt7M3bhX86Ma KMC2jPC6dIGqu0sn/A37iCyoLw6F X-Google-Smtp-Source: AB8JxZrJQ8Vej12gqFTA5gvoJV281N82/f2KzsbsqfLxlRpztRiEvmWeuoX/qZyR3ljujNjPCP6Xog== X-Received: by 2002:ac8:2b50:: with SMTP id 16-v6mr3084516qtv.260.1524776700737; Thu, 26 Apr 2018 14:05:00 -0700 (PDT) Received: from alvin.alvh.no-ip.org ([179.56.29.253]) by smtp.gmail.com with ESMTPSA id p63sm3492428qkd.92.2018.04.26.14.04.59 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 26 Apr 2018 14:04:59 -0700 (PDT) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id 22D9383D; Thu, 26 Apr 2018 18:04:58 -0300 (-03) Date: Thu, 26 Apr 2018 18:04:58 -0300 From: Alvaro Herrera To: Vitaliy Garnashevich Cc: Adrian Klaver , Postgres General Subject: Re: Long running INSERT+SELECT query Message-ID: <20180426210458.phfgeaxsrl2f6zhk@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <5893718d-4449-741f-671a-aea1ee654944@gmail.com> User-Agent: NeoMutt/20170306-137-4415bd-dirty (1.8.0) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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. Row locks are not stored in memory. Of course, a FOR KEY SHARE lock would block DELETEs that try to remove the locked row. I think your proposed strategy of trying to merge what other processes did while you were copying is very problematic. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services