X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id EA316D1DAFB for ; Tue, 9 Mar 2004 21:22:10 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 66612-02 for ; Tue, 9 Mar 2004 17:22:09 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id 9025FD1DAC4 for ; Tue, 9 Mar 2004 17:22:07 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id i29LM11Y026872 for ; Tue, 9 Mar 2004 21:22:01 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id i29LLOjd026777 for pgsql-sql@postgresql.org; Tue, 9 Mar 2004 21:21:24 GMT From: mike_moran@mac.com (Mike Moran) X-Newsgroups: comp.databases.postgresql.sql Subject: Re: Dramatic slowdown of sql when placed in a function Date: 9 Mar 2004 13:21:30 -0800 Organization: http://groups.google.com Lines: 37 Message-ID: References: <971d398a3317a5af5086277b2a1487f4@news.teranews.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: groups-abuse@google.com To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200403/93 X-Sequence-Number: 17192 Jeff Boes wrote in message news:<971d398a3317a5af5086277b2a1487f4@news.teranews.com>... > At some point in time, mike_moran@mac.com (Mike Moran) wrote: > > >Hi. I currently have a fairly large query which I have been optimizing > >for later use in a function. There are things in the query which I > >have been keeping constant whilst optimizing, but which are variables > >in the function. When I run this query as sql, with the `variables' > >constant, I get a runtime of about 3 or 4 seconds. However, when I > >place this same sql in an sql function, and then pass my constants > >from before in as arguments, I get a runtime of about 215 seconds. > > [ ... ] > > My first guess would be that the indexes being used in the query are > mis-matching on data type compared to your function arguments. For instance, [ ... ] Hi. I think it is something like this that is going on. A couple of the variables are dates which are specified in the table as 'timestamp without time zone', whilst the function was using 'timestamp with time zone'. I confirmed the slowdown by casting the types to the 'slow' type in the original query. However, when I change the signature of the function and do a cast of the variable within the function body I still get the same speed. I even cast the arguments to the function given at the psql prompt and still I get the same speed. I will have to sanity-check this again tomorrow (posting from home) but I couldn't see anywhere else that I could force the type to be the same as that specified on the table. Many thanks, -- Mike