2011-02-21 03:06:59 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* file_fdw.c
|
|
|
|
* foreign-data wrapper for server-side flat files.
|
|
|
|
*
|
2014-01-08 05:05:30 +08:00
|
|
|
* Copyright (c) 2010-2014, PostgreSQL Global Development Group
|
2011-02-21 03:06:59 +08:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* contrib/file_fdw/file_fdw.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-08-31 04:15:44 +08:00
|
|
|
#include "access/htup_details.h"
|
2011-02-21 03:06:59 +08:00
|
|
|
#include "access/reloptions.h"
|
2012-07-13 04:26:59 +08:00
|
|
|
#include "access/sysattr.h"
|
2011-02-21 03:06:59 +08:00
|
|
|
#include "catalog/pg_foreign_table.h"
|
|
|
|
#include "commands/copy.h"
|
|
|
|
#include "commands/defrem.h"
|
|
|
|
#include "commands/explain.h"
|
2012-04-07 03:02:35 +08:00
|
|
|
#include "commands/vacuum.h"
|
2011-02-21 03:06:59 +08:00
|
|
|
#include "foreign/fdwapi.h"
|
|
|
|
#include "foreign/foreign.h"
|
|
|
|
#include "miscadmin.h"
|
2011-09-17 04:35:51 +08:00
|
|
|
#include "nodes/makefuncs.h"
|
2011-02-21 03:06:59 +08:00
|
|
|
#include "optimizer/cost.h"
|
2012-03-06 05:15:59 +08:00
|
|
|
#include "optimizer/pathnode.h"
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
#include "optimizer/planmain.h"
|
|
|
|
#include "optimizer/restrictinfo.h"
|
2012-07-13 04:26:59 +08:00
|
|
|
#include "optimizer/var.h"
|
2012-04-07 03:02:35 +08:00
|
|
|
#include "utils/memutils.h"
|
2011-02-24 01:18:09 +08:00
|
|
|
#include "utils/rel.h"
|
2011-02-21 03:06:59 +08:00
|
|
|
|
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Describes the valid options for objects that use this wrapper.
|
|
|
|
*/
|
|
|
|
struct FileFdwOption
|
|
|
|
{
|
|
|
|
const char *optname;
|
|
|
|
Oid optcontext; /* Oid of catalog in which option may appear */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Valid options for file_fdw.
|
2014-03-05 06:31:59 +08:00
|
|
|
* These options are based on the options for the COPY FROM command.
|
|
|
|
* But note that force_not_null and force_null are handled as boolean options
|
|
|
|
* attached to a column, not as table options.
|
2011-02-21 03:06:59 +08:00
|
|
|
*
|
|
|
|
* Note: If you are adding new option for user mapping, you need to modify
|
|
|
|
* fileGetOptions(), which currently doesn't bother to look at user mappings.
|
|
|
|
*/
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
static const struct FileFdwOption valid_options[] = {
|
2011-02-21 03:06:59 +08:00
|
|
|
/* File options */
|
2011-04-10 23:42:00 +08:00
|
|
|
{"filename", ForeignTableRelationId},
|
2011-02-21 03:06:59 +08:00
|
|
|
|
|
|
|
/* Format options */
|
|
|
|
/* oids option is not supported */
|
2011-04-10 23:42:00 +08:00
|
|
|
{"format", ForeignTableRelationId},
|
|
|
|
{"header", ForeignTableRelationId},
|
|
|
|
{"delimiter", ForeignTableRelationId},
|
|
|
|
{"quote", ForeignTableRelationId},
|
|
|
|
{"escape", ForeignTableRelationId},
|
|
|
|
{"null", ForeignTableRelationId},
|
|
|
|
{"encoding", ForeignTableRelationId},
|
2011-09-17 04:35:51 +08:00
|
|
|
{"force_not_null", AttributeRelationId},
|
2014-03-05 06:31:59 +08:00
|
|
|
{"force_null", AttributeRelationId},
|
2011-02-21 03:06:59 +08:00
|
|
|
/*
|
|
|
|
* force_quote is not supported by file_fdw because it's for COPY TO.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Sentinel */
|
2011-04-10 23:42:00 +08:00
|
|
|
{NULL, InvalidOid}
|
2011-02-21 03:06:59 +08:00
|
|
|
};
|
|
|
|
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
/*
|
|
|
|
* FDW-specific information for RelOptInfo.fdw_private.
|
|
|
|
*/
|
|
|
|
typedef struct FileFdwPlanState
|
|
|
|
{
|
|
|
|
char *filename; /* file to read */
|
|
|
|
List *options; /* merged COPY options, excluding filename */
|
|
|
|
BlockNumber pages; /* estimate of file's physical size */
|
|
|
|
double ntuples; /* estimate of number of rows in file */
|
|
|
|
} FileFdwPlanState;
|
|
|
|
|
2011-02-21 03:06:59 +08:00
|
|
|
/*
|
|
|
|
* FDW-specific information for ForeignScanState.fdw_state.
|
|
|
|
*/
|
|
|
|
typedef struct FileFdwExecutionState
|
|
|
|
{
|
2011-04-10 23:42:00 +08:00
|
|
|
char *filename; /* file to read */
|
|
|
|
List *options; /* merged COPY options, excluding filename */
|
|
|
|
CopyState cstate; /* state of reading file */
|
2011-02-21 03:06:59 +08:00
|
|
|
} FileFdwExecutionState;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SQL functions
|
|
|
|
*/
|
|
|
|
extern Datum file_fdw_handler(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum file_fdw_validator(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(file_fdw_handler);
|
|
|
|
PG_FUNCTION_INFO_V1(file_fdw_validator);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FDW callback routines
|
|
|
|
*/
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
static void fileGetForeignRelSize(PlannerInfo *root,
|
2012-06-11 03:20:04 +08:00
|
|
|
RelOptInfo *baserel,
|
|
|
|
Oid foreigntableid);
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
static void fileGetForeignPaths(PlannerInfo *root,
|
2012-06-11 03:20:04 +08:00
|
|
|
RelOptInfo *baserel,
|
|
|
|
Oid foreigntableid);
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
static ForeignScan *fileGetForeignPlan(PlannerInfo *root,
|
2012-06-11 03:20:04 +08:00
|
|
|
RelOptInfo *baserel,
|
|
|
|
Oid foreigntableid,
|
|
|
|
ForeignPath *best_path,
|
|
|
|
List *tlist,
|
|
|
|
List *scan_clauses);
|
2011-02-21 03:06:59 +08:00
|
|
|
static void fileExplainForeignScan(ForeignScanState *node, ExplainState *es);
|
|
|
|
static void fileBeginForeignScan(ForeignScanState *node, int eflags);
|
|
|
|
static TupleTableSlot *fileIterateForeignScan(ForeignScanState *node);
|
|
|
|
static void fileReScanForeignScan(ForeignScanState *node);
|
|
|
|
static void fileEndForeignScan(ForeignScanState *node);
|
2012-04-07 04:04:10 +08:00
|
|
|
static bool fileAnalyzeForeignTable(Relation relation,
|
|
|
|
AcquireSampleRowsFunc *func,
|
|
|
|
BlockNumber *totalpages);
|
2011-02-21 03:06:59 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper functions
|
|
|
|
*/
|
|
|
|
static bool is_valid_option(const char *option, Oid context);
|
|
|
|
static void fileGetOptions(Oid foreigntableid,
|
|
|
|
char **filename, List **other_options);
|
2011-09-17 04:35:51 +08:00
|
|
|
static List *get_file_fdw_attribute_options(Oid relid);
|
2012-07-13 04:26:59 +08:00
|
|
|
static bool check_selective_binary_conversion(RelOptInfo *baserel,
|
2013-05-30 04:58:43 +08:00
|
|
|
Oid foreigntableid,
|
|
|
|
List **columns);
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
static void estimate_size(PlannerInfo *root, RelOptInfo *baserel,
|
|
|
|
FileFdwPlanState *fdw_private);
|
2011-02-21 03:06:59 +08:00
|
|
|
static void estimate_costs(PlannerInfo *root, RelOptInfo *baserel,
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
FileFdwPlanState *fdw_private,
|
2011-04-10 23:42:00 +08:00
|
|
|
Cost *startup_cost, Cost *total_cost);
|
2012-06-11 03:20:04 +08:00
|
|
|
static int file_acquire_sample_rows(Relation onerel, int elevel,
|
2012-04-07 03:02:35 +08:00
|
|
|
HeapTuple *rows, int targrows,
|
2012-04-07 04:04:10 +08:00
|
|
|
double *totalrows, double *totaldeadrows);
|
2011-02-21 03:06:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Foreign-data wrapper handler function: return a struct with pointers
|
|
|
|
* to my callback routines.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
file_fdw_handler(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
FdwRoutine *fdwroutine = makeNode(FdwRoutine);
|
|
|
|
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
fdwroutine->GetForeignRelSize = fileGetForeignRelSize;
|
|
|
|
fdwroutine->GetForeignPaths = fileGetForeignPaths;
|
|
|
|
fdwroutine->GetForeignPlan = fileGetForeignPlan;
|
2011-02-21 03:06:59 +08:00
|
|
|
fdwroutine->ExplainForeignScan = fileExplainForeignScan;
|
|
|
|
fdwroutine->BeginForeignScan = fileBeginForeignScan;
|
|
|
|
fdwroutine->IterateForeignScan = fileIterateForeignScan;
|
|
|
|
fdwroutine->ReScanForeignScan = fileReScanForeignScan;
|
|
|
|
fdwroutine->EndForeignScan = fileEndForeignScan;
|
2012-04-07 03:02:35 +08:00
|
|
|
fdwroutine->AnalyzeForeignTable = fileAnalyzeForeignTable;
|
2011-02-21 03:06:59 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(fdwroutine);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate the generic options given to a FOREIGN DATA WRAPPER, SERVER,
|
|
|
|
* USER MAPPING or FOREIGN TABLE that uses file_fdw.
|
|
|
|
*
|
|
|
|
* Raise an ERROR if the option or its value is considered invalid.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
file_fdw_validator(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
|
|
|
|
Oid catalog = PG_GETARG_OID(1);
|
|
|
|
char *filename = NULL;
|
2012-06-11 03:20:04 +08:00
|
|
|
DefElem *force_not_null = NULL;
|
2014-03-05 06:31:59 +08:00
|
|
|
DefElem *force_null = NULL;
|
2011-02-21 03:06:59 +08:00
|
|
|
List *other_options = NIL;
|
|
|
|
ListCell *cell;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Only superusers are allowed to set options of a file_fdw foreign table.
|
2011-04-10 23:42:00 +08:00
|
|
|
* This is because the filename is one of those options, and we don't want
|
|
|
|
* non-superusers to be able to determine which file gets read.
|
2011-02-21 03:06:59 +08:00
|
|
|
*
|
|
|
|
* Putting this sort of permissions check in a validator is a bit of a
|
|
|
|
* crock, but there doesn't seem to be any other place that can enforce
|
|
|
|
* the check more cleanly.
|
|
|
|
*
|
2011-04-10 23:42:00 +08:00
|
|
|
* Note that the valid_options[] array disallows setting filename at any
|
|
|
|
* options level other than foreign table --- otherwise there'd still be a
|
|
|
|
* security hole.
|
2011-02-21 03:06:59 +08:00
|
|
|
*/
|
|
|
|
if (catalog == ForeignTableRelationId && !superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("only superuser can change options of a file_fdw foreign table")));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that only options supported by file_fdw, and allowed for the
|
|
|
|
* current object type, are given.
|
|
|
|
*/
|
|
|
|
foreach(cell, options_list)
|
|
|
|
{
|
2011-04-10 23:42:00 +08:00
|
|
|
DefElem *def = (DefElem *) lfirst(cell);
|
2011-02-21 03:06:59 +08:00
|
|
|
|
|
|
|
if (!is_valid_option(def->defname, catalog))
|
|
|
|
{
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
const struct FileFdwOption *opt;
|
2011-02-21 03:06:59 +08:00
|
|
|
StringInfoData buf;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unknown option specified, complain about it. Provide a hint
|
|
|
|
* with list of valid options for the object.
|
|
|
|
*/
|
|
|
|
initStringInfo(&buf);
|
|
|
|
for (opt = valid_options; opt->optname; opt++)
|
|
|
|
{
|
|
|
|
if (catalog == opt->optcontext)
|
|
|
|
appendStringInfo(&buf, "%s%s", (buf.len > 0) ? ", " : "",
|
|
|
|
opt->optname);
|
|
|
|
}
|
|
|
|
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
|
|
|
|
errmsg("invalid option \"%s\"", def->defname),
|
2012-05-18 01:20:33 +08:00
|
|
|
buf.len > 0
|
|
|
|
? errhint("Valid options in this context are: %s",
|
|
|
|
buf.data)
|
2012-06-11 03:20:04 +08:00
|
|
|
: errhint("There are no valid options in this context.")));
|
2011-02-21 03:06:59 +08:00
|
|
|
}
|
|
|
|
|
2011-09-17 04:35:51 +08:00
|
|
|
/*
|
2014-03-05 06:31:59 +08:00
|
|
|
* Separate out filename and column-specific options, since
|
|
|
|
* ProcessCopyOptions won't accept them.
|
2011-09-17 04:35:51 +08:00
|
|
|
*/
|
2014-03-05 06:31:59 +08:00
|
|
|
|
2011-02-21 03:06:59 +08:00
|
|
|
if (strcmp(def->defname, "filename") == 0)
|
|
|
|
{
|
|
|
|
if (filename)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
|
|
|
filename = defGetString(def);
|
|
|
|
}
|
2014-03-05 06:31:59 +08:00
|
|
|
/*
|
|
|
|
* force_not_null is a boolean option; after validation we can discard
|
|
|
|
* it - it will be retrieved later in get_file_fdw_attribute_options()
|
|
|
|
*/
|
2011-09-17 04:35:51 +08:00
|
|
|
else if (strcmp(def->defname, "force_not_null") == 0)
|
|
|
|
{
|
|
|
|
if (force_not_null)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
2014-03-05 06:31:59 +08:00
|
|
|
errmsg("conflicting or redundant options"),
|
|
|
|
errhint("option \"force_not_null\" supplied more than once for a column")));
|
|
|
|
if(force_null)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options"),
|
|
|
|
errhint("option \"force_not_null\" cannot be used together with \"force_null\"")));
|
2011-09-17 04:35:51 +08:00
|
|
|
force_not_null = def;
|
|
|
|
/* Don't care what the value is, as long as it's a legal boolean */
|
|
|
|
(void) defGetBoolean(def);
|
|
|
|
}
|
2014-03-05 06:31:59 +08:00
|
|
|
/* See comments for force_not_null above */
|
|
|
|
else if (strcmp(def->defname, "force_null") == 0)
|
|
|
|
{
|
|
|
|
if (force_null)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options"),
|
|
|
|
errhint("option \"force_null\" supplied more than once for a column")));
|
|
|
|
if(force_not_null)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options"),
|
|
|
|
errhint("option \"force_null\" cannot be used together with \"force_not_null\"")));
|
|
|
|
force_null = def;
|
|
|
|
(void) defGetBoolean(def);
|
|
|
|
}
|
2011-02-21 03:06:59 +08:00
|
|
|
else
|
|
|
|
other_options = lappend(other_options, def);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now apply the core COPY code's validation logic for more checks.
|
|
|
|
*/
|
|
|
|
ProcessCopyOptions(NULL, true, other_options);
|
|
|
|
|
2011-07-06 06:46:03 +08:00
|
|
|
/*
|
|
|
|
* Filename option is required for file_fdw foreign tables.
|
|
|
|
*/
|
|
|
|
if (catalog == ForeignTableRelationId && filename == NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FDW_DYNAMIC_PARAMETER_VALUE_NEEDED),
|
|
|
|
errmsg("filename is required for file_fdw foreign tables")));
|
|
|
|
|
2011-02-21 03:06:59 +08:00
|
|
|
PG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the provided option is one of the valid options.
|
|
|
|
* context is the Oid of the catalog holding the object the option is for.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
is_valid_option(const char *option, Oid context)
|
|
|
|
{
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
const struct FileFdwOption *opt;
|
2011-02-21 03:06:59 +08:00
|
|
|
|
|
|
|
for (opt = valid_options; opt->optname; opt++)
|
|
|
|
{
|
|
|
|
if (context == opt->optcontext && strcmp(opt->optname, option) == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fetch the options for a file_fdw foreign table.
|
|
|
|
*
|
|
|
|
* We have to separate out "filename" from the other options because
|
|
|
|
* it must not appear in the options list passed to the core COPY code.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fileGetOptions(Oid foreigntableid,
|
|
|
|
char **filename, List **other_options)
|
|
|
|
{
|
|
|
|
ForeignTable *table;
|
|
|
|
ForeignServer *server;
|
|
|
|
ForeignDataWrapper *wrapper;
|
|
|
|
List *options;
|
|
|
|
ListCell *lc,
|
|
|
|
*prev;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract options from FDW objects. We ignore user mappings because
|
|
|
|
* file_fdw doesn't have any options that can be specified there.
|
|
|
|
*
|
|
|
|
* (XXX Actually, given the current contents of valid_options[], there's
|
|
|
|
* no point in examining anything except the foreign table's own options.
|
|
|
|
* Simplify?)
|
|
|
|
*/
|
|
|
|
table = GetForeignTable(foreigntableid);
|
|
|
|
server = GetForeignServer(table->serverid);
|
|
|
|
wrapper = GetForeignDataWrapper(server->fdwid);
|
|
|
|
|
|
|
|
options = NIL;
|
|
|
|
options = list_concat(options, wrapper->options);
|
|
|
|
options = list_concat(options, server->options);
|
|
|
|
options = list_concat(options, table->options);
|
2011-09-17 04:35:51 +08:00
|
|
|
options = list_concat(options, get_file_fdw_attribute_options(foreigntableid));
|
2011-02-21 03:06:59 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Separate out the filename.
|
|
|
|
*/
|
|
|
|
*filename = NULL;
|
|
|
|
prev = NULL;
|
|
|
|
foreach(lc, options)
|
|
|
|
{
|
2011-04-10 23:42:00 +08:00
|
|
|
DefElem *def = (DefElem *) lfirst(lc);
|
2011-02-21 03:06:59 +08:00
|
|
|
|
|
|
|
if (strcmp(def->defname, "filename") == 0)
|
|
|
|
{
|
|
|
|
*filename = defGetString(def);
|
|
|
|
options = list_delete_cell(options, lc, prev);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev = lc;
|
|
|
|
}
|
2011-07-06 06:46:03 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The validator should have checked that a filename was included in the
|
|
|
|
* options, but check again, just in case.
|
|
|
|
*/
|
2011-02-21 03:06:59 +08:00
|
|
|
if (*filename == NULL)
|
2011-07-06 06:46:03 +08:00
|
|
|
elog(ERROR, "filename is required for file_fdw foreign tables");
|
|
|
|
|
2011-02-21 03:06:59 +08:00
|
|
|
*other_options = options;
|
|
|
|
}
|
|
|
|
|
2011-09-17 04:35:51 +08:00
|
|
|
/*
|
|
|
|
* Retrieve per-column generic options from pg_attribute and construct a list
|
|
|
|
* of DefElems representing them.
|
|
|
|
*
|
2014-03-05 06:31:59 +08:00
|
|
|
* At the moment we only have "force_not_null", and "force_null",
|
|
|
|
* which should each be combined into a single DefElem listing all such
|
|
|
|
* columns, since that's what COPY expects.
|
2011-09-17 04:35:51 +08:00
|
|
|
*/
|
|
|
|
static List *
|
|
|
|
get_file_fdw_attribute_options(Oid relid)
|
|
|
|
{
|
|
|
|
Relation rel;
|
|
|
|
TupleDesc tupleDesc;
|
|
|
|
AttrNumber natts;
|
|
|
|
AttrNumber attnum;
|
|
|
|
List *fnncolumns = NIL;
|
2014-03-05 06:31:59 +08:00
|
|
|
List *fncolumns = NIL;
|
|
|
|
|
|
|
|
List *options = NIL;
|
2011-09-17 04:35:51 +08:00
|
|
|
|
|
|
|
rel = heap_open(relid, AccessShareLock);
|
|
|
|
tupleDesc = RelationGetDescr(rel);
|
|
|
|
natts = tupleDesc->natts;
|
|
|
|
|
|
|
|
/* Retrieve FDW options for all user-defined attributes. */
|
|
|
|
for (attnum = 1; attnum <= natts; attnum++)
|
|
|
|
{
|
2012-03-08 07:20:58 +08:00
|
|
|
Form_pg_attribute attr = tupleDesc->attrs[attnum - 1];
|
|
|
|
List *options;
|
|
|
|
ListCell *lc;
|
2011-09-17 04:35:51 +08:00
|
|
|
|
|
|
|
/* Skip dropped attributes. */
|
2012-03-08 07:20:58 +08:00
|
|
|
if (attr->attisdropped)
|
2011-09-17 04:35:51 +08:00
|
|
|
continue;
|
|
|
|
|
2012-03-08 07:20:58 +08:00
|
|
|
options = GetForeignColumnOptions(relid, attnum);
|
|
|
|
foreach(lc, options)
|
2011-09-17 04:35:51 +08:00
|
|
|
{
|
2012-06-11 03:20:04 +08:00
|
|
|
DefElem *def = (DefElem *) lfirst(lc);
|
2011-09-17 04:35:51 +08:00
|
|
|
|
2012-03-08 07:20:58 +08:00
|
|
|
if (strcmp(def->defname, "force_not_null") == 0)
|
2011-09-17 04:35:51 +08:00
|
|
|
{
|
2012-03-08 07:20:58 +08:00
|
|
|
if (defGetBoolean(def))
|
2011-09-17 04:35:51 +08:00
|
|
|
{
|
2012-06-11 03:20:04 +08:00
|
|
|
char *attname = pstrdup(NameStr(attr->attname));
|
2011-09-17 04:35:51 +08:00
|
|
|
|
2012-03-08 07:20:58 +08:00
|
|
|
fnncolumns = lappend(fnncolumns, makeString(attname));
|
2011-09-17 04:35:51 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-05 06:31:59 +08:00
|
|
|
else if (strcmp(def->defname, "force_null") == 0)
|
|
|
|
{
|
|
|
|
if (defGetBoolean(def))
|
|
|
|
{
|
|
|
|
char *attname = pstrdup(NameStr(attr->attname));
|
|
|
|
|
|
|
|
fncolumns = lappend(fncolumns, makeString(attname));
|
|
|
|
}
|
|
|
|
}
|
2012-03-08 07:20:58 +08:00
|
|
|
/* maybe in future handle other options here */
|
2011-09-17 04:35:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
heap_close(rel, AccessShareLock);
|
|
|
|
|
2014-03-05 06:31:59 +08:00
|
|
|
/* Return DefElem only when some column(s) have force_not_null / force_null options set */
|
2011-09-17 04:35:51 +08:00
|
|
|
if (fnncolumns != NIL)
|
2014-03-05 06:31:59 +08:00
|
|
|
options = lappend(options, makeDefElem("force_not_null", (Node *) fnncolumns));
|
|
|
|
|
|
|
|
if (fncolumns != NIL)
|
|
|
|
options = lappend(options,makeDefElem("force_null", (Node *) fncolumns));
|
|
|
|
|
|
|
|
return options;
|
2011-09-17 04:35:51 +08:00
|
|
|
}
|
|
|
|
|
2011-02-21 03:06:59 +08:00
|
|
|
/*
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
* fileGetForeignRelSize
|
|
|
|
* Obtain relation size estimates for a foreign table
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fileGetForeignRelSize(PlannerInfo *root,
|
|
|
|
RelOptInfo *baserel,
|
|
|
|
Oid foreigntableid)
|
|
|
|
{
|
|
|
|
FileFdwPlanState *fdw_private;
|
|
|
|
|
|
|
|
/*
|
2012-06-11 03:20:04 +08:00
|
|
|
* Fetch options. We only need filename at this point, but we might as
|
|
|
|
* well get everything and not need to re-fetch it later in planning.
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
*/
|
|
|
|
fdw_private = (FileFdwPlanState *) palloc(sizeof(FileFdwPlanState));
|
|
|
|
fileGetOptions(foreigntableid,
|
|
|
|
&fdw_private->filename, &fdw_private->options);
|
|
|
|
baserel->fdw_private = (void *) fdw_private;
|
|
|
|
|
|
|
|
/* Estimate relation size */
|
|
|
|
estimate_size(root, baserel, fdw_private);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fileGetForeignPaths
|
2012-03-06 05:15:59 +08:00
|
|
|
* 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.
|
2011-02-21 03:06:59 +08:00
|
|
|
*/
|
2012-03-06 05:15:59 +08:00
|
|
|
static void
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
fileGetForeignPaths(PlannerInfo *root,
|
|
|
|
RelOptInfo *baserel,
|
|
|
|
Oid foreigntableid)
|
2011-02-21 03:06:59 +08:00
|
|
|
{
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
FileFdwPlanState *fdw_private = (FileFdwPlanState *) baserel->fdw_private;
|
2012-03-06 05:15:59 +08:00
|
|
|
Cost startup_cost;
|
|
|
|
Cost total_cost;
|
2012-07-13 04:26:59 +08:00
|
|
|
List *columns;
|
|
|
|
List *coptions = NIL;
|
|
|
|
|
|
|
|
/* Decide whether to selectively perform binary conversion */
|
|
|
|
if (check_selective_binary_conversion(baserel,
|
|
|
|
foreigntableid,
|
|
|
|
&columns))
|
|
|
|
coptions = list_make1(makeDefElem("convert_selectively",
|
|
|
|
(Node *) columns));
|
2011-02-21 03:06:59 +08:00
|
|
|
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
/* Estimate costs */
|
|
|
|
estimate_costs(root, baserel, fdw_private,
|
2012-03-06 05:15:59 +08:00
|
|
|
&startup_cost, &total_cost);
|
|
|
|
|
2012-07-13 04:26:59 +08:00
|
|
|
/*
|
2013-05-30 04:58:43 +08:00
|
|
|
* Create a ForeignPath node and add it as only possible path. We use the
|
2012-07-13 04:26:59 +08:00
|
|
|
* fdw_private list of the path to carry the convert_selectively option;
|
|
|
|
* it will be propagated into the fdw_private list of the Plan node.
|
|
|
|
*/
|
2012-03-06 05:15:59 +08:00
|
|
|
add_path(baserel, (Path *)
|
|
|
|
create_foreignscan_path(root, baserel,
|
|
|
|
baserel->rows,
|
|
|
|
startup_cost,
|
|
|
|
total_cost,
|
2012-06-11 03:20:04 +08:00
|
|
|
NIL, /* no pathkeys */
|
|
|
|
NULL, /* no outer rel either */
|
2012-07-13 04:26:59 +08:00
|
|
|
coptions));
|
2011-02-21 03:06:59 +08:00
|
|
|
|
2012-03-06 05:15:59 +08:00
|
|
|
/*
|
|
|
|
* If data file was sorted, and we knew it somehow, we could insert
|
2012-06-11 03:20:04 +08:00
|
|
|
* appropriate pathkeys into the ForeignPath node to tell the planner
|
|
|
|
* that.
|
2012-03-06 05:15:59 +08:00
|
|
|
*/
|
2011-02-21 03:06:59 +08:00
|
|
|
}
|
|
|
|
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
/*
|
|
|
|
* fileGetForeignPlan
|
|
|
|
* Create a ForeignScan plan node for scanning the foreign table
|
|
|
|
*/
|
|
|
|
static ForeignScan *
|
|
|
|
fileGetForeignPlan(PlannerInfo *root,
|
|
|
|
RelOptInfo *baserel,
|
|
|
|
Oid foreigntableid,
|
|
|
|
ForeignPath *best_path,
|
|
|
|
List *tlist,
|
|
|
|
List *scan_clauses)
|
|
|
|
{
|
|
|
|
Index scan_relid = baserel->relid;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have no native ability to evaluate restriction clauses, so we just
|
|
|
|
* put all the scan_clauses into the plan node's qual list for the
|
|
|
|
* executor to check. So all we have to do here is strip RestrictInfo
|
|
|
|
* nodes from the clauses and ignore pseudoconstants (which will be
|
|
|
|
* handled elsewhere).
|
|
|
|
*/
|
|
|
|
scan_clauses = extract_actual_clauses(scan_clauses, false);
|
|
|
|
|
|
|
|
/* Create the ForeignScan node */
|
|
|
|
return make_foreignscan(tlist,
|
|
|
|
scan_clauses,
|
|
|
|
scan_relid,
|
2012-06-11 03:20:04 +08:00
|
|
|
NIL, /* no expressions to evaluate */
|
2012-07-13 04:26:59 +08:00
|
|
|
best_path->fdw_private);
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
}
|
|
|
|
|
2011-02-21 03:06:59 +08:00
|
|
|
/*
|
|
|
|
* fileExplainForeignScan
|
|
|
|
* Produce extra output for EXPLAIN
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fileExplainForeignScan(ForeignScanState *node, ExplainState *es)
|
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
List *options;
|
|
|
|
|
|
|
|
/* Fetch options --- we only need filename at this point */
|
|
|
|
fileGetOptions(RelationGetRelid(node->ss.ss_currentRelation),
|
|
|
|
&filename, &options);
|
|
|
|
|
|
|
|
ExplainPropertyText("Foreign File", filename, es);
|
|
|
|
|
|
|
|
/* Suppress file size if we're not showing cost details */
|
|
|
|
if (es->costs)
|
|
|
|
{
|
2011-04-10 23:42:00 +08:00
|
|
|
struct stat stat_buf;
|
2011-02-21 03:06:59 +08:00
|
|
|
|
|
|
|
if (stat(filename, &stat_buf) == 0)
|
|
|
|
ExplainPropertyLong("Foreign File Size", (long) stat_buf.st_size,
|
|
|
|
es);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fileBeginForeignScan
|
|
|
|
* Initiate access to the file by creating CopyState
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fileBeginForeignScan(ForeignScanState *node, int eflags)
|
|
|
|
{
|
2012-07-13 04:26:59 +08:00
|
|
|
ForeignScan *plan = (ForeignScan *) node->ss.ps.plan;
|
2011-02-21 03:06:59 +08:00
|
|
|
char *filename;
|
|
|
|
List *options;
|
|
|
|
CopyState cstate;
|
|
|
|
FileFdwExecutionState *festate;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do nothing in EXPLAIN (no ANALYZE) case. node->fdw_state stays NULL.
|
|
|
|
*/
|
|
|
|
if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Fetch options of foreign table */
|
|
|
|
fileGetOptions(RelationGetRelid(node->ss.ss_currentRelation),
|
|
|
|
&filename, &options);
|
|
|
|
|
2012-07-13 04:26:59 +08:00
|
|
|
/* Add any options from the plan (currently only convert_selectively) */
|
|
|
|
options = list_concat(options, plan->fdw_private);
|
|
|
|
|
2011-02-21 03:06:59 +08:00
|
|
|
/*
|
2011-04-10 23:42:00 +08:00
|
|
|
* Create CopyState from FDW options. We always acquire all columns, so
|
|
|
|
* as to match the expected ScanTupleSlot signature.
|
2011-02-21 03:06:59 +08:00
|
|
|
*/
|
|
|
|
cstate = BeginCopyFrom(node->ss.ss_currentRelation,
|
|
|
|
filename,
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-28 00:17:21 +08:00
|
|
|
false,
|
2011-02-21 03:06:59 +08:00
|
|
|
NIL,
|
|
|
|
options);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save state in node->fdw_state. We must save enough information to call
|
|
|
|
* BeginCopyFrom() again.
|
|
|
|
*/
|
|
|
|
festate = (FileFdwExecutionState *) palloc(sizeof(FileFdwExecutionState));
|
|
|
|
festate->filename = filename;
|
|
|
|
festate->options = options;
|
|
|
|
festate->cstate = cstate;
|
|
|
|
|
|
|
|
node->fdw_state = (void *) festate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fileIterateForeignScan
|
|
|
|
* Read next record from the data file and store it into the
|
|
|
|
* ScanTupleSlot as a virtual tuple
|
|
|
|
*/
|
|
|
|
static TupleTableSlot *
|
|
|
|
fileIterateForeignScan(ForeignScanState *node)
|
|
|
|
{
|
|
|
|
FileFdwExecutionState *festate = (FileFdwExecutionState *) node->fdw_state;
|
|
|
|
TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;
|
2011-04-10 23:42:00 +08:00
|
|
|
bool found;
|
2012-11-13 00:30:51 +08:00
|
|
|
ErrorContextCallback errcallback;
|
2011-02-21 03:06:59 +08:00
|
|
|
|
|
|
|
/* Set up callback to identify error line number. */
|
2012-11-13 00:30:51 +08:00
|
|
|
errcallback.callback = CopyFromErrorCallback;
|
|
|
|
errcallback.arg = (void *) festate->cstate;
|
|
|
|
errcallback.previous = error_context_stack;
|
|
|
|
error_context_stack = &errcallback;
|
2011-02-21 03:06:59 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The protocol for loading a virtual tuple into a slot is first
|
|
|
|
* ExecClearTuple, then fill the values/isnull arrays, then
|
2011-04-10 23:42:00 +08:00
|
|
|
* ExecStoreVirtualTuple. If we don't find another row in the file, we
|
|
|
|
* just skip the last step, leaving the slot empty as required.
|
2011-02-21 03:06:59 +08:00
|
|
|
*
|
|
|
|
* We can pass ExprContext = NULL because we read all columns from the
|
|
|
|
* file, so no need to evaluate default expressions.
|
|
|
|
*
|
|
|
|
* We can also pass tupleOid = NULL because we don't allow oids for
|
|
|
|
* foreign tables.
|
|
|
|
*/
|
|
|
|
ExecClearTuple(slot);
|
|
|
|
found = NextCopyFrom(festate->cstate, NULL,
|
|
|
|
slot->tts_values, slot->tts_isnull,
|
|
|
|
NULL);
|
|
|
|
if (found)
|
|
|
|
ExecStoreVirtualTuple(slot);
|
|
|
|
|
|
|
|
/* Remove error callback. */
|
2012-11-13 00:30:51 +08:00
|
|
|
error_context_stack = errcallback.previous;
|
2011-02-21 03:06:59 +08:00
|
|
|
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
2012-04-07 03:02:35 +08:00
|
|
|
/*
|
|
|
|
* fileReScanForeignScan
|
|
|
|
* Rescan table, possibly with new parameters
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fileReScanForeignScan(ForeignScanState *node)
|
|
|
|
{
|
|
|
|
FileFdwExecutionState *festate = (FileFdwExecutionState *) node->fdw_state;
|
|
|
|
|
|
|
|
EndCopyFrom(festate->cstate);
|
|
|
|
|
|
|
|
festate->cstate = BeginCopyFrom(node->ss.ss_currentRelation,
|
|
|
|
festate->filename,
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-28 00:17:21 +08:00
|
|
|
false,
|
2012-04-07 03:02:35 +08:00
|
|
|
NIL,
|
|
|
|
festate->options);
|
|
|
|
}
|
|
|
|
|
2011-02-21 03:06:59 +08:00
|
|
|
/*
|
|
|
|
* fileEndForeignScan
|
|
|
|
* Finish scanning foreign table and dispose objects used for this scan
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fileEndForeignScan(ForeignScanState *node)
|
|
|
|
{
|
|
|
|
FileFdwExecutionState *festate = (FileFdwExecutionState *) node->fdw_state;
|
|
|
|
|
|
|
|
/* if festate is NULL, we are in EXPLAIN; nothing to do */
|
|
|
|
if (festate)
|
|
|
|
EndCopyFrom(festate->cstate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-04-07 03:02:35 +08:00
|
|
|
* fileAnalyzeForeignTable
|
|
|
|
* Test whether analyzing this foreign table is supported
|
2011-02-21 03:06:59 +08:00
|
|
|
*/
|
2012-04-07 04:04:10 +08:00
|
|
|
static bool
|
|
|
|
fileAnalyzeForeignTable(Relation relation,
|
|
|
|
AcquireSampleRowsFunc *func,
|
|
|
|
BlockNumber *totalpages)
|
2011-02-21 03:06:59 +08:00
|
|
|
{
|
2012-04-07 04:04:10 +08:00
|
|
|
char *filename;
|
|
|
|
List *options;
|
2012-06-11 03:20:04 +08:00
|
|
|
struct stat stat_buf;
|
2012-04-07 04:04:10 +08:00
|
|
|
|
|
|
|
/* Fetch options of foreign table */
|
|
|
|
fileGetOptions(RelationGetRelid(relation), &filename, &options);
|
|
|
|
|
|
|
|
/*
|
2012-06-11 03:20:04 +08:00
|
|
|
* Get size of the file. (XXX if we fail here, would it be better to just
|
|
|
|
* return false to skip analyzing the table?)
|
2012-04-07 04:04:10 +08:00
|
|
|
*/
|
|
|
|
if (stat(filename, &stat_buf) < 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not stat file \"%s\": %m",
|
|
|
|
filename)));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert size to pages. Must return at least 1 so that we can tell
|
|
|
|
* later on that pg_class.relpages is not default.
|
|
|
|
*/
|
|
|
|
*totalpages = (stat_buf.st_size + (BLCKSZ - 1)) / BLCKSZ;
|
|
|
|
if (*totalpages < 1)
|
|
|
|
*totalpages = 1;
|
|
|
|
|
|
|
|
*func = file_acquire_sample_rows;
|
|
|
|
|
|
|
|
return true;
|
2011-02-21 03:06:59 +08:00
|
|
|
}
|
|
|
|
|
2012-07-13 04:26:59 +08:00
|
|
|
/*
|
|
|
|
* check_selective_binary_conversion
|
|
|
|
*
|
|
|
|
* Check to see if it's useful to convert only a subset of the file's columns
|
|
|
|
* to binary. If so, construct a list of the column names to be converted,
|
|
|
|
* return that at *columns, and return TRUE. (Note that it's possible to
|
|
|
|
* determine that no columns need be converted, for instance with a COUNT(*)
|
|
|
|
* query. So we can't use returning a NIL list to indicate failure.)
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
check_selective_binary_conversion(RelOptInfo *baserel,
|
|
|
|
Oid foreigntableid,
|
|
|
|
List **columns)
|
|
|
|
{
|
|
|
|
ForeignTable *table;
|
|
|
|
ListCell *lc;
|
|
|
|
Relation rel;
|
|
|
|
TupleDesc tupleDesc;
|
|
|
|
AttrNumber attnum;
|
|
|
|
Bitmapset *attrs_used = NULL;
|
|
|
|
bool has_wholerow = false;
|
|
|
|
int numattrs;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
*columns = NIL; /* default result */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check format of the file. If binary format, this is irrelevant.
|
|
|
|
*/
|
|
|
|
table = GetForeignTable(foreigntableid);
|
|
|
|
foreach(lc, table->options)
|
|
|
|
{
|
|
|
|
DefElem *def = (DefElem *) lfirst(lc);
|
|
|
|
|
|
|
|
if (strcmp(def->defname, "format") == 0)
|
|
|
|
{
|
|
|
|
char *format = defGetString(def);
|
|
|
|
|
|
|
|
if (strcmp(format, "binary") == 0)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Collect all the attributes needed for joins or final output. */
|
|
|
|
pull_varattnos((Node *) baserel->reltargetlist, baserel->relid,
|
|
|
|
&attrs_used);
|
|
|
|
|
|
|
|
/* Add all the attributes used by restriction clauses. */
|
|
|
|
foreach(lc, baserel->baserestrictinfo)
|
|
|
|
{
|
2013-05-30 04:58:43 +08:00
|
|
|
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
|
2012-07-13 04:26:59 +08:00
|
|
|
|
|
|
|
pull_varattnos((Node *) rinfo->clause, baserel->relid,
|
|
|
|
&attrs_used);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert attribute numbers to column names. */
|
|
|
|
rel = heap_open(foreigntableid, AccessShareLock);
|
|
|
|
tupleDesc = RelationGetDescr(rel);
|
|
|
|
|
|
|
|
while ((attnum = bms_first_member(attrs_used)) >= 0)
|
|
|
|
{
|
|
|
|
/* Adjust for system attributes. */
|
|
|
|
attnum += FirstLowInvalidHeapAttributeNumber;
|
|
|
|
|
|
|
|
if (attnum == 0)
|
|
|
|
{
|
|
|
|
has_wholerow = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore system attributes. */
|
|
|
|
if (attnum < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Get user attributes. */
|
|
|
|
if (attnum > 0)
|
|
|
|
{
|
|
|
|
Form_pg_attribute attr = tupleDesc->attrs[attnum - 1];
|
|
|
|
char *attname = NameStr(attr->attname);
|
|
|
|
|
|
|
|
/* Skip dropped attributes (probably shouldn't see any here). */
|
|
|
|
if (attr->attisdropped)
|
|
|
|
continue;
|
|
|
|
*columns = lappend(*columns, makeString(pstrdup(attname)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Count non-dropped user attributes while we have the tupdesc. */
|
|
|
|
numattrs = 0;
|
|
|
|
for (i = 0; i < tupleDesc->natts; i++)
|
|
|
|
{
|
|
|
|
Form_pg_attribute attr = tupleDesc->attrs[i];
|
|
|
|
|
|
|
|
if (attr->attisdropped)
|
|
|
|
continue;
|
|
|
|
numattrs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
heap_close(rel, AccessShareLock);
|
|
|
|
|
|
|
|
/* If there's a whole-row reference, fail: we need all the columns. */
|
|
|
|
if (has_wholerow)
|
|
|
|
{
|
|
|
|
*columns = NIL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If all the user attributes are needed, fail. */
|
|
|
|
if (numattrs == list_length(*columns))
|
|
|
|
{
|
|
|
|
*columns = NIL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-21 03:06:59 +08:00
|
|
|
/*
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
* Estimate size of a foreign table.
|
2012-03-06 05:15:59 +08:00
|
|
|
*
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
* The main result is returned in baserel->rows. We also set
|
|
|
|
* fdw_private->pages and fdw_private->ntuples for later use in the cost
|
|
|
|
* calculation.
|
2011-02-21 03:06:59 +08:00
|
|
|
*/
|
|
|
|
static void
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
estimate_size(PlannerInfo *root, RelOptInfo *baserel,
|
|
|
|
FileFdwPlanState *fdw_private)
|
2011-02-21 03:06:59 +08:00
|
|
|
{
|
2011-04-10 23:42:00 +08:00
|
|
|
struct stat stat_buf;
|
|
|
|
BlockNumber pages;
|
|
|
|
double ntuples;
|
|
|
|
double nrows;
|
2011-02-21 03:06:59 +08:00
|
|
|
|
|
|
|
/*
|
2011-04-10 23:42:00 +08:00
|
|
|
* Get size of the file. It might not be there at plan time, though, in
|
|
|
|
* which case we have to use a default estimate.
|
2011-02-21 03:06:59 +08:00
|
|
|
*/
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
if (stat(fdw_private->filename, &stat_buf) < 0)
|
2011-02-21 03:06:59 +08:00
|
|
|
stat_buf.st_size = 10 * BLCKSZ;
|
|
|
|
|
|
|
|
/*
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
* Convert size to pages for use in I/O cost estimate later.
|
2011-02-21 03:06:59 +08:00
|
|
|
*/
|
2011-04-10 23:42:00 +08:00
|
|
|
pages = (stat_buf.st_size + (BLCKSZ - 1)) / BLCKSZ;
|
2011-02-21 03:06:59 +08:00
|
|
|
if (pages < 1)
|
|
|
|
pages = 1;
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
fdw_private->pages = pages;
|
|
|
|
|
2011-02-21 03:06:59 +08:00
|
|
|
/*
|
2012-04-07 03:02:35 +08:00
|
|
|
* Estimate the number of tuples in the file.
|
2011-02-21 03:06:59 +08:00
|
|
|
*/
|
2012-04-07 03:02:35 +08:00
|
|
|
if (baserel->pages > 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We have # of pages and # of tuples from pg_class (that is, from a
|
|
|
|
* previous ANALYZE), so compute a tuples-per-page estimate and scale
|
|
|
|
* that by the current file size.
|
|
|
|
*/
|
|
|
|
double density;
|
2011-02-21 03:06:59 +08:00
|
|
|
|
2012-04-07 03:02:35 +08:00
|
|
|
density = baserel->tuples / (double) baserel->pages;
|
|
|
|
ntuples = clamp_row_est(density * (double) pages);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Otherwise we have to fake it. We back into this estimate using the
|
|
|
|
* planner's idea of the relation width; which is bogus if not all
|
|
|
|
* columns are being read, not to mention that the text representation
|
|
|
|
* of a row probably isn't the same size as its internal
|
2012-06-11 03:20:04 +08:00
|
|
|
* representation. Possibly we could do something better, but the
|
2012-04-07 03:02:35 +08:00
|
|
|
* real answer to anyone who complains is "ANALYZE" ...
|
|
|
|
*/
|
|
|
|
int tuple_width;
|
2011-02-21 03:06:59 +08:00
|
|
|
|
2012-04-07 03:02:35 +08:00
|
|
|
tuple_width = MAXALIGN(baserel->width) +
|
|
|
|
MAXALIGN(sizeof(HeapTupleHeaderData));
|
|
|
|
ntuples = clamp_row_est((double) stat_buf.st_size /
|
|
|
|
(double) tuple_width);
|
|
|
|
}
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
fdw_private->ntuples = ntuples;
|
|
|
|
|
2011-02-21 03:06:59 +08:00
|
|
|
/*
|
2011-04-10 23:42:00 +08:00
|
|
|
* Now estimate the number of rows returned by the scan after applying the
|
2012-04-07 03:02:35 +08:00
|
|
|
* baserestrictinfo quals.
|
2011-02-21 03:06:59 +08:00
|
|
|
*/
|
|
|
|
nrows = ntuples *
|
|
|
|
clauselist_selectivity(root,
|
|
|
|
baserel->baserestrictinfo,
|
|
|
|
0,
|
|
|
|
JOIN_INNER,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
nrows = clamp_row_est(nrows);
|
|
|
|
|
|
|
|
/* Save the output-rows estimate for the planner */
|
|
|
|
baserel->rows = nrows;
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Estimate costs of scanning a foreign table.
|
|
|
|
*
|
|
|
|
* Results are returned in *startup_cost and *total_cost.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
estimate_costs(PlannerInfo *root, RelOptInfo *baserel,
|
|
|
|
FileFdwPlanState *fdw_private,
|
|
|
|
Cost *startup_cost, Cost *total_cost)
|
|
|
|
{
|
|
|
|
BlockNumber pages = fdw_private->pages;
|
|
|
|
double ntuples = fdw_private->ntuples;
|
|
|
|
Cost run_cost = 0;
|
|
|
|
Cost cpu_per_tuple;
|
2011-02-21 03:06:59 +08:00
|
|
|
|
|
|
|
/*
|
Revise FDW planning API, again.
Further reflection shows that a single callback isn't very workable if we
desire to let FDWs generate multiple Paths, because that forces the FDW to
do all work necessary to generate a valid Plan node for each Path. Instead
split the former PlanForeignScan API into three steps: GetForeignRelSize,
GetForeignPaths, GetForeignPlan. We had already bit the bullet of breaking
the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
and it's substantially more flexible for complex FDWs.
Add an fdw_private field to RelOptInfo so that the new functions can save
state there rather than possibly having to recalculate information two or
three times.
In addition, we'd not thought through what would be needed to allow an FDW
to set up subexpressions of its choice for runtime execution. We could
treat ForeignScan.fdw_private as an executable expression but that seems
likely to break existing FDWs unnecessarily (in particular, it would
restrict the set of node types allowable in fdw_private to those supported
by expression_tree_walker). Instead, invent a separate field fdw_exprs
which will receive the postprocessing appropriate for expression trees.
(One field is enough since it can be a list of expressions; also, we assume
the corresponding expression state tree(s) will be held within fdw_state,
so we don't need to add anything to ForeignScanState.)
Per review of Hanada Shigeru's pgsql_fdw patch. We may need to tweak this
further as we continue to work on that patch, but to me it feels a lot
closer to being right now.
2012-03-10 01:48:48 +08:00
|
|
|
* We estimate costs almost the same way as cost_seqscan(), thus assuming
|
|
|
|
* that I/O costs are equivalent to a regular table file of the same size.
|
|
|
|
* However, we take per-tuple CPU costs as 10x of a seqscan, to account
|
|
|
|
* for the cost of parsing records.
|
2011-02-21 03:06:59 +08:00
|
|
|
*/
|
|
|
|
run_cost += seq_page_cost * pages;
|
|
|
|
|
|
|
|
*startup_cost = baserel->baserestrictcost.startup;
|
|
|
|
cpu_per_tuple = cpu_tuple_cost * 10 + baserel->baserestrictcost.per_tuple;
|
|
|
|
run_cost += cpu_per_tuple * ntuples;
|
|
|
|
*total_cost = *startup_cost + run_cost;
|
|
|
|
}
|
2012-04-07 03:02:35 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* file_acquire_sample_rows -- acquire a random sample of rows from the table
|
|
|
|
*
|
|
|
|
* Selected rows are returned in the caller-allocated array rows[],
|
|
|
|
* which must have at least targrows entries.
|
|
|
|
* The actual number of rows selected is returned as the function result.
|
|
|
|
* We also count the total number of rows in the file and return it into
|
2012-06-11 03:20:04 +08:00
|
|
|
* *totalrows. Note that *totaldeadrows is always set to 0.
|
2012-04-07 03:02:35 +08:00
|
|
|
*
|
|
|
|
* Note that the returned list of rows is not always in order by physical
|
|
|
|
* position in the file. Therefore, correlation estimates derived later
|
|
|
|
* may be meaningless, but it's OK because we don't use the estimates
|
|
|
|
* currently (the planner only pays attention to correlation for indexscans).
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
file_acquire_sample_rows(Relation onerel, int elevel,
|
|
|
|
HeapTuple *rows, int targrows,
|
2012-04-07 04:04:10 +08:00
|
|
|
double *totalrows, double *totaldeadrows)
|
2012-04-07 03:02:35 +08:00
|
|
|
{
|
|
|
|
int numrows = 0;
|
2012-06-11 03:20:04 +08:00
|
|
|
double rowstoskip = -1; /* -1 means not set yet */
|
2012-04-07 03:02:35 +08:00
|
|
|
double rstate;
|
|
|
|
TupleDesc tupDesc;
|
|
|
|
Datum *values;
|
|
|
|
bool *nulls;
|
|
|
|
bool found;
|
|
|
|
char *filename;
|
|
|
|
List *options;
|
|
|
|
CopyState cstate;
|
2012-11-13 00:30:51 +08:00
|
|
|
ErrorContextCallback errcallback;
|
2012-04-07 03:02:35 +08:00
|
|
|
MemoryContext oldcontext = CurrentMemoryContext;
|
|
|
|
MemoryContext tupcontext;
|
|
|
|
|
|
|
|
Assert(onerel);
|
|
|
|
Assert(targrows > 0);
|
|
|
|
|
|
|
|
tupDesc = RelationGetDescr(onerel);
|
|
|
|
values = (Datum *) palloc(tupDesc->natts * sizeof(Datum));
|
|
|
|
nulls = (bool *) palloc(tupDesc->natts * sizeof(bool));
|
|
|
|
|
|
|
|
/* Fetch options of foreign table */
|
|
|
|
fileGetOptions(RelationGetRelid(onerel), &filename, &options);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create CopyState from FDW options.
|
|
|
|
*/
|
Add support for piping COPY to/from an external program.
This includes backend "COPY TO/FROM PROGRAM '...'" syntax, and corresponding
psql \copy syntax. Like with reading/writing files, the backend version is
superuser-only, and in the psql version, the program is run in the client.
In the passing, the psql \copy STDIN/STDOUT syntax is subtly changed: if you
the stdin/stdout is quoted, it's now interpreted as a filename. For example,
"\copy foo from 'stdin'" now reads from a file called 'stdin', not from
standard input. Before this, there was no way to specify a filename called
stdin, stdout, pstdin or pstdout.
This creates a new function in pgport, wait_result_to_str(), which can
be used to convert the exit status of a process, as returned by wait(3),
to a human-readable string.
Etsuro Fujita, reviewed by Amit Kapila.
2013-02-28 00:17:21 +08:00
|
|
|
cstate = BeginCopyFrom(onerel, filename, false, NIL, options);
|
2012-04-07 03:02:35 +08:00
|
|
|
|
|
|
|
/*
|
2012-06-11 03:20:04 +08:00
|
|
|
* Use per-tuple memory context to prevent leak of memory used to read
|
|
|
|
* rows from the file with Copy routines.
|
2012-04-07 03:02:35 +08:00
|
|
|
*/
|
|
|
|
tupcontext = AllocSetContextCreate(CurrentMemoryContext,
|
|
|
|
"file_fdw temporary context",
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
|
|
|
|
|
|
|
/* Prepare for sampling rows */
|
|
|
|
rstate = anl_init_selection_state(targrows);
|
|
|
|
|
|
|
|
/* Set up callback to identify error line number. */
|
2012-11-13 00:30:51 +08:00
|
|
|
errcallback.callback = CopyFromErrorCallback;
|
|
|
|
errcallback.arg = (void *) cstate;
|
|
|
|
errcallback.previous = error_context_stack;
|
|
|
|
error_context_stack = &errcallback;
|
2012-04-07 03:02:35 +08:00
|
|
|
|
|
|
|
*totalrows = 0;
|
|
|
|
*totaldeadrows = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Check for user-requested abort or sleep */
|
|
|
|
vacuum_delay_point();
|
|
|
|
|
|
|
|
/* Fetch next row */
|
|
|
|
MemoryContextReset(tupcontext);
|
|
|
|
MemoryContextSwitchTo(tupcontext);
|
|
|
|
|
|
|
|
found = NextCopyFrom(cstate, NULL, values, nulls, NULL);
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The first targrows sample rows are simply copied into the
|
|
|
|
* reservoir. Then we start replacing tuples in the sample until we
|
|
|
|
* reach the end of the relation. This algorithm is from Jeff Vitter's
|
|
|
|
* paper (see more info in commands/analyze.c).
|
|
|
|
*/
|
|
|
|
if (numrows < targrows)
|
|
|
|
{
|
|
|
|
rows[numrows++] = heap_form_tuple(tupDesc, values, nulls);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* t in Vitter's paper is the number of records already processed.
|
|
|
|
* If we need to compute a new S value, we must use the
|
|
|
|
* not-yet-incremented value of totalrows as t.
|
|
|
|
*/
|
|
|
|
if (rowstoskip < 0)
|
|
|
|
rowstoskip = anl_get_next_S(*totalrows, targrows, &rstate);
|
|
|
|
|
|
|
|
if (rowstoskip <= 0)
|
|
|
|
{
|
|
|
|
/*
|
2012-06-11 03:20:04 +08:00
|
|
|
* Found a suitable tuple, so save it, replacing one old tuple
|
|
|
|
* at random
|
2012-04-07 03:02:35 +08:00
|
|
|
*/
|
2012-06-11 03:20:04 +08:00
|
|
|
int k = (int) (targrows * anl_random_fract());
|
2012-04-07 03:02:35 +08:00
|
|
|
|
|
|
|
Assert(k >= 0 && k < targrows);
|
|
|
|
heap_freetuple(rows[k]);
|
|
|
|
rows[k] = heap_form_tuple(tupDesc, values, nulls);
|
|
|
|
}
|
|
|
|
|
|
|
|
rowstoskip -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*totalrows += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove error callback. */
|
2012-11-13 00:30:51 +08:00
|
|
|
error_context_stack = errcallback.previous;
|
2012-04-07 03:02:35 +08:00
|
|
|
|
|
|
|
/* Clean up. */
|
|
|
|
MemoryContextDelete(tupcontext);
|
|
|
|
|
|
|
|
EndCopyFrom(cstate);
|
|
|
|
|
|
|
|
pfree(values);
|
|
|
|
pfree(nulls);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Emit some interesting relation info
|
|
|
|
*/
|
|
|
|
ereport(elevel,
|
2012-04-07 04:04:10 +08:00
|
|
|
(errmsg("\"%s\": file contains %.0f rows; "
|
2012-04-07 03:02:35 +08:00
|
|
|
"%d rows in sample",
|
|
|
|
RelationGetRelationName(onerel),
|
2012-04-07 04:04:10 +08:00
|
|
|
*totalrows, numrows)));
|
2012-04-07 03:02:35 +08:00
|
|
|
|
|
|
|
return numrows;
|
|
|
|
}
|