PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
file_fdw.c File Reference
#include "postgres.h"
#include <sys/stat.h>
#include <unistd.h>
#include "access/htup_details.h"
#include "access/reloptions.h"
#include "access/sysattr.h"
#include "access/table.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_foreign_table.h"
#include "commands/copy.h"
#include "commands/copyfrom_internal.h"
#include "commands/defrem.h"
#include "commands/explain_format.h"
#include "commands/explain_state.h"
#include "commands/vacuum.h"
#include "executor/executor.h"
#include "foreign/fdwapi.h"
#include "foreign/foreign.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "optimizer/optimizer.h"
#include "optimizer/pathnode.h"
#include "optimizer/planmain.h"
#include "optimizer/restrictinfo.h"
#include "utils/acl.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/sampling.h"
#include "utils/varlena.h"
Include dependency graph for file_fdw.c:

Go to the source code of this file.

Data Structures

struct  FileFdwOption
 
struct  FileFdwPlanState
 
struct  FileFdwExecutionState
 

Typedefs

typedef struct FileFdwPlanState FileFdwPlanState
 
typedef struct FileFdwExecutionState FileFdwExecutionState
 

Functions

 PG_MODULE_MAGIC_EXT (.name="file_fdw",.version=PG_VERSION)
 
 PG_FUNCTION_INFO_V1 (file_fdw_handler)
 
 PG_FUNCTION_INFO_V1 (file_fdw_validator)
 
static void fileGetForeignRelSize (PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid)
 
static void fileGetForeignPaths (PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid)
 
static ForeignScanfileGetForeignPlan (PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid, ForeignPath *best_path, List *tlist, List *scan_clauses, Plan *outer_plan)
 
static void fileExplainForeignScan (ForeignScanState *node, ExplainState *es)
 
static void fileBeginForeignScan (ForeignScanState *node, int eflags)
 
static TupleTableSlotfileIterateForeignScan (ForeignScanState *node)
 
static void fileReScanForeignScan (ForeignScanState *node)
 
static void fileEndForeignScan (ForeignScanState *node)
 
static bool fileAnalyzeForeignTable (Relation relation, AcquireSampleRowsFunc *func, BlockNumber *totalpages)
 
static bool fileIsForeignScanParallelSafe (PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 
static bool is_valid_option (const char *option, Oid context)
 
static void fileGetOptions (Oid foreigntableid, char **filename, bool *is_program, List **other_options)
 
static Listget_file_fdw_attribute_options (Oid relid)
 
static bool check_selective_binary_conversion (RelOptInfo *baserel, Oid foreigntableid, List **columns)
 
static void estimate_size (PlannerInfo *root, RelOptInfo *baserel, FileFdwPlanState *fdw_private)
 
static void estimate_costs (PlannerInfo *root, RelOptInfo *baserel, 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)
 
Datum file_fdw_handler (PG_FUNCTION_ARGS)
 
Datum file_fdw_validator (PG_FUNCTION_ARGS)
 

Variables

static const struct FileFdwOption valid_options []
 

Typedef Documentation

◆ FileFdwExecutionState

◆ FileFdwPlanState

Function Documentation

◆ check_selective_binary_conversion()

static bool check_selective_binary_conversion ( RelOptInfo baserel,
Oid  foreigntableid,
List **  columns 
)
static

Definition at line 938 of file file_fdw.c.

941{
943 ListCell *lc;
944 Relation rel;
945 TupleDesc tupleDesc;
946 int attidx;
947 Bitmapset *attrs_used = NULL;
948 bool has_wholerow = false;
949 int numattrs;
950 int i;
951
952 *columns = NIL; /* default result */
953
954 /*
955 * Check format of the file. If binary format, this is irrelevant.
956 */
957 table = GetForeignTable(foreigntableid);
958 foreach(lc, table->options)
959 {
960 DefElem *def = (DefElem *) lfirst(lc);
961
962 if (strcmp(def->defname, "format") == 0)
963 {
964 char *format = defGetString(def);
965
966 if (strcmp(format, "binary") == 0)
967 return false;
968 break;
969 }
970 }
971
972 /* Collect all the attributes needed for joins or final output. */
973 pull_varattnos((Node *) baserel->reltarget->exprs, baserel->relid,
974 &attrs_used);
975
976 /* Add all the attributes used by restriction clauses. */
977 foreach(lc, baserel->baserestrictinfo)
978 {
979 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
980
981 pull_varattnos((Node *) rinfo->clause, baserel->relid,
982 &attrs_used);
983 }
984
985 /* Convert attribute numbers to column names. */
986 rel = table_open(foreigntableid, AccessShareLock);
987 tupleDesc = RelationGetDescr(rel);
988
989 attidx = -1;
990 while ((attidx = bms_next_member(attrs_used, attidx)) >= 0)
991 {
992 /* attidx is zero-based, attnum is the normal attribute number */
994
995 if (attnum == 0)
996 {
997 has_wholerow = true;
998 break;
999 }
1000
1001 /* Ignore system attributes. */
1002 if (attnum < 0)
1003 continue;
1004
1005 /* Get user attributes. */
1006 if (attnum > 0)
1007 {
1008 Form_pg_attribute attr = TupleDescAttr(tupleDesc, attnum - 1);
1009 char *attname = NameStr(attr->attname);
1010
1011 /* Skip dropped attributes (probably shouldn't see any here). */
1012 if (attr->attisdropped)
1013 continue;
1014
1015 /*
1016 * Skip generated columns (COPY won't accept them in the column
1017 * list)
1018 */
1019 if (attr->attgenerated)
1020 continue;
1021 *columns = lappend(*columns, makeString(pstrdup(attname)));
1022 }
1023 }
1024
1025 /* Count non-dropped user attributes while we have the tupdesc. */
1026 numattrs = 0;
1027 for (i = 0; i < tupleDesc->natts; i++)
1028 {
1029 Form_pg_attribute attr = TupleDescAttr(tupleDesc, i);
1030
1031 if (attr->attisdropped)
1032 continue;
1033 numattrs++;
1034 }
1035
1037
1038 /* If there's a whole-row reference, fail: we need all the columns. */
1039 if (has_wholerow)
1040 {
1041 *columns = NIL;
1042 return false;
1043 }
1044
1045 /* If all the user attributes are needed, fail. */
1046 if (numattrs == list_length(*columns))
1047 {
1048 *columns = NIL;
1049 return false;
1050 }
1051
1052 return true;
1053}
int16 AttrNumber
Definition: attnum.h:21
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
#define NameStr(name)
Definition: c.h:717
char * defGetString(DefElem *def)
Definition: define.c:35
ForeignTable * GetForeignTable(Oid relid)
Definition: foreign.c:254
int i
Definition: isn.c:77
List * lappend(List *list, void *datum)
Definition: list.c:339
#define AccessShareLock
Definition: lockdefs.h:36
char * pstrdup(const char *in)
Definition: mcxt.c:2321
NameData attname
Definition: pg_attribute.h:41
int16 attnum
Definition: pg_attribute.h:74
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
static char format
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
static const struct lconv_member_info table[]
#define RelationGetDescr(relation)
Definition: rel.h:542
char * defname
Definition: parsenodes.h:826
Definition: nodes.h:135
List * exprs
Definition: pathnodes.h:1669
List * baserestrictinfo
Definition: pathnodes.h:1012
struct PathTarget * reltarget
Definition: pathnodes.h:920
Index relid
Definition: pathnodes.h:945
Expr * clause
Definition: pathnodes.h:2700
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160
String * makeString(char *str)
Definition: value.c:63
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:296

References AccessShareLock, attname, attnum, RelOptInfo::baserestrictinfo, bms_next_member(), RestrictInfo::clause, defGetString(), DefElem::defname, PathTarget::exprs, FirstLowInvalidHeapAttributeNumber, format, GetForeignTable(), i, lappend(), lfirst, list_length(), makeString(), NameStr, TupleDescData::natts, NIL, pstrdup(), pull_varattnos(), RelationGetDescr, RelOptInfo::relid, RelOptInfo::reltarget, table, table_close(), table_open(), and TupleDescAttr().

Referenced by fileGetForeignPaths().

◆ estimate_costs()

static void estimate_costs ( PlannerInfo root,
RelOptInfo baserel,
FileFdwPlanState fdw_private,
Cost startup_cost,
Cost total_cost 
)
static

Definition at line 1144 of file file_fdw.c.

1147{
1148 BlockNumber pages = fdw_private->pages;
1149 double ntuples = fdw_private->ntuples;
1150 Cost run_cost = 0;
1151 Cost cpu_per_tuple;
1152
1153 /*
1154 * We estimate costs almost the same way as cost_seqscan(), thus assuming
1155 * that I/O costs are equivalent to a regular table file of the same size.
1156 * However, we take per-tuple CPU costs as 10x of a seqscan, to account
1157 * for the cost of parsing records.
1158 *
1159 * In the case of a program source, this calculation is even more divorced
1160 * from reality, but we have no good alternative; and it's not clear that
1161 * the numbers we produce here matter much anyway, since there's only one
1162 * access path for the rel.
1163 */
1164 run_cost += seq_page_cost * pages;
1165
1166 *startup_cost = baserel->baserestrictcost.startup;
1167 cpu_per_tuple = cpu_tuple_cost * 10 + baserel->baserestrictcost.per_tuple;
1168 run_cost += cpu_per_tuple * ntuples;
1169 *total_cost = *startup_cost + run_cost;
1170}
uint32 BlockNumber
Definition: block.h:31
double cpu_tuple_cost
Definition: costsize.c:132
double seq_page_cost
Definition: costsize.c:130
double Cost
Definition: nodes.h:257
double ntuples
Definition: file_fdw.c:107
BlockNumber pages
Definition: file_fdw.c:106
Cost per_tuple
Definition: pathnodes.h:48
Cost startup
Definition: pathnodes.h:47
QualCost baserestrictcost
Definition: pathnodes.h:1014

References RelOptInfo::baserestrictcost, cpu_tuple_cost, FileFdwPlanState::ntuples, FileFdwPlanState::pages, QualCost::per_tuple, seq_page_cost, and QualCost::startup.

Referenced by fileGetForeignPaths().

◆ estimate_size()

static void estimate_size ( PlannerInfo root,
RelOptInfo baserel,
FileFdwPlanState fdw_private 
)
static

Definition at line 1063 of file file_fdw.c.

1065{
1066 struct stat stat_buf;
1067 BlockNumber pages;
1068 double ntuples;
1069 double nrows;
1070
1071 /*
1072 * Get size of the file. It might not be there at plan time, though, in
1073 * which case we have to use a default estimate. We also have to fall
1074 * back to the default if using a program as the input.
1075 */
1076 if (fdw_private->is_program || stat(fdw_private->filename, &stat_buf) < 0)
1077 stat_buf.st_size = 10 * BLCKSZ;
1078
1079 /*
1080 * Convert size to pages for use in I/O cost estimate later.
1081 */
1082 pages = (stat_buf.st_size + (BLCKSZ - 1)) / BLCKSZ;
1083 if (pages < 1)
1084 pages = 1;
1085 fdw_private->pages = pages;
1086
1087 /*
1088 * Estimate the number of tuples in the file.
1089 */
1090 if (baserel->tuples >= 0 && baserel->pages > 0)
1091 {
1092 /*
1093 * We have # of pages and # of tuples from pg_class (that is, from a
1094 * previous ANALYZE), so compute a tuples-per-page estimate and scale
1095 * that by the current file size.
1096 */
1097 double density;
1098
1099 density = baserel->tuples / (double) baserel->pages;
1100 ntuples = clamp_row_est(density * (double) pages);
1101 }
1102 else
1103 {
1104 /*
1105 * Otherwise we have to fake it. We back into this estimate using the
1106 * planner's idea of the relation width; which is bogus if not all
1107 * columns are being read, not to mention that the text representation
1108 * of a row probably isn't the same size as its internal
1109 * representation. Possibly we could do something better, but the
1110 * real answer to anyone who complains is "ANALYZE" ...
1111 */
1112 int tuple_width;
1113
1114 tuple_width = MAXALIGN(baserel->reltarget->width) +
1116 ntuples = clamp_row_est((double) stat_buf.st_size /
1117 (double) tuple_width);
1118 }
1119 fdw_private->ntuples = ntuples;
1120
1121 /*
1122 * Now estimate the number of rows returned by the scan after applying the
1123 * baserestrictinfo quals.
1124 */
1125 nrows = ntuples *
1127 baserel->baserestrictinfo,
1128 0,
1129 JOIN_INNER,
1130 NULL);
1131
1132 nrows = clamp_row_est(nrows);
1133
1134 /* Save the output-rows estimate for the planner */
1135 baserel->rows = nrows;
1136}
#define MAXALIGN(LEN)
Definition: c.h:782
Selectivity clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: clausesel.c:100
double clamp_row_est(double nrows)
Definition: costsize.c:213
#define SizeofHeapTupleHeader
Definition: htup_details.h:185
@ JOIN_INNER
Definition: nodes.h:299
tree ctl root
Definition: radixtree.h:1857
char * filename
Definition: file_fdw.c:102
Cardinality tuples
Definition: pathnodes.h:976
BlockNumber pages
Definition: pathnodes.h:975
Cardinality rows
Definition: pathnodes.h:904
__int64 st_size
Definition: win32_port.h:263

References RelOptInfo::baserestrictinfo, clamp_row_est(), clauselist_selectivity(), FileFdwPlanState::filename, FileFdwPlanState::is_program, JOIN_INNER, MAXALIGN, FileFdwPlanState::ntuples, FileFdwPlanState::pages, RelOptInfo::pages, RelOptInfo::reltarget, root, RelOptInfo::rows, SizeofHeapTupleHeader, stat::st_size, RelOptInfo::tuples, and PathTarget::width.

Referenced by fileGetForeignRelSize().

◆ file_acquire_sample_rows()

static int file_acquire_sample_rows ( Relation  onerel,
int  elevel,
HeapTuple rows,
int  targrows,
double *  totalrows,
double *  totaldeadrows 
)
static

Definition at line 1188 of file file_fdw.c.

1191{
1192 int numrows = 0;
1193 double rowstoskip = -1; /* -1 means not set yet */
1194 ReservoirStateData rstate;
1195 TupleDesc tupDesc;
1196 Datum *values;
1197 bool *nulls;
1198 bool found;
1199 char *filename;
1200 bool is_program;
1201 List *options;
1202 CopyFromState cstate;
1203 ErrorContextCallback errcallback;
1205 MemoryContext tupcontext;
1206
1207 Assert(onerel);
1208 Assert(targrows > 0);
1209
1210 tupDesc = RelationGetDescr(onerel);
1211 values = (Datum *) palloc(tupDesc->natts * sizeof(Datum));
1212 nulls = (bool *) palloc(tupDesc->natts * sizeof(bool));
1213
1214 /* Fetch options of foreign table */
1215 fileGetOptions(RelationGetRelid(onerel), &filename, &is_program, &options);
1216
1217 /*
1218 * Create CopyState from FDW options.
1219 */
1220 cstate = BeginCopyFrom(NULL, onerel, NULL, filename, is_program, NULL, NIL,
1221 options);
1222
1223 /*
1224 * Use per-tuple memory context to prevent leak of memory used to read
1225 * rows from the file with Copy routines.
1226 */
1228 "file_fdw temporary context",
1230
1231 /* Prepare for sampling rows */
1232 reservoir_init_selection_state(&rstate, targrows);
1233
1234 /* Set up callback to identify error line number. */
1235 errcallback.callback = CopyFromErrorCallback;
1236 errcallback.arg = cstate;
1237 errcallback.previous = error_context_stack;
1238 error_context_stack = &errcallback;
1239
1240 *totalrows = 0;
1241 *totaldeadrows = 0;
1242 for (;;)
1243 {
1244 /* Check for user-requested abort or sleep */
1245 vacuum_delay_point(true);
1246
1247 /* Fetch next row */
1248 MemoryContextReset(tupcontext);
1249 MemoryContextSwitchTo(tupcontext);
1250
1251 found = NextCopyFrom(cstate, NULL, values, nulls);
1252
1253 MemoryContextSwitchTo(oldcontext);
1254
1255 if (!found)
1256 break;
1257
1258 if (cstate->opts.on_error == COPY_ON_ERROR_IGNORE &&
1259 cstate->escontext->error_occurred)
1260 {
1261 /*
1262 * Soft error occurred, skip this tuple and just make
1263 * ErrorSaveContext ready for the next NextCopyFrom. Since we
1264 * don't set details_wanted and error_data is not to be filled,
1265 * just resetting error_occurred is enough.
1266 */
1267 cstate->escontext->error_occurred = false;
1268
1269 /* Repeat NextCopyFrom() until no soft error occurs */
1270 continue;
1271 }
1272
1273 /*
1274 * The first targrows sample rows are simply copied into the
1275 * reservoir. Then we start replacing tuples in the sample until we
1276 * reach the end of the relation. This algorithm is from Jeff Vitter's
1277 * paper (see more info in commands/analyze.c).
1278 */
1279 if (numrows < targrows)
1280 {
1281 rows[numrows++] = heap_form_tuple(tupDesc, values, nulls);
1282 }
1283 else
1284 {
1285 /*
1286 * t in Vitter's paper is the number of records already processed.
1287 * If we need to compute a new S value, we must use the
1288 * not-yet-incremented value of totalrows as t.
1289 */
1290 if (rowstoskip < 0)
1291 rowstoskip = reservoir_get_next_S(&rstate, *totalrows, targrows);
1292
1293 if (rowstoskip <= 0)
1294 {
1295 /*
1296 * Found a suitable tuple, so save it, replacing one old tuple
1297 * at random
1298 */
1299 int k = (int) (targrows * sampler_random_fract(&rstate.randstate));
1300
1301 Assert(k >= 0 && k < targrows);
1302 heap_freetuple(rows[k]);
1303 rows[k] = heap_form_tuple(tupDesc, values, nulls);
1304 }
1305
1306 rowstoskip -= 1;
1307 }
1308
1309 *totalrows += 1;
1310 }
1311
1312 /* Remove error callback. */
1313 error_context_stack = errcallback.previous;
1314
1315 /* Clean up. */
1316 MemoryContextDelete(tupcontext);
1317
1318 if (cstate->opts.on_error == COPY_ON_ERROR_IGNORE &&
1319 cstate->num_errors > 0 &&
1322 errmsg_plural("%" PRIu64 " row was skipped due to data type incompatibility",
1323 "%" PRIu64 " rows were skipped due to data type incompatibility",
1324 cstate->num_errors,
1325 cstate->num_errors));
1326
1327 EndCopyFrom(cstate);
1328
1329 pfree(values);
1330 pfree(nulls);
1331
1332 /*
1333 * Emit some interesting relation info
1334 */
1335 ereport(elevel,
1336 (errmsg("\"%s\": file contains %.0f rows; "
1337 "%d rows in sample",
1339 *totalrows, numrows)));
1340
1341 return numrows;
1342}
static Datum values[MAXATTR]
Definition: bootstrap.c:151
CopyFromState BeginCopyFrom(ParseState *pstate, Relation rel, Node *whereClause, const char *filename, bool is_program, copy_data_source_cb data_source_cb, List *attnamelist, List *options)
Definition: copyfrom.c:1529
void EndCopyFrom(CopyFromState cstate)
Definition: copyfrom.c:1914
void CopyFromErrorCallback(void *arg)
Definition: copyfrom.c:254
bool NextCopyFrom(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls)
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1181
ErrorContextCallback * error_context_stack
Definition: elog.c:95
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define NOTICE
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:149
static void fileGetOptions(Oid foreigntableid, char **filename, bool *is_program, List **other_options)
Definition: file_fdw.c:379
Assert(PointerIsAligned(start, uint64))
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
@ COPY_ON_ERROR_IGNORE
Definition: copy.h:40
@ COPY_LOG_VERBOSITY_DEFAULT
Definition: copy.h:49
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:414
void pfree(void *pointer)
Definition: mcxt.c:2146
void * palloc(Size size)
Definition: mcxt.c:1939
MemoryContext CurrentMemoryContext
Definition: mcxt.c:159
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:485
#define AllocSetContextCreate
Definition: memutils.h:149
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:180
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
static char * filename
Definition: pg_dumpall.c:123
static char ** options
uintptr_t Datum
Definition: postgres.h:69
#define RelationGetRelid(relation)
Definition: rel.h:516
#define RelationGetRelationName(relation)
Definition: rel.h:550
void reservoir_init_selection_state(ReservoirState rs, int n)
Definition: sampling.c:133
double sampler_random_fract(pg_prng_state *randstate)
Definition: sampling.c:241
double reservoir_get_next_S(ReservoirState rs, double t, int n)
Definition: sampling.c:147
CopyLogVerbosityChoice log_verbosity
Definition: copy.h:87
CopyOnErrorChoice on_error
Definition: copy.h:86
CopyFormatOptions opts
ErrorSaveContext * escontext
struct ErrorContextCallback * previous
Definition: elog.h:297
void(* callback)(void *arg)
Definition: elog.h:298
bool error_occurred
Definition: miscnodes.h:47
Definition: pg_list.h:54
pg_prng_state randstate
Definition: sampling.h:49
void vacuum_delay_point(bool is_analyze)
Definition: vacuum.c:2402

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, ErrorContextCallback::arg, Assert(), BeginCopyFrom(), ErrorContextCallback::callback, COPY_LOG_VERBOSITY_DEFAULT, COPY_ON_ERROR_IGNORE, CopyFromErrorCallback(), CurrentMemoryContext, EndCopyFrom(), ereport, errmsg(), errmsg_plural(), error_context_stack, ErrorSaveContext::error_occurred, CopyFromStateData::escontext, fileGetOptions(), filename, heap_form_tuple(), heap_freetuple(), CopyFormatOptions::log_verbosity, MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), TupleDescData::natts, NextCopyFrom(), NIL, NOTICE, CopyFromStateData::num_errors, CopyFormatOptions::on_error, options, CopyFromStateData::opts, palloc(), pfree(), ErrorContextCallback::previous, ReservoirStateData::randstate, RelationGetDescr, RelationGetRelationName, RelationGetRelid, reservoir_get_next_S(), reservoir_init_selection_state(), sampler_random_fract(), vacuum_delay_point(), and values.

Referenced by fileAnalyzeForeignTable().

◆ file_fdw_handler()

Datum file_fdw_handler ( PG_FUNCTION_ARGS  )

Definition at line 182 of file file_fdw.c.

183{
184 FdwRoutine *fdwroutine = makeNode(FdwRoutine);
185
196
197 PG_RETURN_POINTER(fdwroutine);
198}
static ForeignScan * fileGetForeignPlan(PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid, ForeignPath *best_path, List *tlist, List *scan_clauses, Plan *outer_plan)
Definition: file_fdw.c:609
static void fileEndForeignScan(ForeignScanState *node)
Definition: file_fdw.c:846
static void fileGetForeignRelSize(PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid)
Definition: file_fdw.c:523
static void fileExplainForeignScan(ForeignScanState *node, ExplainState *es)
Definition: file_fdw.c:644
static bool fileAnalyzeForeignTable(Relation relation, AcquireSampleRowsFunc *func, BlockNumber *totalpages)
Definition: file_fdw.c:871
static void fileReScanForeignScan(ForeignScanState *node)
Definition: file_fdw.c:825
static bool fileIsForeignScanParallelSafe(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
Definition: file_fdw.c:922
static void fileGetForeignPaths(PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid)
Definition: file_fdw.c:554
static void fileBeginForeignScan(ForeignScanState *node, int eflags)
Definition: file_fdw.c:676
static TupleTableSlot * fileIterateForeignScan(ForeignScanState *node)
Definition: file_fdw.c:730
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define makeNode(_type_)
Definition: nodes.h:161
ReScanForeignScan_function ReScanForeignScan
Definition: fdwapi.h:214
BeginForeignScan_function BeginForeignScan
Definition: fdwapi.h:212
IsForeignScanParallelSafe_function IsForeignScanParallelSafe
Definition: fdwapi.h:266
GetForeignPaths_function GetForeignPaths
Definition: fdwapi.h:210
GetForeignRelSize_function GetForeignRelSize
Definition: fdwapi.h:209
ExplainForeignScan_function ExplainForeignScan
Definition: fdwapi.h:252
EndForeignScan_function EndForeignScan
Definition: fdwapi.h:215
AnalyzeForeignTable_function AnalyzeForeignTable
Definition: fdwapi.h:257
IterateForeignScan_function IterateForeignScan
Definition: fdwapi.h:213
GetForeignPlan_function GetForeignPlan
Definition: fdwapi.h:211

References FdwRoutine::AnalyzeForeignTable, FdwRoutine::BeginForeignScan, FdwRoutine::EndForeignScan, FdwRoutine::ExplainForeignScan, fileAnalyzeForeignTable(), fileBeginForeignScan(), fileEndForeignScan(), fileExplainForeignScan(), fileGetForeignPaths(), fileGetForeignPlan(), fileGetForeignRelSize(), fileIsForeignScanParallelSafe(), fileIterateForeignScan(), fileReScanForeignScan(), FdwRoutine::GetForeignPaths, FdwRoutine::GetForeignPlan, FdwRoutine::GetForeignRelSize, FdwRoutine::IsForeignScanParallelSafe, FdwRoutine::IterateForeignScan, makeNode, PG_RETURN_POINTER, and FdwRoutine::ReScanForeignScan.

◆ file_fdw_validator()

Datum file_fdw_validator ( PG_FUNCTION_ARGS  )

Definition at line 207 of file file_fdw.c.

208{
209 List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
210 Oid catalog = PG_GETARG_OID(1);
211 char *filename = NULL;
212 DefElem *force_not_null = NULL;
213 DefElem *force_null = NULL;
214 List *other_options = NIL;
215 ListCell *cell;
216
217 /*
218 * Check that only options supported by file_fdw, and allowed for the
219 * current object type, are given.
220 */
221 foreach(cell, options_list)
222 {
223 DefElem *def = (DefElem *) lfirst(cell);
224
225 if (!is_valid_option(def->defname, catalog))
226 {
227 const struct FileFdwOption *opt;
228 const char *closest_match;
230 bool has_valid_options = false;
231
232 /*
233 * Unknown option specified, complain about it. Provide a hint
234 * with a valid option that looks similar, if there is one.
235 */
237 for (opt = valid_options; opt->optname; opt++)
238 {
239 if (catalog == opt->optcontext)
240 {
241 has_valid_options = true;
243 }
244 }
245
246 closest_match = getClosestMatch(&match_state);
248 (errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
249 errmsg("invalid option \"%s\"", def->defname),
250 has_valid_options ? closest_match ?
251 errhint("Perhaps you meant the option \"%s\".",
252 closest_match) : 0 :
253 errhint("There are no valid options in this context.")));
254 }
255
256 /*
257 * Separate out filename, program, and column-specific options, since
258 * ProcessCopyOptions won't accept them.
259 */
260 if (strcmp(def->defname, "filename") == 0 ||
261 strcmp(def->defname, "program") == 0)
262 {
263 if (filename)
265 (errcode(ERRCODE_SYNTAX_ERROR),
266 errmsg("conflicting or redundant options")));
267
268 /*
269 * Check permissions for changing which file or program is used by
270 * the file_fdw.
271 *
272 * Only members of the role 'pg_read_server_files' are allowed to
273 * set the 'filename' option of a file_fdw foreign table, while
274 * only members of the role 'pg_execute_server_program' are
275 * allowed to set the 'program' option. This is because we don't
276 * want regular users to be able to control which file gets read
277 * or which program gets executed.
278 *
279 * Putting this sort of permissions check in a validator is a bit
280 * of a crock, but there doesn't seem to be any other place that
281 * can enforce the check more cleanly.
282 *
283 * Note that the valid_options[] array disallows setting filename
284 * and program at any options level other than foreign table ---
285 * otherwise there'd still be a security hole.
286 */
287 if (strcmp(def->defname, "filename") == 0 &&
288 !has_privs_of_role(GetUserId(), ROLE_PG_READ_SERVER_FILES))
290 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
291 errmsg("permission denied to set the \"%s\" option of a file_fdw foreign table",
292 "filename"),
293 errdetail("Only roles with privileges of the \"%s\" role may set this option.",
294 "pg_read_server_files")));
295
296 if (strcmp(def->defname, "program") == 0 &&
297 !has_privs_of_role(GetUserId(), ROLE_PG_EXECUTE_SERVER_PROGRAM))
299 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
300 errmsg("permission denied to set the \"%s\" option of a file_fdw foreign table",
301 "program"),
302 errdetail("Only roles with privileges of the \"%s\" role may set this option.",
303 "pg_execute_server_program")));
304
305 filename = defGetString(def);
306 }
307
308 /*
309 * force_not_null is a boolean option; after validation we can discard
310 * it - it will be retrieved later in get_file_fdw_attribute_options()
311 */
312 else if (strcmp(def->defname, "force_not_null") == 0)
313 {
314 if (force_not_null)
316 (errcode(ERRCODE_SYNTAX_ERROR),
317 errmsg("conflicting or redundant options"),
318 errhint("Option \"force_not_null\" supplied more than once for a column.")));
319 force_not_null = def;
320 /* Don't care what the value is, as long as it's a legal boolean */
321 (void) defGetBoolean(def);
322 }
323 /* See comments for force_not_null above */
324 else if (strcmp(def->defname, "force_null") == 0)
325 {
326 if (force_null)
328 (errcode(ERRCODE_SYNTAX_ERROR),
329 errmsg("conflicting or redundant options"),
330 errhint("Option \"force_null\" supplied more than once for a column.")));
331 force_null = def;
332 (void) defGetBoolean(def);
333 }
334 else
335 other_options = lappend(other_options, def);
336 }
337
338 /*
339 * Now apply the core COPY code's validation logic for more checks.
340 */
341 ProcessCopyOptions(NULL, NULL, true, other_options);
342
343 /*
344 * Either filename or program option is required for file_fdw foreign
345 * tables.
346 */
347 if (catalog == ForeignTableRelationId && filename == NULL)
349 (errcode(ERRCODE_FDW_DYNAMIC_PARAMETER_VALUE_NEEDED),
350 errmsg("either filename or program is required for file_fdw foreign tables")));
351
353}
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:5268
void ProcessCopyOptions(ParseState *pstate, CopyFormatOptions *opts_out, bool is_from, List *options)
Definition: copy.c:496
bool defGetBoolean(DefElem *def)
Definition: define.c:94
int errdetail(const char *fmt,...)
Definition: elog.c:1204
int errhint(const char *fmt,...)
Definition: elog.c:1318
int errcode(int sqlerrcode)
Definition: elog.c:854
#define ERROR
Definition: elog.h:39
static const struct FileFdwOption valid_options[]
Definition: file_fdw.c:68
static bool is_valid_option(const char *option, Oid context)
Definition: file_fdw.c:360
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
struct parser_state match_state[5]
Oid GetUserId(void)
Definition: miscinit.c:520
unsigned int Oid
Definition: postgres_ext.h:30
List * untransformRelOptions(Datum options)
Definition: reloptions.c:1342
Oid optcontext
Definition: file_fdw.c:56
const char * optname
Definition: file_fdw.c:55
const char * getClosestMatch(ClosestMatchState *state)
Definition: varlena.c:6445
void initClosestMatch(ClosestMatchState *state, const char *source, int max_d)
Definition: varlena.c:6390
void updateClosestMatch(ClosestMatchState *state, const char *candidate)
Definition: varlena.c:6410

References defGetBoolean(), defGetString(), DefElem::defname, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, filename, getClosestMatch(), GetUserId(), has_privs_of_role(), initClosestMatch(), is_valid_option(), lappend(), lfirst, match_state, NIL, FileFdwOption::optcontext, FileFdwOption::optname, PG_GETARG_DATUM, PG_GETARG_OID, PG_RETURN_VOID, ProcessCopyOptions(), untransformRelOptions(), updateClosestMatch(), and valid_options.

◆ fileAnalyzeForeignTable()

static bool fileAnalyzeForeignTable ( Relation  relation,
AcquireSampleRowsFunc func,
BlockNumber totalpages 
)
static

Definition at line 871 of file file_fdw.c.

874{
875 char *filename;
876 bool is_program;
877 List *options;
878 struct stat stat_buf;
879
880 /* Fetch options of foreign table */
881 fileGetOptions(RelationGetRelid(relation), &filename, &is_program, &options);
882
883 /*
884 * If this is a program instead of a file, just return false to skip
885 * analyzing the table. We could run the program and collect stats on
886 * whatever it currently returns, but it seems likely that in such cases
887 * the output would be too volatile for the stats to be useful. Maybe
888 * there should be an option to enable doing this?
889 */
890 if (is_program)
891 return false;
892
893 /*
894 * Get size of the file. (XXX if we fail here, would it be better to just
895 * return false to skip analyzing the table?)
896 */
897 if (stat(filename, &stat_buf) < 0)
900 errmsg("could not stat file \"%s\": %m",
901 filename)));
902
903 /*
904 * Convert size to pages. Must return at least 1 so that we can tell
905 * later on that pg_class.relpages is not default.
906 */
907 *totalpages = (stat_buf.st_size + (BLCKSZ - 1)) / BLCKSZ;
908 if (*totalpages < 1)
909 *totalpages = 1;
910
912
913 return true;
914}
int errcode_for_file_access(void)
Definition: elog.c:877
static int file_acquire_sample_rows(Relation onerel, int elevel, HeapTuple *rows, int targrows, double *totalrows, double *totaldeadrows)
Definition: file_fdw.c:1188
#define stat
Definition: win32_port.h:274

References ereport, errcode_for_file_access(), errmsg(), ERROR, file_acquire_sample_rows(), fileGetOptions(), filename, options, RelationGetRelid, stat::st_size, and stat.

Referenced by file_fdw_handler().

◆ fileBeginForeignScan()

static void fileBeginForeignScan ( ForeignScanState node,
int  eflags 
)
static

Definition at line 676 of file file_fdw.c.

677{
678 ForeignScan *plan = (ForeignScan *) node->ss.ps.plan;
679 char *filename;
680 bool is_program;
681 List *options;
682 CopyFromState cstate;
683 FileFdwExecutionState *festate;
684
685 /*
686 * Do nothing in EXPLAIN (no ANALYZE) case. node->fdw_state stays NULL.
687 */
688 if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
689 return;
690
691 /* Fetch options of foreign table */
693 &filename, &is_program, &options);
694
695 /* Add any options from the plan (currently only convert_selectively) */
696 options = list_concat(options, plan->fdw_private);
697
698 /*
699 * Create CopyState from FDW options. We always acquire all columns, so
700 * as to match the expected ScanTupleSlot signature.
701 */
702 cstate = BeginCopyFrom(NULL,
704 NULL,
705 filename,
706 is_program,
707 NULL,
708 NIL,
709 options);
710
711 /*
712 * Save state in node->fdw_state. We must save enough information to call
713 * BeginCopyFrom() again.
714 */
716 festate->filename = filename;
717 festate->is_program = is_program;
718 festate->options = options;
719 festate->cstate = cstate;
720
721 node->fdw_state = festate;
722}
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:66
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
#define plan(x)
Definition: pg_regress.c:161
ScanState ss
Definition: execnodes.h:2105
Plan * plan
Definition: execnodes.h:1159
Relation ss_currentRelation
Definition: execnodes.h:1616
PlanState ps
Definition: execnodes.h:1615

References BeginCopyFrom(), EXEC_FLAG_EXPLAIN_ONLY, ForeignScanState::fdw_state, fileGetOptions(), filename, if(), list_concat(), NIL, options, palloc(), PlanState::plan, plan, ScanState::ps, RelationGetRelid, ForeignScanState::ss, and ScanState::ss_currentRelation.

Referenced by file_fdw_handler().

◆ fileEndForeignScan()

static void fileEndForeignScan ( ForeignScanState node)
static

Definition at line 846 of file file_fdw.c.

847{
849
850 /* if festate is NULL, we are in EXPLAIN; nothing to do */
851 if (!festate)
852 return;
853
854 if (festate->cstate->opts.on_error == COPY_ON_ERROR_IGNORE &&
855 festate->cstate->num_errors > 0 &&
858 errmsg_plural("%" PRIu64 " row was skipped due to data type incompatibility",
859 "%" PRIu64 " rows were skipped due to data type incompatibility",
860 festate->cstate->num_errors,
861 festate->cstate->num_errors));
862
863 EndCopyFrom(festate->cstate);
864}
CopyFromState cstate
Definition: file_fdw.c:119

References COPY_LOG_VERBOSITY_DEFAULT, COPY_ON_ERROR_IGNORE, FileFdwExecutionState::cstate, EndCopyFrom(), ereport, errmsg_plural(), ForeignScanState::fdw_state, if(), CopyFormatOptions::log_verbosity, NOTICE, CopyFromStateData::num_errors, CopyFormatOptions::on_error, and CopyFromStateData::opts.

Referenced by file_fdw_handler().

◆ fileExplainForeignScan()

static void fileExplainForeignScan ( ForeignScanState node,
ExplainState es 
)
static

Definition at line 644 of file file_fdw.c.

645{
646 char *filename;
647 bool is_program;
648 List *options;
649
650 /* Fetch options --- we only need filename and is_program at this point */
652 &filename, &is_program, &options);
653
654 if (is_program)
655 ExplainPropertyText("Foreign Program", filename, es);
656 else
657 ExplainPropertyText("Foreign File", filename, es);
658
659 /* Suppress file size if we're not showing cost details */
660 if (es->costs)
661 {
662 struct stat stat_buf;
663
664 if (!is_program &&
665 stat(filename, &stat_buf) == 0)
666 ExplainPropertyInteger("Foreign File Size", "b",
667 (int64) stat_buf.st_size, es);
668 }
669}
int64_t int64
Definition: c.h:499
void ExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
void ExplainPropertyInteger(const char *qlabel, const char *unit, int64 value, ExplainState *es)

References ExplainState::costs, ExplainPropertyInteger(), ExplainPropertyText(), fileGetOptions(), filename, options, RelationGetRelid, ForeignScanState::ss, ScanState::ss_currentRelation, stat::st_size, and stat.

Referenced by file_fdw_handler().

◆ fileGetForeignPaths()

static void fileGetForeignPaths ( PlannerInfo root,
RelOptInfo baserel,
Oid  foreigntableid 
)
static

Definition at line 554 of file file_fdw.c.

557{
558 FileFdwPlanState *fdw_private = (FileFdwPlanState *) baserel->fdw_private;
559 Cost startup_cost;
560 Cost total_cost;
561 List *columns;
562 List *coptions = NIL;
563
564 /* Decide whether to selectively perform binary conversion */
566 foreigntableid,
567 &columns))
568 coptions = list_make1(makeDefElem("convert_selectively",
569 (Node *) columns, -1));
570
571 /* Estimate costs */
572 estimate_costs(root, baserel, fdw_private,
573 &startup_cost, &total_cost);
574
575 /*
576 * Create a ForeignPath node and add it as only possible path. We use the
577 * fdw_private list of the path to carry the convert_selectively option;
578 * it will be propagated into the fdw_private list of the Plan node.
579 *
580 * We don't support pushing join clauses into the quals of this path, but
581 * it could still have required parameterization due to LATERAL refs in
582 * its tlist.
583 */
584 add_path(baserel, (Path *)
586 NULL, /* default pathtarget */
587 baserel->rows,
588 0,
589 startup_cost,
590 total_cost,
591 NIL, /* no pathkeys */
592 baserel->lateral_relids,
593 NULL, /* no extra plan */
594 NIL, /* no fdw_restrictinfo list */
595 coptions));
596
597 /*
598 * If data file was sorted, and we knew it somehow, we could insert
599 * appropriate pathkeys into the ForeignPath node to tell the planner
600 * that.
601 */
602}
static bool check_selective_binary_conversion(RelOptInfo *baserel, Oid foreigntableid, List **columns)
Definition: file_fdw.c:938
static void estimate_costs(PlannerInfo *root, RelOptInfo *baserel, FileFdwPlanState *fdw_private, Cost *startup_cost, Cost *total_cost)
Definition: file_fdw.c:1144
DefElem * makeDefElem(char *name, Node *arg, int location)
Definition: makefuncs.c:637
ForeignPath * create_foreignscan_path(PlannerInfo *root, RelOptInfo *rel, PathTarget *target, double rows, int disabled_nodes, Cost startup_cost, Cost total_cost, List *pathkeys, Relids required_outer, Path *fdw_outerpath, List *fdw_restrictinfo, List *fdw_private)
Definition: pathnode.c:2307
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:461
#define list_make1(x1)
Definition: pg_list.h:212
Relids lateral_relids
Definition: pathnodes.h:940

References add_path(), check_selective_binary_conversion(), create_foreignscan_path(), estimate_costs(), if(), RelOptInfo::lateral_relids, list_make1, makeDefElem(), NIL, root, and RelOptInfo::rows.

Referenced by file_fdw_handler().

◆ fileGetForeignPlan()

static ForeignScan * fileGetForeignPlan ( PlannerInfo root,
RelOptInfo baserel,
Oid  foreigntableid,
ForeignPath best_path,
List tlist,
List scan_clauses,
Plan outer_plan 
)
static

Definition at line 609 of file file_fdw.c.

616{
617 Index scan_relid = baserel->relid;
618
619 /*
620 * We have no native ability to evaluate restriction clauses, so we just
621 * put all the scan_clauses into the plan node's qual list for the
622 * executor to check. So all we have to do here is strip RestrictInfo
623 * nodes from the clauses and ignore pseudoconstants (which will be
624 * handled elsewhere).
625 */
626 scan_clauses = extract_actual_clauses(scan_clauses, false);
627
628 /* Create the ForeignScan node */
629 return make_foreignscan(tlist,
630 scan_clauses,
631 scan_relid,
632 NIL, /* no expressions to evaluate */
633 best_path->fdw_private,
634 NIL, /* no custom tlist */
635 NIL, /* no remote quals */
636 outer_plan);
637}
unsigned int Index
Definition: c.h:585
ForeignScan * make_foreignscan(List *qptlist, List *qpqual, Index scanrelid, List *fdw_exprs, List *fdw_private, List *fdw_scan_tlist, List *fdw_recheck_quals, Plan *outer_plan)
Definition: createplan.c:5888
List * extract_actual_clauses(List *restrictinfo_list, bool pseudoconstant)
Definition: restrictinfo.c:485
List * fdw_private
Definition: pathnodes.h:2009

References extract_actual_clauses(), ForeignPath::fdw_private, make_foreignscan(), NIL, and RelOptInfo::relid.

Referenced by file_fdw_handler().

◆ fileGetForeignRelSize()

static void fileGetForeignRelSize ( PlannerInfo root,
RelOptInfo baserel,
Oid  foreigntableid 
)
static

Definition at line 523 of file file_fdw.c.

526{
527 FileFdwPlanState *fdw_private;
528
529 /*
530 * Fetch options. We only need filename (or program) at this point, but
531 * we might as well get everything and not need to re-fetch it later in
532 * planning.
533 */
534 fdw_private = (FileFdwPlanState *) palloc(sizeof(FileFdwPlanState));
535 fileGetOptions(foreigntableid,
536 &fdw_private->filename,
537 &fdw_private->is_program,
538 &fdw_private->options);
539 baserel->fdw_private = fdw_private;
540
541 /* Estimate relation size */
542 estimate_size(root, baserel, fdw_private);
543}
static void estimate_size(PlannerInfo *root, RelOptInfo *baserel, FileFdwPlanState *fdw_private)
Definition: file_fdw.c:1063
List * options
Definition: file_fdw.c:104

References estimate_size(), fileGetOptions(), FileFdwPlanState::filename, FileFdwPlanState::is_program, FileFdwPlanState::options, palloc(), and root.

Referenced by file_fdw_handler().

◆ fileGetOptions()

static void fileGetOptions ( Oid  foreigntableid,
char **  filename,
bool *  is_program,
List **  other_options 
)
static

Definition at line 379 of file file_fdw.c.

381{
383 ForeignServer *server;
384 ForeignDataWrapper *wrapper;
385 List *options;
386 ListCell *lc;
387
388 /*
389 * Extract options from FDW objects. We ignore user mappings because
390 * file_fdw doesn't have any options that can be specified there.
391 *
392 * (XXX Actually, given the current contents of valid_options[], there's
393 * no point in examining anything except the foreign table's own options.
394 * Simplify?)
395 */
396 table = GetForeignTable(foreigntableid);
397 server = GetForeignServer(table->serverid);
398 wrapper = GetForeignDataWrapper(server->fdwid);
399
400 options = NIL;
401 options = list_concat(options, wrapper->options);
403 options = list_concat(options, table->options);
405
406 /*
407 * Separate out the filename or program option (we assume there is only
408 * one).
409 */
410 *filename = NULL;
411 *is_program = false;
412 foreach(lc, options)
413 {
414 DefElem *def = (DefElem *) lfirst(lc);
415
416 if (strcmp(def->defname, "filename") == 0)
417 {
418 *filename = defGetString(def);
420 break;
421 }
422 else if (strcmp(def->defname, "program") == 0)
423 {
424 *filename = defGetString(def);
425 *is_program = true;
427 break;
428 }
429 }
430
431 /*
432 * The validator should have checked that filename or program was included
433 * in the options, but check again, just in case.
434 */
435 if (*filename == NULL)
436 elog(ERROR, "either filename or program is required for file_fdw foreign tables");
437
438 *other_options = options;
439}
#define elog(elevel,...)
Definition: elog.h:226
static List * get_file_fdw_attribute_options(Oid relid)
Definition: file_fdw.c:450
ForeignDataWrapper * GetForeignDataWrapper(Oid fdwid)
Definition: foreign.c:37
ForeignServer * GetForeignServer(Oid serverid)
Definition: foreign.c:111
#define foreach_delete_current(lst, var_or_cell)
Definition: pg_list.h:391
List * options
Definition: foreign.h:31
List * options
Definition: foreign.h:42

References defGetString(), DefElem::defname, elog, ERROR, ForeignServer::fdwid, filename, foreach_delete_current, get_file_fdw_attribute_options(), GetForeignDataWrapper(), GetForeignServer(), GetForeignTable(), lfirst, list_concat(), NIL, options, ForeignDataWrapper::options, ForeignServer::options, and table.

Referenced by file_acquire_sample_rows(), fileAnalyzeForeignTable(), fileBeginForeignScan(), fileExplainForeignScan(), and fileGetForeignRelSize().

◆ fileIsForeignScanParallelSafe()

static bool fileIsForeignScanParallelSafe ( PlannerInfo root,
RelOptInfo rel,
RangeTblEntry rte 
)
static

Definition at line 922 of file file_fdw.c.

924{
925 return true;
926}

Referenced by file_fdw_handler().

◆ fileIterateForeignScan()

static TupleTableSlot * fileIterateForeignScan ( ForeignScanState node)
static

Definition at line 730 of file file_fdw.c.

731{
733 EState *estate = CreateExecutorState();
734 ExprContext *econtext;
736 TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;
737 CopyFromState cstate = festate->cstate;
738 ErrorContextCallback errcallback;
739
740 /* Set up callback to identify error line number. */
741 errcallback.callback = CopyFromErrorCallback;
742 errcallback.arg = cstate;
743 errcallback.previous = error_context_stack;
744 error_context_stack = &errcallback;
745
746 /*
747 * We pass ExprContext because there might be a use of the DEFAULT option
748 * in COPY FROM, so we may need to evaluate default expressions.
749 */
750 econtext = GetPerTupleExprContext(estate);
751
752retry:
753
754 /*
755 * DEFAULT expressions need to be evaluated in a per-tuple context, so
756 * switch in case we are doing that.
757 */
759
760 /*
761 * The protocol for loading a virtual tuple into a slot is first
762 * ExecClearTuple, then fill the values/isnull arrays, then
763 * ExecStoreVirtualTuple. If we don't find another row in the file, we
764 * just skip the last step, leaving the slot empty as required.
765 *
766 */
767 ExecClearTuple(slot);
768
769 if (NextCopyFrom(cstate, econtext, slot->tts_values, slot->tts_isnull))
770 {
771 if (cstate->opts.on_error == COPY_ON_ERROR_IGNORE &&
772 cstate->escontext->error_occurred)
773 {
774 /*
775 * Soft error occurred, skip this tuple and just make
776 * ErrorSaveContext ready for the next NextCopyFrom. Since we
777 * don't set details_wanted and error_data is not to be filled,
778 * just resetting error_occurred is enough.
779 */
780 cstate->escontext->error_occurred = false;
781
782 /* Switch back to original memory context */
783 MemoryContextSwitchTo(oldcontext);
784
785 /*
786 * Make sure we are interruptible while repeatedly calling
787 * NextCopyFrom() until no soft error occurs.
788 */
790
791 /*
792 * Reset the per-tuple exprcontext, to clean-up after expression
793 * evaluations etc.
794 */
796
797 if (cstate->opts.reject_limit > 0 &&
798 cstate->num_errors > cstate->opts.reject_limit)
800 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
801 errmsg("skipped more than REJECT_LIMIT (%" PRId64 ") rows due to data type incompatibility",
802 cstate->opts.reject_limit)));
803
804 /* Repeat NextCopyFrom() until no soft error occurs */
805 goto retry;
806 }
807
809 }
810
811 /* Switch back to original memory context */
812 MemoryContextSwitchTo(oldcontext);
813
814 /* Remove error callback. */
815 error_context_stack = errcallback.previous;
816
817 return slot;
818}
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1741
EState * CreateExecutorState(void)
Definition: execUtils.c:88
#define ResetPerTupleExprContext(estate)
Definition: executor.h:687
#define GetPerTupleExprContext(estate)
Definition: executor.h:678
#define GetPerTupleMemoryContext(estate)
Definition: executor.h:683
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
int64 reject_limit
Definition: copy.h:88
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1618
bool * tts_isnull
Definition: tuptable.h:127
Datum * tts_values
Definition: tuptable.h:125
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:458

References ErrorContextCallback::arg, ErrorContextCallback::callback, CHECK_FOR_INTERRUPTS, COPY_ON_ERROR_IGNORE, CopyFromErrorCallback(), CreateExecutorState(), FileFdwExecutionState::cstate, CurrentMemoryContext, ereport, errcode(), errmsg(), ERROR, error_context_stack, ErrorSaveContext::error_occurred, CopyFromStateData::escontext, ExecClearTuple(), ExecStoreVirtualTuple(), ForeignScanState::fdw_state, GetPerTupleExprContext, GetPerTupleMemoryContext, MemoryContextSwitchTo(), NextCopyFrom(), CopyFromStateData::num_errors, CopyFormatOptions::on_error, CopyFromStateData::opts, ErrorContextCallback::previous, CopyFormatOptions::reject_limit, ResetPerTupleExprContext, ForeignScanState::ss, ScanState::ss_ScanTupleSlot, TupleTableSlot::tts_isnull, and TupleTableSlot::tts_values.

Referenced by file_fdw_handler().

◆ fileReScanForeignScan()

static void fileReScanForeignScan ( ForeignScanState node)
static

◆ get_file_fdw_attribute_options()

static List * get_file_fdw_attribute_options ( Oid  relid)
static

Definition at line 450 of file file_fdw.c.

451{
452 Relation rel;
453 TupleDesc tupleDesc;
454 AttrNumber natts;
456 List *fnncolumns = NIL;
457 List *fncolumns = NIL;
458
459 List *options = NIL;
460
461 rel = table_open(relid, AccessShareLock);
462 tupleDesc = RelationGetDescr(rel);
463 natts = tupleDesc->natts;
464
465 /* Retrieve FDW options for all user-defined attributes. */
466 for (attnum = 1; attnum <= natts; attnum++)
467 {
468 Form_pg_attribute attr = TupleDescAttr(tupleDesc, attnum - 1);
469 List *column_options;
470 ListCell *lc;
471
472 /* Skip dropped attributes. */
473 if (attr->attisdropped)
474 continue;
475
476 column_options = GetForeignColumnOptions(relid, attnum);
477 foreach(lc, column_options)
478 {
479 DefElem *def = (DefElem *) lfirst(lc);
480
481 if (strcmp(def->defname, "force_not_null") == 0)
482 {
483 if (defGetBoolean(def))
484 {
485 char *attname = pstrdup(NameStr(attr->attname));
486
487 fnncolumns = lappend(fnncolumns, makeString(attname));
488 }
489 }
490 else if (strcmp(def->defname, "force_null") == 0)
491 {
492 if (defGetBoolean(def))
493 {
494 char *attname = pstrdup(NameStr(attr->attname));
495
496 fncolumns = lappend(fncolumns, makeString(attname));
497 }
498 }
499 /* maybe in future handle other column options here */
500 }
501 }
502
504
505 /*
506 * Return DefElem only when some column(s) have force_not_null /
507 * force_null options set
508 */
509 if (fnncolumns != NIL)
510 options = lappend(options, makeDefElem("force_not_null", (Node *) fnncolumns, -1));
511
512 if (fncolumns != NIL)
513 options = lappend(options, makeDefElem("force_null", (Node *) fncolumns, -1));
514
515 return options;
516}
List * GetForeignColumnOptions(Oid relid, AttrNumber attnum)
Definition: foreign.c:292

References AccessShareLock, attname, attnum, defGetBoolean(), DefElem::defname, GetForeignColumnOptions(), lappend(), lfirst, makeDefElem(), makeString(), NameStr, TupleDescData::natts, NIL, options, pstrdup(), RelationGetDescr, table_close(), table_open(), and TupleDescAttr().

Referenced by fileGetOptions().

◆ is_valid_option()

static bool is_valid_option ( const char *  option,
Oid  context 
)
static

Definition at line 360 of file file_fdw.c.

361{
362 const struct FileFdwOption *opt;
363
364 for (opt = valid_options; opt->optname; opt++)
365 {
366 if (context == opt->optcontext && strcmp(opt->optname, option) == 0)
367 return true;
368 }
369 return false;
370}

References FileFdwOption::optcontext, FileFdwOption::optname, and valid_options.

Referenced by file_fdw_validator().

◆ PG_FUNCTION_INFO_V1() [1/2]

PG_FUNCTION_INFO_V1 ( file_fdw_handler  )

◆ PG_FUNCTION_INFO_V1() [2/2]

PG_FUNCTION_INFO_V1 ( file_fdw_validator  )

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "file_fdw",
version = PG_VERSION 
)

Variable Documentation

◆ valid_options

const struct FileFdwOption valid_options[]
static
Initial value:
= {
{"filename", ForeignTableRelationId},
{"program", ForeignTableRelationId},
{"format", ForeignTableRelationId},
{"header", ForeignTableRelationId},
{"delimiter", ForeignTableRelationId},
{"quote", ForeignTableRelationId},
{"escape", ForeignTableRelationId},
{"null", ForeignTableRelationId},
{"default", ForeignTableRelationId},
{"encoding", ForeignTableRelationId},
{"on_error", ForeignTableRelationId},
{"log_verbosity", ForeignTableRelationId},
{"reject_limit", ForeignTableRelationId},
{"force_not_null", AttributeRelationId},
{"force_null", AttributeRelationId},
{NULL, InvalidOid}
}
#define InvalidOid
Definition: postgres_ext.h:35

Definition at line 68 of file file_fdw.c.

Referenced by file_fdw_validator(), and is_valid_option().