pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Gaetano Mendola <mendola@bigfoot.com>
To: pgsql-admin@postgresql.org
To: pgsql-performance@postgresql.org
Subject: Function cachable is not anymore inside a function !!!!
Date: Mon, 16 Dec 2002 21:19:54 +0100
Message-ID: <atlcha$b37$1@news.hub.org> (raw)

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











Message-ID: <atlcha$b37$1@news.hub.org>
Permalink:  ../atlcha$b37$1@news.hub.org/
Also on:    postgresql.org/message-id/atlcha$b37$1@news.hub.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: mendola@bigfoot.com, pgsql-performance@postgresql.org
  Subject: Re: Function cachable is not anymore inside a function !!!!
  In-Reply-To: <atlcha$b37$1@news.hub.org>
  References: <atlcha$b37$1@news.hub.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox