Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fmv7u-0002V7-Gr for pgsql-general@arkaria.postgresql.org; Tue, 07 Aug 2018 06:05:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fmv7q-0003aW-1s for pgsql-general@arkaria.postgresql.org; Tue, 07 Aug 2018 06:05:46 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fmv7p-0003aP-MZ for pgsql-general@lists.postgresql.org; Tue, 07 Aug 2018 06:05:45 +0000 Received: from mx2.mailbox.org ([80.241.60.215]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fmv7l-00043P-5B for pgsql-general@postgresql.org; Tue, 07 Aug 2018 06:05:44 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id 551C141395; Tue, 7 Aug 2018 08:05:37 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter03.heinlein-hosting.de (spamfilter03.heinlein-hosting.de [80.241.56.117]) (amavisd-new, port 10030) with ESMTP id yP9wuj6SNR1P; Tue, 7 Aug 2018 08:05:34 +0200 (CEST) Message-ID: <1533621599.2438.4.camel@cybertec.at> Subject: Re: Immutable function WAY slower than Stable function? From: Laurenz Albe To: Ken Tanzer , Tom Lane Cc: PG-General Mailing List In-Reply-To: References: <17961.1533597095@sss.pgh.pa.us> Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Date: Tue, 07 Aug 2018 07:59:59 +0200 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Ken Tanzer wrote: > On Mon, Aug 6, 2018 at 4:11 PM Tom Lane wrote: > > Ken Tanzer writes: > > > Hi. I was recently troubleshooting a function, and realized it had > > > incorrectly been declared as Immutable, when it should have been declared > > > Stable. When I changed it to Stable, the query I was running ran > > > dramatically faster. Digging into this a little more, this is what I found: > > spc=> EXPLAIN (ANALYZE,BUFFERS,VERBOSE) SELECT client_id,si_imm(client_id) FROM tbl_residence_own; > QUERY PLAN > ------------------------------------------------------------------------------------------------------------------------------- > Seq Scan on public.tbl_residence_own (cost=0.00..1990.02 rows=6977 width=8) (actual time=3.771..22665.604 rows=6983 loops=1) > Output: client_id, si_imm(client_id) > Buffers: shared hit=199814 > Planning time: 0.156 ms > Execution time: 22677.333 ms > (5 rows) > > spc=> EXPLAIN (ANALYZE,BUFFERS,VERBOSE) SELECT client_id,si_stable(client_id) FROM tbl_residence_own; > QUERY PLAN > ------------------------------------------------------------------------------------------------------------------------------ > Seq Scan on public.tbl_residence_own (cost=0.00..3734.27 rows=6977 width=8) (actual time=3.100..1302.888 rows=6983 loops=1) > Output: client_id, staff_inspector_stable(client_id, target_date()) > Buffers: shared hit=60174 > Planning time: 0.354 ms > Execution time: 1315.746 ms > (5 rows) That seems to say that the _stable function is running much faster. Buth functions don't get inlined. I'd dig into the functions and find out how long the queries in them take. auto_explain is a good helper for that. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com