#include "optimizer/clauses.h"
 #include "optimizer/planner.h"
 #include "nodes/makefuncs.h"
+#include "parser/parse_relation.h"
 #include "rewrite/rewriteHandler.h"
 #include "storage/fd.h"
 #include "tcop/tcopprot.h"
    Relation    rel;
    Oid         relid;
    RawStmt    *query = NULL;
-   List       *range_table = NIL;
 
    /* Disallow COPY to/from file or program except to superusers. */
    if (!pipe && !superuser())
    if (stmt->relation)
    {
        TupleDesc   tupDesc;
-       AclMode     required_access = (is_from ? ACL_INSERT : ACL_SELECT);
        List       *attnums;
        ListCell   *cur;
        RangeTblEntry *rte;
 
        relid = RelationGetRelid(rel);
 
-       rte = makeNode(RangeTblEntry);
-       rte->rtekind = RTE_RELATION;
-       rte->relid = RelationGetRelid(rel);
-       rte->relkind = rel->rd_rel->relkind;
-       rte->requiredPerms = required_access;
-       range_table = list_make1(rte);
+       rte = addRangeTableEntryForRelation(pstate, rel, NULL, false, false);
+       rte->requiredPerms = (is_from ? ACL_INSERT : ACL_SELECT);
 
        tupDesc = RelationGetDescr(rel);
        attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
            else
                rte->selectedCols = bms_add_member(rte->selectedCols, attno);
        }
-       ExecCheckRTPerms(range_table, true);
+       ExecCheckRTPerms(pstate->p_rtable, true);
 
        /*
         * Permission check for row security policies.
 
        cstate = BeginCopyFrom(pstate, rel, stmt->filename, stmt->is_program,
                               NULL, stmt->attlist, stmt->options);
-       cstate->range_table = range_table;
        *processed = CopyFrom(cstate);  /* copy from file to database */
        EndCopyFrom(cstate);
    }
    cstate->raw_buf = (char *) palloc(RAW_BUF_SIZE + 1);
    cstate->raw_buf_index = cstate->raw_buf_len = 0;
 
+   /* Assign range table, we'll need it in CopyFrom. */
+   if (pstate)
+       cstate->range_table = pstate->p_rtable;
+
    tupDesc = RelationGetDescr(cstate->rel);
    attr = tupDesc->attrs;
    num_phys_attrs = tupDesc->natts;
 
 
 #include "commands/copy.h"
 
+#include "parser/parse_relation.h"
+
 #include "replication/logicallauncher.h"
 #include "replication/logicalrelation.h"
 #include "replication/walreceiver.h"
    StringInfoData      cmd;
    CopyState   cstate;
    List       *attnamelist;
+   ParseState *pstate;
 
    /* Get the publisher relation info. */
    fetch_remote_table_info(get_namespace_name(RelationGetNamespace(rel)),
 
    copybuf = makeStringInfo();
 
-   /* Create CopyState for ingestion of the data from publisher. */
+   pstate = make_parsestate(NULL);
+   addRangeTableEntryForRelation(pstate, rel, NULL, false, false);
+
    attnamelist = make_copy_attnamelist(relmapentry);
-   cstate = BeginCopyFrom(NULL, rel, NULL, false, copy_read_data, attnamelist, NIL);
+   cstate = BeginCopyFrom(pstate, rel, NULL, false, copy_read_data, attnamelist, NIL);
 
    /* Do the copy */
    (void) CopyFrom(cstate);