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 E410ED1E936 for ; Wed, 31 Mar 2004 15:33:18 +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 55949-05 for ; Wed, 31 Mar 2004 11:33:20 -0400 (AST) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 4334DD1E2AF for ; Wed, 31 Mar 2004 11:33:16 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i2VFXKTL005020; Wed, 31 Mar 2004 10:33:20 -0500 (EST) To: "Olivier Hubaut" Cc: pgsql-sql@postgresql.org Subject: Re: Is it normal that functions are so much faster than inline queries In-reply-to: References: Comments: In-reply-to "Olivier Hubaut" message dated "Wed, 31 Mar 2004 15:28:10 +0300" Date: Wed, 31 Mar 2004 10:33:20 -0500 Message-ID: <5019.1080747200@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200403/273 X-Sequence-Number: 17372 "Olivier Hubaut" writes: > When I want to execute this set of queries in a function: > ... > It takes only 2 seconds. > But when I tried to do it directly in the psql term (replacing the $1 > value with the same used in the function call), I'm obliged to kill the > second query after 10 minutes because it's still runnning! You're presumably getting different plans in the two cases. Usually we hear complaints about the function case being slower, because the planner has less information when it has to work with a parameter instead of a constant. In this case it seems the stupider plan is being chosen with a constant :-(. You have not shown enough information to tell why, but I'm wondering about datatype mismatch preventing an index from being used. What is the declared datatype of the $1 parameter, and does it match what will be assumed for the unadorned constant? regards, tom lane