UDF in C slow

Lists: pgsql-general
From: Inanc Seylan <inanc(dot)seylan(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: UDF in C slow
Date: 2012-05-11 13:57:14
Message-ID: 4FAD1ABA.9050806@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi all,

I have implemented a user-defined function in C that returns a boolean
value after some computation. Now I have a query Q such that when I
specify the function in the WHERE clause of Q, Q runs in 40 secs and if
I don't use the function it runs in 4 secs. Then I thought that my
implementation of this function could be slow; so I decided to write a
very simple function that just returns true without any computation. To
my surprise, it also takes around 40 seconds to run Q with the new very
simple function. Does anybody have a clue about what might be going wrong?

Cheers,

Inanc


From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: Inanc Seylan <inanc(dot)seylan(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: UDF in C slow
Date: 2012-05-11 14:21:35
Message-ID: CAF-3MvNt+9yd0VvDnXTV5Y3gNERgZUPtd0HCFk=xRvCijLfFVA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 11 May 2012 15:57, Inanc Seylan <inanc(dot)seylan(at)gmail(dot)com> wrote:
> Hi all,
>
> I have implemented a user-defined function in C that returns a boolean value
> after some computation. Now I have a query Q such that when I specify the
> function in the WHERE clause of Q, Q runs in 40 secs and if I don't use the
> function it runs in 4 secs. Then I thought that my implementation of this
> function could be slow; so I decided to write a very simple function that
> just returns true without any computation. To my surprise, it also takes
> around 40 seconds to run Q with the new very simple function. Does anybody
> have a clue about what might be going wrong?

Is that a VOLATILE, STABLE or IMMUTABLE function? What's the output of
EXPLAIN ANALYZE?
--
If you can't see the forest for the trees,
Cut the trees and you'll see there is no forest.


From: Inanc Seylan <inanc(dot)seylan(at)gmail(dot)com>
To: Alban Hertroys <haramrae(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: UDF in C slow
Date: 2012-05-11 14:39:50
Message-ID: 4FAD24B6.8040704@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

It is IMMUTABLE. I attach the output of EXPLAIN both with and without
the simple function (returning true only) in the query.

On 5/11/12 4:21 PM, Alban Hertroys wrote:
> On 11 May 2012 15:57, Inanc Seylan<inanc(dot)seylan(at)gmail(dot)com> wrote:
>> Hi all,
>>
>> I have implemented a user-defined function in C that returns a boolean value
>> after some computation. Now I have a query Q such that when I specify the
>> function in the WHERE clause of Q, Q runs in 40 secs and if I don't use the
>> function it runs in 4 secs. Then I thought that my implementation of this
>> function could be slow; so I decided to write a very simple function that
>> just returns true without any computation. To my surprise, it also takes
>> around 40 seconds to run Q with the new very simple function. Does anybody
>> have a clue about what might be going wrong?
>
> Is that a VOLATILE, STABLE or IMMUTABLE function? What's the output of
> EXPLAIN ANALYZE?

Attachment Content-Type Size
explain_with_function.txt text/plain 5.4 KB
explain_without_function.txt text/plain 5.8 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Inanc Seylan <inanc(dot)seylan(at)gmail(dot)com>
Cc: Alban Hertroys <haramrae(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: UDF in C slow
Date: 2012-05-11 15:55:23
Message-ID: 5875.1336751723@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Inanc Seylan <inanc(dot)seylan(at)gmail(dot)com> writes:
> It is IMMUTABLE. I attach the output of EXPLAIN both with and without
> the simple function (returning true only) in the query.

EXPLAIN ANALYZE would have been far more helpful. However, the thing
that jumps out at me here is all the seqscans on table "symbols".
Do you not have an index on symbols.id? If you do, perhaps there is
a datatype-mismatch problem preventing it from being used.

regards, tom lane


From: Inanc Seylan <inanc(dot)seylan(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Alban Hertroys <haramrae(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: UDF in C slow
Date: 2012-05-11 16:00:42
Message-ID: 4FAD37AA.9080603@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

I'm quite new to Postgres so I don't know how to read the execution
plans. However it is obvious that the plans for the query with and
without the function are different. I added some indices to the table
roleassertions and it seems to solve this big difference in the
execution times of both queries. So I guess it was not the function that
was the problem in the end. Thanks a lot!

Inanc

On 5/11/12 5:55 PM, Tom Lane wrote:
> Inanc Seylan<inanc(dot)seylan(at)gmail(dot)com> writes:
>> It is IMMUTABLE. I attach the output of EXPLAIN both with and without
>> the simple function (returning true only) in the query.
>
> EXPLAIN ANALYZE would have been far more helpful. However, the thing
> that jumps out at me here is all the seqscans on table "symbols".
> Do you not have an index on symbols.id? If you do, perhaps there is
> a datatype-mismatch problem preventing it from being used.
>
> regards, tom lane