]> granicus.if.org Git - postgresql/commitdiff
Clean up range-table building in copy.c
authorStephen Frost <sfrost@snowman.net>
Wed, 28 Jan 2015 22:42:51 +0000 (17:42 -0500)
committerStephen Frost <sfrost@snowman.net>
Wed, 28 Jan 2015 22:42:51 +0000 (17:42 -0500)
Commit 804b6b6db4dcfc590a468e7be390738f9f7755fb 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.

src/backend/commands/copy.c

index 9ab1e1989fb7fe277171bdc9f5a83831de30d3e8..be829a6eadc657080515e58534991051168f955c 100644 (file)
@@ -785,7 +785,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
        bool            pipe = (stmt->filename == NULL);
        Relation        rel;
        Oid                     relid;
-       RangeTblEntry *rte;
+       List       *range_table = NIL;
 
        /* Disallow COPY to/from file or program except to superusers. */
        if (!pipe && !superuser())
@@ -810,6 +810,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
                AclMode         required_access = (is_from ? ACL_INSERT : ACL_SELECT);
                List       *attnums;
                ListCell   *cur;
+               RangeTblEntry *rte;
 
                Assert(!stmt->query);
 
@@ -824,6 +825,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
                rte->relid = RelationGetRelid(rel);
                rte->relkind = rel->rd_rel->relkind;
                rte->requiredPerms = required_access;
+               range_table = list_make1(rte);
 
                tupDesc = RelationGetDescr(rel);
                attnums = CopyGetAttnums(tupDesc, rel, stmt->attlist);
@@ -837,7 +839,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
                        else
                                rte->selectedCols = bms_add_member(rte->selectedCols, attno);
                }
-               ExecCheckRTPerms(list_make1(rte), true);
+               ExecCheckRTPerms(range_table, true);
        }
        else
        {
@@ -857,7 +859,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
 
                cstate = BeginCopyFrom(rel, stmt->filename, stmt->is_program,
                                                           stmt->attlist, stmt->options);
-               cstate->range_table = list_make1(rte);
+               cstate->range_table = range_table;
                *processed = CopyFrom(cstate);  /* copy from file to database */
                EndCopyFrom(cstate);
        }
@@ -866,7 +868,6 @@ DoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
                cstate = BeginCopyTo(rel, stmt->query, queryString,
                                                         stmt->filename, stmt->is_program,
                                                         stmt->attlist, stmt->options);
-               cstate->range_table = list_make1(rte);
                *processed = DoCopyTo(cstate);  /* copy from database to file */
                EndCopyTo(cstate);
        }