Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 76B9547663A for ; Mon, 16 Dec 2002 15:33:32 -0500 (EST) Received: from news.hub.org (news.hub.org [64.49.215.80]) by postgresql.org (Postfix) with ESMTP id 2744D476946 for ; Mon, 16 Dec 2002 15:19:55 -0500 (EST) Received: by news.hub.org (Postfix, from userid 8) id 0FFA843A57A; Mon, 16 Dec 2002 15:19:55 -0500 (EST) From: "Gaetano Mendola" X-Newsgroups: comp.databases.postgresql.admin, comp.databases.postgresql.performance Subject: Function cachable is not anymore inside a function !!!! Date: Mon, 16 Dec 2002 21:19:54 +0100 Organization: MBI Lines: 86 Message-ID: X-Complaints-To: usenet@news.hub.org X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 To: pgsql-admin@postgresql.org, pgsql-performance@postgresql.org X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200212/208 X-Sequence-Number: 6703 Hi all, take a look a this function: CREATE FUNCTION sp_sm_status_user ( TEXT ) RETURNS INTEGER AS' DECLARE a_status ALIAS FOR $1; my_counter INTEGER; BEGIN SELECT INTO my_counter count(*) FROM user_data WHERE id_user_status = sp_lookup_id(''user_status'', a_status); RETURN my_counter; END; ' LANGUAGE 'plpgsql'; now take a look a these explain: # explain analyze select sp_sm_status_user('Active'); NOTICE: QUERY PLAN: Result (cost=0.00..0.01 rows=1 width=0) (actual time=5004.57..5004.57 rows=1 loops=1) Total runtime: 5004.60 msec EXPLAIN if I do the same query inside the function: # explain analyze SELECT count(*) FROM user_data where id_user_status = sp_lookup_id('user_status', 'Active'); NOTICE: QUERY PLAN: Aggregate (cost=271.14..271.14 rows=1 width=0) (actual time=27.29..27.29 rows=1 loops=1) -> Seq Scan on user_data (cost=0.00..259.65 rows=4596 width=0) (actual time=0.03..21.64 rows=4592 loops=1) Total runtime: 27.35 msec EXPLAIN The function sp_lookup_id is cachable but is seems that is not used If I store the result in a variable all behaviour change: CREATE OR REPLACE FUNCTION sp_sm_status_user ( TEXT ) RETURNS INTEGER AS' DECLARE a_status ALIAS FOR $1; my_counter INTEGER; my_value INTEGER; BEGIN my_value := sp_lookup_id(''user_status'', a_status); SELECT INTO my_counter count(*) FROM user_data WHERE id_user_status = my_value; RETURN my_counter; END; ' LANGUAGE 'plpgsql'; Ciao Gaetano