Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3d9f865

Browse files
committed
Modify ALTER TABLE OWNER to change index ownership; code cleanup.
Neil Conway
1 parent ade0fe5 commit 3d9f865

File tree

2 files changed

+76
-66
lines changed

2 files changed

+76
-66
lines changed

src/backend/commands/command.c

Lines changed: 74 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.159 2002/03/06 06:09:29 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.160 2002/03/06 19:58:26 momjian Exp $
1212
*
1313
* NOTES
1414
* The PerformAddAttribute() code, like most of the relation
@@ -53,10 +53,10 @@
5353
#include "utils/relcache.h"
5454
#include "utils/temprel.h"
5555

56-
5756
static void drop_default(Oid relid, int16 attnum);
5857
static bool needs_toast_table(Relation rel);
59-
58+
static void AlterTableOwnerId(Oid relationOid, int32 newOwnerSysId);
59+
static void CheckTupleType(Form_pg_class tuple_class);
6060

6161
/* --------------------------------
6262
* PortalCleanup
@@ -1559,59 +1559,58 @@ AlterTableDropConstraint(const char *relationName,
15591559
elog(NOTICE, "Multiple constraints dropped");
15601560
}
15611561

1562-
15631562
/*
15641563
* ALTER TABLE OWNER
15651564
*/
15661565
void
15671566
AlterTableOwner(const char *relationName, const char *newOwnerName)
15681567
{
1569-
Relation class_rel;
1570-
HeapTuple tuple;
1571-
int32 newOwnerSysid;
1572-
Relation idescs[Num_pg_class_indices];
1568+
Oid relationOid;
1569+
Relation relation;
1570+
int32 newOwnerSysId;
15731571

1574-
/*
1575-
* first check that we are a superuser
1576-
*/
1572+
/* check that we are the superuser */
15771573
if (!superuser())
15781574
elog(ERROR, "ALTER TABLE: permission denied");
15791575

1580-
/*
1581-
* look up the new owner in pg_shadow and get the sysid
1582-
*/
1583-
newOwnerSysid = get_usesysid(newOwnerName);
1576+
/* lookup the OID of the target relation */
1577+
relation = RelationNameGetRelation(relationName);
1578+
relationOid = relation->rd_id;
1579+
RelationClose(relation);
15841580

1585-
/*
1586-
* find the table's entry in pg_class and make a modifiable copy
1587-
*/
1588-
class_rel = heap_openr(RelationRelationName, RowExclusiveLock);
1581+
/* lookup the sysid of the new owner */
1582+
newOwnerSysId = get_usesysid(newOwnerName);
1583+
1584+
/* do all the actual work */
1585+
AlterTableOwnerId(relationOid, newOwnerSysId);
1586+
}
15891587

1590-
tuple = SearchSysCacheCopy(RELNAME,
1591-
PointerGetDatum(relationName),
1588+
static void
1589+
AlterTableOwnerId(Oid relationOid, int32 newOwnerSysId)
1590+
{
1591+
Relation class_rel;
1592+
HeapTuple tuple;
1593+
Relation idescs[Num_pg_class_indices];
1594+
Form_pg_class tuple_class;
1595+
1596+
tuple = SearchSysCacheCopy(RELOID,
1597+
ObjectIdGetDatum(relationOid),
15921598
0, 0, 0);
15931599
if (!HeapTupleIsValid(tuple))
1594-
elog(ERROR, "ALTER TABLE: relation \"%s\" not found",
1595-
relationName);
1600+
elog(ERROR, "ALTER TABLE: object ID %hd not found",
1601+
relationOid);
15961602

1597-
switch (((Form_pg_class) GETSTRUCT(tuple))->relkind)
1598-
{
1599-
case RELKIND_RELATION:
1600-
case RELKIND_INDEX:
1601-
case RELKIND_VIEW:
1602-
case RELKIND_SEQUENCE:
1603-
/* ok to change owner */
1604-
break;
1605-
default:
1606-
elog(ERROR, "ALTER TABLE: relation \"%s\" is not a table, index, view, or sequence",
1607-
relationName);
1608-
}
1603+
tuple_class = (Form_pg_class) GETSTRUCT(tuple);
1604+
1605+
/* Can we change the ownership of this tuple? */
1606+
CheckTupleType(tuple_class);
16091607

16101608
/*
1611-
* modify the table's entry and write to the heap
1609+
* Okay, this is a valid tuple: change its ownership and
1610+
* write to the heap.
16121611
*/
1613-
((Form_pg_class) GETSTRUCT(tuple))->relowner = newOwnerSysid;
1614-
1612+
class_rel = heap_openr(RelationRelationName, RowExclusiveLock);
1613+
tuple_class->relowner = newOwnerSysId;
16151614
simple_heap_update(class_rel, &tuple->t_self, tuple);
16161615

16171616
/* Keep the catalog indices up to date */
@@ -1620,12 +1619,48 @@ AlterTableOwner(const char *relationName, const char *newOwnerName)
16201619
CatalogCloseIndices(Num_pg_class_indices, idescs);
16211620

16221621
/*
1623-
* unlock everything and return
1622+
* If we are operating on a table, also change the ownership
1623+
* of all of its indexes.
16241624
*/
1625+
if (tuple_class->relkind == RELKIND_RELATION)
1626+
{
1627+
Relation target_rel;
1628+
List *index_oid_list, *i;
1629+
1630+
/* Find all the indexes belonging to this relation */
1631+
target_rel = heap_open(relationOid, RowExclusiveLock);
1632+
index_oid_list = RelationGetIndexList(target_rel);
1633+
heap_close(target_rel, RowExclusiveLock);
1634+
1635+
/* For each index, recursively change its ownership */
1636+
foreach (i, index_oid_list)
1637+
{
1638+
AlterTableOwnerId(lfirsti(i), newOwnerSysId);
1639+
}
1640+
1641+
freeList(index_oid_list);
1642+
}
1643+
16251644
heap_freetuple(tuple);
16261645
heap_close(class_rel, NoLock);
16271646
}
16281647

1648+
static void
1649+
CheckTupleType(Form_pg_class tuple_class)
1650+
{
1651+
switch (tuple_class->relkind)
1652+
{
1653+
case RELKIND_RELATION:
1654+
case RELKIND_INDEX:
1655+
case RELKIND_VIEW:
1656+
case RELKIND_SEQUENCE:
1657+
/* ok to change owner */
1658+
break;
1659+
default:
1660+
elog(ERROR, "ALTER TABLE: relation \"%s\" is not a table, index, view, or sequence",
1661+
NameStr(tuple_class->relname));
1662+
}
1663+
}
16291664

16301665
/*
16311666
* ALTER TABLE CREATE TOAST TABLE

src/backend/utils/adt/ruleutils.c

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* back to source text
44
*
55
* IDENTIFICATION
6-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.91 2002/03/06 06:10:16 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.92 2002/03/06 19:58:26 momjian Exp $
77
*
88
* This software is copyrighted by Jan Wieck - Hamburg.
99
*
@@ -141,7 +141,6 @@ static void get_opclass_name(Oid opclass, Oid actual_datatype,
141141
StringInfo buf);
142142
static bool tleIsArrayAssign(TargetEntry *tle);
143143
static char *quote_identifier(char *ident);
144-
static char *get_relation_name(Oid relid);
145144
static char *get_relid_attribute_name(Oid relid, AttrNumber attnum);
146145

147146
#define only_marker(rte) ((rte)->inh ? "" : "ONLY ")
@@ -752,7 +751,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc)
752751

753752
/* The relation the rule is fired on */
754753
appendStringInfo(buf, " TO %s",
755-
quote_identifier(get_relation_name(ev_class)));
754+
quote_identifier(get_rel_name(ev_class)));
756755
if (ev_attr > 0)
757756
appendStringInfo(buf, ".%s",
758757
quote_identifier(get_relid_attribute_name(ev_class,
@@ -2697,30 +2696,6 @@ quote_identifier(char *ident)
26972696
return result;
26982697
}
26992698

2700-
/* ----------
2701-
* get_relation_name - Get a relation name by Oid
2702-
* ----------
2703-
*/
2704-
static char *
2705-
get_relation_name(Oid relid)
2706-
{
2707-
HeapTuple classtup;
2708-
Form_pg_class classStruct;
2709-
char *result;
2710-
2711-
classtup = SearchSysCache(RELOID,
2712-
ObjectIdGetDatum(relid),
2713-
0, 0, 0);
2714-
if (!HeapTupleIsValid(classtup))
2715-
elog(ERROR, "cache lookup of relation %u failed", relid);
2716-
2717-
classStruct = (Form_pg_class) GETSTRUCT(classtup);
2718-
result = pstrdup(NameStr(classStruct->relname));
2719-
ReleaseSysCache(classtup);
2720-
return result;
2721-
}
2722-
2723-
27242699
/* ----------
27252700
* get_relid_attribute_name
27262701
* Get an attribute name by its relations Oid and its attnum

0 commit comments

Comments
 (0)