Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 4E9299F9C35 for ; Thu, 7 Dec 2006 05:51:41 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 27851-05 for ; Thu, 7 Dec 2006 05:51:40 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mail.rp-online.de (mail.rp-online.de [149.221.1.199]) by postgresql.org (Postfix) with ESMTP id D9EA29F938C for ; Thu, 7 Dec 2006 05:51:40 -0400 (AST) Received: from idefix.d.rp-online.de (idefix.d.rp-online.de [149.221.100.194]) by mail.rp-online.de (Postfix) with ESMTP id 43CD52ED81; Thu, 7 Dec 2006 10:51:39 +0100 (CET) From: Thomas Pundt Reply-To: Thomas Pundt To: pgsql-sql@postgresql.org Subject: Re: Query is fast and function is slow Date: Thu, 7 Dec 2006 10:53:01 +0100 User-Agent: KMail/1.9.4 Cc: Richard Ray References: In-Reply-To: Organization: RP Online GmbH MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200612071053.01287.mlists@rp-online.de> X-Anti-Virus: Kaspersky Anti-Virus for MailServers 5.5.10/RELEASE, bases: 07122006 #234720, status: clean X-Archive-Number: 200612/73 X-Sequence-Number: 27029 Hi, On Wednesday 06 December 2006 16:44, Richard Ray wrote: | select count(*) from documents where doc_num = '106973821' and (select | bit_or(group_access) from mda_groups where group_name in (select groname | from pg_user,pg_group where usename = 'bbob' and usesysid = any(grolist) | and (groname ~ '.*owner$' or groname = 'admin'))) & access > | '0'::bit(100); | | returns very fast | | If I create function | | create or replace function check_for_update_permission(text,text) returns | boolean as ' | declare | doc_number alias for $1; | user alias for $2; | doc_count integer; | begin ... | end; | ' language 'plpgsql'; | | | and run "select check_for_update_permission('106973821','bbob');" | it returns the correct info but takes several minutes | Would someone please enlighten me. | Can you do something like explain analyze on a function Just a guess: is the column "doc_num" really of type text? Maybe using "text" in the function lets the planner choose a sequential scan? I'd try putting a "raise notice '%', explain analyze ..." statement into the function and check the log file. Ciao, Thomas -- Thomas Pundt ---- http://rp-online.de/ ----