LCOV - code coverage report
Current view: top level - contrib/postgres_fdw - postgres_fdw.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12beta2 Lines: 1710 1835 93.2 %
Date: 2019-06-18 07:06:57 Functions: 71 72 98.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * postgres_fdw.c
       4             :  *        Foreign-data wrapper for remote PostgreSQL servers
       5             :  *
       6             :  * Portions Copyright (c) 2012-2019, PostgreSQL Global Development Group
       7             :  *
       8             :  * IDENTIFICATION
       9             :  *        contrib/postgres_fdw/postgres_fdw.c
      10             :  *
      11             :  *-------------------------------------------------------------------------
      12             :  */
      13             : #include "postgres.h"
      14             : 
      15             : #include "postgres_fdw.h"
      16             : 
      17             : #include "access/htup_details.h"
      18             : #include "access/sysattr.h"
      19             : #include "access/table.h"
      20             : #include "catalog/pg_class.h"
      21             : #include "commands/defrem.h"
      22             : #include "commands/explain.h"
      23             : #include "commands/vacuum.h"
      24             : #include "foreign/fdwapi.h"
      25             : #include "funcapi.h"
      26             : #include "miscadmin.h"
      27             : #include "nodes/makefuncs.h"
      28             : #include "nodes/nodeFuncs.h"
      29             : #include "optimizer/clauses.h"
      30             : #include "optimizer/cost.h"
      31             : #include "optimizer/optimizer.h"
      32             : #include "optimizer/pathnode.h"
      33             : #include "optimizer/paths.h"
      34             : #include "optimizer/planmain.h"
      35             : #include "optimizer/restrictinfo.h"
      36             : #include "optimizer/tlist.h"
      37             : #include "parser/parsetree.h"
      38             : #include "utils/builtins.h"
      39             : #include "utils/float.h"
      40             : #include "utils/guc.h"
      41             : #include "utils/lsyscache.h"
      42             : #include "utils/memutils.h"
      43             : #include "utils/rel.h"
      44             : #include "utils/sampling.h"
      45             : #include "utils/selfuncs.h"
      46             : 
      47           2 : PG_MODULE_MAGIC;
      48             : 
      49             : /* Default CPU cost to start up a foreign query. */
      50             : #define DEFAULT_FDW_STARTUP_COST    100.0
      51             : 
      52             : /* Default CPU cost to process 1 row (above and beyond cpu_tuple_cost). */
      53             : #define DEFAULT_FDW_TUPLE_COST      0.01
      54             : 
      55             : /* If no remote estimates, assume a sort costs 20% extra */
      56             : #define DEFAULT_FDW_SORT_MULTIPLIER 1.2
      57             : 
      58             : /*
      59             :  * Indexes of FDW-private information stored in fdw_private lists.
      60             :  *
      61             :  * These items are indexed with the enum FdwScanPrivateIndex, so an item
      62             :  * can be fetched with list_nth().  For example, to get the SELECT statement:
      63             :  *      sql = strVal(list_nth(fdw_private, FdwScanPrivateSelectSql));
      64             :  */
      65             : enum FdwScanPrivateIndex
      66             : {
      67             :     /* SQL statement to execute remotely (as a String node) */
      68             :     FdwScanPrivateSelectSql,
      69             :     /* Integer list of attribute numbers retrieved by the SELECT */
      70             :     FdwScanPrivateRetrievedAttrs,
      71             :     /* Integer representing the desired fetch_size */
      72             :     FdwScanPrivateFetchSize,
      73             : 
      74             :     /*
      75             :      * String describing join i.e. names of relations being joined and types
      76             :      * of join, added when the scan is join
      77             :      */
      78             :     FdwScanPrivateRelations
      79             : };
      80             : 
      81             : /*
      82             :  * Similarly, this enum describes what's kept in the fdw_private list for
      83             :  * a ModifyTable node referencing a postgres_fdw foreign table.  We store:
      84             :  *
      85             :  * 1) INSERT/UPDATE/DELETE statement text to be sent to the remote server
      86             :  * 2) Integer list of target attribute numbers for INSERT/UPDATE
      87             :  *    (NIL for a DELETE)
      88             :  * 3) Boolean flag showing if the remote query has a RETURNING clause
      89             :  * 4) Integer list of attribute numbers retrieved by RETURNING, if any
      90             :  */
      91             : enum FdwModifyPrivateIndex
      92             : {
      93             :     /* SQL statement to execute remotely (as a String node) */
      94             :     FdwModifyPrivateUpdateSql,
      95             :     /* Integer list of target attribute numbers for INSERT/UPDATE */
      96             :     FdwModifyPrivateTargetAttnums,
      97             :     /* has-returning flag (as an integer Value node) */
      98             :     FdwModifyPrivateHasReturning,
      99             :     /* Integer list of attribute numbers retrieved by RETURNING */
     100             :     FdwModifyPrivateRetrievedAttrs
     101             : };
     102             : 
     103             : /*
     104             :  * Similarly, this enum describes what's kept in the fdw_private list for
     105             :  * a ForeignScan node that modifies a foreign table directly.  We store:
     106             :  *
     107             :  * 1) UPDATE/DELETE statement text to be sent to the remote server
     108             :  * 2) Boolean flag showing if the remote query has a RETURNING clause
     109             :  * 3) Integer list of attribute numbers retrieved by RETURNING, if any
     110             :  * 4) Boolean flag showing if we set the command es_processed
     111             :  */
     112             : enum FdwDirectModifyPrivateIndex
     113             : {
     114             :     /* SQL statement to execute remotely (as a String node) */
     115             :     FdwDirectModifyPrivateUpdateSql,
     116             :     /* has-returning flag (as an integer Value node) */
     117             :     FdwDirectModifyPrivateHasReturning,
     118             :     /* Integer list of attribute numbers retrieved by RETURNING */
     119             :     FdwDirectModifyPrivateRetrievedAttrs,
     120             :     /* set-processed flag (as an integer Value node) */
     121             :     FdwDirectModifyPrivateSetProcessed
     122             : };
     123             : 
     124             : /*
     125             :  * Execution state of a foreign scan using postgres_fdw.
     126             :  */
     127             : typedef struct PgFdwScanState
     128             : {
     129             :     Relation    rel;            /* relcache entry for the foreign table. NULL
     130             :                                  * for a foreign join scan. */
     131             :     TupleDesc   tupdesc;        /* tuple descriptor of scan */
     132             :     AttInMetadata *attinmeta;   /* attribute datatype conversion metadata */
     133             : 
     134             :     /* extracted fdw_private data */
     135             :     char       *query;          /* text of SELECT command */
     136             :     List       *retrieved_attrs;    /* list of retrieved attribute numbers */
     137             : 
     138             :     /* for remote query execution */
     139             :     PGconn     *conn;           /* connection for the scan */
     140             :     unsigned int cursor_number; /* quasi-unique ID for my cursor */
     141             :     bool        cursor_exists;  /* have we created the cursor? */
     142             :     int         numParams;      /* number of parameters passed to query */
     143             :     FmgrInfo   *param_flinfo;   /* output conversion functions for them */
     144             :     List       *param_exprs;    /* executable expressions for param values */
     145             :     const char **param_values;  /* textual values of query parameters */
     146             : 
     147             :     /* for storing result tuples */
     148             :     HeapTuple  *tuples;         /* array of currently-retrieved tuples */
     149             :     int         num_tuples;     /* # of tuples in array */
     150             :     int         next_tuple;     /* index of next one to return */
     151             : 
     152             :     /* batch-level state, for optimizing rewinds and avoiding useless fetch */
     153             :     int         fetch_ct_2;     /* Min(# of fetches done, 2) */
     154             :     bool        eof_reached;    /* true if last fetch reached EOF */
     155             : 
     156             :     /* working memory contexts */
     157             :     MemoryContext batch_cxt;    /* context holding current batch of tuples */
     158             :     MemoryContext temp_cxt;     /* context for per-tuple temporary data */
     159             : 
     160             :     int         fetch_size;     /* number of tuples per fetch */
     161             : } PgFdwScanState;
     162             : 
     163             : /*
     164             :  * Execution state of a foreign insert/update/delete operation.
     165             :  */
     166             : typedef struct PgFdwModifyState
     167             : {
     168             :     Relation    rel;            /* relcache entry for the foreign table */
     169             :     AttInMetadata *attinmeta;   /* attribute datatype conversion metadata */
     170             : 
     171             :     /* for remote query execution */
     172             :     PGconn     *conn;           /* connection for the scan */
     173             :     char       *p_name;         /* name of prepared statement, if created */
     174             : 
     175             :     /* extracted fdw_private data */
     176             :     char       *query;          /* text of INSERT/UPDATE/DELETE command */
     177             :     List       *target_attrs;   /* list of target attribute numbers */
     178             :     bool        has_returning;  /* is there a RETURNING clause? */
     179             :     List       *retrieved_attrs;    /* attr numbers retrieved by RETURNING */
     180             : 
     181             :     /* info about parameters for prepared statement */
     182             :     AttrNumber  ctidAttno;      /* attnum of input resjunk ctid column */
     183             :     int         p_nums;         /* number of parameters to transmit */
     184             :     FmgrInfo   *p_flinfo;       /* output conversion functions for them */
     185             : 
     186             :     /* working memory context */
     187             :     MemoryContext temp_cxt;     /* context for per-tuple temporary data */
     188             : 
     189             :     /* for update row movement if subplan result rel */
     190             :     struct PgFdwModifyState *aux_fmstate;   /* foreign-insert state, if
     191             :                                              * created */
     192             : } PgFdwModifyState;
     193             : 
     194             : /*
     195             :  * Execution state of a foreign scan that modifies a foreign table directly.
     196             :  */
     197             : typedef struct PgFdwDirectModifyState
     198             : {
     199             :     Relation    rel;            /* relcache entry for the foreign table */
     200             :     AttInMetadata *attinmeta;   /* attribute datatype conversion metadata */
     201             : 
     202             :     /* extracted fdw_private data */
     203             :     char       *query;          /* text of UPDATE/DELETE command */
     204             :     bool        has_returning;  /* is there a RETURNING clause? */
     205             :     List       *retrieved_attrs;    /* attr numbers retrieved by RETURNING */
     206             :     bool        set_processed;  /* do we set the command es_processed? */
     207             : 
     208             :     /* for remote query execution */
     209             :     PGconn     *conn;           /* connection for the update */
     210             :     int         numParams;      /* number of parameters passed to query */
     211             :     FmgrInfo   *param_flinfo;   /* output conversion functions for them */
     212             :     List       *param_exprs;    /* executable expressions for param values */
     213             :     const char **param_values;  /* textual values of query parameters */
     214             : 
     215             :     /* for storing result tuples */
     216             :     PGresult   *result;         /* result for query */
     217             :     int         num_tuples;     /* # of result tuples */
     218             :     int         next_tuple;     /* index of next one to return */
     219             :     Relation    resultRel;      /* relcache entry for the target relation */
     220             :     AttrNumber *attnoMap;       /* array of attnums of input user columns */
     221             :     AttrNumber  ctidAttno;      /* attnum of input ctid column */
     222             :     AttrNumber  oidAttno;       /* attnum of input oid column */
     223             :     bool        hasSystemCols;  /* are there system columns of resultRel? */
     224             : 
     225             :     /* working memory context */
     226             :     MemoryContext temp_cxt;     /* context for per-tuple temporary data */
     227             : } PgFdwDirectModifyState;
     228             : 
     229             : /*
     230             :  * Workspace for analyzing a foreign table.
     231             :  */
     232             : typedef struct PgFdwAnalyzeState
     233             : {
     234             :     Relation    rel;            /* relcache entry for the foreign table */
     235             :     AttInMetadata *attinmeta;   /* attribute datatype conversion metadata */
     236             :     List       *retrieved_attrs;    /* attr numbers retrieved by query */
     237             : 
     238             :     /* collected sample rows */
     239             :     HeapTuple  *rows;           /* array of size targrows */
     240             :     int         targrows;       /* target # of sample rows */
     241             :     int         numrows;        /* # of sample rows collected */
     242             : 
     243             :     /* for random sampling */
     244             :     double      samplerows;     /* # of rows fetched */
     245             :     double      rowstoskip;     /* # of rows to skip before next sample */
     246             :     ReservoirStateData rstate;  /* state for reservoir sampling */
     247             : 
     248             :     /* working memory contexts */
     249             :     MemoryContext anl_cxt;      /* context for per-analyze lifespan data */
     250             :     MemoryContext temp_cxt;     /* context for per-tuple temporary data */
     251             : } PgFdwAnalyzeState;
     252             : 
     253             : /*
     254             :  * This enum describes what's kept in the fdw_private list for a ForeignPath.
     255             :  * We store:
     256             :  *
     257             :  * 1) Boolean flag showing if the remote query has the final sort
     258             :  * 2) Boolean flag showing if the remote query has the LIMIT clause
     259             :  */
     260             : enum FdwPathPrivateIndex
     261             : {
     262             :     /* has-final-sort flag (as an integer Value node) */
     263             :     FdwPathPrivateHasFinalSort,
     264             :     /* has-limit flag (as an integer Value node) */
     265             :     FdwPathPrivateHasLimit
     266             : };
     267             : 
     268             : /* Struct for extra information passed to estimate_path_cost_size() */
     269             : typedef struct
     270             : {
     271             :     PathTarget *target;
     272             :     bool        has_final_sort;
     273             :     bool        has_limit;
     274             :     double      limit_tuples;
     275             :     int64       count_est;
     276             :     int64       offset_est;
     277             : } PgFdwPathExtraData;
     278             : 
     279             : /*
     280             :  * Identify the attribute where data conversion fails.
     281             :  */
     282             : typedef struct ConversionLocation
     283             : {
     284             :     Relation    rel;            /* foreign table's relcache entry. */
     285             :     AttrNumber  cur_attno;      /* attribute number being processed, or 0 */
     286             : 
     287             :     /*
     288             :      * In case of foreign join push down, fdw_scan_tlist is used to identify
     289             :      * the Var node corresponding to the error location and
     290             :      * fsstate->ss.ps.state gives access to the RTEs of corresponding relation
     291             :      * to get the relation name and attribute name.
     292             :      */
     293             :     ForeignScanState *fsstate;
     294             : } ConversionLocation;
     295             : 
     296             : /* Callback argument for ec_member_matches_foreign */
     297             : typedef struct
     298             : {
     299             :     Expr       *current;        /* current expr, or NULL if not yet found */
     300             :     List       *already_used;   /* expressions already dealt with */
     301             : } ec_member_foreign_arg;
     302             : 
     303             : /*
     304             :  * SQL functions
     305             :  */
     306           4 : PG_FUNCTION_INFO_V1(postgres_fdw_handler);
     307             : 
     308             : /*
     309             :  * FDW callback routines
     310             :  */
     311             : static void postgresGetForeignRelSize(PlannerInfo *root,
     312             :                                       RelOptInfo *baserel,
     313             :                                       Oid foreigntableid);
     314             : static void postgresGetForeignPaths(PlannerInfo *root,
     315             :                                     RelOptInfo *baserel,
     316             :                                     Oid foreigntableid);
     317             : static ForeignScan *postgresGetForeignPlan(PlannerInfo *root,
     318             :                                            RelOptInfo *foreignrel,
     319             :                                            Oid foreigntableid,
     320             :                                            ForeignPath *best_path,
     321             :                                            List *tlist,
     322             :                                            List *scan_clauses,
     323             :                                            Plan *outer_plan);
     324             : static void postgresBeginForeignScan(ForeignScanState *node, int eflags);
     325             : static TupleTableSlot *postgresIterateForeignScan(ForeignScanState *node);
     326             : static void postgresReScanForeignScan(ForeignScanState *node);
     327             : static void postgresEndForeignScan(ForeignScanState *node);
     328             : static void postgresAddForeignUpdateTargets(Query *parsetree,
     329             :                                             RangeTblEntry *target_rte,
     330             :                                             Relation target_relation);
     331             : static List *postgresPlanForeignModify(PlannerInfo *root,
     332             :                                        ModifyTable *plan,
     333             :                                        Index resultRelation,
     334             :                                        int subplan_index);
     335             : static void postgresBeginForeignModify(ModifyTableState *mtstate,
     336             :                                        ResultRelInfo *resultRelInfo,
     337             :                                        List *fdw_private,
     338             :                                        int subplan_index,
     339             :                                        int eflags);
     340             : static TupleTableSlot *postgresExecForeignInsert(EState *estate,
     341             :                                                  ResultRelInfo *resultRelInfo,
     342             :                                                  TupleTableSlot *slot,
     343             :                                                  TupleTableSlot *planSlot);
     344             : static TupleTableSlot *postgresExecForeignUpdate(EState *estate,
     345             :                                                  ResultRelInfo *resultRelInfo,
     346             :                                                  TupleTableSlot *slot,
     347             :                                                  TupleTableSlot *planSlot);
     348             : static TupleTableSlot *postgresExecForeignDelete(EState *estate,
     349             :                                                  ResultRelInfo *resultRelInfo,
     350             :                                                  TupleTableSlot *slot,
     351             :                                                  TupleTableSlot *planSlot);
     352             : static void postgresEndForeignModify(EState *estate,
     353             :                                      ResultRelInfo *resultRelInfo);
     354             : static void postgresBeginForeignInsert(ModifyTableState *mtstate,
     355             :                                        ResultRelInfo *resultRelInfo);
     356             : static void postgresEndForeignInsert(EState *estate,
     357             :                                      ResultRelInfo *resultRelInfo);
     358             : static int  postgresIsForeignRelUpdatable(Relation rel);
     359             : static bool postgresPlanDirectModify(PlannerInfo *root,
     360             :                                      ModifyTable *plan,
     361             :                                      Index resultRelation,
     362             :                                      int subplan_index);
     363             : static void postgresBeginDirectModify(ForeignScanState *node, int eflags);
     364             : static TupleTableSlot *postgresIterateDirectModify(ForeignScanState *node);
     365             : static void postgresEndDirectModify(ForeignScanState *node);
     366             : static void postgresExplainForeignScan(ForeignScanState *node,
     367             :                                        ExplainState *es);
     368             : static void postgresExplainForeignModify(ModifyTableState *mtstate,
     369             :                                          ResultRelInfo *rinfo,
     370             :                                          List *fdw_private,
     371             :                                          int subplan_index,
     372             :                                          ExplainState *es);
     373             : static void postgresExplainDirectModify(ForeignScanState *node,
     374             :                                         ExplainState *es);
     375             : static bool postgresAnalyzeForeignTable(Relation relation,
     376             :                                         AcquireSampleRowsFunc *func,
     377             :                                         BlockNumber *totalpages);
     378             : static List *postgresImportForeignSchema(ImportForeignSchemaStmt *stmt,
     379             :                                          Oid serverOid);
     380             : static void postgresGetForeignJoinPaths(PlannerInfo *root,
     381             :                                         RelOptInfo *joinrel,
     382             :                                         RelOptInfo *outerrel,
     383             :                                         RelOptInfo *innerrel,
     384             :                                         JoinType jointype,
     385             :                                         JoinPathExtraData *extra);
     386             : static bool postgresRecheckForeignScan(ForeignScanState *node,
     387             :                                        TupleTableSlot *slot);
     388             : static void postgresGetForeignUpperPaths(PlannerInfo *root,
     389             :                                          UpperRelationKind stage,
     390             :                                          RelOptInfo *input_rel,
     391             :                                          RelOptInfo *output_rel,
     392             :                                          void *extra);
     393             : 
     394             : /*
     395             :  * Helper functions
     396             :  */
     397             : static void estimate_path_cost_size(PlannerInfo *root,
     398             :                                     RelOptInfo *foreignrel,
     399             :                                     List *param_join_conds,
     400             :                                     List *pathkeys,
     401             :                                     PgFdwPathExtraData *fpextra,
     402             :                                     double *p_rows, int *p_width,
     403             :                                     Cost *p_startup_cost, Cost *p_total_cost);
     404             : static void get_remote_estimate(const char *sql,
     405             :                                 PGconn *conn,
     406             :                                 double *rows,
     407             :                                 int *width,
     408             :                                 Cost *startup_cost,
     409             :                                 Cost *total_cost);
     410             : static void adjust_foreign_grouping_path_cost(PlannerInfo *root,
     411             :                                               List *pathkeys,
     412             :                                               double retrieved_rows,
     413             :                                               double width,
     414             :                                               double limit_tuples,
     415             :                                               Cost *p_startup_cost,
     416             :                                               Cost *p_run_cost);
     417             : static bool ec_member_matches_foreign(PlannerInfo *root, RelOptInfo *rel,
     418             :                                       EquivalenceClass *ec, EquivalenceMember *em,
     419             :                                       void *arg);
     420             : static void create_cursor(ForeignScanState *node);
     421             : static void fetch_more_data(ForeignScanState *node);
     422             : static void close_cursor(PGconn *conn, unsigned int cursor_number);
     423             : static PgFdwModifyState *create_foreign_modify(EState *estate,
     424             :                                                RangeTblEntry *rte,
     425             :                                                ResultRelInfo *resultRelInfo,
     426             :                                                CmdType operation,
     427             :                                                Plan *subplan,
     428             :                                                char *query,
     429             :                                                List *target_attrs,
     430             :                                                bool has_returning,
     431             :                                                List *retrieved_attrs);
     432             : static TupleTableSlot *execute_foreign_modify(EState *estate,
     433             :                                               ResultRelInfo *resultRelInfo,
     434             :                                               CmdType operation,
     435             :                                               TupleTableSlot *slot,
     436             :                                               TupleTableSlot *planSlot);
     437             : static void prepare_foreign_modify(PgFdwModifyState *fmstate);
     438             : static const char **convert_prep_stmt_params(PgFdwModifyState *fmstate,
     439             :                                              ItemPointer tupleid,
     440             :                                              TupleTableSlot *slot);
     441             : static void store_returning_result(PgFdwModifyState *fmstate,
     442             :                                    TupleTableSlot *slot, PGresult *res);
     443             : static void finish_foreign_modify(PgFdwModifyState *fmstate);
     444             : static List *build_remote_returning(Index rtindex, Relation rel,
     445             :                                     List *returningList);
     446             : static void rebuild_fdw_scan_tlist(ForeignScan *fscan, List *tlist);
     447             : static void execute_dml_stmt(ForeignScanState *node);
     448             : static TupleTableSlot *get_returning_data(ForeignScanState *node);
     449             : static void init_returning_filter(PgFdwDirectModifyState *dmstate,
     450             :                                   List *fdw_scan_tlist,
     451             :                                   Index rtindex);
     452             : static TupleTableSlot *apply_returning_filter(PgFdwDirectModifyState *dmstate,
     453             :                                               TupleTableSlot *slot,
     454             :                                               EState *estate);
     455             : static void prepare_query_params(PlanState *node,
     456             :                                  List *fdw_exprs,
     457             :                                  int numParams,
     458             :                                  FmgrInfo **param_flinfo,
     459             :                                  List **param_exprs,
     460             :                                  const char ***param_values);
     461             : static void process_query_params(ExprContext *econtext,
     462             :                                  FmgrInfo *param_flinfo,
     463             :                                  List *param_exprs,
     464             :                                  const char **param_values);
     465             : static int  postgresAcquireSampleRowsFunc(Relation relation, int elevel,
     466             :                                           HeapTuple *rows, int targrows,
     467             :                                           double *totalrows,
     468             :                                           double *totaldeadrows);
     469             : static void analyze_row_processor(PGresult *res, int row,
     470             :                                   PgFdwAnalyzeState *astate);
     471             : static HeapTuple make_tuple_from_result_row(PGresult *res,
     472             :                                             int row,
     473             :                                             Relation rel,
     474             :                                             AttInMetadata *attinmeta,
     475             :                                             List *retrieved_attrs,
     476             :                                             ForeignScanState *fsstate,
     477             :                                             MemoryContext temp_context);
     478             : static void conversion_error_callback(void *arg);
     479             : static bool foreign_join_ok(PlannerInfo *root, RelOptInfo *joinrel,
     480             :                             JoinType jointype, RelOptInfo *outerrel, RelOptInfo *innerrel,
     481             :                             JoinPathExtraData *extra);
     482             : static bool foreign_grouping_ok(PlannerInfo *root, RelOptInfo *grouped_rel,
     483             :                                 Node *havingQual);
     484             : static List *get_useful_pathkeys_for_relation(PlannerInfo *root,
     485             :                                               RelOptInfo *rel);
     486             : static List *get_useful_ecs_for_relation(PlannerInfo *root, RelOptInfo *rel);
     487             : static void add_paths_with_pathkeys_for_rel(PlannerInfo *root, RelOptInfo *rel,
     488             :                                             Path *epq_path);
     489             : static void add_foreign_grouping_paths(PlannerInfo *root,
     490             :                                        RelOptInfo *input_rel,
     491             :                                        RelOptInfo *grouped_rel,
     492             :                                        GroupPathExtraData *extra);
     493             : static void add_foreign_ordered_paths(PlannerInfo *root,
     494             :                                       RelOptInfo *input_rel,
     495             :                                       RelOptInfo *ordered_rel);
     496             : static void add_foreign_final_paths(PlannerInfo *root,
     497             :                                     RelOptInfo *input_rel,
     498             :                                     RelOptInfo *final_rel,
     499             :                                     FinalPathExtraData *extra);
     500             : static void apply_server_options(PgFdwRelationInfo *fpinfo);
     501             : static void apply_table_options(PgFdwRelationInfo *fpinfo);
     502             : static void merge_fdw_options(PgFdwRelationInfo *fpinfo,
     503             :                               const PgFdwRelationInfo *fpinfo_o,
     504             :                               const PgFdwRelationInfo *fpinfo_i);
     505             : 
     506             : 
     507             : /*
     508             :  * Foreign-data wrapper handler function: return a struct with pointers
     509             :  * to my callback routines.
     510             :  */
     511             : Datum
     512         876 : postgres_fdw_handler(PG_FUNCTION_ARGS)
     513             : {
     514         876 :     FdwRoutine *routine = makeNode(FdwRoutine);
     515             : 
     516             :     /* Functions for scanning foreign tables */
     517         876 :     routine->GetForeignRelSize = postgresGetForeignRelSize;
     518         876 :     routine->GetForeignPaths = postgresGetForeignPaths;
     519         876 :     routine->GetForeignPlan = postgresGetForeignPlan;
     520         876 :     routine->BeginForeignScan = postgresBeginForeignScan;
     521         876 :     routine->IterateForeignScan = postgresIterateForeignScan;
     522         876 :     routine->ReScanForeignScan = postgresReScanForeignScan;
     523         876 :     routine->EndForeignScan = postgresEndForeignScan;
     524             : 
     525             :     /* Functions for updating foreign tables */
     526         876 :     routine->AddForeignUpdateTargets = postgresAddForeignUpdateTargets;
     527         876 :     routine->PlanForeignModify = postgresPlanForeignModify;
     528         876 :     routine->BeginForeignModify = postgresBeginForeignModify;
     529         876 :     routine->ExecForeignInsert = postgresExecForeignInsert;
     530         876 :     routine->ExecForeignUpdate = postgresExecForeignUpdate;
     531         876 :     routine->ExecForeignDelete = postgresExecForeignDelete;
     532         876 :     routine->EndForeignModify = postgresEndForeignModify;
     533         876 :     routine->BeginForeignInsert = postgresBeginForeignInsert;
     534         876 :     routine->EndForeignInsert = postgresEndForeignInsert;
     535         876 :     routine->IsForeignRelUpdatable = postgresIsForeignRelUpdatable;
     536         876 :     routine->PlanDirectModify = postgresPlanDirectModify;
     537         876 :     routine->BeginDirectModify = postgresBeginDirectModify;
     538         876 :     routine->IterateDirectModify = postgresIterateDirectModify;
     539         876 :     routine->EndDirectModify = postgresEndDirectModify;
     540             : 
     541             :     /* Function for EvalPlanQual rechecks */
     542         876 :     routine->RecheckForeignScan = postgresRecheckForeignScan;
     543             :     /* Support functions for EXPLAIN */
     544         876 :     routine->ExplainForeignScan = postgresExplainForeignScan;
     545         876 :     routine->ExplainForeignModify = postgresExplainForeignModify;
     546         876 :     routine->ExplainDirectModify = postgresExplainDirectModify;
     547             : 
     548             :     /* Support functions for ANALYZE */
     549         876 :     routine->AnalyzeForeignTable = postgresAnalyzeForeignTable;
     550             : 
     551             :     /* Support functions for IMPORT FOREIGN SCHEMA */
     552         876 :     routine->ImportForeignSchema = postgresImportForeignSchema;
     553             : 
     554             :     /* Support functions for join push-down */
     555         876 :     routine->GetForeignJoinPaths = postgresGetForeignJoinPaths;
     556             : 
     557             :     /* Support functions for upper relation push-down */
     558         876 :     routine->GetForeignUpperPaths = postgresGetForeignUpperPaths;
     559             : 
     560         876 :     PG_RETURN_POINTER(routine);
     561             : }
     562             : 
     563             : /*
     564             :  * postgresGetForeignRelSize
     565             :  *      Estimate # of rows and width of the result of the scan
     566             :  *
     567             :  * We should consider the effect of all baserestrictinfo clauses here, but
     568             :  * not any join clauses.
     569             :  */
     570             : static void
     571        1716 : postgresGetForeignRelSize(PlannerInfo *root,
     572             :                           RelOptInfo *baserel,
     573             :                           Oid foreigntableid)
     574             : {
     575             :     PgFdwRelationInfo *fpinfo;
     576             :     ListCell   *lc;
     577        1716 :     RangeTblEntry *rte = planner_rt_fetch(baserel->relid, root);
     578             :     const char *namespace;
     579             :     const char *relname;
     580             :     const char *refname;
     581             : 
     582             :     /*
     583             :      * We use PgFdwRelationInfo to pass various information to subsequent
     584             :      * functions.
     585             :      */
     586        1716 :     fpinfo = (PgFdwRelationInfo *) palloc0(sizeof(PgFdwRelationInfo));
     587        1716 :     baserel->fdw_private = (void *) fpinfo;
     588             : 
     589             :     /* Base foreign tables need to be pushed down always. */
     590        1716 :     fpinfo->pushdown_safe = true;
     591             : 
     592             :     /* Look up foreign-table catalog info. */
     593        1716 :     fpinfo->table = GetForeignTable(foreigntableid);
     594        1716 :     fpinfo->server = GetForeignServer(fpinfo->table->serverid);
     595             : 
     596             :     /*
     597             :      * Extract user-settable option values.  Note that per-table setting of
     598             :      * use_remote_estimate overrides per-server setting.
     599             :      */
     600        1716 :     fpinfo->use_remote_estimate = false;
     601        1716 :     fpinfo->fdw_startup_cost = DEFAULT_FDW_STARTUP_COST;
     602        1716 :     fpinfo->fdw_tuple_cost = DEFAULT_FDW_TUPLE_COST;
     603        1716 :     fpinfo->shippable_extensions = NIL;
     604        1716 :     fpinfo->fetch_size = 100;
     605             : 
     606        1716 :     apply_server_options(fpinfo);
     607        1716 :     apply_table_options(fpinfo);
     608             : 
     609             :     /*
     610             :      * If the table or the server is configured to use remote estimates,
     611             :      * identify which user to do remote access as during planning.  This
     612             :      * should match what ExecCheckRTEPerms() does.  If we fail due to lack of
     613             :      * permissions, the query would have failed at runtime anyway.
     614             :      */
     615        1716 :     if (fpinfo->use_remote_estimate)
     616             :     {
     617         478 :         Oid         userid = rte->checkAsUser ? rte->checkAsUser : GetUserId();
     618             : 
     619         478 :         fpinfo->user = GetUserMapping(userid, fpinfo->server->serverid);
     620             :     }
     621             :     else
     622        1238 :         fpinfo->user = NULL;
     623             : 
     624             :     /*
     625             :      * Identify which baserestrictinfo clauses can be sent to the remote
     626             :      * server and which can't.
     627             :      */
     628        1716 :     classifyConditions(root, baserel, baserel->baserestrictinfo,
     629             :                        &fpinfo->remote_conds, &fpinfo->local_conds);
     630             : 
     631             :     /*
     632             :      * Identify which attributes will need to be retrieved from the remote
     633             :      * server.  These include all attrs needed for joins or final output, plus
     634             :      * all attrs used in the local_conds.  (Note: if we end up using a
     635             :      * parameterized scan, it's possible that some of the join clauses will be
     636             :      * sent to the remote and thus we wouldn't really need to retrieve the
     637             :      * columns used in them.  Doesn't seem worth detecting that case though.)
     638             :      */
     639        1716 :     fpinfo->attrs_used = NULL;
     640        1716 :     pull_varattnos((Node *) baserel->reltarget->exprs, baserel->relid,
     641             :                    &fpinfo->attrs_used);
     642        1778 :     foreach(lc, fpinfo->local_conds)
     643             :     {
     644          62 :         RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
     645             : 
     646          62 :         pull_varattnos((Node *) rinfo->clause, baserel->relid,
     647             :                        &fpinfo->attrs_used);
     648             :     }
     649             : 
     650             :     /*
     651             :      * Compute the selectivity and cost of the local_conds, so we don't have
     652             :      * to do it over again for each path.  The best we can do for these
     653             :      * conditions is to estimate selectivity on the basis of local statistics.
     654             :      */
     655        1716 :     fpinfo->local_conds_sel = clauselist_selectivity(root,
     656             :                                                      fpinfo->local_conds,
     657        1716 :                                                      baserel->relid,
     658             :                                                      JOIN_INNER,
     659             :                                                      NULL);
     660             : 
     661        1716 :     cost_qual_eval(&fpinfo->local_conds_cost, fpinfo->local_conds, root);
     662             : 
     663             :     /*
     664             :      * Set # of retrieved rows and cached relation costs to some negative
     665             :      * value, so that we can detect when they are set to some sensible values,
     666             :      * during one (usually the first) of the calls to estimate_path_cost_size.
     667             :      */
     668        1716 :     fpinfo->retrieved_rows = -1;
     669        1716 :     fpinfo->rel_startup_cost = -1;
     670        1716 :     fpinfo->rel_total_cost = -1;
     671             : 
     672             :     /*
     673             :      * If the table or the server is configured to use remote estimates,
     674             :      * connect to the foreign server and execute EXPLAIN to estimate the
     675             :      * number of rows selected by the restriction clauses, as well as the
     676             :      * average row width.  Otherwise, estimate using whatever statistics we
     677             :      * have locally, in a way similar to ordinary tables.
     678             :      */
     679        1716 :     if (fpinfo->use_remote_estimate)
     680             :     {
     681             :         /*
     682             :          * Get cost/size estimates with help of remote server.  Save the
     683             :          * values in fpinfo so we don't need to do it again to generate the
     684             :          * basic foreign path.
     685             :          */
     686         478 :         estimate_path_cost_size(root, baserel, NIL, NIL, NULL,
     687             :                                 &fpinfo->rows, &fpinfo->width,
     688             :                                 &fpinfo->startup_cost, &fpinfo->total_cost);
     689             : 
     690             :         /* Report estimated baserel size to planner. */
     691         478 :         baserel->rows = fpinfo->rows;
     692         478 :         baserel->reltarget->width = fpinfo->width;
     693             :     }
     694             :     else
     695             :     {
     696             :         /*
     697             :          * If the foreign table has never been ANALYZEd, it will have relpages
     698             :          * and reltuples equal to zero, which most likely has nothing to do
     699             :          * with reality.  We can't do a whole lot about that if we're not
     700             :          * allowed to consult the remote server, but we can use a hack similar
     701             :          * to plancat.c's treatment of empty relations: use a minimum size
     702             :          * estimate of 10 pages, and divide by the column-datatype-based width
     703             :          * estimate to get the corresponding number of tuples.
     704             :          */
     705        1238 :         if (baserel->pages == 0 && baserel->tuples == 0)
     706             :         {
     707         410 :             baserel->pages = 10;
     708         410 :             baserel->tuples =
     709         410 :                 (10 * BLCKSZ) / (baserel->reltarget->width +
     710             :                                  MAXALIGN(SizeofHeapTupleHeader));
     711             :         }
     712             : 
     713             :         /* Estimate baserel size as best we can with local statistics. */
     714        1238 :         set_baserel_size_estimates(root, baserel);
     715             : 
     716             :         /* Fill in basically-bogus cost estimates for use later. */
     717        1238 :         estimate_path_cost_size(root, baserel, NIL, NIL, NULL,
     718             :                                 &fpinfo->rows, &fpinfo->width,
     719             :                                 &fpinfo->startup_cost, &fpinfo->total_cost);
     720             :     }
     721             : 
     722             :     /*
     723             :      * Set the name of relation in fpinfo, while we are constructing it here.
     724             :      * It will be used to build the string describing the join relation in
     725             :      * EXPLAIN output. We can't know whether VERBOSE option is specified or
     726             :      * not, so always schema-qualify the foreign table name.
     727             :      */
     728        1716 :     fpinfo->relation_name = makeStringInfo();
     729        1716 :     namespace = get_namespace_name(get_rel_namespace(foreigntableid));
     730        1716 :     relname = get_rel_name(foreigntableid);
     731        1716 :     refname = rte->eref->aliasname;
     732        1716 :     appendStringInfo(fpinfo->relation_name, "%s.%s",
     733             :                      quote_identifier(namespace),
     734             :                      quote_identifier(relname));
     735        1716 :     if (*refname && strcmp(refname, relname) != 0)
     736        1042 :         appendStringInfo(fpinfo->relation_name, " %s",
     737        1042 :                          quote_identifier(rte->eref->aliasname));
     738             : 
     739             :     /* No outer and inner relations. */
     740        1716 :     fpinfo->make_outerrel_subquery = false;
     741        1716 :     fpinfo->make_innerrel_subquery = false;
     742        1716 :     fpinfo->lower_subquery_rels = NULL;
     743             :     /* Set the relation index. */
     744        1716 :     fpinfo->relation_index = baserel->relid;
     745        1716 : }
     746             : 
     747             : /*
     748             :  * get_useful_ecs_for_relation
     749             :  *      Determine which EquivalenceClasses might be involved in useful
     750             :  *      orderings of this relation.
     751             :  *
     752             :  * This function is in some respects a mirror image of the core function
     753             :  * pathkeys_useful_for_merging: for a regular table, we know what indexes
     754             :  * we have and want to test whether any of them are useful.  For a foreign
     755             :  * table, we don't know what indexes are present on the remote side but
     756             :  * want to speculate about which ones we'd like to use if they existed.
     757             :  *
     758             :  * This function returns a list of potentially-useful equivalence classes,
     759             :  * but it does not guarantee that an EquivalenceMember exists which contains
     760             :  * Vars only from the given relation.  For example, given ft1 JOIN t1 ON
     761             :  * ft1.x + t1.x = 0, this function will say that the equivalence class
     762             :  * containing ft1.x + t1.x is potentially useful.  Supposing ft1 is remote and
     763             :  * t1 is local (or on a different server), it will turn out that no useful
     764             :  * ORDER BY clause can be generated.  It's not our job to figure that out
     765             :  * here; we're only interested in identifying relevant ECs.
     766             :  */
     767             : static List *
     768         760 : get_useful_ecs_for_relation(PlannerInfo *root, RelOptInfo *rel)
     769             : {
     770         760 :     List       *useful_eclass_list = NIL;
     771             :     ListCell   *lc;
     772             :     Relids      relids;
     773             : 
     774             :     /*
     775             :      * First, consider whether any active EC is potentially useful for a merge
     776             :      * join against this relation.
     777             :      */
     778         760 :     if (rel->has_eclass_joins)
     779             :     {
     780         762 :         foreach(lc, root->eq_classes)
     781             :         {
     782         520 :             EquivalenceClass *cur_ec = (EquivalenceClass *) lfirst(lc);
     783             : 
     784         520 :             if (eclass_useful_for_merging(root, cur_ec, rel))
     785         274 :                 useful_eclass_list = lappend(useful_eclass_list, cur_ec);
     786             :         }
     787             :     }
     788             : 
     789             :     /*
     790             :      * Next, consider whether there are any non-EC derivable join clauses that
     791             :      * are merge-joinable.  If the joininfo list is empty, we can exit
     792             :      * quickly.
     793             :      */
     794         760 :     if (rel->joininfo == NIL)
     795         518 :         return useful_eclass_list;
     796             : 
     797             :     /* If this is a child rel, we must use the topmost parent rel to search. */
     798         242 :     if (IS_OTHER_REL(rel))
     799             :     {
     800             :         Assert(!bms_is_empty(rel->top_parent_relids));
     801          40 :         relids = rel->top_parent_relids;
     802             :     }
     803             :     else
     804         202 :         relids = rel->relids;
     805             : 
     806             :     /* Check each join clause in turn. */
     807         556 :     foreach(lc, rel->joininfo)
     808             :     {
     809         314 :         RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
     810             : 
     811             :         /* Consider only mergejoinable clauses */
     812         314 :         if (restrictinfo->mergeopfamilies == NIL)
     813          18 :             continue;
     814             : 
     815             :         /* Make sure we've got canonical ECs. */
     816         296 :         update_mergeclause_eclasses(root, restrictinfo);
     817             : 
     818             :         /*
     819             :          * restrictinfo->mergeopfamilies != NIL is sufficient to guarantee
     820             :          * that left_ec and right_ec will be initialized, per comments in
     821             :          * distribute_qual_to_rels.
     822             :          *
     823             :          * We want to identify which side of this merge-joinable clause
     824             :          * contains columns from the relation produced by this RelOptInfo. We
     825             :          * test for overlap, not containment, because there could be extra
     826             :          * relations on either side.  For example, suppose we've got something
     827             :          * like ((A JOIN B ON A.x = B.x) JOIN C ON A.y = C.y) LEFT JOIN D ON
     828             :          * A.y = D.y.  The input rel might be the joinrel between A and B, and
     829             :          * we'll consider the join clause A.y = D.y. relids contains a
     830             :          * relation not involved in the join class (B) and the equivalence
     831             :          * class for the left-hand side of the clause contains a relation not
     832             :          * involved in the input rel (C).  Despite the fact that we have only
     833             :          * overlap and not containment in either direction, A.y is potentially
     834             :          * useful as a sort column.
     835             :          *
     836             :          * Note that it's even possible that relids overlaps neither side of
     837             :          * the join clause.  For example, consider A LEFT JOIN B ON A.x = B.x
     838             :          * AND A.x = 1.  The clause A.x = 1 will appear in B's joininfo list,
     839             :          * but overlaps neither side of B.  In that case, we just skip this
     840             :          * join clause, since it doesn't suggest a useful sort order for this
     841             :          * relation.
     842             :          */
     843         296 :         if (bms_overlap(relids, restrictinfo->right_ec->ec_relids))
     844         120 :             useful_eclass_list = list_append_unique_ptr(useful_eclass_list,
     845         120 :                                                         restrictinfo->right_ec);
     846         176 :         else if (bms_overlap(relids, restrictinfo->left_ec->ec_relids))
     847         160 :             useful_eclass_list = list_append_unique_ptr(useful_eclass_list,
     848         160 :                                                         restrictinfo->left_ec);
     849             :     }
     850             : 
     851         242 :     return useful_eclass_list;
     852             : }
     853             : 
     854             : /*
     855             :  * get_useful_pathkeys_for_relation
     856             :  *      Determine which orderings of a relation might be useful.
     857             :  *
     858             :  * Getting data in sorted order can be useful either because the requested
     859             :  * order matches the final output ordering for the overall query we're
     860             :  * planning, or because it enables an efficient merge join.  Here, we try
     861             :  * to figure out which pathkeys to consider.
     862             :  */
     863             : static List *
     864        2142 : get_useful_pathkeys_for_relation(PlannerInfo *root, RelOptInfo *rel)
     865             : {
     866        2142 :     List       *useful_pathkeys_list = NIL;
     867             :     List       *useful_eclass_list;
     868        2142 :     PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) rel->fdw_private;
     869        2142 :     EquivalenceClass *query_ec = NULL;
     870             :     ListCell   *lc;
     871             : 
     872             :     /*
     873             :      * Pushing the query_pathkeys to the remote server is always worth
     874             :      * considering, because it might let us avoid a local sort.
     875             :      */
     876        2142 :     fpinfo->qp_is_pushdown_safe = false;
     877        2142 :     if (root->query_pathkeys)
     878             :     {
     879         836 :         bool        query_pathkeys_ok = true;
     880             : 
     881        1704 :         foreach(lc, root->query_pathkeys)
     882             :         {
     883        1194 :             PathKey    *pathkey = (PathKey *) lfirst(lc);
     884        1194 :             EquivalenceClass *pathkey_ec = pathkey->pk_eclass;
     885             :             Expr       *em_expr;
     886             : 
     887             :             /*
     888             :              * The planner and executor don't have any clever strategy for
     889             :              * taking data sorted by a prefix of the query's pathkeys and
     890             :              * getting it to be sorted by all of those pathkeys. We'll just
     891             :              * end up resorting the entire data set.  So, unless we can push
     892             :              * down all of the query pathkeys, forget it.
     893             :              *
     894             :              * is_foreign_expr would detect volatile expressions as well, but
     895             :              * checking ec_has_volatile here saves some cycles.
     896             :              */
     897        1194 :             if (pathkey_ec->ec_has_volatile ||
     898         894 :                 !(em_expr = find_em_expr_for_rel(pathkey_ec, rel)) ||
     899         894 :                 !is_foreign_expr(root, rel, em_expr))
     900             :             {
     901         326 :                 query_pathkeys_ok = false;
     902         326 :                 break;
     903             :             }
     904             :         }
     905             : 
     906         836 :         if (query_pathkeys_ok)
     907             :         {
     908         510 :             useful_pathkeys_list = list_make1(list_copy(root->query_pathkeys));
     909         510 :             fpinfo->qp_is_pushdown_safe = true;
     910             :         }
     911             :     }
     912             : 
     913             :     /*
     914             :      * Even if we're not using remote estimates, having the remote side do the
     915             :      * sort generally won't be any worse than doing it locally, and it might
     916             :      * be much better if the remote side can generate data in the right order
     917             :      * without needing a sort at all.  However, what we're going to do next is
     918             :      * try to generate pathkeys that seem promising for possible merge joins,
     919             :      * and that's more speculative.  A wrong choice might hurt quite a bit, so
     920             :      * bail out if we can't use remote estimates.
     921             :      */
     922        2142 :     if (!fpinfo->use_remote_estimate)
     923        1382 :         return useful_pathkeys_list;
     924             : 
     925             :     /* Get the list of interesting EquivalenceClasses. */
     926         760 :     useful_eclass_list = get_useful_ecs_for_relation(root, rel);
     927             : 
     928             :     /* Extract unique EC for query, if any, so we don't consider it again. */
     929         760 :     if (list_length(root->query_pathkeys) == 1)
     930             :     {
     931         108 :         PathKey    *query_pathkey = linitial(root->query_pathkeys);
     932             : 
     933         108 :         query_ec = query_pathkey->pk_eclass;
     934             :     }
     935             : 
     936             :     /*
     937             :      * As a heuristic, the only pathkeys we consider here are those of length
     938             :      * one.  It's surely possible to consider more, but since each one we
     939             :      * choose to consider will generate a round-trip to the remote side, we
     940             :      * need to be a bit cautious here.  It would sure be nice to have a local
     941             :      * cache of information about remote index definitions...
     942             :      */
     943        1262 :     foreach(lc, useful_eclass_list)
     944             :     {
     945         502 :         EquivalenceClass *cur_ec = lfirst(lc);
     946             :         Expr       *em_expr;
     947             :         PathKey    *pathkey;
     948             : 
     949             :         /* If redundant with what we did above, skip it. */
     950         502 :         if (cur_ec == query_ec)
     951          22 :             continue;
     952             : 
     953             :         /* If no pushable expression for this rel, skip it. */
     954         480 :         em_expr = find_em_expr_for_rel(cur_ec, rel);
     955         480 :         if (em_expr == NULL || !is_foreign_expr(root, rel, em_expr))
     956          34 :             continue;
     957             : 
     958             :         /* Looks like we can generate a pathkey, so let's do it. */
     959         446 :         pathkey = make_canonical_pathkey(root, cur_ec,
     960         446 :                                          linitial_oid(cur_ec->ec_opfamilies),
     961             :                                          BTLessStrategyNumber,
     962             :                                          false);
     963         446 :         useful_pathkeys_list = lappend(useful_pathkeys_list,
     964         446 :                                        list_make1(pathkey));
     965             :     }
     966             : 
     967         760 :     return useful_pathkeys_list;
     968             : }
     969             : 
     970             : /*
     971             :  * postgresGetForeignPaths
     972             :  *      Create possible scan paths for a scan on the foreign table
     973             :  */
     974             : static void
     975        1716 : postgresGetForeignPaths(PlannerInfo *root,
     976             :                         RelOptInfo *baserel,
     977             :                         Oid foreigntableid)
     978             : {
     979        1716 :     PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) baserel->fdw_private;
     980             :     ForeignPath *path;
     981             :     List       *ppi_list;
     982             :     ListCell   *lc;
     983             : 
     984             :     /*
     985             :      * Create simplest ForeignScan path node and add it to baserel.  This path
     986             :      * corresponds to SeqScan path of regular tables (though depending on what
     987             :      * baserestrict conditions we were able to send to remote, there might
     988             :      * actually be an indexscan happening there).  We already did all the work
     989             :      * to estimate cost and size of this path.
     990             :      *
     991             :      * Although this path uses no join clauses, it could still have required
     992             :      * parameterization due to LATERAL refs in its tlist.
     993             :      */
     994        1716 :     path = create_foreignscan_path(root, baserel,
     995             :                                    NULL,    /* default pathtarget */
     996             :                                    fpinfo->rows,
     997             :                                    fpinfo->startup_cost,
     998             :                                    fpinfo->total_cost,
     999             :                                    NIL, /* no pathkeys */
    1000             :                                    baserel->lateral_relids,
    1001             :                                    NULL,    /* no extra plan */
    1002             :                                    NIL);    /* no fdw_private list */
    1003        1716 :     add_path(baserel, (Path *) path);
    1004             : 
    1005             :     /* Add paths with pathkeys */
    1006        1716 :     add_paths_with_pathkeys_for_rel(root, baserel, NULL);
    1007             : 
    1008             :     /*
    1009             :      * If we're not using remote estimates, stop here.  We have no way to
    1010             :      * estimate whether any join clauses would be worth sending across, so
    1011             :      * don't bother building parameterized paths.
    1012             :      */
    1013        1716 :     if (!fpinfo->use_remote_estimate)
    1014        1238 :         return;
    1015             : 
    1016             :     /*
    1017             :      * Thumb through all join clauses for the rel to identify which outer
    1018             :      * relations could supply one or more safe-to-send-to-remote join clauses.
    1019             :      * We'll build a parameterized path for each such outer relation.
    1020             :      *
    1021             :      * It's convenient to manage this by representing each candidate outer
    1022             :      * relation by the ParamPathInfo node for it.  We can then use the
    1023             :      * ppi_clauses list in the ParamPathInfo node directly as a list of the
    1024             :      * interesting join clauses for that rel.  This takes care of the
    1025             :      * possibility that there are multiple safe join clauses for such a rel,
    1026             :      * and also ensures that we account for unsafe join clauses that we'll
    1027             :      * still have to enforce locally (since the parameterized-path machinery
    1028             :      * insists that we handle all movable clauses).
    1029             :      */
    1030         478 :     ppi_list = NIL;
    1031         716 :     foreach(lc, baserel->joininfo)
    1032             :     {
    1033         238 :         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
    1034             :         Relids      required_outer;
    1035             :         ParamPathInfo *param_info;
    1036             : 
    1037             :         /* Check if clause can be moved to this rel */
    1038         238 :         if (!join_clause_is_movable_to(rinfo, baserel))
    1039         158 :             continue;
    1040             : 
    1041             :         /* See if it is safe to send to remote */
    1042          80 :         if (!is_foreign_expr(root, baserel, rinfo->clause))
    1043          12 :             continue;
    1044             : 
    1045             :         /* Calculate required outer rels for the resulting path */
    1046          68 :         required_outer = bms_union(rinfo->clause_relids,
    1047          68 :                                    baserel->lateral_relids);
    1048             :         /* We do not want the foreign rel itself listed in required_outer */
    1049          68 :         required_outer = bms_del_member(required_outer, baserel->relid);
    1050             : 
    1051             :         /*
    1052             :          * required_outer probably can't be empty here, but if it were, we
    1053             :          * couldn't make a parameterized path.
    1054             :          */
    1055          68 :         if (bms_is_empty(required_outer))
    1056           0 :             continue;
    1057             : 
    1058             :         /* Get the ParamPathInfo */
    1059          68 :         param_info = get_baserel_parampathinfo(root, baserel,
    1060             :                                                required_outer);
    1061             :         Assert(param_info != NULL);
    1062             : 
    1063             :         /*
    1064             :          * Add it to list unless we already have it.  Testing pointer equality
    1065             :          * is OK since get_baserel_parampathinfo won't make duplicates.
    1066             :          */
    1067          68 :         ppi_list = list_append_unique_ptr(ppi_list, param_info);
    1068             :     }
    1069             : 
    1070             :     /*
    1071             :      * The above scan examined only "generic" join clauses, not those that
    1072             :      * were absorbed into EquivalenceClauses.  See if we can make anything out
    1073             :      * of EquivalenceClauses.
    1074             :      */
    1075         478 :     if (baserel->has_eclass_joins)
    1076             :     {
    1077             :         /*
    1078             :          * We repeatedly scan the eclass list looking for column references
    1079             :          * (or expressions) belonging to the foreign rel.  Each time we find
    1080             :          * one, we generate a list of equivalence joinclauses for it, and then
    1081             :          * see if any are safe to send to the remote.  Repeat till there are
    1082             :          * no more candidate EC members.
    1083             :          */
    1084             :         ec_member_foreign_arg arg;
    1085             : 
    1086         186 :         arg.already_used = NIL;
    1087             :         for (;;)
    1088         202 :         {
    1089             :             List       *clauses;
    1090             : 
    1091             :             /* Make clauses, skipping any that join to lateral_referencers */
    1092         388 :             arg.current = NULL;
    1093         388 :             clauses = generate_implied_equalities_for_column(root,
    1094             :                                                              baserel,
    1095             :                                                              ec_member_matches_foreign,
    1096             :                                                              (void *) &arg,
    1097             :                                                              baserel->lateral_referencers);
    1098             : 
    1099             :             /* Done if there are no more expressions in the foreign rel */
    1100         388 :             if (arg.current == NULL)
    1101             :             {
    1102             :                 Assert(clauses == NIL);
    1103         186 :                 break;
    1104             :             }
    1105             : 
    1106             :             /* Scan the extracted join clauses */
    1107         420 :             foreach(lc, clauses)
    1108             :             {
    1109         218 :                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
    1110             :                 Relids      required_outer;
    1111             :                 ParamPathInfo *param_info;
    1112             : 
    1113             :                 /* Check if clause can be moved to this rel */
    1114         218 :                 if (!join_clause_is_movable_to(rinfo, baserel))
    1115           0 :                     continue;
    1116             : 
    1117             :                 /* See if it is safe to send to remote */
    1118         218 :                 if (!is_foreign_expr(root, baserel, rinfo->clause))
    1119          14 :                     continue;
    1120             : 
    1121             :                 /* Calculate required outer rels for the resulting path */
    1122         204 :                 required_outer = bms_union(rinfo->clause_relids,
    1123         204 :                                            baserel->lateral_relids);
    1124         204 :                 required_outer = bms_del_member(required_outer, baserel->relid);
    1125         204 :                 if (bms_is_empty(required_outer))
    1126           0 :                     continue;
    1127             : 
    1128             :                 /* Get the ParamPathInfo */
    1129         204 :                 param_info = get_baserel_parampathinfo(root, baserel,
    1130             :                                                        required_outer);
    1131             :                 Assert(param_info != NULL);
    1132             : 
    1133             :                 /* Add it to list unless we already have it */
    1134         204 :                 ppi_list = list_append_unique_ptr(ppi_list, param_info);
    1135             :             }
    1136             : 
    1137             :             /* Try again, now ignoring the expression we found this time */
    1138         202 :             arg.already_used = lappend(arg.already_used, arg.current);
    1139             :         }
    1140             :     }
    1141             : 
    1142             :     /*
    1143             :      * Now build a path for each useful outer relation.
    1144             :      */
    1145         730 :     foreach(lc, ppi_list)
    1146             :     {
    1147         252 :         ParamPathInfo *param_info = (ParamPathInfo *) lfirst(lc);
    1148             :         double      rows;
    1149             :         int         width;
    1150             :         Cost        startup_cost;
    1151             :         Cost        total_cost;
    1152             : 
    1153             :         /* Get a cost estimate from the remote */
    1154         252 :         estimate_path_cost_size(root, baserel,
    1155             :                                 param_info->ppi_clauses, NIL, NULL,
    1156             :                                 &rows, &width,
    1157             :                                 &startup_cost, &total_cost);
    1158             : 
    1159             :         /*
    1160             :          * ppi_rows currently won't get looked at by anything, but still we
    1161             :          * may as well ensure that it matches our idea of the rowcount.
    1162             :          */
    1163         252 :         param_info->ppi_rows = rows;
    1164             : 
    1165             :         /* Make the path */
    1166         252 :         path = create_foreignscan_path(root, baserel,
    1167             :                                        NULL,    /* default pathtarget */
    1168             :                                        rows,
    1169             :                                        startup_cost,
    1170             :                                        total_cost,
    1171             :                                        NIL, /* no pathkeys */
    1172             :                                        param_info->ppi_req_outer,
    1173             :                                        NULL,
    1174             :                                        NIL);    /* no fdw_private list */
    1175         252 :         add_path(baserel, (Path *) path);
    1176             :     }
    1177             : }
    1178             : 
    1179             : /*
    1180             :  * postgresGetForeignPlan
    1181             :  *      Create ForeignScan plan node which implements selected best path
    1182             :  */
    1183             : static ForeignScan *
    1184        1352 : postgresGetForeignPlan(PlannerInfo *root,
    1185             :                        RelOptInfo *foreignrel,
    1186             :                        Oid foreigntableid,
    1187             :                        ForeignPath *best_path,
    1188             :                        List *tlist,
    1189             :                        List *scan_clauses,
    1190             :                        Plan *outer_plan)
    1191             : {
    1192        1352 :     PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
    1193             :     Index       scan_relid;
    1194             :     List       *fdw_private;
    1195        1352 :     List       *remote_exprs = NIL;
    1196        1352 :     List       *local_exprs = NIL;
    1197        1352 :     List       *params_list = NIL;
    1198        1352 :     List       *fdw_scan_tlist = NIL;
    1199        1352 :     List       *fdw_recheck_quals = NIL;
    1200             :     List       *retrieved_attrs;
    1201             :     StringInfoData sql;
    1202        1352 :     bool        has_final_sort = false;
    1203        1352 :     bool        has_limit = false;
    1204             :     ListCell   *lc;
    1205             : 
    1206             :     /*
    1207             :      * Get FDW private data created by postgresGetForeignUpperPaths(), if any.
    1208             :      */
    1209        1352 :     if (best_path->fdw_private)
    1210             :     {
    1211         204 :         has_final_sort = intVal(list_nth(best_path->fdw_private,
    1212             :                                          FdwPathPrivateHasFinalSort));
    1213         204 :         has_limit = intVal(list_nth(best_path->fdw_private,
    1214             :                                     FdwPathPrivateHasLimit));
    1215             :     }
    1216             : 
    1217        1352 :     if (IS_SIMPLE_REL(foreignrel))
    1218             :     {
    1219             :         /*
    1220             :          * For base relations, set scan_relid as the relid of the relation.
    1221             :          */
    1222         914 :         scan_relid = foreignrel->relid;
    1223             : 
    1224             :         /*
    1225             :          * In a base-relation scan, we must apply the given scan_clauses.
    1226             :          *
    1227             :          * Separate the scan_clauses into those that can be executed remotely
    1228             :          * and those that can't.  baserestrictinfo clauses that were
    1229             :          * previously determined to be safe or unsafe by classifyConditions
    1230             :          * are found in fpinfo->remote_conds and fpinfo->local_conds. Anything
    1231             :          * else in the scan_clauses list will be a join clause, which we have
    1232             :          * to check for remote-safety.
    1233             :          *
    1234             :          * Note: the join clauses we see here should be the exact same ones
    1235             :          * previously examined by postgresGetForeignPaths.  Possibly it'd be
    1236             :          * worth passing forward the classification work done then, rather
    1237             :          * than repeating it here.
    1238             :          *
    1239             :          * This code must match "extract_actual_clauses(scan_clauses, false)"
    1240             :          * except for the additional decision about remote versus local
    1241             :          * execution.
    1242             :          */
    1243        1374 :         foreach(lc, scan_clauses)
    1244             :         {
    1245         460 :             RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
    1246             : 
    1247             :             /* Ignore any pseudoconstants, they're dealt with elsewhere */
    1248         460 :             if (rinfo->pseudoconstant)
    1249           0 :                 continue;
    1250             : 
    1251         460 :             if (list_member_ptr(fpinfo->remote_conds, rinfo))
    1252         388 :                 remote_exprs = lappend(remote_exprs, rinfo->clause);
    1253          72 :             else if (list_member_ptr(fpinfo->local_conds, rinfo))
    1254          60 :                 local_exprs = lappend(local_exprs, rinfo->clause);
    1255          12 :             else if (is_foreign_expr(root, foreignrel, rinfo->clause))
    1256           8 :                 remote_exprs = lappend(remote_exprs, rinfo->clause);
    1257             :             else
    1258           4 :                 local_exprs = lappend(local_exprs, rinfo->clause);
    1259             :         }
    1260             : 
    1261             :         /*
    1262             :          * For a base-relation scan, we have to support EPQ recheck, which
    1263             :          * should recheck all the remote quals.
    1264             :          */
    1265         914 :         fdw_recheck_quals = remote_exprs;
    1266             :     }
    1267             :     else
    1268             :     {
    1269             :         /*
    1270             :          * Join relation or upper relation - set scan_relid to 0.
    1271             :          */
    1272         438 :         scan_relid = 0;
    1273             : 
    1274             :         /*
    1275             :          * For a join rel, baserestrictinfo is NIL and we are not considering
    1276             :          * parameterization right now, so there should be no scan_clauses for
    1277             :          * a joinrel or an upper rel either.
    1278             :          */
    1279             :         Assert(!scan_clauses);
    1280             : 
    1281             :         /*
    1282             :          * Instead we get the conditions to apply from the fdw_private
    1283             :          * structure.
    1284             :          */
    1285         438 :         remote_exprs = extract_actual_clauses(fpinfo->remote_conds, false);
    1286         438 :         local_exprs = extract_actual_clauses(fpinfo->local_conds, false);
    1287             : 
    1288             :         /*
    1289             :          * We leave fdw_recheck_quals empty in this case, since we never need
    1290             :          * to apply EPQ recheck clauses.  In the case of a joinrel, EPQ
    1291             :          * recheck is handled elsewhere --- see postgresGetForeignJoinPaths().
    1292             :          * If we're planning an upperrel (ie, remote grouping or aggregation)
    1293             :          * then there's no EPQ to do because SELECT FOR UPDATE wouldn't be
    1294             :          * allowed, and indeed we *can't* put the remote clauses into
    1295             :          * fdw_recheck_quals because the unaggregated Vars won't be available
    1296             :          * locally.
    1297             :          */
    1298             : 
    1299             :         /* Build the list of columns to be fetched from the foreign server. */
    1300         438 :         fdw_scan_tlist = build_tlist_to_deparse(foreignrel);
    1301             : 
    1302             :         /*
    1303             :          * Ensure that the outer plan produces a tuple whose descriptor
    1304             :          * matches our scan tuple slot.  Also, remove the local conditions
    1305             :          * from outer plan's quals, lest they be evaluated twice, once by the
    1306             :          * local plan and once by the scan.
    1307             :          */
    1308         438 :         if (outer_plan)
    1309             :         {
    1310             :             ListCell   *lc;
    1311             : 
    1312             :             /*
    1313             :              * Right now, we only consider grouping and aggregation beyond
    1314             :              * joins. Queries involving aggregates or grouping do not require
    1315             :              * EPQ mechanism, hence should not have an outer plan here.
    1316             :              */
    1317             :             Assert(!IS_UPPER_REL(foreignrel));
    1318             : 
    1319             :             /*
    1320             :              * First, update the plan's qual list if possible.  In some cases
    1321             :              * the quals might be enforced below the topmost plan level, in
    1322             :              * which case we'll fail to remove them; it's not worth working
    1323             :              * harder than this.
    1324             :              */
    1325          44 :             foreach(lc, local_exprs)
    1326             :             {
    1327           4 :                 Node       *qual = lfirst(lc);
    1328             : 
    1329           4 :                 outer_plan->qual = list_delete(outer_plan->qual, qual);
    1330             : 
    1331             :                 /*
    1332             :                  * For an inner join the local conditions of foreign scan plan
    1333             :                  * can be part of the joinquals as well.  (They might also be
    1334             :                  * in the mergequals or hashquals, but we can't touch those
    1335             :                  * without breaking the plan.)
    1336             :                  */
    1337           4 :                 if (IsA(outer_plan, NestLoop) ||
    1338           0 :                     IsA(outer_plan, MergeJoin) ||
    1339           0 :                     IsA(outer_plan, HashJoin))
    1340             :                 {
    1341           4 :                     Join       *join_plan = (Join *) outer_plan;
    1342             : 
    1343           4 :                     if (join_plan->jointype == JOIN_INNER)
    1344           4 :                         join_plan->joinqual = list_delete(join_plan->joinqual,
    1345             :                                                           qual);
    1346             :                 }
    1347             :             }
    1348             : 
    1349             :             /*
    1350             :              * Now fix the subplan's tlist --- this might result in inserting
    1351             :              * a Result node atop the plan tree.
    1352             :              */
    1353          40 :             outer_plan = change_plan_targetlist(outer_plan, fdw_scan_tlist,
    1354          40 :                                                 best_path->path.parallel_safe);
    1355             :         }
    1356             :     }
    1357             : 
    1358             :     /*
    1359             :      * Build the query string to be sent for execution, and identify
    1360             :      * expressions to be sent as parameters.
    1361             :      */
    1362        1352 :     initStringInfo(&sql);
    1363        1352 :     deparseSelectStmtForRel(&sql, root, foreignrel, fdw_scan_tlist,
    1364             :                             remote_exprs, best_path->path.pathkeys,
    1365             :                             has_final_sort, has_limit, false,
    1366             :                             &retrieved_attrs, &params_list);
    1367             : 
    1368             :     /* Remember remote_exprs for possible use by postgresPlanDirectModify */
    1369        1352 :     fpinfo->final_remote_exprs = remote_exprs;
    1370             : 
    1371             :     /*
    1372             :      * Build the fdw_private list that will be available to the executor.
    1373             :      * Items in the list must match order in enum FdwScanPrivateIndex.
    1374             :      */
    1375        1352 :     fdw_private = list_make3(makeString(sql.data),
    1376             :                              retrieved_attrs,
    1377             :                              makeInteger(fpinfo->fetch_size));
    1378        1352 :     if (IS_JOIN_REL(foreignrel) || IS_UPPER_REL(foreignrel))
    1379         438 :         fdw_private = lappend(fdw_private,
    1380         438 :                               makeString(fpinfo->relation_name->data));
    1381             : 
    1382             :     /*
    1383             :      * Create the ForeignScan node for the given relation.
    1384             :      *
    1385             :      * Note that the remote parameter expressions are stored in the fdw_exprs
    1386             :      * field of the finished plan node; we can't keep them in private state
    1387             :      * because then they wouldn't be subject to later planner processing.
    1388             :      */
    1389        1352 :     return make_foreignscan(tlist,
    1390             :                             local_exprs,
    1391             :                             scan_relid,
    1392             :                             params_list,
    1393             :                             fdw_private,
    1394             :                             fdw_scan_tlist,
    1395             :                             fdw_recheck_quals,
    1396             :                             outer_plan);
    1397             : }
    1398             : 
    1399             : /*
    1400             :  * postgresBeginForeignScan
    1401             :  *      Initiate an executor scan of a foreign PostgreSQL table.
    1402             :  */
    1403             : static void
    1404        1168 : postgresBeginForeignScan(ForeignScanState *node, int eflags)
    1405             : {
    1406        1168 :     ForeignScan *fsplan = (ForeignScan *) node->ss.ps.plan;
    1407        1168 :     EState     *estate = node->ss.ps.state;
    1408             :     PgFdwScanState *fsstate;
    1409             :     RangeTblEntry *rte;
    1410             :     Oid         userid;
    1411             :     ForeignTable *table;
    1412             :     UserMapping *user;
    1413             :     int         rtindex;
    1414             :     int         numParams;
    1415             : 
    1416             :     /*
    1417             :      * Do nothing in EXPLAIN (no ANALYZE) case.  node->fdw_state stays NULL.
    1418             :      */
    1419        1168 :     if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
    1420         532 :         return;
    1421             : 
    1422             :     /*
    1423             :      * We'll save private state in node->fdw_state.
    1424             :      */
    1425         636 :     fsstate = (PgFdwScanState *) palloc0(sizeof(PgFdwScanState));
    1426         636 :     node->fdw_state = (void *) fsstate;
    1427             : 
    1428             :     /*
    1429             :      * Identify which user to do the remote access as.  This should match what
    1430             :      * ExecCheckRTEPerms() does.  In case of a join or aggregate, use the
    1431             :      * lowest-numbered member RTE as a representative; we would get the same
    1432             :      * result from any.
    1433             :      */
    1434         636 :     if (fsplan->scan.scanrelid > 0)
    1435         412 :         rtindex = fsplan->scan.scanrelid;
    1436             :     else
    1437         224 :         rtindex = bms_next_member(fsplan->fs_relids, -1);
    1438         636 :     rte = exec_rt_fetch(rtindex, estate);
    1439         636 :     userid = rte->checkAsUser ? rte->checkAsUser : GetUserId();
    1440             : 
    1441             :     /* Get info about foreign table. */
    1442         636 :     table = GetForeignTable(rte->relid);
    1443         636 :     user = GetUserMapping(userid, table->serverid);
    1444             : 
    1445             :     /*
    1446             :      * Get connection to the foreign server.  Connection manager will
    1447             :      * establish new connection if necessary.
    1448             :      */
    1449         636 :     fsstate->conn = GetConnection(user, false);
    1450             : 
    1451             :     /* Assign a unique ID for my cursor */
    1452         632 :     fsstate->cursor_number = GetCursorNumber(fsstate->conn);
    1453         632 :     fsstate->cursor_exists = false;
    1454             : 
    1455             :     /* Get private info created by planner functions. */
    1456         632 :     fsstate->query = strVal(list_nth(fsplan->fdw_private,
    1457             :                                      FdwScanPrivateSelectSql));
    1458         632 :     fsstate->retrieved_attrs = (List *) list_nth(fsplan->fdw_private,
    1459             :                                                  FdwScanPrivateRetrievedAttrs);
    1460         632 :     fsstate->fetch_size = intVal(list_nth(fsplan->fdw_private,
    1461             :                                           FdwScanPrivateFetchSize));
    1462             : 
    1463             :     /* Create contexts for batches of tuples and per-tuple temp workspace. */
    1464         632 :     fsstate->batch_cxt = AllocSetContextCreate(estate->es_query_cxt,
    1465             :                                                "postgres_fdw tuple data",
    1466             :                                                ALLOCSET_DEFAULT_SIZES);
    1467         632 :     fsstate->temp_cxt = AllocSetContextCreate(estate->es_query_cxt,
    1468             :                                               "postgres_fdw temporary data",
    1469             :                                               ALLOCSET_SMALL_SIZES);
    1470             : 
    1471             :     /*
    1472             :      * Get info we'll need for converting data fetched from the foreign server
    1473             :      * into local representation and error reporting during that process.
    1474             :      */
    1475         632 :     if (fsplan->scan.scanrelid > 0)
    1476             :     {
    1477         408 :         fsstate->rel = node->ss.ss_currentRelation;
    1478         408 :         fsstate->tupdesc = RelationGetDescr(fsstate->rel);
    1479             :     }
    1480             :     else
    1481             :     {
    1482         224 :         fsstate->rel = NULL;
    1483         224 :         fsstate->tupdesc = node->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
    1484             :     }
    1485             : 
    1486         632 :     fsstate->attinmeta = TupleDescGetAttInMetadata(fsstate->tupdesc);
    1487             : 
    1488             :     /*
    1489             :      * Prepare for processing of parameters used in remote query, if any.
    1490             :      */
    1491         632 :     numParams = list_length(fsplan->fdw_exprs);
    1492         632 :     fsstate->numParams = numParams;
    1493         632 :     if (numParams > 0)
    1494          16 :         prepare_query_params((PlanState *) node,
    1495             :                              fsplan->fdw_exprs,
    1496             :                              numParams,
    1497             :                              &fsstate->param_flinfo,
    1498             :                              &fsstate->param_exprs,
    1499             :                              &fsstate->param_values);
    1500             : }
    1501             : 
    1502             : /*
    1503             :  * postgresIterateForeignScan
    1504             :  *      Retrieve next row from the result set, or clear tuple slot to indicate
    1505             :  *      EOF.
    1506             :  */
    1507             : static TupleTableSlot *
    1508      120006 : postgresIterateForeignScan(ForeignScanState *node)
    1509             : {
    1510      120006 :     PgFdwScanState *fsstate = (PgFdwScanState *) node->fdw_state;
    1511      120006 :     TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;
    1512             : 
    1513             :     /*
    1514             :      * If this is the first call after Begin or ReScan, we need to create the
    1515             :      * cursor on the remote side.
    1516             :      */
    1517      120006 :     if (!fsstate->cursor_exists)
    1518        1240 :         create_cursor(node);
    1519             : 
    1520             :     /*
    1521             :      * Get some more tuples, if we've run out.
    1522             :      */
    1523      120006 :     if (fsstate->next_tuple >= fsstate->num_tuples)
    1524             :     {
    1525             :         /* No point in another fetch if we already detected EOF, though. */
    1526        3470 :         if (!fsstate->eof_reached)
    1527        2380 :             fetch_more_data(node);
    1528             :         /* If we didn't get any tuples, must be end of data. */
    1529        3460 :         if (fsstate->next_tuple >= fsstate->num_tuples)
    1530        1212 :             return ExecClearTuple(slot);
    1531             :     }
    1532             : 
    1533             :     /*
    1534             :      * Return the next tuple.
    1535             :      */
    1536      118784 :     ExecStoreHeapTuple(fsstate->tuples[fsstate->next_tuple++],
    1537             :                        slot,
    1538             :                        false);
    1539             : 
    1540      118784 :     return slot;
    1541             : }
    1542             : 
    1543             : /*
    1544             :  * postgresReScanForeignScan
    1545             :  *      Restart the scan.
    1546             :  */
    1547             : static void
    1548         720 : postgresReScanForeignScan(ForeignScanState *node)
    1549             : {
    1550         720 :     PgFdwScanState *fsstate = (PgFdwScanState *) node->fdw_state;
    1551             :     char        sql[64];
    1552             :     PGresult   *res;
    1553             : 
    1554             :     /* If we haven't created the cursor yet, nothing to do. */
    1555         720 :     if (!fsstate->cursor_exists)
    1556          86 :         return;
    1557             : 
    1558             :     /*
    1559             :      * If any internal parameters affecting this node have changed, we'd
    1560             :      * better destroy and recreate the cursor.  Otherwise, rewinding it should
    1561             :      * be good enough.  If we've only fetched zero or one batch, we needn't
    1562             :      * even rewind the cursor, just rescan what we have.
    1563             :      */
    1564         686 :     if (node->ss.ps.chgParam != NULL)
    1565             :     {
    1566         668 :         fsstate->cursor_exists = false;
    1567         668 :         snprintf(sql, sizeof(sql), "CLOSE c%u",
    1568             :                  fsstate->cursor_number);
    1569             :     }
    1570          18 :     else if (fsstate->fetch_ct_2 > 1)
    1571             :     {
    1572           0 :         snprintf(sql, sizeof(sql), "MOVE BACKWARD ALL IN c%u",
    1573             :                  fsstate->cursor_number);
    1574             :     }
    1575             :     else
    1576             :     {
    1577             :         /* Easy: just rescan what we already have in memory, if anything */
    1578          18 :         fsstate->next_tuple = 0;
    1579          18 :         return;
    1580             :     }
    1581             : 
    1582             :     /*
    1583             :      * We don't use a PG_TRY block here, so be careful not to throw error
    1584             :      * without releasing the PGresult.
    1585             :      */
    1586         668 :     res = pgfdw_exec_query(fsstate->conn, sql);
    1587         668 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
    1588           0 :         pgfdw_report_error(ERROR, res, fsstate->conn, true, sql);
    1589         668 :     PQclear(res);
    1590             : 
    1591             :     /* Now force a fresh FETCH. */
    1592         668 :     fsstate->tuples = NULL;
    1593         668 :     fsstate->num_tuples = 0;
    1594         668 :     fsstate->next_tuple = 0;
    1595         668 :     fsstate->fetch_ct_2 = 0;
    1596         668 :     fsstate->eof_reached = false;
    1597             : }
    1598             : 
    1599             : /*
    1600             :  * postgresEndForeignScan
    1601             :  *      Finish scanning foreign table and dispose objects used for this scan
    1602             :  */
    1603             : static void
    1604        1144 : postgresEndForeignScan(ForeignScanState *node)
    1605             : {
    1606        1144 :     PgFdwScanState *fsstate = (PgFdwScanState *) node->fdw_state;
    1607             : 
    1608             :     /* if fsstate is NULL, we are in EXPLAIN; nothing to do */
    1609        1144 :     if (fsstate == NULL)
    1610         532 :         return;
    1611             : 
    1612             :     /* Close the cursor if open, to prevent accumulation of cursors */
    1613         612 :     if (fsstate->cursor_exists)
    1614         554 :         close_cursor(fsstate->conn, fsstate->cursor_number);
    1615             : 
    1616             :     /* Release remote connection */
    1617         612 :     ReleaseConnection(fsstate->conn);
    1618         612 :     fsstate->conn = NULL;
    1619             : 
    1620             :     /* MemoryContexts will be deleted automatically. */
    1621             : }
    1622             : 
    1623             : /*
    1624             :  * postgresAddForeignUpdateTargets
    1625             :  *      Add resjunk column(s) needed for update/delete on a foreign table
    1626             :  */
    1627             : static void
    1628         260 : postgresAddForeignUpdateTargets(Query *parsetree,
    1629             :                                 RangeTblEntry *target_rte,
    1630             :                                 Relation target_relation)
    1631             : {
    1632             :     Var        *var;
    1633             :     const char *attrname;
    1634             :     TargetEntry *tle;
    1635             : 
    1636             :     /*
    1637             :      * In postgres_fdw, what we need is the ctid, same as for a regular table.
    1638             :      */
    1639             : 
    1640             :     /* Make a Var representing the desired value */
    1641         260 :     var = makeVar(parsetree->resultRelation,
    1642             :                   SelfItemPointerAttributeNumber,
    1643             :                   TIDOID,
    1644             :                   -1,
    1645             :                   InvalidOid,
    1646             :                   0);
    1647             : 
    1648             :     /* Wrap it in a resjunk TLE with the right name ... */
    1649         260 :     attrname = "ctid";
    1650             : 
    1651         520 :     tle = makeTargetEntry((Expr *) var,
    1652         260 :                           list_length(parsetree->targetList) + 1,
    1653             :                           pstrdup(attrname),
    1654             :                           true);
    1655             : 
    1656             :     /* ... and add it to the query's targetlist */
    1657         260 :     parsetree->targetList = lappend(parsetree->targetList, tle);
    1658         260 : }
    1659             : 
    1660             : /*
    1661             :  * postgresPlanForeignModify
    1662             :  *      Plan an insert/update/delete operation on a foreign table
    1663             :  */
    1664             : static List *
    1665         188 : postgresPlanForeignModify(PlannerInfo *root,
    1666             :                           ModifyTable *plan,
    1667             :                           Index resultRelation,
    1668             :                           int subplan_index)
    1669             : {
    1670         188 :     CmdType     operation = plan->operation;
    1671         188 :     RangeTblEntry *rte = planner_rt_fetch(resultRelation, root);
    1672             :     Relation    rel;
    1673             :     StringInfoData sql;
    1674         188 :     List       *targetAttrs = NIL;
    1675         188 :     List       *withCheckOptionList = NIL;
    1676         188 :     List       *returningList = NIL;
    1677         188 :     List       *retrieved_attrs = NIL;
    1678         188 :     bool        doNothing = false;
    1679             : 
    1680         188 :     initStringInfo(&sql);
    1681             : 
    1682             :     /*
    1683             :      * Core code already has some lock on each rel being planned, so we can
    1684             :      * use NoLock here.
    1685             :      */
    1686         188 :     rel = table_open(rte->relid, NoLock);
    1687             : 
    1688             :     /*
    1689             :      * In an INSERT, we transmit all columns that are defined in the foreign
    1690             :      * table.  In an UPDATE, if there are BEFORE ROW UPDATE triggers on the
    1691             :      * foreign table, we transmit all columns like INSERT; else we transmit
    1692             :      * only columns that were explicitly targets of the UPDATE, so as to avoid
    1693             :      * unnecessary data transmission.  (We can't do that for INSERT since we
    1694             :      * would miss sending default values for columns not listed in the source
    1695             :      * statement, and for UPDATE if there are BEFORE ROW UPDATE triggers since
    1696             :      * those triggers might change values for non-target columns, in which
    1697             :      * case we would miss sending changed values for those columns.)
    1698             :      */
    1699         188 :     if (operation == CMD_INSERT ||
    1700          76 :         (operation == CMD_UPDATE &&
    1701         108 :          rel->trigdesc &&
    1702          32 :          rel->trigdesc->trig_update_before_row))
    1703         124 :     {
    1704         124 :         TupleDesc   tupdesc = RelationGetDescr(rel);
    1705             :         int         attnum;
    1706             : 
    1707         624 :         for (attnum = 1; attnum <= tupdesc->natts; attnum++)
    1708             :         {
    1709         500 :             Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1);
    1710             : 
    1711         500 :             if (!attr->attisdropped)
    1712         466 :                 targetAttrs = lappend_int(targetAttrs, attnum);
    1713             :         }
    1714             :     }
    1715          64 :     else if (operation == CMD_UPDATE)
    1716             :     {
    1717             :         int         col;
    1718          46 :         Bitmapset  *allUpdatedCols = bms_union(rte->updatedCols, rte->extraUpdatedCols);
    1719             : 
    1720          46 :         col = -1;
    1721         140 :         while ((col = bms_next_member(allUpdatedCols, col)) >= 0)
    1722             :         {
    1723             :             /* bit numbers are offset by FirstLowInvalidHeapAttributeNumber */
    1724          48 :             AttrNumber  attno = col + FirstLowInvalidHeapAttributeNumber;
    1725             : 
    1726          48 :             if (attno <= InvalidAttrNumber) /* shouldn't happen */
    1727           0 :                 elog(ERROR, "system-column update is not supported");
    1728          48 :             targetAttrs = lappend_int(targetAttrs, attno);
    1729             :         }
    1730             :     }
    1731             : 
    1732             :     /*
    1733             :      * Extract the relevant WITH CHECK OPTION list if any.
    1734             :      */
    1735         188 :     if (plan->withCheckOptionLists)
    1736          24 :         withCheckOptionList = (List *) list_nth(plan->withCheckOptionLists,
    1737             :                                                 subplan_index);
    1738             : 
    1739             :     /*
    1740             :      * Extract the relevant RETURNING list if any.
    1741             :      */
    1742         188 :     if (plan->returningLists)
    1743          40 :         returningList = (List *) list_nth(plan->returningLists, subplan_index);
    1744             : 
    1745             :     /*
    1746             :      * ON CONFLICT DO UPDATE and DO NOTHING case with inference specification
    1747             :      * should have already been rejected in the optimizer, as presently there
    1748             :      * is no way to recognize an arbiter index on a foreign table.  Only DO
    1749             :      * NOTHING is supported without an inference specification.
    1750             :      */
    1751         188 :     if (plan->onConflictAction == ONCONFLICT_NOTHING)
    1752           2 :         doNothing = true;
    1753         186 :     else if (plan->onConflictAction != ONCONFLICT_NONE)
    1754           0 :         elog(ERROR, "unexpected ON CONFLICT specification: %d",
    1755             :              (int) plan->onConflictAction);
    1756             : 
    1757             :     /*
    1758             :      * Construct the SQL command string.
    1759             :      */
    1760         188 :     switch (operation)
    1761             :     {
    1762             :         case CMD_INSERT:
    1763          94 :             deparseInsertSql(&sql, rte, resultRelation, rel,
    1764             :                              targetAttrs, doNothing,
    1765             :                              withCheckOptionList, returningList,
    1766             :                              &retrieved_attrs);
    1767          94 :             break;
    1768             :         case CMD_UPDATE:
    1769          76 :             deparseUpdateSql(&sql, rte, resultRelation, rel,
    1770             :                              targetAttrs,
    1771             :                              withCheckOptionList, returningList,
    1772             :                              &retrieved_attrs);
    1773          76 :             break;
    1774             :         case CMD_DELETE:
    1775          18 :             deparseDeleteSql(&sql, rte, resultRelation, rel,
    1776             :                              returningList,
    1777             :                              &retrieved_attrs);
    1778          18 :             break;
    1779             :         default:
    1780           0 :             elog(ERROR, "unexpected operation: %d", (int) operation);
    1781             :             break;
    1782             :     }
    1783             : 
    1784         188 :     table_close(rel, NoLock);
    1785             : 
    1786             :     /*
    1787             :      * Build the fdw_private list that will be available to the executor.
    1788             :      * Items in the list must match enum FdwModifyPrivateIndex, above.
    1789             :      */
    1790         188 :     return list_make4(makeString(sql.data),
    1791             :                       targetAttrs,
    1792             :                       makeInteger((retrieved_attrs != NIL)),
    1793             :                       retrieved_attrs);
    1794             : }
    1795             : 
    1796             : /*
    1797             :  * postgresBeginForeignModify
    1798             :  *      Begin an insert/update/delete operation on a foreign table
    1799             :  */
    1800             : static void
    1801         188 : postgresBeginForeignModify(ModifyTableState *mtstate,
    1802             :                            ResultRelInfo *resultRelInfo,
    1803             :                            List *fdw_private,
    1804             :                            int subplan_index,
    1805             :                            int eflags)
    1806             : {
    1807             :     PgFdwModifyState *fmstate;
    1808             :     char       *query;
    1809             :     List       *target_attrs;
    1810             :     bool        has_returning;
    1811             :     List       *retrieved_attrs;
    1812             :     RangeTblEntry *rte;
    1813             : 
    1814             :     /*
    1815             :      * Do nothing in EXPLAIN (no ANALYZE) case.  resultRelInfo->ri_FdwState
    1816             :      * stays NULL.
    1817             :      */
    1818         188 :     if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
    1819          48 :         return;
    1820             : 
    1821             :     /* Deconstruct fdw_private data. */
    1822         140 :     query = strVal(list_nth(fdw_private,
    1823             :                             FdwModifyPrivateUpdateSql));
    1824         140 :     target_attrs = (List *) list_nth(fdw_private,
    1825             :                                      FdwModifyPrivateTargetAttnums);
    1826         140 :     has_returning = intVal(list_nth(fdw_private,
    1827             :                                     FdwModifyPrivateHasReturning));
    1828         140 :     retrieved_attrs = (List *) list_nth(fdw_private,
    1829             :                                         FdwModifyPrivateRetrievedAttrs);
    1830             : 
    1831             :     /* Find RTE. */
    1832         140 :     rte = exec_rt_fetch(resultRelInfo->ri_RangeTableIndex,
    1833             :                         mtstate->ps.state);
    1834             : 
    1835             :     /* Construct an execution state. */
    1836         280 :     fmstate = create_foreign_modify(mtstate->ps.state,
    1837             :                                     rte,
    1838             :                                     resultRelInfo,
    1839             :                                     mtstate->operation,
    1840         140 :                                     mtstate->mt_plans[subplan_index]->plan,
    1841             :                                     query,
    1842             :                                     target_attrs,
    1843             :                                     has_returning,
    1844             :                                     retrieved_attrs);
    1845             : 
    1846         140 :     resultRelInfo->ri_FdwState = fmstate;
    1847             : }
    1848             : 
    1849             : /*
    1850             :  * postgresExecForeignInsert
    1851             :  *      Insert one row into a foreign table
    1852             :  */
    1853             : static TupleTableSlot *
    1854         432 : postgresExecForeignInsert(EState *estate,
    1855             :                           ResultRelInfo *resultRelInfo,
    1856             :                           TupleTableSlot *slot,
    1857             :                           TupleTableSlot *planSlot)
    1858             : {
    1859         432 :     PgFdwModifyState *fmstate = (PgFdwModifyState *) resultRelInfo->ri_FdwState;
    1860             :     TupleTableSlot *rslot;
    1861             : 
    1862             :     /*
    1863             :      * If the fmstate has aux_fmstate set, use the aux_fmstate (see
    1864             :      * postgresBeginForeignInsert())
    1865             :      */
    1866         432 :     if (fmstate->aux_fmstate)
    1867           2 :         resultRelInfo->ri_FdwState = fmstate->aux_fmstate;
    1868         432 :     rslot = execute_foreign_modify(estate, resultRelInfo, CMD_INSERT,
    1869             :                                    slot, planSlot);
    1870             :     /* Revert that change */
    1871         424 :     if (fmstate->aux_fmstate)
    1872           2 :         resultRelInfo->ri_FdwState = fmstate;
    1873             : 
    1874         424 :     return rslot;
    1875             : }
    1876             : 
    1877             : /*
    1878             :  * postgresExecForeignUpdate
    1879             :  *      Update one row in a foreign table
    1880             :  */
    1881             : static TupleTableSlot *
    1882          82 : postgresExecForeignUpdate(EState *estate,
    1883             :                           ResultRelInfo *resultRelInfo,
    1884             :                           TupleTableSlot *slot,
    1885             :                           TupleTableSlot *planSlot)
    1886             : {
    1887          82 :     return execute_foreign_modify(estate, resultRelInfo, CMD_UPDATE,
    1888             :                                   slot, planSlot);
    1889             : }
    1890             : 
    1891             : /*
    1892             :  * postgresExecForeignDelete
    1893             :  *      Delete one row from a foreign table
    1894             :  */
    1895             : static TupleTableSlot *
    1896          16 : postgresExecForeignDelete(EState *estate,
    1897             :                           ResultRelInfo *resultRelInfo,
    1898             :                           TupleTableSlot *slot,
    1899             :                           TupleTableSlot *planSlot)
    1900             : {
    1901          16 :     return execute_foreign_modify(estate, resultRelInfo, CMD_DELETE,
    1902             :                                   slot, planSlot);
    1903             : }
    1904             : 
    1905             : /*
    1906             :  * postgresEndForeignModify
    1907             :  *      Finish an insert/update/delete operation on a foreign table
    1908             :  */
    1909             : static void
    1910         172 : postgresEndForeignModify(EState *estate,
    1911             :                          ResultRelInfo *resultRelInfo)
    1912             : {
    1913         172 :     PgFdwModifyState *fmstate = (PgFdwModifyState *) resultRelInfo->ri_FdwState;
    1914             : 
    1915             :     /* If fmstate is NULL, we are in EXPLAIN; nothing to do */
    1916         172 :     if (fmstate == NULL)
    1917          48 :         return;
    1918             : 
    1919             :     /* Destroy the execution state */
    1920         124 :     finish_foreign_modify(fmstate);
    1921             : }
    1922             : 
    1923             : /*
    1924             :  * postgresBeginForeignInsert
    1925             :  *      Begin an insert operation on a foreign table
    1926             :  */
    1927             : static void
    1928          80 : postgresBeginForeignInsert(ModifyTableState *mtstate,
    1929             :                            ResultRelInfo *resultRelInfo)
    1930             : {
    1931             :     PgFdwModifyState *fmstate;
    1932          80 :     ModifyTable *plan = castNode(ModifyTable, mtstate->ps.plan);
    1933          80 :     EState     *estate = mtstate->ps.state;
    1934          80 :     Index       resultRelation = resultRelInfo->ri_RangeTableIndex;
    1935          80 :     Relation    rel = resultRelInfo->ri_RelationDesc;
    1936             :     RangeTblEntry *rte;
    1937          80 :     TupleDesc   tupdesc = RelationGetDescr(rel);
    1938             :     int         attnum;
    1939             :     StringInfoData sql;
    1940          80 :     List       *targetAttrs = NIL;
    1941          80 :     List       *retrieved_attrs = NIL;
    1942          80 :     bool        doNothing = false;
    1943             : 
    1944             :     /*
    1945             :      * If the foreign table we are about to insert routed rows into is also an
    1946             :      * UPDATE subplan result rel that will be updated later, proceeding with
    1947             :      * the INSERT will result in the later UPDATE incorrectly modifying those
    1948             :      * routed rows, so prevent the INSERT --- it would be nice if we could
    1949             :      * handle this case; but for now, throw an error for safety.
    1950             :      */
    1951          94 :     if (plan && plan->operation == CMD_UPDATE &&
    1952          20 :         (resultRelInfo->ri_usesFdwDirectModify ||
    1953          18 :          resultRelInfo->ri_FdwState) &&
    1954          12 :         resultRelInfo > mtstate->resultRelInfo + mtstate->mt_whichplan)
    1955           4 :         ereport(ERROR,
    1956             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1957             :                  errmsg("cannot route tuples into foreign table to be updated \"%s\"",
    1958             :                         RelationGetRelationName(rel))));
    1959             : 
    1960          76 :     initStringInfo(&sql);
    1961             : 
    1962             :     /* We transmit all columns that are defined in the foreign table. */
    1963         228 :     for (attnum = 1; attnum <= tupdesc->natts; attnum++)
    1964             :     {
    1965         152 :         Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1);
    1966             : 
    1967         152 :         if (!attr->attisdropped)
    1968         152 :             targetAttrs = lappend_int(targetAttrs, attnum);
    1969             :     }
    1970             : 
    1971             :     /* Check if we add the ON CONFLICT clause to the remote query. */
    1972          76 :     if (plan)
    1973             :     {
    1974          50 :         OnConflictAction onConflictAction = plan->onConflictAction;
    1975             : 
    1976             :         /* We only support DO NOTHING without an inference specification. */
    1977          50 :         if (onConflictAction == ONCONFLICT_NOTHING)
    1978           4 :             doNothing = true;
    1979          46 :         else if (onConflictAction != ONCONFLICT_NONE)
    1980           0 :             elog(ERROR, "unexpected ON CONFLICT specification: %d",
    1981             :                  (int) onConflictAction);
    1982             :     }
    1983             : 
    1984             :     /*
    1985             :      * If the foreign table is a partition, we need to create a new RTE
    1986             :      * describing the foreign table for use by deparseInsertSql and
    1987             :      * create_foreign_modify() below, after first copying the parent's RTE and
    1988             :      * modifying some fields to describe the foreign partition to work on.
    1989             :      * However, if this is invoked by UPDATE, the existing RTE may already
    1990             :      * correspond to this partition if it is one of the UPDATE subplan target
    1991             :      * rels; in that case, we can just use the existing RTE as-is.
    1992             :      */
    1993          76 :     rte = exec_rt_fetch(resultRelation, estate);
    1994          76 :     if (rte->relid != RelationGetRelid(rel))
    1995             :     {
    1996          46 :         rte = copyObject(rte);
    1997          46 :         rte->relid = RelationGetRelid(rel);
    1998          46 :         rte->relkind = RELKIND_FOREIGN_TABLE;
    1999             : 
    2000             :         /*
    2001             :          * For UPDATE, we must use the RT index of the first subplan target
    2002             :          * rel's RTE, because the core code would have built expressions for
    2003             :          * the partition, such as RETURNING, using that RT index as varno of
    2004             :          * Vars contained in those expressions.
    2005             :          */
    2006          48 :         if (plan && plan->operation == CMD_UPDATE &&
    2007           2 :             resultRelation == plan->rootRelation)
    2008           2 :             resultRelation = mtstate->resultRelInfo[0].ri_RangeTableIndex;
    2009             :     }
    2010             : 
    2011             :     /* Construct the SQL command string. */
    2012          76 :     deparseInsertSql(&sql, rte, resultRelation, rel, targetAttrs, doNothing,
    2013             :                      resultRelInfo->ri_WithCheckOptions,
    2014             :                      resultRelInfo->ri_returningList,
    2015             :                      &retrieved_attrs);
    2016             : 
    2017             :     /* Construct an execution state. */
    2018          76 :     fmstate = create_foreign_modify(mtstate->ps.state,
    2019             :                                     rte,
    2020             :                                     resultRelInfo,
    2021             :                                     CMD_INSERT,
    2022             :                                     NULL,
    2023             :                                     sql.data,
    2024             :                                     targetAttrs,
    2025             :                                     retrieved_attrs != NIL,
    2026             :                                     retrieved_attrs);
    2027             : 
    2028             :     /*
    2029             :      * If the given resultRelInfo already has PgFdwModifyState set, it means
    2030             :      * the foreign table is an UPDATE subplan result rel; in which case, store
    2031             :      * the resulting state into the aux_fmstate of the PgFdwModifyState.
    2032             :      */
    2033          76 :     if (resultRelInfo->ri_FdwState)
    2034             :     {
    2035             :         Assert(plan && plan->operation == CMD_UPDATE);
    2036             :         Assert(resultRelInfo->ri_usesFdwDirectModify == false);
    2037           2 :         ((PgFdwModifyState *) resultRelInfo->ri_FdwState)->aux_fmstate = fmstate;
    2038             :     }
    2039             :     else
    2040          74 :         resultRelInfo->ri_FdwState = fmstate;
    2041          76 : }
    2042             : 
    2043             : /*
    2044             :  * postgresEndForeignInsert
    2045             :  *      Finish an insert operation on a foreign table
    2046             :  */
    2047             : static void
    2048          72 : postgresEndForeignInsert(EState *estate,
    2049             :                          ResultRelInfo *resultRelInfo)
    2050             : {
    2051          72 :     PgFdwModifyState *fmstate = (PgFdwModifyState *) resultRelInfo->ri_FdwState;
    2052             : 
    2053             :     Assert(fmstate != NULL);
    2054             : 
    2055             :     /*
    2056             :      * If the fmstate has aux_fmstate set, get the aux_fmstate (see
    2057             :      * postgresBeginForeignInsert())
    2058             :      */
    2059          72 :     if (fmstate->aux_fmstate)
    2060           2 :         fmstate = fmstate->aux_fmstate;
    2061             : 
    2062             :     /* Destroy the execution state */
    2063          72 :     finish_foreign_modify(fmstate);
    2064          72 : }
    2065             : 
    2066             : /*
    2067             :  * postgresIsForeignRelUpdatable
    2068             :  *      Determine whether a foreign table supports INSERT, UPDATE and/or
    2069             :  *      DELETE.
    2070             :  */
    2071             : static int
    2072         434 : postgresIsForeignRelUpdatable(Relation rel)
    2073             : {
    2074             :     bool        updatable;
    2075             :     ForeignTable *table;
    2076             :     ForeignServer *server;
    2077             :     ListCell   *lc;
    2078             : 
    2079             :     /*
    2080             :      * By default, all postgres_fdw foreign tables are assumed updatable. This
    2081             :      * can be overridden by a per-server setting, which in turn can be
    2082             :      * overridden by a per-table setting.
    2083             :      */
    2084         434 :     updatable = true;
    2085             : 
    2086         434 :     table = GetForeignTable(RelationGetRelid(rel));
    2087         434 :     server = GetForeignServer(table->serverid);
    2088             : 
    2089        1720 :     foreach(lc, server->options)
    2090             :     {
    2091        1286 :         DefElem    *def = (DefElem *) lfirst(lc);
    2092             : 
    2093        1286 :         if (strcmp(def->defname, "updatable") == 0)
    2094           0 :             updatable = defGetBoolean(def);
    2095             :     }
    2096        1058 :     foreach(lc, table->options)
    2097             :     {
    2098         624 :         DefElem    *def = (DefElem *) lfirst(lc);
    2099             : 
    2100         624 :         if (strcmp(def->defname, "updatable") == 0)
    2101           0 :             updatable = defGetBoolean(def);
    2102             :     }
    2103             : 
    2104             :     /*
    2105             :      * Currently "updatable" means support for INSERT, UPDATE and DELETE.
    2106             :      */
    2107             :     return updatable ?
    2108         434 :         (1 << CMD_INSERT) | (1 << CMD_UPDATE) | (1 << CMD_DELETE) : 0;
    2109             : }
    2110             : 
    2111             : /*
    2112             :  * postgresRecheckForeignScan
    2113             :  *      Execute a local join execution plan for a foreign join
    2114             :  */
    2115             : static bool
    2116           0 : postgresRecheckForeignScan(ForeignScanState *node, TupleTableSlot *slot)
    2117             : {
    2118           0 :     Index       scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
    2119           0 :     PlanState  *outerPlan = outerPlanState(node);
    2120             :     TupleTableSlot *result;
    2121             : 
    2122             :     /* For base foreign relations, it suffices to set fdw_recheck_quals */
    2123           0 :     if (scanrelid > 0)
    2124           0 :         return true;
    2125             : 
    2126             :     Assert(outerPlan != NULL);
    2127             : 
    2128             :     /* Execute a local join execution plan */
    2129           0 :     result = ExecProcNode(outerPlan);
    2130           0 :     if (TupIsNull(result))
    2131           0 :         return false;
    2132             : 
    2133             :     /* Store result in the given slot */
    2134           0 :     ExecCopySlot(slot, result);
    2135             : 
    2136           0 :     return true;
    2137             : }
    2138             : 
    2139             : /*
    2140             :  * postgresPlanDirectModify
    2141             :  *      Consider a direct foreign table modification
    2142             :  *
    2143             :  * Decide whether it is safe to modify a foreign table directly, and if so,
    2144             :  * rewrite subplan accordingly.
    2145             :  */
    2146             : static bool
    2147         260 : postgresPlanDirectModify(PlannerInfo *root,
    2148             :                          ModifyTable *plan,
    2149             :                          Index resultRelation,
    2150             :                          int subplan_index)
    2151             : {
    2152         260 :     CmdType     operation = plan->operation;
    2153             :     Plan       *subplan;
    2154             :     RelOptInfo *foreignrel;
    2155             :     RangeTblEntry *rte;
    2156             :     PgFdwRelationInfo *fpinfo;
    2157             :     Relation    rel;
    2158             :     StringInfoData sql;
    2159             :     ForeignScan *fscan;
    2160         260 :     List       *targetAttrs = NIL;
    2161             :     List       *remote_exprs;
    2162         260 :     List       *params_list = NIL;
    2163         260 :     List       *returningList = NIL;
    2164         260 :     List       *retrieved_attrs = NIL;
    2165             : 
    2166             :     /*
    2167             :      * Decide whether it is safe to modify a foreign table directly.
    2168             :      */
    2169             : 
    2170             :     /*
    2171             :      * The table modification must be an UPDATE or DELETE.
    2172             :      */
    2173         260 :     if (operation != CMD_UPDATE && operation != CMD_DELETE)
    2174          64 :         return false;
    2175             : 
    2176             :     /*
    2177             :      * It's unsafe to modify a foreign table directly if there are any local
    2178             :      * joins needed.
    2179             :      */
    2180         196 :     subplan = (Plan *) list_nth(plan->plans, subplan_index);
    2181         196 :     if (!IsA(subplan, ForeignScan))
    2182          20 :         return false;
    2183         176 :     fscan = (ForeignScan *) subplan;
    2184             : 
    2185             :     /*
    2186             :      * It's unsafe to modify a foreign table directly if there are any quals
    2187             :      * that should be evaluated locally.
    2188             :      */
    2189         176 :     if (subplan->qual != NIL)
    2190          10 :         return false;
    2191             : 
    2192             :     /* Safe to fetch data about the target foreign rel */
    2193         166 :     if (fscan->scan.scanrelid == 0)
    2194             :     {
    2195          24 :         foreignrel = find_join_rel(root, fscan->fs_relids);
    2196             :         /* We should have a rel for this foreign join. */
    2197             :         Assert(foreignrel);
    2198             :     }
    2199             :     else
    2200         142 :         foreignrel = root->simple_rel_array[resultRelation];
    2201         166 :     rte = root->simple_rte_array[resultRelation];
    2202         166 :     fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
    2203             : 
    2204             :     /*
    2205             :      * It's unsafe to update a foreign table directly, if any expressions to
    2206             :      * assign to the target columns are unsafe to evaluate remotely.
    2207             :      */
    2208         166 :     if (operation == CMD_UPDATE)
    2209             :     {
    2210             :         int         col;
    2211             : 
    2212             :         /*
    2213             :          * We transmit only columns that were explicitly targets of the
    2214             :          * UPDATE, so as to avoid unnecessary data transmission.
    2215             :          */
    2216          82 :         col = -1;
    2217         262 :         while ((col = bms_next_member(rte->updatedCols, col)) >= 0)
    2218             :         {
    2219             :             /* bit numbers are offset by FirstLowInvalidHeapAttributeNumber */
    2220          98 :             AttrNumber  attno = col + FirstLowInvalidHeapAttributeNumber;
    2221             :             TargetEntry *tle;
    2222             : 
    2223          98 :             if (attno <= InvalidAttrNumber) /* shouldn't happen */
    2224           0 :                 elog(ERROR, "system-column update is not supported");
    2225             : 
    2226          98 :             tle = get_tle_by_resno(subplan->targetlist, attno);
    2227             : 
    2228          98 :             if (!tle)
    2229           0 :                 elog(ERROR, "attribute number %d not found in subplan targetlist",
    2230             :                      attno);
    2231             : 
    2232          98 :             if (!is_foreign_expr(root, foreignrel, (Expr *) tle->expr))
    2233           0 :                 return false;
    2234             : 
    2235          98 :             targetAttrs = lappend_int(targetAttrs, attno);
    2236             :         }
    2237             :     }
    2238             : 
    2239             :     /*
    2240             :      * Ok, rewrite subplan so as to modify the foreign table directly.
    2241             :      */
    2242         166 :     initStringInfo(&sql);
    2243             : 
    2244             :     /*
    2245             :      * Core code already has some lock on each rel being planned, so we can
    2246             :      * use NoLock here.
    2247             :      */
    2248         166 :     rel = table_open(rte->relid, NoLock);
    2249             : 
    2250             :     /*
    2251             :      * Recall the qual clauses that must be evaluated remotely.  (These are
    2252             :      * bare clauses not RestrictInfos, but deparse.c's appendConditions()
    2253             :      * doesn't care.)
    2254             :      */
    2255         166 :     remote_exprs = fpinfo->final_remote_exprs;
    2256             : 
    2257             :     /*
    2258             :      * Extract the relevant RETURNING list if any.
    2259             :      */
    2260         166 :     if (plan->returningLists)
    2261             :     {
    2262          58 :         returningList = (List *) list_nth(plan->returningLists, subplan_index);
    2263             : 
    2264             :         /*
    2265             :          * When performing an UPDATE/DELETE .. RETURNING on a join directly,
    2266             :          * we fetch from the foreign server any Vars specified in RETURNING
    2267             :          * that refer not only to the target relation but to non-target
    2268             :          * relations.  So we'll deparse them into the RETURNING clause of the
    2269             :          * remote query; use a targetlist consisting of them instead, which
    2270             :          * will be adjusted to be new fdw_scan_tlist of the foreign-scan plan
    2271             :          * node below.
    2272             :          */
    2273          58 :         if (fscan->scan.scanrelid == 0)
    2274          16 :             returningList = build_remote_returning(resultRelation, rel,
    2275             :                                                    returningList);
    2276             :     }
    2277             : 
    2278             :     /*
    2279             :      * Construct the SQL command string.
    2280             :      */
    2281         166 :     switch (operation)
    2282             :     {
    2283             :         case CMD_UPDATE:
    2284          82 :             deparseDirectUpdateSql(&sql, root, resultRelation, rel,
    2285             :                                    foreignrel,
    2286             :                                    ((Plan *) fscan)->targetlist,
    2287             :                                    targetAttrs,
    2288             :                                    remote_exprs, &params_list,
    2289             :                                    returningList, &retrieved_attrs);
    2290          82 :             break;
    2291             :         case CMD_DELETE:
    2292          84 :             deparseDirectDeleteSql(&sql, root, resultRelation, rel,
    2293             :                                    foreignrel,
    2294             :                                    remote_exprs, &params_list,
    2295             :                                    returningList, &retrieved_attrs);
    2296          84 :             break;
    2297             :         default:
    2298           0 :             elog(ERROR, "unexpected operation: %d", (int) operation);
    2299             :             break;
    2300             :     }
    2301             : 
    2302             :     /*
    2303             :      * Update the operation info.
    2304             :      */
    2305         166 :     fscan->operation = operation;
    2306             : 
    2307             :     /*
    2308             :      * Update the fdw_exprs list that will be available to the executor.
    2309             :      */
    2310         166 :     fscan->fdw_exprs = params_list;
    2311             : 
    2312             :     /*
    2313             :      * Update the fdw_private list that will be available to the executor.
    2314             :      * Items in the list must match enum FdwDirectModifyPrivateIndex, above.
    2315             :      */
    2316         166 :     fscan->fdw_private = list_make4(makeString(sql.data),
    2317             :                                     makeInteger((retrieved_attrs != NIL)),
    2318             :                                     retrieved_attrs,
    2319             :                                     makeInteger(plan->canSetTag));
    2320             : 
    2321             :     /*
    2322             :      * Update the foreign-join-related fields.
    2323             :      */
    2324         166 :     if (fscan->scan.scanrelid == 0)
    2325             :     {
    2326             :         /* No need for the outer subplan. */
    2327          24 :         fscan->scan.plan.lefttree = NULL;
    2328             : 
    2329             :         /* Build new fdw_scan_tlist if UPDATE/DELETE .. RETURNING. */
    2330          24 :         if (returningList)
    2331          12 :             rebuild_fdw_scan_tlist(fscan, returningList);
    2332             :     }
    2333             : 
    2334         166 :     table_close(rel, NoLock);
    2335         166 :     return true;
    2336             : }
    2337             : 
    2338             : /*
    2339             :  * postgresBeginDirectModify
    2340             :  *      Prepare a direct foreign table modification
    2341             :  */
    2342             : static void
    2343         166 : postgresBeginDirectModify(ForeignScanState *node, int eflags)
    2344             : {
    2345         166 :     ForeignScan *fsplan = (ForeignScan *) node->ss.ps.plan;
    2346         166 :     EState     *estate = node->ss.ps.state;
    2347             :     PgFdwDirectModifyState *dmstate;
    2348             :     Index       rtindex;
    2349             :     RangeTblEntry *rte;
    2350             :     Oid         userid;
    2351             :     ForeignTable *table;
    2352             :     UserMapping *user;
    2353             :     int         numParams;
    2354             : 
    2355             :     /*
    2356             :      * Do nothing in EXPLAIN (no ANALYZE) case.  node->fdw_state stays NULL.
    2357             :      */
    2358         166 :     if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
    2359          56 :         return;
    2360             : 
    2361             :     /*
    2362             :      * We'll save private state in node->fdw_state.
    2363             :      */
    2364         110 :     dmstate = (PgFdwDirectModifyState *) palloc0(sizeof(PgFdwDirectModifyState));
    2365         110 :     node->fdw_state = (void *) dmstate;
    2366             : 
    2367             :     /*
    2368             :      * Identify which user to do the remote access as.  This should match what
    2369             :      * ExecCheckRTEPerms() does.
    2370             :      */
    2371         110 :     rtindex = estate->es_result_relation_info->ri_RangeTableIndex;
    2372         110 :     rte = exec_rt_fetch(rtindex, estate);
    2373         110 :     userid = rte->checkAsUser ? rte->checkAsUser : GetUserId();
    2374             : 
    2375             :     /* Get info about foreign table. */
    2376         110 :     if (fsplan->scan.scanrelid == 0)
    2377          12 :         dmstate->rel = ExecOpenScanRelation(estate, rtindex, eflags);
    2378             :     else
    2379          98 :         dmstate->rel = node->ss.ss_currentRelation;
    2380         110 :     table = GetForeignTable(RelationGetRelid(dmstate->rel));
    2381         110 :     user = GetUserMapping(userid, table->serverid);
    2382             : 
    2383             :     /*
    2384             :      * Get connection to the foreign server.  Connection manager will
    2385             :      * establish new connection if necessary.
    2386             :      */
    2387         110 :     dmstate->conn = GetConnection(user, false);
    2388             : 
    2389             :     /* Update the foreign-join-related fields. */
    2390         110 :     if (fsplan->scan.scanrelid == 0)
    2391             :     {
    2392             :         /* Save info about foreign table. */
    2393          12 :         dmstate->resultRel = dmstate->rel;
    2394             : 
    2395             :         /*
    2396             :          * Set dmstate->rel to NULL to teach get_returning_data() and
    2397             :          * make_tuple_from_result_row() that columns fetched from the remote
    2398             :          * server are described by fdw_scan_tlist of the foreign-scan plan
    2399             :          * node, not the tuple descriptor for the target relation.
    2400             :          */
    2401          12 :         dmstate->rel = NULL;
    2402             :     }
    2403             : 
    2404             :     /* Initialize state variable */
    2405         110 :     dmstate->num_tuples = -1;    /* -1 means not set yet */
    2406             : 
    2407             :     /* Get private info created by planner functions. */
    2408         110 :     dmstate->query = strVal(list_nth(fsplan->fdw_private,
    2409             :                                      FdwDirectModifyPrivateUpdateSql));
    2410         110 :     dmstate->has_returning = intVal(list_nth(fsplan->fdw_private,
    2411             :                                              FdwDirectModifyPrivateHasReturning));
    2412         110 :     dmstate->retrieved_attrs = (List *) list_nth(fsplan->fdw_private,
    2413             :                                                  FdwDirectModifyPrivateRetrievedAttrs);
    2414         110 :     dmstate->set_processed = intVal(list_nth(fsplan->fdw_private,
    2415             :                                              FdwDirectModifyPrivateSetProcessed));
    2416             : 
    2417             :     /* Create context for per-tuple temp workspace. */
    2418         110 :     dmstate->temp_cxt = AllocSetContextCreate(estate->es_query_cxt,
    2419             :                                               "postgres_fdw temporary data",
    2420             :                                               ALLOCSET_SMALL_SIZES);
    2421             : 
    2422             :     /* Prepare for input conversion of RETURNING results. */
    2423         110 :     if (dmstate->has_returning)
    2424             :     {
    2425             :         TupleDesc   tupdesc;
    2426             : 
    2427          26 :         if (fsplan->scan.scanrelid == 0)
    2428           6 :             tupdesc = node->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
    2429             :         else
    2430          20 :             tupdesc = RelationGetDescr(dmstate->rel);
    2431             : 
    2432          26 :         dmstate->attinmeta = TupleDescGetAttInMetadata(tupdesc);
    2433             : 
    2434             :         /*
    2435             :          * When performing an UPDATE/DELETE .. RETURNING on a join directly,
    2436             :          * initialize a filter to extract an updated/deleted tuple from a scan
    2437             :          * tuple.
    2438             :          */
    2439          26 :         if (fsplan->scan.scanrelid == 0)
    2440           6 :             init_returning_filter(dmstate, fsplan->fdw_scan_tlist, rtindex);
    2441             :     }
    2442             : 
    2443             :     /*
    2444             :      * Prepare for processing of parameters used in remote query, if any.
    2445             :      */
    2446         110 :     numParams = list_length(fsplan->fdw_exprs);
    2447         110 :     dmstate->numParams = numParams;
    2448         110 :     if (numParams > 0)
    2449           0 :         prepare_query_params((PlanState *) node,
    2450             :                              fsplan->fdw_exprs,
    2451             :                              numParams,
    2452             :                              &dmstate->param_flinfo,
    2453             :                              &dmstate->param_exprs,
    2454             :                              &dmstate->param_values);
    2455             : }
    2456             : 
    2457             : /*
    2458             :  * postgresIterateDirectModify
    2459             :  *      Execute a direct foreign table modification
    2460             :  */
    2461             : static TupleTableSlot *
    2462         800 : postgresIterateDirectModify(ForeignScanState *node)
    2463             : {
    2464         800 :     PgFdwDirectModifyState *dmstate = (PgFdwDirectModifyState *) node->fdw_state;
    2465         800 :     EState     *estate = node->ss.ps.state;
    2466         800 :     ResultRelInfo *resultRelInfo = estate->es_result_relation_info;
    2467             : 
    2468             :     /*
    2469             :      * If this is the first call after Begin, execute the statement.
    2470             :      */
    2471         800 :     if (dmstate->num_tuples == -1)
    2472         108 :         execute_dml_stmt(node);
    2473             : 
    2474             :     /*
    2475             :      * If the local query doesn't specify RETURNING, just clear tuple slot.
    2476             :      */
    2477         792 :     if (!resultRelInfo->ri_projectReturning)
    2478             :     {
    2479          72 :         TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;
    2480          72 :         Instrumentation *instr = node->ss.ps.instrument;
    2481             : 
    2482             :         Assert(!dmstate->has_returning);
    2483             : 
    2484             :         /* Increment the command es_processed count if necessary. */
    2485          72 :         if (dmstate->set_processed)
    2486          72 :             estate->es_processed += dmstate->num_tuples;
    2487             : 
    2488             :         /* Increment the tuple count for EXPLAIN ANALYZE if necessary. */
    2489          72 :         if (instr)
    2490           0 :             instr->tuplecount += dmstate->num_tuples;
    2491             : 
    2492          72 :         return ExecClearTuple(slot);
    2493             :     }
    2494             : 
    2495             :     /*
    2496             :      * Get the next RETURNING tuple.
    2497             :      */
    2498         720 :     return get_returning_data(node);
    2499             : }
    2500             : 
    2501             : /*
    2502             :  * postgresEndDirectModify
    2503             :  *      Finish a direct foreign table modification
    2504             :  */
    2505             : static void
    2506         156 : postgresEndDirectModify(ForeignScanState *node)
    2507             : {
    2508         156 :     PgFdwDirectModifyState *dmstate = (PgFdwDirectModifyState *) node->fdw_state;
    2509             : 
    2510             :     /* if dmstate is NULL, we are in EXPLAIN; nothing to do */
    2511         156 :     if (dmstate == NULL)
    2512          56 :         return;
    2513             : 
    2514             :     /* Release PGresult */
    2515         100 :     if (dmstate->result)
    2516         100 :         PQclear(dmstate->result);
    2517             : 
    2518             :     /* Release remote connection */
    2519         100 :     ReleaseConnection(dmstate->conn);
    2520         100 :     dmstate->conn = NULL;
    2521             : 
    2522             :     /* MemoryContext will be deleted automatically. */
    2523             : }
    2524             : 
    2525             : /*
    2526             :  * postgresExplainForeignScan
    2527             :  *      Produce extra output for EXPLAIN of a ForeignScan on a foreign table
    2528             :  */
    2529             : static void
    2530         532 : postgresExplainForeignScan(ForeignScanState *node, ExplainState *es)
    2531             : {
    2532             :     List       *fdw_private;
    2533             :     char       *sql;
    2534             :     char       *relations;
    2535             : 
    2536         532 :     fdw_private = ((ForeignScan *) node->ss.ps.plan)->fdw_private;
    2537             : 
    2538             :     /*
    2539             :      * Add names of relation handled by the foreign scan when the scan is a
    2540             :      * join
    2541             :      */
    2542         532 :     if (list_length(fdw_private) > FdwScanPrivateRelations)
    2543             :     {
    2544         194 :         relations = strVal(list_nth(fdw_private, FdwScanPrivateRelations));
    2545         194 :         ExplainPropertyText("Relations", relations, es);
    2546             :     }
    2547             : 
    2548             :     /*
    2549             :      * Add remote query, when VERBOSE option is specified.
    2550             :      */
    2551         532 :     if (es->verbose)
    2552             :     {
    2553         480 :         sql = strVal(list_nth(fdw_private, FdwScanPrivateSelectSql));
    2554         480 :         ExplainPropertyText("Remote SQL", sql, es);
    2555             :     }
    2556         532 : }
    2557             : 
    2558             : /*
    2559             :  * postgresExplainForeignModify
    2560             :  *      Produce extra output for EXPLAIN of a ModifyTable on a foreign table
    2561             :  */
    2562             : static void
    2563          48 : postgresExplainForeignModify(ModifyTableState *mtstate,
    2564             :                              ResultRelInfo *rinfo,
    2565             :                              List *fdw_private,
    2566             :                              int subplan_index,
    2567             :                              ExplainState *es)
    2568             : {
    2569          48 :     if (es->verbose)
    2570             :     {
    2571          48 :         char       *sql = strVal(list_nth(fdw_private,
    2572             :                                           FdwModifyPrivateUpdateSql));
    2573             : 
    2574          48 :         ExplainPropertyText("Remote SQL", sql, es);
    2575             :     }
    2576          48 : }
    2577             : 
    2578             : /*
    2579             :  * postgresExplainDirectModify
    2580             :  *      Produce extra output for EXPLAIN of a ForeignScan that modifies a
    2581             :  *      foreign table directly
    2582             :  */
    2583             : static void
    2584          56 : postgresExplainDirectModify(ForeignScanState *node, ExplainState *es)
    2585             : {
    2586             :     List       *fdw_private;
    2587             :     char       *sql;
    2588             : 
    2589          56 :     if (es->verbose)
    2590             :     {
    2591          56 :         fdw_private = ((ForeignScan *) node->ss.ps.plan)->fdw_private;
    2592          56 :         sql = strVal(list_nth(fdw_private, FdwDirectModifyPrivateUpdateSql));
    2593          56 :         ExplainPropertyText("Remote SQL", sql, es);
    2594             :     }
    2595          56 : }
    2596             : 
    2597             : 
    2598             : /*
    2599             :  * estimate_path_cost_size
    2600             :  *      Get cost and size estimates for a foreign scan on given foreign relation
    2601             :  *      either a base relation or a join between foreign relations or an upper
    2602             :  *      relation containing foreign relations.
    2603             :  *
    2604             :  * param_join_conds are the parameterization clauses with outer relations.
    2605             :  * pathkeys specify the expected sort order if any for given path being costed.
    2606             :  * fpextra specifies additional post-scan/join-processing steps such as the
    2607             :  * final sort and the LIMIT restriction.
    2608             :  *
    2609             :  * The function returns the cost and size estimates in p_rows, p_width,
    2610             :  * p_startup_cost and p_total_cost variables.
    2611             :  */
    2612             : static void
    2613        3790 : estimate_path_cost_size(PlannerInfo *root,
    2614             :                         RelOptInfo *foreignrel,
    2615             :                         List *param_join_conds,
    2616             :                         List *pathkeys,
    2617             :                         PgFdwPathExtraData *fpextra,
    2618             :                         double *p_rows, int *p_width,
    2619             :                         Cost *p_startup_cost, Cost *p_total_cost)
    2620             : {
    2621        3790 :     PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) foreignrel->fdw_private;
    2622             :     double      rows;
    2623             :     double      retrieved_rows;
    2624             :     int         width;
    2625             :     Cost        startup_cost;
    2626             :     Cost        total_cost;
    2627             : 
    2628             :     /* Make sure the core code has set up the relation's reltarget */
    2629             :     Assert(foreignrel->reltarget);
    2630             : 
    2631             :     /*
    2632             :      * If the table or the server is configured to use remote estimates,
    2633             :      * connect to the foreign server and execute EXPLAIN to estimate the
    2634             :      * number of rows selected by the restriction+join clauses.  Otherwise,
    2635             :      * estimate rows using whatever statistics we have locally, in a way
    2636             :      * similar to ordinary tables.
    2637             :      */
    2638        3790 :     if (fpinfo->use_remote_estimate)
    2639             :     {
    2640             :         List       *remote_param_join_conds;
    2641             :         List       *local_param_join_conds;
    2642             :         StringInfoData sql;
    2643             :         PGconn     *conn;
    2644             :         Selectivity local_sel;
    2645             :         QualCost    local_cost;
    2646        1728 :         List       *fdw_scan_tlist = NIL;
    2647             :         List       *remote_conds;
    2648             : 
    2649             :         /* Required only to be passed to deparseSelectStmtForRel */
    2650             :         List       *retrieved_attrs;
    2651             : 
    2652             :         /*
    2653             :          * param_join_conds might contain both clauses that are safe to send
    2654             :          * across, and clauses that aren't.
    2655             :          */
    2656        1728 :         classifyConditions(root, foreignrel, param_join_conds,
    2657             :                            &remote_param_join_conds, &local_param_join_conds);
    2658             : 
    2659             :         /* Build the list of columns to be fetched from the foreign server. */
    2660        1728 :         if (IS_JOIN_REL(foreignrel) || IS_UPPER_REL(foreignrel))
    2661         594 :             fdw_scan_tlist = build_tlist_to_deparse(foreignrel);
    2662             :         else
    2663        1134 :             fdw_scan_tlist = NIL;
    2664             : 
    2665             :         /*
    2666             :          * The complete list of remote conditions includes everything from
    2667             :          * baserestrictinfo plus any extra join_conds relevant to this
    2668             :          * particular path.
    2669             :          */
    2670        1728 :         remote_conds = list_concat(list_copy(remote_param_join_conds),
    2671             :                                    fpinfo->remote_conds);
    2672             : 
    2673             :         /*
    2674             :          * Construct EXPLAIN query including the desired SELECT, FROM, and
    2675             :          * WHERE clauses. Params and other-relation Vars are replaced by dummy
    2676             :          * values, so don't request params_list.
    2677             :          */
    2678        1728 :         initStringInfo(&sql);
    2679        1728 :         appendStringInfoString(&sql, "EXPLAIN ");
    2680        3456 :         deparseSelectStmtForRel(&sql, root, foreignrel, fdw_scan_tlist,
    2681             :                                 remote_conds, pathkeys,
    2682        1728 :                                 fpextra ? fpextra->has_final_sort : false,
    2683        1728 :                                 fpextra ? fpextra->has_limit : false,
    2684             :                                 false, &retrieved_attrs, NULL);
    2685             : 
    2686             :         /* Get the remote estimate */
    2687        1728 :         conn = GetConnection(fpinfo->user, false);
    2688        1728 :         get_remote_estimate(sql.data, conn, &rows, &width,
    2689             :                             &startup_cost, &total_cost);
    2690        1728 :         ReleaseConnection(conn);
    2691             : 
    2692        1728 :         retrieved_rows = rows;
    2693             : 
    2694             :         /* Factor in the selectivity of the locally-checked quals */
    2695        1728 :         local_sel = clauselist_selectivity(root,
    2696             :                                            local_param_join_conds,
    2697        1728 :                                            foreignrel->relid,
    2698             :                                            JOIN_INNER,
    2699             :                                            NULL);
    2700        1728 :         local_sel *= fpinfo->local_conds_sel;
    2701             : 
    2702        1728 :         rows = clamp_row_est(rows * local_sel);
    2703             : 
    2704             :         /* Add in the eval cost of the locally-checked quals */
    2705        1728 :         startup_cost += fpinfo->local_conds_cost.startup;
    2706        1728 :         total_cost += fpinfo->local_conds_cost.per_tuple * retrieved_rows;
    2707        1728 :         cost_qual_eval(&local_cost, local_param_join_conds, root);
    2708        1728 :         startup_cost += local_cost.startup;
    2709        1728 :         total_cost += local_cost.per_tuple * retrieved_rows;
    2710             : 
    2711             :         /*
    2712             :          * Add in tlist eval cost for each output row.  In case of an
    2713             :          * aggregate, some of the tlist expressions such as grouping
    2714             :          * expressions will be evaluated remotely, so adjust the costs.
    2715             :          */
    2716        1728 :         startup_cost += foreignrel->reltarget->cost.startup;
    2717        1728 :         total_cost += foreignrel->reltarget->cost.startup;
    2718        1728 :         total_cost += foreignrel->reltarget->cost.per_tuple * rows;
    2719        1728 :         if (IS_UPPER_REL(foreignrel))
    2720             :         {
    2721             :             QualCost    tlist_cost;
    2722             : 
    2723          78 :             cost_qual_eval(&tlist_cost, fdw_scan_tlist, root);
    2724          78 :             startup_cost -= tlist_cost.startup;
    2725          78 :             total_cost -= tlist_cost.startup;
    2726          78 :             total_cost -= tlist_cost.per_tuple * rows;
    2727             :         }
    2728             :     }
    2729             :     else
    2730             :     {
    2731        2062 :         Cost        run_cost = 0;
    2732             : 
    2733             :         /*
    2734             :          * We don't support join conditions in this mode (hence, no
    2735             :          * parameterized paths can be made).
    2736             :          */
    2737             :         Assert(param_join_conds == NIL);
    2738             : 
    2739             :         /*
    2740             :          * We will come here again and again with different set of pathkeys or
    2741             :          * additional post-scan/join-processing steps that caller wants to
    2742             :          * cost.  We don't need to calculate the cost/size estimates for the
    2743             :          * underlying scan, join, or grouping each time.  Instead, use those
    2744             :          * estimates if we have cached them already.
    2745             :          */
    2746        2062 :         if (fpinfo->rel_startup_cost >= 0 && fpinfo->rel_total_cost >= 0)
    2747             :         {
    2748             :             Assert(fpinfo->retrieved_rows >= 1);
    2749             : 
    2750         534 :             rows = fpinfo->rows;
    2751         534 :             retrieved_rows = fpinfo->retrieved_rows;
    2752         534 :             width = fpinfo->width;
    2753         534 :             startup_cost = fpinfo->rel_startup_cost;
    2754         534 :             run_cost = fpinfo->rel_total_cost - fpinfo->rel_startup_cost;
    2755             : 
    2756             :             /*
    2757             :              * If we estimate the costs of a foreign scan or a foreign join
    2758             :              * with additional post-scan/join-processing steps, the scan or
    2759             :              * join costs obtained from the cache wouldn't yet contain the
    2760             :              * eval costs for the final scan/join target, which would've been
    2761             :              * updated by apply_scanjoin_target_to_paths(); add the eval costs
    2762             :              * now.
    2763             :              */
    2764        1068 :             if (fpextra && !IS_UPPER_REL(foreignrel))
    2765             :             {
    2766             :                 /* Shouldn't get here unless we have LIMIT */
    2767             :                 Assert(fpextra->has_limit);
    2768             :                 Assert(foreignrel->reloptkind == RELOPT_BASEREL ||
    2769             :                        foreignrel->reloptkind == RELOPT_JOINREL);
    2770         122 :                 startup_cost += foreignrel->reltarget->cost.startup;
    2771         122 :                 run_cost += foreignrel->reltarget->cost.per_tuple * rows;
    2772             :             }
    2773             :         }
    2774        1528 :         else if (IS_JOIN_REL(foreignrel))
    2775         144 :         {
    2776             :             PgFdwRelationInfo *fpinfo_i;
    2777             :             PgFdwRelationInfo *fpinfo_o;
    2778             :             QualCost    join_cost;
    2779             :             QualCost    remote_conds_cost;
    2780             :             double      nrows;
    2781             : 
    2782             :             /* Use rows/width estimates made by the core code. */
    2783         144 :             rows = foreignrel->rows;
    2784         144 :             width = foreignrel->reltarget->width;
    2785             : 
    2786             :             /* For join we expect inner and outer relations set */
    2787             :             Assert(fpinfo->innerrel && fpinfo->outerrel);
    2788             : 
    2789         144 :             fpinfo_i = (PgFdwRelationInfo *) fpinfo->innerrel->fdw_private;
    2790         144 :             fpinfo_o = (PgFdwRelationInfo *) fpinfo->outerrel->fdw_private;
    2791             : 
    2792             :             /* Estimate of number of rows in cross product */
    2793         144 :             nrows = fpinfo_i->rows * fpinfo_o->rows;
    2794             : 
    2795             :             /*
    2796             :              * Back into an estimate of the number of retrieved rows.  Just in
    2797             :              * case this is nuts, clamp to at most nrow.
    2798             :              */
    2799         144 :             retrieved_rows = clamp_row_est(rows / fpinfo->local_conds_sel);
    2800         144 :             retrieved_rows = Min(retrieved_rows, nrows);
    2801             : 
    2802             :             /*
    2803             :              * The cost of foreign join is estimated as cost of generating
    2804             :              * rows for the joining relations + cost for applying quals on the
    2805             :              * rows.
    2806             :              */
    2807             : 
    2808             :             /*
    2809             :              * Calculate the cost of clauses pushed down to the foreign server
    2810             :              */
    2811         144 :             cost_qual_eval(&remote_conds_cost, fpinfo->remote_conds, root);
    2812             :             /* Calculate the cost of applying join clauses */
    2813         144 :             cost_qual_eval(&join_cost, fpinfo->joinclauses, root);
    2814             : 
    2815             :             /*
    2816             :              * Startup cost includes startup cost of joining relations and the
    2817             :              * startup cost for join and other clauses. We do not include the
    2818             :              * startup cost specific to join strategy (e.g. setting up hash
    2819             :              * tables) since we do not know what strategy the foreign server
    2820             :              * is going to use.
    2821             :              */
    2822         144 :             startup_cost = fpinfo_i->rel_startup_cost + fpinfo_o->rel_startup_cost;
    2823         144 :             startup_cost += join_cost.startup;
    2824         144 :             startup_cost += remote_conds_cost.startup;
    2825         144 :             startup_cost += fpinfo->local_conds_cost.startup;
    2826             : 
    2827             :             /*
    2828             :              * Run time cost includes:
    2829             :              *
    2830             :              * 1. Run time cost (total_cost - startup_cost) of relations being
    2831             :              * joined
    2832             :              *
    2833             :              * 2. Run time cost of applying join clauses on the cross product
    2834             :              * of the joining relations.
    2835             :              *
    2836             :              * 3. Run time cost of applying pushed down other clauses on the
    2837             :              * result of join
    2838             :              *
    2839             :              * 4. Run time cost of applying nonpushable other clauses locally
    2840             :              * on the result fetched from the foreign server.
    2841             :              */
    2842         144 :             run_cost = fpinfo_i->rel_total_cost - fpinfo_i->rel_startup_cost;
    2843         144 :             run_cost += fpinfo_o->rel_total_cost - fpinfo_o->rel_startup_cost;
    2844         144 :             run_cost += nrows * join_cost.per_tuple;
    2845         144 :             nrows = clamp_row_est(nrows * fpinfo->joinclause_sel);
    2846         144 :             run_cost += nrows * remote_conds_cost.per_tuple;
    2847         144 :             run_cost += fpinfo->local_conds_cost.per_tuple * retrieved_rows;
    2848             : 
    2849             :             /* Add in tlist eval cost for each output row */
    2850         144 :             startup_cost += foreignrel->reltarget->cost.startup;
    2851         144 :             run_cost += foreignrel->reltarget->cost.per_tuple * rows;
    2852             :         }
    2853        1384 :         else if (IS_UPPER_REL(foreignrel))
    2854         146 :         {
    2855         146 :             RelOptInfo *outerrel = fpinfo->outerrel;
    2856             :             PgFdwRelationInfo *ofpinfo;
    2857             :             AggClauseCosts aggcosts;
    2858             :             double      input_rows;
    2859             :             int         numGroupCols;
    2860         146 :             double      numGroups = 1;
    2861             : 
    2862             :             /* The upper relation should have its outer relation set */
    2863             :             Assert(outerrel);
    2864             :             /* and that outer relation should have its reltarget set */
    2865             :             Assert(outerrel->reltarget);
    2866             : 
    2867             :             /*
    2868             :              * This cost model is mixture of costing done for sorted and
    2869             :              * hashed aggregates in cost_agg().  We are not sure which
    2870             :              * strategy will be considered at remote side, thus for
    2871             :              * simplicity, we put all startup related costs in startup_cost
    2872             :              * and all finalization and run cost are added in total_cost.
    2873             :              */
    2874             : 
    2875         146 :             ofpinfo = (PgFdwRelationInfo *) outerrel->fdw_private;
    2876             : 
    2877             :             /* Get rows from input rel */
    2878         146 :             input_rows = ofpinfo->rows;
    2879             : 
    2880             :             /* Collect statistics about aggregates for estimating costs. */
    2881         146 :             MemSet(&aggcosts, 0, sizeof(AggClauseCosts));
    2882         146 :             if (root->parse->hasAggs)
    2883             :             {
    2884         138 :                 get_agg_clause_costs(root, (Node *) fpinfo->grouped_tlist,
    2885             :                                      AGGSPLIT_SIMPLE, &aggcosts);
    2886             : 
    2887             :                 /*
    2888             :                  * The cost of aggregates in the HAVING qual will be the same
    2889             :                  * for each child as it is for the parent, so there's no need
    2890             :                  * to use a translated version of havingQual.
    2891             :                  */
    2892         138 :                 get_agg_clause_costs(root, (Node *) root->parse->havingQual,
    2893             :                                      AGGSPLIT_SIMPLE, &aggcosts);
    2894             :             }
    2895             : 
    2896             :             /* Get number of grouping columns and possible number of groups */
    2897         146 :             numGroupCols = list_length(root->parse->groupClause);
    2898         292 :             numGroups = estimate_num_groups(root,
    2899         146 :                                             get_sortgrouplist_exprs(root->parse->groupClause,
    2900             :                                                                     fpinfo->grouped_tlist),
    2901             :                                             input_rows, NULL);
    2902             : 
    2903             :             /*
    2904             :              * Get the retrieved_rows and rows estimates.  If there are HAVING
    2905             :              * quals, account for their selectivity.
    2906             :              */
    2907         146 :             if (root->parse->havingQual)
    2908             :             {
    2909             :                 /* Factor in the selectivity of the remotely-checked quals */
    2910          24 :                 retrieved_rows =
    2911          24 :                     clamp_row_est(numGroups *
    2912          24 :                                   clauselist_selectivity(root,
    2913             :                                                          fpinfo->remote_conds,
    2914             :                                                          0,
    2915             :                                                          JOIN_INNER,
    2916             :                                                          NULL));
    2917             :                 /* Factor in the selectivity of the locally-checked quals */
    2918          24 :                 rows = clamp_row_est(retrieved_rows * fpinfo->local_conds_sel);
    2919             :             }
    2920             :             else
    2921             :             {
    2922         122 :                 rows = retrieved_rows = numGroups;
    2923             :             }
    2924             : 
    2925             :             /* Use width estimate made by the core code. */
    2926         146 :             width = foreignrel->reltarget->width;
    2927             : 
    2928             :             /*-----
    2929             :              * Startup cost includes:
    2930             :              *    1. Startup cost for underneath input relation, adjusted for
    2931             :              *       tlist replacement by apply_scanjoin_target_to_paths()
    2932             :              *    2. Cost of performing aggregation, per cost_agg()
    2933             :              *-----
    2934             :              */
    2935         146 :             startup_cost = ofpinfo->rel_startup_cost;
    2936         146 :             startup_cost += outerrel->reltarget->cost.startup;
    2937         146 :             startup_cost += aggcosts.transCost.startup;
    2938         146 :             startup_cost += aggcosts.transCost.per_tuple * input_rows;
    2939         146 :             startup_cost += aggcosts.finalCost.startup;
    2940         146 :             startup_cost += (cpu_operator_cost * numGroupCols) * input_rows;
    2941             : 
    2942             :             /*-----
    2943             :              * Run time cost includes:
    2944             :              *    1. Run time cost of underneath input relation, adjusted for
    2945             :              *       tlist replacement by apply_scanjoin_target_to_paths()
    2946             :              *    2. Run time cost of performing aggregation, per cost_agg()
    2947             :              *-----
    2948             :              */
    2949         146 :             run_cost = ofpinfo->rel_total_cost - ofpinfo->rel_startup_cost;
    2950         146 :             run_cost += outerrel->reltarget->cost.per_tuple * input_rows;
    2951         146 :             run_cost += aggcosts.finalCost.per_tuple * numGroups;
    2952         146 :             run_cost += cpu_tuple_cost * numGroups;
    2953             : 
    2954             :             /* Account for the eval cost of HAVING quals, if any */
    2955         146 :             if (root->parse->havingQual)
    2956             :             {
    2957             :                 QualCost    remote_cost;
    2958             : 
    2959             :                 /* Add in the eval cost of the remotely-checked quals */
    2960          24 :                 cost_qual_eval(&remote_cost, fpinfo->remote_conds, root);
    2961          24 :                 startup_cost += remote_cost.startup;
    2962          24 :                 run_cost += remote_cost.per_tuple * numGroups;
    2963             :                 /* Add in the eval cost of the locally-checked quals */
    2964          24 :                 startup_cost += fpinfo->local_conds_cost.startup;
    2965          24 :                 run_cost += fpinfo->local_conds_cost.per_tuple * retrieved_rows;
    2966             :             }
    2967             : 
    2968             :             /* Add in tlist eval cost for each output row */
    2969         146 :             startup_cost += foreignrel->reltarget->cost.startup;
    2970         146 :             run_cost += foreignrel->reltarget->cost.per_tuple * rows;
    2971             :         }
    2972             :         else
    2973             :         {
    2974             :             Cost        cpu_per_tuple;
    2975             : 
    2976             :             /* Use rows/width estimates made by set_baserel_size_estimates. */
    2977        1238 :             rows = foreignrel->rows;
    2978        1238 :             width = foreignrel->reltarget->width;
    2979             : 
    2980             :             /*
    2981             :              * Back into an estimate of the number of retrieved rows.  Just in
    2982             :              * case this is nuts, clamp to at most foreignrel->tuples.
    2983             :              */
    2984        1238 :             retrieved_rows = clamp_row_est(rows / fpinfo->local_conds_sel);
    2985        1238 :             retrieved_rows = Min(retrieved_rows, foreignrel->tuples);
    2986             : 
    2987             :             /*
    2988             :              * Cost as though this were a seqscan, which is pessimistic.  We
    2989             :              * effectively imagine the local_conds are being evaluated
    2990             :              * remotely, too.
    2991             :              */
    2992        1238 :             startup_cost = 0;
    2993        1238 :             run_cost = 0;
    2994        1238 :             run_cost += seq_page_cost * foreignrel->pages;
    2995             : 
    2996        1238 :             startup_cost += foreignrel->baserestrictcost.startup;
    2997        1238 :             cpu_per_tuple = cpu_tuple_cost + foreignrel->baserestrictcost.per_tuple;
    2998        1238 :             run_cost += cpu_per_tuple * foreignrel->tuples;
    2999             : 
    3000             :             /* Add in tlist eval cost for each output row */
    3001        1238 :             startup_cost += foreignrel->reltarget->cost.startup;
    3002        1238 :             run_cost += foreignrel->reltarget->cost.per_tuple * rows;
    3003             :         }
    3004             : 
    3005             :         /*
    3006             :          * Without remote estimates, we have no real way to estimate the cost
    3007             :          * of generating sorted output.  It could be free if the query plan
    3008             :          * the remote side would have chosen generates properly-sorted output
    3009             :          * anyway, but in most cases it will cost something.  Estimate a value
    3010             :          * high enough that we won't pick the sorted path when the ordering
    3011             :          * isn't locally useful, but low enough that we'll err on the side of
    3012             :          * pushing down the ORDER BY clause when it's useful to do so.
    3013             :          */
    3014        2062 :         if (pathkeys != NIL)
    3015             :         {
    3016         472 :             if (IS_UPPER_REL(foreignrel))
    3017             :             {
    3018             :                 Assert(foreignrel->reloptkind == RELOPT_UPPER_REL &&
    3019             :                        fpinfo->stage == UPPERREL_GROUP_AGG);
    3020          60 :                 adjust_foreign_grouping_path_cost(root, pathkeys,
    3021             :                                                   retrieved_rows, width,
    3022             :                                                   fpextra->limit_tuples,
    3023             :                                                   &startup_cost, &run_cost);
    3024             :             }
    3025             :             else
    3026             :             {
    3027         412 :                 startup_cost *= DEFAULT_FDW_SORT_MULTIPLIER;
    3028         412 :                 run_cost *= DEFAULT_FDW_SORT_MULTIPLIER;
    3029             :             }
    3030             :         }
    3031             : 
    3032        2062 :         total_cost = startup_cost + run_cost;
    3033             : 
    3034             :         /* Adjust the cost estimates if we have LIMIT */
    3035        2062 :         if (fpextra && fpextra->has_limit)
    3036             :         {
    3037         126 :             adjust_limit_rows_costs(&rows, &startup_cost, &total_cost,
    3038             :                                     fpextra->offset_est, fpextra->count_est);
    3039         126 :             retrieved_rows = rows;
    3040             :         }
    3041             :     }
    3042             : 
    3043             :     /*
    3044             :      * If this includes the final sort step, the given target, which will be
    3045             :      * applied to the resulting path, might have different expressions from
    3046             :      * the foreignrel's reltarget (see make_sort_input_target()); adjust tlist
    3047             :      * eval costs.
    3048             :      */
    3049        3972 :     if (fpextra && fpextra->has_final_sort &&
    3050         182 :         fpextra->target != foreignrel->reltarget)
    3051             :     {
    3052          12 :         QualCost    oldcost = foreignrel->reltarget->cost;
    3053          12 :         QualCost    newcost = fpextra->target->cost;
    3054             : 
    3055          12 :         startup_cost += newcost.startup - oldcost.startup;
    3056          12 :         total_cost += newcost.startup - oldcost.startup;
    3057          12 :         total_cost += (newcost.per_tuple - oldcost.per_tuple) * rows;
    3058             :     }
    3059             : 
    3060             :     /*
    3061             :      * Cache the retrieved rows and cost estimates for scans, joins, or
    3062             :      * groupings without any parameterization, pathkeys, or additional
    3063             :      * post-scan/join-processing steps, before adding the costs for
    3064             :      * transferring data from the foreign server.  These estimates are useful
    3065             :      * for costing remote joins involving this relation or costing other
    3066             :      * remote operations on this relation such as remote sorts and remote
    3067             :      * LIMIT restrictions, when the costs can not be obtained from the foreign
    3068             :      * server.  This function will be called at least once for every foreign
    3069             :      * relation without any parameterization, pathkeys, or additional
    3070             :      * post-scan/join-processing steps.
    3071             :      */
    3072        3790 :     if (pathkeys == NIL && param_join_conds == NIL && fpextra == NULL)
    3073             :     {
    3074        2342 :         fpinfo->retrieved_rows = retrieved_rows;
    3075        2342 :         fpinfo->rel_startup_cost = startup_cost;
    3076        2342 :         fpinfo->rel_total_cost = total_cost;
    3077             :     }
    3078             : 
    3079             :     /*
    3080             :      * Add some additional cost factors to account for connection overhead
    3081             :      * (fdw_startup_cost), transferring data across the network
    3082             :      * (fdw_tuple_cost per retrieved row), and local manipulation of the data
    3083             :      * (cpu_tuple_cost per retrieved row).
    3084             :      */
    3085        3790 :     startup_cost += fpinfo->fdw_startup_cost;
    3086        3790 :     total_cost += fpinfo->fdw_startup_cost;
    3087        3790 :     total_cost += fpinfo->fdw_tuple_cost * retrieved_rows;
    3088        3790 :     total_cost += cpu_tuple_cost * retrieved_rows;
    3089             : 
    3090             :     /*
    3091             :      * If we have LIMIT, we should prefer performing the restriction remotely
    3092             :      * rather than locally, as the former avoids extra row fetches from the
    3093             :      * remote that the latter might cause.  But since the core code doesn't
    3094             :      * account for such fetches when estimating the costs of the local
    3095             :      * restriction (see create_limit_path()), there would be no difference
    3096             :      * between the costs of the local restriction and the costs of the remote
    3097             :      * restriction estimated above if we don't use remote estimates (except
    3098             :      * for the case where the foreignrel is a grouping relation, the given
    3099             :      * pathkeys is not NIL, and the effects of a bounded sort for that rel is
    3100             :      * accounted for in costing the remote restriction).  Tweak the costs of
    3101             :      * the remote restriction to ensure we'll prefer it if LIMIT is a useful
    3102             :      * one.
    3103             :      */
    3104        3790 :     if (!fpinfo->use_remote_estimate &&
    3105         312 :         fpextra && fpextra->has_limit &&
    3106         252 :         fpextra->limit_tuples > 0 &&
    3107         126 :         fpextra->limit_tuples < fpinfo->rows)
    3108             :     {
    3109             :         Assert(fpinfo->rows > 0);
    3110         342 :         total_cost -= (total_cost - startup_cost) * 0.05 *
    3111         228 :             (fpinfo->rows - fpextra->limit_tuples) / fpinfo->rows;
    3112             :     }
    3113             : 
    3114             :     /* Return results. */
    3115        3790 :     *p_rows = rows;
    3116        3790 :     *p_width = width;
    3117        3790 :     *p_startup_cost = startup_cost;
    3118        3790 :     *p_total_cost = total_cost;
    3119        3790 : }
    3120             : 
    3121             : /*
    3122             :  * Estimate costs of executing a SQL statement remotely.
    3123             :  * The given "sql" must be an EXPLAIN command.
    3124             :  */
    3125             : static void
    3126        1728 : get_remote_estimate(const char *sql, PGconn *conn,
    3127             :                     double *rows, int *width,
    3128             :                     Cost *startup_cost, Cost *total_cost)
    3129             : {
    3130        1728 :     PGresult   *volatile res = NULL;
    3131             : 
    3132             :     /* PGresult must be released before leaving this function. */
    3133        1728 :     PG_TRY();
    3134             :     {
    3135             :         char       *line;
    3136             :         char       *p;
    3137             :         int         n;
    3138             : 
    3139             :         /*
    3140             :          * Execute EXPLAIN remotely.
    3141             :          */
    3142        1728 :         res = pgfdw_exec_query(conn, sql);
    3143        1728 :         if (PQresultStatus(res) != PGRES_TUPLES_OK)
    3144           0 :             pgfdw_report_error(ERROR, res, conn, false, sql);
    3145             : 
    3146             :         /*
    3147             :          * Extract cost numbers for topmost plan node.  Note we search for a
    3148             :          * left paren from the end of the line to avoid being confused by
    3149             :          * other uses of parentheses.
    3150             :          */
    3151        1728 :         line = PQgetvalue(res, 0, 0);
    3152        1728 :         p = strrchr(line, '(');
    3153        1728 :         if (p == NULL)
    3154           0 :             elog(ERROR, "could not interpret EXPLAIN output: \"%s\"", line);
    3155        1728 :         n = sscanf(p, "(cost=%lf..%lf rows=%lf width=%d)",
    3156             :                    startup_cost, total_cost, rows, width);
    3157        1728 :         if (n != 4)
    3158           0 :             elog(ERROR, "could not interpret EXPLAIN output: \"%s\"", line);
    3159             : 
    3160        1728 :         PQclear(res);
    3161        1728 :         res = NULL;
    3162             :     }
    3163           0 :     PG_CATCH();
    3164             :     {
    3165           0 :         if (res)
    3166           0 :             PQclear(res);
    3167           0 :         PG_RE_THROW();
    3168             :     }
    3169        1728 :     PG_END_TRY();
    3170        1728 : }
    3171             : 
    3172             : /*
    3173             :  * Adjust the cost estimates of a foreign grouping path to include the cost of
    3174             :  * generating properly-sorted output.
    3175             :  */
    3176             : static void
    3177          60 : adjust_foreign_grouping_path_cost(PlannerInfo *root,
    3178             :                                   List *pathkeys,
    3179             :                                   double retrieved_rows,
    3180             :                                   double width,
    3181             :                                   double limit_tuples,
    3182             :                                   Cost *p_startup_cost,
    3183             :                                   Cost *p_run_cost)
    3184             : {
    3185             :     /*
    3186             :      * If the GROUP BY clause isn't sort-able, the plan chosen by the remote
    3187             :      * side is unlikely to generate properly-sorted output, so it would need
    3188             :      * an explicit sort; adjust the given costs with cost_sort().  Likewise,
    3189             :      * if the GROUP BY clause is sort-able but isn't a superset of the given
    3190             :      * pathkeys, adjust the costs with that function.  Otherwise, adjust the
    3191             :      * costs by applying the same heuristic as for the scan or join case.
    3192             :      */
    3193         120 :     if (!grouping_is_sortable(root->parse->groupClause) ||
    3194          60 :         !pathkeys_contained_in(pathkeys, root->group_pathkeys))
    3195          44 :     {
    3196             :         Path        sort_path;  /* dummy for result of cost_sort */
    3197             : 
    3198          88 :         cost_sort(&sort_path,
    3199             :                   root,
    3200             :                   pathkeys,
    3201          44 :                   *p_startup_cost + *p_run_cost,
    3202             :                   retrieved_rows,
    3203             :                   width,
    3204             :                   0.0,
    3205             :                   work_mem,
    3206             :                   limit_tuples);
    3207             : 
    3208          44 :         *p_startup_cost = sort_path.startup_cost;
    3209          44 :         *p_run_cost = sort_path.total_cost - sort_path.startup_cost;
    3210             :     }
    3211             :     else
    3212             :     {
    3213             :         /*
    3214             :          * The default extra cost seems too large for foreign-grouping cases;
    3215             :          * add 1/4th of that default.
    3216             :          */
    3217          16 :         double      sort_multiplier = 1.0 + (DEFAULT_FDW_SORT_MULTIPLIER
    3218             :                                              - 1.0) * 0.25;
    3219             : 
    3220          16 :         *p_startup_cost *= sort_multiplier;
    3221          16 :         *p_run_cost *= sort_multiplier;
    3222             :     }
    3223          60 : }
    3224             : 
    3225             : /*
    3226             :  * Detect whether we want to process an EquivalenceClass member.
    3227             :  *
    3228             :  * This is a callback for use by generate_implied_equalities_for_column.
    3229             :  */
    3230             : static bool
    3231         436 : ec_member_matches_foreign(PlannerInfo *root, RelOptInfo *rel,
    3232             :                           EquivalenceClass *ec, EquivalenceMember *em,
    3233             :                           void *arg)
    3234             : {
    3235         436 :     ec_member_foreign_arg *state = (ec_member_foreign_arg *) arg;
    3236         436 :     Expr       *expr = em->em_expr;
    3237             : 
    3238             :     /*
    3239             :      * If we've identified what we're processing in the current scan, we only
    3240             :      * want to match that expression.
    3241             :      */
    3242         436 :     if (state->current != NULL)
    3243           0 :         return equal(expr, state->current);
    3244             : 
    3245             :     /*
    3246             :      * Otherwise, ignore anything we've already processed.
    3247             :      */
    3248         436 :     if (list_member(state->already_used, expr))
    3249         234 :         return false;
    3250             : 
    3251             :     /* This is the new target to process. */
    3252         202 :     state->current = expr;
    3253         202 :     return true;
    3254             : }
    3255             : 
    3256             : /*
    3257             :  * Create cursor for node's query with current parameter values.
    3258             :  */
    3259             : static void
    3260        1240 : create_cursor(ForeignScanState *node)
    3261             : {
    3262        1240 :     PgFdwScanState *fsstate = (PgFdwScanState *) node->fdw_state;
    3263        1240 :     ExprContext *econtext = node->ss.ps.ps_ExprContext;
    3264        1240 :     int         numParams = fsstate->numParams;
    3265        1240 :     const char **values = fsstate->param_values;
    3266        1240 :     PGconn     *conn = fsstate->conn;
    3267             :     StringInfoData buf;
    3268             :     PGresult   *res;
    3269             : 
    3270             :     /*
    3271             :      * Construct array of query parameter values in text format.  We do the
    3272             :      * conversions in the short-lived per-tuple context, so as not to cause a
    3273             :      * memory leak over repeated scans.
    3274             :      */
    3275        1240 :     if (numParams > 0)
    3276             :     {
    3277             :         MemoryContext oldcontext;
    3278             : 
    3279         668 :         oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
    3280             : 
    3281         668 :         process_query_params(econtext,
    3282             :                              fsstate->param_flinfo,
    3283             :                              fsstate->param_exprs,
    3284             :                              values);
    3285             : 
    3286         668 :         MemoryContextSwitchTo(oldcontext);
    3287             :     }
    3288             : 
    3289             :     /* Construct the DECLARE CURSOR command */
    3290        1240 :     initStringInfo(&buf);
    3291        1240 :     appendStringInfo(&buf, "DECLARE c%u CURSOR FOR\n%s",
    3292             :                      fsstate->cursor_number, fsstate->query);
    3293             : 
    3294             :     /*
    3295             :      * Notice that we pass NULL for paramTypes, thus forcing the remote server
    3296             :      * to infer types for all parameters.  Since we explicitly cast every
    3297             :      * parameter (see deparse.c), the "inference" is trivial and will produce
    3298             :      * the desired result.  This allows us to avoid assuming that the remote
    3299             :      * server has the same OIDs we do for the parameters' types.
    3300             :      */
    3301        1240 :     if (!PQsendQueryParams(conn, buf.data, numParams,
    3302             :                            NULL, values, NULL, NULL, 0))
    3303           0 :         pgfdw_report_error(ERROR, NULL, conn, false, buf.data);
    3304             : 
    3305             :     /*
    3306             :      * Get the result, and check for success.
    3307             :      *
    3308             :      * We don't use a PG_TRY block here, so be careful not to throw error
    3309             :      * without releasing the PGresult.
    3310             :      */
    3311        1240 :     res = pgfdw_get_result(conn, buf.data);
    3312        1240 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
    3313           0 :         pgfdw_report_error(ERROR, res, conn, true, fsstate->query);
    3314        1240 :     PQclear(res);
    3315             : 
    3316             :     /* Mark the cursor as created, and show no tuples have been retrieved */
    3317        1240 :     fsstate->cursor_exists = true;
    3318        1240 :     fsstate->tuples = NULL;
    3319        1240 :     fsstate->num_tuples = 0;
    3320        1240 :     fsstate->next_tuple = 0;
    3321        1240 :     fsstate->fetch_ct_2 = 0;
    3322        1240 :     fsstate->eof_reached = false;
    3323             : 
    3324             :     /* Clean up */
    3325        1240 :     pfree(buf.data);
    3326        1240 : }
    3327             : 
    3328             : /*
    3329             :  * Fetch some more rows from the node's cursor.
    3330             :  */
    3331             : static void
    3332        2380 : fetch_more_data(ForeignScanState *node)
    3333             : {
    3334        2380 :     PgFdwScanState *fsstate = (PgFdwScanState *) node->fdw_state;
    3335        2380 :     PGresult   *volatile res = NULL;
    3336             :     MemoryContext oldcontext;
    3337             : 
    3338             :     /*
    3339             :      * We'll store the tuples in the batch_cxt.  First, flush the previous
    3340             :      * batch.
    3341             :      */
    3342        2380 :     fsstate->tuples = NULL;
    3343        2380 :     MemoryContextReset(fsstate->batch_cxt);
    3344        2380 :     oldcontext = MemoryContextSwitchTo(fsstate->batch_cxt);
    3345             : 
    3346             :     /* PGresult must be released before leaving this function. */
    3347        2380 :     PG_TRY();
    3348             :     {
    3349        2380 :         PGconn     *conn = fsstate->conn;
    3350             :         char        sql[64];
    3351             :         int         numrows;
    3352             :         int         i;
    3353             : 
    3354        2380 :         snprintf(sql, sizeof(sql), "FETCH %d FROM c%u",
    3355             :                  fsstate->fetch_size, fsstate->cursor_number);
    3356             : 
    3357        2380 :         res = pgfdw_exec_query(conn, sql);
    3358             :         /* On error, report the original query, not the FETCH. */
    3359        2380 :         if (PQresultStatus(res) != PGRES_TUPLES_OK)
    3360           2 :             pgfdw_report_error(ERROR, res, conn, false, fsstate->query);
    3361             : 
    3362             :         /* Convert the data into HeapTuples */
    3363        2378 :         numrows = PQntuples(res);
    3364        2378 :         fsstate->tuples = (HeapTuple *) palloc0(numrows * sizeof(HeapTuple));
    3365        2378 :         fsstate->num_tuples = numrows;
    3366        2378 :         fsstate->next_tuple = 0;
    3367             : 
    3368      122890 :         for (i = 0; i < numrows; i++)
    3369             :         {
    3370             :             Assert(IsA(node->ss.ps.plan, ForeignScan));
    3371             : 
    3372      241032 :             fsstate->tuples[i] =
    3373      120520 :                 make_tuple_from_result_row(res, i,
    3374             :                                            fsstate->rel,
    3375             :                                            fsstate->attinmeta,
    3376             :                                            fsstate->retrieved_attrs,
    3377             :                                            node,
    3378             :                                            fsstate->temp_cxt);
    3379             :         }
    3380             : 
    3381             :         /* Update fetch_ct_2 */
    3382        2370 :         if (fsstate->fetch_ct_2 < 2)
    3383        1376 :             fsstate->fetch_ct_2++;
    3384             : 
    3385             :         /* Must be EOF if we didn't get as many tuples as we asked for. */
    3386        2370 :         fsstate->eof_reached = (numrows < fsstate->fetch_size);
    3387             : 
    3388        2370 :         PQclear(res);
    3389        2370 :         res = NULL;
    3390             :     }
    3391          10 :     PG_CATCH();
    3392             :     {
    3393          10 :         if (res)
    3394          10 :             PQclear(res);
    3395          10 :         PG_RE_THROW();
    3396             :     }
    3397        2370 :     PG_END_TRY();
    3398             : 
    3399        2370 :     MemoryContextSwitchTo(oldcontext);
    3400        2370 : }
    3401             : 
    3402             : /*
    3403             :  * Force assorted GUC parameters to settings that ensure that we'll output
    3404             :  * data values in a form that is unambiguous to the remote server.
    3405             :  *
    3406             :  * This is rather expensive and annoying to do once per row, but there's
    3407             :  * little choice if we want to be sure values are transmitted accurately;
    3408             :  * we can't leave the settings in place between rows for fear of affecting
    3409             :  * user-visible computations.
    3410             :  *
    3411             :  * We use the equivalent of a function SET option to allow the settings to
    3412             :  * persist only until the caller calls reset_transmission_modes().  If an
    3413             :  * error is thrown in between, guc.c will take care of undoing the settings.
    3414             :  *
    3415             :  * The return value is the nestlevel that must be passed to
    3416             :  * reset_transmission_modes() to undo things.
    3417             :  */
    3418             : int
    3419        4666 : set_transmission_modes(void)
    3420             : {
    3421        4666 :     int         nestlevel = NewGUCNestLevel();
    3422             : 
    3423             :     /*
    3424             :      * The values set here should match what pg_dump does.  See also
    3425             :      * configure_remote_session in connection.c.
    3426             :      */
    3427        4666 :     if (DateStyle != USE_ISO_DATES)
    3428        4666 :         (void) set_config_option("datestyle", "ISO",
    3429             :                                  PGC_USERSET, PGC_S_SESSION,
    3430             :                                  GUC_ACTION_SAVE, true, 0, false);
    3431        4666 :     if (IntervalStyle != INTSTYLE_POSTGRES)
    3432        4666 :         (void) set_config_option("intervalstyle", "postgres",
    3433             :                                  PGC_USERSET, PGC_S_SESSION,
    3434             :                                  GUC_ACTION_SAVE, true, 0, false);
    3435        4666 :     if (extra_float_digits < 3)
    3436        4666 :         (void) set_config_option("extra_float_digits", "3",
    3437             :                                  PGC_USERSET, PGC_S_SESSION,
    3438             :                                  GUC_ACTION_SAVE, true, 0, false);
    3439             : 
    3440        4666 :     return nestlevel;
    3441             : }
    3442             : 
    3443             : /*
    3444             :  * Undo the effects of set_transmission_modes().
    3445             :  */
    3446             : void
    3447        4666 : reset_transmission_modes(int nestlevel)
    3448             : {
    3449        4666 :     AtEOXact_GUC(true, nestlevel);
    3450        4666 : }
    3451             : 
    3452             : /*
    3453             :  * Utility routine to close a cursor.
    3454             :  */
    3455             : static void
    3456         602 : close_cursor(PGconn *conn, unsigned int cursor_number)
    3457             : {
    3458             :     char        sql[64];
    3459             :     PGresult   *res;
    3460             : 
    3461         602 :     snprintf(sql, sizeof(sql), "CLOSE c%u", cursor_number);
    3462             : 
    3463             :     /*
    3464             :      * We don't use a PG_TRY block here, so be careful not to throw error
    3465             :      * without releasing the PGresult.
    3466             :      */
    3467         602 :     res = pgfdw_exec_query(conn, sql);
    3468         602 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
    3469           0 :         pgfdw_report_error(ERROR, res, conn, true, sql);
    3470         602 :     PQclear(res);
    3471         602 : }
    3472             : 
    3473             : /*
    3474             :  * create_foreign_modify
    3475             :  *      Construct an execution state of a foreign insert/update/delete
    3476             :  *      operation
    3477             :  */
    3478             : static PgFdwModifyState *
    3479         216 : create_foreign_modify(EState *estate,
    3480             :                       RangeTblEntry *rte,
    3481             :                       ResultRelInfo *resultRelInfo,
    3482             :                       CmdType operation,
    3483             :                       Plan *subplan,
    3484             :                       char *query,
    3485             :                       List *target_attrs,
    3486             :                       bool has_returning,
    3487             :                       List *retrieved_attrs)
    3488             : {
    3489             :     PgFdwModifyState *fmstate;
    3490         216 :     Relation    rel = resultRelInfo->ri_RelationDesc;
    3491         216 :     TupleDesc   tupdesc = RelationGetDescr(rel);
    3492             :     Oid         userid;
    3493             :     ForeignTable *table;
    3494             :     UserMapping *user;
    3495             :     AttrNumber  n_params;
    3496             :     Oid         typefnoid;
    3497             :     bool        isvarlena;
    3498             :     ListCell   *lc;
    3499             : 
    3500             :     /* Begin constructing PgFdwModifyState. */
    3501         216 :     fmstate = (PgFdwModifyState *) palloc0(sizeof(PgFdwModifyState));
    3502         216 :     fmstate->rel = rel;
    3503             : 
    3504             :     /*
    3505             :      * Identify which user to do the remote access as.  This should match what
    3506             :      * ExecCheckRTEPerms() does.
    3507             :      */
    3508         216 :     userid = rte->checkAsUser ? rte->checkAsUser : GetUserId();
    3509             : 
    3510             :     /* Get info about foreign table. */
    3511         216 :     table = GetForeignTable(RelationGetRelid(rel));
    3512         216 :     user = GetUserMapping(userid, table->serverid);
    3513             : 
    3514             :     /* Open connection; report that we'll create a prepared statement. */
    3515         216 :     fmstate->conn = GetConnection(user, true);
    3516         216 :     fmstate->p_name = NULL;      /* prepared statement not made yet */
    3517             : 
    3518             :     /* Set up remote query information. */
    3519         216 :     fmstate->query = query;
    3520         216 :     fmstate->target_attrs = target_attrs;
    3521         216 :     fmstate->has_returning = has_returning;
    3522         216 :     fmstate->retrieved_attrs = retrieved_attrs;
    3523             : 
    3524             :     /* Create context for per-tuple temp workspace. */
    3525         216 :     fmstate->temp_cxt = AllocSetContextCreate(estate->es_query_cxt,
    3526             :                                               "postgres_fdw temporary data",
    3527             :                                               ALLOCSET_SMALL_SIZES);
    3528             : 
    3529             :     /* Prepare for input conversion of RETURNING results. */
    3530         216 :     if (fmstate->has_returning)
    3531         106 :         fmstate->attinmeta = TupleDescGetAttInMetadata(tupdesc);
    3532             : 
    3533             :     /* Prepare for output conversion of parameters used in prepared stmt. */
    3534         216 :     n_params = list_length(fmstate->target_attrs) + 1;
    3535         216 :     fmstate->p_flinfo = (FmgrInfo *) palloc0(sizeof(FmgrInfo) * n_params);
    3536         216 :     fmstate->p_nums = 0;
    3537             : 
    3538         216 :     if (operation == CMD_UPDATE || operation == CMD_DELETE)
    3539             :     {
    3540             :         Assert(subplan != NULL);
    3541             : 
    3542             :         /* Find the ctid resjunk column in the subplan's result */
    3543          58 :         fmstate->ctidAttno = ExecFindJunkAttributeInTlist(subplan->targetlist,
    3544             :                                                           "ctid");
    3545          58 :         if (!AttributeNumberIsValid(fmstate->ctidAttno))
    3546           0 :             elog(ERROR, "could not find junk ctid column");
    3547             : 
    3548             :         /* First transmittable parameter will be ctid */
    3549          58 :         getTypeOutputInfo(TIDOID, &typefnoid, &isvarlena);
    3550          58 :         fmgr_info(typefnoid, &fmstate->p_flinfo[fmstate->p_nums]);
    3551          58 :         fmstate->p_nums++;
    3552             :     }
    3553             : 
    3554         216 :     if (operation == CMD_INSERT || operation == CMD_UPDATE)
    3555             :     {
    3556             :         /* Set up for remaining transmittable parameters */
    3557         764 :         foreach(lc, fmstate->target_attrs)
    3558             :         {
    3559         558 :             int         attnum = lfirst_int(lc);
    3560         558 :             Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1);
    3561             : 
    3562             :             Assert(!attr->attisdropped);
    3563             : 
    3564         558 :             getTypeOutputInfo(attr->atttypid, &typefnoid, &isvarlena);
    3565         558 :             fmgr_info(typefnoid, &fmstate->p_flinfo[fmstate->p_nums]);
    3566         558 :             fmstate->p_nums++;
    3567             :         }
    3568             :     }
    3569             : 
    3570             :     Assert(fmstate->p_nums <= n_params);
    3571             : 
    3572             :     /* Initialize auxiliary state */
    3573         216 :     fmstate->aux_fmstate = NULL;
    3574             : 
    3575         216 :     return fmstate;
    3576             : }
    3577             : 
    3578             : /*
    3579             :  * execute_foreign_modify
    3580             :  *      Perform foreign-table modification as required, and fetch RETURNING
    3581             :  *      result if any.  (This is the shared guts of postgresExecForeignInsert,
    3582             :  *      postgresExecForeignUpdate, and postgresExecForeignDelete.)
    3583             :  */
    3584             : static TupleTableSlot *
    3585         530 : execute_foreign_modify(EState *estate,
    3586             :                        ResultRelInfo *resultRelInfo,
    3587             :                        CmdType operation,
    3588             :                        TupleTableSlot *slot,
    3589             :                        TupleTableSlot *planSlot)
    3590             : {
    3591         530 :     PgFdwModifyState *fmstate = (PgFdwModifyState *) resultRelInfo->ri_FdwState;
    3592         530 :     ItemPointer ctid = NULL;
    3593             :     const char **p_values;
    3594             :     PGresult   *res;
    3595             :     int         n_rows;
    3596             : 
    3597             :     /* The operation should be INSERT, UPDATE, or DELETE */
    3598             :     Assert(operation == CMD_INSERT ||
    3599             :            operation == CMD_UPDATE ||
    3600             :            operation == CMD_DELETE);
    3601             : 
    3602             :     /* Set up the prepared statement on the remote server, if we didn't yet */
    3603         530 :     if (!fmstate->p_name)
    3604         204 :         prepare_foreign_modify(fmstate);
    3605             : 
    3606             :     /*
    3607             :      * For UPDATE/DELETE, get the ctid that was passed up as a resjunk column
    3608             :      */
    3609         530 :     if (operation == CMD_UPDATE || operation == CMD_DELETE)
    3610             :     {
    3611             :         Datum       datum;
    3612             :         bool        isNull;
    3613             : 
    3614          98 :         datum = ExecGetJunkAttribute(planSlot,
    3615          98 :                                      fmstate->ctidAttno,
    3616             :                                      &isNull);
    3617             :         /* shouldn't ever get a null result... */
    3618          98 :         if (isNull)
    3619           0 :             elog(ERROR, "ctid is NULL");
    3620          98 :         ctid = (ItemPointer) DatumGetPointer(datum);
    3621             :     }
    3622             : 
    3623             :     /* Convert parameters needed by prepared statement to text form */
    3624         530 :     p_values = convert_prep_stmt_params(fmstate, ctid, slot);
    3625             : 
    3626             :     /*
    3627             :      * Execute the prepared statement.
    3628             :      */
    3629        1060 :     if (!PQsendQueryPrepared(fmstate->conn,
    3630         530 :                              fmstate->p_name,
    3631             :                              fmstate->p_nums,
    3632             :                              p_values,
    3633             :                              NULL,
    3634             :                              NULL,
    3635             :                              0))
    3636           0 :         pgfdw_report_error(ERROR, NULL, fmstate->conn, false, fmstate->query);
    3637             : 
    3638             :     /*
    3639             :      * Get the result, and check for success.
    3640             :      *
    3641             :      * We don't use a PG_TRY block here, so be careful not to throw error
    3642             :      * without releasing the PGresult.
    3643             :      */
    3644         530 :     res = pgfdw_get_result(fmstate->conn, fmstate->query);
    3645        1060 :     if (PQresultStatus(res) !=
    3646         530 :         (fmstate->has_returning ? PGRES_TUPLES_OK : PGRES_COMMAND_OK))
    3647           8 :         pgfdw_report_error(ERROR, res, fmstate->conn, true, fmstate->query);
    3648             : 
    3649             :     /* Check number of rows affected, and fetch RETURNING tuple if any */
    3650         522 :     if (fmstate->has_returning)
    3651             :     {
    3652         146 :         n_rows = PQntuples(res);
    3653         146 :         if (n_rows > 0)
    3654         144 :             store_returning_result(fmstate, slot, res);
    3655             :     }
    3656             :     else
    3657         376 :         n_rows = atoi(PQcmdTuples(res));
    3658             : 
    3659             :     /* And clean up */
    3660         522 :     PQclear(res);
    3661             : 
    3662         522 :     MemoryContextReset(fmstate->temp_cxt);
    3663             : 
    3664             :     /*
    3665             :      * Return NULL if nothing was inserted/updated/deleted on the remote end
    3666             :      */
    3667         522 :     return (n_rows > 0) ? slot : NULL;
    3668             : }
    3669             : 
    3670             : /*
    3671             :  * prepare_foreign_modify
    3672             :  *      Establish a prepared statement for execution of INSERT/UPDATE/DELETE
    3673             :  */
    3674             : static void
    3675         204 : prepare_foreign_modify(PgFdwModifyState *fmstate)
    3676             : {
    3677             :     char        prep_name[NAMEDATALEN];
    3678             :     char       *p_name;
    3679             :     PGresult   *res;
    3680             : 
    3681             :     /* Construct name we'll use for the prepared statement. */
    3682         204 :     snprintf(prep_name, sizeof(prep_name), "pgsql_fdw_prep_%u",
    3683             :              GetPrepStmtNumber(fmstate->conn));
    3684         204 :     p_name = pstrdup(prep_name);
    3685             : 
    3686             :     /*
    3687             :      * We intentionally do not specify parameter types here, but leave the
    3688             :      * remote server to derive them by default.  This avoids possible problems
    3689             :      * with the remote server using different type OIDs than we do.  All of
    3690             :      * the prepared statements we use in this module are simple enough that
    3691             :      * the remote server will make the right choices.
    3692             :      */
    3693         204 :     if (!PQsendPrepare(fmstate->conn,
    3694             :                        p_name,
    3695         204 :                        fmstate->query,
    3696             :                        0,
    3697             :                        NULL))
    3698           0 :         pgfdw_report_error(ERROR, NULL, fmstate->conn, false, fmstate->query);
    3699             : 
    3700             :     /*
    3701             :      * Get the result, and check for success.
    3702             :      *
    3703             :      * We don't use a PG_TRY block here, so be careful not to throw error
    3704             :      * without releasing the PGresult.
    3705             :      */
    3706         204 :     res = pgfdw_get_result(fmstate->conn, fmstate->query);
    3707         204 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
    3708           0 :         pgfdw_report_error(ERROR, res, fmstate->conn, true, fmstate->query);
    3709         204 :     PQclear(res);
    3710             : 
    3711             :     /* This action shows that the prepare has been done. */
    3712         204 :     fmstate->p_name = p_name;
    3713         204 : }
    3714             : 
    3715             : /*
    3716             :  * convert_prep_stmt_params
    3717             :  *      Create array of text strings representing parameter values
    3718             :  *
    3719             :  * tupleid is ctid to send, or NULL if none
    3720             :  * slot is slot to get remaining parameters from, or NULL if none
    3721             :  *
    3722             :  * Data is constructed in temp_cxt; caller should reset that after use.
    3723             :  */
    3724             : static const char **
    3725         530 : convert_prep_stmt_params(PgFdwModifyState *fmstate,
    3726             :                          ItemPointer tupleid,
    3727             :                          TupleTableSlot *slot)
    3728             : {
    3729             :     const char **p_values;
    3730         530 :     int         pindex = 0;
    3731             :     MemoryContext oldcontext;
    3732             : 
    3733         530 :     oldcontext = MemoryContextSwitchTo(fmstate->temp_cxt);
    3734             : 
    3735         530 :     p_values = (const char **) palloc(sizeof(char *) * fmstate->p_nums);
    3736             : 
    3737             :     /* 1st parameter should be ctid, if it's in use */
    3738         530 :     if (tupleid != NULL)
    3739             :     {
    3740             :         /* don't need set_transmission_modes for TID output */
    3741          98 :         p_values[pindex] = OutputFunctionCall(&fmstate->p_flinfo[pindex],
    3742             :                                               PointerGetDatum(tupleid));
    3743          98 :         pindex++;
    3744             :     }
    3745             : 
    3746             :     /* get following parameters from slot */
    3747         530 :     if (slot != NULL && fmstate->target_attrs != NIL)
    3748             :     {
    3749             :         int         nestlevel;
    3750             :         ListCell   *lc;
    3751             : 
    3752         514 :         nestlevel = set_transmission_modes();
    3753             : 
    3754        3230 :         foreach(lc, fmstate->target_attrs)
    3755             :         {
    3756        2716 :             int         attnum = lfirst_int(lc);
    3757             :             Datum       value;
    3758             :             bool        isnull;
    3759             : 
    3760        2716 :             value = slot_getattr(slot, attnum, &isnull);
    3761        2716 :             if (isnull)
    3762        1158 :                 p_values[pindex] = NULL;
    3763             :             else
    3764        1558 :                 p_values[pindex] = OutputFunctionCall(&fmstate->p_flinfo[pindex],
    3765             :                                                       value);
    3766        2716 :             pindex++;
    3767             :         }
    3768             : 
    3769         514 :         reset_transmission_modes(nestlevel);
    3770             :     }
    3771             : 
    3772             :     Assert(pindex == fmstate->p_nums);
    3773             : 
    3774         530 :     MemoryContextSwitchTo(oldcontext);
    3775             : 
    3776         530 :     return p_values;
    3777             : }
    3778             : 
    3779             : /*
    3780             :  * store_returning_result
    3781             :  *      Store the result of a RETURNING clause
    3782             :  *
    3783             :  * On error, be sure to release the PGresult on the way out.  Callers do not
    3784             :  * have PG_TRY blocks to ensure this happens.
    3785             :  */
    3786             : static void
    3787         144 : store_returning_result(PgFdwModifyState *fmstate,
    3788             :                        TupleTableSlot *slot, PGresult *res)
    3789             : {
    3790         144 :     PG_TRY();
    3791             :     {
    3792             :         HeapTuple   newtup;
    3793             : 
    3794         144 :         newtup = make_tuple_from_result_row(res, 0,
    3795             :                                             fmstate->rel,
    3796             :                                             fmstate->attinmeta,
    3797             :                                             fmstate->retrieved_attrs,
    3798             :                                             NULL,
    3799             :                                             fmstate->temp_cxt);
    3800             : 
    3801             :         /*
    3802             :          * The returning slot will not necessarily be suitable to store
    3803             :          * heaptuples directly, so allow for conversion.
    3804             :          */
    3805         144 :         ExecForceStoreHeapTuple(newtup, slot, true);
    3806             :     }
    3807           0 :     PG_CATCH();
    3808             :     {
    3809           0 :         if (res)
    3810           0 :             PQclear(res);
    3811           0 :         PG_RE_THROW();
    3812             :     }
    3813         144 :     PG_END_TRY();
    3814         144 : }
    3815             : 
    3816             : /*
    3817             :  * finish_foreign_modify
    3818             :  *      Release resources for a foreign insert/update/delete operation
    3819             :  */
    3820             : static void
    3821         196 : finish_foreign_modify(PgFdwModifyState *fmstate)
    3822             : {
    3823             :     Assert(fmstate != NULL);
    3824             : 
    3825             :     /* If we created a prepared statement, destroy it */
    3826         196 :     if (fmstate->p_name)
    3827             :     {
    3828             :         char        sql[64];
    3829             :         PGresult   *res;
    3830             : 
    3831         188 :         snprintf(sql, sizeof(sql), "DEALLOCATE %s", fmstate->p_name);
    3832             : 
    3833             :         /*
    3834             :          * We don't use a PG_TRY block here, so be careful not to throw error
    3835             :          * without releasing the PGresult.
    3836             :          */
    3837         188 :         res = pgfdw_exec_query(fmstate->conn, sql);
    3838         188 :         if (PQresultStatus(res) != PGRES_COMMAND_OK)
    3839           0 :             pgfdw_report_error(ERROR, res, fmstate->conn, true, sql);
    3840         188 :         PQclear(res);
    3841         188 :         fmstate->p_name = NULL;
    3842             :     }
    3843             : 
    3844             :     /* Release remote connection */
    3845         196 :     ReleaseConnection(fmstate->conn);
    3846         196 :     fmstate->conn = NULL;
    3847         196 : }
    3848             : 
    3849             : /*
    3850             :  * build_remote_returning
    3851             :  *      Build a RETURNING targetlist of a remote query for performing an
    3852             :  *      UPDATE/DELETE .. RETURNING on a join directly
    3853             :  */
    3854             : static List *
    3855          16 : build_remote_returning(Index rtindex, Relation rel, List *returningList)
    3856             : {
    3857          16 :     bool        have_wholerow = false;
    3858          16 :     List       *tlist = NIL;
    3859             :     List       *vars;
    3860             :     ListCell   *lc;
    3861             : 
    3862             :     Assert(returningList);
    3863             : 
    3864          16 :     vars = pull_var_clause((Node *) returningList, PVC_INCLUDE_PLACEHOLDERS);
    3865             : 
    3866             :     /*
    3867             :      * If there's a whole-row reference to the target relation, then we'll
    3868             :      * need all the columns of the relation.
    3869             :      */
    3870          32 :     foreach(lc, vars)
    3871             :     {
    3872          24 :         Var        *var = (Var *) lfirst(lc);
    3873             : 
    3874          48 :         if (IsA(var, Var) &&
    3875          40 :             var->varno == rtindex &&
    3876          16 :             var->varattno == InvalidAttrNumber)
    3877             :         {
    3878           8 :             have_wholerow = true;
    3879           8 :             break;
    3880             :         }
    3881             :     }
    3882             : 
    3883          16 :     if (have_wholerow)
    3884             :     {
    3885           8 :         TupleDesc   tupdesc = RelationGetDescr(rel);
    3886             :         int         i;
    3887             : 
    3888          52 :         for (i = 1; i <= tupdesc->natts; i++)
    3889             :         {
    3890          44 :             Form_pg_attribute attr = TupleDescAttr(tupdesc, i - 1);
    3891             :             Var        *var;
    3892             : 
    3893             :             /* Ignore dropped attributes. */
    3894          44 :             if (attr->attisdropped)
    3895           4 :                 continue;
    3896             : 
    3897          40 :             var = makeVar(rtindex,
    3898             :                           i,
    3899             :                           attr->atttypid,
    3900             :                           attr->atttypmod,
    3901             :                           attr->attcollation,
    3902             :                           0);
    3903             : 
    3904          40 :             tlist = lappend(tlist,
    3905          40 :                             makeTargetEntry((Expr *) var,
    3906          40 :                                             list_length(tlist) + 1,
    3907             :                                             NULL,
    3908             :                                             false));
    3909             :         }
    3910             :     }
    3911             : 
    3912             :     /* Now add any remaining columns to tlist. */
    3913          88 :     foreach(lc, vars)
    3914             :     {
    3915          72 :         Var        *var = (Var *) lfirst(lc);
    3916             : 
    3917             :         /*
    3918             :          * No need for whole-row references to the target relation.  We don't
    3919             :          * need system columns other than ctid and oid either, since those are
    3920             :          * set locally.
    3921             :          */
    3922         144 :         if (IsA(var, Var) &&
    3923         120 :             var->varno == rtindex &&
    3924          56 :             var->varattno <= InvalidAttrNumber &&
    3925           8 :             var->varattno != SelfItemPointerAttributeNumber)
    3926           8 :             continue;           /* don't need it */
    3927             : 
    3928          64 :         if (tlist_member((Expr *) var, tlist))
    3929          32 :             continue;           /* already got it */
    3930             : 
    3931          32 :         tlist = lappend(tlist,
    3932          32 :                         makeTargetEntry((Expr *) var,
    3933          32 :                                         list_length(tlist) + 1,
    3934             :                                         NULL,
    3935             :                                         false));
    3936             :     }
    3937             : 
    3938          16 :     list_free(vars);
    3939             : 
    3940          16 :     return tlist;
    3941             : }
    3942             : 
    3943             : /*
    3944             :  * rebuild_fdw_scan_tlist
    3945             :  *      Build new fdw_scan_tlist of given foreign-scan plan node from given
    3946             :  *      tlist
    3947             :  *
    3948             :  * There might be columns that the fdw_scan_tlist of the given foreign-scan
    3949             :  * plan node contains that the given tlist doesn't.  The fdw_scan_tlist would
    3950             :  * have contained resjunk columns such as 'ctid' of the target relation and
    3951             :  * 'wholerow' of non-target relations, but the tlist might not contain them,
    3952             :  * for example.  So, adjust the tlist so it contains all the columns specified
    3953             :  * in the fdw_scan_tlist; else setrefs.c will get confused.
    3954             :  */
    3955             : static void
    3956          12 : rebuild_fdw_scan_tlist(ForeignScan *fscan, List *tlist)
    3957             : {
    3958          12 :     List       *new_tlist = tlist;
    3959          12 :     List       *old_tlist = fscan->fdw_scan_tlist;
    3960             :     ListCell   *lc;
    3961             : 
    3962          96 :     foreach(lc, old_tlist)
    3963             :     {
    3964          84 :         TargetEntry *tle = (TargetEntry *) lfirst(lc);
    3965             : 
    3966          84 :         if (tlist_member(tle->expr, new_tlist))
    3967          60 :             continue;           /* already got it */
    3968             : 
    3969          24 :         new_tlist = lappend(new_tlist,
    3970          24 :                             makeTargetEntry(tle->expr,
    3971          24 :                                             list_length(new_tlist) + 1,
    3972             :                                             NULL,
    3973             :                                             false));
    3974             :     }
    3975          12 :     fscan->fdw_scan_tlist = new_tlist;
    3976          12 : }
    3977             : 
    3978             : /*
    3979             :  * Execute a direct UPDATE/DELETE statement.
    3980             :  */
    3981             : static void
    3982         108 : execute_dml_stmt(ForeignScanState *node)
    3983             : {
    3984         108 :     PgFdwDirectModifyState *dmstate = (PgFdwDirectModifyState *) node->fdw_state;
    3985         108 :     ExprContext *econtext = node->ss.ps.ps_ExprContext;
    3986         108 :     int         numParams = dmstate->numParams;
    3987         108 :     const char **values = dmstate->param_values;
    3988             : 
    3989             :     /*
    3990             :      * Construct array of query parameter values in text format.
    3991             :      */
    3992         108 :     if (numParams > 0)
    3993           0 :         process_query_params(econtext,
    3994             :                              dmstate->param_flinfo,
    3995             :                              dmstate->param_exprs,
    3996             :                              values);
    3997             : 
    3998             :     /*
    3999             :      * Notice that we pass NULL for paramTypes, thus forcing the remote server
    4000             :      * to infer types for all parameters.  Since we explicitly cast every
    4001             :      * parameter (see deparse.c), the "inference" is trivial and will produce
    4002             :      * the desired result.  This allows us to avoid assuming that the remote
    4003             :      * server has the same OIDs we do for the parameters' types.
    4004             :      */
    4005         108 :     if (!PQsendQueryParams(dmstate->conn, dmstate->query, numParams,
    4006             :                            NULL, values, NULL, NULL, 0))
    4007           0 :         pgfdw_report_error(ERROR, NULL, dmstate->conn, false, dmstate->query);
    4008             : 
    4009             :     /*
    4010             :      * Get the result, and check for success.
    4011             :      *
    4012             :      * We don't use a PG_TRY block here, so be careful not to throw error
    4013             :      * without releasing the PGresult.
    4014             :      */
    4015         108 :     dmstate->result = pgfdw_get_result(dmstate->conn, dmstate->query);
    4016         216 :     if (PQresultStatus(dmstate->result) !=
    4017         108 :         (dmstate->has_returning ? PGRES_TUPLES_OK : PGRES_COMMAND_OK))
    4018           8 :         pgfdw_report_error(ERROR, dmstate->result, dmstate->conn, true,
    4019           8 :                            dmstate->query);
    4020             : 
    4021             :     /* Get the number of rows affected. */
    4022         100 :     if (dmstate->has_returning)
    4023          22 :         dmstate->num_tuples = PQntuples(dmstate->result);
    4024             :     else
    4025          78 :         dmstate->num_tuples = atoi(PQcmdTuples(dmstate->result));
    4026         100 : }
    4027             : 
    4028             : /*
    4029             :  * Get the result of a RETURNING clause.
    4030             :  */
    4031             : static TupleTableSlot *
    4032         720 : get_returning_data(ForeignScanState *node)
    4033             : {
    4034         720 :     PgFdwDirectModifyState *dmstate = (PgFdwDirectModifyState *) node->fdw_state;
    4035         720 :     EState     *estate = node->ss.ps.state;
    4036         720 :     ResultRelInfo *resultRelInfo = estate->es_result_relation_info;
    4037         720 :     TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;
    4038             :     TupleTableSlot *resultSlot;
    4039             : 
    4040             :     Assert(resultRelInfo->ri_projectReturning);
    4041             : 
    4042             :     /* If we didn't get any tuples, must be end of data. */
    4043         720 :     if (dmstate->next_tuple >= dmstate->num_tuples)
    4044          28 :         return ExecClearTuple(slot);
    4045             : 
    4046             :     /* Increment the command es_processed count if necessary. */
    4047         692 :     if (dmstate->set_processed)
    4048         692 :         estate->es_processed += 1;
    4049             : 
    4050             :     /*
    4051             :      * Store a RETURNING tuple.  If has_returning is false, just emit a dummy
    4052             :      * tuple.  (has_returning is false when the local query is of the form
    4053             :      * "UPDATE/DELETE .. RETURNING 1" for example.)
    4054             :      */
    4055         692 :     if (!dmstate->has_returning)
    4056             :     {
    4057          24 :         ExecStoreAllNullTuple(slot);
    4058          24 :         resultSlot = slot;
    4059             :     }
    4060             :     else
    4061             :     {
    4062             :         /*
    4063             :          * On error, be sure to release the PGresult on the way out.  Callers
    4064             :          * do not have PG_TRY blocks to ensure this happens.
    4065             :          */
    4066         668 :         PG_TRY();
    4067             :         {
    4068             :             HeapTuple   newtup;
    4069             : 
    4070         668 :             newtup = make_tuple_from_result_row(dmstate->result,
    4071             :                                                 dmstate->next_tuple,
    4072             :                                                 dmstate->rel,
    4073             :                                                 dmstate->attinmeta,
    4074             :                                                 dmstate->retrieved_attrs,
    4075             :                                                 node,
    4076             :                                                 dmstate->temp_cxt);
    4077         668 :             ExecStoreHeapTuple(newtup, slot, false);
    4078             :         }
    4079           0 :         PG_CATCH();
    4080             :         {
    4081           0 :             if (dmstate->result)
    4082           0 :                 PQclear(dmstate->result);
    4083           0 :             PG_RE_THROW();
    4084             :         }
    4085         668 :         PG_END_TRY();
    4086             : 
    4087             :         /* Get the updated/deleted tuple. */
    4088         668 :         if (dmstate->rel)
    4089         628 :             resultSlot = slot;
    4090             :         else
    4091          40 :             resultSlot = apply_returning_filter(dmstate, slot, estate);
    4092             :     }
    4093         692 :     dmstate->next_tuple++;
    4094             : 
    4095             :     /* Make slot available for evaluation of the local query RETURNING list. */
    4096         692 :     resultRelInfo->ri_projectReturning->pi_exprContext->ecxt_scantuple =
    4097             :         resultSlot;
    4098             : 
    4099         692 :     return slot;
    4100             : }
    4101             : 
    4102             : /*
    4103             :  * Initialize a filter to extract an updated/deleted tuple from a scan tuple.
    4104             :  */
    4105             : static void
    4106           6 : init_returning_filter(PgFdwDirectModifyState *dmstate,
    4107             :                       List *fdw_scan_tlist,
    4108             :                       Index rtindex)
    4109             : {
    4110           6 :     TupleDesc   resultTupType = RelationGetDescr(dmstate->resultRel);
    4111             :     ListCell   *lc;
    4112             :     int         i;
    4113             : 
    4114             :     /*
    4115             :      * Calculate the mapping between the fdw_scan_tlist's entries and the
    4116             :      * result tuple's attributes.
    4117             :      *
    4118             :      * The "map" is an array of indexes of the result tuple's attributes in
    4119             :      * fdw_scan_tlist, i.e., one entry for every attribute of the result
    4120             :      * tuple.  We store zero for any attributes that don't have the
    4121             :      * corresponding entries in that list, marking that a NULL is needed in
    4122             :      * the result tuple.
    4123             :      *
    4124             :      * Also get the indexes of the entries for ctid and oid if any.
    4125             :      */
    4126           6 :     dmstate->attnoMap = (AttrNumber *)
    4127           6 :         palloc0(resultTupType->natts * sizeof(AttrNumber));
    4128             : 
    4129           6 :     dmstate->ctidAttno = dmstate->oidAttno = 0;
    4130             : 
    4131           6 :     i = 1;
    4132           6 :     dmstate->hasSystemCols = false;
    4133          54 :     foreach(lc, fdw_scan_tlist)
    4134             :     {
    4135          48 :         TargetEntry *tle = (TargetEntry *) lfirst(lc);
    4136          48 :         Var        *var = (Var *) tle->expr;
    4137             : 
    4138             :         Assert(IsA(var, Var));
    4139             : 
    4140             :         /*
    4141             :          * If the Var is a column of the target relation to be retrieved from
    4142             :          * the foreign server, get the index of the entry.
    4143             :          */
    4144          78 :         if (var->varno == rtindex &&
    4145          30 :             list_member_int(dmstate->retrieved_attrs, i))
    4146             :         {
    4147          24 :             int         attrno = var->varattno;
    4148             : 
    4149          24 :             if (attrno < 0)
    4150             :             {
    4151             :                 /*
    4152             :                  * We don't retrieve system columns other than ctid and oid.
    4153             :                  */
    4154           0 :                 if (attrno == SelfItemPointerAttributeNumber)
    4155           0 :                     dmstate->ctidAttno = i;
    4156             :                 else
    4157             :                     Assert(false);
    4158           0 :                 dmstate->hasSystemCols = true;
    4159             :             }
    4160             :             else
    4161             :             {
    4162             :                 /*
    4163             :                  * We don't retrieve whole-row references to the target
    4164             :                  * relation either.
    4165             :                  */
    4166             :                 Assert(attrno > 0);
    4167             : 
    4168          24 :                 dmstate->attnoMap[attrno - 1] = i;
    4169             :             }
    4170             :         }
    4171          48 :         i++;
    4172             :     }
    4173           6 : }
    4174             : 
    4175             : /*
    4176             :  * Extract and return an updated/deleted tuple from a scan tuple.
    4177             :  */
    4178             : static TupleTableSlot *
    4179          40 : apply_returning_filter(PgFdwDirectModifyState *dmstate,
    4180             :                        TupleTableSlot *slot,
    4181             :                        EState *estate)
    4182             : {
    4183          40 :     ResultRelInfo *relInfo = estate->es_result_relation_info;
    4184          40 :     TupleDesc   resultTupType = RelationGetDescr(dmstate->resultRel);
    4185             :     TupleTableSlot *resultSlot;
    4186             :     Datum      *values;
    4187             :     bool       *isnull;
    4188             :     Datum      *old_values;
    4189             :     bool       *old_isnull;
    4190             :     int         i;
    4191             : 
    4192             :     /*
    4193             :      * Use the return tuple slot as a place to store the result tuple.
    4194             :      */
    4195          40 :     resultSlot = ExecGetReturningSlot(estate, relInfo);
    4196             : 
    4197             :     /*
    4198             :      * Extract all the values of the scan tuple.
    4199             :      */
    4200          40 :     slot_getallattrs(slot);
    4201          40 :     old_values = slot->tts_values;
    4202          40 :     old_isnull = slot->tts_isnull;
    4203             : 
    4204             :     /*
    4205             :      * Prepare to build the result tuple.
    4206             :      */
    4207          40 :     ExecClearTuple(resultSlot);
    4208          40 :     values = resultSlot->tts_values;
    4209          40 :     isnull = resultSlot->tts_isnull;
    4210             : 
    4211             :     /*
    4212             :      * Transpose data into proper fields of the result tuple.
    4213             :      */
    4214         344 :     for (i = 0; i < resultTupType->natts; i++)
    4215             :     {
    4216         304 :         int         j = dmstate->attnoMap[i];
    4217             : 
    4218         304 :         if (j == 0)
    4219             :         {
    4220          32 :             values[i] = (Datum) 0;
    4221          32 :             isnull[i] = true;
    4222             :         }
    4223             :         else
    4224             :         {
    4225         272 :             values[i] = old_values[j - 1];
    4226         272 :             isnull[i] = old_isnull[j - 1];
    4227             :         }
    4228             :     }
    4229             : 
    4230             :     /*
    4231             :      * Build the virtual tuple.
    4232             :      */
    4233          40 :     ExecStoreVirtualTuple(resultSlot);
    4234             : 
    4235             :     /*
    4236             :      * If we have any system columns to return, materialize a heap tuple in
    4237             :      * the slot from column values set above and install system columns in
    4238             :      * that tuple.
    4239             :      */
    4240          40 :     if (dmstate->hasSystemCols)
    4241             :     {
    4242           0 :         HeapTuple   resultTup = ExecFetchSlotHeapTuple(resultSlot, true, NULL);
    4243             : 
    4244             :         /* ctid */
    4245           0 :         if (dmstate->ctidAttno)
    4246             :         {
    4247           0 :             ItemPointer ctid = NULL;
    4248             : 
    4249           0 :             ctid = (ItemPointer) DatumGetPointer(old_values[dmstate->ctidAttno - 1]);
    4250           0 :             resultTup->t_self = *ctid;
    4251             :         }
    4252             : 
    4253             :         /*
    4254             :          * And remaining columns
    4255             :          *
    4256             :          * Note: since we currently don't allow the target relation to appear
    4257             :          * on the nullable side of an outer join, any system columns wouldn't
    4258             :          * go to NULL.
    4259             :          *
    4260             :          * Note: no need to care about tableoid here because it will be
    4261             :          * initialized in ExecProcessReturning().
    4262             :          */
    4263           0 :         HeapTupleHeaderSetXmin(resultTup->t_data, InvalidTransactionId);
    4264           0 :         HeapTupleHeaderSetXmax(resultTup->t_data, InvalidTransactionId);
    4265           0 :         HeapTupleHeaderSetCmin(resultTup->t_data, InvalidTransactionId);
    4266             :     }
    4267             : 
    4268             :     /*
    4269             :      * And return the result tuple.
    4270             :      */
    4271          40 :     return resultSlot;
    4272             : }
    4273             : 
    4274             : /*
    4275             :  * Prepare for processing of parameters used in remote query.
    4276             :  */
    4277             : static void
    4278          16 : prepare_query_params(PlanState *node,
    4279             :                      List *fdw_exprs,
    4280             :                      int numParams,
    4281             :                      FmgrInfo **param_flinfo,
    4282             :                      List **param_exprs,
    4283             :                      const char ***param_values)
    4284             : {
    4285             :     int         i;
    4286             :     ListCell   *lc;
    4287             : 
    4288             :     Assert(numParams > 0);
    4289             : 
    4290             :     /* Prepare for output conversion of parameters used in remote query. */
    4291          16 :     *param_flinfo = (FmgrInfo *) palloc0(sizeof(FmgrInfo) * numParams);
    4292             : 
    4293          16 :     i = 0;
    4294          34 :     foreach(lc, fdw_exprs)
    4295             :     {
    4296          18 :         Node       *param_expr = (Node *) lfirst(lc);
    4297             :         Oid         typefnoid;
    4298             :         bool        isvarlena;
    4299             : 
    4300          18 :         getTypeOutputInfo(exprType(param_expr), &typefnoid, &isvarlena);
    4301          18 :         fmgr_info(typefnoid, &(*param_flinfo)[i]);
    4302          18 :         i++;
    4303             :     }
    4304             : 
    4305             :     /*
    4306             :      * Prepare remote-parameter expressions for evaluation.  (Note: in
    4307             :      * practice, we expect that all these expressions will be just Params, so
    4308             :      * we could possibly do something more efficient than using the full
    4309             :      * expression-eval machinery for this.  But probably there would be little
    4310             :      * benefit, and it'd require postgres_fdw to know more than is desirable
    4311             :      * about Param evaluation.)
    4312             :      */
    4313          16 :     *param_exprs = ExecInitExprList(fdw_exprs, node);
    4314             : 
    4315             :     /* Allocate buffer for text form of query parameters. */
    4316          16 :     *param_values = (const char **) palloc0(numParams * sizeof(char *));
    4317          16 : }
    4318             : 
    4319             : /*
    4320             :  * Construct array of query parameter values in text format.
    4321             :  */
    4322             : static void
    4323         668 : process_query_params(ExprContext *econtext,
    4324             :                      FmgrInfo *param_flinfo,
    4325             :                      List *param_exprs,
    4326             :                      const char **param_values)
    4327             : {
    4328             :     int         nestlevel;
    4329             :     int         i;
    4330             :     ListCell   *lc;
    4331             : 
    4332         668 :     nestlevel = set_transmission_modes();
    4333             : 
    4334         668 :     i = 0;
    4335        1736 :     foreach(lc, param_exprs)
    4336             :     {
    4337        1068 :         ExprState  *expr_state = (ExprState *) lfirst(lc);
    4338             :         Datum       expr_value;
    4339             :         bool        isNull;
    4340             : 
    4341             :         /* Evaluate the parameter expression */
    4342        1068 :         expr_value = ExecEvalExpr(expr_state, econtext, &isNull);
    4343             : 
    4344             :         /*
    4345             :          * Get string representation of each parameter value by invoking
    4346             :          * type-specific output function, unless the value is null.
    4347             :          */
    4348        1068 :         if (isNull)
    4349           0 :             param_values[i] = NULL;
    4350             :         else
    4351        1068 :             param_values[i] = OutputFunctionCall(&param_flinfo[i], expr_value);
    4352             : 
    4353        1068 :         i++;
    4354             :     }
    4355             : 
    4356         668 :     reset_transmission_modes(nestlevel);
    4357         668 : }
    4358             : 
    4359             : /*
    4360             :  * postgresAnalyzeForeignTable
    4361             :  *      Test whether analyzing this foreign table is supported
    4362             :  */
    4363             : static bool
    4364          48 : postgresAnalyzeForeignTable(Relation relation,
    4365             :                             AcquireSampleRowsFunc *func,
    4366             :                             BlockNumber *totalpages)
    4367             : {
    4368             :     ForeignTable *table;
    4369             :     UserMapping *user;
    4370             :     PGconn     *conn;
    4371             :     StringInfoData sql;
    4372          48 :     PGresult   *volatile res = NULL;
    4373             : 
    4374             :     /* Return the row-analysis function pointer */
    4375          48 :     *func = postgresAcquireSampleRowsFunc;
    4376             : 
    4377             :     /*
    4378             :      * Now we have to get the number of pages.  It's annoying that the ANALYZE
    4379             :      * API requires us to return that now, because it forces some duplication
    4380             :      * of effort between this routine and postgresAcquireSampleRowsFunc.  But
    4381             :      * it's probably not worth redefining that API at this point.
    4382             :      */
    4383             : 
    4384             :     /*
    4385             :      * Get the connection to use.  We do the remote access as the table's
    4386             :      * owner, even if the ANALYZE was started by some other user.
    4387             :      */
    4388          48 :     table = GetForeignTable(RelationGetRelid(relation));
    4389          48 :     user = GetUserMapping(relation->rd_rel->relowner, table->serverid);
    4390          48 :     conn = GetConnection(user, false);
    4391             : 
    4392             :     /*
    4393             :      * Construct command to get page count for relation.
    4394             :      */
    4395          48 :     initStringInfo(&sql);
    4396          48 :     deparseAnalyzeSizeSql(&sql, relation);
    4397             : 
    4398             :     /* In what follows, do not risk leaking any PGresults. */
    4399          48 :     PG_TRY();
    4400             :     {
    4401          48 :         res = pgfdw_exec_query(conn, sql.data);
    4402          48 :         if (PQresultStatus(res) != PGRES_TUPLES_OK)
    4403           0 :             pgfdw_report_error(ERROR, res, conn, false, sql.data);
    4404             : 
    4405          48 :         if (PQntuples(res) != 1 || PQnfields(res) != 1)
    4406           0 :             elog(ERROR, "unexpected result from deparseAnalyzeSizeSql query");
    4407          48 :         *totalpages = strtoul(PQgetvalue(res, 0, 0), NULL, 10);
    4408             : 
    4409          48 :         PQclear(res);
    4410          48 :         res = NULL;
    4411             :     }
    4412           0 :     PG_CATCH();
    4413             :     {
    4414           0 :         if (res)
    4415           0 :             PQclear(res);
    4416           0 :         PG_RE_THROW();
    4417             :     }
    4418          48 :     PG_END_TRY();
    4419             : 
    4420          48 :     ReleaseConnection(conn);
    4421             : 
    4422          48 :     return true;
    4423             : }
    4424             : 
    4425             : /*
    4426             :  * Acquire a random sample of rows from foreign table managed by postgres_fdw.
    4427             :  *
    4428             :  * We fetch the whole table from the remote side and pick out some sample rows.
    4429             :  *
    4430             :  * Selected rows are returned in the caller-allocated array rows[],
    4431             :  * which must have at least targrows entries.
    4432             :  * The actual number of rows selected is returned as the function result.
    4433             :  * We also count the total number of rows in the table and return it into
    4434             :  * *totalrows.  Note that *totaldeadrows is always set to 0.
    4435             :  *
    4436             :  * Note that the returned list of rows is not always in order by physical
    4437             :  * position in the table.  Therefore, correlation estimates derived later
    4438             :  * may be meaningless, but it's OK because we don't use the estimates
    4439             :  * currently (the planner only pays attention to correlation for indexscans).
    4440             :  */
    4441             : static int
    4442          48 : postgresAcquireSampleRowsFunc(Relation relation, int elevel,
    4443             :                               HeapTuple *rows, int targrows,
    4444             :                               double *totalrows,
    4445             :                               double *totaldeadrows)
    4446             : {
    4447             :     PgFdwAnalyzeState astate;
    4448             :     ForeignTable *table;
    4449             :     ForeignServer *server;
    4450             :     UserMapping *user;
    4451             :     PGconn     *conn;
    4452             :     unsigned int cursor_number;
    4453             :     StringInfoData sql;
    4454          48 :     PGresult   *volatile res = NULL;
    4455             : 
    4456             :     /* Initialize workspace state */
    4457          48 :     astate.rel = relation;
    4458          48 :     astate.attinmeta = TupleDescGetAttInMetadata(RelationGetDescr(relation));
    4459             : 
    4460          48 :     astate.rows = rows;
    4461          48 :     astate.targrows = targrows;
    4462          48 :     astate.numrows = 0;
    4463          48 :     astate.samplerows = 0;
    4464          48 :     astate.rowstoskip = -1;     /* -1 means not set yet */
    4465          48 :     reservoir_init_selection_state(&astate.rstate, targrows);
    4466             : 
    4467             :     /* Remember ANALYZE context, and create a per-tuple temp context */
    4468          48 :     astate.anl_cxt = CurrentMemoryContext;
    4469          48 :     astate.temp_cxt = AllocSetContextCreate(CurrentMemoryContext,
    4470             :                                             "postgres_fdw temporary data",
    4471             :                                             ALLOCSET_SMALL_SIZES);
    4472             : 
    4473             :     /*
    4474             :      * Get the connection to use.  We do the remote access as the table's
    4475             :      * owner, even if the ANALYZE was started by some other user.
    4476             :      */
    4477          48 :     table = GetForeignTable(RelationGetRelid(relation));
    4478          48 :     server = GetForeignServer(table->serverid);
    4479          48 :     user = GetUserMapping(relation->rd_rel->relowner, table->serverid);
    4480          48 :     conn = GetConnection(user, false);
    4481             : 
    4482             :     /*
    4483             :      * Construct cursor that retrieves whole rows from remote.
    4484             :      */
    4485          48 :     cursor_number = GetCursorNumber(conn);
    4486          48 :     initStringInfo(&sql);
    4487          48 :     appendStringInfo(&sql, "DECLARE c%u CURSOR FOR ", cursor_number);
    4488          48 :     deparseAnalyzeSql(&sql, relation, &astate.retrieved_attrs);
    4489             : 
    4490             :     /* In what follows, do not risk leaking any PGresults. */
    4491          48 :     PG_TRY();
    4492             :     {
    4493          48 :         res = pgfdw_exec_query(conn, sql.data);
    4494          48 :         if (PQresultStatus(res) != PGRES_COMMAND_OK)
    4495           0 :             pgfdw_report_error(ERROR, res, conn, false, sql.data);
    4496          48 :         PQclear(res);
    4497          48 :         res = NULL;
    4498             : 
    4499             :         /* Retrieve and process rows a batch at a time. */
    4500             :         for (;;)
    4501         248 :         {
    4502             :             char        fetch_sql[64];
    4503             :             int         fetch_size;
    4504             :             int         numrows;
    4505             :             int         i;
    4506             :             ListCell   *lc;
    4507             : 
    4508             :             /* Allow users to cancel long query */
    4509         296 :             CHECK_FOR_INTERRUPTS();
    4510             : 
    4511             :             /*
    4512             :              * XXX possible future improvement: if rowstoskip is large, we
    4513             :              * could issue a MOVE rather than physically fetching the rows,
    4514             :              * then just adjust rowstoskip and samplerows appropriately.
    4515             :              */
    4516             : 
    4517             :             /* The fetch size is arbitrary, but shouldn't be enormous. */
    4518         296 :             fetch_size = 100;
    4519        1162 :             foreach(lc, server->options)
    4520             :             {
    4521         866 :                 DefElem    *def = (DefElem *) lfirst(lc);
    4522             : 
    4523         866 :                 if (strcmp(def->defname, "fetch_size") == 0)
    4524             :                 {
    4525           0 :                     fetch_size = strtol(defGetString(def), NULL, 10);
    4526           0 :                     break;
    4527             :                 }
    4528             :             }
    4529         700 :             foreach(lc, table->options)
    4530             :             {
    4531         404 :                 DefElem    *def = (DefElem *) lfirst(lc);
    4532             : 
    4533         404 :                 if (strcmp(def->defname, "fetch_size") == 0)
    4534             :                 {
    4535           0 :                     fetch_size = strtol(defGetString(def), NULL, 10);
    4536           0 :                     break;
    4537             :                 }
    4538             :             }
    4539             : 
    4540             :             /* Fetch some rows */
    4541         296 :             snprintf(fetch_sql, sizeof(fetch_sql), "FETCH %d FROM c%u",
    4542             :                      fetch_size, cursor_number);
    4543             : 
    4544         296 :             res = pgfdw_exec_query(conn, fetch_sql);
    4545             :             /* On error, report the original query, not the FETCH. */
    4546         296 :             if (PQresultStatus(res) != PGRES_TUPLES_OK)
    4547           0 :                 pgfdw_report_error(ERROR, res, conn, false, sql.data);
    4548             : 
    4549             :             /* Process whatever we got. */
    4550         296 :             numrows = PQntuples(res);
    4551       26144 :             for (i = 0; i < numrows; i++)
    4552       25848 :                 analyze_row_processor(res, i, &astate);
    4553             : 
    4554         296 :             PQclear(res);
    4555         296 :             res = NULL;
    4556             : 
    4557             :             /* Must be EOF if we didn't get all the rows requested. */
    4558         296 :             if (numrows < fetch_size)
    4559          48 :                 break;
    4560             :         }
    4561             : 
    4562             :         /* Close the cursor, just to be tidy. */
    4563          48 :         close_cursor(conn, cursor_number);
    4564             :     }
    4565           0 :     PG_CATCH();
    4566             :     {
    4567           0 :         if (res)
    4568           0 :             PQclear(res);
    4569           0 :         PG_RE_THROW();
    4570             :     }
    4571          48 :     PG_END_TRY();
    4572             : 
    4573          48 :     ReleaseConnection(conn);
    4574             : 
    4575             :     /* We assume that we have no dead tuple. */
    4576          48 :     *totaldeadrows = 0.0;
    4577             : 
    4578             :     /* We've retrieved all living tuples from foreign server. */
    4579          48 :     *totalrows = astate.samplerows;
    4580             : 
    4581             :     /*
    4582             :      * Emit some interesting relation info
    4583             :      */
    4584          48 :     ereport(elevel,
    4585             :             (errmsg("\"%s\": table contains %.0f rows, %d rows in sample",
    4586             :                     RelationGetRelationName(relation),
    4587             :                     astate.samplerows, astate.numrows)));
    4588             : 
    4589          48 :     return astate.numrows;
    4590             : }
    4591             : 
    4592             : /*
    4593             :  * Collect sample rows from the result of query.
    4594             :  *   - Use all tuples in sample until target # of samples are collected.
    4595             :  *   - Subsequently, replace already-sampled tuples randomly.
    4596             :  */
    4597             : static void
    4598       25848 : analyze_row_processor(PGresult *res, int row, PgFdwAnalyzeState *astate)
    4599             : {
    4600       25848 :     int         targrows = astate->targrows;
    4601             :     int         pos;            /* array index to store tuple in */
    4602             :     MemoryContext oldcontext;
    4603             : 
    4604             :     /* Always increment sample row counter. */
    4605       25848 :     astate->samplerows += 1;
    4606             : 
    4607             :     /*
    4608             :      * Determine the slot where this sample row should be stored.  Set pos to
    4609             :      * negative value to indicate the row should be skipped.
    4610             :      */
    4611       25848 :     if (astate->numrows < targrows)
    4612             :     {
    4613             :         /* First targrows rows are always included into the sample */
    4614       25848 :         pos = astate->numrows++;
    4615             :     }
    4616             :     else
    4617             :     {
    4618             :         /*
    4619             :          * Now we start replacing tuples in the sample until we reach the end
    4620             :          * of the relation.  Same algorithm as in acquire_sample_rows in
    4621             :          * analyze.c; see Jeff Vitter's paper.
    4622             :          */
    4623           0 :         if (astate->rowstoskip < 0)
    4624           0 :             astate->rowstoskip = reservoir_get_next_S(&astate->rstate, astate->samplerows, targrows);
    4625             : 
    4626           0 :         if (astate->rowstoskip <= 0)
    4627             :         {
    4628             :             /* Choose a random reservoir element to replace. */
    4629           0 :             pos = (int) (targrows * sampler_random_fract(astate->rstate.randstate));
    4630             :             Assert(pos >= 0 && pos < targrows);
    4631           0 :             heap_freetuple(astate->rows[pos]);
    4632             :         }
    4633             :         else
    4634             :         {
    4635             :             /* Skip this tuple. */
    4636           0 :             pos = -1;
    4637             :         }
    4638             : 
    4639           0 :         astate->rowstoskip -= 1;
    4640             :     }
    4641             : 
    4642       25848 :     if (pos >= 0)
    4643             :     {
    4644             :         /*
    4645             :          * Create sample tuple from current result row, and store it in the
    4646             :          * position determined above.  The tuple has to be created in anl_cxt.
    4647             :          */
    4648       25848 :         oldcontext = MemoryContextSwitchTo(astate->anl_cxt);
    4649             : 
    4650       25848 :         astate->rows[pos] = make_tuple_from_result_row(res, row,
    4651             :                                                        astate->rel,
    4652             :                                                        astate->attinmeta,
    4653             :                                                        astate->retrieved_attrs,
    4654             :                                                        NULL,
    4655             :                                                        astate->temp_cxt);
    4656             : 
    4657       25848 :         MemoryContextSwitchTo(oldcontext);
    4658             :     }
    4659       25848 : }
    4660             : 
    4661             : /*
    4662             :  * Import a foreign schema
    4663             :  */
    4664             : static List *
    4665          16 : postgresImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid)
    4666             : {
    4667          16 :     List       *commands = NIL;
    4668          16 :     bool        import_collate = true;
    4669          16 :     bool        import_default = false;
    4670          16 :     bool        import_not_null = true;
    4671             :     ForeignServer *server;
    4672             :     UserMapping *mapping;
    4673             :     PGconn     *conn;
    4674             :     StringInfoData buf;
    4675          16 :     PGresult   *volatile res = NULL;
    4676             :     int         numrows,
    4677             :                 i;
    4678             :     ListCell   *lc;
    4679             : 
    4680             :     /* Parse statement options */
    4681          22 :     foreach(lc, stmt->options)
    4682             :     {
    4683           6 :         DefElem    *def = (DefElem *) lfirst(lc);
    4684             : 
    4685           6 :         if (strcmp(def->defname, "import_collate") == 0)
    4686           2 :             import_collate = defGetBoolean(def);
    4687           4 :         else if (strcmp(def->defname, "import_default") == 0)
    4688           2 :             import_default = defGetBoolean(def);
    4689           2 :         else if (strcmp(def->defname, "import_not_null") == 0)
    4690           2 :             import_not_null = defGetBoolean(def);
    4691             :         else
    4692           0 :             ereport(ERROR,
    4693             :                     (errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
    4694             :                      errmsg("invalid option \"%s\"", def->defname)));
    4695             :     }
    4696             : 
    4697             :     /*
    4698             :      * Get connection to the foreign server.  Connection manager will
    4699             :      * establish new connection if necessary.
    4700             :      */
    4701          16 :     server = GetForeignServer(serverOid);
    4702          16 :     mapping = GetUserMapping(GetUserId(), server->serverid);
    4703          16 :     conn = GetConnection(mapping, false);
    4704             : 
    4705             :     /* Don't attempt to import collation if remote server hasn't got it */
    4706          16 :     if (PQserverVersion(conn) < 90100)
    4707           0 :         import_collate = false;
    4708             : 
    4709             :     /* Create workspace for strings */
    4710          16 :     initStringInfo(&buf);
    4711             : 
    4712             :     /* In what follows, do not risk leaking any PGresults. */
    4713          16 :     PG_TRY();
    4714             :     {
    4715             :         /* Check that the schema really exists */
    4716          16 :         appendStringInfoString(&buf, "SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname = ");
    4717          16 :         deparseStringLiteral(&buf, stmt->remote_schema);
    4718             : 
    4719          16 :         res = pgfdw_exec_query(conn, buf.data);
    4720          16 :         if (PQresultStatus(res) != PGRES_TUPLES_OK)
    4721           0 :             pgfdw_report_error(ERROR, res, conn, false, buf.data);
    4722             : 
    4723          16 :         if (PQntuples(res) != 1)
    4724           2 :             ereport(ERROR,
    4725             :                     (errcode(ERRCODE_FDW_SCHEMA_NOT_FOUND),
    4726             :                      errmsg("schema \"%s\" is not present on foreign server \"%s\"",
    4727             :                             stmt->remote_schema, server->servername)));
    4728             : 
    4729          14 :         PQclear(res);
    4730          14 :         res = NULL;
    4731          14 :         resetStringInfo(&buf);
    4732             : 
    4733             :         /*
    4734             :          * Fetch all table data from this schema, possibly restricted by
    4735             :          * EXCEPT or LIMIT TO.  (We don't actually need to pay any attention
    4736             :          * to EXCEPT/LIMIT TO here, because the core code will filter the
    4737             :          * statements we return according to those lists anyway.  But it
    4738             :          * should save a few cycles to not process excluded tables in the
    4739             :          * first place.)
    4740             :          *
    4741             :          * Ignore table data for partitions and only include the definitions
    4742             :          * of the root partitioned tables to allow access to the complete
    4743             :          * remote data set locally in the schema imported.
    4744             :          *
    4745             :          * Note: because we run the connection with search_path restricted to
    4746             :          * pg_catalog, the format_type() and pg_get_expr() outputs will always
    4747             :          * include a schema name for types/functions in other schemas, which
    4748             :          * is what we want.
    4749             :          */
    4750          14 :         if (import_collate)
    4751          12 :             appendStringInfoString(&buf,
    4752             :                                    "SELECT relname, "
    4753             :                                    "  attname, "
    4754             :                                    "  format_type(atttypid, atttypmod), "
    4755             :                                    "  attnotnull, "
    4756             :                                    "  pg_get_expr(adbin, adrelid), "
    4757             :                                    "  collname, "
    4758             :                                    "  collnsp.nspname "
    4759             :                                    "FROM pg_class c "
    4760             :                                    "  JOIN pg_namespace n ON "
    4761             :                                    "    relnamespace = n.oid "
    4762             :                                    "  LEFT JOIN pg_attribute a ON "
    4763             :                                    "    attrelid = c.oid AND attnum > 0 "
    4764             :                                    "      AND NOT attisdropped "
    4765             :                                    "  LEFT JOIN pg_attrdef ad ON "
    4766             :                                    "    adrelid = c.oid AND adnum = attnum "
    4767             :                                    "  LEFT JOIN pg_collation coll ON "
    4768             :                                    "    coll.oid = attcollation "
    4769             :                                    "  LEFT JOIN pg_namespace collnsp ON "
    4770             :                                    "    collnsp.oid = collnamespace ");
    4771             :         else
    4772           2 :             appendStringInfoString(&buf,
    4773             :                                    "SELECT relname, "
    4774             :                                    "  attname, "
    4775             :                                    "  format_type(atttypid, atttypmod), "
    4776             :                                    "  attnotnull, "
    4777             :                                    "  pg_get_expr(adbin, adrelid), "
    4778             :                                    "  NULL, NULL "
    4779             :                                    "FROM pg_class c "
    4780             :                                    "  JOIN pg_namespace n ON "
    4781             :                                    "    relnamespace = n.oid "
    4782             :                                    "  LEFT JOIN pg_attribute a ON "
    4783             :                                    "    attrelid = c.oid AND attnum > 0 "
    4784             :                                    "      AND NOT attisdropped "
    4785             :                                    "  LEFT JOIN pg_attrdef ad ON "
    4786             :                                    "    adrelid = c.oid AND adnum = attnum ");
    4787             : 
    4788          14 :         appendStringInfoString(&buf,
    4789             :                                "WHERE c.relkind IN ("
    4790             :                                CppAsString2(RELKIND_RELATION) ","
    4791             :                                CppAsString2(RELKIND_VIEW) ","
    4792             :                                CppAsString2(RELKIND_FOREIGN_TABLE) ","
    4793             :                                CppAsString2(RELKIND_MATVIEW) ","
    4794             :                                CppAsString2(RELKIND_PARTITIONED_TABLE) ") "
    4795             :                                "  AND n.nspname = ");
    4796          14 :         deparseStringLiteral(&buf, stmt->remote_schema);
    4797             : 
    4798             :         /* Partitions are supported since Postgres 10 */
    4799          14 :         if (PQserverVersion(conn) >= 100000)
    4800          14 :             appendStringInfoString(&buf, " AND NOT c.relispartition ");
    4801             : 
    4802             :         /* Apply restrictions for LIMIT TO and EXCEPT */
    4803          24 :         if (stmt->list_type == FDW_IMPORT_SCHEMA_LIMIT_TO ||
    4804          10 :             stmt->list_type == FDW_IMPORT_SCHEMA_EXCEPT)
    4805             :         {
    4806           6 :             bool        first_item = true;
    4807             : 
    4808           6 :             appendStringInfoString(&buf, " AND c.relname ");
    4809           6 :             if (stmt->list_type == FDW_IMPORT_SCHEMA_EXCEPT)
    4810           2 :                 appendStringInfoString(&buf, "NOT ");
    4811           6 :             appendStringInfoString(&buf, "IN (");
    4812             : 
    4813             :             /* Append list of table names within IN clause */
    4814          18 :             foreach(lc, stmt->table_list)
    4815             :             {
    4816          12 :                 RangeVar   *rv = (RangeVar *) lfirst(lc);
    4817             : 
    4818          12 :                 if (first_item)
    4819           6 :                     first_item = false;
    4820             :                 else
    4821           6 :                     appendStringInfoString(&buf, ", ");
    4822          12 :                 deparseStringLiteral(&buf, rv->relname);
    4823             :             }
    4824           6 :             appendStringInfoChar(&buf, ')');
    4825             :         }
    4826             : 
    4827             :         /* Append ORDER BY at the end of query to ensure output ordering */
    4828          14 :         appendStringInfoString(&buf, " ORDER BY c.relname, a.attnum");
    4829             : 
    4830             :         /* Fetch the data */
    4831          14 :         res = pgfdw_exec_query(conn, buf.data);
    4832          14 :         if (PQresultStatus(res) != PGRES_TUPLES_OK)
    4833           0 :             pgfdw_report_error(ERROR, res, conn, false, buf.data);
    4834             : 
    4835             :         /* Process results */
    4836          14 :         numrows = PQntuples(res);
    4837             :         /* note: incrementation of i happens in inner loop's while() test */
    4838          88 :         for (i = 0; i < numrows;)
    4839             :         {
    4840          60 :             char       *tablename = PQgetvalue(res, i, 0);
    4841          60 :             bool        first_item = true;
    4842             : 
    4843          60 :             resetStringInfo(&buf);
    4844          60 :             appendStringInfo(&buf, "CREATE FOREIGN TABLE %s (\n",
    4845             :                              quote_identifier(tablename));
    4846             : 
    4847             :             /* Scan all rows for this table */
    4848             :             do
    4849             :             {
    4850             :                 char       *attname;
    4851             :                 char       *typename;
    4852             :                 char       *attnotnull;
    4853             :                 char       *attdefault;
    4854             :                 char       *collname;
    4855             :                 char       *collnamespace;
    4856             : 
    4857             :                 /* If table has no columns, we'll see nulls here */
    4858         120 :                 if (PQgetisnull(res, i, 1))
    4859          10 :                     continue;
    4860             : 
    4861         110 :                 attname = PQgetvalue(res, i, 1);
    4862         110 :                 typename = PQgetvalue(res, i, 2);
    4863         110 :                 attnotnull = PQgetvalue(res, i, 3);
    4864         130 :                 attdefault = PQgetisnull(res, i, 4) ? (char *) NULL :
    4865          20 :                     PQgetvalue(res, i, 4);
    4866         148 :                 collname = PQgetisnull(res, i, 5) ? (char *) NULL :
    4867          38 :                     PQgetvalue(res, i, 5);
    4868         148 :                 collnamespace = PQgetisnull(res, i, 6) ? (char *) NULL :
    4869          38 :                     PQgetvalue(res, i, 6);
    4870             : 
    4871         110 :                 if (first_item)
    4872          50 :                     first_item = false;
    4873             :                 else
    4874          60 :                     appendStringInfoString(&buf, ",\n");
    4875             : 
    4876             :                 /* Print column name and type */
    4877         110 :                 appendStringInfo(&buf, "  %s %s",
    4878             :                                  quote_identifier(attname),
    4879             :                                  typename);
    4880             : 
    4881             :                 /*
    4882             :                  * Add column_name option so that renaming the foreign table's
    4883             :                  * column doesn't break the association to the underlying
    4884             :                  * column.
    4885             :                  */
    4886         110 :                 appendStringInfoString(&buf, " OPTIONS (column_name ");
    4887         110 :                 deparseStringLiteral(&buf, attname);
    4888         110 :                 appendStringInfoChar(&buf, ')');
    4889             : 
    4890             :                 /* Add COLLATE if needed */
    4891         110 :                 if (import_collate && collname != NULL && collnamespace != NULL)
    4892          38 :                     appendStringInfo(&buf, " COLLATE %s.%s",
    4893             :                                      quote_identifier(collnamespace),
    4894             :                                      quote_identifier(collname));
    4895             : 
    4896             :                 /* Add DEFAULT if needed */
    4897         110 :                 if (import_default && attdefault != NULL)
    4898           4 :                     appendStringInfo(&buf, " DEFAULT %s", attdefault);
    4899             : 
    4900             :                 /* Add NOT NULL if needed */
    4901         110 :                 if (import_not_null && attnotnull[0] == 't')
    4902           8 :                     appendStringInfoString(&buf, " NOT NULL");
    4903             :             }
    4904         106 :             while (++i < numrows &&
    4905         120 :                    strcmp(PQgetvalue(res, i, 0), tablename) == 0);
    4906             : 
    4907             :             /*
    4908             :              * Add server name and table-level options.  We specify remote
    4909             :              * schema and table name as options (the latter to ensure that
    4910             :              * renaming the foreign table doesn't break the association).
    4911             :              */
    4912          60 :             appendStringInfo(&buf, "\n) SERVER %s\nOPTIONS (",
    4913          60 :                              quote_identifier(server->servername));
    4914             : 
    4915          60 :             appendStringInfoString(&buf, "schema_name ");
    4916          60 :             deparseStringLiteral(&buf, stmt->remote_schema);
    4917          60 :             appendStringInfoString(&buf, ", table_name ");
    4918          60 :             deparseStringLiteral(&buf, tablename);
    4919             : 
    4920          60 :             appendStringInfoString(&buf, ");");
    4921             : 
    4922          60 :             commands = lappend(commands, pstrdup(buf.data));
    4923             :         }
    4924             : 
    4925             :         /* Clean up */
    4926          14 :         PQclear(res);
    4927          14 :         res = NULL;
    4928             :     }
    4929           2 :     PG_CATCH();
    4930             :     {
    4931           2 :         if (res)
    4932           2 :             PQclear(res);
    4933           2 :         PG_RE_THROW();
    4934             :     }
    4935          14 :     PG_END_TRY();
    4936             : 
    4937          14 :     ReleaseConnection(conn);
    4938             : 
    4939          14 :     return commands;
    4940             : }
    4941             : 
    4942             : /*
    4943             :  * Assess whether the join between inner and outer relations can be pushed down
    4944             :  * to the foreign server. As a side effect, save information we obtain in this
    4945             :  * function to PgFdwRelationInfo passed in.
    4946             :  */
    4947             : static bool
    4948         482 : foreign_join_ok(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype,
    4949             :                 RelOptInfo *outerrel, RelOptInfo *innerrel,
    4950             :                 JoinPathExtraData *extra)
    4951             : {
    4952             :     PgFdwRelationInfo *fpinfo;
    4953             :     PgFdwRelationInfo *fpinfo_o;
    4954             :     PgFdwRelationInfo *fpinfo_i;
    4955             :     ListCell   *lc;
    4956             :     List       *joinclauses;
    4957             : 
    4958             :     /*
    4959             :      * We support pushing down INNER, LEFT, RIGHT and FULL OUTER joins.
    4960             :      * Constructing queries representing SEMI and ANTI joins is hard, hence
    4961             :      * not considered right now.
    4962             :      */
    4963         482 :     if (jointype != JOIN_INNER && jointype != JOIN_LEFT &&
    4964         114 :         jointype != JOIN_RIGHT && jointype != JOIN_FULL)
    4965          22 :         return false;
    4966             : 
    4967             :     /*
    4968             :      * If either of the joining relations is marked as unsafe to pushdown, the
    4969             :      * join can not be pushed down.
    4970             :      */
    4971         460 :     fpinfo = (PgFdwRelationInfo *) joinrel->fdw_private;
    4972         460 :     fpinfo_o = (PgFdwRelationInfo *) outerrel->fdw_private;
    4973         460 :     fpinfo_i = (PgFdwRelationInfo *) innerrel->fdw_private;
    4974         460 :     if (!fpinfo_o || !fpinfo_o->pushdown_safe ||
    4975         460 :         !fpinfo_i || !fpinfo_i->pushdown_safe)
    4976           0 :         return false;
    4977             : 
    4978             :     /*
    4979             :      * If joining relations have local conditions, those conditions are
    4980             :      * required to be applied before joining the relations. Hence the join can
    4981             :      * not be pushed down.
    4982             :      */
    4983         460 :     if (fpinfo_o->local_conds || fpinfo_i->local_conds)
    4984          12 :         return false;
    4985             : 
    4986             :     /*
    4987             :      * Merge FDW options.  We might be tempted to do this after we have deemed
    4988             :      * the foreign join to be OK.  But we must do this beforehand so that we
    4989             :      * know which quals can be evaluated on the foreign server, which might
    4990             :      * depend on shippable_extensions.
    4991             :      */
    4992         448 :     fpinfo->server = fpinfo_o->server;
    4993         448 :     merge_fdw_options(fpinfo, fpinfo_o, fpinfo_i);
    4994             : 
    4995             :     /*
    4996             :      * Separate restrict list into join quals and pushed-down (other) quals.
    4997             :      *
    4998             :      * Join quals belonging to an outer join must all be shippable, else we
    4999             :      * cannot execute the join remotely.  Add such quals to 'joinclauses'.
    5000             :      *
    5001             :      * Add other quals to fpinfo->remote_conds if they are shippable, else to
    5002             :      * fpinfo->local_conds.  In an inner join it's okay to execute conditions
    5003             :      * either locally or remotely; the same is true for pushed-down conditions
    5004             :      * at an outer join.
    5005             :      *
    5006             :      * Note we might return failure after having already scribbled on
    5007             :      * fpinfo->remote_conds and fpinfo->local_conds.  That's okay because we
    5008             :      * won't consult those lists again if we deem the join unshippable.
    5009             :      */
    5010         448 :     joinclauses = NIL;
    5011         914 :     foreach(lc, extra->restrictlist)
    5012             :     {
    5013         472 :         RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
    5014         472 :         bool        is_remote_clause = is_foreign_expr(root, joinrel,
    5015             :                                                        rinfo->clause);
    5016             : 
    5017         722 :         if (IS_OUTER_JOIN(jointype) &&
    5018         468 :             !RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
    5019             :         {
    5020         218 :             if (!is_remote_clause)
    5021           6 :                 return false;
    5022         212 :             joinclauses = lappend(joinclauses, rinfo);
    5023             :         }
    5024             :         else
    5025             :         {
    5026         254 :             if (is_remote_clause)
    5027         234 :                 fpinfo->remote_conds = lappend(fpinfo->remote_conds, rinfo);
    5028             :             else
    5029          20 :                 fpinfo->local_conds = lappend(fpinfo->local_conds, rinfo);
    5030             :         }
    5031             :     }
    5032             : 
    5033             :     /*
    5034             :      * deparseExplicitTargetList() isn't smart enough to handle anything other
    5035             :      * than a Var.  In particular, if there's some PlaceHolderVar that would
    5036             :      * need to be evaluated within this join tree (because there's an upper
    5037             :      * reference to a quantity that may go to NULL as a result of an outer
    5038             :      * join), then we can't try to push the join down because we'll fail when
    5039             :      * we get to deparseExplicitTargetList().  However, a PlaceHolderVar that
    5040             :      * needs to be evaluated *at the top* of this join tree is OK, because we
    5041             :      * can do that locally after fetching the results from the remote side.
    5042             :      */
    5043         446 :     foreach(lc, root->placeholder_list)
    5044             :     {
    5045          20 :         PlaceHolderInfo *phinfo = lfirst(lc);
    5046             :         Relids      relids;
    5047             : 
    5048             :         /* PlaceHolderInfo refers to parent relids, not child relids. */
    5049          60 :         relids = IS_OTHER_REL(joinrel) ?
    5050          28 :             joinrel->top_parent_relids : joinrel->relids;
    5051             : 
    5052          40 :         if (bms_is_subset(phinfo->ph_eval_at, relids) &&
    5053          20 :             bms_nonempty_difference(relids, phinfo->ph_eval_at))
    5054          16 :             return false;
    5055             :     }
    5056             : 
    5057             :     /* Save the join clauses, for later use. */
    5058         426 :     fpinfo->joinclauses = joinclauses;
    5059             : 
    5060         426 :     fpinfo->outerrel = outerrel;
    5061         426 :     fpinfo->innerrel = innerrel;
    5062         426 :     fpinfo->jointype = jointype;
    5063             : 
    5064             :     /*
    5065             :      * By default, both the input relations are not required to be deparsed as
    5066             :      * subqueries, but there might be some relations covered by the input
    5067             :      * relations that are required to be deparsed as subqueries, so save the
    5068             :      * relids of those relations for later use by the deparser.
    5069             :      */
    5070         426 :     fpinfo->make_outerrel_subquery = false;
    5071         426 :     fpinfo->make_innerrel_subquery = false;
    5072             :     Assert(bms_is_subset(fpinfo_o->lower_subquery_rels, outerrel->relids));
    5073             :     Assert(bms_is_subset(fpinfo_i->lower_subquery_rels, innerrel->relids));
    5074         426 :     fpinfo->lower_subquery_rels = bms_union(fpinfo_o->lower_subquery_rels,
    5075         426 :                                             fpinfo_i->lower_subquery_rels);
    5076             : 
    5077             :     /*
    5078             :      * Pull the other remote conditions from the joining relations into join
    5079             :      * clauses or other remote clauses (remote_conds) of this relation
    5080             :      * wherever possible. This avoids building subqueries at every join step.
    5081             :      *
    5082             :      * For an inner join, clauses from both the relations are added to the
    5083             :      * other remote clauses. For LEFT and RIGHT OUTER join, the clauses from
    5084             :      * the outer side are added to remote_conds since those can be evaluated
    5085             :      * after the join is evaluated. The clauses from inner side are added to
    5086             :      * the joinclauses, since they need to be evaluated while constructing the
    5087             :      * join.
    5088             :      *
    5089             :      * For a FULL OUTER JOIN, the other clauses from either relation can not
    5090             :      * be added to the joinclauses or remote_conds, since each relation acts
    5091             :      * as an outer relation for the other.
    5092             :      *
    5093             :      * The joining sides can not have local conditions, thus no need to test
    5094             :      * shippability of the clauses being pulled up.
    5095             :      */
    5096         426 :     switch (jointype)
    5097             :     {
    5098             :         case JOIN_INNER:
    5099         230 :             fpinfo->remote_conds = list_concat(fpinfo->remote_conds,
    5100         230 :                                                list_copy(fpinfo_i->remote_conds));
    5101         230 :             fpinfo->remote_conds = list_concat(fpinfo->remote_conds,
    5102         230 :                                                list_copy(fpinfo_o->remote_conds));
    5103         230 :             break;
    5104             : 
    5105             :         case JOIN_LEFT:
    5106         112 :             fpinfo->joinclauses = list_concat(fpinfo->joinclauses,
    5107         112 :                                               list_copy(fpinfo_i->remote_conds));
    5108         112 :             fpinfo->remote_conds = list_concat(fpinfo->remote_conds,
    5109         112 :                                                list_copy(fpinfo_o->remote_conds));
    5110         112 :             break;
    5111             : 
    5112             :         case JOIN_RIGHT:
    5113           0 :             fpinfo->joinclauses = list_concat(fpinfo->joinclauses,
    5114           0 :                                               list_copy(fpinfo_o->remote_conds));
    5115           0 :             fpinfo->remote_conds = list_concat(fpinfo->remote_conds,
    5116           0 :                                                list_copy(fpinfo_i->remote_conds));
    5117           0 :             break;
    5118             : 
    5119             :         case JOIN_FULL:
    5120             : 
    5121             :             /*
    5122             :              * In this case, if any of the input relations has conditions, we
    5123             :              * need to deparse that relation as a subquery so that the
    5124             :              * conditions can be evaluated before the join.  Remember it in
    5125             :              * the fpinfo of this relation so that the deparser can take
    5126             :              * appropriate action.  Also, save the relids of base relations
    5127             :              * covered by that relation for later use by the deparser.
    5128             :              */
    5129          84 :             if (fpinfo_o->remote_conds)
    5130             :             {
    5131          28 :                 fpinfo->make_outerrel_subquery = true;
    5132          28 :                 fpinfo->lower_subquery_rels =
    5133          28 :                     bms_add_members(fpinfo->lower_subquery_rels,
    5134          28 :                                     outerrel->relids);
    5135             :             }
    5136          84 :             if (fpinfo_i->remote_conds)
    5137             :             {
    5138          28 :                 fpinfo->make_innerrel_subquery = true;
    5139          28 :                 fpinfo->lower_subquery_rels =
    5140          28 :                     bms_add_members(fpinfo->lower_subquery_rels,
    5141          28 :                                     innerrel->relids);
    5142             :             }
    5143          84 :             break;
    5144             : 
    5145             :         default:
    5146             :             /* Should not happen, we have just checked this above */
    5147           0 :             elog(ERROR, "unsupported join type %d", jointype);
    5148             :     }
    5149             : 
    5150             :     /*
    5151             :      * For an inner join, all restrictions can be treated alike. Treating the
    5152             :      * pushed down conditions as join conditions allows a top level full outer
    5153             :      * join to be deparsed without requiring subqueries.
    5154             :      */
    5155         426 :     if (jointype == JOIN_INNER)
    5156             :     {
    5157             :         Assert(!fpinfo->joinclauses);
    5158         230 :         fpinfo->joinclauses = fpinfo->remote_conds;
    5159         230 :         fpinfo->remote_conds = NIL;
    5160             :     }
    5161             : 
    5162             :     /* Mark that this join can be pushed down safely */
    5163         426 :     fpinfo->pushdown_safe = true;
    5164             : 
    5165             :     /* Get user mapping */
    5166         426 :     if (fpinfo->use_remote_estimate)
    5167             :     {
    5168         282 :         if (fpinfo_o->use_remote_estimate)
    5169         178 :             fpinfo->user = fpinfo_o->user;
    5170             :         else
    5171         104 :             fpinfo->user = fpinfo_i->user;
    5172             :     }
    5173             :     else
    5174         144 :         fpinfo->user = NULL;
    5175             : 
    5176             :     /*
    5177             :      * Set # of retrieved rows and cached relation costs to some negative
    5178             :      * value, so that we can detect when they are set to some sensible values,
    5179             :      * during one (usually the first) of the calls to estimate_path_cost_size.
    5180             :      */
    5181         426 :     fpinfo->retrieved_rows = -1;
    5182         426 :     fpinfo->rel_startup_cost = -1;
    5183         426 :     fpinfo->rel_total_cost = -1;
    5184             : 
    5185             :     /*
    5186             :      * Set the string describing this join relation to be used in EXPLAIN
    5187             :      * output of corresponding ForeignScan.
    5188             :      */
    5189         426 :     fpinfo->relation_name = makeStringInfo();
    5190         852 :     appendStringInfo(fpinfo->relation_name, "(%s) %s JOIN (%s)",
    5191         426 :                      fpinfo_o->relation_name->data,
    5192             :                      get_jointype_name(fpinfo->jointype),
    5193         426 :                      fpinfo_i->relation_name->data);
    5194             : 
    5195             :     /*
    5196             :      * Set the relation index.  This is defined as the position of this
    5197             :      * joinrel in the join_rel_list list plus the length of the rtable list.
    5198             :      * Note that since this joinrel is at the end of the join_rel_list list
    5199             :      * when we are called, we can get the position by list_length.
    5200             :      */
    5201             :     Assert(fpinfo->relation_index == 0); /* shouldn't be set yet */
    5202         426 :     fpinfo->relation_index =
    5203         426 :         list_length(root->parse->rtable) + list_length(root->join_rel_list);
    5204             : 
    5205         426 :     return true;
    5206             : }
    5207             : 
    5208             : static void
    5209        2142 : add_paths_with_pathkeys_for_rel(PlannerInfo *root, RelOptInfo *rel,
    5210             :                                 Path *epq_path)
    5211             : {
    5212        2142 :     List       *useful_pathkeys_list = NIL; /* List of all pathkeys */
    5213             :     ListCell   *lc;
    5214             : 
    5215        2142 :     useful_pathkeys_list = get_useful_pathkeys_for_relation(root, rel);
    5216             : 
    5217             :     /* Create one path for each set of pathkeys we found above. */
    5218        3098 :     foreach(lc, useful_pathkeys_list)
    5219             :     {
    5220             :         double      rows;
    5221             :         int         width;
    5222             :         Cost        startup_cost;
    5223             :         Cost        total_cost;
    5224         956 :         List       *useful_pathkeys = lfirst(lc);
    5225             :         Path       *sorted_epq_path;
    5226             : 
    5227         956 :         estimate_path_cost_size(root, rel, NIL, useful_pathkeys, NULL,
    5228             :                                 &rows, &width, &startup_cost, &total_cost);
    5229             : 
    5230             :         /*
    5231             :          * The EPQ path must be at least as well sorted as the path itself, in
    5232             :          * case it gets used as input to a mergejoin.
    5233             :          */
    5234         956 :         sorted_epq_path = epq_path;
    5235        1008 :         if (sorted_epq_path != NULL &&
    5236          52 :             !pathkeys_contained_in(useful_pathkeys,
    5237             :                                    sorted_epq_path->pathkeys))
    5238          40 :             sorted_epq_path = (Path *)
    5239             :                 create_sort_path(root,
    5240             :                                  rel,
    5241             :                                  sorted_epq_path,
    5242             :                                  useful_pathkeys,
    5243             :                                  -1.0);
    5244             : 
    5245         956 :         if (IS_SIMPLE_REL(rel))
    5246        1360 :             add_path(rel, (Path *)
    5247        1360 :                      create_foreignscan_path(root, rel,
    5248             :                                              NULL,
    5249             :                                              rows,
    5250             :                                              startup_cost,
    5251             :                                              total_cost,
    5252             :                                              useful_pathkeys,
    5253             :                                              rel->lateral_relids,
    5254             :                                              sorted_epq_path,
    5255             :                                              NIL));
    5256             :         else
    5257         552 :             add_path(rel, (Path *)
    5258         552 :                      create_foreign_join_path(root, rel,
    5259             :                                               NULL,
    5260             :                                               rows,
    5261             :                                               startup_cost,
    5262             :                                               total_cost,
    5263             :                                               useful_pathkeys,
    5264             :                                               rel->lateral_relids,
    5265             :                                               sorted_epq_path,
    5266             :                                               NIL));
    5267             :     }
    5268        2142 : }
    5269             : 
    5270             : /*
    5271             :  * Parse options from foreign server and apply them to fpinfo.
    5272             :  *
    5273             :  * New options might also require tweaking merge_fdw_options().
    5274             :  */
    5275             : static void
    5276        1716 : apply_server_options(PgFdwRelationInfo *fpinfo)
    5277             : {
    5278             :     ListCell   *lc;
    5279             : 
    5280        6670 :     foreach(lc, fpinfo->server->options)
    5281             :     {
    5282        4954 :         DefElem    *def = (DefElem *) lfirst(lc);
    5283             : 
    5284        4954 :         if (strcmp(def->defname, "use_remote_estimate") == 0)
    5285           0 :             fpinfo->use_remote_estimate = defGetBoolean(def);
    5286        4954 :         else if (strcmp(def->defname, "fdw_startup_cost") == 0)
    5287           0 :             fpinfo->fdw_startup_cost = strtod(defGetString(def), NULL);
    5288        4954 :         else if (strcmp(def->defname, "fdw_tuple_cost") == 0)
    5289           0 :             fpinfo->fdw_tuple_cost = strtod(defGetString(def), NULL);
    5290        4954 :         else if (strcmp(def->defname, "extensions") == 0)
    5291        1522 :             fpinfo->shippable_extensions =
    5292        1522 :                 ExtractExtensionList(defGetString(def), false);
    5293        3432 :         else if (strcmp(def->defname, "fetch_size") == 0)
    5294           0 :             fpinfo->fetch_size = strtol(defGetString(def), NULL, 10);
    5295             :     }
    5296        1716 : }
    5297             : 
    5298             : /*
    5299             :  * Parse options from foreign table and apply them to fpinfo.
    5300             :  *
    5301             :  * New options might also require tweaking merge_fdw_options().
    5302             :  */
    5303             : static void
    5304        1716 : apply_table_options(PgFdwRelationInfo *fpinfo)
    5305             : {
    5306             :     ListCell   *lc;
    5307             : 
    5308        5018 :     foreach(lc, fpinfo->table->options)
    5309             :     {
    5310        3302 :         DefElem    *def = (DefElem *) lfirst(lc);
    5311             : 
    5312        3302 :         if (strcmp(def->defname, "use_remote_estimate") == 0)
    5313         478 :             fpinfo->use_remote_estimate = defGetBoolean(def);
    5314        2824 :         else if (strcmp(def->defname, "fetch_size") == 0)
    5315           0 :             fpinfo->fetch_size = strtol(defGetString(def), NULL, 10);
    5316             :     }
    5317        1716 : }
    5318             : 
    5319             : /*
    5320             :  * Merge FDW options from input relations into a new set of options for a join
    5321             :  * or an upper rel.
    5322             :  *
    5323             :  * For a join relation, FDW-specific information about the inner and outer
    5324             :  * relations is provided using fpinfo_i and fpinfo_o.  For an upper relation,
    5325             :  * fpinfo_o provides the information for the input relation; fpinfo_i is
    5326             :  * expected to NULL.
    5327             :  */
    5328             : static void
    5329        1114 : merge_fdw_options(PgFdwRelationInfo *fpinfo,
    5330             :                   const PgFdwRelationInfo *fpinfo_o,
    5331             :                   const PgFdwRelationInfo *fpinfo_i)
    5332             : {
    5333             :     /* We must always have fpinfo_o. */
    5334             :     Assert(fpinfo_o);
    5335             : 
    5336             :     /* fpinfo_i may be NULL, but if present the servers must both match. */
    5337             :     Assert(!fpinfo_i ||
    5338             :            fpinfo_i->server->serverid == fpinfo_o->server->serverid);
    5339             : 
    5340             :     /*
    5341             :      * Copy the server specific FDW options.  (For a join, both relations come
    5342             :      * from the same server, so the server options should have the same value
    5343             :      * for both relations.)
    5344             :      */
    5345        1114 :     fpinfo->fdw_startup_cost = fpinfo_o->fdw_startup_cost;
    5346        1114 :     fpinfo->fdw_tuple_cost = fpinfo_o->fdw_tuple_cost;
    5347        1114 :     fpinfo->shippable_extensions = fpinfo_o->shippable_extensions;
    5348        1114 :     fpinfo->use_remote_estimate = fpinfo_o->use_remote_estimate;
    5349        1114 :     fpinfo->fetch_size = fpinfo_o->fetch_size;
    5350             : 
    5351             :     /* Merge the table level options from either side of the join. */
    5352        1114 :     if (fpinfo_i)
    5353             :     {
    5354             :         /*
    5355             :          * We'll prefer to use remote estimates for this join if any table
    5356             :          * from either side of the join is using remote estimates.  This is
    5357             :          * most likely going to be preferred since they're already willing to
    5358             :          * pay the price of a round trip to get the remote EXPLAIN.  In any
    5359             :          * case it's not entirely clear how we might otherwise handle this
    5360             :          * best.
    5361             :          */
    5362         708 :         fpinfo->use_remote_estimate = fpinfo_o->use_remote_estimate ||
    5363         260 :             fpinfo_i->use_remote_estimate;
    5364             : 
    5365             :         /*
    5366             :          * Set fetch size to maximum of the joining sides, since we are
    5367             :          * expecting the rows returned by the join to be proportional to the
    5368             :          * relation sizes.
    5369             :          */
    5370         448 :         fpinfo->fetch_size = Max(fpinfo_o->fetch_size, fpinfo_i->fetch_size);
    5371             :     }
    5372        1114 : }
    5373             : 
    5374             : /*
    5375             :  * postgresGetForeignJoinPaths
    5376             :  *      Add possible ForeignPath to joinrel, if join is safe to push down.
    5377             :  */
    5378             : static void
    5379        1182 : postgresGetForeignJoinPaths(PlannerInfo *root,
    5380             :                             RelOptInfo *joinrel,
    5381             :                             RelOptInfo *outerrel,
    5382             :                             RelOptInfo *innerrel,
    5383             :                             JoinType jointype,
    5384             :                             JoinPathExtraData *extra)
    5385             : {
    5386             :     PgFdwRelationInfo *fpinfo;
    5387             :     ForeignPath *joinpath;
    5388             :     double      rows;
    5389             :     int         width;
    5390             :     Cost        startup_cost;
    5391             :     Cost        total_cost;
    5392             :     Path       *epq_path;       /* Path to create plan to be executed when
    5393             :                                  * EvalPlanQual gets triggered. */
    5394             : 
    5395             :     /*
    5396             :      * Skip if this join combination has been considered already.
    5397             :      */
    5398        1182 :     if (joinrel->fdw_private)
    5399        1448 :         return;
    5400             : 
    5401             :     /*
    5402             :      * This code does not work for joins with lateral references, since those
    5403             :      * must have parameterized paths, which we don't generate yet.
    5404             :      */
    5405         490 :     if (!bms_is_empty(joinrel->lateral_relids))
    5406           8 :         return;
    5407             : 
    5408             :     /*
    5409             :      * Create unfinished PgFdwRelationInfo entry which is used to indicate
    5410             :      * that the join relation is already considered, so that we won't waste
    5411             :      * time in judging safety of join pushdown and adding the same paths again
    5412             :      * if found safe. Once we know that this join can be pushed down, we fill
    5413             :      * the entry.
    5414             :      */
    5415         482 :     fpinfo = (PgFdwRelationInfo *) palloc0(sizeof(PgFdwRelationInfo));
    5416         482 :     fpinfo->pushdown_safe = false;
    5417         482 :     joinrel->fdw_private = fpinfo;
    5418             :     /* attrs_used is only for base relations. */
    5419         482 :     fpinfo->attrs_used = NULL;
    5420             : 
    5421             :     /*
    5422             :      * If there is a possibility that EvalPlanQual will be executed, we need
    5423             :      * to be able to reconstruct the row using scans of the base relations.
    5424             :      * GetExistingLocalJoinPath will find a suitable path for this purpose in
    5425             :      * the path list of the joinrel, if one exists.  We must be careful to
    5426             :      * call it before adding any ForeignPath, since the ForeignPath might
    5427             :      * dominate the only suitable local path available.  We also do it before
    5428             :      * calling foreign_join_ok(), since that function updates fpinfo and marks
    5429             :      * it as pushable if the join is found to be pushable.
    5430             :      */
    5431         932 :     if (root->parse->commandType == CMD_DELETE ||
    5432         864 :         root->parse->commandType == CMD_UPDATE ||
    5433         414 :         root->rowMarks)
    5434             :     {
    5435         120 :         epq_path = GetExistingLocalJoinPath(joinrel);
    5436         240 :         if (!epq_path)
    5437             :         {
    5438           0 :             elog(DEBUG3, "could not push down foreign join because a local path suitable for EPQ checks was not found");
    5439           0 :             return;
    5440             :         }
    5441             :     }
    5442             :     else
    5443         362 :         epq_path = NULL;
    5444             : 
    5445         482 :     if (!foreign_join_ok(root, joinrel, jointype, outerrel, innerrel, extra))
    5446             :     {
    5447             :         /* Free path required for EPQ if we copied one; we don't need it now */
    5448          56 :         if (epq_path)
    5449           4 :             pfree(epq_path);
    5450          56 :         return;
    5451             :     }
    5452             : 
    5453             :     /*
    5454             :      * Compute the selectivity and cost of the local_conds, so we don't have
    5455             :      * to do it over again for each path. The best we can do for these
    5456             :      * conditions is to estimate selectivity on the basis of local statistics.
    5457             :      * The local conditions are applied after the join has been computed on
    5458             :      * the remote side like quals in WHERE clause, so pass jointype as
    5459             :      * JOIN_INNER.
    5460             :      */
    5461         426 :     fpinfo->local_conds_sel = clauselist_selectivity(root,
    5462             :                                                      fpinfo->local_conds,
    5463             :                                                      0,
    5464             :                                                      JOIN_INNER,
    5465             :                                                      NULL);
    5466         426 :     cost_qual_eval(&fpinfo->local_conds_cost, fpinfo->local_conds, root);
    5467             : 
    5468             :     /*
    5469             :      * If we are going to estimate costs locally, estimate the join clause
    5470             :      * selectivity here while we have special join info.
    5471             :      */
    5472         426 :     if (!fpinfo->use_remote_estimate)
    5473         144 :         fpinfo->joinclause_sel = clauselist_selectivity(root, fpinfo->joinclauses,
    5474             :                                                         0, fpinfo->jointype,
    5475             :                                                         extra->sjinfo);
    5476             : 
    5477             :     /* Estimate costs for bare join relation */
    5478         426 :     estimate_path_cost_size(root, joinrel, NIL, NIL, NULL,
    5479             :                             &rows, &width, &startup_cost, &total_cost);
    5480             :     /* Now update this information in the joinrel */
    5481         426 :     joinrel->rows = rows;
    5482         426 :     joinrel->reltarget->width = width;
    5483         426 :     fpinfo->rows = rows;
    5484         426 :     fpinfo->width = width;
    5485         426 :     fpinfo->startup_cost = startup_cost;
    5486         426 :     fpinfo->total_cost = total_cost;
    5487             : 
    5488             :     /*
    5489             :      * Create a new join path and add it to the joinrel which represents a
    5490             :      * join between foreign tables.
    5491             :      */
    5492         426 :     joinpath = create_foreign_join_path(root,
    5493             :                                         joinrel,
    5494             :                                         NULL,   /* default pathtarget */
    5495             :                                         rows,
    5496             :                                         startup_cost,
    5497             :                                         total_cost,
    5498             :                                         NIL,    /* no pathkeys */
    5499             :                                         joinrel->lateral_relids,
    5500             :                                         epq_path,
    5501             :                                         NIL);   /* no fdw_private */
    5502             : 
    5503             :     /* Add generated path into joinrel by add_path(). */
    5504         426 :     add_path(joinrel, (Path *) joinpath);
    5505             : 
    5506             :     /* Consider pathkeys for the join relation */
    5507         426 :     add_paths_with_pathkeys_for_rel(root, joinrel, epq_path);
    5508             : 
    5509             :     /* XXX Consider parameterized paths for the join relation */
    5510             : }
    5511             : 
    5512             : /*
    5513             :  * Assess whether the aggregation, grouping and having operations can be pushed
    5514             :  * down to the foreign server.  As a side effect, save information we obtain in
    5515             :  * this function to PgFdwRelationInfo of the input relation.
    5516             :  */
    5517             : static bool
    5518         262 : foreign_grouping_ok(PlannerInfo *root, RelOptInfo *grouped_rel,
    5519             :                     Node *havingQual)
    5520             : {
    5521         262 :     Query      *query = root->parse;
    5522         262 :     PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) grouped_rel->fdw_private;
    5523         262 :     PathTarget *grouping_target = grouped_rel->reltarget;
    5524             :     PgFdwRelationInfo *ofpinfo;
    5525             :     ListCell   *lc;
    5526             :     int         i;
    5527         262 :     List       *tlist = NIL;
    5528             : 
    5529             :     /* We currently don't support pushing Grouping Sets. */
    5530         262 :     if (query->groupingSets)
    5531          12 :         return false;
    5532             : 
    5533             :     /* Get the fpinfo of the underlying scan relation. */
    5534         250 :     ofpinfo = (PgFdwRelationInfo *) fpinfo->outerrel->fdw_private;
    5535             : 
    5536             :     /*
    5537             :      * If underlying scan relation has any local conditions, those conditions
    5538             :      * are required to be applied before performing aggregation.  Hence the
    5539             :      * aggregate cannot be pushed down.
    5540             :      */
    5541         250 :     if (ofpinfo->local_conds)
    5542          12 :         return false;
    5543             : 
    5544             :     /*
    5545             :      * Examine grouping expressions, as well as other expressions we'd need to
    5546             :      * compute, and check whether they are safe to push down to the foreign
    5547             :      * server.  All GROUP BY expressions will be part of the grouping target
    5548             :      * and thus there is no need to search for them separately.  Add grouping
    5549             :      * expressions into target list which will be passed to foreign server.
    5550             :      *
    5551             :      * A tricky fine point is that we must not put any expression into the
    5552             :      * target list that is just a foreign param (that is, something that
    5553             :      * deparse.c would conclude has to be sent to the foreign server).  If we
    5554             :      * do, the expression will also appear in the fdw_exprs list of the plan
    5555             :      * node, and setrefs.c will get confused and decide that the fdw_exprs
    5556             :      * entry is actually a reference to the fdw_scan_tlist entry, resulting in
    5557             :      * a broken plan.  Somewhat oddly, it's OK if the expression contains such
    5558             :      * a node, as long as it's not at top level; then no match is possible.
    5559             :      */
    5560         238 :     i = 0;
    5561         746 :     foreach(lc, grouping_target->exprs)
    5562             :     {
    5563         544 :         Expr       *expr = (Expr *) lfirst(lc);
    5564         544 :         Index       sgref = get_pathtarget_sortgroupref(grouping_target, i);
    5565             :         ListCell   *l;
    5566             : 
    5567             :         /* Check whether this expression is part of GROUP BY clause */
    5568         544 :         if (sgref && get_sortgroupref_clause_noerr(sgref, query->groupClause))
    5569         184 :         {
    5570             :             TargetEntry *tle;
    5571             : 
    5572             :             /*
    5573             :              * If any GROUP BY expression is not shippable, then we cannot
    5574             :              * push down aggregation to the foreign server.
    5575             :              */
    5576         190 :             if (!is_foreign_expr(root, grouped_rel, expr))
    5577           2 :                 return false;
    5578             : 
    5579             :             /*
    5580             :              * If it would be a foreign param, we can't put it into the tlist,
    5581             :              * so we have to fail.
    5582             :              */
    5583         188 :             if (is_foreign_param(root, grouped_rel, expr))
    5584           4 :                 return false;
    5585             : 
    5586             :             /*
    5587             :              * Pushable, so add to tlist.  We need to create a TLE for this
    5588             :              * expression and apply the sortgroupref to it.  We cannot use
    5589             :              * add_to_flat_tlist() here because that avoids making duplicate
    5590             :              * entries in the tlist.  If there are duplicate entries with
    5591             :              * distinct sortgrouprefs, we have to duplicate that situation in
    5592             :              * the output tlist.
    5593             :              */
    5594         184 :             tle = makeTargetEntry(expr, list_length(tlist) + 1, NULL, false);
    5595         184 :             tle->ressortgroupref = sgref;
    5596         184 :             tlist = lappend(tlist, tle);
    5597             :         }
    5598             :         else
    5599             :         {
    5600             :             /*
    5601             :              * Non-grouping expression we need to compute.  Can we ship it
    5602             :              * as-is to the foreign server?
    5603             :              */
    5604         666 :             if (is_foreign_expr(root, grouped_rel, expr) &&
    5605         312 :                 !is_foreign_param(root, grouped_rel, expr))
    5606             :             {
    5607             :                 /* Yes, so add to tlist as-is; OK to suppress duplicates */
    5608         308 :                 tlist = add_to_flat_tlist(tlist, list_make1(expr));
    5609             :             }
    5610             :             else
    5611             :             {
    5612             :                 /* Not pushable as a whole; extract its Vars and aggregates */
    5613             :                 List       *aggvars;
    5614             : 
    5615          46 :                 aggvars = pull_var_clause((Node *) expr,
    5616             :                                           PVC_INCLUDE_AGGREGATES);
    5617             : 
    5618             :                 /*
    5619             :                  * If any aggregate expression is not shippable, then we
    5620             :                  * cannot push down aggregation to the foreign server.  (We
    5621             :                  * don't have to check is_foreign_param, since that certainly
    5622             :                  * won't return true for any such expression.)
    5623             :                  */
    5624          46 :                 if (!is_foreign_expr(root, grouped_rel, (Expr *) aggvars))
    5625          30 :                     return false;
    5626             : 
    5627             :                 /*
    5628             :                  * Add aggregates, if any, into the targetlist.  Plain Vars
    5629             :                  * outside an aggregate can be ignored, because they should be
    5630             :                  * either same as some GROUP BY column or part of some GROUP
    5631             :                  * BY expression.  In either case, they are already part of
    5632             :                  * the targetlist and thus no need to add them again.  In fact
    5633             :                  * including plain Vars in the tlist when they do not match a
    5634             :                  * GROUP BY column would cause the foreign server to complain
    5635             :                  * that the shipped query is invalid.
    5636             :                  */
    5637          28 :                 foreach(l, aggvars)
    5638             :                 {
    5639          12 :                     Expr       *expr = (Expr *) lfirst(l);
    5640             : 
    5641          12 :                     if (IsA(expr, Aggref))
    5642           8 :                         tlist = add_to_flat_tlist(tlist, list_make1(expr));
    5643             :                 }
    5644             :             }
    5645             :         }
    5646             : 
    5647         508 :         i++;
    5648             :     }
    5649             : 
    5650             :     /*
    5651             :      * Classify the pushable and non-pushable HAVING clauses and save them in
    5652             :      * remote_conds and local_conds of the grouped rel's fpinfo.
    5653             :      */
    5654         202 :     if (havingQual)
    5655             :     {
    5656             :         ListCell   *lc;
    5657             : 
    5658          68 :         foreach(lc, (List *) havingQual)
    5659             :         {
    5660          38 :             Expr       *expr = (Expr *) lfirst(lc);
    5661             :             RestrictInfo *rinfo;
    5662             : 
    5663             :             /*
    5664             :              * Currently, the core code doesn't wrap havingQuals in
    5665             :              * RestrictInfos, so we must make our own.
    5666             :              */
    5667             :             Assert(!IsA(expr, RestrictInfo));
    5668          38 :             rinfo = make_restrictinfo(expr,
    5669             :                                       true,
    5670             :                                       false,
    5671             :                                       false,
    5672             :                                       root->qual_security_level,
    5673             :                                       grouped_rel->relids,
    5674             :                                       NULL,
    5675             :                                       NULL);
    5676          38 :             if (is_foreign_expr(root, grouped_rel, expr))
    5677          32 :                 fpinfo->remote_conds = lappend(fpinfo->remote_conds, rinfo);
    5678             :             else
    5679           6 :                 fpinfo->local_conds = lappend(fpinfo->local_conds, rinfo);
    5680             :         }
    5681             :     }
    5682             : 
    5683             :     /*
    5684             :      * If there are any local conditions, pull Vars and aggregates from it and
    5685             :      * check whether they are safe to pushdown or not.
    5686             :      */
    5687         202 :     if (fpinfo->local_conds)
    5688             :     {
    5689           6 :         List       *aggvars = NIL;
    5690             :         ListCell   *lc;
    5691             : 
    5692          12 :         foreach(lc, fpinfo->local_conds)
    5693             :         {
    5694           6 :             RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
    5695             : 
    5696           6 :             aggvars = list_concat(aggvars,
    5697           6 :                                   pull_var_clause((Node *) rinfo->clause,
    5698             :                                                   PVC_INCLUDE_AGGREGATES));
    5699             :         }
    5700             : 
    5701          14 :         foreach(lc, aggvars)
    5702             :         {
    5703          10 :             Expr       *expr = (Expr *) lfirst(lc);
    5704             : 
    5705             :             /*
    5706             :              * If aggregates within local conditions are not safe to push
    5707             :              * down, then we cannot push down the query.  Vars are already
    5708             :              * part of GROUP BY clause which are checked above, so no need to
    5709             :              * access them again here.  Again, we need not check
    5710             :              * is_foreign_param for a foreign aggregate.
    5711             :              */
    5712          10 :             if (IsA(expr, Aggref))
    5713             :             {
    5714          10 :                 if (!is_foreign_expr(root, grouped_rel, expr))
    5715           2 :                     return false;
    5716             : 
    5717           8 :                 tlist = add_to_flat_tlist(tlist, list_make1(expr));
    5718             :             }
    5719             :         }
    5720             :     }
    5721             : 
    5722             :     /* Store generated targetlist */
    5723         200 :     fpinfo->grouped_tlist = tlist;
    5724             : 
    5725             :     /* Safe to pushdown */
    5726         200 :     fpinfo->pushdown_safe = true;
    5727             : 
    5728             :     /*
    5729             :      * Set # of retrieved rows and cached relation costs to some negative
    5730             :      * value, so that we can detect when they are set to some sensible values,
    5731             :      * during one (usually the first) of the calls to estimate_path_cost_size.
    5732             :      */
    5733         200 :     fpinfo->retrieved_rows = -1;
    5734         200 :     fpinfo->rel_startup_cost = -1;
    5735         200 :     fpinfo->rel_total_cost = -1;
    5736             : 
    5737             :     /*
    5738             :      * Set the string describing this grouped relation to be used in EXPLAIN
    5739             :      * output of corresponding ForeignScan.
    5740             :      */
    5741         200 :     fpinfo->relation_name = makeStringInfo();
    5742         200 :     appendStringInfo(fpinfo->relation_name, "Aggregate on (%s)",
    5743         200 :                      ofpinfo->relation_name->data);
    5744             : 
    5745         200 :     return true;
    5746             : }
    5747             : 
    5748             : /*
    5749             :  * postgresGetForeignUpperPaths
    5750             :  *      Add paths for post-join operations like aggregation, grouping etc. if
    5751             :  *      corresponding operations are safe to push down.
    5752             :  */
    5753             : static void
    5754        1544 : postgresGetForeignUpperPaths(PlannerInfo *root, UpperRelationKind stage,
    5755             :                              RelOptInfo *input_rel, RelOptInfo *output_rel,
    5756             :                              void *extra)
    5757             : {
    5758             :     PgFdwRelationInfo *fpinfo;
    5759             : 
    5760             :     /*
    5761             :      * If input rel is not safe to pushdown, then simply return as we cannot
    5762             :      * perform any post-join operations on the foreign server.
    5763             :      */
    5764        2960 :     if (!input_rel->fdw_private ||
    5765        1416 :         !((PgFdwRelationInfo *) input_rel->fdw_private)->pushdown_safe)
    5766         242 :         return;
    5767             : 
    5768             :     /* Ignore stages we don't support; and skip any duplicate calls. */
    5769        1302 :     if ((stage != UPPERREL_GROUP_AGG &&
    5770         814 :          stage != UPPERREL_ORDERED &&
    5771        1268 :          stage != UPPERREL_FINAL) ||
    5772        1268 :         output_rel->fdw_private)
    5773          34 :         return;
    5774             : 
    5775        1268 :     fpinfo = (PgFdwRelationInfo *) palloc0(sizeof(PgFdwRelationInfo));
    5776        1268 :     fpinfo->pushdown_safe = false;
    5777        1268 :     fpinfo->stage = stage;
    5778        1268 :     output_rel->fdw_private = fpinfo;
    5779             : 
    5780        1268 :     switch (stage)
    5781             :     {
    5782             :         case UPPERREL_GROUP_AGG:
    5783         262 :             add_foreign_grouping_paths(root, input_rel, output_rel,
    5784             :                                        (GroupPathExtraData *) extra);
    5785         262 :             break;
    5786             :         case UPPERREL_ORDERED:
    5787         226 :             add_foreign_ordered_paths(root, input_rel, output_rel);
    5788         226 :             break;
    5789             :         case UPPERREL_FINAL:
    5790         780 :             add_foreign_final_paths(root, input_rel, output_rel,
    5791             :                                     (FinalPathExtraData *) extra);
    5792         780 :             break;
    5793             :         default:
    5794           0 :             elog(ERROR, "unexpected upper relation: %d", (int) stage);
    5795             :             break;
    5796             :     }
    5797             : }
    5798             : 
    5799             : /*
    5800             :  * add_foreign_grouping_paths
    5801             :  *      Add foreign path for grouping and/or aggregation.
    5802             :  *
    5803             :  * Given input_rel represents the underlying scan.  The paths are added to the
    5804             :  * given grouped_rel.
    5805             :  */
    5806             : static void
    5807         262 : add_foreign_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
    5808             :                            RelOptInfo *grouped_rel,
    5809             :                            GroupPathExtraData *extra)
    5810             : {
    5811         262 :     Query      *parse = root->parse;
    5812         262 :     PgFdwRelationInfo *ifpinfo = input_rel->fdw_private;
    5813         262 :     PgFdwRelationInfo *fpinfo = grouped_rel->fdw_private;
    5814             :     ForeignPath *grouppath;
    5815             :     double      rows;
    5816             :     int         width;
    5817             :     Cost        startup_cost;
    5818             :     Cost        total_cost;
    5819             : 
    5820             :     /* Nothing to be done, if there is no grouping or aggregation required. */
    5821         262 :     if (!parse->groupClause && !parse->groupingSets && !parse->hasAggs &&
    5822           0 :         !root->hasHavingQual)
    5823          62 :         return;
    5824             : 
    5825             :     Assert(extra->patype == PARTITIONWISE_AGGREGATE_NONE ||
    5826             :            extra->patype == PARTITIONWISE_AGGREGATE_FULL);
    5827             : 
    5828             :     /* save the input_rel as outerrel in fpinfo */
    5829         262 :     fpinfo->outerrel = input_rel;
    5830             : 
    5831             :     /*
    5832             :      * Copy foreign table, foreign server, user mapping, FDW options etc.
    5833             :      * details from the input relation's fpinfo.
    5834             :      */
    5835         262 :     fpinfo->table = ifpinfo->table;
    5836         262 :     fpinfo->server = ifpinfo->server;
    5837         262 :     fpinfo->user = ifpinfo->user;
    5838         262 :     merge_fdw_options(fpinfo, ifpinfo, NULL);
    5839             : 
    5840             :     /*
    5841             :      * Assess if it is safe to push down aggregation and grouping.
    5842             :      *
    5843             :      * Use HAVING qual from extra. In case of child partition, it will have
    5844             :      * translated Vars.
    5845             :      */
    5846         262 :     if (!foreign_grouping_ok(root, grouped_rel, extra->havingQual))
    5847          62 :         return;
    5848             : 
    5849             :     /*
    5850             :      * Compute the selectivity and cost of the local_conds, so we don't have
    5851             :      * to do it over again for each path.  (Currently we create just a single
    5852             :      * path here, but in future it would be possible that we build more paths
    5853             :      * such as pre-sorted paths as in postgresGetForeignPaths and
    5854             :      * postgresGetForeignJoinPaths.)  The best we can do for these conditions
    5855             :      * is to estimate selectivity on the basis of local statistics.
    5856             :      */
    5857         200 :     fpinfo->local_conds_sel = clauselist_selectivity(root,
    5858             :                                                      fpinfo->local_conds,
    5859             :                                                      0,
    5860             :                                                      JOIN_INNER,
    5861             :                                                      NULL);
    5862             : 
    5863         200 :     cost_qual_eval(&fpinfo->local_conds_cost, fpinfo->local_conds, root);
    5864             : 
    5865             :     /* Estimate the cost of push down */
    5866         200 :     estimate_path_cost_size(root, grouped_rel, NIL, NIL, NULL,
    5867             :                             &rows, &width, &startup_cost, &total_cost);
    5868             : 
    5869             :     /* Now update this information in the fpinfo */
    5870         200 :     fpinfo->rows = rows;
    5871         200 :     fpinfo->width = width;
    5872         200 :     fpinfo->startup_cost = startup_cost;
    5873         200 :     fpinfo->total_cost = total_cost;
    5874             : 
    5875             :     /* Create and add foreign path to the grouping relation. */
    5876         400 :     grouppath = create_foreign_upper_path(root,
    5877             :                                           grouped_rel,
    5878         200 :                                           grouped_rel->reltarget,
    5879             :                                           rows,
    5880             :                                           startup_cost,
    5881             :                                           total_cost,
    5882             :                                           NIL,  /* no pathkeys */
    5883             :                                           NULL,
    5884             :                                           NIL); /* no fdw_private */
    5885             : 
    5886             :     /* Add generated path into grouped_rel by add_path(). */
    5887         200 :     add_path(grouped_rel, (Path *) grouppath);
    5888             : }
    5889             : 
    5890             : /*
    5891             :  * add_foreign_ordered_paths
    5892             :  *      Add foreign paths for performing the final sort remotely.
    5893             :  *
    5894             :  * Given input_rel contains the source-data Paths.  The paths are added to the
    5895             :  * given ordered_rel.
    5896             :  */
    5897             : static void
    5898         226 : add_foreign_ordered_paths(PlannerInfo *root, RelOptInfo *input_rel,
    5899             :                           RelOptInfo *ordered_rel)
    5900             : {
    5901         226 :     Query      *parse = root->parse;
    5902         226 :     PgFdwRelationInfo *ifpinfo = input_rel->fdw_private;
    5903         226 :     PgFdwRelationInfo *fpinfo = ordered_rel->fdw_private;
    5904             :     PgFdwPathExtraData *fpextra;
    5905             :     double      rows;
    5906             :     int         width;
    5907             :     Cost        startup_cost;
    5908             :     Cost        total_cost;
    5909             :     List       *fdw_private;
    5910             :     ForeignPath *ordered_path;
    5911             :     ListCell   *lc;
    5912             : 
    5913             :     /* Shouldn't get here unless the query has ORDER BY */
    5914             :     Assert(parse->sortClause);
    5915             : 
    5916             :     /* We don't support cases where there are any SRFs in the targetlist */
    5917         226 :     if (parse->hasTargetSRFs)
    5918         142 :         return;
    5919             : 
    5920             :     /* Save the input_rel as outerrel in fpinfo */
    5921         226 :     fpinfo->outerrel = input_rel;
    5922             : 
    5923             :     /*
    5924             :      * Copy foreign table, foreign server, user mapping, FDW options etc.
    5925             :      * details from the input relation's fpinfo.
    5926             :      */
    5927         226 :     fpinfo->table = ifpinfo->table;
    5928         226 :     fpinfo->server = ifpinfo->server;
    5929         226 :     fpinfo->user = ifpinfo->user;
    5930         226 :     merge_fdw_options(fpinfo, ifpinfo, NULL);
    5931             : 
    5932             :     /*
    5933             :      * If the input_rel is a base or join relation, we would already have
    5934             :      * considered pushing down the final sort to the remote server when
    5935             :      * creating pre-sorted foreign paths for that relation, because the
    5936             :      * query_pathkeys is set to the root->sort_pathkeys in that case (see
    5937             :      * standard_qp_callback()).
    5938             :      */
    5939         398 :     if (input_rel->reloptkind == RELOPT_BASEREL ||
    5940         172 :         input_rel->reloptkind == RELOPT_JOINREL)
    5941             :     {
    5942             :         Assert(root->query_pathkeys == root->sort_pathkeys);
    5943             : 
    5944             :         /* Safe to push down if the query_pathkeys is safe to push down */
    5945         134 :         fpinfo->pushdown_safe = ifpinfo->qp_is_pushdown_safe;
    5946             : 
    5947         134 :         return;
    5948             :     }
    5949             : 
    5950             :     /* The input_rel should be a grouping relation */
    5951             :     Assert(input_rel->reloptkind == RELOPT_UPPER_REL &&
    5952             :            ifpinfo->stage == UPPERREL_GROUP_AGG);
    5953             : 
    5954             :     /*
    5955             :      * We try to create a path below by extending a simple foreign path for
    5956             :      * the underlying grouping relation to perform the final sort remotely,
    5957             :      * which is stored into the fdw_private list of the resulting path.
    5958             :      */
    5959             : 
    5960             :     /* Assess if it is safe to push down the final sort */
    5961         188 :     foreach(lc, root->sort_pathkeys)
    5962             :     {
    5963         104 :         PathKey    *pathkey = (PathKey *) lfirst(lc);
    5964         104 :         EquivalenceClass *pathkey_ec = pathkey->pk_eclass;
    5965             :         Expr       *sort_expr;
    5966             : 
    5967             :         /*
    5968             :          * is_foreign_expr would detect volatile expressions as well, but
    5969             :          * checking ec_has_volatile here saves some cycles.
    5970             :          */
    5971         104 :         if (pathkey_ec->ec_has_volatile)
    5972           8 :             return;
    5973             : 
    5974             :         /* Get the sort expression for the pathkey_ec */
    5975          96 :         sort_expr = find_em_expr_for_input_target(root,
    5976             :                                                   pathkey_ec,
    5977          96 :                                                   input_rel->reltarget);
    5978             : 
    5979             :         /* If it's unsafe to remote, we cannot push down the final sort */
    5980          96 :         if (!is_foreign_expr(root, input_rel, sort_expr))
    5981           0 :             return;
    5982             :     }
    5983             : 
    5984             :     /* Safe to push down */
    5985          84 :     fpinfo->pushdown_safe = true;
    5986             : 
    5987             :     /* Construct PgFdwPathExtraData */
    5988          84 :     fpextra = (PgFdwPathExtraData *) palloc0(sizeof(PgFdwPathExtraData));
    5989          84 :     fpextra->target = root->upper_targets[UPPERREL_ORDERED];
    5990          84 :     fpextra->has_final_sort = true;
    5991             : 
    5992             :     /* Estimate the costs of performing the final sort remotely */
    5993          84 :     estimate_path_cost_size(root, input_rel, NIL, root->sort_pathkeys, fpextra,
    5994             :                             &rows, &width, &startup_cost, &total_cost);
    5995             : 
    5996             :     /*
    5997             :      * Build the fdw_private list that will be used by postgresGetForeignPlan.
    5998             :      * Items in the list must match order in enum FdwPathPrivateIndex.
    5999             :      */
    6000          84 :     fdw_private = list_make2(makeInteger(true), makeInteger(false));
    6001             : 
    6002             :     /* Create foreign ordering path */
    6003         168 :     ordered_path = create_foreign_upper_path(root,
    6004             :                                              input_rel,
    6005          84 :                                              root->upper_targets[UPPERREL_ORDERED],
    6006             :                                              rows,
    6007             :                                              startup_cost,
    6008             :                                              total_cost,
    6009             :                                              root->sort_pathkeys,
    6010             :                                              NULL,  /* no extra plan */
    6011             :                                              fdw_private);
    6012             : 
    6013             :     /* and add it to the ordered_rel */
    6014          84 :     add_path(ordered_rel, (Path *) ordered_path);
    6015             : }
    6016             : 
    6017             : /*
    6018             :  * add_foreign_final_paths
    6019             :  *      Add foreign paths for performing the final processing remotely.
    6020             :  *
    6021             :  * Given input_rel contains the source-data Paths.  The paths are added to the
    6022             :  * given final_rel.
    6023             :  */
    6024             : static void
    6025         780 : add_foreign_final_paths(PlannerInfo *root, RelOptInfo *input_rel,
    6026             :                         RelOptInfo *final_rel,
    6027             :                         FinalPathExtraData *extra)
    6028             : {
    6029         780 :     Query      *parse = root->parse;
    6030         780 :     PgFdwRelationInfo *ifpinfo = (PgFdwRelationInfo *) input_rel->fdw_private;
    6031         780 :     PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) final_rel->fdw_private;
    6032         780 :     bool        has_final_sort = false;
    6033         780 :     List       *pathkeys = NIL;
    6034             :     PgFdwPathExtraData *fpextra;
    6035         780 :     bool        save_use_remote_estimate = false;
    6036             :     double      rows;
    6037             :     int         width;
    6038             :     Cost        startup_cost;
    6039             :     Cost        total_cost;
    6040             :     List       *fdw_private;
    6041             :     ForeignPath *final_path;
    6042             : 
    6043             :     /*
    6044             :      * Currently, we only support this for SELECT commands
    6045             :      */
    6046         780 :     if (parse->commandType != CMD_SELECT)
    6047         864 :         return;
    6048             : 
    6049             :     /*
    6050             :      * No work if there is no FOR UPDATE/SHARE clause and if there is no need
    6051             :      * to add a LIMIT node
    6052             :      */
    6053         540 :     if (!parse->rowMarks && !extra->limit_needed)
    6054         362 :         return;
    6055             : 
    6056             :     /* We don't support cases where there are any SRFs in the targetlist */
    6057         178 :     if (parse->hasTargetSRFs)
    6058           0 :         return;
    6059             : 
    6060             :     /* Save the input_rel as outerrel in fpinfo */
    6061         178 :     fpinfo->outerrel = input_rel;
    6062             : 
    6063             :     /*
    6064             :      * Copy foreign table, foreign server, user mapping, FDW options etc.
    6065             :      * details from the input relation's fpinfo.
    6066             :      */
    6067         178 :     fpinfo->table = ifpinfo->table;
    6068         178 :     fpinfo->server = ifpinfo->server;
    6069         178 :     fpinfo->user = ifpinfo->user;
    6070         178 :     merge_fdw_options(fpinfo, ifpinfo, NULL);
    6071             : 
    6072             :     /*
    6073             :      * If there is no need to add a LIMIT node, there might be a ForeignPath
    6074             :      * in the input_rel's pathlist that implements all behavior of the query.
    6075             :      * Note: we would already have accounted for the query's FOR UPDATE/SHARE
    6076             :      * (if any) before we get here.
    6077             :      */
    6078         178 :     if (!extra->limit_needed)
    6079             :     {
    6080             :         ListCell   *lc;
    6081             : 
    6082             :         Assert(parse->rowMarks);
    6083             : 
    6084             :         /*
    6085             :          * Grouping and aggregation are not supported with FOR UPDATE/SHARE,
    6086             :          * so the input_rel should be a base, join, or ordered relation; and
    6087             :          * if it's an ordered relation, its input relation should be a base or
    6088             :          * join relation.
    6089             :          */
    6090             :         Assert(input_rel->reloptkind == RELOPT_BASEREL ||
    6091             :                input_rel->reloptkind == RELOPT_JOINREL ||
    6092             :                (input_rel->reloptkind == RELOPT_UPPER_REL &&
    6093             :                 ifpinfo->stage == UPPERREL_ORDERED &&
    6094             :                 (ifpinfo->outerrel->reloptkind == RELOPT_BASEREL ||
    6095             :                  ifpinfo->outerrel->reloptkind == RELOPT_JOINREL)));
    6096             : 
    6097           8 :         foreach(lc, input_rel->pathlist)
    6098             :         {
    6099           8 :             Path       *path = (Path *) lfirst(lc);
    6100             : 
    6101             :             /*
    6102             :              * apply_scanjoin_target_to_paths() uses create_projection_path()
    6103             :              * to adjust each of its input paths if needed, whereas
    6104             :              * create_ordered_paths() uses apply_projection_to_path() to do
    6105             :              * that.  So the former might have put a ProjectionPath on top of
    6106             :              * the ForeignPath; look through ProjectionPath and see if the
    6107             :              * path underneath it is ForeignPath.
    6108             :              */
    6109           8 :             if (IsA(path, ForeignPath) ||
    6110           0 :                 (IsA(path, ProjectionPath) &&
    6111           0 :                  IsA(((ProjectionPath *) path)->subpath, ForeignPath)))
    6112             :             {
    6113             :                 /*
    6114             :                  * Create foreign final path; this gets rid of a
    6115             :                  * no-longer-needed outer plan (if any), which makes the
    6116             :                  * EXPLAIN output look cleaner
    6117             :                  */
    6118           8 :                 final_path = create_foreign_upper_path(root,
    6119             :                                                        path->parent,
    6120             :                                                        path->pathtarget,
    6121             :                                                        path->rows,
    6122             :                                                        path->startup_cost,
    6123             :                                                        path->total_cost,
    6124             :                                                        path->pathkeys,
    6125             :                                                        NULL,    /* no extra plan */
    6126             :                                                        NULL);   /* no fdw_private */
    6127             : 
    6128             :                 /* and add it to the final_rel */
    6129           8 :                 add_path(final_rel, (Path *) final_path);
    6130             : 
    6131             :                 /* Safe to push down */
    6132           8 :                 fpinfo->pushdown_safe = true;
    6133             : 
    6134           8 :                 return;
    6135             :             }
    6136             :         }
    6137             : 
    6138             :         /*
    6139             :          * If we get here it means no ForeignPaths; since we would already
    6140             :          * have considered pushing down all operations for the query to the
    6141             :          * remote server, give up on it.
    6142             :          */
    6143           0 :         return;
    6144             :     }
    6145             : 
    6146             :     Assert(extra->limit_needed);
    6147             : 
    6148             :     /*
    6149             :      * If the input_rel is an ordered relation, replace the input_rel with its
    6150             :      * input relation
    6151             :      */
    6152         276 :     if (input_rel->reloptkind == RELOPT_UPPER_REL &&
    6153         106 :         ifpinfo->stage == UPPERREL_ORDERED)
    6154             :     {
    6155         106 :         input_rel = ifpinfo->outerrel;
    6156         106 :         ifpinfo = (PgFdwRelationInfo *) input_rel->fdw_private;
    6157         106 :         has_final_sort = true;
    6158         106 :         pathkeys = root->sort_pathkeys;
    6159             :     }
    6160             : 
    6161             :     /* The input_rel should be a base, join, or grouping relation */
    6162             :     Assert(input_rel->reloptkind == RELOPT_BASEREL ||
    6163             :            input_rel->reloptkind == RELOPT_JOINREL ||
    6164             :            (input_rel->reloptkind == RELOPT_UPPER_REL &&
    6165             :             ifpinfo->stage == UPPERREL_GROUP_AGG));
    6166             : 
    6167             :     /*
    6168             :      * We try to create a path below by extending a simple foreign path for
    6169             :      * the underlying base, join, or grouping relation to perform the final
    6170             :      * sort (if has_final_sort) and the LIMIT restriction remotely, which is
    6171             :      * stored into the fdw_private list of the resulting path.  (We
    6172             :      * re-estimate the costs of sorting the underlying relation, if
    6173             :      * has_final_sort.)
    6174             :      */
    6175             : 
    6176             :     /*
    6177             :      * Assess if it is safe to push down the LIMIT and OFFSET to the remote
    6178             :      * server
    6179             :      */
    6180             : 
    6181             :     /*
    6182             :      * If the underlying relation has any local conditions, the LIMIT/OFFSET
    6183             :      * cannot be pushed down.
    6184             :      */
    6185         170 :     if (ifpinfo->local_conds)
    6186          14 :         return;
    6187             : 
    6188             :     /*
    6189             :      * Also, the LIMIT/OFFSET cannot be pushed down, if their expressions are
    6190             :      * not safe to remote.
    6191             :      */
    6192         312 :     if (!is_foreign_expr(root, input_rel, (Expr *) parse->limitOffset) ||
    6193         156 :         !is_foreign_expr(root, input_rel, (Expr *) parse->limitCount))
    6194           0 :         return;
    6195             : 
    6196             :     /* Safe to push down */
    6197         156 :     fpinfo->pushdown_safe = true;
    6198             : 
    6199             :     /* Construct PgFdwPathExtraData */
    6200         156 :     fpextra = (PgFdwPathExtraData *) palloc0(sizeof(PgFdwPathExtraData));
    6201         156 :     fpextra->target = root->upper_targets[UPPERREL_FINAL];
    6202         156 :     fpextra->has_final_sort = has_final_sort;
    6203         156 :     fpextra->has_limit = extra->limit_needed;
    6204         156 :     fpextra->limit_tuples = extra->limit_tuples;
    6205         156 :     fpextra->count_est = extra->count_est;
    6206         156 :     fpextra->offset_est = extra->offset_est;
    6207             : 
    6208             :     /*
    6209             :      * Estimate the costs of performing the final sort and the LIMIT
    6210             :      * restriction remotely.  If has_final_sort is false, we wouldn't need to
    6211             :      * execute EXPLAIN anymore if use_remote_estimate, since the costs can be
    6212             :      * roughly estimated using the costs we already have for the underlying
    6213             :      * relation, in the same way as when use_remote_estimate is false.  Since
    6214             :      * it's pretty expensive to execute EXPLAIN, force use_remote_estimate to
    6215             :      * false in that case.
    6216             :      */
    6217         156 :     if (!fpextra->has_final_sort)
    6218             :     {
    6219          58 :         save_use_remote_estimate = ifpinfo->use_remote_estimate;
    6220          58 :         ifpinfo->use_remote_estimate = false;
    6221             :     }
    6222         156 :     estimate_path_cost_size(root, input_rel, NIL, pathkeys, fpextra,
    6223             :                             &rows, &width, &startup_cost, &total_cost);
    6224         156 :     if (!fpextra->has_final_sort)
    6225          58 :         ifpinfo->use_remote_estimate = save_use_remote_estimate;
    6226             : 
    6227             :     /*
    6228             :      * Build the fdw_private list that will be used by postgresGetForeignPlan.
    6229             :      * Items in the list must match order in enum FdwPathPrivateIndex.
    6230             :      */
    6231         156 :     fdw_private = list_make2(makeInteger(has_final_sort),
    6232             :                              makeInteger(extra->limit_needed));
    6233             : 
    6234             :     /*
    6235             :      * Create foreign final path; this gets rid of a no-longer-needed outer
    6236             :      * plan (if any), which makes the EXPLAIN output look cleaner
    6237             :      */
    6238         312 :     final_path = create_foreign_upper_path(root,
    6239             :                                            input_rel,
    6240         156 :                                            root->upper_targets[UPPERREL_FINAL],
    6241             :                                            rows,
    6242             :                                            startup_cost,
    6243             :                                            total_cost,
    6244             :                                            pathkeys,
    6245             :                                            NULL,    /* no extra plan */
    6246             :                                            fdw_private);
    6247             : 
    6248             :     /* and add it to the final_rel */
    6249         156 :     add_path(final_rel, (Path *) final_path);
    6250             : }
    6251             : 
    6252             : /*
    6253             :  * Create a tuple from the specified row of the PGresult.
    6254             :  *
    6255             :  * rel is the local representation of the foreign table, attinmeta is
    6256             :  * conversion data for the rel's tupdesc, and retrieved_attrs is an
    6257             :  * integer list of the table column numbers present in the PGresult.
    6258             :  * temp_context is a working context that can be reset after each tuple.
    6259             :  */
    6260             : static HeapTuple
    6261      147180 : make_tuple_from_result_row(PGresult *res,
    6262             :                            int row,
    6263             :                            Relation rel,
    6264             :                            AttInMetadata *attinmeta,
    6265             :                            List *retrieved_attrs,
    6266             :                            ForeignScanState *fsstate,
    6267             :                            MemoryContext temp_context)
    6268             : {
    6269             :     HeapTuple   tuple;
    6270             :     TupleDesc   tupdesc;
    6271             :     Datum      *values;
    6272             :     bool       *nulls;
    6273      147180 :     ItemPointer ctid = NULL;
    6274             :     ConversionLocation errpos;
    6275             :     ErrorContextCallback errcallback;
    6276             :     MemoryContext oldcontext;
    6277             :     ListCell   *lc;
    6278             :     int         j;
    6279             : 
    6280             :     Assert(row < PQntuples(res));
    6281             : 
    6282             :     /*
    6283             :      * Do the following work in a temp context that we reset after each tuple.
    6284             :      * This cleans up not only the data we have direct access to, but any
    6285             :      * cruft the I/O functions might leak.
    6286             :      */
    6287      147180 :     oldcontext = MemoryContextSwitchTo(temp_context);
    6288             : 
    6289      147180 :     if (rel)
    6290       75696 :         tupdesc = RelationGetDescr(rel);
    6291             :     else
    6292             :     {
    6293             :         Assert(fsstate);
    6294       71484 :         tupdesc = fsstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
    6295             :     }
    6296             : 
    6297      147180 :     values = (Datum *) palloc0(tupdesc->natts * sizeof(Datum));
    6298      147180 :     nulls = (bool *) palloc(tupdesc->natts * sizeof(bool));
    6299             :     /* Initialize to nulls for any columns not present in result */
    6300      147180 :     memset(nulls, true, tupdesc->natts * sizeof(bool));
    6301             : 
    6302             :     /*
    6303             :      * Set up and install callback to report where conversion error occurs.
    6304             :      */
    6305      147180 :     errpos.rel = rel;
    6306      147180 :     errpos.cur_attno = 0;
    6307      147180 :     errpos.fsstate = fsstate;
    6308      147180 :     errcallback.callback = conversion_error_callback;
    6309      147180 :     errcallback.arg = (void *) &errpos;
    6310      147180 :     errcallback.previous = error_context_stack;
    6311      147180 :     error_context_stack = &errcallback;
    6312             : 
    6313             :     /*
    6314             :      * i indexes columns in the relation, j indexes columns in the PGresult.
    6315             :      */
    6316      147180 :     j = 0;
    6317      554748 :     foreach(lc, retrieved_attrs)
    6318             :     {
    6319      407576 :         int         i = lfirst_int(lc);
    6320             :         char       *valstr;
    6321             : 
    6322             :         /* fetch next column's textual value */
    6323      407576 :         if (PQgetisnull(res, row, j))
    6324        1256 :             valstr = NULL;
    6325             :         else
    6326      406320 :             valstr = PQgetvalue(res, row, j);
    6327             : 
    6328             :         /*
    6329             :          * convert value to internal representation
    6330             :          *
    6331             :          * Note: we ignore system columns other than ctid and oid in result
    6332             :          */
    6333      407576 :         errpos.cur_attno = i;
    6334      407576 :         if (i > 0)
    6335             :         {
    6336             :             /* ordinary column */
    6337             :             Assert(i <= tupdesc->natts);
    6338      405426 :             nulls[i - 1] = (valstr == NULL);
    6339             :             /* Apply the input function even to nulls, to support domains */
    6340     1216278 :             values[i - 1] = InputFunctionCall(&attinmeta->attinfuncs[i - 1],
    6341             :                                               valstr,
    6342      405426 :                                               attinmeta->attioparams[i - 1],
    6343      405426 :                                               attinmeta->atttypmods[i - 1]);
    6344             :         }
    6345        2150 :         else if (i == SelfItemPointerAttributeNumber)
    6346             :         {
    6347             :             /* ctid */
    6348        2150 :             if (valstr != NULL)
    6349             :             {
    6350             :                 Datum       datum;
    6351             : 
    6352        2150 :                 datum = DirectFunctionCall1(tidin, CStringGetDatum(valstr));
    6353        2150 :                 ctid = (ItemPointer) DatumGetPointer(datum);
    6354             :             }
    6355             :         }
    6356      407568 :         errpos.cur_attno = 0;
    6357             : 
    6358      407568 :         j++;
    6359             :     }
    6360             : 
    6361             :     /* Uninstall error context callback. */
    6362      147172 :     error_context_stack = errcallback.previous;
    6363             : 
    6364             :     /*
    6365             :      * Check we got the expected number of columns.  Note: j == 0 and
    6366             :      * PQnfields == 1 is expected, since deparse emits a NULL if no columns.
    6367             :      */
    6368      147172 :     if (j > 0 && j != PQnfields(res))
    6369           0 :         elog(ERROR, "remote query result does not match the foreign table");
    6370             : 
    6371             :     /*
    6372             :      * Build the result tuple in caller's memory context.
    6373             :      */
    6374      147172 :     MemoryContextSwitchTo(oldcontext);
    6375             : 
    6376      147172 :     tuple = heap_form_tuple(tupdesc, values, nulls);
    6377             : 
    6378             :     /*
    6379             :      * If we have a CTID to return, install it in both t_self and t_ctid.
    6380             :      * t_self is the normal place, but if the tuple is converted to a
    6381             :      * composite Datum, t_self will be lost; setting t_ctid allows CTID to be
    6382             :      * preserved during EvalPlanQual re-evaluations (see ROW_MARK_COPY code).
    6383             :      */
    6384      147172 :     if (ctid)
    6385        2150 :         tuple->t_self = tuple->t_data->t_ctid = *ctid;
    6386             : 
    6387             :     /*
    6388             :      * Stomp on the xmin, xmax, and cmin fields from the tuple created by
    6389             :      * heap_form_tuple.  heap_form_tuple actually creates the tuple with
    6390             :      * DatumTupleFields, not HeapTupleFields, but the executor expects
    6391             :      * HeapTupleFields and will happily extract system columns on that
    6392             :      * assumption.  If we don't do this then, for example, the tuple length
    6393             :      * ends up in the xmin field, which isn't what we want.
    6394             :      */
    6395      147172 :     HeapTupleHeaderSetXmax(tuple->t_data, InvalidTransactionId);
    6396      147172 :     HeapTupleHeaderSetXmin(tuple->t_data, InvalidTransactionId);
    6397      147172 :     HeapTupleHeaderSetCmin(tuple->t_data, InvalidTransactionId);
    6398             : 
    6399             :     /* Clean up */
    6400      147172 :     MemoryContextReset(temp_context);
    6401             : 
    6402      147172 :     return tuple;
    6403             : }
    6404             : 
    6405             : /*
    6406             :  * Callback function which is called when error occurs during column value
    6407             :  * conversion.  Print names of column and relation.
    6408             :  */
    6409             : static void
    6410           8 : conversion_error_callback(void *arg)
    6411             : {
    6412           8 :     const char *attname = NULL;
    6413           8 :     const char *relname = NULL;
    6414           8 :     bool        is_wholerow = false;
    6415           8 :     ConversionLocation *errpos = (ConversionLocation *) arg;
    6416             : 
    6417           8 :     if (errpos->rel)
    6418             :     {
    6419             :         /* error occurred in a scan against a foreign table */
    6420           2 :         TupleDesc   tupdesc = RelationGetDescr(errpos->rel);
    6421           2 :         Form_pg_attribute attr = TupleDescAttr(tupdesc, errpos->cur_attno - 1);
    6422             : 
    6423           2 :         if (errpos->cur_attno > 0 && errpos->cur_attno <= tupdesc->natts)
    6424           2 :             attname = NameStr(attr->attname);
    6425           0 :         else if (errpos->cur_attno == SelfItemPointerAttributeNumber)
    6426           0 :             attname = "ctid";
    6427             : 
    6428           2 :         relname = RelationGetRelationName(errpos->rel);
    6429             :     }
    6430             :     else
    6431             :     {
    6432             :         /* error occurred in a scan against a foreign join */
    6433           6 :         ForeignScanState *fsstate = errpos->fsstate;
    6434           6 :         ForeignScan *fsplan = castNode(ForeignScan, fsstate->ss.ps.plan);
    6435           6 :         EState     *estate = fsstate->ss.ps.state;
    6436             :         TargetEntry *tle;
    6437             : 
    6438           6 :         tle = list_nth_node(TargetEntry, fsplan->fdw_scan_tlist,
    6439             :                             errpos->cur_attno - 1);
    6440             : 
    6441             :         /*
    6442             :          * Target list can have Vars and expressions.  For Vars, we can get
    6443             :          * its relation, however for expressions we can't.  Thus for
    6444             :          * expressions, just show generic context message.
    6445             :          */
    6446           6 :         if (IsA(tle->expr, Var))
    6447             :         {
    6448             :             RangeTblEntry *rte;
    6449           4 :             Var        *var = (Var *) tle->expr;
    6450             : 
    6451           4 :             rte = exec_rt_fetch(var->varno, estate);
    6452             : 
    6453           4 :             if (var->varattno == 0)
    6454           2 :                 is_wholerow = true;
    6455             :             else
    6456           2 :                 attname = get_attname(rte->relid, var->varattno, false);
    6457             : 
    6458           4 :             relname = get_rel_name(rte->relid);
    6459             :         }
    6460             :         else
    6461           4 :             errcontext("processing expression at position %d in select list",
    6462           2 :                        errpos->cur_attno);
    6463             :     }
    6464             : 
    6465           8 :     if (relname)
    6466             :     {
    6467           6 :         if (is_wholerow)
    6468           2 :             errcontext("whole-row reference to foreign table \"%s\"", relname);
    6469           4 :         else if (attname)
    6470           4 :             errcontext("column \"%s\" of foreign table \"%s\"", attname, relname);
    6471             :     }
    6472           8 : }
    6473             : 
    6474             : /*
    6475             :  * Find an equivalence class member expression, all of whose Vars, come from
    6476             :  * the indicated relation.
    6477             :  */
    6478             : Expr *
    6479        2506 : find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel)
    6480             : {
    6481             :     ListCell   *lc_em;
    6482             : 
    6483        4982 :     foreach(lc_em, ec->ec_members)
    6484             :     {
    6485        4666 :         EquivalenceMember *em = lfirst(lc_em);
    6486             : 
    6487        6856 :         if (bms_is_subset(em->em_relids, rel->relids) &&
    6488        2190 :             !bms_is_empty(em->em_relids))
    6489             :         {
    6490             :             /*
    6491             :              * If there is more than one equivalence member whose Vars are
    6492             :              * taken entirely from this relation, we'll be content to choose
    6493             :              * any one of those.
    6494             :              */
    6495        2190 :             return em->em_expr;
    6496             :         }
    6497             :     }
    6498             : 
    6499             :     /* We didn't find any suitable equivalence class expression */
    6500         316 :     return NULL;
    6501             : }
    6502             : 
    6503             : /*
    6504             :  * Find an equivalence class member expression to be computed as a sort column
    6505             :  * in the given target.
    6506             :  */
    6507             : Expr *
    6508         434 : find_em_expr_for_input_target(PlannerInfo *root,
    6509             :                               EquivalenceClass *ec,
    6510             :                               PathTarget *target)
    6511             : {
    6512             :     ListCell   *lc1;
    6513             :     int         i;
    6514             : 
    6515         434 :     i = 0;
    6516         770 :     foreach(lc1, target->exprs)
    6517             :     {
    6518         770 :         Expr       *expr = (Expr *) lfirst(lc1);
    6519         770 :         Index       sgref = get_pathtarget_sortgroupref(target, i);
    6520             :         ListCell   *lc2;
    6521             : 
    6522             :         /* Ignore non-sort expressions */
    6523        1374 :         if (sgref == 0 ||
    6524         604 :             get_sortgroupref_clause_noerr(sgref,
    6525         604 :                                           root->parse->sortClause) == NULL)
    6526             :         {
    6527         182 :             i++;
    6528         182 :             continue;
    6529             :         }
    6530             : 
    6531             :         /* We ignore binary-compatible relabeling on both ends */
    6532        1176 :         while (expr && IsA(expr, RelabelType))
    6533           0 :             expr = ((RelabelType *) expr)->arg;
    6534             : 
    6535             :         /* Locate an EquivalenceClass member matching this expr, if any */
    6536         742 :         foreach(lc2, ec->ec_members)
    6537             :         {
    6538         588 :             EquivalenceMember *em = (EquivalenceMember *) lfirst(lc2);
    6539             :             Expr       *em_expr;
    6540             : 
    6541             :             /* Don't match constants */
    6542         588 :             if (em->em_is_const)
    6543           0 :                 continue;
    6544             : 
    6545             :             /* Ignore child members */
    6546         588 :             if (em->em_is_child)
    6547           0 :                 continue;
    6548             : 
    6549             :             /* Match if same expression (after stripping relabel) */
    6550         588 :             em_expr = em->em_expr;
    6551        1200 :             while (em_expr && IsA(em_expr, RelabelType))
    6552          24 :                 em_expr = ((RelabelType *) em_expr)->arg;
    6553             : 
    6554         588 :             if (equal(em_expr, expr))
    6555         434 :                 return em->em_expr;
    6556             :         }
    6557             : 
    6558         154 :         i++;
    6559             :     }
    6560             : 
    6561           0 :     elog(ERROR, "could not find pathkey item to sort");
    6562             :     return NULL;                /* keep compiler quiet */
    6563             : }

Generated by: LCOV version 1.13