Skip to content

Commit d5d46e6

Browse files
committed
Clean up range-table building in copy.c
Commit 804b6b6 added the build of a range table in copy.c to initialize the EState es_range_table since it can be needed in error paths. Unfortunately, that commit didn't appreciate that some code paths might end up not initializing the rte which is used to build the range table. Fix that and clean up a couple others things along the way- build it only once and don't explicitly set it on the !is_from path as it doesn't make any sense there (cstate is palloc0'd, so this isn't an issue from an initializing standpoint either). The prior commit went back to 9.0, but this only goes back to 9.1 as prior to that the range table build happens immediately after building the RTE and therefore doesn't suffer from this issue. Pointed out by Robert.
1 parent 4b98742 commit d5d46e6

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/backend/commands/copy.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
783783
bool pipe = (stmt->filename == NULL);
784784
Relation rel;
785785
Oid relid;
786-
RangeTblEntry *rte;
786+
List *range_table = NIL;
787787

788788
/* Disallow COPY to/from file or program except to superusers. */
789789
if (!pipe && !superuser())
@@ -808,6 +808,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
808808
AclMode required_access = (is_from ? ACL_INSERT : ACL_SELECT);
809809
List *attnums;
810810
ListCell *cur;
811+
RangeTblEntry *rte;
811812

812813
Assert(!stmt->query);
813814

@@ -822,6 +823,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
822823
rte->relid = RelationGetRelid(rel);
823824
rte->relkind = rel->rd_rel->relkind;
824825
rte->requiredPerms = required_access;
826+
range_table = list_make1(rte);
825827

826828
tupDesc = RelationGetDescr(rel);
827829
attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
@@ -835,7 +837,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
835837
else
836838
rte->selectedCols = bms_add_member(rte->selectedCols, attno);
837839
}
838-
ExecCheckRTPerms(list_make1(rte), true);
840+
ExecCheckRTPerms(range_table, true);
839841
}
840842
else
841843
{
@@ -855,7 +857,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
855857

856858
cstate = BeginCopyFrom(rel, stmt->filename, stmt->is_program,
857859
stmt->attlist, stmt->options);
858-
cstate->range_table = list_make1(rte);
860+
cstate->range_table = range_table;
859861
*processed = CopyFrom(cstate); /* copy from file to database */
860862
EndCopyFrom(cstate);
861863
}
@@ -864,7 +866,6 @@ DoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
864866
cstate = BeginCopyTo(rel, stmt->query, queryString,
865867
stmt->filename, stmt->is_program,
866868
stmt->attlist, stmt->options);
867-
cstate->range_table = list_make1(rte);
868869
*processed = DoCopyTo(cstate); /* copy from database to file */
869870
EndCopyTo(cstate);
870871
}

0 commit comments

Comments
 (0)