Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id A9E4FB5DC48 for ; Thu, 3 Nov 2011 15:41:25 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 09001-07 for ; Thu, 3 Nov 2011 18:41:18 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id CAF75B5DC50 for ; Thu, 3 Nov 2011 15:41:17 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id pA3IfGqH006647; Thu, 3 Nov 2011 14:41:16 -0400 (EDT) To: Jay Levitt cc: pgsql-performance@postgresql.org Subject: Re: Predicates not getting pushed into SQL function? In-reply-to: <4EB2D3CB.7020708@gmail.com> References: <4EB2D3CB.7020708@gmail.com> Comments: In-reply-to Jay Levitt message dated "Thu, 03 Nov 2011 13:47:55 -0400" Date: Thu, 03 Nov 2011 14:41:16 -0400 Message-ID: <6646.1320345676@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-3.101 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-1.201 X-Spam-Level: X-Archive-Number: 201111/64 X-Sequence-Number: 45575 Jay Levitt writes: > I'm confused. I have a now-trivial SQL function that, unrestricted, would > scan my whole users table. When I paste the body of the function as a > subquery and restrict it to one row, it only produces one row. When I paste > the body of the function into a view and restrict it to one row, it produces > one row. But when I put it in a SQL function... it scans the whole users > table and then throws the other rows away. > I thought SQL functions were generally inline-able, push-down-able, etc. inline-able, yes, but if they're not inlined you don't get any such thing as pushdown of external conditions into the function body. A non-inlined function is a black box. The interesting question here is why the function doesn't get inlined into the calling query. You got the obvious showstoppers: it has a SETOF result, it's not volatile, nor strict. The only other possibility I can see offhand is that there's some sort of result datatype mismatch, but you've not provided enough info to be sure about that. regards, tom lane