Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 55A5F9FA376 for ; Sun, 5 Nov 2006 21:23:52 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 16783-05 for ; Mon, 6 Nov 2006 01:23:49 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id B4EE59F9EEC for ; Sun, 5 Nov 2006 21:23:49 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id kA61Nixw026922; Sun, 5 Nov 2006 20:23:44 -0500 (EST) To: Stuart Bishop cc: Stuart Bishop , pgsql-performance@postgresql.org Subject: Re: Slow functional indexes? In-reply-to: <454E4AC7.9020201@stuartbishop.net> References: <45387666.204@canonical.com> <454E4AC7.9020201@stuartbishop.net> Comments: In-reply-to Stuart Bishop message dated "Sun, 05 Nov 2006 12:34:15 -0800" Date: Sun, 05 Nov 2006 20:23:44 -0500 Message-ID: <26921.1162776224@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200611/55 X-Sequence-Number: 21825 Stuart Bishop writes: > Here is a minimal test case that demonstrates the issue. Can anyone else > reproduce these results? Of the four EXPLAIN ANALYZE SELECT statements at > the end, the one that orders by a user created IMMUTABLE stored procedure is > consistently slower than the other three variants. Wow, interesting. I'm surprised we never realized this before, but here's the deal: the generated plan computes the ORDER BY expressions even if we end up not needing them because the ordering is created by an indexscan rather than an explicit sort step. (Such a sort step would of course need the values as input.) So the differential you're seeing represents the time for all those useless evaluations of the function. The difference in the estimated cost comes from that too --- the code doing the estimation can see perfectly well that there's an extra function call in the plan ... Not sure whether there's a simple way to fix this; it might take some nontrivial rejiggering in the planner. Or maybe not, but I don't have any cute ideas about it at the moment. I wonder whether there are any other cases where we are doing useless computations of resjunk columns? regards, tom lane