2011-02-20 20:06:59 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* file_fdw.c
|
2016-09-29 19:32:27 +02:00
|
|
|
* foreign-data wrapper for server-side flat files (or programs).
|
2011-02-20 20:06:59 +01:00
|
|
|
*
|
2021-01-02 19:06:25 +01:00
|
|
|
* Copyright (c) 2010-2021, PostgreSQL Global Development Group
|
2011-02-20 20:06:59 +01:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* contrib/file_fdw/file_fdw.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-08-30 22:15:44 +02:00
|
|
|
#include "access/htup_details.h"
|
2011-02-20 20:06:59 +01:00
|
|
|
#include "access/reloptions.h"
|
2012-07-12 22:26:59 +02:00
|
|
|
#include "access/sysattr.h"
|
2019-01-21 19:18:20 +01:00
|
|
|
#include "access/table.h"
|
2018-04-06 20:47:10 +02:00
|
|
|
#include "catalog/pg_authid.h"
|
2011-02-20 20:06:59 +01:00
|
|
|
#include "catalog/pg_foreign_table.h"
|
|
|
|
#include "commands/copy.h"
|
|
|
|
#include "commands/defrem.h"
|
|
|
|
#include "commands/explain.h"
|
2012-04-06 21:02:35 +02:00
|
|
|
#include "commands/vacuum.h"
|
2011-02-20 20:06:59 +01:00
|
|
|
#include "foreign/fdwapi.h"
|
|
|
|
#include "foreign/foreign.h"
|
|
|
|
#include "miscadmin.h"
|
2011-09-16 22:35:51 +02:00
|
|
|
#include "nodes/makefuncs.h"
|
2019-01-29 21:48:51 +01:00
|
|
|
#include "optimizer/optimizer.h"
|
2012-03-05 22:15:59 +01: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-09 18:48:48 +01:00
|
|
|
#include "optimizer/planmain.h"
|
|
|
|
#include "optimizer/restrictinfo.h"
|
2020-03-10 10:22:52 +01:00
|
|
|
#include "utils/acl.h"
|
2012-04-06 21:02:35 +02:00
|
|
|
#include "utils/memutils.h"
|
2011-02-23 18:18:09 +01:00
|
|
|
#include "utils/rel.h"
|
2015-05-15 04:02:54 +02:00
|
|
|
#include "utils/sampling.h"
|
2011-02-20 20:06:59 +01: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-04 23:31:59 +01: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-20 20:06:59 +01: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-09 18:48:48 +01:00
|
|
|
static const struct FileFdwOption valid_options[] = {
|
2016-09-29 19:32:27 +02:00
|
|
|
/* Data source options */
|
2011-04-10 17:42:00 +02:00
|
|
|
{"filename", ForeignTableRelationId},
|
2016-09-29 19:32:27 +02:00
|
|
|
{"program", ForeignTableRelationId},
|
2011-02-20 20:06:59 +01:00
|
|
|
|
|
|
|
/* Format options */
|
|
|
|
/* oids option is not supported */
|
2011-04-10 17:42:00 +02:00
|
|
|
{"format", ForeignTableRelationId},
|
|
|
|
{"header", ForeignTableRelationId},
|
|
|
|
{"delimiter", ForeignTableRelationId},
|
|
|
|
{"quote", ForeignTableRelationId},
|
|
|
|
{"escape", ForeignTableRelationId},
|
|
|
|
{"null", ForeignTableRelationId},
|
|
|
|
{"encoding", ForeignTableRelationId},
|
2011-09-16 22:35:51 +02:00
|
|
|
{"force_not_null", AttributeRelationId},
|
2014-03-04 23:31:59 +01:00
|
|
|
{"force_null", AttributeRelationId},
|
2014-05-06 18:12:18 +02:00
|
|
|
|
2011-02-20 20:06:59 +01:00
|
|
|
/*
|
|
|
|
* force_quote is not supported by file_fdw because it's for COPY TO.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Sentinel */
|
2011-04-10 17:42:00 +02:00
|
|
|
{NULL, InvalidOid}
|
2011-02-20 20:06:59 +01: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-09 18:48:48 +01:00
|
|
|
/*
|
|
|
|
* FDW-specific information for RelOptInfo.fdw_private.
|
|
|
|
*/
|
|
|
|
typedef struct FileFdwPlanState
|
|
|
|
{
|
2016-09-29 19:32:27 +02:00
|
|
|
char *filename; /* file or program to read from */
|
|
|
|
bool is_program; /* true if filename represents an OS command */
|
|
|
|
List *options; /* merged COPY options, excluding filename and
|
|
|
|
* is_program */
|
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-09 18:48:48 +01:00
|
|
|
BlockNumber pages; /* estimate of file's physical size */
|
2016-09-29 19:32:27 +02:00
|
|
|
double ntuples; /* estimate of number of data rows */
|
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-09 18:48:48 +01:00
|
|
|
} FileFdwPlanState;
|
|
|
|
|
2011-02-20 20:06:59 +01:00
|
|
|
/*
|
|
|
|
* FDW-specific information for ForeignScanState.fdw_state.
|
|
|
|
*/
|
|
|
|
typedef struct FileFdwExecutionState
|
|
|
|
{
|
2016-09-29 19:32:27 +02:00
|
|
|
char *filename; /* file or program to read from */
|
|
|
|
bool is_program; /* true if filename represents an OS command */
|
|
|
|
List *options; /* merged COPY options, excluding filename and
|
|
|
|
* is_program */
|
Split copy.c into four files.
Copy.c has grown really large. Split it into more manageable parts:
- copy.c now contains only a few functions that are common to COPY FROM
and COPY TO.
- copyto.c contains code for COPY TO.
- copyfrom.c contains code for initializing COPY FROM, and inserting the
tuples to the correct table.
- copyfromparse.c contains code for reading from the client/file/program,
and parsing the input text/CSV/binary format into tuples.
All of these parts are fairly complicated, and fairly independent of each
other. There is a patch being discussed to implement parallel COPY FROM,
which will add a lot of new code to the COPY FROM path, and another patch
which would allow INSERTs to use the same multi-insert machinery as COPY
FROM, both of which will require refactoring that code. With those two
patches, there's going to be a lot of code churn in copy.c anyway, so now
seems like a good time to do this refactoring.
The CopyStateData struct is also split. All the formatting options, like
FORMAT, QUOTE, ESCAPE, are put in a new CopyFormatOption struct, which
is used by both COPY FROM and TO. Other state data are kept in separate
CopyFromStateData and CopyToStateData structs.
Reviewed-by: Soumyadeep Chakraborty, Erik Rijkers, Vignesh C, Andres Freund
Discussion: https://www.postgresql.org/message-id/8e15b560-f387-7acc-ac90-763986617bfb%40iki.fi
2020-11-23 09:50:50 +01:00
|
|
|
CopyFromState cstate; /* COPY execution state */
|
2011-02-20 20:06:59 +01:00
|
|
|
} FileFdwExecutionState;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SQL functions
|
|
|
|
*/
|
|
|
|
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-09 18:48:48 +01:00
|
|
|
static void fileGetForeignRelSize(PlannerInfo *root,
|
2019-05-22 19:04:48 +02: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-09 18:48:48 +01:00
|
|
|
static void fileGetForeignPaths(PlannerInfo *root,
|
2019-05-22 19:04:48 +02: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-09 18:48:48 +01:00
|
|
|
static ForeignScan *fileGetForeignPlan(PlannerInfo *root,
|
2019-05-22 19:04:48 +02:00
|
|
|
RelOptInfo *baserel,
|
|
|
|
Oid foreigntableid,
|
|
|
|
ForeignPath *best_path,
|
|
|
|
List *tlist,
|
|
|
|
List *scan_clauses,
|
|
|
|
Plan *outer_plan);
|
2011-02-20 20:06:59 +01: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-06 22:04:10 +02:00
|
|
|
static bool fileAnalyzeForeignTable(Relation relation,
|
2019-05-22 19:04:48 +02:00
|
|
|
AcquireSampleRowsFunc *func,
|
|
|
|
BlockNumber *totalpages);
|
2016-02-26 11:44:46 +01:00
|
|
|
static bool fileIsForeignScanParallelSafe(PlannerInfo *root, RelOptInfo *rel,
|
2019-05-22 19:04:48 +02:00
|
|
|
RangeTblEntry *rte);
|
2011-02-20 20:06:59 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper functions
|
|
|
|
*/
|
|
|
|
static bool is_valid_option(const char *option, Oid context);
|
|
|
|
static void fileGetOptions(Oid foreigntableid,
|
2019-05-22 19:04:48 +02:00
|
|
|
char **filename,
|
|
|
|
bool *is_program,
|
|
|
|
List **other_options);
|
2011-09-16 22:35:51 +02:00
|
|
|
static List *get_file_fdw_attribute_options(Oid relid);
|
2012-07-12 22:26:59 +02:00
|
|
|
static bool check_selective_binary_conversion(RelOptInfo *baserel,
|
2019-05-22 19:04:48 +02: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-09 18:48:48 +01:00
|
|
|
static void estimate_size(PlannerInfo *root, RelOptInfo *baserel,
|
2019-05-22 19:04:48 +02:00
|
|
|
FileFdwPlanState *fdw_private);
|
2011-02-20 20:06:59 +01:00
|
|
|
static void estimate_costs(PlannerInfo *root, RelOptInfo *baserel,
|
2019-05-22 19:04:48 +02:00
|
|
|
FileFdwPlanState *fdw_private,
|
|
|
|
Cost *startup_cost, Cost *total_cost);
|
|
|
|
static int file_acquire_sample_rows(Relation onerel, int elevel,
|
|
|
|
HeapTuple *rows, int targrows,
|
|
|
|
double *totalrows, double *totaldeadrows);
|
2011-02-20 20:06:59 +01: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-09 18:48:48 +01:00
|
|
|
fdwroutine->GetForeignRelSize = fileGetForeignRelSize;
|
|
|
|
fdwroutine->GetForeignPaths = fileGetForeignPaths;
|
|
|
|
fdwroutine->GetForeignPlan = fileGetForeignPlan;
|
2011-02-20 20:06:59 +01:00
|
|
|
fdwroutine->ExplainForeignScan = fileExplainForeignScan;
|
|
|
|
fdwroutine->BeginForeignScan = fileBeginForeignScan;
|
|
|
|
fdwroutine->IterateForeignScan = fileIterateForeignScan;
|
|
|
|
fdwroutine->ReScanForeignScan = fileReScanForeignScan;
|
|
|
|
fdwroutine->EndForeignScan = fileEndForeignScan;
|
2012-04-06 21:02:35 +02:00
|
|
|
fdwroutine->AnalyzeForeignTable = fileAnalyzeForeignTable;
|
2016-02-26 11:44:46 +01:00
|
|
|
fdwroutine->IsForeignScanParallelSafe = fileIsForeignScanParallelSafe;
|
2011-02-20 20:06:59 +01: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-10 21:20:04 +02:00
|
|
|
DefElem *force_not_null = NULL;
|
2014-03-04 23:31:59 +01:00
|
|
|
DefElem *force_null = NULL;
|
2011-02-20 20:06:59 +01:00
|
|
|
List *other_options = NIL;
|
|
|
|
ListCell *cell;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that only options supported by file_fdw, and allowed for the
|
|
|
|
* current object type, are given.
|
|
|
|
*/
|
|
|
|
foreach(cell, options_list)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
DefElem *def = (DefElem *) lfirst(cell);
|
2011-02-20 20:06:59 +01: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-09 18:48:48 +01:00
|
|
|
const struct FileFdwOption *opt;
|
2011-02-20 20:06:59 +01: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-17 19:20:33 +02:00
|
|
|
buf.len > 0
|
|
|
|
? errhint("Valid options in this context are: %s",
|
|
|
|
buf.data)
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
: errhint("There are no valid options in this context.")));
|
2011-02-20 20:06:59 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 22:35:51 +02:00
|
|
|
/*
|
2016-09-29 19:32:27 +02:00
|
|
|
* Separate out filename, program, and column-specific options, since
|
2014-03-04 23:31:59 +01:00
|
|
|
* ProcessCopyOptions won't accept them.
|
2011-09-16 22:35:51 +02:00
|
|
|
*/
|
2016-09-29 19:32:27 +02:00
|
|
|
if (strcmp(def->defname, "filename") == 0 ||
|
|
|
|
strcmp(def->defname, "program") == 0)
|
2011-02-20 20:06:59 +01:00
|
|
|
{
|
|
|
|
if (filename)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("conflicting or redundant options")));
|
2018-04-06 20:47:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check permissions for changing which file or program is used by
|
|
|
|
* the file_fdw.
|
|
|
|
*
|
|
|
|
* Only members of the role 'pg_read_server_files' are allowed to
|
|
|
|
* set the 'filename' option of a file_fdw foreign table, while
|
|
|
|
* only members of the role 'pg_execute_server_program' are
|
|
|
|
* allowed to set the 'program' option. This is because we don't
|
|
|
|
* want regular users to be able to control which file gets read
|
|
|
|
* or which program gets executed.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Note that the valid_options[] array disallows setting filename
|
|
|
|
* and program at any options level other than foreign table ---
|
|
|
|
* otherwise there'd still be a security hole.
|
|
|
|
*/
|
|
|
|
if (strcmp(def->defname, "filename") == 0 &&
|
2021-04-01 21:32:06 +02:00
|
|
|
!is_member_of_role(GetUserId(), ROLE_PG_READ_SERVER_FILES))
|
2018-04-06 20:47:10 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("only superuser or a member of the pg_read_server_files role may specify the filename option of a file_fdw foreign table")));
|
|
|
|
|
|
|
|
if (strcmp(def->defname, "program") == 0 &&
|
2021-04-01 21:32:06 +02:00
|
|
|
!is_member_of_role(GetUserId(), ROLE_PG_EXECUTE_SERVER_PROGRAM))
|
2018-04-06 20:47:10 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("only superuser or a member of the pg_execute_server_program role may specify the program option of a file_fdw foreign table")));
|
|
|
|
|
2011-02-20 20:06:59 +01:00
|
|
|
filename = defGetString(def);
|
|
|
|
}
|
2014-05-06 18:12:18 +02:00
|
|
|
|
2014-03-04 23:31:59 +01: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-16 22:35:51 +02:00
|
|
|
else if (strcmp(def->defname, "force_not_null") == 0)
|
|
|
|
{
|
|
|
|
if (force_not_null)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
2014-03-04 23:31:59 +01:00
|
|
|
errmsg("conflicting or redundant options"),
|
2018-03-22 22:33:10 +01:00
|
|
|
errhint("Option \"force_not_null\" supplied more than once for a column.")));
|
2011-09-16 22:35:51 +02: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-04 23:31:59 +01: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"),
|
2018-03-22 22:33:10 +01:00
|
|
|
errhint("Option \"force_null\" supplied more than once for a column.")));
|
2014-03-04 23:31:59 +01:00
|
|
|
force_null = def;
|
|
|
|
(void) defGetBoolean(def);
|
|
|
|
}
|
2011-02-20 20:06:59 +01:00
|
|
|
else
|
|
|
|
other_options = lappend(other_options, def);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now apply the core COPY code's validation logic for more checks.
|
|
|
|
*/
|
2016-09-06 18:00:00 +02:00
|
|
|
ProcessCopyOptions(NULL, NULL, true, other_options);
|
2011-02-20 20:06:59 +01:00
|
|
|
|
2011-07-06 00:46:03 +02:00
|
|
|
/*
|
2016-09-29 19:32:27 +02:00
|
|
|
* Either filename or program option is required for file_fdw foreign
|
|
|
|
* tables.
|
2011-07-06 00:46:03 +02:00
|
|
|
*/
|
|
|
|
if (catalog == ForeignTableRelationId && filename == NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FDW_DYNAMIC_PARAMETER_VALUE_NEEDED),
|
2016-09-29 19:32:27 +02:00
|
|
|
errmsg("either filename or program is required for file_fdw foreign tables")));
|
2011-07-06 00:46:03 +02:00
|
|
|
|
2011-02-20 20:06:59 +01: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-09 18:48:48 +01:00
|
|
|
const struct FileFdwOption *opt;
|
2011-02-20 20:06:59 +01: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.
|
|
|
|
*
|
2016-09-29 19:32:27 +02:00
|
|
|
* We have to separate out filename/program from the other options because
|
|
|
|
* those must not appear in the options list passed to the core COPY code.
|
2011-02-20 20:06:59 +01:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fileGetOptions(Oid foreigntableid,
|
2016-09-29 19:32:27 +02:00
|
|
|
char **filename, bool *is_program, List **other_options)
|
2011-02-20 20:06:59 +01:00
|
|
|
{
|
|
|
|
ForeignTable *table;
|
|
|
|
ForeignServer *server;
|
|
|
|
ForeignDataWrapper *wrapper;
|
|
|
|
List *options;
|
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of
Lisp lists, which consist of chains of separately-allocated cons cells,
each having a value and a next-cell link. We'd hacked that once before
(commit d0b4399d8) to add a separate List header, but the data was still
in cons cells. That makes some operations -- notably list_nth() -- O(N),
and it's bulky because of the next-cell pointers and per-cell palloc
overhead, and it's very cache-unfriendly if the cons cells end up
scattered around rather than being adjacent.
In this rewrite, we still have List headers, but the data is in a
resizable array of values, with no next-cell links. Now we need at
most two palloc's per List, and often only one, since we can allocate
some values in the same palloc call as the List header. (Of course,
extending an existing List may require repalloc's to enlarge the array.
But this involves just O(log N) allocations not O(N).)
Of course this is not without downsides. The key difficulty is that
addition or deletion of a list entry may now cause other entries to
move, which it did not before.
For example, that breaks foreach() and sister macros, which historically
used a pointer to the current cons-cell as loop state. We can repair
those macros transparently by making their actual loop state be an
integer list index; the exposed "ListCell *" pointer is no longer state
carried across loop iterations, but is just a derived value. (In
practice, modern compilers can optimize things back to having just one
loop state value, at least for simple cases with inline loop bodies.)
In principle, this is a semantics change for cases where the loop body
inserts or deletes list entries ahead of the current loop index; but
I found no such cases in the Postgres code.
The change is not at all transparent for code that doesn't use foreach()
but chases lists "by hand" using lnext(). The largest share of such
code in the backend is in loops that were maintaining "prev" and "next"
variables in addition to the current-cell pointer, in order to delete
list cells efficiently using list_delete_cell(). However, we no longer
need a previous-cell pointer to delete a list cell efficiently. Keeping
a next-cell pointer doesn't work, as explained above, but we can improve
matters by changing such code to use a regular foreach() loop and then
using the new macro foreach_delete_current() to delete the current cell.
(This macro knows how to update the associated foreach loop's state so
that no cells will be missed in the traversal.)
There remains a nontrivial risk of code assuming that a ListCell *
pointer will remain good over an operation that could now move the list
contents. To help catch such errors, list.c can be compiled with a new
define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents
whenever that could possibly happen. This makes list operations
significantly more expensive so it's not normally turned on (though it
is on by default if USE_VALGRIND is on).
There are two notable API differences from the previous code:
* lnext() now requires the List's header pointer in addition to the
current cell's address.
* list_delete_cell() no longer requires a previous-cell argument.
These changes are somewhat unfortunate, but on the other hand code using
either function needs inspection to see if it is assuming anything
it shouldn't, so it's not all bad.
Programmers should be aware of these significant performance changes:
* list_nth() and related functions are now O(1); so there's no
major access-speed difference between a list and an array.
* Inserting or deleting a list element now takes time proportional to
the distance to the end of the list, due to moving the array elements.
(However, it typically *doesn't* require palloc or pfree, so except in
long lists it's probably still faster than before.) Notably, lcons()
used to be about the same cost as lappend(), but that's no longer true
if the list is long. Code that uses lcons() and list_delete_first()
to maintain a stack might usefully be rewritten to push and pop at the
end of the list rather than the beginning.
* There are now list_insert_nth...() and list_delete_nth...() functions
that add or remove a list cell identified by index. These have the
data-movement penalty explained above, but there's no search penalty.
* list_concat() and variants now copy the second list's data into
storage belonging to the first list, so there is no longer any
sharing of cells between the input lists. The second argument is
now declared "const List *" to reflect that it isn't changed.
This patch just does the minimum needed to get the new implementation
in place and fix bugs exposed by the regression tests. As suggested
by the foregoing, there's a fair amount of followup work remaining to
do.
Also, the ENABLE_LIST_COMPAT macros are finally removed in this
commit. Code using those should have been gone a dozen years ago.
Patch by me; thanks to David Rowley, Jesper Pedersen, and others
for review.
Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
2019-07-15 19:41:58 +02:00
|
|
|
ListCell *lc;
|
2011-02-20 20:06:59 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-16 22:35:51 +02:00
|
|
|
options = list_concat(options, get_file_fdw_attribute_options(foreigntableid));
|
2011-02-20 20:06:59 +01:00
|
|
|
|
|
|
|
/*
|
2016-09-29 19:32:27 +02:00
|
|
|
* Separate out the filename or program option (we assume there is only
|
|
|
|
* one).
|
2011-02-20 20:06:59 +01:00
|
|
|
*/
|
|
|
|
*filename = NULL;
|
2016-09-29 19:32:27 +02:00
|
|
|
*is_program = false;
|
2011-02-20 20:06:59 +01:00
|
|
|
foreach(lc, options)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
DefElem *def = (DefElem *) lfirst(lc);
|
2011-02-20 20:06:59 +01:00
|
|
|
|
|
|
|
if (strcmp(def->defname, "filename") == 0)
|
|
|
|
{
|
|
|
|
*filename = defGetString(def);
|
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of
Lisp lists, which consist of chains of separately-allocated cons cells,
each having a value and a next-cell link. We'd hacked that once before
(commit d0b4399d8) to add a separate List header, but the data was still
in cons cells. That makes some operations -- notably list_nth() -- O(N),
and it's bulky because of the next-cell pointers and per-cell palloc
overhead, and it's very cache-unfriendly if the cons cells end up
scattered around rather than being adjacent.
In this rewrite, we still have List headers, but the data is in a
resizable array of values, with no next-cell links. Now we need at
most two palloc's per List, and often only one, since we can allocate
some values in the same palloc call as the List header. (Of course,
extending an existing List may require repalloc's to enlarge the array.
But this involves just O(log N) allocations not O(N).)
Of course this is not without downsides. The key difficulty is that
addition or deletion of a list entry may now cause other entries to
move, which it did not before.
For example, that breaks foreach() and sister macros, which historically
used a pointer to the current cons-cell as loop state. We can repair
those macros transparently by making their actual loop state be an
integer list index; the exposed "ListCell *" pointer is no longer state
carried across loop iterations, but is just a derived value. (In
practice, modern compilers can optimize things back to having just one
loop state value, at least for simple cases with inline loop bodies.)
In principle, this is a semantics change for cases where the loop body
inserts or deletes list entries ahead of the current loop index; but
I found no such cases in the Postgres code.
The change is not at all transparent for code that doesn't use foreach()
but chases lists "by hand" using lnext(). The largest share of such
code in the backend is in loops that were maintaining "prev" and "next"
variables in addition to the current-cell pointer, in order to delete
list cells efficiently using list_delete_cell(). However, we no longer
need a previous-cell pointer to delete a list cell efficiently. Keeping
a next-cell pointer doesn't work, as explained above, but we can improve
matters by changing such code to use a regular foreach() loop and then
using the new macro foreach_delete_current() to delete the current cell.
(This macro knows how to update the associated foreach loop's state so
that no cells will be missed in the traversal.)
There remains a nontrivial risk of code assuming that a ListCell *
pointer will remain good over an operation that could now move the list
contents. To help catch such errors, list.c can be compiled with a new
define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents
whenever that could possibly happen. This makes list operations
significantly more expensive so it's not normally turned on (though it
is on by default if USE_VALGRIND is on).
There are two notable API differences from the previous code:
* lnext() now requires the List's header pointer in addition to the
current cell's address.
* list_delete_cell() no longer requires a previous-cell argument.
These changes are somewhat unfortunate, but on the other hand code using
either function needs inspection to see if it is assuming anything
it shouldn't, so it's not all bad.
Programmers should be aware of these significant performance changes:
* list_nth() and related functions are now O(1); so there's no
major access-speed difference between a list and an array.
* Inserting or deleting a list element now takes time proportional to
the distance to the end of the list, due to moving the array elements.
(However, it typically *doesn't* require palloc or pfree, so except in
long lists it's probably still faster than before.) Notably, lcons()
used to be about the same cost as lappend(), but that's no longer true
if the list is long. Code that uses lcons() and list_delete_first()
to maintain a stack might usefully be rewritten to push and pop at the
end of the list rather than the beginning.
* There are now list_insert_nth...() and list_delete_nth...() functions
that add or remove a list cell identified by index. These have the
data-movement penalty explained above, but there's no search penalty.
* list_concat() and variants now copy the second list's data into
storage belonging to the first list, so there is no longer any
sharing of cells between the input lists. The second argument is
now declared "const List *" to reflect that it isn't changed.
This patch just does the minimum needed to get the new implementation
in place and fix bugs exposed by the regression tests. As suggested
by the foregoing, there's a fair amount of followup work remaining to
do.
Also, the ENABLE_LIST_COMPAT macros are finally removed in this
commit. Code using those should have been gone a dozen years ago.
Patch by me; thanks to David Rowley, Jesper Pedersen, and others
for review.
Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
2019-07-15 19:41:58 +02:00
|
|
|
options = foreach_delete_current(options, lc);
|
2011-02-20 20:06:59 +01:00
|
|
|
break;
|
|
|
|
}
|
2016-09-29 19:32:27 +02:00
|
|
|
else if (strcmp(def->defname, "program") == 0)
|
|
|
|
{
|
|
|
|
*filename = defGetString(def);
|
|
|
|
*is_program = true;
|
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of
Lisp lists, which consist of chains of separately-allocated cons cells,
each having a value and a next-cell link. We'd hacked that once before
(commit d0b4399d8) to add a separate List header, but the data was still
in cons cells. That makes some operations -- notably list_nth() -- O(N),
and it's bulky because of the next-cell pointers and per-cell palloc
overhead, and it's very cache-unfriendly if the cons cells end up
scattered around rather than being adjacent.
In this rewrite, we still have List headers, but the data is in a
resizable array of values, with no next-cell links. Now we need at
most two palloc's per List, and often only one, since we can allocate
some values in the same palloc call as the List header. (Of course,
extending an existing List may require repalloc's to enlarge the array.
But this involves just O(log N) allocations not O(N).)
Of course this is not without downsides. The key difficulty is that
addition or deletion of a list entry may now cause other entries to
move, which it did not before.
For example, that breaks foreach() and sister macros, which historically
used a pointer to the current cons-cell as loop state. We can repair
those macros transparently by making their actual loop state be an
integer list index; the exposed "ListCell *" pointer is no longer state
carried across loop iterations, but is just a derived value. (In
practice, modern compilers can optimize things back to having just one
loop state value, at least for simple cases with inline loop bodies.)
In principle, this is a semantics change for cases where the loop body
inserts or deletes list entries ahead of the current loop index; but
I found no such cases in the Postgres code.
The change is not at all transparent for code that doesn't use foreach()
but chases lists "by hand" using lnext(). The largest share of such
code in the backend is in loops that were maintaining "prev" and "next"
variables in addition to the current-cell pointer, in order to delete
list cells efficiently using list_delete_cell(). However, we no longer
need a previous-cell pointer to delete a list cell efficiently. Keeping
a next-cell pointer doesn't work, as explained above, but we can improve
matters by changing such code to use a regular foreach() loop and then
using the new macro foreach_delete_current() to delete the current cell.
(This macro knows how to update the associated foreach loop's state so
that no cells will be missed in the traversal.)
There remains a nontrivial risk of code assuming that a ListCell *
pointer will remain good over an operation that could now move the list
contents. To help catch such errors, list.c can be compiled with a new
define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents
whenever that could possibly happen. This makes list operations
significantly more expensive so it's not normally turned on (though it
is on by default if USE_VALGRIND is on).
There are two notable API differences from the previous code:
* lnext() now requires the List's header pointer in addition to the
current cell's address.
* list_delete_cell() no longer requires a previous-cell argument.
These changes are somewhat unfortunate, but on the other hand code using
either function needs inspection to see if it is assuming anything
it shouldn't, so it's not all bad.
Programmers should be aware of these significant performance changes:
* list_nth() and related functions are now O(1); so there's no
major access-speed difference between a list and an array.
* Inserting or deleting a list element now takes time proportional to
the distance to the end of the list, due to moving the array elements.
(However, it typically *doesn't* require palloc or pfree, so except in
long lists it's probably still faster than before.) Notably, lcons()
used to be about the same cost as lappend(), but that's no longer true
if the list is long. Code that uses lcons() and list_delete_first()
to maintain a stack might usefully be rewritten to push and pop at the
end of the list rather than the beginning.
* There are now list_insert_nth...() and list_delete_nth...() functions
that add or remove a list cell identified by index. These have the
data-movement penalty explained above, but there's no search penalty.
* list_concat() and variants now copy the second list's data into
storage belonging to the first list, so there is no longer any
sharing of cells between the input lists. The second argument is
now declared "const List *" to reflect that it isn't changed.
This patch just does the minimum needed to get the new implementation
in place and fix bugs exposed by the regression tests. As suggested
by the foregoing, there's a fair amount of followup work remaining to
do.
Also, the ENABLE_LIST_COMPAT macros are finally removed in this
commit. Code using those should have been gone a dozen years ago.
Patch by me; thanks to David Rowley, Jesper Pedersen, and others
for review.
Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
2019-07-15 19:41:58 +02:00
|
|
|
options = foreach_delete_current(options, lc);
|
2016-09-29 19:32:27 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-02-20 20:06:59 +01:00
|
|
|
}
|
2011-07-06 00:46:03 +02:00
|
|
|
|
|
|
|
/*
|
2016-09-29 19:32:27 +02:00
|
|
|
* The validator should have checked that filename or program was included
|
|
|
|
* in the options, but check again, just in case.
|
2011-07-06 00:46:03 +02:00
|
|
|
*/
|
2011-02-20 20:06:59 +01:00
|
|
|
if (*filename == NULL)
|
2016-09-29 19:32:27 +02:00
|
|
|
elog(ERROR, "either filename or program is required for file_fdw foreign tables");
|
2011-07-06 00:46:03 +02:00
|
|
|
|
2011-02-20 20:06:59 +01:00
|
|
|
*other_options = options;
|
|
|
|
}
|
|
|
|
|
2011-09-16 22:35:51 +02:00
|
|
|
/*
|
|
|
|
* Retrieve per-column generic options from pg_attribute and construct a list
|
|
|
|
* of DefElems representing them.
|
|
|
|
*
|
2014-03-04 23:31:59 +01: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-16 22:35:51 +02:00
|
|
|
*/
|
|
|
|
static List *
|
|
|
|
get_file_fdw_attribute_options(Oid relid)
|
|
|
|
{
|
|
|
|
Relation rel;
|
|
|
|
TupleDesc tupleDesc;
|
|
|
|
AttrNumber natts;
|
|
|
|
AttrNumber attnum;
|
|
|
|
List *fnncolumns = NIL;
|
2014-03-04 23:31:59 +01:00
|
|
|
List *fncolumns = NIL;
|
|
|
|
|
2014-05-06 18:12:18 +02:00
|
|
|
List *options = NIL;
|
2011-09-16 22:35:51 +02:00
|
|
|
|
2019-01-21 19:32:19 +01:00
|
|
|
rel = table_open(relid, AccessShareLock);
|
2011-09-16 22:35:51 +02:00
|
|
|
tupleDesc = RelationGetDescr(rel);
|
|
|
|
natts = tupleDesc->natts;
|
|
|
|
|
|
|
|
/* Retrieve FDW options for all user-defined attributes. */
|
|
|
|
for (attnum = 1; attnum <= natts; attnum++)
|
|
|
|
{
|
2017-08-20 20:19:07 +02:00
|
|
|
Form_pg_attribute attr = TupleDescAttr(tupleDesc, attnum - 1);
|
2012-03-08 00:20:58 +01:00
|
|
|
List *options;
|
|
|
|
ListCell *lc;
|
2011-09-16 22:35:51 +02:00
|
|
|
|
|
|
|
/* Skip dropped attributes. */
|
2012-03-08 00:20:58 +01:00
|
|
|
if (attr->attisdropped)
|
2011-09-16 22:35:51 +02:00
|
|
|
continue;
|
|
|
|
|
2012-03-08 00:20:58 +01:00
|
|
|
options = GetForeignColumnOptions(relid, attnum);
|
|
|
|
foreach(lc, options)
|
2011-09-16 22:35:51 +02:00
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
DefElem *def = (DefElem *) lfirst(lc);
|
2011-09-16 22:35:51 +02:00
|
|
|
|
2012-03-08 00:20:58 +01:00
|
|
|
if (strcmp(def->defname, "force_not_null") == 0)
|
2011-09-16 22:35:51 +02:00
|
|
|
{
|
2012-03-08 00:20:58 +01:00
|
|
|
if (defGetBoolean(def))
|
2011-09-16 22:35:51 +02:00
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
char *attname = pstrdup(NameStr(attr->attname));
|
2011-09-16 22:35:51 +02:00
|
|
|
|
2012-03-08 00:20:58 +01:00
|
|
|
fnncolumns = lappend(fnncolumns, makeString(attname));
|
2011-09-16 22:35:51 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-04 23:31:59 +01: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 00:20:58 +01:00
|
|
|
/* maybe in future handle other options here */
|
2011-09-16 22:35:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 19:32:19 +01:00
|
|
|
table_close(rel, AccessShareLock);
|
2011-09-16 22:35:51 +02:00
|
|
|
|
2014-05-06 18:12:18 +02:00
|
|
|
/*
|
|
|
|
* Return DefElem only when some column(s) have force_not_null /
|
|
|
|
* force_null options set
|
|
|
|
*/
|
2011-09-16 22:35:51 +02:00
|
|
|
if (fnncolumns != NIL)
|
2016-09-06 18:00:00 +02:00
|
|
|
options = lappend(options, makeDefElem("force_not_null", (Node *) fnncolumns, -1));
|
2014-03-04 23:31:59 +01:00
|
|
|
|
|
|
|
if (fncolumns != NIL)
|
2016-09-06 18:00:00 +02:00
|
|
|
options = lappend(options, makeDefElem("force_null", (Node *) fncolumns, -1));
|
2014-03-04 23:31:59 +01:00
|
|
|
|
|
|
|
return options;
|
2011-09-16 22:35:51 +02:00
|
|
|
}
|
|
|
|
|
2011-02-20 20:06:59 +01: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-09 18:48:48 +01:00
|
|
|
* fileGetForeignRelSize
|
|
|
|
* Obtain relation size estimates for a foreign table
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fileGetForeignRelSize(PlannerInfo *root,
|
|
|
|
RelOptInfo *baserel,
|
|
|
|
Oid foreigntableid)
|
|
|
|
{
|
|
|
|
FileFdwPlanState *fdw_private;
|
|
|
|
|
|
|
|
/*
|
2016-09-29 19:32:27 +02:00
|
|
|
* Fetch options. We only need filename (or program) 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-09 18:48:48 +01:00
|
|
|
*/
|
|
|
|
fdw_private = (FileFdwPlanState *) palloc(sizeof(FileFdwPlanState));
|
|
|
|
fileGetOptions(foreigntableid,
|
2016-09-29 19:32:27 +02:00
|
|
|
&fdw_private->filename,
|
|
|
|
&fdw_private->is_program,
|
|
|
|
&fdw_private->options);
|
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-09 18:48:48 +01:00
|
|
|
baserel->fdw_private = (void *) fdw_private;
|
|
|
|
|
|
|
|
/* Estimate relation size */
|
|
|
|
estimate_size(root, baserel, fdw_private);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fileGetForeignPaths
|
2012-03-05 22:15:59 +01: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-20 20:06:59 +01:00
|
|
|
*/
|
2012-03-05 22:15:59 +01: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-09 18:48:48 +01:00
|
|
|
fileGetForeignPaths(PlannerInfo *root,
|
|
|
|
RelOptInfo *baserel,
|
|
|
|
Oid foreigntableid)
|
2011-02-20 20:06:59 +01: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-09 18:48:48 +01:00
|
|
|
FileFdwPlanState *fdw_private = (FileFdwPlanState *) baserel->fdw_private;
|
2012-03-05 22:15:59 +01:00
|
|
|
Cost startup_cost;
|
|
|
|
Cost total_cost;
|
2012-07-12 22:26:59 +02: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",
|
2016-09-06 18:00:00 +02:00
|
|
|
(Node *) columns, -1));
|
2011-02-20 20:06:59 +01: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-09 18:48:48 +01:00
|
|
|
/* Estimate costs */
|
|
|
|
estimate_costs(root, baserel, fdw_private,
|
2012-03-05 22:15:59 +01:00
|
|
|
&startup_cost, &total_cost);
|
|
|
|
|
2012-07-12 22:26:59 +02:00
|
|
|
/*
|
2014-05-06 18:12:18 +02:00
|
|
|
* Create a ForeignPath node and add it as only possible path. We use the
|
2012-07-12 22:26:59 +02: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.
|
Split create_foreignscan_path() into three functions.
Up to now postgres_fdw has been using create_foreignscan_path() to
generate not only base-relation paths, but also paths for foreign joins
and foreign upperrels. This is wrong, because create_foreignscan_path()
calls get_baserel_parampathinfo() which will only do the right thing for
baserels. It accidentally fails to fail for unparameterized paths, which
are the only ones postgres_fdw (thought it) was handling, but we really
need different APIs for the baserel and join cases.
In HEAD, the best thing to do seems to be to split up the baserel,
joinrel, and upperrel cases into three functions so that they can
have different APIs. I haven't actually given create_foreign_join_path
a different API in this commit: we should spend a bit of time thinking
about just what we want to do there, since perhaps FDWs would want to
do something different from the build-up-a-join-pairwise approach that
get_joinrel_parampathinfo expects. In the meantime, since postgres_fdw
isn't prepared to generate parameterized joins anyway, just give it a
defense against trying to plan joins with lateral refs.
In addition (and this is what triggered this whole mess) fix bug #15613
from Srinivasan S A, by teaching file_fdw and postgres_fdw that plain
baserel foreign paths still have outer refs if the relation has
lateral_relids. Add some assertions in relnode.c to catch future
occurrences of the same error --- in particular, to catch other FDWs
doing that, but also as backstop against core-code mistakes like the
one fixed by commit bdd9a99aa.
Bug #15613 also needs to be fixed in the back branches, but the
appropriate fix will look quite a bit different there, since we don't
want to assume that existing FDWs get the word right away.
Discussion: https://postgr.es/m/15613-092be1be9576c728@postgresql.org
2019-02-07 18:59:47 +01:00
|
|
|
*
|
|
|
|
* We don't support pushing join clauses into the quals of this path, but
|
|
|
|
* it could still have required parameterization due to LATERAL refs in
|
|
|
|
* its tlist.
|
2012-07-12 22:26:59 +02:00
|
|
|
*/
|
2012-03-05 22:15:59 +01:00
|
|
|
add_path(baserel, (Path *)
|
|
|
|
create_foreignscan_path(root, baserel,
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
NULL, /* default pathtarget */
|
2012-03-05 22:15:59 +01:00
|
|
|
baserel->rows,
|
|
|
|
startup_cost,
|
|
|
|
total_cost,
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
NIL, /* no pathkeys */
|
Split create_foreignscan_path() into three functions.
Up to now postgres_fdw has been using create_foreignscan_path() to
generate not only base-relation paths, but also paths for foreign joins
and foreign upperrels. This is wrong, because create_foreignscan_path()
calls get_baserel_parampathinfo() which will only do the right thing for
baserels. It accidentally fails to fail for unparameterized paths, which
are the only ones postgres_fdw (thought it) was handling, but we really
need different APIs for the baserel and join cases.
In HEAD, the best thing to do seems to be to split up the baserel,
joinrel, and upperrel cases into three functions so that they can
have different APIs. I haven't actually given create_foreign_join_path
a different API in this commit: we should spend a bit of time thinking
about just what we want to do there, since perhaps FDWs would want to
do something different from the build-up-a-join-pairwise approach that
get_joinrel_parampathinfo expects. In the meantime, since postgres_fdw
isn't prepared to generate parameterized joins anyway, just give it a
defense against trying to plan joins with lateral refs.
In addition (and this is what triggered this whole mess) fix bug #15613
from Srinivasan S A, by teaching file_fdw and postgres_fdw that plain
baserel foreign paths still have outer refs if the relation has
lateral_relids. Add some assertions in relnode.c to catch future
occurrences of the same error --- in particular, to catch other FDWs
doing that, but also as backstop against core-code mistakes like the
one fixed by commit bdd9a99aa.
Bug #15613 also needs to be fixed in the back branches, but the
appropriate fix will look quite a bit different there, since we don't
want to assume that existing FDWs get the word right away.
Discussion: https://postgr.es/m/15613-092be1be9576c728@postgresql.org
2019-02-07 18:59:47 +01:00
|
|
|
baserel->lateral_relids,
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
NULL, /* no extra plan */
|
2012-07-12 22:26:59 +02:00
|
|
|
coptions));
|
2011-02-20 20:06:59 +01:00
|
|
|
|
2012-03-05 22:15:59 +01:00
|
|
|
/*
|
|
|
|
* If data file was sorted, and we knew it somehow, we could insert
|
2012-06-10 21:20:04 +02:00
|
|
|
* appropriate pathkeys into the ForeignPath node to tell the planner
|
|
|
|
* that.
|
2012-03-05 22:15:59 +01:00
|
|
|
*/
|
2011-02-20 20:06:59 +01: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-09 18:48:48 +01: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,
|
Allow foreign and custom joins to handle EvalPlanQual rechecks.
Commit e7cb7ee14555cc9c5773e2c102efd6371f6f2005 provided basic
infrastructure for allowing a foreign data wrapper or custom scan
provider to replace a join of one or more tables with a scan.
However, this infrastructure failed to take into account the need
for possible EvalPlanQual rechecks, and ExecScanFetch would fail
an assertion (or just overwrite memory) if such a check was attempted
for a plan containing a pushed-down join. To fix, adjust the EPQ
machinery to skip some processing steps when scanrelid == 0, making
those the responsibility of scan's recheck method, which also has
the responsibility in this case of correctly populating the relevant
slot.
To allow foreign scans to gain control in the right place to make
use of this new facility, add a new, optional RecheckForeignScan
method. Also, allow a foreign scan to have a child plan, which can
be used to correctly populate the slot (or perhaps for something
else, but this is the only use currently envisioned).
KaiGai Kohei, reviewed by Robert Haas, Etsuro Fujita, and Kyotaro
Horiguchi.
2015-12-08 18:31:03 +01:00
|
|
|
List *scan_clauses,
|
|
|
|
Plan *outer_plan)
|
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-09 18:48:48 +01:00
|
|
|
{
|
|
|
|
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-10 21:20:04 +02:00
|
|
|
NIL, /* no expressions to evaluate */
|
Code review for foreign/custom join pushdown patch.
Commit e7cb7ee14555cc9c5773e2c102efd6371f6f2005 included some design
decisions that seem pretty questionable to me, and there was quite a lot
of stuff not to like about the documentation and comments. Clean up
as follows:
* Consider foreign joins only between foreign tables on the same server,
rather than between any two foreign tables with the same underlying FDW
handler function. In most if not all cases, the FDW would simply have had
to apply the same-server restriction itself (far more expensively, both for
lack of caching and because it would be repeated for each combination of
input sub-joins), or else risk nasty bugs. Anyone who's really intent on
doing something outside this restriction can always use the
set_join_pathlist_hook.
* Rename fdw_ps_tlist/custom_ps_tlist to fdw_scan_tlist/custom_scan_tlist
to better reflect what they're for, and allow these custom scan tlists
to be used even for base relations.
* Change make_foreignscan() API to include passing the fdw_scan_tlist
value, since the FDW is required to set that. Backwards compatibility
doesn't seem like an adequate reason to expect FDWs to set it in some
ad-hoc extra step, and anyway existing FDWs can just pass NIL.
* Change the API of path-generating subroutines of add_paths_to_joinrel,
and in particular that of GetForeignJoinPaths and set_join_pathlist_hook,
so that various less-used parameters are passed in a struct rather than
as separate parameter-list entries. The objective here is to reduce the
probability that future additions to those parameter lists will result in
source-level API breaks for users of these hooks. It's possible that this
is even a small win for the core code, since most CPU architectures can't
pass more than half a dozen parameters efficiently anyway. I kept root,
joinrel, outerrel, innerrel, and jointype as separate parameters to reduce
code churn in joinpath.c --- in particular, putting jointype into the
struct would have been problematic because of the subroutines' habit of
changing their local copies of that variable.
* Avoid ad-hocery in ExecAssignScanProjectionInfo. It was probably all
right for it to know about IndexOnlyScan, but if the list is to grow
we should refactor the knowledge out to the callers.
* Restore nodeForeignscan.c's previous use of the relcache to avoid
extra GetFdwRoutine lookups for base-relation scans.
* Lots of cleanup of documentation and missed comments. Re-order some
code additions into more logical places.
2015-05-10 20:36:30 +02:00
|
|
|
best_path->fdw_private,
|
2015-10-15 19:00:40 +02:00
|
|
|
NIL, /* no custom tlist */
|
Allow foreign and custom joins to handle EvalPlanQual rechecks.
Commit e7cb7ee14555cc9c5773e2c102efd6371f6f2005 provided basic
infrastructure for allowing a foreign data wrapper or custom scan
provider to replace a join of one or more tables with a scan.
However, this infrastructure failed to take into account the need
for possible EvalPlanQual rechecks, and ExecScanFetch would fail
an assertion (or just overwrite memory) if such a check was attempted
for a plan containing a pushed-down join. To fix, adjust the EPQ
machinery to skip some processing steps when scanrelid == 0, making
those the responsibility of scan's recheck method, which also has
the responsibility in this case of correctly populating the relevant
slot.
To allow foreign scans to gain control in the right place to make
use of this new facility, add a new, optional RecheckForeignScan
method. Also, allow a foreign scan to have a child plan, which can
be used to correctly populate the slot (or perhaps for something
else, but this is the only use currently envisioned).
KaiGai Kohei, reviewed by Robert Haas, Etsuro Fujita, and Kyotaro
Horiguchi.
2015-12-08 18:31:03 +01:00
|
|
|
NIL, /* no remote quals */
|
|
|
|
outer_plan);
|
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-09 18:48:48 +01:00
|
|
|
}
|
|
|
|
|
2011-02-20 20:06:59 +01:00
|
|
|
/*
|
|
|
|
* fileExplainForeignScan
|
|
|
|
* Produce extra output for EXPLAIN
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fileExplainForeignScan(ForeignScanState *node, ExplainState *es)
|
|
|
|
{
|
|
|
|
char *filename;
|
2016-09-29 19:32:27 +02:00
|
|
|
bool is_program;
|
2011-02-20 20:06:59 +01:00
|
|
|
List *options;
|
|
|
|
|
2016-09-29 19:32:27 +02:00
|
|
|
/* Fetch options --- we only need filename and is_program at this point */
|
2011-02-20 20:06:59 +01:00
|
|
|
fileGetOptions(RelationGetRelid(node->ss.ss_currentRelation),
|
2016-09-29 19:32:27 +02:00
|
|
|
&filename, &is_program, &options);
|
2011-02-20 20:06:59 +01:00
|
|
|
|
2016-09-29 19:32:27 +02:00
|
|
|
if (is_program)
|
|
|
|
ExplainPropertyText("Foreign Program", filename, es);
|
|
|
|
else
|
|
|
|
ExplainPropertyText("Foreign File", filename, es);
|
2011-02-20 20:06:59 +01:00
|
|
|
|
|
|
|
/* Suppress file size if we're not showing cost details */
|
|
|
|
if (es->costs)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
struct stat stat_buf;
|
2011-02-20 20:06:59 +01:00
|
|
|
|
2016-09-29 19:32:27 +02:00
|
|
|
if (!is_program &&
|
|
|
|
stat(filename, &stat_buf) == 0)
|
2018-03-17 07:13:12 +01:00
|
|
|
ExplainPropertyInteger("Foreign File Size", "b",
|
2018-03-17 07:13:12 +01:00
|
|
|
(int64) stat_buf.st_size, es);
|
2011-02-20 20:06:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fileBeginForeignScan
|
|
|
|
* Initiate access to the file by creating CopyState
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fileBeginForeignScan(ForeignScanState *node, int eflags)
|
|
|
|
{
|
2012-07-12 22:26:59 +02:00
|
|
|
ForeignScan *plan = (ForeignScan *) node->ss.ps.plan;
|
2011-02-20 20:06:59 +01:00
|
|
|
char *filename;
|
2016-09-29 19:32:27 +02:00
|
|
|
bool is_program;
|
2011-02-20 20:06:59 +01:00
|
|
|
List *options;
|
Split copy.c into four files.
Copy.c has grown really large. Split it into more manageable parts:
- copy.c now contains only a few functions that are common to COPY FROM
and COPY TO.
- copyto.c contains code for COPY TO.
- copyfrom.c contains code for initializing COPY FROM, and inserting the
tuples to the correct table.
- copyfromparse.c contains code for reading from the client/file/program,
and parsing the input text/CSV/binary format into tuples.
All of these parts are fairly complicated, and fairly independent of each
other. There is a patch being discussed to implement parallel COPY FROM,
which will add a lot of new code to the COPY FROM path, and another patch
which would allow INSERTs to use the same multi-insert machinery as COPY
FROM, both of which will require refactoring that code. With those two
patches, there's going to be a lot of code churn in copy.c anyway, so now
seems like a good time to do this refactoring.
The CopyStateData struct is also split. All the formatting options, like
FORMAT, QUOTE, ESCAPE, are put in a new CopyFormatOption struct, which
is used by both COPY FROM and TO. Other state data are kept in separate
CopyFromStateData and CopyToStateData structs.
Reviewed-by: Soumyadeep Chakraborty, Erik Rijkers, Vignesh C, Andres Freund
Discussion: https://www.postgresql.org/message-id/8e15b560-f387-7acc-ac90-763986617bfb%40iki.fi
2020-11-23 09:50:50 +01:00
|
|
|
CopyFromState cstate;
|
2011-02-20 20:06:59 +01:00
|
|
|
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),
|
2016-09-29 19:32:27 +02:00
|
|
|
&filename, &is_program, &options);
|
2011-02-20 20:06:59 +01:00
|
|
|
|
2012-07-12 22:26:59 +02:00
|
|
|
/* Add any options from the plan (currently only convert_selectively) */
|
|
|
|
options = list_concat(options, plan->fdw_private);
|
|
|
|
|
2011-02-20 20:06:59 +01:00
|
|
|
/*
|
2011-04-10 17:42:00 +02:00
|
|
|
* Create CopyState from FDW options. We always acquire all columns, so
|
|
|
|
* as to match the expected ScanTupleSlot signature.
|
2011-02-20 20:06:59 +01:00
|
|
|
*/
|
2016-09-06 18:00:00 +02:00
|
|
|
cstate = BeginCopyFrom(NULL,
|
|
|
|
node->ss.ss_currentRelation,
|
Split copy.c into four files.
Copy.c has grown really large. Split it into more manageable parts:
- copy.c now contains only a few functions that are common to COPY FROM
and COPY TO.
- copyto.c contains code for COPY TO.
- copyfrom.c contains code for initializing COPY FROM, and inserting the
tuples to the correct table.
- copyfromparse.c contains code for reading from the client/file/program,
and parsing the input text/CSV/binary format into tuples.
All of these parts are fairly complicated, and fairly independent of each
other. There is a patch being discussed to implement parallel COPY FROM,
which will add a lot of new code to the COPY FROM path, and another patch
which would allow INSERTs to use the same multi-insert machinery as COPY
FROM, both of which will require refactoring that code. With those two
patches, there's going to be a lot of code churn in copy.c anyway, so now
seems like a good time to do this refactoring.
The CopyStateData struct is also split. All the formatting options, like
FORMAT, QUOTE, ESCAPE, are put in a new CopyFormatOption struct, which
is used by both COPY FROM and TO. Other state data are kept in separate
CopyFromStateData and CopyToStateData structs.
Reviewed-by: Soumyadeep Chakraborty, Erik Rijkers, Vignesh C, Andres Freund
Discussion: https://www.postgresql.org/message-id/8e15b560-f387-7acc-ac90-763986617bfb%40iki.fi
2020-11-23 09:50:50 +01:00
|
|
|
NULL,
|
2011-02-20 20:06:59 +01:00
|
|
|
filename,
|
2016-09-29 19:32:27 +02:00
|
|
|
is_program,
|
2017-03-23 13:36:36 +01:00
|
|
|
NULL,
|
2011-02-20 20:06:59 +01: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;
|
2016-09-29 19:32:27 +02:00
|
|
|
festate->is_program = is_program;
|
2011-02-20 20:06:59 +01:00
|
|
|
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 17:42:00 +02:00
|
|
|
bool found;
|
2012-11-12 17:30:51 +01:00
|
|
|
ErrorContextCallback errcallback;
|
2011-02-20 20:06:59 +01:00
|
|
|
|
|
|
|
/* Set up callback to identify error line number. */
|
2012-11-12 17:30:51 +01:00
|
|
|
errcallback.callback = CopyFromErrorCallback;
|
|
|
|
errcallback.arg = (void *) festate->cstate;
|
|
|
|
errcallback.previous = error_context_stack;
|
|
|
|
error_context_stack = &errcallback;
|
2011-02-20 20:06:59 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The protocol for loading a virtual tuple into a slot is first
|
|
|
|
* ExecClearTuple, then fill the values/isnull arrays, then
|
2011-04-10 17:42:00 +02: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-20 20:06:59 +01:00
|
|
|
*
|
|
|
|
* We can pass ExprContext = NULL because we read all columns from the
|
|
|
|
* file, so no need to evaluate default expressions.
|
|
|
|
*/
|
|
|
|
ExecClearTuple(slot);
|
|
|
|
found = NextCopyFrom(festate->cstate, NULL,
|
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
|
|
|
slot->tts_values, slot->tts_isnull);
|
2011-02-20 20:06:59 +01:00
|
|
|
if (found)
|
|
|
|
ExecStoreVirtualTuple(slot);
|
|
|
|
|
|
|
|
/* Remove error callback. */
|
2012-11-12 17:30:51 +01:00
|
|
|
error_context_stack = errcallback.previous;
|
2011-02-20 20:06:59 +01:00
|
|
|
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
2012-04-06 21:02:35 +02:00
|
|
|
/*
|
|
|
|
* fileReScanForeignScan
|
|
|
|
* Rescan table, possibly with new parameters
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fileReScanForeignScan(ForeignScanState *node)
|
|
|
|
{
|
|
|
|
FileFdwExecutionState *festate = (FileFdwExecutionState *) node->fdw_state;
|
|
|
|
|
|
|
|
EndCopyFrom(festate->cstate);
|
|
|
|
|
2016-09-06 18:00:00 +02:00
|
|
|
festate->cstate = BeginCopyFrom(NULL,
|
|
|
|
node->ss.ss_currentRelation,
|
Split copy.c into four files.
Copy.c has grown really large. Split it into more manageable parts:
- copy.c now contains only a few functions that are common to COPY FROM
and COPY TO.
- copyto.c contains code for COPY TO.
- copyfrom.c contains code for initializing COPY FROM, and inserting the
tuples to the correct table.
- copyfromparse.c contains code for reading from the client/file/program,
and parsing the input text/CSV/binary format into tuples.
All of these parts are fairly complicated, and fairly independent of each
other. There is a patch being discussed to implement parallel COPY FROM,
which will add a lot of new code to the COPY FROM path, and another patch
which would allow INSERTs to use the same multi-insert machinery as COPY
FROM, both of which will require refactoring that code. With those two
patches, there's going to be a lot of code churn in copy.c anyway, so now
seems like a good time to do this refactoring.
The CopyStateData struct is also split. All the formatting options, like
FORMAT, QUOTE, ESCAPE, are put in a new CopyFormatOption struct, which
is used by both COPY FROM and TO. Other state data are kept in separate
CopyFromStateData and CopyToStateData structs.
Reviewed-by: Soumyadeep Chakraborty, Erik Rijkers, Vignesh C, Andres Freund
Discussion: https://www.postgresql.org/message-id/8e15b560-f387-7acc-ac90-763986617bfb%40iki.fi
2020-11-23 09:50:50 +01:00
|
|
|
NULL,
|
2012-04-06 21:02:35 +02:00
|
|
|
festate->filename,
|
2016-09-29 19:32:27 +02:00
|
|
|
festate->is_program,
|
2017-03-23 13:36:36 +01:00
|
|
|
NULL,
|
2012-04-06 21:02:35 +02:00
|
|
|
NIL,
|
|
|
|
festate->options);
|
|
|
|
}
|
|
|
|
|
2011-02-20 20:06:59 +01: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-06 21:02:35 +02:00
|
|
|
* fileAnalyzeForeignTable
|
|
|
|
* Test whether analyzing this foreign table is supported
|
2011-02-20 20:06:59 +01:00
|
|
|
*/
|
2012-04-06 22:04:10 +02:00
|
|
|
static bool
|
|
|
|
fileAnalyzeForeignTable(Relation relation,
|
|
|
|
AcquireSampleRowsFunc *func,
|
|
|
|
BlockNumber *totalpages)
|
2011-02-20 20:06:59 +01:00
|
|
|
{
|
2012-04-06 22:04:10 +02:00
|
|
|
char *filename;
|
2016-09-29 19:32:27 +02:00
|
|
|
bool is_program;
|
2012-04-06 22:04:10 +02:00
|
|
|
List *options;
|
2012-06-10 21:20:04 +02:00
|
|
|
struct stat stat_buf;
|
2012-04-06 22:04:10 +02:00
|
|
|
|
|
|
|
/* Fetch options of foreign table */
|
2016-09-29 19:32:27 +02:00
|
|
|
fileGetOptions(RelationGetRelid(relation), &filename, &is_program, &options);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is a program instead of a file, just return false to skip
|
|
|
|
* analyzing the table. We could run the program and collect stats on
|
|
|
|
* whatever it currently returns, but it seems likely that in such cases
|
|
|
|
* the output would be too volatile for the stats to be useful. Maybe
|
|
|
|
* there should be an option to enable doing this?
|
|
|
|
*/
|
|
|
|
if (is_program)
|
|
|
|
return false;
|
2012-04-06 22:04:10 +02:00
|
|
|
|
|
|
|
/*
|
2012-06-10 21:20:04 +02: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-06 22:04:10 +02: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-20 20:06:59 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 11:44:46 +01:00
|
|
|
/*
|
|
|
|
* fileIsForeignScanParallelSafe
|
2016-09-29 19:32:27 +02:00
|
|
|
* Reading a file, or external program, in a parallel worker should work
|
|
|
|
* just the same as reading it in the leader, so mark scans safe.
|
2016-02-26 11:44:46 +01:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
fileIsForeignScanParallelSafe(PlannerInfo *root, RelOptInfo *rel,
|
2016-06-10 00:02:36 +02:00
|
|
|
RangeTblEntry *rte)
|
2016-02-26 11:44:46 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-12 22:26:59 +02: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,
|
2017-08-16 06:22:32 +02:00
|
|
|
* return that at *columns, and return true. (Note that it's possible to
|
2012-07-12 22:26:59 +02:00
|
|
|
* 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. */
|
2016-03-14 21:59:59 +01:00
|
|
|
pull_varattnos((Node *) baserel->reltarget->exprs, baserel->relid,
|
2012-07-12 22:26:59 +02:00
|
|
|
&attrs_used);
|
|
|
|
|
|
|
|
/* Add all the attributes used by restriction clauses. */
|
|
|
|
foreach(lc, baserel->baserestrictinfo)
|
|
|
|
{
|
2013-05-29 22:58:43 +02:00
|
|
|
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
|
2012-07-12 22:26:59 +02:00
|
|
|
|
|
|
|
pull_varattnos((Node *) rinfo->clause, baserel->relid,
|
|
|
|
&attrs_used);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert attribute numbers to column names. */
|
2019-01-21 19:32:19 +01:00
|
|
|
rel = table_open(foreigntableid, AccessShareLock);
|
2012-07-12 22:26:59 +02:00
|
|
|
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)
|
|
|
|
{
|
2017-08-20 20:19:07 +02:00
|
|
|
Form_pg_attribute attr = TupleDescAttr(tupleDesc, attnum - 1);
|
2012-07-12 22:26:59 +02:00
|
|
|
char *attname = NameStr(attr->attname);
|
|
|
|
|
|
|
|
/* Skip dropped attributes (probably shouldn't see any here). */
|
|
|
|
if (attr->attisdropped)
|
|
|
|
continue;
|
2019-05-22 18:55:34 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip generated columns (COPY won't accept them in the column
|
|
|
|
* list)
|
|
|
|
*/
|
2019-04-04 09:24:48 +02:00
|
|
|
if (attr->attgenerated)
|
|
|
|
continue;
|
2012-07-12 22:26:59 +02:00
|
|
|
*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++)
|
|
|
|
{
|
2017-08-20 20:19:07 +02:00
|
|
|
Form_pg_attribute attr = TupleDescAttr(tupleDesc, i);
|
2012-07-12 22:26:59 +02:00
|
|
|
|
|
|
|
if (attr->attisdropped)
|
|
|
|
continue;
|
|
|
|
numattrs++;
|
|
|
|
}
|
|
|
|
|
2019-01-21 19:32:19 +01:00
|
|
|
table_close(rel, AccessShareLock);
|
2012-07-12 22:26:59 +02:00
|
|
|
|
|
|
|
/* 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-20 20:06:59 +01: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-09 18:48:48 +01:00
|
|
|
* Estimate size of a foreign table.
|
2012-03-05 22:15:59 +01: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-09 18:48:48 +01: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-20 20:06:59 +01: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-09 18:48:48 +01:00
|
|
|
estimate_size(PlannerInfo *root, RelOptInfo *baserel,
|
|
|
|
FileFdwPlanState *fdw_private)
|
2011-02-20 20:06:59 +01:00
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
struct stat stat_buf;
|
|
|
|
BlockNumber pages;
|
|
|
|
double ntuples;
|
|
|
|
double nrows;
|
2011-02-20 20:06:59 +01:00
|
|
|
|
|
|
|
/*
|
2011-04-10 17:42:00 +02:00
|
|
|
* Get size of the file. It might not be there at plan time, though, in
|
2016-09-29 19:32:27 +02:00
|
|
|
* which case we have to use a default estimate. We also have to fall
|
|
|
|
* back to the default if using a program as the input.
|
2011-02-20 20:06:59 +01:00
|
|
|
*/
|
2016-09-29 19:32:27 +02:00
|
|
|
if (fdw_private->is_program || stat(fdw_private->filename, &stat_buf) < 0)
|
2011-02-20 20:06:59 +01: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-09 18:48:48 +01:00
|
|
|
* Convert size to pages for use in I/O cost estimate later.
|
2011-02-20 20:06:59 +01:00
|
|
|
*/
|
2011-04-10 17:42:00 +02:00
|
|
|
pages = (stat_buf.st_size + (BLCKSZ - 1)) / BLCKSZ;
|
2011-02-20 20:06:59 +01: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-09 18:48:48 +01:00
|
|
|
fdw_private->pages = pages;
|
|
|
|
|
2011-02-20 20:06:59 +01:00
|
|
|
/*
|
2012-04-06 21:02:35 +02:00
|
|
|
* Estimate the number of tuples in the file.
|
2011-02-20 20:06:59 +01:00
|
|
|
*/
|
Redefine pg_class.reltuples to be -1 before the first VACUUM or ANALYZE.
Historically, we've considered the state with relpages and reltuples
both zero as indicating that we do not know the table's tuple density.
This is problematic because it's impossible to distinguish "never yet
vacuumed" from "vacuumed and seen to be empty". In particular, a user
cannot use VACUUM or ANALYZE to override the planner's normal heuristic
that an empty table should not be believed to be empty because it is
probably about to get populated. That heuristic is a good safety
measure, so I don't care to abandon it, but there should be a way to
override it if the table is indeed intended to stay empty.
Hence, represent the initial state of ignorance by setting reltuples
to -1 (relpages is still set to zero), and apply the minimum-ten-pages
heuristic only when reltuples is still -1. If the table is empty,
VACUUM or ANALYZE (but not CREATE INDEX) will override that to
reltuples = relpages = 0, and then we'll plan on that basis.
This requires a bunch of fiddly little changes, but we can get rid of
some ugly kluges that were formerly needed to maintain the old definition.
One notable point is that FDWs' GetForeignRelSize methods will see
baserel->tuples = -1 when no ANALYZE has been done on the foreign table.
That seems like a net improvement, since those methods were formerly
also in the dark about what baserel->tuples = 0 really meant. Still,
it is an API change.
I bumped catversion because code predating this change would get confused
by seeing reltuples = -1.
Discussion: https://postgr.es/m/F02298E0-6EF4-49A1-BCB6-C484794D9ACC@thebuild.com
2020-08-30 18:21:51 +02:00
|
|
|
if (baserel->tuples >= 0 && baserel->pages > 0)
|
2012-04-06 21:02:35 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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-20 20:06:59 +01:00
|
|
|
|
2012-04-06 21:02:35 +02: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
|
2014-05-06 18:12:18 +02:00
|
|
|
* representation. Possibly we could do something better, but the
|
2012-04-06 21:02:35 +02:00
|
|
|
* real answer to anyone who complains is "ANALYZE" ...
|
|
|
|
*/
|
|
|
|
int tuple_width;
|
2011-02-20 20:06:59 +01:00
|
|
|
|
2016-03-14 21:59:59 +01:00
|
|
|
tuple_width = MAXALIGN(baserel->reltarget->width) +
|
2015-02-21 21:13:06 +01:00
|
|
|
MAXALIGN(SizeofHeapTupleHeader);
|
2012-04-06 21:02:35 +02:00
|
|
|
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-09 18:48:48 +01:00
|
|
|
fdw_private->ntuples = ntuples;
|
|
|
|
|
2011-02-20 20:06:59 +01:00
|
|
|
/*
|
2011-04-10 17:42:00 +02:00
|
|
|
* Now estimate the number of rows returned by the scan after applying the
|
2012-04-06 21:02:35 +02:00
|
|
|
* baserestrictinfo quals.
|
2011-02-20 20:06:59 +01: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-09 18:48:48 +01: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-20 20:06:59 +01: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-09 18:48:48 +01: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.
|
2016-09-29 19:32:27 +02:00
|
|
|
*
|
|
|
|
* In the case of a program source, this calculation is even more divorced
|
|
|
|
* from reality, but we have no good alternative; and it's not clear that
|
|
|
|
* the numbers we produce here matter much anyway, since there's only one
|
|
|
|
* access path for the rel.
|
2011-02-20 20:06:59 +01: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-06 21:02:35 +02: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
|
2014-05-06 18:12:18 +02:00
|
|
|
* *totalrows. Note that *totaldeadrows is always set to 0.
|
2012-04-06 21:02:35 +02: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-06 22:04:10 +02:00
|
|
|
double *totalrows, double *totaldeadrows)
|
2012-04-06 21:02:35 +02:00
|
|
|
{
|
|
|
|
int numrows = 0;
|
2012-06-10 21:20:04 +02:00
|
|
|
double rowstoskip = -1; /* -1 means not set yet */
|
2015-05-15 04:02:54 +02:00
|
|
|
ReservoirStateData rstate;
|
2012-04-06 21:02:35 +02:00
|
|
|
TupleDesc tupDesc;
|
|
|
|
Datum *values;
|
|
|
|
bool *nulls;
|
|
|
|
bool found;
|
|
|
|
char *filename;
|
2016-09-29 19:32:27 +02:00
|
|
|
bool is_program;
|
2012-04-06 21:02:35 +02:00
|
|
|
List *options;
|
Split copy.c into four files.
Copy.c has grown really large. Split it into more manageable parts:
- copy.c now contains only a few functions that are common to COPY FROM
and COPY TO.
- copyto.c contains code for COPY TO.
- copyfrom.c contains code for initializing COPY FROM, and inserting the
tuples to the correct table.
- copyfromparse.c contains code for reading from the client/file/program,
and parsing the input text/CSV/binary format into tuples.
All of these parts are fairly complicated, and fairly independent of each
other. There is a patch being discussed to implement parallel COPY FROM,
which will add a lot of new code to the COPY FROM path, and another patch
which would allow INSERTs to use the same multi-insert machinery as COPY
FROM, both of which will require refactoring that code. With those two
patches, there's going to be a lot of code churn in copy.c anyway, so now
seems like a good time to do this refactoring.
The CopyStateData struct is also split. All the formatting options, like
FORMAT, QUOTE, ESCAPE, are put in a new CopyFormatOption struct, which
is used by both COPY FROM and TO. Other state data are kept in separate
CopyFromStateData and CopyToStateData structs.
Reviewed-by: Soumyadeep Chakraborty, Erik Rijkers, Vignesh C, Andres Freund
Discussion: https://www.postgresql.org/message-id/8e15b560-f387-7acc-ac90-763986617bfb%40iki.fi
2020-11-23 09:50:50 +01:00
|
|
|
CopyFromState cstate;
|
2012-11-12 17:30:51 +01:00
|
|
|
ErrorContextCallback errcallback;
|
2012-04-06 21:02:35 +02: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 */
|
2016-09-29 19:32:27 +02:00
|
|
|
fileGetOptions(RelationGetRelid(onerel), &filename, &is_program, &options);
|
2012-04-06 21:02:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create CopyState from FDW options.
|
|
|
|
*/
|
Split copy.c into four files.
Copy.c has grown really large. Split it into more manageable parts:
- copy.c now contains only a few functions that are common to COPY FROM
and COPY TO.
- copyto.c contains code for COPY TO.
- copyfrom.c contains code for initializing COPY FROM, and inserting the
tuples to the correct table.
- copyfromparse.c contains code for reading from the client/file/program,
and parsing the input text/CSV/binary format into tuples.
All of these parts are fairly complicated, and fairly independent of each
other. There is a patch being discussed to implement parallel COPY FROM,
which will add a lot of new code to the COPY FROM path, and another patch
which would allow INSERTs to use the same multi-insert machinery as COPY
FROM, both of which will require refactoring that code. With those two
patches, there's going to be a lot of code churn in copy.c anyway, so now
seems like a good time to do this refactoring.
The CopyStateData struct is also split. All the formatting options, like
FORMAT, QUOTE, ESCAPE, are put in a new CopyFormatOption struct, which
is used by both COPY FROM and TO. Other state data are kept in separate
CopyFromStateData and CopyToStateData structs.
Reviewed-by: Soumyadeep Chakraborty, Erik Rijkers, Vignesh C, Andres Freund
Discussion: https://www.postgresql.org/message-id/8e15b560-f387-7acc-ac90-763986617bfb%40iki.fi
2020-11-23 09:50:50 +01:00
|
|
|
cstate = BeginCopyFrom(NULL, onerel, NULL, filename, is_program, NULL, NIL,
|
2017-03-23 13:36:36 +01:00
|
|
|
options);
|
2012-04-06 21:02:35 +02:00
|
|
|
|
|
|
|
/*
|
2012-06-10 21:20:04 +02:00
|
|
|
* Use per-tuple memory context to prevent leak of memory used to read
|
|
|
|
* rows from the file with Copy routines.
|
2012-04-06 21:02:35 +02:00
|
|
|
*/
|
|
|
|
tupcontext = AllocSetContextCreate(CurrentMemoryContext,
|
|
|
|
"file_fdw temporary context",
|
Add macros to make AllocSetContextCreate() calls simpler and safer.
I found that half a dozen (nearly 5%) of our AllocSetContextCreate calls
had typos in the context-sizing parameters. While none of these led to
especially significant problems, they did create minor inefficiencies,
and it's now clear that expecting people to copy-and-paste those calls
accurately is not a great idea. Let's reduce the risk of future errors
by introducing single macros that encapsulate the common use-cases.
Three such macros are enough to cover all but two special-purpose contexts;
those two calls can be left as-is, I think.
While this patch doesn't in itself improve matters for third-party
extensions, it doesn't break anything for them either, and they can
gradually adopt the simplified notation over time.
In passing, change TopMemoryContext to use the default allocation
parameters. Formerly it could only be extended 8K at a time. That was
probably reasonable when this code was written; but nowadays we create
many more contexts than we did then, so that it's not unusual to have a
couple hundred K in TopMemoryContext, even without considering various
dubious code that sticks other things there. There seems no good reason
not to let it use growing blocks like most other contexts.
Back-patch to 9.6, mostly because that's still close enough to HEAD that
it's easy to do so, and keeping the branches in sync can be expected to
avoid some future back-patching pain. The bugs fixed by these changes
don't seem to be significant enough to justify fixing them further back.
Discussion: <21072.1472321324@sss.pgh.pa.us>
2016-08-27 23:50:38 +02:00
|
|
|
ALLOCSET_DEFAULT_SIZES);
|
2012-04-06 21:02:35 +02:00
|
|
|
|
|
|
|
/* Prepare for sampling rows */
|
2015-05-15 04:02:54 +02:00
|
|
|
reservoir_init_selection_state(&rstate, targrows);
|
2012-04-06 21:02:35 +02:00
|
|
|
|
|
|
|
/* Set up callback to identify error line number. */
|
2012-11-12 17:30:51 +01:00
|
|
|
errcallback.callback = CopyFromErrorCallback;
|
|
|
|
errcallback.arg = (void *) cstate;
|
|
|
|
errcallback.previous = error_context_stack;
|
|
|
|
error_context_stack = &errcallback;
|
2012-04-06 21:02:35 +02:00
|
|
|
|
|
|
|
*totalrows = 0;
|
|
|
|
*totaldeadrows = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Check for user-requested abort or sleep */
|
|
|
|
vacuum_delay_point();
|
|
|
|
|
|
|
|
/* Fetch next row */
|
|
|
|
MemoryContextReset(tupcontext);
|
|
|
|
MemoryContextSwitchTo(tupcontext);
|
|
|
|
|
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
|
|
|
found = NextCopyFrom(cstate, NULL, values, nulls);
|
2012-04-06 21:02:35 +02:00
|
|
|
|
|
|
|
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)
|
2015-05-15 04:02:54 +02:00
|
|
|
rowstoskip = reservoir_get_next_S(&rstate, *totalrows, targrows);
|
2012-04-06 21:02:35 +02:00
|
|
|
|
|
|
|
if (rowstoskip <= 0)
|
|
|
|
{
|
|
|
|
/*
|
2012-06-10 21:20:04 +02:00
|
|
|
* Found a suitable tuple, so save it, replacing one old tuple
|
|
|
|
* at random
|
2012-04-06 21:02:35 +02:00
|
|
|
*/
|
Replace random(), pg_erand48(), etc with a better PRNG API and algorithm.
Standardize on xoroshiro128** as our basic PRNG algorithm, eliminating
a bunch of platform dependencies as well as fundamentally-obsolete PRNG
code. In addition, this API replacement will ease replacing the
algorithm again in future, should that become necessary.
xoroshiro128** is a few percent slower than the drand48 family,
but it can produce full-width 64-bit random values not only 48-bit,
and it should be much more trustworthy. It's likely to be noticeably
faster than the platform's random(), depending on which platform you
are thinking about; and we can have non-global state vectors easily,
unlike with random(). It is not cryptographically strong, but neither
are the functions it replaces.
Fabien Coelho, reviewed by Dean Rasheed, Aleksander Alekseev, and myself
Discussion: https://postgr.es/m/alpine.DEB.2.22.394.2105241211230.165418@pseudo
2021-11-29 03:32:36 +01:00
|
|
|
int k = (int) (targrows * sampler_random_fract(&rstate.randstate));
|
2012-04-06 21:02:35 +02: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-12 17:30:51 +01:00
|
|
|
error_context_stack = errcallback.previous;
|
2012-04-06 21:02:35 +02:00
|
|
|
|
|
|
|
/* Clean up. */
|
|
|
|
MemoryContextDelete(tupcontext);
|
|
|
|
|
|
|
|
EndCopyFrom(cstate);
|
|
|
|
|
|
|
|
pfree(values);
|
|
|
|
pfree(nulls);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Emit some interesting relation info
|
|
|
|
*/
|
|
|
|
ereport(elevel,
|
2012-04-06 22:04:10 +02:00
|
|
|
(errmsg("\"%s\": file contains %.0f rows; "
|
2012-04-06 21:02:35 +02:00
|
|
|
"%d rows in sample",
|
|
|
|
RelationGetRelationName(onerel),
|
2012-04-06 22:04:10 +02:00
|
|
|
*totalrows, numrows)));
|
2012-04-06 21:02:35 +02:00
|
|
|
|
|
|
|
return numrows;
|
|
|
|
}
|