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

Started by Dmitry Chichkovover 10 years ago6 messageshackers
Jump to latest
#1Dmitry Chichkov
dchichkov@gmail.com

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

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Dmitry Chichkov (#1)
Re: How to get value of 'Param' of the WHERE clause in the FDW?

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

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#3Dmitry Chichkov
dchichkov@gmail.com
In reply to: Tom Lane (#2)
Re: How to get value of 'Param' of the WHERE clause in the FDW?

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:

Show quoted text

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

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Dmitry Chichkov (#3)
Re: How to get value of 'Param' of the WHERE clause in the FDW?

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

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#5Dmitry Chichkov
dchichkov@gmail.com
In reply to: Tom Lane (#4)
Re: How to get value of 'Param' of the WHERE clause in the FDW?

Evaluate via ExecEvalExpr, right? 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 see code that is getting exec_exprs from ForeignScan *node:
ForeignScan *fsplan = (ForeignScan *)node->ss.ps.plan;
List *exec_exprs = (List *)ExecInitExpr((Expr *)fsplan->fdw_exprs,
(PlanState *)node);

then it goes through the list, initializes paramDesc/ExprState and
executes it via ExecEvalExpr. Is that what I should do to get that
'abc123' value?

And I'm getting "‘ForeignScan’ has no member named ‘fdw_exprs’" in the 9.1
API. Is it possible to do in 9.1?

....

Is there some alternative way to flatten these subexpressions into consts,
before they are passed to FDW?

Kind Regards,
Dmitry

On Fri, Sep 25, 2015 at 11:54 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Show quoted text

ith that, and certainly
a Param should not be a special case in any w

#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: Dmitry Chichkov (#5)
Re: How to get value of 'Param' of the WHERE clause in the FDW?

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

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers