Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id E7B6BB5DEC6 for ; Mon, 30 May 2011 20:12:49 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 79123-04 for ; Mon, 30 May 2011 23:12:41 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from exprod7og111.obsmtp.com (exprod7og111.obsmtp.com [64.18.2.175]) by mail.postgresql.org (Postfix) with ESMTP id DA22BB5DDDF for ; Mon, 30 May 2011 20:12:40 -0300 (ADT) Received: from mail-fx0-f45.google.com ([209.85.161.45]) (using TLSv1) by exprod7ob111.postini.com ([64.18.6.12]) with SMTP ID DSNKTeQkaMOCR/1NAImGFEkiWqQFaydUHD67@postini.com; Mon, 30 May 2011 16:12:41 PDT Received: by fxm2 with SMTP id 2so3562233fxm.18 for ; Mon, 30 May 2011 16:12:39 -0700 (PDT) Received: by 10.223.59.92 with SMTP id k28mr1872853fah.27.1306758352305; Mon, 30 May 2011 05:25:52 -0700 (PDT) Received: from [192.168.1.183] (dsl-hkibrasgw2-ff7ac300-240.dhcp.inet.fi [88.195.122.240]) by mx.google.com with ESMTPS id n15sm722558fag.42.2011.05.30.05.25.50 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 30 May 2011 05:25:51 -0700 (PDT) Message-ID: <4DE38CCD.80702@enterprisedb.com> Date: Mon, 30 May 2011 15:25:49 +0300 From: Heikki Linnakangas User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; fi-FI; rv:1.9.2.17) Gecko/20110510 Icedove/3.1.10 MIME-Version: 1.0 To: Tom Lane CC: PostgreSQL-development Subject: Re: Nested CASE-WHEN scoping References: <4DDCEEB8.50602@enterprisedb.com> <5487.1306334876@sss.pgh.pa.us> <4DDD35B6.2050203@enterprisedb.com> <27772.1306359110@sss.pgh.pa.us> In-Reply-To: <27772.1306359110@sss.pgh.pa.us> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201105/1536 X-Sequence-Number: 188570 On 26.05.2011 00:31, Tom Lane wrote: > The main annoyance here is that standalone expressions may now need > Param slots to execute, which will complicate places that need to > evaluate them, but there's probably no way around that Yeah, I can see two complications: 1. Domain constraints Domain constraint check expressions are fetched and constant-folded separately from the enclosing expression, in ExecInitExpr(). In order to allocate distinct paramids for any CASE values within the domain check expression, we'd need to know how many paramids are in use in the parent expression. We could dig into the parent PlanState and its EState to get that, but we don't have that for stand-alone expressions. 2. Index expressions Index expressions are stored in relcache after constant evaluation, so any CaseTestExprs will already be replaced with Params. When the recheck expression is evaluated, the paramids used in the recheck expression will clash with real PARAM_EXEC params used to pass values up and down subqueries, as well as any params used for CASEs. I think we can work around both of those by just saving and restoring the value of each Param that we set while evaluating an expression, as the values should not be used simultaneously, but it makes me a bit uncomfortable. If we ever try to inline those expressions into other expressions, we'll be right back to the issue we have with nested CASE now. I'm not sure if we might already do that for index recheck expressions. Alternatively, we could adjust the paramids when an expression is inlined into another, similar to what OffsetVarNodes does for Vars. For debugging purposes, it seems like a good idea to invent a new kind of Param for these, and keep them separate from PARAM_EXEC params. The scope would be different, PARAM_EXECs are used to pass values between planner nodes, while these new kind of Params would be used to pass values within a single expression. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com