Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3d9f865

Browse files
committed
Modify ALTER TABLE OWNER to change index ownership; code cleanup.
Neil Conway
1 parentade0fe5 commit3d9f865

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/0606:09:29 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.160 2002/03/0619: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
staticvoiddrop_default(Oidrelid,int16attnum);
5857
staticboolneeds_toast_table(Relationrel);
59-
58+
staticvoidAlterTableOwnerId(OidrelationOid,int32newOwnerSysId);
59+
staticvoidCheckTupleType(Form_pg_classtuple_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(constchar*relationName,constchar*newOwnerName)
15681567
{
1569-
Relationclass_rel;
1570-
HeapTupletuple;
1571-
int32newOwnerSysid;
1572-
Relationidescs[Num_pg_class_indices];
1568+
OidrelationOid;
1569+
Relationrelation;
1570+
int32newOwnerSysId;
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+
staticvoid
1589+
AlterTableOwnerId(OidrelationOid,int32newOwnerSysId)
1590+
{
1591+
Relationclass_rel;
1592+
HeapTupletuple;
1593+
Relationidescs[Num_pg_class_indices];
1594+
Form_pg_classtuple_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-
caseRELKIND_RELATION:
1600-
caseRELKIND_INDEX:
1601-
caseRELKIND_VIEW:
1602-
caseRELKIND_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+
Relationtarget_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+
staticvoid
1649+
CheckTupleType(Form_pg_classtuple_class)
1650+
{
1651+
switch (tuple_class->relkind)
1652+
{
1653+
caseRELKIND_RELATION:
1654+
caseRELKIND_INDEX:
1655+
caseRELKIND_VIEW:
1656+
caseRELKIND_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/0606:10:16 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.92 2002/03/0619: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
StringInfobuf);
142142
staticbooltleIsArrayAssign(TargetEntry*tle);
143143
staticchar*quote_identifier(char*ident);
144-
staticchar*get_relation_name(Oidrelid);
145144
staticchar*get_relid_attribute_name(Oidrelid,AttrNumberattnum);
146145

147146
#defineonly_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
returnresult;
26982697
}
26992698

2700-
/* ----------
2701-
* get_relation_name- Get a relation name by Oid
2702-
* ----------
2703-
*/
2704-
staticchar*
2705-
get_relation_name(Oidrelid)
2706-
{
2707-
HeapTupleclasstup;
2708-
Form_pg_classclassStruct;
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-
returnresult;
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)

[8]ページ先頭

©2009-2025 Movatter.jp