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 1fBlJT-0002oe-Or for pgsql-general@arkaria.postgresql.org; Thu, 26 Apr 2018 18:08:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fBlJS-0005Ww-8x for pgsql-general@arkaria.postgresql.org; Thu, 26 Apr 2018 18:08:10 +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 1fBlJR-0005Wk-TT for pgsql-general@lists.postgresql.org; Thu, 26 Apr 2018 18:08:10 +0000 Received: from mail-yb0-x22f.google.com ([2607:f8b0:4002:c09::22f]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fBlJN-0002NT-M0 for pgsql-general@postgresql.org; Thu, 26 Apr 2018 18:08:09 +0000 Received: by mail-yb0-x22f.google.com with SMTP id o14-v6so6382294ybq.3 for ; Thu, 26 Apr 2018 11:08:05 -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=lmtYiFdLR5XLhZ8tYvNFjTgQ1vQ3GpxhcBTzQBTNU/o=; b=ufKqDriqxEqjj3yBR7pQVsptuLF/QQRXxz/4oeN1ZTEZh/NOwh3/QKbUN1swV4Y6Lv ibPnvS4o9mBXEkP1L6VdNyuvCSdDQoYJKXwiBP7++W9F0x07zv80vNtVikyNd4UD1kHz XO+P+z6GO6l/DWjLKi+s0DX+wZXCsGm1fMRyIfVqnLhTZttkzzAPieVoK0sXh/z0oa9Z Ml754CwpVC8JP1ZvRUhk7QrMNfROsDUYz60TSkC3r5ceMuKPf+gPR/t0A1KthMyO57Mq aWmbhc0HCYe4fBHcRLoG4iCuwCy2A9k/6hZW/psVemSfd7PqrR8vLNuVxQBeoWVYEKMr 6tDA== 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=lmtYiFdLR5XLhZ8tYvNFjTgQ1vQ3GpxhcBTzQBTNU/o=; b=HxbrGazwzI3kMxiW1LXYdQa7ZtEIzx2t4RWO/PBMIGXx5R5aQGXBHKJJ7QVFSC3Ftc k6NrOpx6PxP7wdcxiHmXtu1U6eUS9SaQxy+KxBzyUWblJvwvMyuxWJT2TuNiWESs1lt1 sK0isT48mGzoMUB56/nhkOANBGRXG5Dv/O7qiGggmMV4Srk/dUSu+COjBKSrQHgwoE/X Qob+6/Oe5TEwzRmSwXZZAzirlUTUvKeWPlAwPbH+ix+GwOylvto56ZoE8aUD9UGgJkS4 2XJD1dgApQjfdLWfQu8FiFa1tym0UGgu0dSlb0Hv22h5jZ1LcWfsnFVV/pmI4Va3blYV L1lA== X-Gm-Message-State: ALQs6tAQKodw3DtBI10TCF6JKK/1xSEllEKTVkfxw8vZ718nJw4QLse2 vMBh6Fj5c5Fdz0/Hi8Q9A5S9hq3t X-Google-Smtp-Source: AIpwx4+eUH1Ws1EnJ9kDLD1iYuHR43HePBqy3IM9bIp1CpEHdTvPMeGsv8PSmg0RGcFRvhh5BJekDA== X-Received: by 2002:a25:c481:: with SMTP id u123-v6mr19082004ybf.425.1524766083102; Thu, 26 Apr 2018 11:08:03 -0700 (PDT) Received: from [192.168.1.151] ([178.54.41.83]) by smtp.gmail.com with ESMTPSA id f2-v6sm8679050ywe.6.2018.04.26.11.08.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 Apr 2018 11:08:02 -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> From: Vitaliy Garnashevich Message-ID: Date: Thu, 26 Apr 2018 21:08:01 +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: <39121e73-b04a-77c4-cb5f-55530e82f0d3@aklaver.com> 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 > Without the query we are flying blind, so suggestions will have a ? Here is one such query:     INSERT INTO cmdb_sp_usage_history       (created_by, updated_by, created_on, updated_on, mod_count,       summary_on, quarter, product, used_from, "user",       keystrokes, minutes_in_use, times_started, avg_keystrokes, max_keystrokes, spkg_operational)     SELECT        2, 2, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 1,        CURRENT_TIMESTAMP, quarter.id, "spv"."product", "usage"."used_from", "usage"."user",        coalesce(sum("usage"."keystrokes"), 0),        coalesce(sum("usage"."minutes_in_use"), 0),        coalesce(sum("usage"."times_started"), 0),        coalesce(avg("usage"."keystrokes"), 0),        coalesce(max("usage"."keystrokes"), 0),        bool_or("cmdb_ci"."operational")     FROM       "cmdb_program_daily_usage" "usage"     LEFT OUTER JOIN       "cmdb_program_instance" "p" ON "p"."id" = "usage"."program_instance"     LEFT OUTER JOIN       "cmdb_ci_spkg" "s" ON "s"."id" = "p"."spkg"     LEFT OUTER JOIN       "cmdb_ci" "cmdb_ci" ON "s"."id" = "cmdb_ci"."id"     LEFT OUTER JOIN       "cmdb_software_product_version" "spv" ON "spv"."id" = "s"."software"     WHERE ("usage"."minutes_in_use" > 0)       AND ((NOT ("s"."software" IS NULL))            AND ((NOT ("s"."os" = TRUE))                 OR ("s"."os" IS NULL)))       AND ("usage"."usage_date" >= quarter.start_date)       AND ("usage"."usage_date" < quarter.end_date)     GROUP BY "spv"."product", "usage"."used_from", "usage"."user"     HAVING (coalesce(sum("usage"."keystrokes"), 0) > 0) OR (coalesce(sum("usage"."minutes_in_use"), 0) > 0) OR (coalesce(sum("usage"."times_started"), 0) > 0)     ORDER BY "spv"."product", "usage"."used_from", "usage"."user"; Regards, Vitaliy