Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iWPL3-0005fv-9P for pgsql-general@arkaria.postgresql.org; Sun, 17 Nov 2019 18:31:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iWPL1-0007kT-T7 for pgsql-general@arkaria.postgresql.org; Sun, 17 Nov 2019 18:31:55 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1iWPKz-0007kM-WD for pgsql-general@lists.postgresql.org; Sun, 17 Nov 2019 18:31:55 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iWPKw-0004P5-Gw for pgsql-general@postgresql.org; Sun, 17 Nov 2019 18:31:53 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 5ABE821D2B; Sun, 17 Nov 2019 13:31:47 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Sun, 17 Nov 2019 13:31:47 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=z w8lmyInxrXZnY2sGE+YKC2Wv1sjPaJQcyK5Dv+cJeQ=; b=W2OHemtJP7mIUE1Py wK5/i70L0CwGXyk2fYwFh2s5zSFJ6VwK/7Jk4BJpqHk3SqJ+I7+fRuYnjx/qXLIZ NEMnxrt9EuBAJoSGcxwWIHAN2CDq526+ng2RXRzjJlW/8vXkIDLE7XRBZf0P84us 6YjPoWJzAqeuzwxE97EoNbzeifJlB6gROSksxvKmtXWIMAwT+CuBI4ci8qjLoHtu hwceaEDo+eYpNa6YKywb8HQsQYNFbdtIkybCfcqCFEvpRp6uWwl/JlJ7wcMBHLo8 4ne2obbrTM6rYUMmo3CeN77m+9FJR6Zm7Kq9ofUtlWx8AC9fatD9OkxJ5YajTTAH hEd+g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=zw8lmyInxrXZnY2sGE+YKC2Wv1sjPaJQcyK5Dv+cJ eQ=; b=Q6NRa4B/74TNxftsSnaGOMtDUS6AdTD0obTnwcRge9rWCJnExC7A6nwAX Z2cU21MMcI1pwLhTfvsb6lO9SaRjrB6hvFRp1kalI2liyNst9eqb7fxQwrMzPnDS wv2Qkl6xerFr7MDGmh8r4VI8MN/tiIAITN92f3dlVLiIb3Nw7PlvNWUxtLwJoEGc TtaPzScv8+DIXS0BSp7sbqUYoyp4a7Cilm1dAhf+fmEQHsZR9f02sKofxqr43ZNp j7UpvmPOeG1TchaRyQe60fDRAW+Ut1Yygaux+DlLT/v3zLchdiB+3JbaBN86kIpG /53/oYaLjtmUVhLG0CGKemhR5IPDg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrudegfedgvdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefuvfhfhffkffgfgggjtgfgsehtke ertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecukfhppeejuddrvdduvddruddvkedrke eknecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhl rghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.3] (unknown [71.212.128.88]) by mail.messagingengine.com (Postfix) with ESMTPA id 8BA218005A; Sun, 17 Nov 2019 13:31:46 -0500 (EST) Subject: Re: Function's execute overhead reducing To: =?UTF-8?B?0JjQs9C+0YDRjCDQktGL0YHQutC+0YDQutC+?= , "pgsql-general@postgresql.org" References: <115048921573975392@myt6-887fb48a9c29.qloud-c.yandex.net> From: Adrian Klaver Message-ID: <896bf0f4-1d27-1d19-6447-0809837a3aa9@aklaver.com> Date: Sun, 17 Nov 2019 10:31:45 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <115048921573975392@myt6-887fb48a9c29.qloud-c.yandex.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 11/16/19 11:23 PM, Игорь Выскорко wrote: > Hi all! > > The best way to describe my question is to show the code as first: > > create table tst( > id int primary key, > j1 jsonb, > j2 jsonb > ); > > insert into tst > select > ser, > jsonb_build_object( > floor(random() * 10 + 1), floor(random() * 1000 + 1), > floor(random() * 10 + 1), floor(random() * 1000 + 1), > floor(random() * 10 + 1), floor(random() * 1000 + 1) > ), > jsonb_build_object( > floor(random() * 10 + 1), floor(random() * 1000 + 1), > floor(random() * 10 + 1), floor(random() * 1000 + 1), > floor(random() * 10 + 1), floor(random() * 1000 + 1) > ) > from generate_series(1, 500000) ser; > > analyze tst; > > > -- original func is a bit complicated. But it doesn't matter here > create or replace function tst_func(a jsonb, b jsonb) returns bigint > stable > language sql > as $$ > select > sum( > ((_a.value::text)::int - (coalesce(b->>_a.key, '0'))::int)::int > ) > from > jsonb_each(a) _a > $$; > > -- get plain data > explain analyze select > id, > j1, > j2 > from > tst; > > -- use subquery (the same code as in function) > explain analyze select > id, > j1, > j2, > ( > select > sum( > ((_a.value::text)::int - (coalesce(j2->>_a.key, '0'))::int)::int > ) > from > jsonb_each(j1) _a > ) > from > tst; > > -- use function > explain analyze select > id, > j1, > j2, > tst_func(j1, j2) > from > tst; > > select version(); > > > And after run it I got following results: > > CREATE TABLE > INSERT 0 500000 > ANALYZE > CREATE FUNCTION > QUERY PLAN > -------------------------------------------------------------------------------------------------------------- > Seq Scan on tst (cost=0.00..13558.00 rows=500000 width=108) (actual time=0.009..40.348 rows=500000 loops=1) > Planning time: 0.189 ms > Execution time: 56.356 ms > (3 rows) > > QUERY PLAN > ---------------------------------------------------------------------------------------------------------------------------------- > Seq Scan on tst (cost=0.00..1644808.00 rows=500000 width=116) (actual time=0.021..1966.190 rows=500000 loops=1) > SubPlan 1 > -> Aggregate (cost=3.25..3.26 rows=1 width=8) (actual time=0.004..0.004 rows=1 loops=500000) > -> Function Scan on jsonb_each _a (cost=0.00..1.00 rows=100 width=64) (actual time=0.002..0.002 rows=3 loops=500000) > Planning time: 0.072 ms > Execution time: 1982.192 ms > (6 rows) > > QUERY PLAN > ----------------------------------------------------------------------------------------------------------------- > Seq Scan on tst (cost=0.00..138558.00 rows=500000 width=116) (actual time=0.072..5308.897 rows=500000 loops=1) > Planning time: 0.067 ms > Execution time: 5328.196 ms > (3 rows) > > version > ------------------------------------------------------------------------------------------------------------------------------------------------- > PostgreSQL 9.6.13 on x86_64-pc-linux-gnu (Ubuntu 9.6.13-1.pgdg16.04+1), compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609, 64-bit > (1 row) > > > As you see, subquery version needs 2 seconds when func version needs more than 5. And it's sad to see. I love functions and actually this func (its production version) is widely used across our project. > I tried to alter function as immutable and even parallel safe but it doesn't help. > I understand that functions execution is not free, but why so much? > So, my question is: is there any way to make this function works faster? Something similar to prepared statement or maybe rules (create rule) or whatever else is available. My suspicion is that the query in the function is hidden from the planner and so it ends up running two separate SELECT queries without reference to each other. A quick test and possible solution: My results for the above on old machine.: Straight query: QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------- Seq Scan on tst (cost=0.00..1644807.00 rows=500000 width=116) (actual time=0.033..2808.596 rows=500000 loops=1) SubPlan 1 -> Aggregate (cost=3.25..3.26 rows=1 width=8) (actual time=0.005..0.005 rows=1 loops=500000) -> Function Scan on jsonb_each _a (cost=0.00..1.00 rows=100 width=64) (actual time=0.003..0.003 rows=3 loops=500000) Planning Time: 16.162 ms Execution Time: 2846.815 ms Function in query QUERY PLAN ----------------------------------------------------------------------------------------------------------------- Seq Scan on tst (cost=0.00..138557.00 rows=500000 width=116) (actual time=0.119..7048.285 rows=500000 loops=1) Planning Time: 0.105 ms Execution Time: 7098.057 ms I changed the function to: CREATE OR REPLACE FUNCTION public.tst_func(i integer, a jsonb, b jsonb) RETURNS TABLE(id integer, val bigint) LANGUAGE sql STABLE AS $function$ select i, sum( ((_a.value::text)::int - (coalesce(b->>_a.key, '0'))::int)::int ) from jsonb_each(a) _a $function$ Using 'table' function: test=# explain analyze select tst.id, j1, j2 from tst join tst_func(id, j1, j2) as f on tst.id = f.id; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=1.00..531057.00 rows=1 width=108) (actual time=0.042..2002.258 rows=500000 loops=1) -> Seq Scan on tst (cost=0.00..13557.00 rows=500000 width=108) (actual time=0.014..70.936 rows=500000 loops=1) -> Subquery Scan on f (cost=1.00..1.02 rows=1 width=4) (actual time=0.003..0.003 rows=1 loops=500000) Filter: (tst.id = f.id) -> Aggregate (cost=1.00..1.01 rows=1 width=12) (actual time=0.003..0.003 rows=1 loops=500000) -> Function Scan on jsonb_each _a (cost=0.00..1.00 rows=100 width=0) (actual time=0.003..0.003 rows=3 loops=500000) Planning Time: 0.297 ms Execution Time: 2037.601 ms > > PS current situation maybe solved by denormalization with precalculations of function and storing results along with data, but it's not the way i would like to use, because it leads to more issues to solve (invalidation, for instance) > > > > > -- Adrian Klaver adrian.klaver@aklaver.com