function calls in WHERE clause - Mailing list pgsql-general

From Lee Harr
Subject function calls in WHERE clause
Date
Msg-id BAY2-F19gcTEGi9R6K7000091a0@hotmail.com
Whole thread Raw
Responses Re: function calls in WHERE clause  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
I have a view like this:

CREATE or replace VIEW program_member_this_year AS
    SELECT DISTINCT person.person_id
        FROM unit.person, unit.visit
        WHERE
            unit.person.person_id = unit.visit.person_id
            AND
            visit.program_id = this_program()
            AND
            visit.time_stamp > shared.last_september();



and I would like to speed it up ...

I notice that if I write it like this:

CREATE or replace VIEW program_member_this_year AS
    SELECT DISTINCT person.person_id
        FROM unit.person, unit.visit
        WHERE
            unit.person.person_id = unit.visit.person_id
            AND
            visit.program_id = 3
            AND
            visit.time_stamp > '2004-9-1';

It takes about 1/4 the time as with the function calls.

Both of those function calls will always return the exact
same thing for any given query.

My approach now is to make a Pl/PgSQL function which
will build up the correct query with the function calls
replaced by their constant results .... but is there a
better way?

I should note that this_program() is defined as VOLATILE.

It was a while ago, but I believe this is because I have
a this_program() defined in each schema and they
return a different value depending on which schema
you are in when you call the function...

Thanks for any insight.

_________________________________________________________________
MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.
http://join.msn.com/?page=features/virus


pgsql-general by date:

Previous
From: Gaetano Mendola
Date:
Subject: Re: Salt in encrypted password in pg_shadow
Next
From: Tom Lane
Date:
Subject: Re: function calls in WHERE clause