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 1fBjWP-0002w3-SD for pgsql-general@arkaria.postgresql.org; Thu, 26 Apr 2018 16:13:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fBjWO-00064C-Fw for pgsql-general@arkaria.postgresql.org; Thu, 26 Apr 2018 16:13:24 +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 1fBjWO-000640-5M for pgsql-general@lists.postgresql.org; Thu, 26 Apr 2018 16:13:24 +0000 Received: from mail-yb0-x229.google.com ([2607:f8b0:4002:c09::229]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fBjWL-0008Cr-Mk for pgsql-general@postgresql.org; Thu, 26 Apr 2018 16:13:23 +0000 Received: by mail-yb0-x229.google.com with SMTP id 140-v6so915643ybc.9 for ; Thu, 26 Apr 2018 09:13:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-transfer-encoding:content-language; bh=m7IpTo2Riop3OTbDm6cVJLR/5jbFzw5fV3t0GHfp19I=; b=VOxPJDPlQcyrGLcXJE/JdrzrNucydZGwpteoW4fgF6tN4/EXOm5E7xKVo6TGluX2dS HaHV00CoSDzBp+zAOuxZv2PBajDBpLewe0BetSZeLlWHuz+H9XTf0kAmghpmrQI21b0L oXq0B8Et/pxW0SYZG/UDhREcz4OYKzZUorEiEjtgWxHSrwYzfc8IT4GfMzhMtrTPTynF by5PhmK41QBvj2np6l02eU6bEX2fYllgotHDe7RNXKscSkKEDFYpjFStZuojmEU04CGQ gmmeWyeEnqQQZzTiuqt3buQ0WmoQYpFNvJ4IzLEsLU/uBCqcEi9B0uN9XGPxVQWiyNuU +RgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-transfer-encoding:content-language; bh=m7IpTo2Riop3OTbDm6cVJLR/5jbFzw5fV3t0GHfp19I=; b=iie0Z5jLsdJchObnRCXxO5GpKL68KBP5WcVp6dG15ueTtBJ2hbTcxPQpLqoPwp0WBy Ql0hEOWg7XApi/0+L4g+F8zHAKlF38sZuM1ldbZnFV8rqgG3U5JBp5U47ShdHhWsMayx lgP29Wu4xGfdlbU2rakJFvmCO61NPdA5SB4c9Ro36Rl31lylXU9q5KbojEapx8cqJtc7 GsSMkMKLNgamaBeMmebGCSnOf2a/J2frl9ZQnvAyZCFZxC7XGdqduw0jy/fsW3vJUuNS oxj236x4XCEIWF/bOSwYQigAoZomWGFv9vEP3ajVl9QFpCPlqbBLU0Nm7xvpVYbWRpyp 41Vw== X-Gm-Message-State: ALQs6tAV4sCtZuCPx5xH5l3Nzc2g0bHFqQXjRIwp4+ncCt6HmhjSV/rz pCHB+pg84kzZTZ/sKTHfMFNeZj1t X-Google-Smtp-Source: AIpwx495WzykHI368boCTneqZ6dZjdUEo4bTe0QyifWBOW0kqQxE2j2cqnMaf2Fs8XsqX8K+lAAkmQ== X-Received: by 2002:a25:b042:: with SMTP id e2-v6mr19577720ybj.474.1524759199929; Thu, 26 Apr 2018 09:13:19 -0700 (PDT) Received: from [192.168.1.151] ([178.54.41.83]) by smtp.gmail.com with ESMTPSA id f4-v6sm8715236ywh.76.2018.04.26.09.13.18 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 Apr 2018 09:13:19 -0700 (PDT) To: Postgres General From: Vitaliy Garnashevich Subject: Long running INSERT+SELECT query Message-ID: <11ea9b74-243d-b275-be10-1004f51a4aa6@gmail.com> Date: Thu, 26 Apr 2018 19:13:17 +0300 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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 "...". Who do we make sure that such aggregating query would not fail? Regards, Vitaliy