Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jz4a7-0004up-E2 for pgsql-general@arkaria.postgresql.org; Fri, 24 Jul 2020 20:46:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jz4a5-00080k-TA for pgsql-general@arkaria.postgresql.org; Fri, 24 Jul 2020 20:46:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jz4a5-00080d-LH for pgsql-general@lists.postgresql.org; Fri, 24 Jul 2020 20:46:13 +0000 Received: from mailout.easymail.ca ([64.68.200.34]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jz4a2-0006PO-BK for pgsql-general@lists.postgresql.org; Fri, 24 Jul 2020 20:46:13 +0000 Received: from localhost (localhost [127.0.0.1]) by mailout.easymail.ca (Postfix) with ESMTP id A8E40A0836 for ; Fri, 24 Jul 2020 20:46:07 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at emo05-pco.easydns.vpn Received: from mailout.easymail.ca ([127.0.0.1]) by localhost (emo05-pco.easydns.vpn [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id n6ChIu2r5NI9 for ; Fri, 24 Jul 2020 20:46:07 +0000 (UTC) Received: from shr-macbook-pro.local (unknown [4.28.96.150]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mailout.easymail.ca (Postfix) with ESMTPSA id 5219CA0833 for ; Fri, 24 Jul 2020 20:46:06 +0000 (UTC) From: Scott Ribe Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.80.23.2.2\)) Subject: bad JIT decision Message-Id: <7736C40E-6DB5-4E7A-8FE3-4B2AB8E22793@elevated-dev.com> Date: Fri, 24 Jul 2020 14:46:06 -0600 To: pgsql-general@lists.postgresql.org X-Mailer: Apple Mail (2.3608.80.23.2.2) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I have come across a case where PG 12 with default JIT settings makes a = dramatically bad decision. PG11 without JIT, executes the query in <1ms, = PG12 with JIT takes 7s--and explain analyze attributes all that time to = JIT. (The plan is the same on both 11 & 12, it's just the JIT.)=20 It is a complex query, with joins to subqueries etc; there is a decent = amount of data (~50M rows), and around 80 partitions (by date) on the = main table. The particular query that I'm testing is intended as a sort = of base case, in that it queries on a small set (4) of unique ids which = will not match any rows, thus the complex bits never get executed, and = this is reflected in the plan, where the innermost section is: -> Index Scan using equities_rds_id on equities e0 (cost=3D0.42..33.74 = rows=3D1 width=3D37) (actual time=3D6751.892..6751.892 rows=3D0 loops=3D1)= Index Cond: (rds_id =3D ANY ('{..., ..., ..., ...}'::uuid[])) Filter: (security_type =3D 'ETP'::text) Rows Removed by Filter: 4 And that is ultimately followed by a couple of sets of 80'ish scans of = partitions, which show never executed, pretty much as expected since = there are no rows left to check. The final bit is: JIT: Functions: 683 Options: Inlining true, Optimization true, Expressions true, Deforming = true Timing: Generation 86.439 ms, Inlining 21.994 ms, Optimization = 3900.318 ms, Emission 2561.409 ms, Total 6570.161 ms Now I think the query is not so complex that there could possibly be 683 = distinct functions. I think this count must be the result of a smaller = number of functions created per-partition. I can understand how that = would make sense, and some testing in which I added conditions that = would restrict the matches to a single partition seem to bear it out = (JIT reports 79 functions in that case). Given the magnitude of the miss in using JIT here, I am wondering: is it = possible that the planner does not properly take into account the cost = of JIT'ing a function for multiple partitions? Or is it that the planner = doesn't have enough info about the restrictiveness of conditions, and is = therefore anticipating running the functions against a great many rows? -- Scott Ribe scott_ribe@elevated-dev.com https://www.linkedin.com/in/scottribe/