]> 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:28 +0000 (17:42 -0500)
committerStephen Frost <sfrost@snowman.net>
Wed, 28 Jan 2015 22:42:28 +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 72abd770f7a705b4fc64bc8ec80be3e295d1b1c2..8cb2f13b27861b533845b85a4ea2bdd21c51ff5a 100644 (file)
@@ -790,7 +790,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
        Relation        rel;
        Oid                     relid;
        Node       *query = NULL;
-       RangeTblEntry *rte;
+       List       *range_table = NIL;
 
        /* Disallow COPY to/from file or program except to superusers. */
        if (!pipe && !superuser())
@@ -815,6 +815,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);
 
@@ -829,6 +830,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);
@@ -842,7 +844,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);
 
                /*
                 * Permission check for row security policies.
@@ -921,7 +923,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);
        }
@@ -930,7 +932,6 @@ DoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
                cstate = BeginCopyTo(rel, query, queryString, relid,
                                                         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);
        }