Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id B46A41337BBC for ; Wed, 12 Jan 2011 12:39:07 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 44055-01 for ; Wed, 12 Jan 2011 16:39:00 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from exprod7og108.obsmtp.com (exprod7og108.obsmtp.com [64.18.2.169]) by mail.postgresql.org (Postfix) with ESMTP id 41BA1133616B for ; Wed, 12 Jan 2011 12:38:59 -0400 (AST) Received: from source ([209.85.216.177]) (using TLSv1) by exprod7ob108.postini.com ([64.18.6.12]) with SMTP ID DSNKTS3ZItmsn52Afc/rH4Bz5k6VEKpv3w9Q@postini.com; Wed, 12 Jan 2011 08:38:59 PST Received: by qyk27 with SMTP id 27so911789qyk.15 for ; Wed, 12 Jan 2011 08:38:57 -0800 (PST) Received: by 10.224.67.142 with SMTP id r14mr1086591qai.166.1294850337481; Wed, 12 Jan 2011 08:38:57 -0800 (PST) Received: from [192.168.2.36] (va-76-4-65-131.sta.embarqhsd.net [76.4.65.131]) by mx.google.com with ESMTPS id q12sm588252qcu.42.2011.01.12.08.38.55 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 12 Jan 2011 08:38:56 -0800 (PST) Subject: Re: BUG #5816: index not used in function Mime-Version: 1.0 (Apple Message framework v1082) Content-Type: multipart/alternative; boundary=Apple-Mail-110-863037691 From: Korry Douglas X-Priority: 3 (Normal) In-Reply-To: <000001cbb134$46a0f340$6900a8c0@frank> Date: Wed, 12 Jan 2011 11:38:53 -0500 Cc: "'Kevin Grittner'" , Message-Id: References: <000001cbb134$46a0f340$6900a8c0@frank> To: frank X-Mailer: Apple Mail (2.1082) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-4.199 tagged_above=-5 required=5 tests=BAYES_00=-1.9, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_MED=-2.3 X-Spam-Level: X-Archive-Number: 201101/88 X-Sequence-Number: 29110 --Apple-Mail-110-863037691 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=windows-1252 Frank, thanks for educating me.=20 -- Korry > -----Original Message----- > From: Korry Douglas [mailto:korry.douglas@enterprisedb.com]=20 > Sent: Sunday, January 09, 2011 2:34 PM > To: frank > Cc: 'Kevin Grittner'; pgsql-bugs@postgresql.org > Subject: Re: [BUGS] BUG #5816: index not used in function >=20 > =20 >=20 > > We may have different perceptions of something being a 'bug'. I = always >=20 > > have several simple ways of determining it. One of them is when a >=20 > > work-around is in the proposal. Yours is one. >=20 > =20 >=20 > It seems to me that the important question in this case is whether or = not the query produced the correct result. >=20 > The important question by nature is not whether correct result is = produce eventually. In this case, the important thing is the inability = to use the index when in fact, if designed and implemented correctly, it = could. >=20 > =20 >=20 > You are complaining about a performance issue, not a correctness = issue, right? >=20 > No. I am pointing out a low-level-design/implementation defect. Poor = performance is the symptom. Poor performance due to sub-optimal = execution of the spec is a correctness issue. (See my comment on the = nature of a function below) >=20 > =20 >=20 > Kevin's work-around is meant to help you *gain better performance*, = not to obtain correct results when you are getting incorrect results. >=20 > If it is not a bug, why do we need a work-around? >=20 > =20 >=20 > > There can be quite a number of ways of looking at the issue. First, = it >=20 > > is truly an implementation matter (making it in the true sense a = bug). I >=20 > > do not believe that the spec would in formal way say that 'well, = there >=20 > > are caveats where you have to do this and that to work around'. >=20 > =20 >=20 > The "spec" (by which I assume you mean the SQL standard) says nothing = about which execution plan will be selected (by) the optimizer. >=20 > No. Whatever the spec, it will never say that a function will not work = as a function, or something that works outside one will not work once = moved inside. >=20 > =20 >=20 > > >=20 > > If by 'kept from one execution to another' means that (the concept = of) a >=20 > > plan is implemented static, this can be a low level design issue, = which >=20 > > in general will still be regarded as implementation, thus a bug. >=20 > =20 >=20 > The execution plan is not quite static - it is computed the first time = you run the function (within a session) and is discarded when your = session ends (or when the compiled function/execution plan becomes = obsolete because of a change to a dependency). >=20 > =20 >=20 > That is by design. >=20 > Then the design is poor. >=20 > =20 >=20 > If you want a dynamic plan that is re-computed each time you execute = the query, you can get that behavior by using dynamic SQL, as Kevin = suggested. >=20 > =20 >=20 > This seems far fetched and irrelevant. Whatever is truly static should = be implemented static; whatever is dynamic should be implemented = dynamic; whatever is partially static, the static part should be static = and the dynamic part should be dynamic. It is natural and correct = treatment. >=20 > =20 >=20 > Purely dynamic situation in which the final query can not be = determined in any fashion, will have to be constructed either outside of = the function or within, so EXECUTE is the only way to handle. What do = you think of requiring the caller to construct such a static statement = as =93select count(*) from sometable=94 and use EXECUTE? >=20 > =20 >=20 > By the same token, =93select thiscolumn from thistable where = upper(thiscolumn) like $1=94 has to be treated statically for the static = part. The only unknown is the parameter, which can be, by the right = design and implementation, delayed till execution (runtime). The code to = deal with this is what I pointed out (via a conditional). If the plan is = a piece of code, then the conditional will be in it. If the plan is a = piece of text to be further interpreted for actual execution (why would = one want to do it that way?), the conditional could contain a text = reference to two pieces of code (w/o the use of the index). If it is not = properly designed/implemented and such situation results in the loss of = the said ability, it is a defect to be addressed. Whether one wants to = address it is one issue. A defect is a defect. >=20 > =20 >=20 > You seem to suggest that the plan was only built at (the first) = execution. That is poor design/implementation. >=20 > =20 >=20 > Lastly, what is a function? One of the fundamental features of a = function is encapsulation. One is guaranteed some well-defined output = based on well-defined input. No implementation detail is necessary or is = obliged to be available. The user does not have to know what table or = anything for that matter is involved. When you push for what is = suggested as a work-around, it defeats one of the basic purposes for a = function. >=20 > =20 >=20 > More can be said, but why one wants to defend a defect is quite beyond = me. >=20 > =20 >=20 > -- Korry >=20 --Apple-Mail-110-863037691 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=windows-1252

-----Original Message-----
From: Korry Douglas [mailto:korry.douglas@enterprisedb.com]
Sent:
Sunday, = January 09, 2011 2:34 = PM
To: frank
Cc: 'Kevin Grittner'; pgsql-bugs@postgresql.org Subject: Re: [BUGS] BUG #5816: index not used in = function

 

> We may have different perceptions of something being a = 'bug'. I always

> have several simple ways of determining it. One of them is = when a

> work-around is in the proposal. Yours is = one.

 

It seems to me that the important question in this case is = whether or not the query produced the correct = result.

The important question by nature is = not whether correct result is produce eventually. In this= case, the important thing is the inability to use the index when in fact, if = designed and implemented correctly, it could.

 

=

You are complaining about a performance issue, not a correctness = issue, right?

No. I am pointing out a = low-level-design/implementation defect. Poor performance is the symptom. Poor performance due to = sub-optimal execution of the spec is a correctness issue. (See my comment on the = nature of a function below)

 

=

Kevin's work-around is meant to help you *gain better = performance*, not to obtain correct results when you are getting incorrect = results.

If it is not a bug, why do we need = a work-around?

 

=

> There can be quite a number of ways of looking at the = issue. First, it

> is truly an implementation matter (making it in the true = sense a bug). I

> do not believe that the spec would in formal way say that = 'well, there

> are caveats where you have to do this and that to work = around'.

 

The "spec" (by which I assume you mean the SQL standard) says nothing about which execution plan will be selected (by) the = optimizer.

No. Whatever the spec, it will = never say that a function will not work as a function, or something that works = outside one will not work once moved inside.

 

> <snip>

> If by 'kept from one execution to another' means that (the = concept of) a

> plan is implemented static, this can be a low level design = issue, which

> in general will still be regarded as implementation, thus a = bug.

 

The execution plan is not quite static - it is computed the = first time you run the function (within a session) and is discarded when your = session ends (or when the compiled function/execution plan becomes obsolete because = of a change to a dependency).

 

That is by design.

Then the design is = poor.

 

=

If you want a dynamic plan that is re-computed each time you = execute the query, you can get that behavior by using dynamic SQL, as Kevin = suggested.

 

=

This seems far fetched = and irrelevant. Whatever is truly static should be implemented static; whatever is dynamic should = be implemented dynamic; whatever is partially static, the static part = should be static and the dynamic part should be dynamic. It is natural and correct treatment.

 

Purely dynamic situation = in which the final query can not be determined in any fashion, will have to be constructed either = outside of the function or within, so EXECUTE is the only way to handle. What do = you think of requiring the caller to construct such a static statement as = =93select count(*) from sometable=94 and use EXECUTE?

 

By the same token, = =93select thiscolumn from thistable where upper(thiscolumn) = like $1=94 has to be treated statically for the static part. The only unknown is = the parameter, which can be, by the right design and implementation, delayed = till execution (runtime). The code to deal with this is what I pointed out = (via a conditional). If the plan is a piece of code, then the conditional will = be in it. If the plan is a piece of text to be further interpreted for actual execution (why would one want to do it that way?), the conditional could contain a text reference to two pieces of code (w/o the use of the = index). If it is not properly designed/implemented and = such situation results in the loss of the said ability, it is a defect to be addressed. Whether one wants to address it is one issue. A defect is a = defect.

 

You seem to suggest that = the plan was only built at (the first) execution. That is poor = design/implementation.

 

Lastly, what is a = function? One of the fundamental features of a function is encapsulation. One is guaranteed = some well-defined output based on well-defined input. No implementation = detail is necessary or is obliged to be available. The user does not have to know what table = or anything for that matter is involved. When you push for what is = suggested as a work-around, it defeats one of the basic purposes for a = function.

 

More can be said, but = why one wants to defend a defect is quite beyond me.

 

        =           -- Korry


= --Apple-Mail-110-863037691--