Thread: How to get value of 'Param' of the WHERE clause in the FDW?

How to get value of 'Param' of the WHERE clause in the FDW?

From
Dmitry Chichkov
Date:
Please help.... I'm doing a following query to a foreign wrapper:
   FUNCTION fwcall(text) ....   SELECT * FROM fwtable WHERE col=$1....  ;
   ...
   SELECT * from fdwcall('abc123');


I'm looking for a way to get that parameter 'abc123' value in the FDW wrapper code...


It seems like during fdwPlan(..., RelOptInfo *baserel) stage I'm getting  baserel->baserestrictinfo, in which I see a Node *x  of  IsA(x, Param).  But it looks like the value 'abc123' is not yet available in the planning stage, right?   And I don't see how can I get baserestrictinfo in the execution stage or if the 'abc123' value would be there...

Can somebody kick me to the right direction?  Please?

Thanks,
Dmitry

Re: How to get value of 'Param' of the WHERE clause in the FDW?

From
Tom Lane
Date:
Dmitry Chichkov <dchichkov@gmail.com> writes:
> It seems like during fdwPlan(..., RelOptInfo *baserel) stage I'm getting
> baserel->baserestrictinfo, in which I see a Node *x  of  IsA(x, Param).
> But it looks like the value 'abc123' is not yet available in the planning
> stage, right?   And I don't see how can I get baserestrictinfo in the
> execution stage or if the 'abc123' value would be there...

If you are trying to get an estimated value for some subexpression at plan
time, estimate_expression_value() is what to use; see for example the uses
of that function in selfuncs.c.  Keep in mind that it *is* an estimate and
cannot be guaranteed to still be correct at execution time, since the plan
might be re-used with another parameter value.
        regards, tom lane



Re: How to get value of 'Param' of the WHERE clause in the FDW?

From
Dmitry Chichkov
Date:
Thank you for the reply!  I'm trying to get the correct value and I need it at the execution stage.  I just don't see how to get baserestrictinfo in the execution stage or if the 'abc123' value would be there at all...

Kind regards,
Dmitry

On Fri, Sep 25, 2015 at 11:44 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Dmitry Chichkov <dchichkov@gmail.com> writes:
> It seems like during fdwPlan(..., RelOptInfo *baserel) stage I'm getting
> baserel->baserestrictinfo, in which I see a Node *x  of  IsA(x, Param).
> But it looks like the value 'abc123' is not yet available in the planning
> stage, right?   And I don't see how can I get baserestrictinfo in the
> execution stage or if the 'abc123' value would be there...

If you are trying to get an estimated value for some subexpression at plan
time, estimate_expression_value() is what to use; see for example the uses
of that function in selfuncs.c.  Keep in mind that it *is* an estimate and
cannot be guaranteed to still be correct at execution time, since the plan
might be re-used with another parameter value.

                        regards, tom lane

Re: How to get value of 'Param' of the WHERE clause in the FDW?

From
Tom Lane
Date:
Dmitry Chichkov <dchichkov@gmail.com> writes:
> Thank you for the reply!  I'm trying to get the correct value and I need it
> at the execution stage.  I just don't see how to get baserestrictinfo in
> the execution stage or if the 'abc123' value would be there at all...

Hm?  At execution, you'd just evaluate whatever the expression is.
The planner constructs don't have much to do with that, and certainly
a Param should not be a special case in any way.
        regards, tom lane



Re: How to get value of 'Param' of the WHERE clause in the FDW?

From
Dmitry Chichkov
Date:
<div dir="ltr"><div class="gmail_extra">Evaluate via ExecEvalExpr, right?     And sorry for a beginner question,  what
doI need to do to get that Expr from ForeignScanState?    Is it accessible at all in old 9.1 API?<br /><br /></div><div
class="gmail_extra">Isee code that is getting exec_exprs  from ForeignScan *node:<br />  ForeignScan *fsplan =
(ForeignScan*)node->ss.ps.plan;    <br />  List *exec_exprs = (List *)ExecInitExpr((Expr *)fsplan->fdw_exprs,
(PlanState*)node);<br /> <br /></div><div class="gmail_extra">  then it goes through the list, initializes
paramDesc/ExprStateand executes it via ExecEvalExpr.   Is that what I should do to get that 'abc123' value?    <br
/><br/>And I'm getting  "‘ForeignScan’ has no member named ‘fdw_exprs’" in the 9.1 API.  Is it possible to do in
9.1?<br/><div class="gmail_extra"><br /></div></div><div class="gmail_extra"><br />....<br /><br />Is there some
alternativeway to flatten these subexpressions into consts, before they are passed to FDW?<br /><br />Kind Regards,<br
/></div><divclass="gmail_extra">Dmitry<br /></div><div class="gmail_extra"><br /><br /><br /></div><div
class="gmail_extra"><br/><br /><div class="gmail_quote">On Fri, Sep 25, 2015 at 11:54 AM, Tom Lane <span
dir="ltr"><<ahref="mailto:tgl@sss.pgh.pa.us" target="_blank">tgl@sss.pgh.pa.us</a>></span> wrote:<br
/><blockquoteclass="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid
rgb(204,204,204);padding-left:1ex"><divclass="" id=":vf" style="overflow:hidden">ith that, and certainly<br /> a Param
shouldnot be a special case in any w</div></blockquote></div><br /><br /></div></div> 

Re: How to get value of 'Param' of the WHERE clause in the FDW?

From
Tom Lane
Date:
Dmitry Chichkov <dchichkov@gmail.com> writes:
> Evaluate via ExecEvalExpr, right?

Yeah.

> And sorry for a beginner question,
> what do I need to do to get that Expr from ForeignScanState?    Is it
> accessible at all in old 9.1 API?

I think you're out of luck before 9.2.  There's no provision for
expressions to be executed by the FDW itself in 9.1.  And you can't
really work around that behind the planner's back, because if you put
an expression into your private fdw state, it won't get adjusted properly
during setrefs.c cleanup.
        regards, tom lane