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 10:19:24 -0700
Message-ID: <a596f16f-e665-6d03-2e92-bed2e276bb68@aklaver.com> (raw)
In-Reply-To: <11ea9b74-243d-b275-be10-1004f51a4aa6@gmail.com>
References: <11ea9b74-243d-b275-be10-1004f51a4aa6@gmail.com>

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




view thread (16+ messages)  latest in thread

Message-ID: <a596f16f-e665-6d03-2e92-bed2e276bb68@aklaver.com>
Permalink:  ../a596f16f-e665-6d03-2e92-bed2e276bb68@aklaver.com/
Also on:    postgresql.org/message-id/a596f16f-e665-6d03-2e92-bed2e276bb68@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: <a596f16f-e665-6d03-2e92-bed2e276bb68@aklaver.com>
  References: <11ea9b74-243d-b275-be10-1004f51a4aa6@gmail.com> <a596f16f-e665-6d03-2e92-bed2e276bb68@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