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 1fC4Pq-0007Uz-Rw for pgsql-general@arkaria.postgresql.org; Fri, 27 Apr 2018 14:32:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fC4Pp-0005Km-Ne for pgsql-general@arkaria.postgresql.org; Fri, 27 Apr 2018 14:32:01 +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 1fC4Ox-000831-CJ for pgsql-general@lists.postgresql.org; Fri, 27 Apr 2018 14:31:07 +0000 Received: from 23-25-19-49-static.hfc.comcastbusiness.net ([23.25.19.49] helo=jeeves.wrkhors.com) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fC4Ot-0006a2-Bp for pgsql-general@lists.postgresql.org; Fri, 27 Apr 2018 14:31:06 +0000 Received: from wrkhors.com (localhost [127.0.0.1]) by jeeves.wrkhors.com (Postfix) with ESMTP id 85BA91171; Fri, 27 Apr 2018 09:41:11 -0500 (CDT) Date: Fri, 27 Apr 2018 09:29:30 -0500 From: Steven Lembark To: pgsql-general@lists.postgresql.org Cc: lembark@wrkhors.com Subject: Re: Long running INSERT+SELECT query Message-ID: <20180427092930.1ee8aa43@wrkhors.com> In-Reply-To: References: <11ea9b74-243d-b275-be10-1004f51a4aa6@gmail.com> <39121e73-b04a-77c4-cb5f-55530e82f0d3@aklaver.com> Organization: Workhorse Computing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, 26 Apr 2018 21:08:01 +0300 Vitaliy Garnashevich wrote: > =C2=A0=C2=A0=C2=A0 INSERT INTO cmdb_sp_usage_history > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (created_by, updated_by, created_on, upda= ted_on, mod_count, > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 summary_on, quarter, product, used_from, = "user", > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 keystrokes, minutes_in_use, times_started= , avg_keystrokes,=20 > max_keystrokes, spkg_operational) > =C2=A0=C2=A0=C2=A0 SELECT > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 2, 2, CURRENT_TIMESTAMP, CURRENT_TI= MESTAMP, 1, > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 CURRENT_TIMESTAMP, quarter.id, "spv= "."product",=20 > "usage"."used_from", "usage"."user", > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 coalesce(sum("usage"."keystrokes"),= 0), > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 coalesce(sum("usage"."minutes_in_us= e"), 0), > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 coalesce(sum("usage"."times_started= "), 0), > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 coalesce(avg("usage"."keystrokes"),= 0), > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 coalesce(max("usage"."keystrokes"),= 0), > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 bool_or("cmdb_ci"."operational") > =C2=A0=C2=A0=C2=A0 FROM > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 "cmdb_program_daily_usage" "usage" > =C2=A0=C2=A0=C2=A0 LEFT OUTER JOIN > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 "cmdb_program_instance" "p" ON "p"."id" = =3D > "usage"."program_instance" LEFT OUTER JOIN > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 "cmdb_ci_spkg" "s" ON "s"."id" =3D "p"."s= pkg" > =C2=A0=C2=A0=C2=A0 LEFT OUTER JOIN > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 "cmdb_ci" "cmdb_ci" ON "s"."id" =3D "cmdb= _ci"."id" > =C2=A0=C2=A0=C2=A0 LEFT OUTER JOIN > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 "cmdb_software_product_version" "spv" ON = "spv"."id" =3D > "s"."software" WHERE ("usage"."minutes_in_use" > 0) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 AND ((NOT ("s"."software" IS NULL)) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 AND ((NOT (= "s"."os" =3D TRUE)) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 OR ("s"."os" IS NULL))) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 AND ("usage"."usage_date" >=3D quarter.st= art_date) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 AND ("usage"."usage_date" < quarter.end_d= ate) > =C2=A0=C2=A0=C2=A0 GROUP BY "spv"."product", "usage"."used_from", "usage= "."user" > =C2=A0=C2=A0=C2=A0 HAVING (coalesce(sum("usage"."keystrokes"), 0) > 0) O= R=20 > (coalesce(sum("usage"."minutes_in_use"), 0) > 0) OR=20 > (coalesce(sum("usage"."times_started"), 0) > 0) > =C2=A0=C2=A0=C2=A0 ORDER BY "spv"."product", "usage"."used_from", "usage= "."user"; create temporary table foobar=20 select from ; This isolates the lock time to performing the bare select, after which you can coalesce and sum to your heart's content without=20 locking any of it. The point is performing the absolute minimum of processing to=20 generate the temp table so as to release any locks quickly and avoid "group by" in the main join. Yes, this might end up creating a large-ish temp table :-) One other approach would be selecting only incremental data (e.g., daily) which locks a much smaller subset of the rows and aggregating the daily totals into quarterly, whatever. Call it daily usage, select where usage_date =3D today's or timestamp && a tstzrange of ( 0000, 2400, [) ). That might also simplify your query logic: all the coalesce op's end up in your daily/weekly/monthly/whatever summary, the quarterly=20 values in the reporting are just sum X group by. --=20 Steven Lembark 1505 National Ave Workhorse Computing Rockford, IL 61103 lembark@wrkhors.com +1 888 359 3508