PostgreSQL Source Code  git master
alter.h File Reference
Include dependency graph for alter.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ObjectAddress ExecRenameStmt (RenameStmt *stmt)
 
ObjectAddress ExecAlterObjectDependsStmt (AlterObjectDependsStmt *stmt, ObjectAddress *refAddress)
 
ObjectAddress ExecAlterObjectSchemaStmt (AlterObjectSchemaStmt *stmt, ObjectAddress *oldSchemaAddr)
 
Oid AlterObjectNamespace_oid (Oid classId, Oid objid, Oid nspOid, ObjectAddresses *objsMoved)
 
ObjectAddress ExecAlterOwnerStmt (AlterOwnerStmt *stmt)
 
void AlterObjectOwner_internal (Relation catalog, Oid objectId, Oid new_ownerId)
 

Function Documentation

◆ AlterObjectNamespace_oid()

Oid AlterObjectNamespace_oid ( Oid  classId,
Oid  objid,
Oid  nspOid,
ObjectAddresses objsMoved 
)

Definition at line 564 of file alter.c.

References AccessExclusiveLock, AlterObjectNamespace_internal(), AlterTableNamespaceInternal(), AlterTypeNamespace_oid(), ObjectAddress::classId, getObjectClass(), InvalidOid, NoLock, ObjectAddress::objectId, ObjectAddress::objectSubId, OCLASS_AM, OCLASS_AMOP, OCLASS_AMPROC, OCLASS_CAST, OCLASS_CLASS, OCLASS_COLLATION, OCLASS_CONSTRAINT, OCLASS_CONVERSION, OCLASS_DATABASE, OCLASS_DEFACL, OCLASS_DEFAULT, OCLASS_EVENT_TRIGGER, OCLASS_EXTENSION, OCLASS_FDW, OCLASS_FOREIGN_SERVER, OCLASS_LANGUAGE, OCLASS_LARGEOBJECT, OCLASS_OPCLASS, OCLASS_OPERATOR, OCLASS_OPFAMILY, OCLASS_POLICY, OCLASS_PROC, OCLASS_PUBLICATION, OCLASS_PUBLICATION_REL, OCLASS_REWRITE, OCLASS_ROLE, OCLASS_SCHEMA, OCLASS_STATISTIC_EXT, OCLASS_SUBSCRIPTION, OCLASS_TBLSPACE, OCLASS_TRANSFORM, OCLASS_TRIGGER, OCLASS_TSCONFIG, OCLASS_TSDICT, OCLASS_TSPARSER, OCLASS_TSTEMPLATE, OCLASS_TYPE, OCLASS_USER_MAPPING, relation_close(), relation_open(), RelationGetNamespace, RowExclusiveLock, table_close(), and table_open().

Referenced by AlterExtensionNamespace().

566 {
567  Oid oldNspOid = InvalidOid;
568  ObjectAddress dep;
569 
570  dep.classId = classId;
571  dep.objectId = objid;
572  dep.objectSubId = 0;
573 
574  switch (getObjectClass(&dep))
575  {
576  case OCLASS_CLASS:
577  {
578  Relation rel;
579 
580  rel = relation_open(objid, AccessExclusiveLock);
581  oldNspOid = RelationGetNamespace(rel);
582 
583  AlterTableNamespaceInternal(rel, oldNspOid, nspOid, objsMoved);
584 
585  relation_close(rel, NoLock);
586  break;
587  }
588 
589  case OCLASS_TYPE:
590  oldNspOid = AlterTypeNamespace_oid(objid, nspOid, objsMoved);
591  break;
592 
593  case OCLASS_PROC:
594  case OCLASS_COLLATION:
595  case OCLASS_CONVERSION:
596  case OCLASS_OPERATOR:
597  case OCLASS_OPCLASS:
598  case OCLASS_OPFAMILY:
600  case OCLASS_TSPARSER:
601  case OCLASS_TSDICT:
602  case OCLASS_TSTEMPLATE:
603  case OCLASS_TSCONFIG:
604  {
605  Relation catalog;
606 
607  catalog = table_open(classId, RowExclusiveLock);
608 
609  oldNspOid = AlterObjectNamespace_internal(catalog, objid,
610  nspOid);
611 
612  table_close(catalog, RowExclusiveLock);
613  }
614  break;
615 
616  case OCLASS_CAST:
617  case OCLASS_CONSTRAINT:
618  case OCLASS_DEFAULT:
619  case OCLASS_LANGUAGE:
620  case OCLASS_LARGEOBJECT:
621  case OCLASS_AM:
622  case OCLASS_AMOP:
623  case OCLASS_AMPROC:
624  case OCLASS_REWRITE:
625  case OCLASS_TRIGGER:
626  case OCLASS_SCHEMA:
627  case OCLASS_ROLE:
628  case OCLASS_DATABASE:
629  case OCLASS_TBLSPACE:
630  case OCLASS_FDW:
632  case OCLASS_USER_MAPPING:
633  case OCLASS_DEFACL:
634  case OCLASS_EXTENSION:
636  case OCLASS_POLICY:
637  case OCLASS_PUBLICATION:
639  case OCLASS_SUBSCRIPTION:
640  case OCLASS_TRANSFORM:
641  /* ignore object types that don't have schema-qualified names */
642  break;
643 
644  /*
645  * There's intentionally no default: case here; we want the
646  * compiler to warn if a new OCLASS hasn't been handled above.
647  */
648  }
649 
650  return oldNspOid;
651 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
static Oid AlterObjectNamespace_internal(Relation rel, Oid objid, Oid nspOid)
Definition: alter.c:665
unsigned int Oid
Definition: postgres_ext.h:31
ObjectClass getObjectClass(const ObjectAddress *object)
Definition: dependency.c:2695
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
void AlterTableNamespaceInternal(Relation rel, Oid oldNspOid, Oid nspOid, ObjectAddresses *objsMoved)
Definition: tablecmds.c:14281
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
Oid AlterTypeNamespace_oid(Oid typeOid, Oid nspOid, ObjectAddresses *objsMoved)
Definition: typecmds.c:3541
#define InvalidOid
Definition: postgres_ext.h:36
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define AccessExclusiveLock
Definition: lockdefs.h:45
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define RelationGetNamespace(relation)
Definition: rel.h:457

◆ AlterObjectOwner_internal()

void AlterObjectOwner_internal ( Relation  catalog,
Oid  objectId,
Oid  new_ownerId 
)

Definition at line 910 of file alter.c.

References ACL_CREATE, aclcheck_error(), ACLCHECK_NOT_OWNER, ACLCHECK_OK, aclnewowner(), Assert, CatalogTupleUpdate(), changeDependencyOnOwner(), check_is_member_of_role(), DatumGetAclP, DatumGetName, DatumGetObjectId, elog, ERROR, get_catalog_object_by_oid(), get_namespace_name(), get_object_attnum_acl(), get_object_attnum_name(), get_object_attnum_namespace(), get_object_attnum_oid(), get_object_attnum_owner(), get_object_type(), GetUserId(), has_privs_of_role(), heap_getattr, heap_modify_tuple(), InvalidAttrNumber, InvalidOid, InvokeObjectPostAlterHook, NAMEDATALEN, NameStr, OBJECT_SCHEMA, ObjectIdGetDatum, OidIsValid, palloc0(), pfree(), pg_namespace_aclcheck(), PointerGetDatum, RelationGetDescr, RelationGetNumberOfAttributes, RelationGetRelationName, RelationGetRelid, snprintf, superuser(), HeapTupleData::t_self, and values.

Referenced by ExecAlterOwnerStmt(), and shdepReassignOwned().

911 {
912  Oid classId = RelationGetRelid(rel);
913  AttrNumber Anum_oid = get_object_attnum_oid(classId);
914  AttrNumber Anum_owner = get_object_attnum_owner(classId);
915  AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
916  AttrNumber Anum_acl = get_object_attnum_acl(classId);
917  AttrNumber Anum_name = get_object_attnum_name(classId);
918  HeapTuple oldtup;
919  Datum datum;
920  bool isnull;
921  Oid old_ownerId;
922  Oid namespaceId = InvalidOid;
923 
924  oldtup = get_catalog_object_by_oid(rel, Anum_oid, objectId);
925  if (oldtup == NULL)
926  elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
927  objectId, RelationGetRelationName(rel));
928 
929  datum = heap_getattr(oldtup, Anum_owner,
930  RelationGetDescr(rel), &isnull);
931  Assert(!isnull);
932  old_ownerId = DatumGetObjectId(datum);
933 
934  if (Anum_namespace != InvalidAttrNumber)
935  {
936  datum = heap_getattr(oldtup, Anum_namespace,
937  RelationGetDescr(rel), &isnull);
938  Assert(!isnull);
939  namespaceId = DatumGetObjectId(datum);
940  }
941 
942  if (old_ownerId != new_ownerId)
943  {
944  AttrNumber nattrs;
945  HeapTuple newtup;
946  Datum *values;
947  bool *nulls;
948  bool *replaces;
949 
950  /* Superusers can bypass permission checks */
951  if (!superuser())
952  {
953  ObjectType objtype = get_object_type(classId, objectId);
954 
955  /* must be owner */
956  if (!has_privs_of_role(GetUserId(), old_ownerId))
957  {
958  char *objname;
959  char namebuf[NAMEDATALEN];
960 
961  if (Anum_name != InvalidAttrNumber)
962  {
963  datum = heap_getattr(oldtup, Anum_name,
964  RelationGetDescr(rel), &isnull);
965  Assert(!isnull);
966  objname = NameStr(*DatumGetName(datum));
967  }
968  else
969  {
970  snprintf(namebuf, sizeof(namebuf), "%u", objectId);
971  objname = namebuf;
972  }
973  aclcheck_error(ACLCHECK_NOT_OWNER, objtype, objname);
974  }
975  /* Must be able to become new owner */
976  check_is_member_of_role(GetUserId(), new_ownerId);
977 
978  /* New owner must have CREATE privilege on namespace */
979  if (OidIsValid(namespaceId))
980  {
981  AclResult aclresult;
982 
983  aclresult = pg_namespace_aclcheck(namespaceId, new_ownerId,
984  ACL_CREATE);
985  if (aclresult != ACLCHECK_OK)
986  aclcheck_error(aclresult, OBJECT_SCHEMA,
987  get_namespace_name(namespaceId));
988  }
989  }
990 
991  /* Build a modified tuple */
992  nattrs = RelationGetNumberOfAttributes(rel);
993  values = palloc0(nattrs * sizeof(Datum));
994  nulls = palloc0(nattrs * sizeof(bool));
995  replaces = palloc0(nattrs * sizeof(bool));
996  values[Anum_owner - 1] = ObjectIdGetDatum(new_ownerId);
997  replaces[Anum_owner - 1] = true;
998 
999  /*
1000  * Determine the modified ACL for the new owner. This is only
1001  * necessary when the ACL is non-null.
1002  */
1003  if (Anum_acl != InvalidAttrNumber)
1004  {
1005  datum = heap_getattr(oldtup,
1006  Anum_acl, RelationGetDescr(rel), &isnull);
1007  if (!isnull)
1008  {
1009  Acl *newAcl;
1010 
1011  newAcl = aclnewowner(DatumGetAclP(datum),
1012  old_ownerId, new_ownerId);
1013  values[Anum_acl - 1] = PointerGetDatum(newAcl);
1014  replaces[Anum_acl - 1] = true;
1015  }
1016  }
1017 
1018  newtup = heap_modify_tuple(oldtup, RelationGetDescr(rel),
1019  values, nulls, replaces);
1020 
1021  /* Perform actual update */
1022  CatalogTupleUpdate(rel, &newtup->t_self, newtup);
1023 
1024  /* Update owner dependency reference */
1025  if (classId == LargeObjectMetadataRelationId)
1026  classId = LargeObjectRelationId;
1027  changeDependencyOnOwner(classId, objectId, new_ownerId);
1028 
1029  /* Release memory */
1030  pfree(values);
1031  pfree(nulls);
1032  pfree(replaces);
1033  }
1034 
1035  InvokeObjectPostAlterHook(classId, objectId, 0);
1036 }
AttrNumber get_object_attnum_oid(Oid class_id)
AttrNumber get_object_attnum_owner(Oid class_id)
#define RelationGetDescr(relation)
Definition: rel.h:442
Oid GetUserId(void)
Definition: miscinit.c:380
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:422
#define DatumGetAclP(X)
Definition: acl.h:120
#define PointerGetDatum(X)
Definition: postgres.h:556
#define DatumGetObjectId(X)
Definition: postgres.h:500
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4908
bool superuser(void)
Definition: superuser.c:47
AttrNumber get_object_attnum_namespace(Oid class_id)
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4693
#define NAMEDATALEN
#define DatumGetName(X)
Definition: postgres.h:585
void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
Definition: pg_shdepend.c:310
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3353
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ACL_CREATE
Definition: parsenodes.h:84
ItemPointerData t_self
Definition: htup.h:65
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3094
AttrNumber get_object_attnum_acl(Oid class_id)
#define RelationGetRelationName(relation)
Definition: rel.h:450
void check_is_member_of_role(Oid member, Oid role)
Definition: acl.c:4954
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:163
ObjectType
Definition: parsenodes.h:1669
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:762
AttrNumber get_object_attnum_name(Oid class_id)
void * palloc0(Size size)
Definition: mcxt.c:955
AclResult
Definition: acl.h:177
HeapTuple get_catalog_object_by_oid(Relation catalog, AttrNumber oidcol, Oid objectId)
uintptr_t Datum
Definition: postgres.h:367
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:732
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:224
ObjectType get_object_type(Oid class_id, Oid object_id)
#define InvalidAttrNumber
Definition: attnum.h:23
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
#define snprintf
Definition: port.h:192
Acl * aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId)
Definition: acl.c:1052
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:416

◆ ExecAlterObjectDependsStmt()

ObjectAddress ExecAlterObjectDependsStmt ( AlterObjectDependsStmt stmt,
ObjectAddress refAddress 
)

Definition at line 432 of file alter.c.

References AccessExclusiveLock, Assert, DEPENDENCY_AUTO_EXTENSION, AlterObjectDependsStmt::extname, get_object_address(), get_object_address_rv(), NoLock, AlterObjectDependsStmt::object, OBJECT_EXTENSION, AlterObjectDependsStmt::objectType, recordDependencyOn(), AlterObjectDependsStmt::relation, and table_close().

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

433 {
434  ObjectAddress address;
435  ObjectAddress refAddr;
436  Relation rel;
437 
438  address =
439  get_object_address_rv(stmt->objectType, stmt->relation, (List *) stmt->object,
440  &rel, AccessExclusiveLock, false);
441 
442  /*
443  * If a relation was involved, it would have been opened and locked. We
444  * don't need the relation here, but we'll retain the lock until commit.
445  */
446  if (rel)
447  table_close(rel, NoLock);
448 
449  refAddr = get_object_address(OBJECT_EXTENSION, (Node *) stmt->extname,
450  &rel, AccessExclusiveLock, false);
451  Assert(rel == NULL);
452  if (refAddress)
453  *refAddress = refAddr;
454 
455  recordDependencyOn(&address, &refAddr, DEPENDENCY_AUTO_EXTENSION);
456 
457  return address;
458 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
Definition: nodes.h:525
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:43
#define NoLock
Definition: lockdefs.h:34
ObjectAddress get_object_address_rv(ObjectType objtype, RangeVar *rel, List *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
#define Assert(condition)
Definition: c.h:732
ObjectAddress get_object_address(ObjectType objtype, Node *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
#define AccessExclusiveLock
Definition: lockdefs.h:45
Definition: pg_list.h:50

◆ ExecAlterObjectSchemaStmt()

ObjectAddress ExecAlterObjectSchemaStmt ( AlterObjectSchemaStmt stmt,
ObjectAddress oldSchemaAddr 
)

Definition at line 470 of file alter.c.

References AccessExclusiveLock, AlterExtensionNamespace(), AlterObjectNamespace_internal(), AlterTableNamespace(), AlterTypeNamespace(), Assert, castNode, ObjectAddress::classId, elog, ERROR, get_object_address(), InvalidObjectAddress, LookupCreationNamespace(), AlterObjectSchemaStmt::newschema, AlterObjectSchemaStmt::object, OBJECT_AGGREGATE, OBJECT_COLLATION, OBJECT_CONVERSION, OBJECT_DOMAIN, OBJECT_EXTENSION, OBJECT_FOREIGN_TABLE, OBJECT_FUNCTION, OBJECT_MATVIEW, OBJECT_OPCLASS, OBJECT_OPERATOR, OBJECT_OPFAMILY, OBJECT_PROCEDURE, OBJECT_ROUTINE, OBJECT_SEQUENCE, OBJECT_STATISTIC_EXT, OBJECT_TABLE, OBJECT_TSCONFIGURATION, OBJECT_TSDICTIONARY, OBJECT_TSPARSER, OBJECT_TSTEMPLATE, OBJECT_TYPE, OBJECT_VIEW, ObjectAddressSet, ObjectAddress::objectId, AlterObjectSchemaStmt::objectType, RowExclusiveLock, strVal, table_close(), and table_open().

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

472 {
473  ObjectAddress address;
474  Oid oldNspOid;
475 
476  switch (stmt->objectType)
477  {
478  case OBJECT_EXTENSION:
479  address = AlterExtensionNamespace(strVal((Value *) stmt->object), stmt->newschema,
480  oldSchemaAddr ? &oldNspOid : NULL);
481  break;
482 
484  case OBJECT_SEQUENCE:
485  case OBJECT_TABLE:
486  case OBJECT_VIEW:
487  case OBJECT_MATVIEW:
488  address = AlterTableNamespace(stmt,
489  oldSchemaAddr ? &oldNspOid : NULL);
490  break;
491 
492  case OBJECT_DOMAIN:
493  case OBJECT_TYPE:
494  address = AlterTypeNamespace(castNode(List, stmt->object), stmt->newschema,
495  stmt->objectType,
496  oldSchemaAddr ? &oldNspOid : NULL);
497  break;
498 
499  /* generic code path */
500  case OBJECT_AGGREGATE:
501  case OBJECT_COLLATION:
502  case OBJECT_CONVERSION:
503  case OBJECT_FUNCTION:
504  case OBJECT_OPERATOR:
505  case OBJECT_OPCLASS:
506  case OBJECT_OPFAMILY:
507  case OBJECT_PROCEDURE:
508  case OBJECT_ROUTINE:
511  case OBJECT_TSDICTIONARY:
512  case OBJECT_TSPARSER:
513  case OBJECT_TSTEMPLATE:
514  {
515  Relation catalog;
516  Relation relation;
517  Oid classId;
518  Oid nspOid;
519 
520  address = get_object_address(stmt->objectType,
521  stmt->object,
522  &relation,
524  false);
525  Assert(relation == NULL);
526  classId = address.classId;
527  catalog = table_open(classId, RowExclusiveLock);
528  nspOid = LookupCreationNamespace(stmt->newschema);
529 
530  oldNspOid = AlterObjectNamespace_internal(catalog, address.objectId,
531  nspOid);
532  table_close(catalog, RowExclusiveLock);
533  }
534  break;
535 
536  default:
537  elog(ERROR, "unrecognized AlterObjectSchemaStmt type: %d",
538  (int) stmt->objectType);
539  return InvalidObjectAddress; /* keep compiler happy */
540  }
541 
542  if (oldSchemaAddr)
543  ObjectAddressSet(*oldSchemaAddr, NamespaceRelationId, oldNspOid);
544 
545  return address;
546 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
Oid LookupCreationNamespace(const char *nspname)
Definition: namespace.c:2928
ObjectAddress AlterTypeNamespace(List *names, const char *newschema, ObjectType objecttype, Oid *oldschema)
Definition: typecmds.c:3504
#define strVal(v)
Definition: value.h:54
static Oid AlterObjectNamespace_internal(Relation rel, Oid objid, Oid nspOid)
Definition: alter.c:665
unsigned int Oid
Definition: postgres_ext.h:31
ObjectAddress AlterTableNamespace(AlterObjectSchemaStmt *stmt, Oid *oldschema)
Definition: tablecmds.c:14210
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define Assert(condition)
Definition: c.h:732
Definition: value.h:42
ObjectAddress get_object_address(ObjectType objtype, Node *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define AccessExclusiveLock
Definition: lockdefs.h:45
const ObjectAddress InvalidObjectAddress
#define elog(elevel,...)
Definition: elog.h:226
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
ObjectAddress AlterExtensionNamespace(const char *extensionName, const char *newschema, Oid *oldschema)
Definition: extension.c:2708

◆ ExecAlterOwnerStmt()

ObjectAddress ExecAlterOwnerStmt ( AlterOwnerStmt stmt)

Definition at line 809 of file alter.c.

References AccessExclusiveLock, AlterDatabaseOwner(), AlterEventTriggerOwner(), AlterForeignDataWrapperOwner(), AlterForeignServerOwner(), AlterObjectOwner_internal(), AlterPublicationOwner(), AlterSchemaOwner(), AlterSubscriptionOwner(), AlterTypeOwner(), Assert, castNode, ObjectAddress::classId, elog, ERROR, get_object_address(), get_rolespec_oid(), InvalidObjectAddress, AlterOwnerStmt::newowner, AlterOwnerStmt::object, OBJECT_AGGREGATE, OBJECT_COLLATION, OBJECT_CONVERSION, OBJECT_DATABASE, OBJECT_DOMAIN, OBJECT_EVENT_TRIGGER, OBJECT_FDW, OBJECT_FOREIGN_SERVER, OBJECT_FUNCTION, OBJECT_LANGUAGE, OBJECT_LARGEOBJECT, OBJECT_OPCLASS, OBJECT_OPERATOR, OBJECT_OPFAMILY, OBJECT_PROCEDURE, OBJECT_PUBLICATION, OBJECT_ROUTINE, OBJECT_SCHEMA, OBJECT_STATISTIC_EXT, OBJECT_SUBSCRIPTION, OBJECT_TABLESPACE, OBJECT_TSCONFIGURATION, OBJECT_TSDICTIONARY, OBJECT_TYPE, ObjectAddress::objectId, AlterOwnerStmt::objectType, RowExclusiveLock, strVal, table_close(), and table_open().

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

810 {
811  Oid newowner = get_rolespec_oid(stmt->newowner, false);
812 
813  switch (stmt->objectType)
814  {
815  case OBJECT_DATABASE:
816  return AlterDatabaseOwner(strVal((Value *) stmt->object), newowner);
817 
818  case OBJECT_SCHEMA:
819  return AlterSchemaOwner(strVal((Value *) stmt->object), newowner);
820 
821  case OBJECT_TYPE:
822  case OBJECT_DOMAIN: /* same as TYPE */
823  return AlterTypeOwner(castNode(List, stmt->object), newowner, stmt->objectType);
824  break;
825 
826  case OBJECT_FDW:
828  newowner);
829 
831  return AlterForeignServerOwner(strVal((Value *) stmt->object),
832  newowner);
833 
835  return AlterEventTriggerOwner(strVal((Value *) stmt->object),
836  newowner);
837 
838  case OBJECT_PUBLICATION:
839  return AlterPublicationOwner(strVal((Value *) stmt->object),
840  newowner);
841 
842  case OBJECT_SUBSCRIPTION:
843  return AlterSubscriptionOwner(strVal((Value *) stmt->object),
844  newowner);
845 
846  /* Generic cases */
847  case OBJECT_AGGREGATE:
848  case OBJECT_COLLATION:
849  case OBJECT_CONVERSION:
850  case OBJECT_FUNCTION:
851  case OBJECT_LANGUAGE:
852  case OBJECT_LARGEOBJECT:
853  case OBJECT_OPERATOR:
854  case OBJECT_OPCLASS:
855  case OBJECT_OPFAMILY:
856  case OBJECT_PROCEDURE:
857  case OBJECT_ROUTINE:
859  case OBJECT_TABLESPACE:
860  case OBJECT_TSDICTIONARY:
862  {
863  Relation catalog;
864  Relation relation;
865  Oid classId;
866  ObjectAddress address;
867 
868  address = get_object_address(stmt->objectType,
869  stmt->object,
870  &relation,
872  false);
873  Assert(relation == NULL);
874  classId = address.classId;
875 
876  /*
877  * XXX - get_object_address returns Oid of pg_largeobject
878  * catalog for OBJECT_LARGEOBJECT because of historical
879  * reasons. Fix up it here.
880  */
881  if (classId == LargeObjectRelationId)
882  classId = LargeObjectMetadataRelationId;
883 
884  catalog = table_open(classId, RowExclusiveLock);
885 
886  AlterObjectOwner_internal(catalog, address.objectId, newowner);
887  table_close(catalog, RowExclusiveLock);
888 
889  return address;
890  }
891  break;
892 
893  default:
894  elog(ERROR, "unrecognized AlterOwnerStmt type: %d",
895  (int) stmt->objectType);
896  return InvalidObjectAddress; /* keep compiler happy */
897  }
898 }
RoleSpec * newowner
Definition: parsenodes.h:2939
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
ObjectType objectType
Definition: parsenodes.h:2936
#define strVal(v)
Definition: value.h:54
unsigned int Oid
Definition: postgres_ext.h:31
ObjectAddress AlterEventTriggerOwner(const char *name, Oid newOwnerId)
ObjectAddress AlterTypeOwner(List *names, Oid newOwnerId, ObjectType objecttype)
Definition: typecmds.c:3300
#define ERROR
Definition: elog.h:43
ObjectAddress AlterForeignDataWrapperOwner(const char *name, Oid newOwnerId)
Definition: foreigncmds.c:275
void AlterObjectOwner_internal(Relation rel, Oid objectId, Oid new_ownerId)
Definition: alter.c:910
#define RowExclusiveLock
Definition: lockdefs.h:38
ObjectAddress AlterSubscriptionOwner(const char *name, Oid newOwnerId)
Oid get_rolespec_oid(const RoleSpec *role, bool missing_ok)
Definition: acl.c:5225
ObjectAddress AlterPublicationOwner(const char *name, Oid newOwnerId)
ObjectAddress AlterSchemaOwner(const char *name, Oid newOwnerId)
Definition: schemacmds.c:323
#define Assert(condition)
Definition: c.h:732
Definition: value.h:42
ObjectAddress AlterForeignServerOwner(const char *name, Oid newOwnerId)
Definition: foreigncmds.c:415
ObjectAddress get_object_address(ObjectType objtype, Node *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
ObjectAddress AlterDatabaseOwner(const char *dbname, Oid newOwnerId)
Definition: dbcommands.c:1643
#define AccessExclusiveLock
Definition: lockdefs.h:45
const ObjectAddress InvalidObjectAddress
#define elog(elevel,...)
Definition: elog.h:226
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50

◆ ExecRenameStmt()

ObjectAddress ExecRenameStmt ( RenameStmt stmt)

Definition at line 332 of file alter.c.

References AccessExclusiveLock, AlterObjectRename_internal(), Assert, ObjectAddress::classId, elog, ERROR, get_object_address(), InvalidObjectAddress, RenameStmt::newname, RenameStmt::object, OBJECT_AGGREGATE, OBJECT_ATTRIBUTE, OBJECT_COLLATION, OBJECT_COLUMN, OBJECT_CONVERSION, OBJECT_DATABASE, OBJECT_DOMAIN, OBJECT_DOMCONSTRAINT, OBJECT_EVENT_TRIGGER, OBJECT_FDW, OBJECT_FOREIGN_SERVER, OBJECT_FOREIGN_TABLE, OBJECT_FUNCTION, OBJECT_INDEX, OBJECT_LANGUAGE, OBJECT_MATVIEW, OBJECT_OPCLASS, OBJECT_OPFAMILY, OBJECT_POLICY, OBJECT_PROCEDURE, OBJECT_PUBLICATION, OBJECT_ROLE, OBJECT_ROUTINE, OBJECT_RULE, OBJECT_SCHEMA, OBJECT_SEQUENCE, OBJECT_STATISTIC_EXT, OBJECT_SUBSCRIPTION, OBJECT_TABCONSTRAINT, OBJECT_TABLE, OBJECT_TABLESPACE, OBJECT_TRIGGER, OBJECT_TSCONFIGURATION, OBJECT_TSDICTIONARY, OBJECT_TSPARSER, OBJECT_TSTEMPLATE, OBJECT_TYPE, OBJECT_VIEW, ObjectAddress::objectId, RenameStmt::relation, rename_policy(), renameatt(), RenameConstraint(), RenameDatabase(), RenameRelation(), RenameRewriteRule(), RenameRole(), RenameSchema(), RenameTableSpace(), renametrig(), RenameStmt::renameType, RenameType(), RowExclusiveLock, RenameStmt::subname, table_close(), and table_open().

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

333 {
334  switch (stmt->renameType)
335  {
338  return RenameConstraint(stmt);
339 
340  case OBJECT_DATABASE:
341  return RenameDatabase(stmt->subname, stmt->newname);
342 
343  case OBJECT_ROLE:
344  return RenameRole(stmt->subname, stmt->newname);
345 
346  case OBJECT_SCHEMA:
347  return RenameSchema(stmt->subname, stmt->newname);
348 
349  case OBJECT_TABLESPACE:
350  return RenameTableSpace(stmt->subname, stmt->newname);
351 
352  case OBJECT_TABLE:
353  case OBJECT_SEQUENCE:
354  case OBJECT_VIEW:
355  case OBJECT_MATVIEW:
356  case OBJECT_INDEX:
358  return RenameRelation(stmt);
359 
360  case OBJECT_COLUMN:
361  case OBJECT_ATTRIBUTE:
362  return renameatt(stmt);
363 
364  case OBJECT_RULE:
365  return RenameRewriteRule(stmt->relation, stmt->subname,
366  stmt->newname);
367 
368  case OBJECT_TRIGGER:
369  return renametrig(stmt);
370 
371  case OBJECT_POLICY:
372  return rename_policy(stmt);
373 
374  case OBJECT_DOMAIN:
375  case OBJECT_TYPE:
376  return RenameType(stmt);
377 
378  case OBJECT_AGGREGATE:
379  case OBJECT_COLLATION:
380  case OBJECT_CONVERSION:
382  case OBJECT_FDW:
384  case OBJECT_FUNCTION:
385  case OBJECT_OPCLASS:
386  case OBJECT_OPFAMILY:
387  case OBJECT_LANGUAGE:
388  case OBJECT_PROCEDURE:
389  case OBJECT_ROUTINE:
392  case OBJECT_TSDICTIONARY:
393  case OBJECT_TSPARSER:
394  case OBJECT_TSTEMPLATE:
395  case OBJECT_PUBLICATION:
396  case OBJECT_SUBSCRIPTION:
397  {
398  ObjectAddress address;
399  Relation catalog;
400  Relation relation;
401 
402  address = get_object_address(stmt->renameType,
403  stmt->object,
404  &relation,
405  AccessExclusiveLock, false);
406  Assert(relation == NULL);
407 
408  catalog = table_open(address.classId, RowExclusiveLock);
410  address.objectId,
411  stmt->newname);
412  table_close(catalog, RowExclusiveLock);
413 
414  return address;
415  }
416 
417  default:
418  elog(ERROR, "unrecognized rename stmt type: %d",
419  (int) stmt->renameType);
420  return InvalidObjectAddress; /* keep compiler happy */
421  }
422 }
ObjectAddress RenameSchema(const char *oldname, const char *newname)
Definition: schemacmds.c:242
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
ObjectAddress RenameType(RenameStmt *stmt)
Definition: typecmds.c:3222
char * subname
Definition: parsenodes.h:2895
ObjectType renameType
Definition: parsenodes.h:2891
ObjectAddress RenameRelation(RenameStmt *stmt)
Definition: tablecmds.c:3273
char * newname
Definition: parsenodes.h:2897
ObjectAddress RenameDatabase(const char *oldname, const char *newname)
Definition: dbcommands.c:1010
#define ERROR
Definition: elog.h:43
ObjectAddress rename_policy(RenameStmt *stmt)
Definition: policy.c:1218
#define RowExclusiveLock
Definition: lockdefs.h:38
Node * object
Definition: parsenodes.h:2894
ObjectAddress renametrig(RenameStmt *stmt)
Definition: trigger.c:1673
ObjectAddress renameatt(RenameStmt *stmt)
Definition: tablecmds.c:3078
ObjectAddress RenameConstraint(RenameStmt *stmt)
Definition: tablecmds.c:3222
RangeVar * relation
Definition: parsenodes.h:2893
ObjectAddress RenameRole(const char *oldname, const char *newname)
Definition: user.c:1161
#define Assert(condition)
Definition: c.h:732
ObjectAddress get_object_address(ObjectType objtype, Node *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
static void AlterObjectRename_internal(Relation rel, Oid objectId, const char *new_name)
Definition: alter.c:167
ObjectAddress RenameRewriteRule(RangeVar *relation, const char *oldName, const char *newName)
ObjectAddress RenameTableSpace(const char *oldname, const char *newname)
Definition: tablespace.c:926
#define AccessExclusiveLock
Definition: lockdefs.h:45
const ObjectAddress InvalidObjectAddress
#define elog(elevel,...)
Definition: elog.h:226
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39