PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
alter.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/sysattr.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_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 "parser/parse_func.h"
#include "miscadmin.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 "utils/tqual.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

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

Definition at line 615 of file alter.c.

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

Referenced by AlterObjectNamespace_oid(), and ExecAlterObjectSchemaStmt().

616 {
617  Oid classId = RelationGetRelid(rel);
618  int oidCacheId = get_object_catcache_oid(classId);
619  int nameCacheId = get_object_catcache_name(classId);
620  AttrNumber Anum_name = get_object_attnum_name(classId);
621  AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
622  AttrNumber Anum_owner = get_object_attnum_owner(classId);
623  AclObjectKind acl_kind = get_object_aclkind(classId);
624  Oid oldNspOid;
625  Datum name,
626  namespace;
627  bool isnull;
628  HeapTuple tup,
629  newtup;
630  Datum *values;
631  bool *nulls;
632  bool *replaces;
633 
634  tup = SearchSysCacheCopy1(oidCacheId, ObjectIdGetDatum(objid));
635  if (!HeapTupleIsValid(tup)) /* should not happen */
636  elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
637  objid, RelationGetRelationName(rel));
638 
639  name = heap_getattr(tup, Anum_name, RelationGetDescr(rel), &isnull);
640  Assert(!isnull);
641  namespace = heap_getattr(tup, Anum_namespace, RelationGetDescr(rel),
642  &isnull);
643  Assert(!isnull);
644  oldNspOid = DatumGetObjectId(namespace);
645 
646  /*
647  * If the object is already in the correct namespace, we don't need to do
648  * anything except fire the object access hook.
649  */
650  if (oldNspOid == nspOid)
651  {
652  InvokeObjectPostAlterHook(classId, objid, 0);
653  return oldNspOid;
654  }
655 
656  /* Check basic namespace related issues */
657  CheckSetNamespace(oldNspOid, nspOid);
658 
659  /* Permission checks ... superusers can always do it */
660  if (!superuser())
661  {
662  Datum owner;
663  Oid ownerId;
664  AclResult aclresult;
665 
666  /* Fail if object does not have an explicit owner */
667  if (Anum_owner <= 0)
668  ereport(ERROR,
669  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
670  (errmsg("must be superuser to set schema of %s",
671  getObjectDescriptionOids(classId, objid)))));
672 
673  /* Otherwise, must be owner of the existing object */
674  owner = heap_getattr(tup, Anum_owner, RelationGetDescr(rel), &isnull);
675  Assert(!isnull);
676  ownerId = DatumGetObjectId(owner);
677 
678  if (!has_privs_of_role(GetUserId(), ownerId))
680  NameStr(*(DatumGetName(name))));
681 
682  /* User must have CREATE privilege on new namespace */
683  aclresult = pg_namespace_aclcheck(nspOid, GetUserId(), ACL_CREATE);
684  if (aclresult != ACLCHECK_OK)
686  get_namespace_name(nspOid));
687  }
688 
689  /*
690  * Check for duplicate name (more friendly than unique-index failure).
691  * Since this is just a friendliness check, we can just skip it in cases
692  * where there isn't suitable support.
693  */
694  if (classId == ProcedureRelationId)
695  {
696  Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(tup);
697 
698  IsThereFunctionInNamespace(NameStr(proc->proname), proc->pronargs,
699  &proc->proargtypes, nspOid);
700  }
701  else if (classId == CollationRelationId)
702  {
704 
705  IsThereCollationInNamespace(NameStr(coll->collname), nspOid);
706  }
707  else if (classId == OperatorClassRelationId)
708  {
710 
711  IsThereOpClassInNamespace(NameStr(opc->opcname),
712  opc->opcmethod, nspOid);
713  }
714  else if (classId == OperatorFamilyRelationId)
715  {
717 
718  IsThereOpFamilyInNamespace(NameStr(opf->opfname),
719  opf->opfmethod, nspOid);
720  }
721  else if (nameCacheId >= 0 &&
722  SearchSysCacheExists2(nameCacheId, name,
723  ObjectIdGetDatum(nspOid)))
725  NameStr(*(DatumGetName(name))),
726  nspOid);
727 
728  /* Build modified tuple */
729  values = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(Datum));
730  nulls = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
731  replaces = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
732  values[Anum_namespace - 1] = ObjectIdGetDatum(nspOid);
733  replaces[Anum_namespace - 1] = true;
734  newtup = heap_modify_tuple(tup, RelationGetDescr(rel),
735  values, nulls, replaces);
736 
737  /* Perform actual update */
738  CatalogTupleUpdate(rel, &tup->t_self, newtup);
739 
740  /* Release memory */
741  pfree(values);
742  pfree(nulls);
743  pfree(replaces);
744 
745  /* update dependencies to point to the new schema */
746  changeDependencyFor(classId, objid,
747  NamespaceRelationId, oldNspOid, nspOid);
748 
749  InvokeObjectPostAlterHook(classId, objid, 0);
750 
751  return oldNspOid;
752 }
#define NamespaceRelationId
Definition: pg_namespace.h:34
static void report_namespace_conflict(Oid classId, const char *name, Oid nspOid)
Definition: alter.c:111
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
AttrNumber get_object_attnum_owner(Oid class_id)
#define RelationGetDescr(relation)
Definition: rel.h:425
Oid GetUserId(void)
Definition: miscinit.c:283
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:419
#define ProcedureRelationId
Definition: pg_proc.h:33
#define DatumGetObjectId(X)
Definition: postgres.h:506
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4813
int get_object_catcache_name(Oid class_id)
#define OperatorClassRelationId
Definition: pg_opclass.h:49
#define OperatorFamilyRelationId
Definition: pg_opfamily.h:29
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
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:4458
void IsThereOpClassInNamespace(const char *opcname, Oid opcmethod, Oid opcnamespace)
Definition: opclasscmds.c:1649
#define DatumGetName(X)
Definition: postgres.h:591
void pfree(void *pointer)
Definition: mcxt.c:950
AclObjectKind get_object_aclkind(Oid class_id)
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ACL_CREATE
Definition: parsenodes.h:75
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:3006
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3377
#define RelationGetRelationName(relation)
Definition: rel.h:433
void IsThereCollationInNamespace(const char *collname, Oid nspOid)
#define ereport(elevel, rest)
Definition: elog.h:122
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:163
void CheckSetNamespace(Oid oldNspOid, Oid nspOid)
Definition: namespace.c:2762
void IsThereFunctionInNamespace(const char *proname, int pronargs, oidvector *proargtypes, Oid nspOid)
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:769
FormData_pg_opfamily * Form_pg_opfamily
Definition: pg_opfamily.h:44
AttrNumber get_object_attnum_name(Oid class_id)
void * palloc0(Size size)
Definition: mcxt.c:878
AclResult
Definition: acl.h:170
uintptr_t Datum
Definition: postgres.h:372
#define CollationRelationId
Definition: pg_collation.h:30
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:675
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:170
const char * name
Definition: encode.c:521
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:51
static Datum values[MAXATTR]
Definition: bootstrap.c:162
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:159
int errmsg(const char *fmt,...)
Definition: elog.c:797
long changeDependencyFor(Oid classId, Oid objectId, Oid refClassId, Oid oldRefObjectId, Oid newRefObjectId)
Definition: pg_depend.c:295
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
AclObjectKind
Definition: acl.h:179
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:793
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:68
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:413
void IsThereOpFamilyInNamespace(const char *opfname, Oid opfmethod, Oid opfnamespace)
Definition: opclasscmds.c:1672
Oid AlterObjectNamespace_oid ( Oid  classId,
Oid  objid,
Oid  nspOid,
ObjectAddresses objsMoved 
)

Definition at line 545 of file alter.c.

References AccessExclusiveLock, AlterObjectNamespace_internal(), AlterTableNamespaceInternal(), AlterTypeNamespace_oid(), ObjectAddress::classId, getObjectClass(), heap_close, heap_open(), InvalidOid, NoLock, ObjectAddress::objectId, ObjectAddress::objectSubId, OCLASS_CLASS, OCLASS_COLLATION, OCLASS_CONVERSION, OCLASS_OPCLASS, OCLASS_OPERATOR, OCLASS_OPFAMILY, OCLASS_PROC, OCLASS_TSCONFIG, OCLASS_TSDICT, OCLASS_TSPARSER, OCLASS_TSTEMPLATE, OCLASS_TYPE, relation_close(), relation_open(), RelationGetNamespace, and RowExclusiveLock.

Referenced by AlterExtensionNamespace().

547 {
548  Oid oldNspOid = InvalidOid;
549  ObjectAddress dep;
550 
551  dep.classId = classId;
552  dep.objectId = objid;
553  dep.objectSubId = 0;
554 
555  switch (getObjectClass(&dep))
556  {
557  case OCLASS_CLASS:
558  {
559  Relation rel;
560 
561  rel = relation_open(objid, AccessExclusiveLock);
562  oldNspOid = RelationGetNamespace(rel);
563 
564  AlterTableNamespaceInternal(rel, oldNspOid, nspOid, objsMoved);
565 
566  relation_close(rel, NoLock);
567  break;
568  }
569 
570  case OCLASS_TYPE:
571  oldNspOid = AlterTypeNamespace_oid(objid, nspOid, objsMoved);
572  break;
573 
574  case OCLASS_COLLATION:
575  case OCLASS_CONVERSION:
576  case OCLASS_OPERATOR:
577  case OCLASS_OPCLASS:
578  case OCLASS_OPFAMILY:
579  case OCLASS_PROC:
580  case OCLASS_TSPARSER:
581  case OCLASS_TSDICT:
582  case OCLASS_TSTEMPLATE:
583  case OCLASS_TSCONFIG:
584  {
585  Relation catalog;
586 
587  catalog = heap_open(classId, RowExclusiveLock);
588 
589  oldNspOid = AlterObjectNamespace_internal(catalog, objid,
590  nspOid);
591 
592  heap_close(catalog, RowExclusiveLock);
593  }
594  break;
595 
596  default:
597  break;
598  }
599 
600  return oldNspOid;
601 }
static Oid AlterObjectNamespace_internal(Relation rel, Oid objid, Oid nspOid)
Definition: alter.c:615
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1263
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
ObjectClass getObjectClass(const ObjectAddress *object)
Definition: dependency.c:2312
void AlterTableNamespaceInternal(Relation rel, Oid oldNspOid, Oid nspOid, ObjectAddresses *objsMoved)
Definition: tablecmds.c:12156
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
Oid AlterTypeNamespace_oid(Oid typeOid, Oid nspOid, ObjectAddresses *objsMoved)
Definition: typecmds.c:3453
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define InvalidOid
Definition: postgres_ext.h:36
#define AccessExclusiveLock
Definition: lockdefs.h:46
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1117
#define RelationGetNamespace(relation)
Definition: rel.h:440
void AlterObjectOwner_internal ( Relation  rel,
Oid  objectId,
Oid  new_ownerId 
)

Definition at line 857 of file alter.c.

References ACL_CREATE, ACL_KIND_NAMESPACE, 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_aclkind(), get_object_attnum_acl(), get_object_attnum_name(), get_object_attnum_namespace(), get_object_attnum_owner(), GetUserId(), has_privs_of_role(), heap_getattr, heap_modify_tuple(), HeapTupleGetOid, InvalidAttrNumber, InvalidOid, InvokeObjectPostAlterHook, LargeObjectMetadataRelationId, LargeObjectRelationId, NAMEDATALEN, NameStr, NULL, ObjectIdGetDatum, OidIsValid, palloc0(), pfree(), pg_namespace_aclcheck(), PointerGetDatum, RelationGetDescr, RelationGetNumberOfAttributes, RelationGetRelationName, RelationGetRelid, snprintf(), superuser(), HeapTupleData::t_self, and values.

Referenced by ExecAlterOwnerStmt(), and shdepReassignOwned().

858 {
859  Oid classId = RelationGetRelid(rel);
860  AttrNumber Anum_owner = get_object_attnum_owner(classId);
861  AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
862  AttrNumber Anum_acl = get_object_attnum_acl(classId);
863  AttrNumber Anum_name = get_object_attnum_name(classId);
864  HeapTuple oldtup;
865  Datum datum;
866  bool isnull;
867  Oid old_ownerId;
868  Oid namespaceId = InvalidOid;
869 
870  oldtup = get_catalog_object_by_oid(rel, objectId);
871  if (oldtup == NULL)
872  elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
873  objectId, RelationGetRelationName(rel));
874 
875  datum = heap_getattr(oldtup, Anum_owner,
876  RelationGetDescr(rel), &isnull);
877  Assert(!isnull);
878  old_ownerId = DatumGetObjectId(datum);
879 
880  if (Anum_namespace != InvalidAttrNumber)
881  {
882  datum = heap_getattr(oldtup, Anum_namespace,
883  RelationGetDescr(rel), &isnull);
884  Assert(!isnull);
885  namespaceId = DatumGetObjectId(datum);
886  }
887 
888  if (old_ownerId != new_ownerId)
889  {
890  AttrNumber nattrs;
891  HeapTuple newtup;
892  Datum *values;
893  bool *nulls;
894  bool *replaces;
895 
896  /* Superusers can bypass permission checks */
897  if (!superuser())
898  {
899  AclObjectKind aclkind = get_object_aclkind(classId);
900 
901  /* must be owner */
902  if (!has_privs_of_role(GetUserId(), old_ownerId))
903  {
904  char *objname;
905  char namebuf[NAMEDATALEN];
906 
907  if (Anum_name != InvalidAttrNumber)
908  {
909  datum = heap_getattr(oldtup, Anum_name,
910  RelationGetDescr(rel), &isnull);
911  Assert(!isnull);
912  objname = NameStr(*DatumGetName(datum));
913  }
914  else
915  {
916  snprintf(namebuf, sizeof(namebuf), "%u",
917  HeapTupleGetOid(oldtup));
918  objname = namebuf;
919  }
920  aclcheck_error(ACLCHECK_NOT_OWNER, aclkind, objname);
921  }
922  /* Must be able to become new owner */
923  check_is_member_of_role(GetUserId(), new_ownerId);
924 
925  /* New owner must have CREATE privilege on namespace */
926  if (OidIsValid(namespaceId))
927  {
928  AclResult aclresult;
929 
930  aclresult = pg_namespace_aclcheck(namespaceId, new_ownerId,
931  ACL_CREATE);
932  if (aclresult != ACLCHECK_OK)
934  get_namespace_name(namespaceId));
935  }
936  }
937 
938  /* Build a modified tuple */
939  nattrs = RelationGetNumberOfAttributes(rel);
940  values = palloc0(nattrs * sizeof(Datum));
941  nulls = palloc0(nattrs * sizeof(bool));
942  replaces = palloc0(nattrs * sizeof(bool));
943  values[Anum_owner - 1] = ObjectIdGetDatum(new_ownerId);
944  replaces[Anum_owner - 1] = true;
945 
946  /*
947  * Determine the modified ACL for the new owner. This is only
948  * necessary when the ACL is non-null.
949  */
950  if (Anum_acl != InvalidAttrNumber)
951  {
952  datum = heap_getattr(oldtup,
953  Anum_acl, RelationGetDescr(rel), &isnull);
954  if (!isnull)
955  {
956  Acl *newAcl;
957 
958  newAcl = aclnewowner(DatumGetAclP(datum),
959  old_ownerId, new_ownerId);
960  values[Anum_acl - 1] = PointerGetDatum(newAcl);
961  replaces[Anum_acl - 1] = true;
962  }
963  }
964 
965  newtup = heap_modify_tuple(oldtup, RelationGetDescr(rel),
966  values, nulls, replaces);
967 
968  /* Perform actual update */
969  CatalogTupleUpdate(rel, &newtup->t_self, newtup);
970 
971  /* Update owner dependency reference */
972  if (classId == LargeObjectMetadataRelationId)
973  classId = LargeObjectRelationId;
974  changeDependencyOnOwner(classId, HeapTupleGetOid(newtup), new_ownerId);
975 
976  /* Release memory */
977  pfree(values);
978  pfree(nulls);
979  pfree(replaces);
980  }
981 
982  InvokeObjectPostAlterHook(classId, objectId, 0);
983 }
AttrNumber get_object_attnum_owner(Oid class_id)
#define RelationGetDescr(relation)
Definition: rel.h:425
Oid GetUserId(void)
Definition: miscinit.c:283
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:419
#define DatumGetAclP(X)
Definition: acl.h:113
#define PointerGetDatum(X)
Definition: postgres.h:562
#define DatumGetObjectId(X)
Definition: postgres.h:506
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4813
bool superuser(void)
Definition: superuser.c:47
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
AttrNumber get_object_attnum_namespace(Oid class_id)
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4458
#define NAMEDATALEN
#define DatumGetName(X)
Definition: postgres.h:591
void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
Definition: pg_shdepend.c:303
void pfree(void *pointer)
Definition: mcxt.c:950
AclObjectKind get_object_aclkind(Oid class_id)
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ACL_CREATE
Definition: parsenodes.h:75
ItemPointerData t_self
Definition: htup.h:65
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
AttrNumber get_object_attnum_acl(Oid class_id)
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3377
#define RelationGetRelationName(relation)
Definition: rel.h:433
void check_is_member_of_role(Oid member, Oid role)
Definition: acl.c:4859
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:163
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:769
AttrNumber get_object_attnum_name(Oid class_id)
void * palloc0(Size size)
Definition: mcxt.c:878
AclResult
Definition: acl.h:170
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define LargeObjectMetadataRelationId
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
HeapTuple get_catalog_object_by_oid(Relation catalog, Oid objectId)
#define InvalidAttrNumber
Definition: attnum.h:23
static Datum values[MAXATTR]
Definition: bootstrap.c:162
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
AclObjectKind
Definition: acl.h:179
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:793
#define LargeObjectRelationId
Acl * aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId)
Definition: acl.c:1035
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:413
static void AlterObjectRename_internal ( Relation  rel,
Oid  objectId,
const char *  new_name 
)
static

Definition at line 161 of file alter.c.

References ACL_CREATE, ACL_KIND_NAMESPACE, aclcheck_error(), ACLCHECK_NOT_OWNER, ACLCHECK_OK, Assert, CatalogTupleUpdate(), CollationRelationId, CStringGetDatum, DatumGetName, DatumGetObjectId, elog, ereport, errcode(), errmsg(), ERROR, get_namespace_name(), get_object_aclkind(), get_object_attnum_name(), get_object_attnum_namespace(), get_object_attnum_owner(), get_object_catcache_name(), get_object_catcache_oid(), 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(), ObjectIdGetDatum, OidIsValid, OperatorClassRelationId, OperatorFamilyRelationId, palloc0(), pfree(), pg_namespace_aclcheck(), ProcedureRelationId, RelationGetDescr, RelationGetNumberOfAttributes, RelationGetRelationName, RelationGetRelid, ReleaseSysCache(), report_name_conflict(), report_namespace_conflict(), SearchSysCache1, SearchSysCacheExists1, SearchSysCacheExists2, SUBSCRIPTIONNAME, SubscriptionRelationId, superuser(), HeapTupleData::t_self, and values.

Referenced by ExecRenameStmt().

162 {
163  Oid classId = RelationGetRelid(rel);
164  int oidCacheId = get_object_catcache_oid(classId);
165  int nameCacheId = get_object_catcache_name(classId);
166  AttrNumber Anum_name = get_object_attnum_name(classId);
167  AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
168  AttrNumber Anum_owner = get_object_attnum_owner(classId);
169  AclObjectKind acl_kind = get_object_aclkind(classId);
170  HeapTuple oldtup;
171  HeapTuple newtup;
172  Datum datum;
173  bool isnull;
174  Oid namespaceId;
175  Oid ownerId;
176  char *old_name;
177  AclResult aclresult;
178  Datum *values;
179  bool *nulls;
180  bool *replaces;
181  NameData nameattrdata;
182 
183  oldtup = SearchSysCache1(oidCacheId, ObjectIdGetDatum(objectId));
184  if (!HeapTupleIsValid(oldtup))
185  elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
186  objectId, RelationGetRelationName(rel));
187 
188  datum = heap_getattr(oldtup, Anum_name,
189  RelationGetDescr(rel), &isnull);
190  Assert(!isnull);
191  old_name = NameStr(*(DatumGetName(datum)));
192 
193  /* Get OID of namespace */
194  if (Anum_namespace > 0)
195  {
196  datum = heap_getattr(oldtup, Anum_namespace,
197  RelationGetDescr(rel), &isnull);
198  Assert(!isnull);
199  namespaceId = DatumGetObjectId(datum);
200  }
201  else
202  namespaceId = InvalidOid;
203 
204  /* Permission checks ... superusers can always do it */
205  if (!superuser())
206  {
207  /* Fail if object does not have an explicit owner */
208  if (Anum_owner <= 0)
209  ereport(ERROR,
210  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
211  (errmsg("must be superuser to rename %s",
212  getObjectDescriptionOids(classId, objectId)))));
213 
214  /* Otherwise, must be owner of the existing object */
215  datum = heap_getattr(oldtup, Anum_owner,
216  RelationGetDescr(rel), &isnull);
217  Assert(!isnull);
218  ownerId = DatumGetObjectId(datum);
219 
220  if (!has_privs_of_role(GetUserId(), DatumGetObjectId(ownerId)))
221  aclcheck_error(ACLCHECK_NOT_OWNER, acl_kind, old_name);
222 
223  /* User must have CREATE privilege on the namespace */
224  if (OidIsValid(namespaceId))
225  {
226  aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
227  ACL_CREATE);
228  if (aclresult != ACLCHECK_OK)
230  get_namespace_name(namespaceId));
231  }
232  }
233 
234  /*
235  * Check for duplicate name (more friendly than unique-index failure).
236  * Since this is just a friendliness check, we can just skip it in cases
237  * where there isn't suitable support.
238  */
239  if (classId == ProcedureRelationId)
240  {
241  Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(oldtup);
242 
243  IsThereFunctionInNamespace(new_name, proc->pronargs,
244  &proc->proargtypes, proc->pronamespace);
245  }
246  else if (classId == CollationRelationId)
247  {
249 
250  IsThereCollationInNamespace(new_name, coll->collnamespace);
251  }
252  else if (classId == OperatorClassRelationId)
253  {
254  Form_pg_opclass opc = (Form_pg_opclass) GETSTRUCT(oldtup);
255 
256  IsThereOpClassInNamespace(new_name, opc->opcmethod,
257  opc->opcnamespace);
258  }
259  else if (classId == OperatorFamilyRelationId)
260  {
262 
263  IsThereOpFamilyInNamespace(new_name, opf->opfmethod,
264  opf->opfnamespace);
265  }
266  else if (classId == SubscriptionRelationId)
267  {
269  CStringGetDatum(new_name)))
270  report_name_conflict(classId, new_name);
271  }
272  else if (nameCacheId >= 0)
273  {
274  if (OidIsValid(namespaceId))
275  {
276  if (SearchSysCacheExists2(nameCacheId,
277  CStringGetDatum(new_name),
278  ObjectIdGetDatum(namespaceId)))
279  report_namespace_conflict(classId, new_name, namespaceId);
280  }
281  else
282  {
283  if (SearchSysCacheExists1(nameCacheId,
284  CStringGetDatum(new_name)))
285  report_name_conflict(classId, new_name);
286  }
287  }
288 
289  /* Build modified tuple */
290  values = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(Datum));
291  nulls = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
292  replaces = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
293  namestrcpy(&nameattrdata, new_name);
294  values[Anum_name - 1] = NameGetDatum(&nameattrdata);
295  replaces[Anum_name - 1] = true;
296  newtup = heap_modify_tuple(oldtup, RelationGetDescr(rel),
297  values, nulls, replaces);
298 
299  /* Perform actual update */
300  CatalogTupleUpdate(rel, &oldtup->t_self, newtup);
301 
302  InvokeObjectPostAlterHook(classId, objectId, 0);
303 
304  /* Release memory */
305  pfree(values);
306  pfree(nulls);
307  pfree(replaces);
308  heap_freetuple(newtup);
309 
310  ReleaseSysCache(oldtup);
311 }
#define NameGetDatum(X)
Definition: postgres.h:601
static void report_namespace_conflict(Oid classId, const char *name, Oid nspOid)
Definition: alter.c:111
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
AttrNumber get_object_attnum_owner(Oid class_id)
#define RelationGetDescr(relation)
Definition: rel.h:425
Oid GetUserId(void)
Definition: miscinit.c:283
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:419
#define ProcedureRelationId
Definition: pg_proc.h:33
#define DatumGetObjectId(X)
Definition: postgres.h:506
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4813
int get_object_catcache_name(Oid class_id)
#define OperatorClassRelationId
Definition: pg_opclass.h:49
#define OperatorFamilyRelationId
Definition: pg_opfamily.h:29
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
AttrNumber get_object_attnum_namespace(Oid class_id)
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
unsigned int Oid
Definition: postgres_ext.h:31
int namestrcpy(Name name, const char *str)
Definition: name.c:217
#define OidIsValid(objectId)
Definition: c.h:538
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4458
void IsThereOpClassInNamespace(const char *opcname, Oid opcmethod, Oid opcnamespace)
Definition: opclasscmds.c:1649
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:150
#define DatumGetName(X)
Definition: postgres.h:591
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:168
void pfree(void *pointer)
Definition: mcxt.c:950
AclObjectKind get_object_aclkind(Oid class_id)
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ACL_CREATE
Definition: parsenodes.h:75
int get_object_catcache_oid(Oid class_id)
ItemPointerData t_self
Definition: htup.h:65
Definition: c.h:493
char * getObjectDescriptionOids(Oid classid, Oid objid)
#define SubscriptionRelationId
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3377
#define CStringGetDatum(X)
Definition: postgres.h:584
#define RelationGetRelationName(relation)
Definition: rel.h:433
void IsThereCollationInNamespace(const char *collname, Oid nspOid)
#define ereport(elevel, rest)
Definition: elog.h:122
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:163
void IsThereFunctionInNamespace(const char *proname, int pronargs, oidvector *proargtypes, Oid nspOid)
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:769
FormData_pg_opfamily * Form_pg_opfamily
Definition: pg_opfamily.h:44
AttrNumber get_object_attnum_name(Oid class_id)
void * palloc0(Size size)
Definition: mcxt.c:878
AclResult
Definition: acl.h:170
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1093
Oid MyDatabaseId
Definition: globals.c:76
#define CollationRelationId
Definition: pg_collation.h:30
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:675
static void report_name_conflict(Oid classId, const char *name)
Definition: alter.c:76
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:170
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:51
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
AclObjectKind
Definition: acl.h:179
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:793
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:68
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:413
void IsThereOpFamilyInNamespace(const char *opfname, Oid opfmethod, Oid opfnamespace)
Definition: opclasscmds.c:1672
ObjectAddress ExecAlterObjectDependsStmt ( AlterObjectDependsStmt stmt,
ObjectAddress refAddress 
)

Definition at line 417 of file alter.c.

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

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

418 {
419  ObjectAddress address;
420  ObjectAddress refAddr;
421  Relation rel;
422 
423  address =
424  get_object_address_rv(stmt->objectType, stmt->relation, (List *) stmt->object,
425  &rel, AccessExclusiveLock, false);
426 
427  /*
428  * If a relation was involved, it would have been opened and locked. We
429  * don't need the relation here, but we'll retain the lock until commit.
430  */
431  if (rel)
432  heap_close(rel, NoLock);
433 
434  refAddr = get_object_address(OBJECT_EXTENSION, (Node *) stmt->extname,
435  &rel, AccessExclusiveLock, false);
436  Assert(rel == NULL);
437  if (refAddress)
438  *refAddress = refAddr;
439 
440  recordDependencyOn(&address, &refAddr, DEPENDENCY_AUTO_EXTENSION);
441 
442  return address;
443 }
Definition: nodes.h:520
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
#define heap_close(r, l)
Definition: heapam.h:97
#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 NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
ObjectAddress get_object_address(ObjectType objtype, Node *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
#define AccessExclusiveLock
Definition: lockdefs.h:46
Definition: pg_list.h:45
ObjectAddress ExecAlterObjectSchemaStmt ( AlterObjectSchemaStmt stmt,
ObjectAddress oldSchemaAddr 
)

Definition at line 455 of file alter.c.

References AccessExclusiveLock, AlterExtensionNamespace(), AlterObjectNamespace_internal(), AlterTableNamespace(), AlterTypeNamespace(), Assert, castNode, ObjectAddress::classId, elog, ERROR, get_object_address(), heap_close, heap_open(), InvalidObjectAddress, LookupCreationNamespace(), NamespaceRelationId, AlterObjectSchemaStmt::newschema, NULL, 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_SEQUENCE, OBJECT_TABLE, OBJECT_TSCONFIGURATION, OBJECT_TSDICTIONARY, OBJECT_TSPARSER, OBJECT_TSTEMPLATE, OBJECT_TYPE, OBJECT_VIEW, ObjectAddressSet, ObjectAddress::objectId, AlterObjectSchemaStmt::objectType, RowExclusiveLock, and strVal.

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

457 {
458  ObjectAddress address;
459  Oid oldNspOid;
460 
461  switch (stmt->objectType)
462  {
463  case OBJECT_EXTENSION:
464  address = AlterExtensionNamespace(strVal((Value *) stmt->object), stmt->newschema,
465  oldSchemaAddr ? &oldNspOid : NULL);
466  break;
467 
469  case OBJECT_SEQUENCE:
470  case OBJECT_TABLE:
471  case OBJECT_VIEW:
472  case OBJECT_MATVIEW:
473  address = AlterTableNamespace(stmt,
474  oldSchemaAddr ? &oldNspOid : NULL);
475  break;
476 
477  case OBJECT_DOMAIN:
478  case OBJECT_TYPE:
479  address = AlterTypeNamespace(castNode(List, stmt->object), stmt->newschema,
480  stmt->objectType,
481  oldSchemaAddr ? &oldNspOid : NULL);
482  break;
483 
484  /* generic code path */
485  case OBJECT_AGGREGATE:
486  case OBJECT_COLLATION:
487  case OBJECT_CONVERSION:
488  case OBJECT_FUNCTION:
489  case OBJECT_OPERATOR:
490  case OBJECT_OPCLASS:
491  case OBJECT_OPFAMILY:
493  case OBJECT_TSDICTIONARY:
494  case OBJECT_TSPARSER:
495  case OBJECT_TSTEMPLATE:
496  {
497  Relation catalog;
498  Relation relation;
499  Oid classId;
500  Oid nspOid;
501 
502  address = get_object_address(stmt->objectType,
503  stmt->object,
504  &relation,
506  false);
507  Assert(relation == NULL);
508  classId = address.classId;
509  catalog = heap_open(classId, RowExclusiveLock);
510  nspOid = LookupCreationNamespace(stmt->newschema);
511 
512  oldNspOid = AlterObjectNamespace_internal(catalog, address.objectId,
513  nspOid);
514  heap_close(catalog, RowExclusiveLock);
515  }
516  break;
517 
518  default:
519  elog(ERROR, "unrecognized AlterObjectSchemaStmt type: %d",
520  (int) stmt->objectType);
521  return InvalidObjectAddress; /* keep compiler happy */
522  }
523 
524  if (oldSchemaAddr)
525  ObjectAddressSet(*oldSchemaAddr, NamespaceRelationId, oldNspOid);
526 
527  return address;
528 }
#define NamespaceRelationId
Definition: pg_namespace.h:34
#define castNode(_type_, nodeptr)
Definition: nodes.h:589
Oid LookupCreationNamespace(const char *nspname)
Definition: namespace.c:2730
ObjectAddress AlterTypeNamespace(List *names, const char *newschema, ObjectType objecttype, Oid *oldschema)
Definition: typecmds.c:3416
#define strVal(v)
Definition: value.h:54
static Oid AlterObjectNamespace_internal(Relation rel, Oid objid, Oid nspOid)
Definition: alter.c:615
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
ObjectAddress AlterTableNamespace(AlterObjectSchemaStmt *stmt, Oid *oldschema)
Definition: tablecmds.c:12086
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
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:46
const ObjectAddress InvalidObjectAddress
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
ObjectAddress AlterExtensionNamespace(const char *extensionName, const char *newschema, Oid *oldschema)
Definition: extension.c:2675
ObjectAddress ExecAlterOwnerStmt ( AlterOwnerStmt stmt)

Definition at line 759 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(), heap_close, heap_open(), InvalidObjectAddress, LargeObjectMetadataRelationId, LargeObjectRelationId, AlterOwnerStmt::newowner, NULL, 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_PUBLICATION, OBJECT_SCHEMA, OBJECT_SUBSCRIPTION, OBJECT_TABLESPACE, OBJECT_TSCONFIGURATION, OBJECT_TSDICTIONARY, OBJECT_TYPE, ObjectAddress::objectId, AlterOwnerStmt::objectType, RowExclusiveLock, and strVal.

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

760 {
761  Oid newowner = get_rolespec_oid(stmt->newowner, false);
762 
763  switch (stmt->objectType)
764  {
765  case OBJECT_DATABASE:
766  return AlterDatabaseOwner(strVal((Value *) stmt->object), newowner);
767 
768  case OBJECT_SCHEMA:
769  return AlterSchemaOwner(strVal((Value *) stmt->object), newowner);
770 
771  case OBJECT_TYPE:
772  case OBJECT_DOMAIN: /* same as TYPE */
773  return AlterTypeOwner(castNode(List, stmt->object), newowner, stmt->objectType);
774  break;
775 
776  case OBJECT_FDW:
778  newowner);
779 
781  return AlterForeignServerOwner(strVal((Value *) stmt->object),
782  newowner);
783 
785  return AlterEventTriggerOwner(strVal((Value *) stmt->object),
786  newowner);
787 
788  case OBJECT_PUBLICATION:
789  return AlterPublicationOwner(strVal((Value *) stmt->object),
790  newowner);
791 
792  case OBJECT_SUBSCRIPTION:
793  return AlterSubscriptionOwner(strVal((Value *) stmt->object),
794  newowner);
795 
796  /* Generic cases */
797  case OBJECT_AGGREGATE:
798  case OBJECT_COLLATION:
799  case OBJECT_CONVERSION:
800  case OBJECT_FUNCTION:
801  case OBJECT_LANGUAGE:
802  case OBJECT_LARGEOBJECT:
803  case OBJECT_OPERATOR:
804  case OBJECT_OPCLASS:
805  case OBJECT_OPFAMILY:
806  case OBJECT_TABLESPACE:
807  case OBJECT_TSDICTIONARY:
809  {
810  Relation catalog;
811  Relation relation;
812  Oid classId;
813  ObjectAddress address;
814 
815  address = get_object_address(stmt->objectType,
816  stmt->object,
817  &relation,
819  false);
820  Assert(relation == NULL);
821  classId = address.classId;
822 
823  /*
824  * XXX - get_object_address returns Oid of pg_largeobject
825  * catalog for OBJECT_LARGEOBJECT because of historical
826  * reasons. Fix up it here.
827  */
828  if (classId == LargeObjectRelationId)
830 
831  catalog = heap_open(classId, RowExclusiveLock);
832 
833  AlterObjectOwner_internal(catalog, address.objectId, newowner);
834  heap_close(catalog, RowExclusiveLock);
835 
836  return address;
837  }
838  break;
839 
840  default:
841  elog(ERROR, "unrecognized AlterOwnerStmt type: %d",
842  (int) stmt->objectType);
843  return InvalidObjectAddress; /* keep compiler happy */
844  }
845 }
RoleSpec * newowner
Definition: parsenodes.h:2775
#define castNode(_type_, nodeptr)
Definition: nodes.h:589
ObjectType objectType
Definition: parsenodes.h:2772
#define strVal(v)
Definition: value.h:54
#define heap_close(r, l)
Definition: heapam.h:97
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:3212
#define ERROR
Definition: elog.h:43
ObjectAddress AlterForeignDataWrapperOwner(const char *name, Oid newOwnerId)
Definition: foreigncmds.c:277
void AlterObjectOwner_internal(Relation rel, Oid objectId, Oid new_ownerId)
Definition: alter.c:857
#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:5129
ObjectAddress AlterPublicationOwner(const char *name, Oid newOwnerId)
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define NULL
Definition: c.h:229
ObjectAddress AlterSchemaOwner(const char *name, Oid newOwnerId)
Definition: schemacmds.c:320
#define Assert(condition)
Definition: c.h:675
Definition: value.h:42
ObjectAddress AlterForeignServerOwner(const char *name, Oid newOwnerId)
Definition: foreigncmds.c:414
ObjectAddress get_object_address(ObjectType objtype, Node *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
#define LargeObjectMetadataRelationId
ObjectAddress AlterDatabaseOwner(const char *dbname, Oid newOwnerId)
Definition: dbcommands.c:1593
#define AccessExclusiveLock
Definition: lockdefs.h:46
const ObjectAddress InvalidObjectAddress
#define elog
Definition: elog.h:219
#define LargeObjectRelationId
Definition: pg_list.h:45
ObjectAddress ExecRenameStmt ( RenameStmt stmt)

Definition at line 320 of file alter.c.

References AccessExclusiveLock, AlterObjectRename_internal(), Assert, ObjectAddress::classId, elog, ERROR, get_object_address(), heap_close, heap_open(), InvalidObjectAddress, RenameStmt::newname, NULL, 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_PUBLICATION, OBJECT_ROLE, OBJECT_RULE, OBJECT_SCHEMA, OBJECT_SEQUENCE, 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, and RenameStmt::subname.

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

321 {
322  switch (stmt->renameType)
323  {
326  return RenameConstraint(stmt);
327 
328  case OBJECT_DATABASE:
329  return RenameDatabase(stmt->subname, stmt->newname);
330 
331  case OBJECT_ROLE:
332  return RenameRole(stmt->subname, stmt->newname);
333 
334  case OBJECT_SCHEMA:
335  return RenameSchema(stmt->subname, stmt->newname);
336 
337  case OBJECT_TABLESPACE:
338  return RenameTableSpace(stmt->subname, stmt->newname);
339 
340  case OBJECT_TABLE:
341  case OBJECT_SEQUENCE:
342  case OBJECT_VIEW:
343  case OBJECT_MATVIEW:
344  case OBJECT_INDEX:
346  return RenameRelation(stmt);
347 
348  case OBJECT_COLUMN:
349  case OBJECT_ATTRIBUTE:
350  return renameatt(stmt);
351 
352  case OBJECT_RULE:
353  return RenameRewriteRule(stmt->relation, stmt->subname,
354  stmt->newname);
355 
356  case OBJECT_TRIGGER:
357  return renametrig(stmt);
358 
359  case OBJECT_POLICY:
360  return rename_policy(stmt);
361 
362  case OBJECT_DOMAIN:
363  case OBJECT_TYPE:
364  return RenameType(stmt);
365 
366  case OBJECT_AGGREGATE:
367  case OBJECT_COLLATION:
368  case OBJECT_CONVERSION:
370  case OBJECT_FDW:
372  case OBJECT_FUNCTION:
373  case OBJECT_OPCLASS:
374  case OBJECT_OPFAMILY:
375  case OBJECT_LANGUAGE:
377  case OBJECT_TSDICTIONARY:
378  case OBJECT_TSPARSER:
379  case OBJECT_TSTEMPLATE:
380  case OBJECT_PUBLICATION:
381  case OBJECT_SUBSCRIPTION:
382  {
383  ObjectAddress address;
384  Relation catalog;
385  Relation relation;
386 
387  address = get_object_address(stmt->renameType,
388  stmt->object,
389  &relation,
390  AccessExclusiveLock, false);
391  Assert(relation == NULL);
392 
393  catalog = heap_open(address.classId, RowExclusiveLock);
395  address.objectId,
396  stmt->newname);
397  heap_close(catalog, RowExclusiveLock);
398 
399  return address;
400  }
401 
402  default:
403  elog(ERROR, "unrecognized rename stmt type: %d",
404  (int) stmt->renameType);
405  return InvalidObjectAddress; /* keep compiler happy */
406  }
407 }
ObjectAddress RenameSchema(const char *oldname, const char *newname)
Definition: schemacmds.c:241
ObjectAddress RenameType(RenameStmt *stmt)
Definition: typecmds.c:3134
char * subname
Definition: parsenodes.h:2731
ObjectType renameType
Definition: parsenodes.h:2727
ObjectAddress RenameRelation(RenameStmt *stmt)
Definition: tablecmds.c:2830
#define heap_close(r, l)
Definition: heapam.h:97
char * newname
Definition: parsenodes.h:2733
ObjectAddress RenameDatabase(const char *oldname, const char *newname)
Definition: dbcommands.c:969
#define ERROR
Definition: elog.h:43
ObjectAddress rename_policy(RenameStmt *stmt)
Definition: policy.c:1201
#define RowExclusiveLock
Definition: lockdefs.h:38
Node * object
Definition: parsenodes.h:2730
ObjectAddress renametrig(RenameStmt *stmt)
Definition: trigger.c:1364
ObjectAddress renameatt(RenameStmt *stmt)
Definition: tablecmds.c:2642
ObjectAddress RenameConstraint(RenameStmt *stmt)
Definition: tablecmds.c:2779
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
RangeVar * relation
Definition: parsenodes.h:2729
ObjectAddress RenameRole(const char *oldname, const char *newname)
Definition: user.c:1131
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
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:161
ObjectAddress RenameRewriteRule(RangeVar *relation, const char *oldName, const char *newName)
ObjectAddress RenameTableSpace(const char *oldname, const char *newname)
Definition: tablespace.c:910
#define AccessExclusiveLock
Definition: lockdefs.h:46
const ObjectAddress InvalidObjectAddress
#define elog
Definition: elog.h:219
static void report_name_conflict ( Oid  classId,
const char *  name 
)
static

Definition at line 76 of file alter.c.

References elog, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, EventTriggerRelationId, ForeignDataWrapperRelationId, ForeignServerRelationId, gettext_noop, LanguageRelationId, PublicationRelationId, and SubscriptionRelationId.

Referenced by AlterObjectRename_internal().

77 {
78  char *msgfmt;
79 
80  switch (classId)
81  {
83  msgfmt = gettext_noop("event trigger \"%s\" already exists");
84  break;
86  msgfmt = gettext_noop("foreign-data wrapper \"%s\" already exists");
87  break;
89  msgfmt = gettext_noop("server \"%s\" already exists");
90  break;
91  case LanguageRelationId:
92  msgfmt = gettext_noop("language \"%s\" already exists");
93  break;
95  msgfmt = gettext_noop("publication \"%s\" already exists");
96  break;
98  msgfmt = gettext_noop("subscription \"%s\" already exists");
99  break;
100  default:
101  elog(ERROR, "unsupported object class %u", classId);
102  break;
103  }
104 
105  ereport(ERROR,
107  errmsg(msgfmt, name)));
108 }
#define gettext_noop(x)
Definition: c.h:139
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PublicationRelationId
#define ERROR
Definition: elog.h:43
#define SubscriptionRelationId
#define ereport(elevel, rest)
Definition: elog.h:122
#define ForeignServerRelationId
#define EventTriggerRelationId
#define ForeignDataWrapperRelationId
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define LanguageRelationId
Definition: pg_language.h:29
#define elog
Definition: elog.h:219
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:31
static void report_namespace_conflict ( Oid  classId,
const char *  name,
Oid  nspOid 
)
static

Definition at line 111 of file alter.c.

References Assert, ConversionRelationId, elog, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, get_namespace_name(), gettext_noop, OidIsValid, TSConfigRelationId, TSDictionaryRelationId, TSParserRelationId, and TSTemplateRelationId.

Referenced by AlterObjectNamespace_internal(), and AlterObjectRename_internal().

112 {
113  char *msgfmt;
114 
115  Assert(OidIsValid(nspOid));
116 
117  switch (classId)
118  {
120  Assert(OidIsValid(nspOid));
121  msgfmt = gettext_noop("conversion \"%s\" already exists in schema \"%s\"");
122  break;
123  case TSParserRelationId:
124  Assert(OidIsValid(nspOid));
125  msgfmt = gettext_noop("text search parser \"%s\" already exists in schema \"%s\"");
126  break;
128  Assert(OidIsValid(nspOid));
129  msgfmt = gettext_noop("text search dictionary \"%s\" already exists in schema \"%s\"");
130  break;
132  Assert(OidIsValid(nspOid));
133  msgfmt = gettext_noop("text search template \"%s\" already exists in schema \"%s\"");
134  break;
135  case TSConfigRelationId:
136  Assert(OidIsValid(nspOid));
137  msgfmt = gettext_noop("text search configuration \"%s\" already exists in schema \"%s\"");
138  break;
139  default:
140  elog(ERROR, "unsupported object class %u", classId);
141  break;
142  }
143 
144  ereport(ERROR,
146  errmsg(msgfmt, name, get_namespace_name(nspOid))));
147 }
#define gettext_noop(x)
Definition: c.h:139
int errcode(int sqlerrcode)
Definition: elog.c:575
#define OidIsValid(objectId)
Definition: c.h:538
#define TSConfigRelationId
Definition: pg_ts_config.h:31
#define ERROR
Definition: elog.h:43
#define TSDictionaryRelationId
Definition: pg_ts_dict.h:31
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
#define ereport(elevel, rest)
Definition: elog.h:122
#define TSParserRelationId
Definition: pg_ts_parser.h:31
#define Assert(condition)
Definition: c.h:675
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:31
#define ConversionRelationId
Definition: pg_conversion.h:38
#define TSTemplateRelationId