diff options
Diffstat (limited to 'src/backend/commands/cluster.c')
-rw-r--r-- | src/backend/commands/cluster.c | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c index 2b042978db0..f8ec8daf618 100644 --- a/src/backend/commands/cluster.c +++ b/src/backend/commands/cluster.c @@ -253,6 +253,9 @@ void cluster_rel(Oid tableOid, Oid indexOid, int options) { Relation OldHeap; + Oid save_userid; + int save_sec_context; + int save_nestlevel; bool verbose = ((options & CLUOPT_VERBOSE) != 0); bool recheck = ((options & CLUOPT_RECHECK) != 0); @@ -283,6 +286,16 @@ cluster_rel(Oid tableOid, Oid indexOid, int options) } /* + * Switch to the table owner's userid, so that any index functions are run + * as that user. Also lock down security-restricted operations and + * arrange to make GUC variable changes local to this command. + */ + GetUserIdAndSecContext(&save_userid, &save_sec_context); + SetUserIdAndSecContext(OldHeap->rd_rel->relowner, + save_sec_context | SECURITY_RESTRICTED_OPERATION); + save_nestlevel = NewGUCNestLevel(); + + /* * Since we may open a new transaction for each relation, we have to check * that the relation still is what we think it is. * @@ -293,11 +306,10 @@ cluster_rel(Oid tableOid, Oid indexOid, int options) if (recheck) { /* Check that the user still owns the relation */ - if (!pg_class_ownercheck(tableOid, GetUserId())) + if (!pg_class_ownercheck(tableOid, save_userid)) { relation_close(OldHeap, AccessExclusiveLock); - pgstat_progress_end_command(); - return; + goto out; } /* @@ -311,8 +323,7 @@ cluster_rel(Oid tableOid, Oid indexOid, int options) if (RELATION_IS_OTHER_TEMP(OldHeap)) { relation_close(OldHeap, AccessExclusiveLock); - pgstat_progress_end_command(); - return; + goto out; } if (OidIsValid(indexOid)) @@ -323,8 +334,7 @@ cluster_rel(Oid tableOid, Oid indexOid, int options) if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(indexOid))) { relation_close(OldHeap, AccessExclusiveLock); - pgstat_progress_end_command(); - return; + goto out; } /* @@ -333,8 +343,7 @@ cluster_rel(Oid tableOid, Oid indexOid, int options) if (!get_index_isclustered(indexOid)) { relation_close(OldHeap, AccessExclusiveLock); - pgstat_progress_end_command(); - return; + goto out; } } } @@ -387,8 +396,7 @@ cluster_rel(Oid tableOid, Oid indexOid, int options) !RelationIsPopulated(OldHeap)) { relation_close(OldHeap, AccessExclusiveLock); - pgstat_progress_end_command(); - return; + goto out; } /* @@ -404,6 +412,13 @@ cluster_rel(Oid tableOid, Oid indexOid, int options) /* NB: rebuild_relation does table_close() on OldHeap */ +out: + /* Roll back any GUC changes executed by index functions */ + AtEOXact_GUC(false, save_nestlevel); + + /* Restore userid and security context */ + SetUserIdAndSecContext(save_userid, save_sec_context); + pgstat_progress_end_command(); } |