PostgreSQL Source Code  git master
alter.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/relation.h"
#include "access/sysattr.h"
#include "access/table.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/namespace.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_conversion.h"
#include "catalog/pg_event_trigger.h"
#include "catalog/pg_foreign_data_wrapper.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_language.h"
#include "catalog/pg_largeobject.h"
#include "catalog/pg_largeobject_metadata.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_opfamily.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_statistic_ext.h"
#include "catalog/pg_subscription.h"
#include "catalog/pg_ts_config.h"
#include "catalog/pg_ts_dict.h"
#include "catalog/pg_ts_parser.h"
#include "catalog/pg_ts_template.h"
#include "commands/alter.h"
#include "commands/collationcmds.h"
#include "commands/conversioncmds.h"
#include "commands/dbcommands.h"
#include "commands/defrem.h"
#include "commands/event_trigger.h"
#include "commands/extension.h"
#include "commands/policy.h"
#include "commands/proclang.h"
#include "commands/publicationcmds.h"
#include "commands/schemacmds.h"
#include "commands/subscriptioncmds.h"
#include "commands/tablecmds.h"
#include "commands/tablespace.h"
#include "commands/trigger.h"
#include "commands/typecmds.h"
#include "commands/user.h"
#include "miscadmin.h"
#include "parser/parse_func.h"
#include "rewrite/rewriteDefine.h"
#include "tcop/utility.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for alter.c:

Go to the source code of this file.

Functions

static Oid AlterObjectNamespace_internal (Relation rel, Oid objid, Oid nspOid)
 
static void report_name_conflict (Oid classId, const char *name)
 
static void report_namespace_conflict (Oid classId, const char *name, Oid nspOid)
 
static void AlterObjectRename_internal (Relation rel, Oid objectId, const char *new_name)
 
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 rel, Oid objectId, Oid new_ownerId)
 

Function Documentation

◆ AlterObjectNamespace_internal()

static Oid AlterObjectNamespace_internal ( Relation  rel,
Oid  objid,
Oid  nspOid 
)
static

Definition at line 691 of file alter.c.

References ACL_CREATE, aclcheck_error(), ACLCHECK_NOT_OWNER, ACLCHECK_OK, Assert, CatalogTupleUpdate(), changeDependencyFor(), CheckSetNamespace(), DatumGetName, DatumGetObjectId, elog, ereport, errcode(), errmsg(), ERROR, get_namespace_name(), get_object_attnum_name(), get_object_attnum_namespace(), get_object_attnum_owner(), get_object_catcache_name(), get_object_catcache_oid(), get_object_type(), getObjectDescriptionOids(), GETSTRUCT, GetUserId(), has_privs_of_role(), heap_getattr, heap_modify_tuple(), HeapTupleIsValid, InvokeObjectPostAlterHook, IsThereCollationInNamespace(), IsThereFunctionInNamespace(), IsThereOpClassInNamespace(), IsThereOpFamilyInNamespace(), name, NameStr, OBJECT_SCHEMA, ObjectIdGetDatum, palloc0(), pfree(), pg_namespace_aclcheck(), RelationGetDescr, RelationGetNumberOfAttributes, RelationGetRelationName, RelationGetRelid, report_namespace_conflict(), SearchSysCacheCopy1, SearchSysCacheExists2, superuser(), and values.

Referenced by AlterObjectNamespace_oid(), and ExecAlterObjectSchemaStmt().

692 {
693  Oid classId = RelationGetRelid(rel);
694  int oidCacheId = get_object_catcache_oid(classId);
695  int nameCacheId = get_object_catcache_name(classId);
696  AttrNumber Anum_name = get_object_attnum_name(classId);
697  AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
698  AttrNumber Anum_owner = get_object_attnum_owner(classId);
699  Oid oldNspOid;
700  Datum name,
701  namespace;
702  bool isnull;
703  HeapTuple tup,
704  newtup;
705  Datum *values;
706  bool *nulls;
707  bool *replaces;
708 
709  tup = SearchSysCacheCopy1(oidCacheId, ObjectIdGetDatum(objid));
710  if (!HeapTupleIsValid(tup)) /* should not happen */
711  elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
712  objid, RelationGetRelationName(rel));
713 
714  name = heap_getattr(tup, Anum_name, RelationGetDescr(rel), &isnull);
715  Assert(!isnull);
716  namespace = heap_getattr(tup, Anum_namespace, RelationGetDescr(rel),
717  &isnull);
718  Assert(!isnull);
719  oldNspOid = DatumGetObjectId(namespace);
720 
721  /*
722  * If the object is already in the correct namespace, we don't need to do
723  * anything except fire the object access hook.
724  */
725  if (oldNspOid == nspOid)
726  {
727  InvokeObjectPostAlterHook(classId, objid, 0);
728  return oldNspOid;
729  }
730 
731  /* Check basic namespace related issues */
732  CheckSetNamespace(oldNspOid, nspOid);
733 
734  /* Permission checks ... superusers can always do it */
735  if (!superuser())
736  {
737  Datum owner;
738  Oid ownerId;
739  AclResult aclresult;
740 
741  /* Fail if object does not have an explicit owner */
742  if (Anum_owner <= 0)
743  ereport(ERROR,
744  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
745  errmsg("must be superuser to set schema of %s",
746  getObjectDescriptionOids(classId, objid))));
747 
748  /* Otherwise, must be owner of the existing object */
749  owner = heap_getattr(tup, Anum_owner, RelationGetDescr(rel), &isnull);
750  Assert(!isnull);
751  ownerId = DatumGetObjectId(owner);
752 
753  if (!has_privs_of_role(GetUserId(), ownerId))
755  NameStr(*(DatumGetName(name))));
756 
757  /* User must have CREATE privilege on new namespace */
758  aclresult = pg_namespace_aclcheck(nspOid, GetUserId(), ACL_CREATE);
759  if (aclresult != ACLCHECK_OK)
760  aclcheck_error(aclresult, OBJECT_SCHEMA,
761  get_namespace_name(nspOid));
762  }
763 
764  /*
765  * Check for duplicate name (more friendly than unique-index failure).
766  * Since this is just a friendliness check, we can just skip it in cases
767  * where there isn't suitable support.
768  */
769  if (classId == ProcedureRelationId)
770  {
771  Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(tup);
772 
773  IsThereFunctionInNamespace(NameStr(proc->proname), proc->pronargs,
774  &proc->proargtypes, nspOid);
775  }
776  else if (classId == CollationRelationId)
777  {
779 
780  IsThereCollationInNamespace(NameStr(coll->collname), nspOid);
781  }
782  else if (classId == OperatorClassRelationId)
783  {
785 
786  IsThereOpClassInNamespace(NameStr(opc->opcname),
787  opc->opcmethod, nspOid);
788  }
789  else if (classId == OperatorFamilyRelationId)
790  {
792 
793  IsThereOpFamilyInNamespace(NameStr(opf->opfname),
794  opf->opfmethod, nspOid);
795  }
796  else if (nameCacheId >= 0 &&
797  SearchSysCacheExists2(nameCacheId, name,
798  ObjectIdGetDatum(nspOid)))
800  NameStr(*(DatumGetName(name))),
801  nspOid);
802 
803  /* Build modified tuple */
804  values = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(Datum));
805  nulls = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
806  replaces = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
807  values[Anum_namespace - 1] = ObjectIdGetDatum(nspOid);
808  replaces[Anum_namespace - 1] = true;
809  newtup = heap_modify_tuple(tup, RelationGetDescr(rel),
810  values, nulls, replaces);
811 
812  /* Perform actual update */
813  CatalogTupleUpdate(rel, &tup->t_self, newtup);
814 
815  /* Release memory */
816  pfree(values);
817  pfree(nulls);
818  pfree(replaces);
819 
820  /* update dependencies to point to the new schema */
821  changeDependencyFor(classId, objid,
822  NamespaceRelationId, oldNspOid, nspOid);
823 
824  InvokeObjectPostAlterHook(classId, objid, 0);
825 
826  return oldNspOid;
827 }
static void report_namespace_conflict(Oid classId, const char *name, Oid nspOid)
Definition: alter.c:112
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
AttrNumber get_object_attnum_owner(Oid class_id)
#define RelationGetDescr(relation)
Definition: rel.h:503
Oid GetUserId(void)
Definition: miscinit.c:495
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:483
#define DatumGetObjectId(X)
Definition: postgres.h:544
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4843
int get_object_catcache_name(Oid class_id)
int errcode(int sqlerrcode)
Definition: elog.c:698
bool superuser(void)
Definition: superuser.c:46
AttrNumber get_object_attnum_namespace(Oid class_id)
unsigned int Oid
Definition: postgres_ext.h:31
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4758
void IsThereOpClassInNamespace(const char *opcname, Oid opcmethod, Oid opcnamespace)
Definition: opclasscmds.c:1699
#define DatumGetName(X)
Definition: postgres.h:629
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3308
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_CREATE
Definition: parsenodes.h:92
int get_object_catcache_oid(Oid class_id)
ItemPointerData t_self
Definition: htup.h:65
char * getObjectDescriptionOids(Oid classid, Oid objid)
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
#define RelationGetRelationName(relation)
Definition: rel.h:511
void IsThereCollationInNamespace(const char *collname, Oid nspOid)
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:175
void CheckSetNamespace(Oid oldNspOid, Oid nspOid)
Definition: namespace.c:3012
void IsThereFunctionInNamespace(const char *proname, int pronargs, oidvector *proargtypes, Oid nspOid)
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
FormData_pg_opfamily * Form_pg_opfamily
Definition: pg_opfamily.h:51
AttrNumber get_object_attnum_name(Oid class_id)
void * palloc0(Size size)
Definition: mcxt.c:1093
AclResult
Definition: acl.h:177
uintptr_t Datum
Definition: postgres.h:411
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
ObjectType get_object_type(Oid class_id, Oid object_id)
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:188
const char * name
Definition: encode.c:561
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:56
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:177
int errmsg(const char *fmt,...)
Definition: elog.c:909
long changeDependencyFor(Oid classId, Oid objectId, Oid refClassId, Oid oldRefObjectId, Oid newRefObjectId)
Definition: pg_depend.c:399
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:477
void IsThereOpFamilyInNamespace(const char *opfname, Oid opfmethod, Oid opfnamespace)
Definition: opclasscmds.c:1722

◆ AlterObjectNamespace_oid()

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

Definition at line 589 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_NAMESPACE, 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().

591 {
592  Oid oldNspOid = InvalidOid;
593  ObjectAddress dep;
594 
595  dep.classId = classId;
596  dep.objectId = objid;
597  dep.objectSubId = 0;
598 
599  switch (getObjectClass(&dep))
600  {
601  case OCLASS_CLASS:
602  {
603  Relation rel;
604 
605  rel = relation_open(objid, AccessExclusiveLock);
606  oldNspOid = RelationGetNamespace(rel);
607 
608  AlterTableNamespaceInternal(rel, oldNspOid, nspOid, objsMoved);
609 
610  relation_close(rel, NoLock);
611  break;
612  }
613 
614  case OCLASS_TYPE:
615  oldNspOid = AlterTypeNamespace_oid(objid, nspOid, objsMoved);
616  break;
617 
618  case OCLASS_PROC:
619  case OCLASS_COLLATION:
620  case OCLASS_CONVERSION:
621  case OCLASS_OPERATOR:
622  case OCLASS_OPCLASS:
623  case OCLASS_OPFAMILY:
625  case OCLASS_TSPARSER:
626  case OCLASS_TSDICT:
627  case OCLASS_TSTEMPLATE:
628  case OCLASS_TSCONFIG:
629  {
630  Relation catalog;
631 
632  catalog = table_open(classId, RowExclusiveLock);
633 
634  oldNspOid = AlterObjectNamespace_internal(catalog, objid,
635  nspOid);
636 
637  table_close(catalog, RowExclusiveLock);
638  }
639  break;
640 
641  case OCLASS_CAST:
642  case OCLASS_CONSTRAINT:
643  case OCLASS_DEFAULT:
644  case OCLASS_LANGUAGE:
645  case OCLASS_LARGEOBJECT:
646  case OCLASS_AM:
647  case OCLASS_AMOP:
648  case OCLASS_AMPROC:
649  case OCLASS_REWRITE:
650  case OCLASS_TRIGGER:
651  case OCLASS_SCHEMA:
652  case OCLASS_ROLE:
653  case OCLASS_DATABASE:
654  case OCLASS_TBLSPACE:
655  case OCLASS_FDW:
657  case OCLASS_USER_MAPPING:
658  case OCLASS_DEFACL:
659  case OCLASS_EXTENSION:
661  case OCLASS_POLICY:
662  case OCLASS_PUBLICATION:
665  case OCLASS_SUBSCRIPTION:
666  case OCLASS_TRANSFORM:
667  /* ignore object types that don't have schema-qualified names */
668  break;
669 
670  /*
671  * There's intentionally no default: case here; we want the
672  * compiler to warn if a new OCLASS hasn't been handled above.
673  */
674  }
675 
676  return oldNspOid;
677 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
static Oid AlterObjectNamespace_internal(Relation rel, Oid objid, Oid nspOid)
Definition: alter.c:691
unsigned int Oid
Definition: postgres_ext.h:31
ObjectClass getObjectClass(const ObjectAddress *object)
Definition: dependency.c:2746
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
void AlterTableNamespaceInternal(Relation rel, Oid oldNspOid, Oid nspOid, ObjectAddresses *objsMoved)
Definition: tablecmds.c:16049
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
Oid AlterTypeNamespace_oid(Oid typeOid, Oid nspOid, ObjectAddresses *objsMoved)
Definition: typecmds.c:3917
#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:518

◆ AlterObjectOwner_internal()

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

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

936 {
937  Oid classId = RelationGetRelid(rel);
938  AttrNumber Anum_oid = get_object_attnum_oid(classId);
939  AttrNumber Anum_owner = get_object_attnum_owner(classId);
940  AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
941  AttrNumber Anum_acl = get_object_attnum_acl(classId);
942  AttrNumber Anum_name = get_object_attnum_name(classId);
943  HeapTuple oldtup;
944  Datum datum;
945  bool isnull;
946  Oid old_ownerId;
947  Oid namespaceId = InvalidOid;
948 
949  oldtup = get_catalog_object_by_oid(rel, Anum_oid, objectId);
950  if (oldtup == NULL)
951  elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
952  objectId, RelationGetRelationName(rel));
953 
954  datum = heap_getattr(oldtup, Anum_owner,
955  RelationGetDescr(rel), &isnull);
956  Assert(!isnull);
957  old_ownerId = DatumGetObjectId(datum);
958 
959  if (Anum_namespace != InvalidAttrNumber)
960  {
961  datum = heap_getattr(oldtup, Anum_namespace,
962  RelationGetDescr(rel), &isnull);
963  Assert(!isnull);
964  namespaceId = DatumGetObjectId(datum);
965  }
966 
967  if (old_ownerId != new_ownerId)
968  {
969  AttrNumber nattrs;
970  HeapTuple newtup;
971  Datum *values;
972  bool *nulls;
973  bool *replaces;
974 
975  /* Superusers can bypass permission checks */
976  if (!superuser())
977  {
978  /* must be owner */
979  if (!has_privs_of_role(GetUserId(), old_ownerId))
980  {
981  char *objname;
982  char namebuf[NAMEDATALEN];
983 
984  if (Anum_name != InvalidAttrNumber)
985  {
986  datum = heap_getattr(oldtup, Anum_name,
987  RelationGetDescr(rel), &isnull);
988  Assert(!isnull);
989  objname = NameStr(*DatumGetName(datum));
990  }
991  else
992  {
993  snprintf(namebuf, sizeof(namebuf), "%u", objectId);
994  objname = namebuf;
995  }
997  objname);
998  }
999  /* Must be able to become new owner */
1000  check_is_member_of_role(GetUserId(), new_ownerId);
1001 
1002  /* New owner must have CREATE privilege on namespace */
1003  if (OidIsValid(namespaceId))
1004  {
1005  AclResult aclresult;
1006 
1007  aclresult = pg_namespace_aclcheck(namespaceId, new_ownerId,
1008  ACL_CREATE);
1009  if (aclresult != ACLCHECK_OK)
1010  aclcheck_error(aclresult, OBJECT_SCHEMA,
1011  get_namespace_name(namespaceId));
1012  }
1013  }
1014 
1015  /* Build a modified tuple */
1016  nattrs = RelationGetNumberOfAttributes(rel);
1017  values = palloc0(nattrs * sizeof(Datum));
1018  nulls = palloc0(nattrs * sizeof(bool));
1019  replaces = palloc0(nattrs * sizeof(bool));
1020  values[Anum_owner - 1] = ObjectIdGetDatum(new_ownerId);
1021  replaces[Anum_owner - 1] = true;
1022 
1023  /*
1024  * Determine the modified ACL for the new owner. This is only
1025  * necessary when the ACL is non-null.
1026  */
1027  if (Anum_acl != InvalidAttrNumber)
1028  {
1029  datum = heap_getattr(oldtup,
1030  Anum_acl, RelationGetDescr(rel), &isnull);
1031  if (!isnull)
1032  {
1033  Acl *newAcl;
1034 
1035  newAcl = aclnewowner(DatumGetAclP(datum),
1036  old_ownerId, new_ownerId);
1037  values[Anum_acl - 1] = PointerGetDatum(newAcl);
1038  replaces[Anum_acl - 1] = true;
1039  }
1040  }
1041 
1042  newtup = heap_modify_tuple(oldtup, RelationGetDescr(rel),
1043  values, nulls, replaces);
1044 
1045  /* Perform actual update */
1046  CatalogTupleUpdate(rel, &newtup->t_self, newtup);
1047 
1048  /* Update owner dependency reference */
1049  if (classId == LargeObjectMetadataRelationId)
1050  classId = LargeObjectRelationId;
1051  changeDependencyOnOwner(classId, objectId, new_ownerId);
1052 
1053  /* Release memory */
1054  pfree(values);
1055  pfree(nulls);
1056  pfree(replaces);
1057  }
1058 
1059  InvokeObjectPostAlterHook(classId, objectId, 0);
1060 }
AttrNumber get_object_attnum_oid(Oid class_id)
AttrNumber get_object_attnum_owner(Oid class_id)
#define RelationGetDescr(relation)
Definition: rel.h:503
Oid GetUserId(void)
Definition: miscinit.c:495
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:483
#define DatumGetAclP(X)
Definition: acl.h:120
#define PointerGetDatum(X)
Definition: postgres.h:600
#define DatumGetObjectId(X)
Definition: postgres.h:544
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4843
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:710
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4758
#define NAMEDATALEN
#define DatumGetName(X)
Definition: postgres.h:629
void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
Definition: pg_shdepend.c:311
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3308
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_CREATE
Definition: parsenodes.h:92
ItemPointerData t_self
Definition: htup.h:65
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
AttrNumber get_object_attnum_acl(Oid class_id)
#define RelationGetRelationName(relation)
Definition: rel.h:511
void check_is_member_of_role(Oid member, Oid role)
Definition: acl.c:4893
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:175
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
AttrNumber get_object_attnum_name(Oid class_id)
void * palloc0(Size size)
Definition: mcxt.c:1093
AclResult
Definition: acl.h:177
HeapTuple get_catalog_object_by_oid(Relation catalog, AttrNumber oidcol, Oid objectId)
uintptr_t Datum
Definition: postgres.h:411
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:804
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
ObjectType get_object_type(Oid class_id, Oid object_id)
#define InvalidAttrNumber
Definition: attnum.h:23
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
#define snprintf
Definition: port.h:217
Acl * aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId)
Definition: acl.c:1037
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:477

◆ AlterObjectRename_internal()

static void AlterObjectRename_internal ( Relation  rel,
Oid  objectId,
const char *  new_name 
)
static

Definition at line 166 of file alter.c.

References ACL_CREATE, aclcheck_error(), ACLCHECK_NOT_OWNER, ACLCHECK_OK, Assert, CatalogTupleUpdate(), CStringGetDatum, DatumGetName, DatumGetObjectId, elog, ereport, errcode(), errmsg(), ERROR, get_namespace_name(), get_object_attnum_name(), get_object_attnum_namespace(), get_object_attnum_owner(), get_object_catcache_name(), get_object_catcache_oid(), get_object_type(), getObjectDescriptionOids(), GETSTRUCT, GetUserId(), has_privs_of_role(), heap_freetuple(), heap_getattr, heap_modify_tuple(), HeapTupleIsValid, InvalidOid, InvokeObjectPostAlterHook, IsThereCollationInNamespace(), IsThereFunctionInNamespace(), IsThereOpClassInNamespace(), IsThereOpFamilyInNamespace(), MyDatabaseId, NameGetDatum, NameStr, namestrcpy(), OBJECT_SCHEMA, ObjectIdGetDatum, OidIsValid, palloc0(), pfree(), pg_namespace_aclcheck(), RelationGetDescr, RelationGetNumberOfAttributes, RelationGetRelationName, RelationGetRelid, ReleaseSysCache(), report_name_conflict(), report_namespace_conflict(), SearchSysCache1(), SearchSysCacheExists1, SearchSysCacheExists2, SUBSCRIPTIONNAME, superuser(), HeapTupleData::t_self, values, and WARNING.

Referenced by ExecRenameStmt().

167 {
168  Oid classId = RelationGetRelid(rel);
169  int oidCacheId = get_object_catcache_oid(classId);
170  int nameCacheId = get_object_catcache_name(classId);
171  AttrNumber Anum_name = get_object_attnum_name(classId);
172  AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
173  AttrNumber Anum_owner = get_object_attnum_owner(classId);
174  HeapTuple oldtup;
175  HeapTuple newtup;
176  Datum datum;
177  bool isnull;
178  Oid namespaceId;
179  Oid ownerId;
180  char *old_name;
181  AclResult aclresult;
182  Datum *values;
183  bool *nulls;
184  bool *replaces;
185  NameData nameattrdata;
186 
187  oldtup = SearchSysCache1(oidCacheId, ObjectIdGetDatum(objectId));
188  if (!HeapTupleIsValid(oldtup))
189  elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
190  objectId, RelationGetRelationName(rel));
191 
192  datum = heap_getattr(oldtup, Anum_name,
193  RelationGetDescr(rel), &isnull);
194  Assert(!isnull);
195  old_name = NameStr(*(DatumGetName(datum)));
196 
197  /* Get OID of namespace */
198  if (Anum_namespace > 0)
199  {
200  datum = heap_getattr(oldtup, Anum_namespace,
201  RelationGetDescr(rel), &isnull);
202  Assert(!isnull);
203  namespaceId = DatumGetObjectId(datum);
204  }
205  else
206  namespaceId = InvalidOid;
207 
208  /* Permission checks ... superusers can always do it */
209  if (!superuser())
210  {
211  /* Fail if object does not have an explicit owner */
212  if (Anum_owner <= 0)
213  ereport(ERROR,
214  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
215  errmsg("must be superuser to rename %s",
216  getObjectDescriptionOids(classId, objectId))));
217 
218  /* Otherwise, must be owner of the existing object */
219  datum = heap_getattr(oldtup, Anum_owner,
220  RelationGetDescr(rel), &isnull);
221  Assert(!isnull);
222  ownerId = DatumGetObjectId(datum);
223 
224  if (!has_privs_of_role(GetUserId(), DatumGetObjectId(ownerId)))
226  old_name);
227 
228  /* User must have CREATE privilege on the namespace */
229  if (OidIsValid(namespaceId))
230  {
231  aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
232  ACL_CREATE);
233  if (aclresult != ACLCHECK_OK)
234  aclcheck_error(aclresult, OBJECT_SCHEMA,
235  get_namespace_name(namespaceId));
236  }
237  }
238 
239  /*
240  * Check for duplicate name (more friendly than unique-index failure).
241  * Since this is just a friendliness check, we can just skip it in cases
242  * where there isn't suitable support.
243  */
244  if (classId == ProcedureRelationId)
245  {
246  Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(oldtup);
247 
248  IsThereFunctionInNamespace(new_name, proc->pronargs,
249  &proc->proargtypes, proc->pronamespace);
250  }
251  else if (classId == CollationRelationId)
252  {
254 
255  IsThereCollationInNamespace(new_name, coll->collnamespace);
256  }
257  else if (classId == OperatorClassRelationId)
258  {
259  Form_pg_opclass opc = (Form_pg_opclass) GETSTRUCT(oldtup);
260 
261  IsThereOpClassInNamespace(new_name, opc->opcmethod,
262  opc->opcnamespace);
263  }
264  else if (classId == OperatorFamilyRelationId)
265  {
267 
268  IsThereOpFamilyInNamespace(new_name, opf->opfmethod,
269  opf->opfnamespace);
270  }
271  else if (classId == SubscriptionRelationId)
272  {
274  CStringGetDatum(new_name)))
275  report_name_conflict(classId, new_name);
276 
277  /* Also enforce regression testing naming rules, if enabled */
278 #ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
279  if (strncmp(new_name, "regress_", 8) != 0)
280  elog(WARNING, "subscriptions created by regression test cases should have names starting with \"regress_\"");
281 #endif
282  }
283  else if (nameCacheId >= 0)
284  {
285  if (OidIsValid(namespaceId))
286  {
287  if (SearchSysCacheExists2(nameCacheId,
288  CStringGetDatum(new_name),
289  ObjectIdGetDatum(namespaceId)))
290  report_namespace_conflict(classId, new_name, namespaceId);
291  }
292  else
293  {
294  if (SearchSysCacheExists1(nameCacheId,
295  CStringGetDatum(new_name)))
296  report_name_conflict(classId, new_name);
297  }
298  }
299 
300  /* Build modified tuple */
301  values = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(Datum));
302  nulls = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
303  replaces = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
304  namestrcpy(&nameattrdata, new_name);
305  values[Anum_name - 1] = NameGetDatum(&nameattrdata);
306  replaces[Anum_name - 1] = true;
307  newtup = heap_modify_tuple(oldtup, RelationGetDescr(rel),
308  values, nulls, replaces);
309 
310  /* Perform actual update */
311  CatalogTupleUpdate(rel, &oldtup->t_self, newtup);
312 
313  InvokeObjectPostAlterHook(classId, objectId, 0);
314 
315  /* Release memory */
316  pfree(values);
317  pfree(nulls);
318  pfree(replaces);
319  heap_freetuple(newtup);
320 
321  ReleaseSysCache(oldtup);
322 }
#define NameGetDatum(X)
Definition: postgres.h:639
static void report_namespace_conflict(Oid classId, const char *name, Oid nspOid)
Definition: alter.c:112
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
AttrNumber get_object_attnum_owner(Oid class_id)
#define RelationGetDescr(relation)
Definition: rel.h:503
Oid GetUserId(void)
Definition: miscinit.c:495
void namestrcpy(Name name, const char *str)
Definition: name.c:233
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:483
#define DatumGetObjectId(X)
Definition: postgres.h:544
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4843
int get_object_catcache_name(Oid class_id)
int errcode(int sqlerrcode)
Definition: elog.c:698
bool superuser(void)
Definition: superuser.c:46
AttrNumber get_object_attnum_namespace(Oid class_id)
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4758
void IsThereOpClassInNamespace(const char *opcname, Oid opcmethod, Oid opcnamespace)
Definition: opclasscmds.c:1699
#define DatumGetName(X)
Definition: postgres.h:629
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3308
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:186
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_CREATE
Definition: parsenodes.h:92
int get_object_catcache_oid(Oid class_id)
ItemPointerData t_self
Definition: htup.h:65
Definition: c.h:675
char * getObjectDescriptionOids(Oid classid, Oid objid)
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
#define CStringGetDatum(X)
Definition: postgres.h:622
#define RelationGetRelationName(relation)
Definition: rel.h:511
void IsThereCollationInNamespace(const char *collname, Oid nspOid)
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:175
#define WARNING
Definition: elog.h:40
void IsThereFunctionInNamespace(const char *proname, int pronargs, oidvector *proargtypes, Oid nspOid)
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1150
FormData_pg_opfamily * Form_pg_opfamily
Definition: pg_opfamily.h:51
AttrNumber get_object_attnum_name(Oid class_id)
void * palloc0(Size size)
Definition: mcxt.c:1093
AclResult
Definition: acl.h:177
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1198
Oid MyDatabaseId
Definition: globals.c:88
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
static void report_name_conflict(Oid classId, const char *name)
Definition: alter.c:77
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
ObjectType get_object_type(Oid class_id, Oid object_id)
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:188
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:56
static Datum values[MAXATTR]
Definition: bootstrap.c:156
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:477
void IsThereOpFamilyInNamespace(const char *opfname, Oid opfmethod, Oid opfnamespace)
Definition: opclasscmds.c:1722

◆ ExecAlterObjectDependsStmt()

ObjectAddress ExecAlterObjectDependsStmt ( AlterObjectDependsStmt stmt,
ObjectAddress refAddress 
)

Definition at line 431 of file alter.c.

References AccessExclusiveLock, Assert, check_object_ownership(), ObjectAddress::classId, deleteDependencyRecordsForSpecific(), DEPENDENCY_AUTO_EXTENSION, AlterObjectDependsStmt::extname, get_object_address(), get_object_address_rv(), getAutoExtensionsOfObject(), GetUserId(), list_member_oid(), NoLock, AlterObjectDependsStmt::object, OBJECT_EXTENSION, ObjectAddress::objectId, AlterObjectDependsStmt::objectType, recordDependencyOn(), AlterObjectDependsStmt::relation, AlterObjectDependsStmt::remove, 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  if (stmt->remove)
466  {
469  refAddr.classId, refAddr.objectId);
470  }
471  else
472  {
473  List *currexts;
474 
475  /* Avoid duplicates */
476  currexts = getAutoExtensionsOfObject(address.classId,
477  address.objectId);
478  if (!list_member_oid(currexts, refAddr.objectId))
479  recordDependencyOn(&address, &refAddr, DEPENDENCY_AUTO_EXTENSION);
480  }
481 
482  return address;
483 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
Oid GetUserId(void)
Definition: miscinit.c:495
Definition: nodes.h:538
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
long deleteDependencyRecordsForSpecific(Oid classId, Oid objectId, char deptype, Oid refclassId, Oid refobjectId)
Definition: pg_depend.c:340
#define NoLock
Definition: lockdefs.h:34
ObjectAddress get_object_address_rv(ObjectType objtype, RangeVar *rel, List *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:689
#define Assert(condition)
Definition: c.h:804
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
List * getAutoExtensionsOfObject(Oid classId, Oid objectId)
Definition: pg_depend.c:720

◆ ExecAlterObjectSchemaStmt()

ObjectAddress ExecAlterObjectSchemaStmt ( AlterObjectSchemaStmt stmt,
ObjectAddress oldSchemaAddr 
)

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

497 {
498  ObjectAddress address;
499  Oid oldNspOid;
500 
501  switch (stmt->objectType)
502  {
503  case OBJECT_EXTENSION:
504  address = AlterExtensionNamespace(strVal(stmt->object), stmt->newschema,
505  oldSchemaAddr ? &oldNspOid : NULL);
506  break;
507 
509  case OBJECT_SEQUENCE:
510  case OBJECT_TABLE:
511  case OBJECT_VIEW:
512  case OBJECT_MATVIEW:
513  address = AlterTableNamespace(stmt,
514  oldSchemaAddr ? &oldNspOid : NULL);
515  break;
516 
517  case OBJECT_DOMAIN:
518  case OBJECT_TYPE:
519  address = AlterTypeNamespace(castNode(List, stmt->object), stmt->newschema,
520  stmt->objectType,
521  oldSchemaAddr ? &oldNspOid : NULL);
522  break;
523 
524  /* generic code path */
525  case OBJECT_AGGREGATE:
526  case OBJECT_COLLATION:
527  case OBJECT_CONVERSION:
528  case OBJECT_FUNCTION:
529  case OBJECT_OPERATOR:
530  case OBJECT_OPCLASS:
531  case OBJECT_OPFAMILY:
532  case OBJECT_PROCEDURE:
533  case OBJECT_ROUTINE:
536  case OBJECT_TSDICTIONARY:
537  case OBJECT_TSPARSER:
538  case OBJECT_TSTEMPLATE:
539  {
540  Relation catalog;
541  Relation relation;
542  Oid classId;
543  Oid nspOid;
544 
545  address = get_object_address(stmt->objectType,
546  stmt->object,
547  &relation,
549  false);
550  Assert(relation == NULL);
551  classId = address.classId;
552  catalog = table_open(classId, RowExclusiveLock);
553  nspOid = LookupCreationNamespace(stmt->newschema);
554 
555  oldNspOid = AlterObjectNamespace_internal(catalog, address.objectId,
556  nspOid);
557  table_close(catalog, RowExclusiveLock);
558  }
559  break;
560 
561  default:
562  elog(ERROR, "unrecognized AlterObjectSchemaStmt type: %d",
563  (int) stmt->objectType);
564  return InvalidObjectAddress; /* keep compiler happy */
565  }
566 
567  if (oldSchemaAddr)
568  ObjectAddressSet(*oldSchemaAddr, NamespaceRelationId, oldNspOid);
569 
570  return address;
571 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define castNode(_type_, nodeptr)
Definition: nodes.h:607
Oid LookupCreationNamespace(const char *nspname)
Definition: namespace.c:2981
ObjectAddress AlterTypeNamespace(List *names, const char *newschema, ObjectType objecttype, Oid *oldschema)
Definition: typecmds.c:3880
#define strVal(v)
Definition: value.h:65
static Oid AlterObjectNamespace_internal(Relation rel, Oid objid, Oid nspOid)
Definition: alter.c:691
unsigned int Oid
Definition: postgres_ext.h:31
ObjectAddress AlterTableNamespace(AlterObjectSchemaStmt *stmt, Oid *oldschema)
Definition: tablecmds.c:15951
#define ERROR
Definition: elog.h:46
#define RowExclusiveLock
Definition: lockdefs.h:38
#define Assert(condition)
Definition: c.h:804
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:232
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:2779

◆ ExecAlterOwnerStmt()

ObjectAddress ExecAlterOwnerStmt ( AlterOwnerStmt stmt)

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

835 {
836  Oid newowner = get_rolespec_oid(stmt->newowner, false);
837 
838  switch (stmt->objectType)
839  {
840  case OBJECT_DATABASE:
841  return AlterDatabaseOwner(strVal(stmt->object), newowner);
842 
843  case OBJECT_SCHEMA:
844  return AlterSchemaOwner(strVal(stmt->object), newowner);
845 
846  case OBJECT_TYPE:
847  case OBJECT_DOMAIN: /* same as TYPE */
848  return AlterTypeOwner(castNode(List, stmt->object), newowner, stmt->objectType);
849  break;
850 
851  case OBJECT_FDW:
853  newowner);
854 
856  return AlterForeignServerOwner(strVal(stmt->object),
857  newowner);
858 
860  return AlterEventTriggerOwner(strVal(stmt->object),
861  newowner);
862 
863  case OBJECT_PUBLICATION:
864  return AlterPublicationOwner(strVal(stmt->object),
865  newowner);
866 
867  case OBJECT_SUBSCRIPTION:
868  return AlterSubscriptionOwner(strVal(stmt->object),
869  newowner);
870 
871  /* Generic cases */
872  case OBJECT_AGGREGATE:
873  case OBJECT_COLLATION:
874  case OBJECT_CONVERSION:
875  case OBJECT_FUNCTION:
876  case OBJECT_LANGUAGE:
877  case OBJECT_LARGEOBJECT:
878  case OBJECT_OPERATOR:
879  case OBJECT_OPCLASS:
880  case OBJECT_OPFAMILY:
881  case OBJECT_PROCEDURE:
882  case OBJECT_ROUTINE:
884  case OBJECT_TABLESPACE:
885  case OBJECT_TSDICTIONARY:
887  {
888  Relation catalog;
889  Relation relation;
890  Oid classId;
891  ObjectAddress address;
892 
893  address = get_object_address(stmt->objectType,
894  stmt->object,
895  &relation,
897  false);
898  Assert(relation == NULL);
899  classId = address.classId;
900 
901  /*
902  * XXX - get_object_address returns Oid of pg_largeobject
903  * catalog for OBJECT_LARGEOBJECT because of historical
904  * reasons. Fix up it here.
905  */
906  if (classId == LargeObjectRelationId)
907  classId = LargeObjectMetadataRelationId;
908 
909  catalog = table_open(classId, RowExclusiveLock);
910 
911  AlterObjectOwner_internal(catalog, address.objectId, newowner);
912  table_close(catalog, RowExclusiveLock);
913 
914  return address;
915  }
916  break;
917 
918  default:
919  elog(ERROR, "unrecognized AlterOwnerStmt type: %d",
920  (int) stmt->objectType);
921  return InvalidObjectAddress; /* keep compiler happy */
922  }
923 }
RoleSpec * newowner
Definition: parsenodes.h:3105
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define castNode(_type_, nodeptr)
Definition: nodes.h:607
ObjectType objectType
Definition: parsenodes.h:3102
#define strVal(v)
Definition: value.h:65
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:3677
#define ERROR
Definition: elog.h:46
ObjectAddress AlterForeignDataWrapperOwner(const char *name, Oid newOwnerId)
Definition: foreigncmds.c:275
void AlterObjectOwner_internal(Relation rel, Oid objectId, Oid new_ownerId)
Definition: alter.c:935
#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:5128
ObjectAddress AlterPublicationOwner(const char *name, Oid newOwnerId)
ObjectAddress AlterSchemaOwner(const char *name, Oid newOwnerId)
Definition: schemacmds.c:300
#define Assert(condition)
Definition: c.h:804
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:1634
#define AccessExclusiveLock
Definition: lockdefs.h:45
const ObjectAddress InvalidObjectAddress
#define elog(elevel,...)
Definition: elog.h:232
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:219
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
ObjectAddress RenameType(RenameStmt *stmt)
Definition: typecmds.c:3600
char * subname
Definition: parsenodes.h:3060
ObjectType renameType
Definition: parsenodes.h:3056
ObjectAddress RenameRelation(RenameStmt *stmt)
Definition: tablecmds.c:3736
char * newname
Definition: parsenodes.h:3062
ObjectAddress RenameDatabase(const char *oldname, const char *newname)
Definition: dbcommands.c:988
#define ERROR
Definition: elog.h:46
ObjectAddress rename_policy(RenameStmt *stmt)
Definition: policy.c:1102
#define RowExclusiveLock
Definition: lockdefs.h:38
Node * object
Definition: parsenodes.h:3059
ObjectAddress renametrig(RenameStmt *stmt)
Definition: trigger.c:1427
ObjectAddress renameatt(RenameStmt *stmt)
Definition: tablecmds.c:3541
ObjectAddress RenameConstraint(RenameStmt *stmt)
Definition: tablecmds.c:3685
RangeVar * relation
Definition: parsenodes.h:3058
ObjectAddress RenameRole(const char *oldname, const char *newname)
Definition: user.c:1114
#define Assert(condition)
Definition: c.h:804
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:931
#define AccessExclusiveLock
Definition: lockdefs.h:45
const ObjectAddress InvalidObjectAddress
#define elog(elevel,...)
Definition: elog.h:232
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ report_name_conflict()

static void report_name_conflict ( Oid  classId,
const char *  name 
)
static

Definition at line 77 of file alter.c.

References elog, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, and gettext_noop.

Referenced by AlterObjectRename_internal().

78 {
79  char *msgfmt;
80 
81  switch (classId)
82  {
83  case EventTriggerRelationId:
84  msgfmt = gettext_noop("event trigger \"%s\" already exists");
85  break;
86  case ForeignDataWrapperRelationId:
87  msgfmt = gettext_noop("foreign-data wrapper \"%s\" already exists");
88  break;
89  case ForeignServerRelationId:
90  msgfmt = gettext_noop("server \"%s\" already exists");
91  break;
92  case LanguageRelationId:
93  msgfmt = gettext_noop("language \"%s\" already exists");
94  break;
95  case PublicationRelationId:
96  msgfmt = gettext_noop("publication \"%s\" already exists");
97  break;
98  case SubscriptionRelationId:
99  msgfmt = gettext_noop("subscription \"%s\" already exists");
100  break;
101  default:
102  elog(ERROR, "unsupported object class %u", classId);
103  break;
104  }
105 
106  ereport(ERROR,
108  errmsg(msgfmt, name)));
109 }
#define gettext_noop(x)
Definition: c.h:1197
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
const char * name
Definition: encode.c:561
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32

◆ report_namespace_conflict()

static void report_namespace_conflict ( Oid  classId,
const char *  name,
Oid  nspOid 
)
static

Definition at line 112 of file alter.c.

References Assert, elog, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, get_namespace_name(), gettext_noop, and OidIsValid.

Referenced by AlterObjectNamespace_internal(), and AlterObjectRename_internal().

113 {
114  char *msgfmt;
115 
116  Assert(OidIsValid(nspOid));
117 
118  switch (classId)
119  {
120  case ConversionRelationId:
121  Assert(OidIsValid(nspOid));
122  msgfmt = gettext_noop("conversion \"%s\" already exists in schema \"%s\"");
123  break;
124  case StatisticExtRelationId:
125  Assert(OidIsValid(nspOid));
126  msgfmt = gettext_noop("statistics object \"%s\" already exists in schema \"%s\"");
127  break;
128  case TSParserRelationId:
129  Assert(OidIsValid(nspOid));
130  msgfmt = gettext_noop("text search parser \"%s\" already exists in schema \"%s\"");
131  break;
132  case TSDictionaryRelationId:
133  Assert(OidIsValid(nspOid));
134  msgfmt = gettext_noop("text search dictionary \"%s\" already exists in schema \"%s\"");
135  break;
136  case TSTemplateRelationId:
137  Assert(OidIsValid(nspOid));
138  msgfmt = gettext_noop("text search template \"%s\" already exists in schema \"%s\"");
139  break;
140  case TSConfigRelationId:
141  Assert(OidIsValid(nspOid));
142  msgfmt = gettext_noop("text search configuration \"%s\" already exists in schema \"%s\"");
143  break;
144  default:
145  elog(ERROR, "unsupported object class %u", classId);
146  break;
147  }
148 
149  ereport(ERROR,
151  errmsg(msgfmt, name, get_namespace_name(nspOid))));
152 }
#define gettext_noop(x)
Definition: c.h:1197
int errcode(int sqlerrcode)
Definition: elog.c:698
#define OidIsValid(objectId)
Definition: c.h:710
#define ERROR
Definition: elog.h:46
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
const char * name
Definition: encode.c:561
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32