Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m3RP8-0002Ne-4i for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Jul 2021 23:01:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m3RP7-0005CD-3p for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Jul 2021 23:01:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m3RP6-0005C5-SF for pgsql-hackers@lists.postgresql.org; Tue, 13 Jul 2021 23:01:28 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m3RP4-0003Dx-UB for pgsql-hackers@lists.postgresql.org; Tue, 13 Jul 2021 23:01:28 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 16DN1O8H3441863; Tue, 13 Jul 2021 19:01:24 -0400 From: Tom Lane To: Julien Rouhaud cc: pgsql-hackers@lists.postgresql.org Subject: Re: [HACKERS] Preserving param location In-reply-to: <20210627033539.53gfcixzd3u5yl4j@nol> References: <20170311220932.GJ15188@nol.local> <20210627033153.4orlkah2grcnslgi@nol> <20210627033539.53gfcixzd3u5yl4j@nol> Comments: In-reply-to Julien Rouhaud message dated "Sun, 27 Jun 2021 11:35:39 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3441861.1626217284.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 13 Jul 2021 19:01:24 -0400 Message-ID: <3441862.1626217284@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Julien Rouhaud writes: > On Sun, Jun 27, 2021 at 11:31:53AM +0800, Julien Rouhaud wrote: >> On Sat, Mar 11, 2017 at 11:09:32PM +0100, Julien Rouhaud wrote: >>> When a query contains parameters, the original param node contains the= token >>> location. However, this information is lost when the Const node is ge= nerated, >>> this one will only contain position -1 (unknown). >>> = >>> FWIW, we do have a use case for this (custom extension that tracks qua= ls >>> statistics, which among other thing is used to regenerate query string= from a >>> pgss normalized query, thus needing the original param location). I looked at this for a bit. It's certainly a simple and harmless-looking patch, but I'm having a hard time buying that it's actually useful in isolation. In particular, even if we keep the Param's location in the immediately-resulting Const, what happens when that node is further mashed by planner transformations? As an example, given regression=3D# prepare foo(int) as select $1 + 1; PREPARE regression=3D# explain verbose execute foo(42); QUERY PLAN = ------------------------------------------ Result (cost=3D0.00..0.01 rows=3D1 width=3D4) Output: 43 (2 rows) this patch isn't gonna do anything to help you figure out where the 42 went. I thought for a bit about also having evaluate_expr() inject the exprLocation(expr) into its result Const, but in this example that would likely result in having the 43 labeled with the offset of the $1 (given exprLocation's penchant for preferring the leftmost token location). That could be more confusing not less, especially when you consider that the variant case "1 + $1" would not be traceable to the Param at all. I also fear that this is still just the tip of the iceberg, in terms of when rewriting and planning preserve useful info about the source location of modified Nodes. There's probably other places to touch if we want to have consistent results. So I'm not really convinced that there's a fully-baked use case here, and would like more detail about how you hope to use the location value. regards, tom lane