]> granicus.if.org Git - postgresql/blobdiff - contrib/file_fdw/file_fdw.c
Add macros to make AllocSetContextCreate() calls simpler and safer.
[postgresql] / contrib / file_fdw / file_fdw.c
index ea4ed4008296f2a7e8866808967caa48520c8d1c..b42de873e0f56ced19c15ed15a88431b7202ebb7 100644 (file)
@@ -3,7 +3,7 @@
  * file_fdw.c
  *               foreign-data wrapper for server-side flat files.
  *
- * Copyright (c) 2010-2015, PostgreSQL Global Development Group
+ * Copyright (c) 2010-2016, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
  *               contrib/file_fdw/file_fdw.c
@@ -121,7 +121,8 @@ static ForeignScan *fileGetForeignPlan(PlannerInfo *root,
                                   Oid foreigntableid,
                                   ForeignPath *best_path,
                                   List *tlist,
-                                  List *scan_clauses);
+                                  List *scan_clauses,
+                                  Plan *outer_plan);
 static void fileExplainForeignScan(ForeignScanState *node, ExplainState *es);
 static void fileBeginForeignScan(ForeignScanState *node, int eflags);
 static TupleTableSlot *fileIterateForeignScan(ForeignScanState *node);
@@ -130,6 +131,8 @@ static void fileEndForeignScan(ForeignScanState *node);
 static bool fileAnalyzeForeignTable(Relation relation,
                                                AcquireSampleRowsFunc *func,
                                                BlockNumber *totalpages);
+static bool fileIsForeignScanParallelSafe(PlannerInfo *root, RelOptInfo *rel,
+                                                         RangeTblEntry *rte);
 
 /*
  * Helper functions
@@ -169,6 +172,7 @@ file_fdw_handler(PG_FUNCTION_ARGS)
        fdwroutine->ReScanForeignScan = fileReScanForeignScan;
        fdwroutine->EndForeignScan = fileEndForeignScan;
        fdwroutine->AnalyzeForeignTable = fileAnalyzeForeignTable;
+       fdwroutine->IsForeignScanParallelSafe = fileIsForeignScanParallelSafe;
 
        PG_RETURN_POINTER(fdwroutine);
 }
@@ -520,11 +524,13 @@ fileGetForeignPaths(PlannerInfo *root,
         */
        add_path(baserel, (Path *)
                         create_foreignscan_path(root, baserel,
+                                                                        NULL,          /* default pathtarget */
                                                                         baserel->rows,
                                                                         startup_cost,
                                                                         total_cost,
                                                                         NIL,           /* no pathkeys */
                                                                         NULL,          /* no outer rel either */
+                                                                        NULL,          /* no extra plan */
                                                                         coptions));
 
        /*
@@ -544,7 +550,8 @@ fileGetForeignPlan(PlannerInfo *root,
                                   Oid foreigntableid,
                                   ForeignPath *best_path,
                                   List *tlist,
-                                  List *scan_clauses)
+                                  List *scan_clauses,
+                                  Plan *outer_plan)
 {
        Index           scan_relid = baserel->relid;
 
@@ -563,7 +570,9 @@ fileGetForeignPlan(PlannerInfo *root,
                                                        scan_relid,
                                                        NIL,    /* no expressions to evaluate */
                                                        best_path->fdw_private,
-                                                       NIL /* no custom tlist */ );
+                                                       NIL,    /* no custom tlist */
+                                                       NIL,    /* no remote quals */
+                                                       outer_plan);
 }
 
 /*
@@ -756,6 +765,18 @@ fileAnalyzeForeignTable(Relation relation,
        return true;
 }
 
+/*
+ * fileIsForeignScanParallelSafe
+ *             Reading a file in a parallel worker should work just the same as
+ *             reading it in the leader, so mark scans safe.
+ */
+static bool
+fileIsForeignScanParallelSafe(PlannerInfo *root, RelOptInfo *rel,
+                                                         RangeTblEntry *rte)
+{
+       return true;
+}
+
 /*
  * check_selective_binary_conversion
  *
@@ -801,7 +822,7 @@ check_selective_binary_conversion(RelOptInfo *baserel,
        }
 
        /* Collect all the attributes needed for joins or final output. */
-       pull_varattnos((Node *) baserel->reltargetlist, baserel->relid,
+       pull_varattnos((Node *) baserel->reltarget->exprs, baserel->relid,
                                   &attrs_used);
 
        /* Add all the attributes used by restriction clauses. */
@@ -933,7 +954,7 @@ estimate_size(PlannerInfo *root, RelOptInfo *baserel,
                 */
                int                     tuple_width;
 
-               tuple_width = MAXALIGN(baserel->width) +
+               tuple_width = MAXALIGN(baserel->reltarget->width) +
                        MAXALIGN(SizeofHeapTupleHeader);
                ntuples = clamp_row_est((double) stat_buf.st_size /
                                                                (double) tuple_width);
@@ -1040,9 +1061,7 @@ file_acquire_sample_rows(Relation onerel, int elevel,
         */
        tupcontext = AllocSetContextCreate(CurrentMemoryContext,
                                                                           "file_fdw temporary context",
-                                                                          ALLOCSET_DEFAULT_MINSIZE,
-                                                                          ALLOCSET_DEFAULT_INITSIZE,
-                                                                          ALLOCSET_DEFAULT_MAXSIZE);
+                                                                          ALLOCSET_DEFAULT_SIZES);
 
        /* Prepare for sampling rows */
        reservoir_init_selection_state(&rstate, targrows);
@@ -1097,7 +1116,7 @@ file_acquire_sample_rows(Relation onerel, int elevel,
                                 * Found a suitable tuple, so save it, replacing one old tuple
                                 * at random
                                 */
-                               int                     k = (int) (targrows * sampler_random_fract());
+                               int                     k = (int) (targrows * sampler_random_fract(rstate.randstate));
 
                                Assert(k >= 0 && k < targrows);
                                heap_freetuple(rows[k]);