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 
> > generated,
> > this one will only contain position -1 (unknown).
> >
> > FWIW, we do have a use case for this (custom extension that tracks quals
> > statistics, which among other thing is used to regenerate query string from 
> > a
> > pgss normalized query, thus needing the original param location).
> 
> rebased v2.

This time with the patch.
>From 5c4831437e5b832d73dbab827d7f5e737bd1dfae Mon Sep 17 00:00:00 2001
From: Julien Rouhaud <julien.rouh...@free.fr>
Date: Sat, 11 Mar 2017 22:48:00 +0100
Subject: [PATCH v2] Preserve param location when generating a const node.

Discussion: https://postgr.es/m/20170311220932.GJ15188@nol.local
---
 src/backend/optimizer/util/clauses.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index 059fa70254..084560acd8 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -2285,6 +2285,7 @@ eval_const_expressions_mutator(Node *node,
 							int16		typLen;
 							bool		typByVal;
 							Datum		pval;
+							Const	   *con;
 
 							get_typlenbyval(param->paramtype,
 											&typLen, &typByVal);
@@ -2292,13 +2293,17 @@ eval_const_expressions_mutator(Node *node,
 								pval = prm->value;
 							else
 								pval = datumCopy(prm->value, typByVal, typLen);
-							return (Node *) makeConst(param->paramtype,
+
+							con = makeConst(param->paramtype,
 													  param->paramtypmod,
 													  param->paramcollid,
 													  (int) typLen,
 													  pval,
 													  prm->isnull,
 													  typByVal);
+							con->location = param->location;
+
+							return (Node *) con;
 						}
 					}
 				}
-- 
2.31.1

Reply via email to