#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "optimizer/cost.h"
+#include "optimizer/pathnode.h"
#include "utils/rel.h"
#include "utils/syscache.h"
/*
* FDW callback routines
*/
-static FdwPlan *filePlanForeignScan(Oid foreigntableid,
+static void filePlanForeignScan(Oid foreigntableid,
PlannerInfo *root,
RelOptInfo *baserel);
static void fileExplainForeignScan(ForeignScanState *node, ExplainState *es);
/*
* filePlanForeignScan
- * Create a FdwPlan for a scan on the foreign table
+ * Create possible access paths for a scan on the foreign table
+ *
+ * Currently we don't support any push-down feature, so there is only one
+ * possible access path, which simply returns all records in the order in
+ * the data file.
*/
-static FdwPlan *
+static void
filePlanForeignScan(Oid foreigntableid,
PlannerInfo *root,
RelOptInfo *baserel)
{
- FdwPlan *fdwplan;
char *filename;
List *options;
+ Cost startup_cost;
+ Cost total_cost;
/* Fetch options --- we only need filename at this point */
fileGetOptions(foreigntableid, &filename, &options);
- /* Construct FdwPlan with cost estimates */
- fdwplan = makeNode(FdwPlan);
+ /* Estimate costs and update baserel->rows */
estimate_costs(root, baserel, filename,
- &fdwplan->startup_cost, &fdwplan->total_cost);
- fdwplan->fdw_private = NIL; /* not used */
+ &startup_cost, &total_cost);
+
+ /* Create a ForeignPath node and add it as only possible path */
+ add_path(baserel, (Path *)
+ create_foreignscan_path(root, baserel,
+ baserel->rows,
+ startup_cost,
+ total_cost,
+ NIL, /* no pathkeys */
+ NULL, /* no outer rel either */
+ NIL,
+ NIL)); /* no fdw_private data */
- return fdwplan;
+ /*
+ * If data file was sorted, and we knew it somehow, we could insert
+ * appropriate pathkeys into the ForeignPath node to tell the planner that.
+ */
}
/*
/*
* Estimate costs of scanning a foreign table.
+ *
+ * In addition to setting *startup_cost and *total_cost, this should
+ * update baserel->rows.
*/
static void
estimate_costs(PlannerInfo *root, RelOptInfo *baserel,
<para>
<programlisting>
-FdwPlan *
+void
PlanForeignScan (Oid foreigntableid,
PlannerInfo *root,
RelOptInfo *baserel);
</programlisting>
- Plan a scan on a foreign table. This is called when a query is planned.
+ Create possible access paths for a scan on a foreign table. This is
+ called when a query is planned.
<literal>foreigntableid</> is the <structname>pg_class</> OID of the
foreign table. <literal>root</> is the planner's global information
about the query, and <literal>baserel</> is the planner's information
about this table.
- The function must return a palloc'd struct that contains cost estimates
- plus any FDW-private information that is needed to execute the foreign
- scan at a later time. (Note that the private information must be
- represented in a form that <function>copyObject</> knows how to copy.)
+ </para>
+
+ <para>
+ The function must generate at least one access path (ForeignPath node)
+ for a scan on the foreign table and must call <function>add_path</> to
+ add the path to <literal>baserel->pathlist</>. It's recommended to
+ use <function>create_foreignscan_path</> to build the ForeignPath node.
+ The function may generate multiple access paths, e.g., a path which has
+ valid <literal>pathkeys</> to represent a pre-sorted result. Each access
+ path must contain cost estimates, and can contain any FDW-private
+ information that is needed to execute the foreign scan at a later time.
+ (Note that the private information must be represented in a form that
+ <function>copyObject</> knows how to copy.)
</para>
<para>
its <structfield>fdw_state</> field is still NULL. Information about
the table to scan is accessible through the
<structname>ForeignScanState</> node (in particular, from the underlying
- <structname>ForeignScan</> plan node, which contains a pointer to the
- <structname>FdwPlan</> structure returned by
- <function>PlanForeignScan</>).
+ <structname>ForeignScan</> plan node, which contains any FDW-private
+ information provided by <function>PlanForeignScan</>).
</para>
<para>
</para>
<para>
- The <structname>FdwRoutine</> and <structname>FdwPlan</> struct types
- are declared in <filename>src/include/foreign/fdwapi.h</>, which see
- for additional details.
+ The <structname>FdwRoutine</> struct type is declared in
+ <filename>src/include/foreign/fdwapi.h</>, which see for additional
+ details.
</para>
</sect1>
#include "postgres.h"
#include "miscadmin.h"
-#include "foreign/fdwapi.h"
+#include "nodes/plannodes.h"
+#include "nodes/relation.h"
#include "utils/datum.h"
* copy remainder of node
*/
COPY_SCALAR_FIELD(fsSystemCol);
- COPY_NODE_FIELD(fdwplan);
-
- return newnode;
-}
-
-/*
- * _copyFdwPlan
- */
-static FdwPlan *
-_copyFdwPlan(const FdwPlan *from)
-{
- FdwPlan *newnode = makeNode(FdwPlan);
-
- COPY_SCALAR_FIELD(startup_cost);
- COPY_SCALAR_FIELD(total_cost);
COPY_NODE_FIELD(fdw_private);
return newnode;
case T_ForeignScan:
retval = _copyForeignScan(from);
break;
- case T_FdwPlan:
- retval = _copyFdwPlan(from);
- break;
case T_Join:
retval = _copyJoin(from);
break;
#include <ctype.h>
-#include "foreign/fdwapi.h"
+#include "lib/stringinfo.h"
+#include "nodes/plannodes.h"
+#include "nodes/relation.h"
#include "utils/datum.h"
_outScanInfo(str, (const Scan *) node);
WRITE_BOOL_FIELD(fsSystemCol);
- WRITE_NODE_FIELD(fdwplan);
-}
-
-static void
-_outFdwPlan(StringInfo str, const FdwPlan *node)
-{
- WRITE_NODE_TYPE("FDWPLAN");
-
- WRITE_FLOAT_FIELD(startup_cost, "%.2f");
- WRITE_FLOAT_FIELD(total_cost, "%.2f");
WRITE_NODE_FIELD(fdw_private);
}
_outPathInfo(str, (const Path *) node);
- WRITE_NODE_FIELD(fdwplan);
+ WRITE_NODE_FIELD(fdw_private);
}
static void
case T_ForeignScan:
_outForeignScan(str, obj);
break;
- case T_FdwPlan:
- _outFdwPlan(str, obj);
- break;
case T_Join:
_outJoin(str, obj);
break;
#include <math.h>
#include "catalog/pg_class.h"
+#include "foreign/fdwapi.h"
#include "nodes/nodeFuncs.h"
#ifdef OPTIMIZER_DEBUG
#include "nodes/print.h"
/*
* set_foreign_pathlist
- * Build the (single) access path for a foreign table RTE
+ * Build access paths for a foreign table RTE
*/
static void
set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
{
- /* Generate appropriate path */
- add_path(rel, (Path *) create_foreignscan_path(root, rel));
+ FdwRoutine *fdwroutine;
- /* Select cheapest path (pretty easy in this case...) */
+ /* Call the FDW's PlanForeignScan function to generate path(s) */
+ fdwroutine = GetFdwRoutineByRelId(rte->relid);
+ fdwroutine->PlanForeignScan(rte->relid, root, rel);
+
+ /* Select cheapest path */
set_cheapest(rel);
}
#include <math.h>
#include "access/skey.h"
-#include "foreign/fdwapi.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
static WorkTableScan *make_worktablescan(List *qptlist, List *qpqual,
Index scanrelid, int wtParam);
static ForeignScan *make_foreignscan(List *qptlist, List *qpqual,
- Index scanrelid, bool fsSystemCol, FdwPlan *fdwplan);
+ Index scanrelid, bool fsSystemCol, List *fdw_private);
static BitmapAnd *make_bitmap_and(List *bitmapplans);
static BitmapOr *make_bitmap_or(List *bitmapplans);
static NestLoop *make_nestloop(List *tlist,
scan_clauses,
scan_relid,
fsSystemCol,
- best_path->fdwplan);
+ best_path->fdw_private);
copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
List *qpqual,
Index scanrelid,
bool fsSystemCol,
- FdwPlan *fdwplan)
+ List *fdw_private)
{
ForeignScan *node = makeNode(ForeignScan);
Plan *plan = &node->scan.plan;
plan->righttree = NULL;
node->scan.scanrelid = scanrelid;
node->fsSystemCol = fsSystemCol;
- node->fdwplan = fdwplan;
+ node->fdw_private = fdw_private;
return node;
}
#include <math.h>
-#include "foreign/fdwapi.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
* create_foreignscan_path
* Creates a path corresponding to a scan of a foreign table,
* returning the pathnode.
+ *
+ * This function is never called from core Postgres; rather, it's expected
+ * to be called by the PlanForeignScan function of a foreign data wrapper.
+ * We make the FDW supply all fields of the path, since we do not have any
+ * way to calculate them in core.
*/
ForeignPath *
-create_foreignscan_path(PlannerInfo *root, RelOptInfo *rel)
+create_foreignscan_path(PlannerInfo *root, RelOptInfo *rel,
+ double rows, Cost startup_cost, Cost total_cost,
+ List *pathkeys,
+ Relids required_outer, List *param_clauses,
+ List *fdw_private)
{
ForeignPath *pathnode = makeNode(ForeignPath);
- RangeTblEntry *rte;
- FdwRoutine *fdwroutine;
- FdwPlan *fdwplan;
pathnode->path.pathtype = T_ForeignScan;
pathnode->path.parent = rel;
- pathnode->path.pathkeys = NIL; /* result is always unordered */
- pathnode->path.required_outer = NULL;
- pathnode->path.param_clauses = NIL;
+ pathnode->path.rows = rows;
+ pathnode->path.startup_cost = startup_cost;
+ pathnode->path.total_cost = total_cost;
+ pathnode->path.pathkeys = pathkeys;
+ pathnode->path.required_outer = required_outer;
+ pathnode->path.param_clauses = param_clauses;
- /* Get FDW's callback info */
- rte = planner_rt_fetch(rel->relid, root);
- fdwroutine = GetFdwRoutineByRelId(rte->relid);
-
- /* Let the FDW do its planning */
- fdwplan = fdwroutine->PlanForeignScan(rte->relid, root, rel);
- if (fdwplan == NULL || !IsA(fdwplan, FdwPlan))
- elog(ERROR, "foreign-data wrapper PlanForeignScan function for relation %u did not return an FdwPlan struct",
- rte->relid);
- pathnode->fdwplan = fdwplan;
-
- /* use costs estimated by FDW */
- pathnode->path.rows = rel->rows;
- pathnode->path.startup_cost = fdwplan->startup_cost;
- pathnode->path.total_cost = fdwplan->total_cost;
+ pathnode->fdw_private = fdw_private;
return pathnode;
}
struct ExplainState;
-/*
- * FdwPlan is the information returned to the planner by PlanForeignScan.
- */
-typedef struct FdwPlan
-{
- NodeTag type;
-
- /*
- * Cost estimation info. The startup_cost is time before retrieving the
- * first row, so it should include costs of connecting to the remote host,
- * sending over the query, etc. Note that PlanForeignScan also ought to
- * set baserel->rows and baserel->width if it can produce any usable
- * estimates of those values.
- */
- Cost startup_cost; /* cost expended before fetching any tuples */
- Cost total_cost; /* total cost (assuming all tuples fetched) */
-
- /*
- * FDW private data, which will be available at execution time.
- *
- * Note that everything in this list must be copiable by copyObject(). One
- * way to store an arbitrary blob of bytes is to represent it as a bytea
- * Const. Usually, though, you'll be better off choosing a representation
- * that can be dumped usefully by nodeToString().
- */
- List *fdw_private;
-} FdwPlan;
-
-
/*
* Callback function signatures --- see fdwhandler.sgml for more info.
*/
-typedef FdwPlan *(*PlanForeignScan_function) (Oid foreigntableid,
- PlannerInfo *root,
- RelOptInfo *baserel);
+typedef void (*PlanForeignScan_function) (Oid foreigntableid,
+ PlannerInfo *root,
+ RelOptInfo *baserel);
typedef void (*ExplainForeignScan_function) (ForeignScanState *node,
struct ExplainState *es);
T_CteScan,
T_WorkTableScan,
T_ForeignScan,
- T_FdwPlan,
T_Join,
T_NestLoop,
T_MergeJoin,
{
Scan scan;
bool fsSystemCol; /* true if any "system column" is needed */
- /* use struct pointer to avoid including fdwapi.h here */
- struct FdwPlan *fdwplan;
+ List *fdw_private; /* private data for FDW */
} ForeignScan;
/*
* ForeignPath represents a scan of a foreign table
+ *
+ * fdw_private contains FDW private data about the scan, which will be copied
+ * to the final ForeignScan plan node so that it is available at execution
+ * time. Note that everything in this list must be copiable by copyObject().
+ * One way to store an arbitrary blob of bytes is to represent it as a bytea
+ * Const. Usually, though, you'll be better off choosing a representation
+ * that can be dumped usefully by nodeToString().
*/
typedef struct ForeignPath
{
Path path;
- /* use struct pointer to avoid including fdwapi.h here */
- struct FdwPlan *fdwplan;
+ List *fdw_private;
} ForeignPath;
/*
extern Path *create_valuesscan_path(PlannerInfo *root, RelOptInfo *rel);
extern Path *create_ctescan_path(PlannerInfo *root, RelOptInfo *rel);
extern Path *create_worktablescan_path(PlannerInfo *root, RelOptInfo *rel);
-extern ForeignPath *create_foreignscan_path(PlannerInfo *root, RelOptInfo *rel);
+extern ForeignPath *create_foreignscan_path(PlannerInfo *root, RelOptInfo *rel,
+ double rows, Cost startup_cost, Cost total_cost,
+ List *pathkeys,
+ Relids required_outer, List *param_clauses,
+ List *fdw_private);
extern Relids calc_nestloop_required_outer(Path *outer_path, Path *inner_path);
extern Relids calc_non_nestloop_required_outer(Path *outer_path, Path *inner_path);