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 1ezNr3-0001n9-GY for pgsql-performance@arkaria.postgresql.org; Fri, 23 Mar 2018 14:39:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ezNr2-0003CL-A5 for pgsql-performance@arkaria.postgresql.org; Fri, 23 Mar 2018 14:39:40 +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 1ezNqA-0006Ba-Ly for pgsql-performance@lists.postgresql.org; Fri, 23 Mar 2018 14:38:46 +0000 Received: from uucp.dinoex.sub.de ([2001:1440:5001:1::2]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ezNq6-0000xH-Ki for pgsql-performance@lists.postgresql.org; Fri, 23 Mar 2018 14:38:45 +0000 Received: from uucp.dinoex.sub.de (uucp.dinoex.sub.de [194.45.71.2]) by uucp.dinoex.sub.de (8.15.2/8.15.2) with ESMTPS id w2NEbwFk006674 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Fri, 23 Mar 2018 15:37:59 +0100 (CET) (envelope-from pmc@citylink.dinoex.sub.org) Received: from citylink.dinoex.sub.org (uucp@localhost) by uucp.dinoex.sub.de (8.15.2/8.15.2/Submit) with UUCP id w2NEbwKd006673; Fri, 23 Mar 2018 15:37:58 +0100 (CET) (envelope-from pmc@citylink.dinoex.sub.org) Received: from gate.oper.dinoex.org (gate-e [192.168.98.2]) by citylink.dinoex.sub.de (8.15.2/8.15.2) with ESMTP id w2NEXJDQ035751; Fri, 23 Mar 2018 15:33:19 +0100 (CET) (envelope-from peter@gate.oper.dinoex.org) Received: from gate.oper.dinoex.org (gate-e [192.168.98.2]) by gate.oper.dinoex.org (8.15.2/8.15.2) with ESMTP id w2NEULbR035481; Fri, 23 Mar 2018 15:30:21 +0100 (CET) (envelope-from peter@gate.oper.dinoex.org) Received: (from peter@localhost) by gate.oper.dinoex.org (8.15.2/8.15.2/Submit) id w2NEULYR035480; Fri, 23 Mar 2018 15:30:21 +0100 (CET) (envelope-from peter) Date: Fri, 23 Mar 2018 15:30:21 +0100 From: Peter To: Laurenz Albe Cc: pgsql-performance@lists.postgresql.org Subject: Re: Should from_collapse be switched off? (queries 10 times faster) Message-ID: <20180323143021.GA31620@gate.oper.dinoex.org> References: <20180323100308.GA1188@gate.oper.dinoex.org> <1521805295.2471.40.camel@cybertec.at> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1521805295.2471.40.camel@cybertec.at> User-Agent: Mutt/1.8.3 (2017-05-23) X-Milter: Spamilter (Reciever: uucp.dinoex.sub.de; Sender-ip: 194.45.71.2; Sender-helo: uucp.dinoex.sub.de;) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (uucp.dinoex.sub.de [194.45.71.2]); Fri, 23 Mar 2018 15:38:00 +0100 (CET) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk The problem appeared when I found the queries suddenly taking longer than usual. Investigation showed that execution time greatly depends on the way the queries are invoked. Consider fn(x) simply a macro containing a plain SQL SELECT statement returning SETOF (further detail follows below): # SELECT fn(x); -> 6.3 sec. # SELECT a from fn(x) as a; -> 1.3 sec. Further investigation with auto_explain shows different plans being chosen. The slower one uses an Index Only Scan, which seems to perform bad. Slightly increasing random_page_cost solves this, but this seems the wrong way, because we are on SSD+ZFS, where random_page_cost actually should be DEcreased, as there is no difference if random or sequential. During this effort I accidentally came upon from_collapse_limit, and setting it off significantly changed things: # SET from_collapse_limit = 1; # SELECT fn(x); -> 0.6 sec. # SELECT a from fn(x) as a; -> 1.2 sec. The plans look different now (obviousely), and again the difference between the two invocations comes from an an Index Only Scan, but this time the Index Only Scan is faster. So now we can reduce random_page_cost in order to better reflect physical circumstances, and then both invocations will be fast.