Skip to content

Commit 8e36028

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 d49f84b commit 8e36028

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
@@ -748,7 +748,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
748748
bool pipe = (stmt->filename == NULL);
749749
Relation rel;
750750
uint64 processed;
751-
RangeTblEntry *rte;
751+
List *range_table = NIL;
752752

753753
/* Disallow file COPY except to superusers. */
754754
if (!pipe && !superuser())
@@ -764,6 +764,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
764764
AclMode required_access = (is_from ? ACL_INSERT : ACL_SELECT);
765765
List *attnums;
766766
ListCell *cur;
767+
RangeTblEntry *rte;
767768

768769
Assert(!stmt->query);
769770

@@ -776,6 +777,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
776777
rte->relid = RelationGetRelid(rel);
777778
rte->relkind = rel->rd_rel->relkind;
778779
rte->requiredPerms = required_access;
780+
range_table = list_make1(rte);
779781

780782
tupDesc = RelationGetDescr(rel);
781783
attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
@@ -789,7 +791,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
789791
else
790792
rte->selectedCols = bms_add_member(rte->selectedCols, attno);
791793
}
792-
ExecCheckRTPerms(list_make1(rte), true);
794+
ExecCheckRTPerms(range_table, true);
793795
}
794796
else
795797
{
@@ -808,15 +810,14 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
808810

809811
cstate = BeginCopyFrom(rel, stmt->filename,
810812
stmt->attlist, stmt->options);
811-
cstate->range_table = list_make1(rte);
813+
cstate->range_table = range_table;
812814
processed = CopyFrom(cstate); /* copy from file to database */
813815
EndCopyFrom(cstate);
814816
}
815817
else
816818
{
817819
cstate = BeginCopyTo(rel, stmt->query, queryString, stmt->filename,
818820
stmt->attlist, stmt->options);
819-
cstate->range_table = list_make1(rte);
820821
processed = DoCopyTo(cstate); /* copy from database to file */
821822
EndCopyTo(cstate);
822823
}

0 commit comments

Comments
 (0)