Skip to content

Commit e6242c1

Browse files
committed
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: Euler Taveira <euler@timbira.com.br>
1 parent ee69221 commit e6242c1

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/backend/commands/copy.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "optimizer/clauses.h"
3838
#include "optimizer/planner.h"
3939
#include "nodes/makefuncs.h"
40+
#include "parser/parse_relation.h"
4041
#include "rewrite/rewriteHandler.h"
4142
#include "storage/fd.h"
4243
#include "tcop/tcopprot.h"
@@ -787,7 +788,6 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
787788
Relation rel;
788789
Oid relid;
789790
RawStmt *query = NULL;
790-
List *range_table = NIL;
791791

792792
/* Disallow COPY to/from file or program except to superusers. */
793793
if (!pipe && !superuser())
@@ -809,7 +809,6 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
809809
if (stmt->relation)
810810
{
811811
TupleDesc tupDesc;
812-
AclMode required_access = (is_from ? ACL_INSERT : ACL_SELECT);
813812
List *attnums;
814813
ListCell *cur;
815814
RangeTblEntry *rte;
@@ -822,12 +821,8 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
822821

823822
relid = RelationGetRelid(rel);
824823

825-
rte = makeNode(RangeTblEntry);
826-
rte->rtekind = RTE_RELATION;
827-
rte->relid = RelationGetRelid(rel);
828-
rte->relkind = rel->rd_rel->relkind;
829-
rte->requiredPerms = required_access;
830-
range_table = list_make1(rte);
824+
rte = addRangeTableEntryForRelation(pstate, rel, NULL, false, false);
825+
rte->requiredPerms = (is_from ? ACL_INSERT : ACL_SELECT);
831826

832827
tupDesc = RelationGetDescr(rel);
833828
attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
@@ -841,7 +836,7 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
841836
else
842837
rte->selectedCols = bms_add_member(rte->selectedCols, attno);
843838
}
844-
ExecCheckRTPerms(range_table, true);
839+
ExecCheckRTPerms(pstate->p_rtable, true);
845840

846841
/*
847842
* Permission check for row security policies.
@@ -977,7 +972,6 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
977972

978973
cstate = BeginCopyFrom(pstate, rel, stmt->filename, stmt->is_program,
979974
NULL, stmt->attlist, stmt->options);
980-
cstate->range_table = range_table;
981975
*processed = CopyFrom(cstate); /* copy from file to database */
982976
EndCopyFrom(cstate);
983977
}
@@ -2921,6 +2915,10 @@ BeginCopyFrom(ParseState *pstate,
29212915
cstate->raw_buf = (char *) palloc(RAW_BUF_SIZE + 1);
29222916
cstate->raw_buf_index = cstate->raw_buf_len = 0;
29232917

2918+
/* Assign range table, we'll need it in CopyFrom. */
2919+
if (pstate)
2920+
cstate->range_table = pstate->p_rtable;
2921+
29242922
tupDesc = RelationGetDescr(cstate->rel);
29252923
attr = tupDesc->attrs;
29262924
num_phys_attrs = tupDesc->natts;

src/backend/replication/logical/tablesync.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@
9393

9494
#include "commands/copy.h"
9595

96+
#include "parser/parse_relation.h"
97+
9698
#include "replication/logicallauncher.h"
9799
#include "replication/logicalrelation.h"
98100
#include "replication/walreceiver.h"
@@ -654,6 +656,7 @@ copy_table(Relation rel)
654656
StringInfoData cmd;
655657
CopyState cstate;
656658
List *attnamelist;
659+
ParseState *pstate;
657660

658661
/* Get the publisher relation info. */
659662
fetch_remote_table_info(get_namespace_name(RelationGetNamespace(rel)),
@@ -680,9 +683,11 @@ copy_table(Relation rel)
680683

681684
copybuf = makeStringInfo();
682685

683-
/* Create CopyState for ingestion of the data from publisher. */
686+
pstate = make_parsestate(NULL);
687+
addRangeTableEntryForRelation(pstate, rel, NULL, false, false);
688+
684689
attnamelist = make_copy_attnamelist(relmapentry);
685-
cstate = BeginCopyFrom(NULL, rel, NULL, false, copy_read_data, attnamelist, NIL);
690+
cstate = BeginCopyFrom(pstate, rel, NULL, false, copy_read_data, attnamelist, NIL);
686691

687692
/* Do the copy */
688693
(void) CopyFrom(cstate);

0 commit comments

Comments
 (0)