pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Ray <rray@mstc.state.ms.us>
To: pgsql-sql@postgresql.org
Subject: Re: Query is fast and function is slow
Date: Thu, 7 Dec 2006 08:53:31 -0600 (CST)
Message-ID: <Pine.LNX.4.64.0612070826040.16909@rray.drdc.mstc.ms.gov> (raw)
In-Reply-To: <200612071053.01287.mlists@rp-online.de>
References: <Pine.LNX.4.64.0612060934410.15200@rray.drdc.mstc.ms.gov>
	<200612071053.01287.mlists@rp-online.de>

On Thu, 7 Dec 2006, Thomas Pundt wrote:

> 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?

Actually "doc_num" is char(9)
I changed text to char(9) and got same slow results

>
> I'd try putting a "raise notice '%', explain analyze ..." statement into the
> function and check the log file.

It appears that the function is not using the index
The table documents has a index on doc_num and doc_num is a unique value
dcc=# explain analyze select doc_num from documents where doc_num = 
'106973821';
                                                          QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
  Index Scan using documents_pkey on documents  (cost=0.00..5.48 rows=1 
width=13) (actual time=37.475..37.481 rows=1 loops=1)
    Index Cond: (doc_num = '106973821'::bpchar)
  Total runtime: 37.535 ms
(3 rows)

dcc=#

But this same statement in a function takes several minutes;

My SQL knowledge is pitiful so would you explain how to use
"explain analyze" in the function

I get errors when I try to load the file with
raise notice  ''%'',explain analyze select doc_num from documents where 
doc_num = doc_number;

dcc=# \i 
/src/check_for_update_permission
psql:/src/check_for_update_permission:52: 
ERROR:  syntax error at or near "analyze" at character 16
QUERY:  SELECT explain analyze select doc_num from documents where doc_num 
=  $1
CONTEXT:  SQL statement in PL/PgSQL function "check_for_update_permission" 
near line 18
psql:/src/check_for_update_permission:52: 
LINE 1: SELECT explain analyze select doc_num from documents where d...
psql:/src/check_for_update_permission:52: 
^
dcc=#

>
> Ciao,
> Thomas
>
>



view thread (7+ messages)  latest in thread

Message-ID: <Pine.LNX.4.64.0612070826040.16909@rray.drdc.mstc.ms.gov>
Permalink:  ../Pine.LNX.4.64.0612070826040.16909@rray.drdc.mstc.ms.gov/
Also on:    postgresql.org/message-id/Pine.LNX.4.64.0612070826040.16909@rray.drdc.mstc.ms.gov

 · 

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-sql@postgresql.org
  Cc: rray@mstc.state.ms.us
  Subject: Re: Query is fast and function is slow
  In-Reply-To: <Pine.LNX.4.64.0612070826040.16909@rray.drdc.mstc.ms.gov>
  References: <Pine.LNX.4.64.0612060934410.15200@rray.drdc.mstc.ms.gov> <200612071053.01287.mlists@rp-online.de> <Pine.LNX.4.64.0612070826040.16909@rray.drdc.mstc.ms.gov>

* 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