Commit e6242c18 authored by Peter Eisentraut's avatar Peter Eisentraut

Set range table for CopyFrom() in tablesync

CopyFrom() needs a range table for formatting certain errors for
constraint violations.

This changes the mechanism of how the range table is passed to the
CopyFrom() executor state.  We used to generate the range table and one
entry for the relation manually inside DoCopy().  Now we use
addRangeTableEntryForRelation() to setup the range table and relation
entry for the ParseState, which is then passed down by BeginCopyFrom().

Author: Petr Jelinek <petr.jelinek@2ndquadrant.com>
Reported-by: default avatarEuler Taveira <euler@timbira.com.br>
parent ee692211
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include "optimizer/clauses.h" #include "optimizer/clauses.h"
#include "optimizer/planner.h" #include "optimizer/planner.h"
#include "nodes/makefuncs.h" #include "nodes/makefuncs.h"
#include "parser/parse_relation.h"
#include "rewrite/rewriteHandler.h" #include "rewrite/rewriteHandler.h"
#include "storage/fd.h" #include "storage/fd.h"
#include "tcop/tcopprot.h" #include "tcop/tcopprot.h"
...@@ -787,7 +788,6 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt, ...@@ -787,7 +788,6 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
Relation rel; Relation rel;
Oid relid; Oid relid;
RawStmt *query = NULL; RawStmt *query = NULL;
List *range_table = NIL;
/* Disallow COPY to/from file or program except to superusers. */ /* Disallow COPY to/from file or program except to superusers. */
if (!pipe && !superuser()) if (!pipe && !superuser())
...@@ -809,7 +809,6 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt, ...@@ -809,7 +809,6 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
if (stmt->relation) if (stmt->relation)
{ {
TupleDesc tupDesc; TupleDesc tupDesc;
AclMode required_access = (is_from ? ACL_INSERT : ACL_SELECT);
List *attnums; List *attnums;
ListCell *cur; ListCell *cur;
RangeTblEntry *rte; RangeTblEntry *rte;
...@@ -822,12 +821,8 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt, ...@@ -822,12 +821,8 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
relid = RelationGetRelid(rel); relid = RelationGetRelid(rel);
rte = makeNode(RangeTblEntry); rte = addRangeTableEntryForRelation(pstate, rel, NULL, false, false);
rte->rtekind = RTE_RELATION; rte->requiredPerms = (is_from ? ACL_INSERT : ACL_SELECT);
rte->relid = RelationGetRelid(rel);
rte->relkind = rel->rd_rel->relkind;
rte->requiredPerms = required_access;
range_table = list_make1(rte);
tupDesc = RelationGetDescr(rel); tupDesc = RelationGetDescr(rel);
attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist); attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
...@@ -841,7 +836,7 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt, ...@@ -841,7 +836,7 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
else else
rte->selectedCols = bms_add_member(rte->selectedCols, attno); rte->selectedCols = bms_add_member(rte->selectedCols, attno);
} }
ExecCheckRTPerms(range_table, true); ExecCheckRTPerms(pstate->p_rtable, true);
/* /*
* Permission check for row security policies. * Permission check for row security policies.
...@@ -977,7 +972,6 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt, ...@@ -977,7 +972,6 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
cstate = BeginCopyFrom(pstate, rel, stmt->filename, stmt->is_program, cstate = BeginCopyFrom(pstate, rel, stmt->filename, stmt->is_program,
NULL, stmt->attlist, stmt->options); NULL, stmt->attlist, stmt->options);
cstate->range_table = range_table;
*processed = CopyFrom(cstate); /* copy from file to database */ *processed = CopyFrom(cstate); /* copy from file to database */
EndCopyFrom(cstate); EndCopyFrom(cstate);
} }
...@@ -2921,6 +2915,10 @@ BeginCopyFrom(ParseState *pstate, ...@@ -2921,6 +2915,10 @@ BeginCopyFrom(ParseState *pstate,
cstate->raw_buf = (char *) palloc(RAW_BUF_SIZE + 1); cstate->raw_buf = (char *) palloc(RAW_BUF_SIZE + 1);
cstate->raw_buf_index = cstate->raw_buf_len = 0; 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); tupDesc = RelationGetDescr(cstate->rel);
attr = tupDesc->attrs; attr = tupDesc->attrs;
num_phys_attrs = tupDesc->natts; num_phys_attrs = tupDesc->natts;
......
...@@ -93,6 +93,8 @@ ...@@ -93,6 +93,8 @@
#include "commands/copy.h" #include "commands/copy.h"
#include "parser/parse_relation.h"
#include "replication/logicallauncher.h" #include "replication/logicallauncher.h"
#include "replication/logicalrelation.h" #include "replication/logicalrelation.h"
#include "replication/walreceiver.h" #include "replication/walreceiver.h"
...@@ -654,6 +656,7 @@ copy_table(Relation rel) ...@@ -654,6 +656,7 @@ copy_table(Relation rel)
StringInfoData cmd; StringInfoData cmd;
CopyState cstate; CopyState cstate;
List *attnamelist; List *attnamelist;
ParseState *pstate;
/* Get the publisher relation info. */ /* Get the publisher relation info. */
fetch_remote_table_info(get_namespace_name(RelationGetNamespace(rel)), fetch_remote_table_info(get_namespace_name(RelationGetNamespace(rel)),
...@@ -680,9 +683,11 @@ copy_table(Relation rel) ...@@ -680,9 +683,11 @@ copy_table(Relation rel)
copybuf = makeStringInfo(); 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); 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 */ /* Do the copy */
(void) CopyFrom(cstate); (void) CopyFrom(cstate);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment