Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TZNDX-0001iY-78 for pgsql-bugs@postgresql.org; Fri, 16 Nov 2012 14:48:27 +0000 Received: from cassarossa.samfundet.no ([2001:700:300:1800::1919]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TZNDW-0002uj-Da for pgsql-bugs@postgresql.org; Fri, 16 Nov 2012 14:48:26 +0000 Received: from akuma.no ([2001:700:300:1800::666]) by cassarossa.samfundet.no with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72) (envelope-from ) id 1TZNDU-0001es-Ck for pgsql-bugs@postgresql.org; Fri, 16 Nov 2012 15:48:24 +0100 Received: from xim by akuma.no with local (Exim 4.80) (envelope-from ) id 1TZNDT-0001pH-Lb for pgsql-bugs@postgresql.org; Fri, 16 Nov 2012 15:48:23 +0100 Date: Fri, 16 Nov 2012 15:48:23 +0100 From: Morten Minde Neergaard To: pgsql-bugs@postgresql.org Subject: Re: BUG #7651: Superfluous calls to functions used for indexing Message-ID: <20121116144823.GG610@8d.no> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1353019478.14335.23.camel@sussancws0025> User-Agent: Mutt/1.5.21 (2010-09-15) X-SA-Exim-Connect-IP: X-SA-Exim-Mail-From: m@8d.no X-SA-Exim-Scanned: No (on akuma.no); SAEximRunCond expanded to false X-Pg-Spam-Score: 0.7 (/) X-Archive-Number: 201211/94 X-Sequence-Number: 34652 At 14:44, Thu 2012-11-15, Jeff Davis wrote: […] > It's possible that the function call may be unnecessary, but that is > more of a performance enhancement, not a bug. For me it's a matter of orders of size of performance, so it feels like a bug ;p > Also, the example function has side effects. If you declare functions > with side effects to be IMMUTABLE, you can get all kinds of problems. > You should certainly not rely on an IMMUTABLE function to be called a > specific number of times. The function was only a minimal example, not what I'm running in my production code =) At 11:40, Fri 2012-11-16, Stuart Bishop wrote: > I think this is the same issue as was discussed here, dating from > PostgreSQL 8.1: > > http://postgresql.1045698.n5.nabble.com/Slow-functional-indexes-td2059587.html Yup, looks like the same thing. Would be nice to have this fixed. Did, however, find two workarounds possible from PostgreSQL 9.2: 1. Create an index over all the columns you want. The index-only scan eliminates recalculation 2. Create an index over (my_complex_function(whatever_columns), table_primary_key) and wrap the query as such: SELECT a_lot_of_columns FROM my_table WHERE table_primary_key IN (SELECT table_primary_key FROM my_table ORDER BY my_complex_function(whatever_columns)); Did stumble across a situation where the function was rerun for *all* rows at some point *despite* having such an index. Will see if I can reproduce that if I have the time! Smiles, -- Morten Minde Neergaard