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

Generated by: LCOV version 1.13