pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Vitaliy Garnashevich <vgarnashevich@gmail.com>
To: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Long running INSERT+SELECT query
Date: Thu, 26 Apr 2018 14:33:26 -0700
Message-ID: <823b9271-02ba-c42a-ba90-71dfdf6ce5ed@aklaver.com> (raw)
In-Reply-To: <5893718d-4449-741f-671a-aea1ee654944@gmail.com>
References: <11ea9b74-243d-b275-be10-1004f51a4aa6@gmail.com>
	<a596f16f-e665-6d03-2e92-bed2e276bb68@aklaver.com>
	<ce1a27f4-6085-6be4-8b9e-08598fd7e5eb@gmail.com>
	<39121e73-b04a-77c4-cb5f-55530e82f0d3@aklaver.com>
	<f2b804fc-bd9a-fcf4-f3bd-cb1b129daab4@gmail.com>
	<320a63d9-71b5-90d1-fd2c-9ed408ae1016@aklaver.com>
	<5893718d-4449-741f-671a-aea1ee654944@gmail.com>

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




view thread (16+ messages)  latest in thread

Message-ID: <823b9271-02ba-c42a-ba90-71dfdf6ce5ed@aklaver.com>
Permalink:  ../823b9271-02ba-c42a-ba90-71dfdf6ce5ed@aklaver.com/
Also on:    postgresql.org/message-id/823b9271-02ba-c42a-ba90-71dfdf6ce5ed@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, vgarnashevich@gmail.com
  Subject: Re: Long running INSERT+SELECT query
  In-Reply-To: <823b9271-02ba-c42a-ba90-71dfdf6ce5ed@aklaver.com>
  References: <11ea9b74-243d-b275-be10-1004f51a4aa6@gmail.com> <a596f16f-e665-6d03-2e92-bed2e276bb68@aklaver.com> <ce1a27f4-6085-6be4-8b9e-08598fd7e5eb@gmail.com> <39121e73-b04a-77c4-cb5f-55530e82f0d3@aklaver.com> <f2b804fc-bd9a-fcf4-f3bd-cb1b129daab4@gmail.com> <320a63d9-71b5-90d1-fd2c-9ed408ae1016@aklaver.com> <5893718d-4449-741f-671a-aea1ee654944@gmail.com> <823b9271-02ba-c42a-ba90-71dfdf6ce5ed@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox