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 574 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().

576 {
577  Oid oldNspOid = InvalidOid;
578  ObjectAddress dep;
579 
580  dep.classId = classId;
581  dep.objectId = objid;
582  dep.objectSubId = 0;
583 
584  switch (getObjectClass(&dep))
585  {
586  case OCLASS_CLASS:
587  {
588  Relation rel;
589 
590  rel = relation_open(objid, AccessExclusiveLock);
591  oldNspOid = RelationGetNamespace(rel);
592 
593  AlterTableNamespaceInternal(rel, oldNspOid, nspOid, objsMoved);
594 
595  relation_close(rel, NoLock);
596  break;
597  }
598 
599  case OCLASS_TYPE:
600  oldNspOid = AlterTypeNamespace_oid(objid, nspOid, objsMoved);
601  break;
602 
603  case OCLASS_PROC:
604  case OCLASS_COLLATION:
605  case OCLASS_CONVERSION:
606  case OCLASS_OPERATOR:
607  case OCLASS_OPCLASS:
608  case OCLASS_OPFAMILY:
610  case OCLASS_TSPARSER:
611  case OCLASS_TSDICT:
612  case OCLASS_TSTEMPLATE:
613  case OCLASS_TSCONFIG:
614  {
615  Relation catalog;
616 
617  catalog = table_open(classId, RowExclusiveLock);
618 
619  oldNspOid = AlterObjectNamespace_internal(catalog, objid,
620  nspOid);
621 
622  table_close(catalog, RowExclusiveLock);
623  }
624  break;
625 
626  case OCLASS_CAST:
627  case OCLASS_CONSTRAINT:
628  case OCLASS_DEFAULT:
629  case OCLASS_LANGUAGE:
630  case OCLASS_LARGEOBJECT:
631  case OCLASS_AM:
632  case OCLASS_AMOP:
633  case OCLASS_AMPROC:
634  case OCLASS_REWRITE:
635  case OCLASS_TRIGGER:
636  case OCLASS_SCHEMA:
637  case OCLASS_ROLE:
638  case OCLASS_DATABASE:
639  case OCLASS_TBLSPACE:
640  case OCLASS_FDW:
642  case OCLASS_USER_MAPPING:
643  case OCLASS_DEFACL:
644  case OCLASS_EXTENSION:
646  case OCLASS_POLICY:
647  case OCLASS_PUBLICATION:
649  case OCLASS_SUBSCRIPTION:
650  case OCLASS_TRANSFORM:
651  /* ignore object types that don't have schema-qualified names */
652  break;
653 
654  /*
655  * There's intentionally no default: case here; we want the
656  * compiler to warn if a new OCLASS hasn't been handled above.
657  */
658  }
659 
660  return oldNspOid;
661 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
static Oid AlterObjectNamespace_internal(Relation rel, Oid objid, Oid nspOid)
Definition: alter.c:675
unsigned int Oid
Definition: postgres_ext.h:31
ObjectClass getObjectClass(const ObjectAddress *object)
Definition: dependency.c:2724
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
void AlterTableNamespaceInternal(Relation rel, Oid oldNspOid, Oid nspOid, ObjectAddresses *objsMoved)
Definition: tablecmds.c:14807
#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:469

◆ AlterObjectOwner_internal()

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

Definition at line 919 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().

920 {
921  Oid classId = RelationGetRelid(rel);
922  AttrNumber Anum_oid = get_object_attnum_oid(classId);
923  AttrNumber Anum_owner = get_object_attnum_owner(classId);
924  AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
925  AttrNumber Anum_acl = get_object_attnum_acl(classId);
926  AttrNumber Anum_name = get_object_attnum_name(classId);
927  HeapTuple oldtup;
928  Datum datum;
929  bool isnull;
930  Oid old_ownerId;
931  Oid namespaceId = InvalidOid;
932 
933  oldtup = get_catalog_object_by_oid(rel, Anum_oid, objectId);
934  if (oldtup == NULL)
935  elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
936  objectId, RelationGetRelationName(rel));
937 
938  datum = heap_getattr(oldtup, Anum_owner,
939  RelationGetDescr(rel), &isnull);
940  Assert(!isnull);
941  old_ownerId = DatumGetObjectId(datum);
942 
943  if (Anum_namespace != InvalidAttrNumber)
944  {
945  datum = heap_getattr(oldtup, Anum_namespace,
946  RelationGetDescr(rel), &isnull);
947  Assert(!isnull);
948  namespaceId = DatumGetObjectId(datum);
949  }
950 
951  if (old_ownerId != new_ownerId)
952  {
953  AttrNumber nattrs;
954  HeapTuple newtup;
955  Datum *values;
956  bool *nulls;
957  bool *replaces;
958 
959  /* Superusers can bypass permission checks */
960  if (!superuser())
961  {
962  /* must be owner */
963  if (!has_privs_of_role(GetUserId(), old_ownerId))
964  {
965  char *objname;
966  char namebuf[NAMEDATALEN];
967 
968  if (Anum_name != InvalidAttrNumber)
969  {
970  datum = heap_getattr(oldtup, Anum_name,
971  RelationGetDescr(rel), &isnull);
972  Assert(!isnull);
973  objname = NameStr(*DatumGetName(datum));
974  }
975  else
976  {
977  snprintf(namebuf, sizeof(namebuf), "%u", objectId);
978  objname = namebuf;
979  }
981  objname);
982  }
983  /* Must be able to become new owner */
984  check_is_member_of_role(GetUserId(), new_ownerId);
985 
986  /* New owner must have CREATE privilege on namespace */
987  if (OidIsValid(namespaceId))
988  {
989  AclResult aclresult;
990 
991  aclresult = pg_namespace_aclcheck(namespaceId, new_ownerId,
992  ACL_CREATE);
993  if (aclresult != ACLCHECK_OK)
994  aclcheck_error(aclresult, OBJECT_SCHEMA,
995  get_namespace_name(namespaceId));
996  }
997  }
998 
999  /* Build a modified tuple */
1000  nattrs = RelationGetNumberOfAttributes(rel);
1001  values = palloc0(nattrs * sizeof(Datum));
1002  nulls = palloc0(nattrs * sizeof(bool));
1003  replaces = palloc0(nattrs * sizeof(bool));
1004  values[Anum_owner - 1] = ObjectIdGetDatum(new_ownerId);
1005  replaces[Anum_owner - 1] = true;
1006 
1007  /*
1008  * Determine the modified ACL for the new owner. This is only
1009  * necessary when the ACL is non-null.
1010  */
1011  if (Anum_acl != InvalidAttrNumber)
1012  {
1013  datum = heap_getattr(oldtup,
1014  Anum_acl, RelationGetDescr(rel), &isnull);
1015  if (!isnull)
1016  {
1017  Acl *newAcl;
1018 
1019  newAcl = aclnewowner(DatumGetAclP(datum),
1020  old_ownerId, new_ownerId);
1021  values[Anum_acl - 1] = PointerGetDatum(newAcl);
1022  replaces[Anum_acl - 1] = true;
1023  }
1024  }
1025 
1026  newtup = heap_modify_tuple(oldtup, RelationGetDescr(rel),
1027  values, nulls, replaces);
1028 
1029  /* Perform actual update */
1030  CatalogTupleUpdate(rel, &newtup->t_self, newtup);
1031 
1032  /* Update owner dependency reference */
1033  if (classId == LargeObjectMetadataRelationId)
1034  classId = LargeObjectRelationId;
1035  changeDependencyOnOwner(classId, objectId, new_ownerId);
1036 
1037  /* Release memory */
1038  pfree(values);
1039  pfree(nulls);
1040  pfree(replaces);
1041  }
1042 
1043  InvokeObjectPostAlterHook(classId, objectId, 0);
1044 }
AttrNumber get_object_attnum_oid(Oid class_id)
AttrNumber get_object_attnum_owner(Oid class_id)
#define RelationGetDescr(relation)
Definition: rel.h:454
Oid GetUserId(void)
Definition: miscinit.c:378
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:434
#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:4900
bool superuser(void)
Definition: superuser.c:46
AttrNumber get_object_attnum_namespace(Oid class_id)
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:644
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4691
#define NAMEDATALEN
#define DatumGetName(X)
Definition: postgres.h:585
void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
Definition: pg_shdepend.c:309
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3352
void pfree(void *pointer)
Definition: mcxt.c:1056
#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:462
void check_is_member_of_role(Oid member, Oid role)
Definition: acl.c:4946
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:175
#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:980
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:738
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:228
#define NameStr(name)
Definition: c.h:615
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:428

◆ ExecAlterObjectDependsStmt()

ObjectAddress ExecAlterObjectDependsStmt ( AlterObjectDependsStmt stmt,
ObjectAddress refAddress 
)

Definition at line 431 of file alter.c.

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

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

432 {
433  ObjectAddress address;
434  ObjectAddress refAddr;
435  Relation rel;
436 
437  address =
438  get_object_address_rv(stmt->objectType, stmt->relation, (List *) stmt->object,
439  &rel, AccessExclusiveLock, false);
440 
441  /*
442  * Verify that the user is entitled to run the command.
443  *
444  * We don't check any privileges on the extension, because that's not
445  * needed. The object owner is stipulating, by running this command, that
446  * the extension owner can drop the object whenever they feel like it,
447  * which is not considered a problem.
448  */
450  stmt->objectType, address, stmt->object, rel);
451 
452  /*
453  * If a relation was involved, it would have been opened and locked. We
454  * don't need the relation here, but we'll retain the lock until commit.
455  */
456  if (rel)
457  table_close(rel, NoLock);
458 
459  refAddr = get_object_address(OBJECT_EXTENSION, (Node *) stmt->extname,
460  &rel, AccessExclusiveLock, false);
461  Assert(rel == NULL);
462  if (refAddress)
463  *refAddress = refAddr;
464 
465  recordDependencyOn(&address, &refAddr, DEPENDENCY_AUTO_EXTENSION);
466 
467  return address;
468 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
Oid GetUserId(void)
Definition: miscinit.c:378
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:738
ObjectAddress get_object_address(ObjectType objtype, Node *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
void check_object_ownership(Oid roleid, ObjectType objtype, ObjectAddress address, Node *object, Relation relation)
#define AccessExclusiveLock
Definition: lockdefs.h:45
Definition: pg_list.h:50

◆ ExecAlterObjectSchemaStmt()

ObjectAddress ExecAlterObjectSchemaStmt ( AlterObjectSchemaStmt stmt,
ObjectAddress oldSchemaAddr 
)

Definition at line 480 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().

482 {
483  ObjectAddress address;
484  Oid oldNspOid;
485 
486  switch (stmt->objectType)
487  {
488  case OBJECT_EXTENSION:
489  address = AlterExtensionNamespace(strVal((Value *) stmt->object), stmt->newschema,
490  oldSchemaAddr ? &oldNspOid : NULL);
491  break;
492 
494  case OBJECT_SEQUENCE:
495  case OBJECT_TABLE:
496  case OBJECT_VIEW:
497  case OBJECT_MATVIEW:
498  address = AlterTableNamespace(stmt,
499  oldSchemaAddr ? &oldNspOid : NULL);
500  break;
501 
502  case OBJECT_DOMAIN:
503  case OBJECT_TYPE:
504  address = AlterTypeNamespace(castNode(List, stmt->object), stmt->newschema,
505  stmt->objectType,
506  oldSchemaAddr ? &oldNspOid : NULL);
507  break;
508 
509  /* generic code path */
510  case OBJECT_AGGREGATE:
511  case OBJECT_COLLATION:
512  case OBJECT_CONVERSION:
513  case OBJECT_FUNCTION:
514  case OBJECT_OPERATOR:
515  case OBJECT_OPCLASS:
516  case OBJECT_OPFAMILY:
517  case OBJECT_PROCEDURE:
518  case OBJECT_ROUTINE:
521  case OBJECT_TSDICTIONARY:
522  case OBJECT_TSPARSER:
523  case OBJECT_TSTEMPLATE:
524  {
525  Relation catalog;
526  Relation relation;
527  Oid classId;
528  Oid nspOid;
529 
530  address = get_object_address(stmt->objectType,
531  stmt->object,
532  &relation,
534  false);
535  Assert(relation == NULL);
536  classId = address.classId;
537  catalog = table_open(classId, RowExclusiveLock);
538  nspOid = LookupCreationNamespace(stmt->newschema);
539 
540  oldNspOid = AlterObjectNamespace_internal(catalog, address.objectId,
541  nspOid);
542  table_close(catalog, RowExclusiveLock);
543  }
544  break;
545 
546  default:
547  elog(ERROR, "unrecognized AlterObjectSchemaStmt type: %d",
548  (int) stmt->objectType);
549  return InvalidObjectAddress; /* keep compiler happy */
550  }
551 
552  if (oldSchemaAddr)
553  ObjectAddressSet(*oldSchemaAddr, NamespaceRelationId, oldNspOid);
554 
555  return address;
556 }
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:675
unsigned int Oid
Definition: postgres_ext.h:31
ObjectAddress AlterTableNamespace(AlterObjectSchemaStmt *stmt, Oid *oldschema)
Definition: tablecmds.c:14736
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define Assert(condition)
Definition: c.h:738
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:228
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:2774

◆ ExecAlterOwnerStmt()

ObjectAddress ExecAlterOwnerStmt ( AlterOwnerStmt stmt)

Definition at line 818 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().

819 {
820  Oid newowner = get_rolespec_oid(stmt->newowner, false);
821 
822  switch (stmt->objectType)
823  {
824  case OBJECT_DATABASE:
825  return AlterDatabaseOwner(strVal((Value *) stmt->object), newowner);
826 
827  case OBJECT_SCHEMA:
828  return AlterSchemaOwner(strVal((Value *) stmt->object), newowner);
829 
830  case OBJECT_TYPE:
831  case OBJECT_DOMAIN: /* same as TYPE */
832  return AlterTypeOwner(castNode(List, stmt->object), newowner, stmt->objectType);
833  break;
834 
835  case OBJECT_FDW:
837  newowner);
838 
840  return AlterForeignServerOwner(strVal((Value *) stmt->object),
841  newowner);
842 
844  return AlterEventTriggerOwner(strVal((Value *) stmt->object),
845  newowner);
846 
847  case OBJECT_PUBLICATION:
848  return AlterPublicationOwner(strVal((Value *) stmt->object),
849  newowner);
850 
851  case OBJECT_SUBSCRIPTION:
852  return AlterSubscriptionOwner(strVal((Value *) stmt->object),
853  newowner);
854 
855  /* Generic cases */
856  case OBJECT_AGGREGATE:
857  case OBJECT_COLLATION:
858  case OBJECT_CONVERSION:
859  case OBJECT_FUNCTION:
860  case OBJECT_LANGUAGE:
861  case OBJECT_LARGEOBJECT:
862  case OBJECT_OPERATOR:
863  case OBJECT_OPCLASS:
864  case OBJECT_OPFAMILY:
865  case OBJECT_PROCEDURE:
866  case OBJECT_ROUTINE:
868  case OBJECT_TABLESPACE:
869  case OBJECT_TSDICTIONARY:
871  {
872  Relation catalog;
873  Relation relation;
874  Oid classId;
875  ObjectAddress address;
876 
877  address = get_object_address(stmt->objectType,
878  stmt->object,
879  &relation,
881  false);
882  Assert(relation == NULL);
883  classId = address.classId;
884 
885  /*
886  * XXX - get_object_address returns Oid of pg_largeobject
887  * catalog for OBJECT_LARGEOBJECT because of historical
888  * reasons. Fix up it here.
889  */
890  if (classId == LargeObjectRelationId)
891  classId = LargeObjectMetadataRelationId;
892 
893  catalog = table_open(classId, RowExclusiveLock);
894 
895  AlterObjectOwner_internal(catalog, address.objectId, newowner);
896  table_close(catalog, RowExclusiveLock);
897 
898  return address;
899  }
900  break;
901 
902  default:
903  elog(ERROR, "unrecognized AlterOwnerStmt type: %d",
904  (int) stmt->objectType);
905  return InvalidObjectAddress; /* keep compiler happy */
906  }
907 }
RoleSpec * newowner
Definition: parsenodes.h:2959
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
ObjectType objectType
Definition: parsenodes.h:2956
#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:919
#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:5217
ObjectAddress AlterPublicationOwner(const char *name, Oid newOwnerId)
ObjectAddress AlterSchemaOwner(const char *name, Oid newOwnerId)
Definition: schemacmds.c:322
#define Assert(condition)
Definition: c.h:738
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:1676
#define AccessExclusiveLock
Definition: lockdefs.h:45
const ObjectAddress InvalidObjectAddress
#define elog(elevel,...)
Definition: elog.h:228
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50

◆ ExecRenameStmt()

ObjectAddress ExecRenameStmt ( RenameStmt stmt)

Definition at line 331 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().

332 {
333  switch (stmt->renameType)
334  {
337  return RenameConstraint(stmt);
338 
339  case OBJECT_DATABASE:
340  return RenameDatabase(stmt->subname, stmt->newname);
341 
342  case OBJECT_ROLE:
343  return RenameRole(stmt->subname, stmt->newname);
344 
345  case OBJECT_SCHEMA:
346  return RenameSchema(stmt->subname, stmt->newname);
347 
348  case OBJECT_TABLESPACE:
349  return RenameTableSpace(stmt->subname, stmt->newname);
350 
351  case OBJECT_TABLE:
352  case OBJECT_SEQUENCE:
353  case OBJECT_VIEW:
354  case OBJECT_MATVIEW:
355  case OBJECT_INDEX:
357  return RenameRelation(stmt);
358 
359  case OBJECT_COLUMN:
360  case OBJECT_ATTRIBUTE:
361  return renameatt(stmt);
362 
363  case OBJECT_RULE:
364  return RenameRewriteRule(stmt->relation, stmt->subname,
365  stmt->newname);
366 
367  case OBJECT_TRIGGER:
368  return renametrig(stmt);
369 
370  case OBJECT_POLICY:
371  return rename_policy(stmt);
372 
373  case OBJECT_DOMAIN:
374  case OBJECT_TYPE:
375  return RenameType(stmt);
376 
377  case OBJECT_AGGREGATE:
378  case OBJECT_COLLATION:
379  case OBJECT_CONVERSION:
381  case OBJECT_FDW:
383  case OBJECT_FUNCTION:
384  case OBJECT_OPCLASS:
385  case OBJECT_OPFAMILY:
386  case OBJECT_LANGUAGE:
387  case OBJECT_PROCEDURE:
388  case OBJECT_ROUTINE:
391  case OBJECT_TSDICTIONARY:
392  case OBJECT_TSPARSER:
393  case OBJECT_TSTEMPLATE:
394  case OBJECT_PUBLICATION:
395  case OBJECT_SUBSCRIPTION:
396  {
397  ObjectAddress address;
398  Relation catalog;
399  Relation relation;
400 
401  address = get_object_address(stmt->renameType,
402  stmt->object,
403  &relation,
404  AccessExclusiveLock, false);
405  Assert(relation == NULL);
406 
407  catalog = table_open(address.classId, RowExclusiveLock);
409  address.objectId,
410  stmt->newname);
411  table_close(catalog, RowExclusiveLock);
412 
413  return address;
414  }
415 
416  default:
417  elog(ERROR, "unrecognized rename stmt type: %d",
418  (int) stmt->renameType);
419  return InvalidObjectAddress; /* keep compiler happy */
420  }
421 }
ObjectAddress RenameSchema(const char *oldname, const char *newname)
Definition: schemacmds.c:241
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
ObjectAddress RenameType(RenameStmt *stmt)
Definition: typecmds.c:3222
char * subname
Definition: parsenodes.h:2915
ObjectType renameType
Definition: parsenodes.h:2911
ObjectAddress RenameRelation(RenameStmt *stmt)
Definition: tablecmds.c:3336
char * newname
Definition: parsenodes.h:2917
ObjectAddress RenameDatabase(const char *oldname, const char *newname)
Definition: dbcommands.c:1018
#define ERROR
Definition: elog.h:43
ObjectAddress rename_policy(RenameStmt *stmt)
Definition: policy.c:1219
#define RowExclusiveLock
Definition: lockdefs.h:38
Node * object
Definition: parsenodes.h:2914
ObjectAddress renametrig(RenameStmt *stmt)
Definition: trigger.c:1666
ObjectAddress renameatt(RenameStmt *stmt)
Definition: tablecmds.c:3141
ObjectAddress RenameConstraint(RenameStmt *stmt)
Definition: tablecmds.c:3285
RangeVar * relation
Definition: parsenodes.h:2913
ObjectAddress RenameRole(const char *oldname, const char *newname)
Definition: user.c:1161
#define Assert(condition)
Definition: c.h:738
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:166
ObjectAddress RenameRewriteRule(RangeVar *relation, const char *oldName, const char *newName)
ObjectAddress RenameTableSpace(const char *oldname, const char *newname)
Definition: tablespace.c:925
#define AccessExclusiveLock
Definition: lockdefs.h:45
const ObjectAddress InvalidObjectAddress
#define elog(elevel,...)
Definition: elog.h:228
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39