PostgreSQL Source Code git master
alter.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/relation.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_database_d.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_operator.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/dbcommands.h"
#include "commands/defrem.h"
#include "commands/event_trigger.h"
#include "commands/extension.h"
#include "commands/policy.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 "replication/logicalworker.h"
#include "rewrite/rewriteDefine.h"
#include "storage/lmgr.h"
#include "utils/acl.h"
#include "utils/builtins.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 (Oid classId, Oid objectId, Oid new_ownerId)
 

Function Documentation

◆ AlterObjectNamespace_internal()

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

Definition at line 692 of file alter.c.

693{
694 Oid classId = RelationGetRelid(rel);
695 int oidCacheId = get_object_catcache_oid(classId);
696 int nameCacheId = get_object_catcache_name(classId);
697 AttrNumber Anum_name = get_object_attnum_name(classId);
698 AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
699 AttrNumber Anum_owner = get_object_attnum_owner(classId);
700 Oid oldNspOid;
701 Datum name,
702 namespace;
703 bool isnull;
704 HeapTuple tup,
705 newtup;
706 Datum *values;
707 bool *nulls;
708 bool *replaces;
709
710 tup = SearchSysCacheCopy1(oidCacheId, ObjectIdGetDatum(objid));
711 if (!HeapTupleIsValid(tup)) /* should not happen */
712 elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
713 objid, RelationGetRelationName(rel));
714
715 name = heap_getattr(tup, Anum_name, RelationGetDescr(rel), &isnull);
716 Assert(!isnull);
717 namespace = heap_getattr(tup, Anum_namespace, RelationGetDescr(rel),
718 &isnull);
719 Assert(!isnull);
720 oldNspOid = DatumGetObjectId(namespace);
721
722 /*
723 * If the object is already in the correct namespace, we don't need to do
724 * anything except fire the object access hook.
725 */
726 if (oldNspOid == nspOid)
727 {
728 InvokeObjectPostAlterHook(classId, objid, 0);
729 return oldNspOid;
730 }
731
732 /* Check basic namespace related issues */
733 CheckSetNamespace(oldNspOid, nspOid);
734
735 /* Permission checks ... superusers can always do it */
736 if (!superuser())
737 {
738 Datum owner;
739 Oid ownerId;
740 AclResult aclresult;
741
742 /* Fail if object does not have an explicit owner */
743 if (Anum_owner <= 0)
745 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
746 errmsg("must be superuser to set schema of %s",
747 getObjectDescriptionOids(classId, objid))));
748
749 /* Otherwise, must be owner of the existing object */
750 owner = heap_getattr(tup, Anum_owner, RelationGetDescr(rel), &isnull);
751 Assert(!isnull);
752 ownerId = DatumGetObjectId(owner);
753
754 if (!has_privs_of_role(GetUserId(), ownerId))
757
758 /* User must have CREATE privilege on new namespace */
759 aclresult = object_aclcheck(NamespaceRelationId, nspOid, GetUserId(), ACL_CREATE);
760 if (aclresult != ACLCHECK_OK)
761 aclcheck_error(aclresult, OBJECT_SCHEMA,
762 get_namespace_name(nspOid));
763 }
764
765 /*
766 * Check for duplicate name (more friendly than unique-index failure).
767 * Since this is just a friendliness check, we can just skip it in cases
768 * where there isn't suitable support.
769 */
770 if (classId == ProcedureRelationId)
771 {
772 Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(tup);
773
774 IsThereFunctionInNamespace(NameStr(proc->proname), proc->pronargs,
775 &proc->proargtypes, nspOid);
776 }
777 else if (classId == CollationRelationId)
778 {
780
781 IsThereCollationInNamespace(NameStr(coll->collname), nspOid);
782 }
783 else if (classId == OperatorClassRelationId)
784 {
786
788 opc->opcmethod, nspOid);
789 }
790 else if (classId == OperatorFamilyRelationId)
791 {
793
795 opf->opfmethod, nspOid);
796 }
797 else if (nameCacheId >= 0 &&
798 SearchSysCacheExists2(nameCacheId, name,
799 ObjectIdGetDatum(nspOid)))
802 nspOid);
803
804 /* Build modified tuple */
806 nulls = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
807 replaces = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
808 values[Anum_namespace - 1] = ObjectIdGetDatum(nspOid);
809 replaces[Anum_namespace - 1] = true;
810 newtup = heap_modify_tuple(tup, RelationGetDescr(rel),
811 values, nulls, replaces);
812
813 /* Perform actual update */
814 CatalogTupleUpdate(rel, &tup->t_self, newtup);
815
816 /* Release memory */
817 pfree(values);
818 pfree(nulls);
819 pfree(replaces);
820
821 /* update dependency to point to the new schema */
822 if (changeDependencyFor(classId, objid,
823 NamespaceRelationId, oldNspOid, nspOid) != 1)
824 elog(ERROR, "could not change schema dependency for object %u",
825 objid);
826
827 InvokeObjectPostAlterHook(classId, objid, 0);
828
829 return oldNspOid;
830}
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:5268
AclResult
Definition: acl.h:182
@ ACLCHECK_OK
Definition: acl.h:183
@ ACLCHECK_NOT_OWNER
Definition: acl.h:185
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2622
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition: aclchk.c:3804
static void report_namespace_conflict(Oid classId, const char *name, Oid nspOid)
Definition: alter.c:111
int16 AttrNumber
Definition: attnum.h:21
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define NameStr(name)
Definition: c.h:717
void IsThereCollationInNamespace(const char *collname, Oid nspOid)
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
void IsThereFunctionInNamespace(const char *proname, int pronargs, oidvector *proargtypes, Oid nspOid)
Assert(PointerIsAligned(start, uint64))
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition: heaptuple.c:1210
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: htup_details.h:903
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:313
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3449
void pfree(void *pointer)
Definition: mcxt.c:1524
void * palloc0(Size size)
Definition: mcxt.c:1347
Oid GetUserId(void)
Definition: miscinit.c:520
void CheckSetNamespace(Oid oldNspOid, Oid nspOid)
Definition: namespace.c:3459
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:197
int get_object_catcache_name(Oid class_id)
AttrNumber get_object_attnum_owner(Oid class_id)
AttrNumber get_object_attnum_namespace(Oid class_id)
char * getObjectDescriptionOids(Oid classid, Oid objid)
AttrNumber get_object_attnum_name(Oid class_id)
int get_object_catcache_oid(Oid class_id)
ObjectType get_object_type(Oid class_id, Oid object_id)
void IsThereOpFamilyInNamespace(const char *opfname, Oid opfmethod, Oid opfnamespace)
Definition: opclasscmds.c:1828
void IsThereOpClassInNamespace(const char *opcname, Oid opcmethod, Oid opcnamespace)
Definition: opclasscmds.c:1805
@ OBJECT_SCHEMA
Definition: parsenodes.h:2348
#define ACL_CREATE
Definition: parsenodes.h:85
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:58
long changeDependencyFor(Oid classId, Oid objectId, Oid refClassId, Oid oldRefObjectId, Oid newRefObjectId)
Definition: pg_depend.c:457
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83
FormData_pg_opfamily * Form_pg_opfamily
Definition: pg_opfamily.h:51
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
static Name DatumGetName(Datum X)
Definition: postgres.h:365
uintptr_t Datum
Definition: postgres.h:69
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:247
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
unsigned int Oid
Definition: postgres_ext.h:32
#define RelationGetRelid(relation)
Definition: rel.h:513
#define RelationGetDescr(relation)
Definition: rel.h:539
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:519
#define RelationGetRelationName(relation)
Definition: rel.h:547
ItemPointerData t_self
Definition: htup.h:65
bool superuser(void)
Definition: superuser.c:46
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:91
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:102
const char * name

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_aclcheck(), OBJECT_SCHEMA, ObjectIdGetDatum(), palloc0(), pfree(), RelationGetDescr, RelationGetNumberOfAttributes, RelationGetRelationName, RelationGetRelid, report_namespace_conflict(), SearchSysCacheCopy1, SearchSysCacheExists2, superuser(), HeapTupleData::t_self, and values.

Referenced by AlterObjectNamespace_oid(), and ExecAlterObjectSchemaStmt().

◆ AlterObjectNamespace_oid()

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

Definition at line 625 of file alter.c.

627{
628 Oid oldNspOid = InvalidOid;
629
630 switch (classId)
631 {
632 case RelationRelationId:
633 {
634 Relation rel;
635
637 oldNspOid = RelationGetNamespace(rel);
638
639 AlterTableNamespaceInternal(rel, oldNspOid, nspOid, objsMoved);
640
642 break;
643 }
644
645 case TypeRelationId:
646 oldNspOid = AlterTypeNamespace_oid(objid, nspOid, true, objsMoved);
647 break;
648
649 case ProcedureRelationId:
650 case CollationRelationId:
651 case ConversionRelationId:
652 case OperatorRelationId:
653 case OperatorClassRelationId:
654 case OperatorFamilyRelationId:
655 case StatisticExtRelationId:
656 case TSParserRelationId:
657 case TSDictionaryRelationId:
658 case TSTemplateRelationId:
659 case TSConfigRelationId:
660 {
661 Relation catalog;
662
663 catalog = table_open(classId, RowExclusiveLock);
664
665 oldNspOid = AlterObjectNamespace_internal(catalog, objid,
666 nspOid);
667
669 }
670 break;
671
672 default:
673 /* ignore object types that don't have schema-qualified names */
675 }
676
677 return oldNspOid;
678}
static Oid AlterObjectNamespace_internal(Relation rel, Oid objid, Oid nspOid)
Definition: alter.c:692
#define InvalidAttrNumber
Definition: attnum.h:23
#define NoLock
Definition: lockdefs.h:34
#define AccessExclusiveLock
Definition: lockdefs.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define InvalidOid
Definition: postgres_ext.h:37
#define RelationGetNamespace(relation)
Definition: rel.h:554
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:47
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
void AlterTableNamespaceInternal(Relation rel, Oid oldNspOid, Oid nspOid, ObjectAddresses *objsMoved)
Definition: tablecmds.c:18284
Oid AlterTypeNamespace_oid(Oid typeOid, Oid nspOid, bool ignoreDependent, ObjectAddresses *objsMoved)
Definition: typecmds.c:4091

References AccessExclusiveLock, AlterObjectNamespace_internal(), AlterTableNamespaceInternal(), AlterTypeNamespace_oid(), Assert(), get_object_attnum_namespace(), InvalidAttrNumber, InvalidOid, NoLock, relation_close(), relation_open(), RelationGetNamespace, RowExclusiveLock, table_close(), and table_open().

Referenced by AlterExtensionNamespace().

◆ AlterObjectOwner_internal()

void AlterObjectOwner_internal ( Oid  classId,
Oid  objectId,
Oid  new_ownerId 
)

Definition at line 926 of file alter.c.

927{
928 /* For large objects, the catalog to modify is pg_largeobject_metadata */
929 Oid catalogId = (classId == LargeObjectRelationId) ? LargeObjectMetadataRelationId : classId;
930 AttrNumber Anum_oid = get_object_attnum_oid(catalogId);
931 AttrNumber Anum_owner = get_object_attnum_owner(catalogId);
932 AttrNumber Anum_namespace = get_object_attnum_namespace(catalogId);
933 AttrNumber Anum_acl = get_object_attnum_acl(catalogId);
934 AttrNumber Anum_name = get_object_attnum_name(catalogId);
935 Relation rel;
936 HeapTuple oldtup;
937 Datum datum;
938 bool isnull;
939 Oid old_ownerId;
940 Oid namespaceId = InvalidOid;
941
942 rel = table_open(catalogId, RowExclusiveLock);
943
944 /* Search tuple and lock it. */
945 oldtup =
946 get_catalog_object_by_oid_extended(rel, Anum_oid, objectId, true);
947 if (oldtup == NULL)
948 elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
949 objectId, RelationGetRelationName(rel));
950
951 datum = heap_getattr(oldtup, Anum_owner,
952 RelationGetDescr(rel), &isnull);
953 Assert(!isnull);
954 old_ownerId = DatumGetObjectId(datum);
955
956 if (Anum_namespace != InvalidAttrNumber)
957 {
958 datum = heap_getattr(oldtup, Anum_namespace,
959 RelationGetDescr(rel), &isnull);
960 Assert(!isnull);
961 namespaceId = DatumGetObjectId(datum);
962 }
963
964 if (old_ownerId != new_ownerId)
965 {
966 AttrNumber nattrs;
967 HeapTuple newtup;
968 Datum *values;
969 bool *nulls;
970 bool *replaces;
971
972 /* Superusers can bypass permission checks */
973 if (!superuser())
974 {
975 /* must be owner */
976 if (!has_privs_of_role(GetUserId(), old_ownerId))
977 {
978 char *objname;
979 char namebuf[NAMEDATALEN];
980
981 if (Anum_name != InvalidAttrNumber)
982 {
983 datum = heap_getattr(oldtup, Anum_name,
984 RelationGetDescr(rel), &isnull);
985 Assert(!isnull);
986 objname = NameStr(*DatumGetName(datum));
987 }
988 else
989 {
990 snprintf(namebuf, sizeof(namebuf), "%u", objectId);
991 objname = namebuf;
992 }
994 get_object_type(catalogId, objectId),
995 objname);
996 }
997 /* Must be able to become new owner */
998 check_can_set_role(GetUserId(), new_ownerId);
999
1000 /* New owner must have CREATE privilege on namespace */
1001 if (OidIsValid(namespaceId))
1002 {
1003 AclResult aclresult;
1004
1005 aclresult = object_aclcheck(NamespaceRelationId, namespaceId, new_ownerId,
1006 ACL_CREATE);
1007 if (aclresult != ACLCHECK_OK)
1008 aclcheck_error(aclresult, OBJECT_SCHEMA,
1009 get_namespace_name(namespaceId));
1010 }
1011 }
1012
1013 /* Build a modified tuple */
1014 nattrs = RelationGetNumberOfAttributes(rel);
1015 values = palloc0(nattrs * sizeof(Datum));
1016 nulls = palloc0(nattrs * sizeof(bool));
1017 replaces = palloc0(nattrs * sizeof(bool));
1018 values[Anum_owner - 1] = ObjectIdGetDatum(new_ownerId);
1019 replaces[Anum_owner - 1] = true;
1020
1021 /*
1022 * Determine the modified ACL for the new owner. This is only
1023 * necessary when the ACL is non-null.
1024 */
1025 if (Anum_acl != InvalidAttrNumber)
1026 {
1027 datum = heap_getattr(oldtup,
1028 Anum_acl, RelationGetDescr(rel), &isnull);
1029 if (!isnull)
1030 {
1031 Acl *newAcl;
1032
1033 newAcl = aclnewowner(DatumGetAclP(datum),
1034 old_ownerId, new_ownerId);
1035 values[Anum_acl - 1] = PointerGetDatum(newAcl);
1036 replaces[Anum_acl - 1] = true;
1037 }
1038 }
1039
1040 newtup = heap_modify_tuple(oldtup, RelationGetDescr(rel),
1041 values, nulls, replaces);
1042
1043 /* Perform actual update */
1044 CatalogTupleUpdate(rel, &newtup->t_self, newtup);
1045
1047
1048 /* Update owner dependency reference */
1049 changeDependencyOnOwner(classId, objectId, new_ownerId);
1050
1051 /* Release memory */
1052 pfree(values);
1053 pfree(nulls);
1054 pfree(replaces);
1055 }
1056 else
1058
1059 /* Note the post-alter hook gets classId not catalogId */
1060 InvokeObjectPostAlterHook(classId, objectId, 0);
1061
1063}
Acl * aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId)
Definition: acl.c:1103
void check_can_set_role(Oid member, Oid role)
Definition: acl.c:5325
#define DatumGetAclP(X)
Definition: acl.h:120
#define OidIsValid(objectId)
Definition: c.h:746
void UnlockTuple(Relation relation, ItemPointer tid, LOCKMODE lockmode)
Definition: lmgr.c:601
#define InplaceUpdateTupleLock
Definition: lockdefs.h:48
AttrNumber get_object_attnum_oid(Oid class_id)
AttrNumber get_object_attnum_acl(Oid class_id)
HeapTuple get_catalog_object_by_oid_extended(Relation catalog, AttrNumber oidcol, Oid objectId, bool locktup)
#define NAMEDATALEN
void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
Definition: pg_shdepend.c:316
#define snprintf
Definition: port.h:239
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327

References ACL_CREATE, aclcheck_error(), ACLCHECK_NOT_OWNER, ACLCHECK_OK, aclnewowner(), Assert(), CatalogTupleUpdate(), changeDependencyOnOwner(), check_can_set_role(), DatumGetAclP, DatumGetName(), DatumGetObjectId(), elog, ERROR, get_catalog_object_by_oid_extended(), 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(), InplaceUpdateTupleLock, InvalidAttrNumber, InvalidOid, InvokeObjectPostAlterHook, NAMEDATALEN, NameStr, object_aclcheck(), OBJECT_SCHEMA, ObjectIdGetDatum(), OidIsValid, palloc0(), pfree(), PointerGetDatum(), RelationGetDescr, RelationGetNumberOfAttributes, RelationGetRelationName, RowExclusiveLock, snprintf, superuser(), HeapTupleData::t_self, table_close(), table_open(), UnlockTuple(), and values.

Referenced by ExecAlterOwnerStmt(), and shdepReassignOwned_Owner().

◆ AlterObjectRename_internal()

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

Definition at line 165 of file alter.c.

166{
167 Oid classId = RelationGetRelid(rel);
168 int oidCacheId = get_object_catcache_oid(classId);
169 int nameCacheId = get_object_catcache_name(classId);
170 AttrNumber Anum_name = get_object_attnum_name(classId);
171 AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
172 AttrNumber Anum_owner = get_object_attnum_owner(classId);
173 HeapTuple oldtup;
174 HeapTuple newtup;
175 Datum datum;
176 bool isnull;
177 Oid namespaceId;
178 Oid ownerId;
179 char *old_name;
180 AclResult aclresult;
181 Datum *values;
182 bool *nulls;
183 bool *replaces;
184 NameData nameattrdata;
185
186 oldtup = SearchSysCache1(oidCacheId, ObjectIdGetDatum(objectId));
187 if (!HeapTupleIsValid(oldtup))
188 elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
189 objectId, RelationGetRelationName(rel));
190
191 datum = heap_getattr(oldtup, Anum_name,
192 RelationGetDescr(rel), &isnull);
193 Assert(!isnull);
194 old_name = NameStr(*(DatumGetName(datum)));
195
196 /* Get OID of namespace */
197 if (Anum_namespace > 0)
198 {
199 datum = heap_getattr(oldtup, Anum_namespace,
200 RelationGetDescr(rel), &isnull);
201 Assert(!isnull);
202 namespaceId = DatumGetObjectId(datum);
203 }
204 else
205 namespaceId = InvalidOid;
206
207 /* Permission checks ... superusers can always do it */
208 if (!superuser())
209 {
210 /* Fail if object does not have an explicit owner */
211 if (Anum_owner <= 0)
213 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
214 errmsg("must be superuser to rename %s",
215 getObjectDescriptionOids(classId, objectId))));
216
217 /* Otherwise, must be owner of the existing object */
218 datum = heap_getattr(oldtup, Anum_owner,
219 RelationGetDescr(rel), &isnull);
220 Assert(!isnull);
221 ownerId = DatumGetObjectId(datum);
222
225 old_name);
226
227 /* User must have CREATE privilege on the namespace */
228 if (OidIsValid(namespaceId))
229 {
230 aclresult = object_aclcheck(NamespaceRelationId, namespaceId, GetUserId(),
231 ACL_CREATE);
232 if (aclresult != ACLCHECK_OK)
233 aclcheck_error(aclresult, OBJECT_SCHEMA,
234 get_namespace_name(namespaceId));
235 }
236
237 if (classId == SubscriptionRelationId)
238 {
240
241 /* must have CREATE privilege on database */
242 aclresult = object_aclcheck(DatabaseRelationId, MyDatabaseId,
244 if (aclresult != ACLCHECK_OK)
247
248 /*
249 * Don't allow non-superuser modification of a subscription with
250 * password_required=false.
251 */
252 form = (Form_pg_subscription) GETSTRUCT(oldtup);
253 if (!form->subpasswordrequired && !superuser())
255 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
256 errmsg("password_required=false is superuser-only"),
257 errhint("Subscriptions with the password_required option set to false may only be created or modified by the superuser.")));
258 }
259 }
260
261 /*
262 * Check for duplicate name (more friendly than unique-index failure).
263 * Since this is just a friendliness check, we can just skip it in cases
264 * where there isn't suitable support.
265 */
266 if (classId == ProcedureRelationId)
267 {
268 Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(oldtup);
269
270 IsThereFunctionInNamespace(new_name, proc->pronargs,
271 &proc->proargtypes, proc->pronamespace);
272 }
273 else if (classId == CollationRelationId)
274 {
276
277 IsThereCollationInNamespace(new_name, coll->collnamespace);
278 }
279 else if (classId == OperatorClassRelationId)
280 {
282
283 IsThereOpClassInNamespace(new_name, opc->opcmethod,
284 opc->opcnamespace);
285 }
286 else if (classId == OperatorFamilyRelationId)
287 {
289
290 IsThereOpFamilyInNamespace(new_name, opf->opfmethod,
291 opf->opfnamespace);
292 }
293 else if (classId == SubscriptionRelationId)
294 {
295 if (SearchSysCacheExists2(SUBSCRIPTIONNAME,
297 CStringGetDatum(new_name)))
298 report_name_conflict(classId, new_name);
299
300 /* Also enforce regression testing naming rules, if enabled */
301#ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
302 if (strncmp(new_name, "regress_", 8) != 0)
303 elog(WARNING, "subscriptions created by regression test cases should have names starting with \"regress_\"");
304#endif
305
306 /* Wake up related replication workers to handle this change quickly */
308 }
309 else if (nameCacheId >= 0)
310 {
311 if (OidIsValid(namespaceId))
312 {
313 if (SearchSysCacheExists2(nameCacheId,
314 CStringGetDatum(new_name),
315 ObjectIdGetDatum(namespaceId)))
316 report_namespace_conflict(classId, new_name, namespaceId);
317 }
318 else
319 {
320 if (SearchSysCacheExists1(nameCacheId,
321 CStringGetDatum(new_name)))
322 report_name_conflict(classId, new_name);
323 }
324 }
325
326 /* Build modified tuple */
328 nulls = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
329 replaces = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
330 namestrcpy(&nameattrdata, new_name);
331 values[Anum_name - 1] = NameGetDatum(&nameattrdata);
332 replaces[Anum_name - 1] = true;
333 newtup = heap_modify_tuple(oldtup, RelationGetDescr(rel),
334 values, nulls, replaces);
335
336 /* Perform actual update */
337 CatalogTupleUpdate(rel, &oldtup->t_self, newtup);
338
339 InvokeObjectPostAlterHook(classId, objectId, 0);
340
341 /* Do post catalog-update tasks */
342 if (classId == PublicationRelationId)
343 {
345
346 /*
347 * Invalidate relsynccache entries.
348 *
349 * Unlike ALTER PUBLICATION ADD/SET/DROP commands, renaming a
350 * publication does not impact the publication status of tables. So,
351 * we don't need to invalidate relcache to rebuild the rd_pubdesc.
352 * Instead, we invalidate only the relsyncache.
353 */
354 InvalidatePubRelSyncCache(pub->oid, pub->puballtables);
355 }
356
357 /* Release memory */
358 pfree(values);
359 pfree(nulls);
360 pfree(replaces);
361 heap_freetuple(newtup);
362
363 ReleaseSysCache(oldtup);
364}
static void report_name_conflict(Oid classId, const char *name)
Definition: alter.c:76
void LogicalRepWorkersWakeupAtCommit(Oid subid)
Definition: worker.c:5115
char * get_database_name(Oid dbid)
Definition: dbcommands.c:3188
int errhint(const char *fmt,...)
Definition: elog.c:1317
#define WARNING
Definition: elog.h:36
Oid MyDatabaseId
Definition: globals.c:93
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
void namestrcpy(Name name, const char *str)
Definition: name.c:233
@ OBJECT_DATABASE
Definition: parsenodes.h:2321
FormData_pg_publication * Form_pg_publication
FormData_pg_subscription * Form_pg_subscription
static Datum NameGetDatum(const NameData *X)
Definition: postgres.h:378
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:355
void InvalidatePubRelSyncCache(Oid pubid, bool puballtables)
Definition: c.h:712
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:100

References ACL_CREATE, aclcheck_error(), ACLCHECK_NOT_OWNER, ACLCHECK_OK, Assert(), CatalogTupleUpdate(), CStringGetDatum(), DatumGetName(), DatumGetObjectId(), elog, ereport, errcode(), errhint(), errmsg(), ERROR, get_database_name(), 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, InvalidatePubRelSyncCache(), InvalidOid, InvokeObjectPostAlterHook, IsThereCollationInNamespace(), IsThereFunctionInNamespace(), IsThereOpClassInNamespace(), IsThereOpFamilyInNamespace(), LogicalRepWorkersWakeupAtCommit(), MyDatabaseId, NameGetDatum(), NameStr, namestrcpy(), object_aclcheck(), OBJECT_DATABASE, OBJECT_SCHEMA, ObjectIdGetDatum(), OidIsValid, palloc0(), pfree(), RelationGetDescr, RelationGetNumberOfAttributes, RelationGetRelationName, RelationGetRelid, ReleaseSysCache(), report_name_conflict(), report_namespace_conflict(), SearchSysCache1(), SearchSysCacheExists1, SearchSysCacheExists2, superuser(), HeapTupleData::t_self, values, and WARNING.

Referenced by ExecRenameStmt().

◆ ExecAlterObjectDependsStmt()

ObjectAddress ExecAlterObjectDependsStmt ( AlterObjectDependsStmt stmt,
ObjectAddress refAddress 
)

Definition at line 471 of file alter.c.

472{
473 ObjectAddress address;
474 ObjectAddress refAddr;
475 Relation rel;
476
477 address =
478 get_object_address_rv(stmt->objectType, stmt->relation, (List *) stmt->object,
479 &rel, AccessExclusiveLock, false);
480
481 /*
482 * Verify that the user is entitled to run the command.
483 *
484 * We don't check any privileges on the extension, because that's not
485 * needed. The object owner is stipulating, by running this command, that
486 * the extension owner can drop the object whenever they feel like it,
487 * which is not considered a problem.
488 */
490 stmt->objectType, address, stmt->object, rel);
491
492 /*
493 * If a relation was involved, it would have been opened and locked. We
494 * don't need the relation here, but we'll retain the lock until commit.
495 */
496 if (rel)
497 table_close(rel, NoLock);
498
499 refAddr = get_object_address(OBJECT_EXTENSION, (Node *) stmt->extname,
500 NULL, AccessExclusiveLock, false);
501 if (refAddress)
502 *refAddress = refAddr;
503
504 if (stmt->remove)
505 {
508 refAddr.classId, refAddr.objectId);
509 }
510 else
511 {
512 List *currexts;
513
514 /* Avoid duplicates */
515 currexts = getAutoExtensionsOfObject(address.classId,
516 address.objectId);
517 if (!list_member_oid(currexts, refAddr.objectId))
519 }
520
521 return address;
522}
@ DEPENDENCY_AUTO_EXTENSION
Definition: dependency.h:39
#define stmt
Definition: indent_codes.h:59
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:722
ObjectAddress get_object_address_rv(ObjectType objtype, RangeVar *rel, List *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
void check_object_ownership(Oid roleid, ObjectType objtype, ObjectAddress address, Node *object, Relation relation)
ObjectAddress get_object_address(ObjectType objtype, Node *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
@ OBJECT_EXTENSION
Definition: parsenodes.h:2327
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:45
long deleteDependencyRecordsForSpecific(Oid classId, Oid objectId, char deptype, Oid refclassId, Oid refobjectId)
Definition: pg_depend.c:398
List * getAutoExtensionsOfObject(Oid classId, Oid objectId)
Definition: pg_depend.c:778
Definition: pg_list.h:54
Definition: nodes.h:131

References AccessExclusiveLock, check_object_ownership(), ObjectAddress::classId, deleteDependencyRecordsForSpecific(), DEPENDENCY_AUTO_EXTENSION, get_object_address(), get_object_address_rv(), getAutoExtensionsOfObject(), GetUserId(), list_member_oid(), NoLock, OBJECT_EXTENSION, ObjectAddress::objectId, recordDependencyOn(), stmt, and table_close().

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

◆ ExecAlterObjectSchemaStmt()

ObjectAddress ExecAlterObjectSchemaStmt ( AlterObjectSchemaStmt stmt,
ObjectAddress oldSchemaAddr 
)

Definition at line 534 of file alter.c.

536{
537 ObjectAddress address;
538 Oid oldNspOid;
539
540 switch (stmt->objectType)
541 {
542 case OBJECT_EXTENSION:
543 address = AlterExtensionNamespace(strVal(stmt->object), stmt->newschema,
544 oldSchemaAddr ? &oldNspOid : NULL);
545 break;
546
548 case OBJECT_SEQUENCE:
549 case OBJECT_TABLE:
550 case OBJECT_VIEW:
551 case OBJECT_MATVIEW:
552 address = AlterTableNamespace(stmt,
553 oldSchemaAddr ? &oldNspOid : NULL);
554 break;
555
556 case OBJECT_DOMAIN:
557 case OBJECT_TYPE:
558 address = AlterTypeNamespace(castNode(List, stmt->object), stmt->newschema,
559 stmt->objectType,
560 oldSchemaAddr ? &oldNspOid : NULL);
561 break;
562
563 /* generic code path */
564 case OBJECT_AGGREGATE:
565 case OBJECT_COLLATION:
567 case OBJECT_FUNCTION:
568 case OBJECT_OPERATOR:
569 case OBJECT_OPCLASS:
570 case OBJECT_OPFAMILY:
571 case OBJECT_PROCEDURE:
572 case OBJECT_ROUTINE:
576 case OBJECT_TSPARSER:
578 {
579 Relation catalog;
580 Oid classId;
581 Oid nspOid;
582
583 address = get_object_address(stmt->objectType,
584 stmt->object,
585 NULL,
587 false);
588 classId = address.classId;
589 catalog = table_open(classId, RowExclusiveLock);
590 nspOid = LookupCreationNamespace(stmt->newschema);
591
592 oldNspOid = AlterObjectNamespace_internal(catalog, address.objectId,
593 nspOid);
595 }
596 break;
597
598 default:
599 elog(ERROR, "unrecognized AlterObjectSchemaStmt type: %d",
600 (int) stmt->objectType);
601 return InvalidObjectAddress; /* keep compiler happy */
602 }
603
604 if (oldSchemaAddr)
605 ObjectAddressSet(*oldSchemaAddr, NamespaceRelationId, oldNspOid);
606
607 return address;
608}
ObjectAddress AlterExtensionNamespace(const char *extensionName, const char *newschema, Oid *oldschema)
Definition: extension.c:2987
Oid LookupCreationNamespace(const char *nspname)
Definition: namespace.c:3428
#define castNode(_type_, nodeptr)
Definition: nodes.h:178
const ObjectAddress InvalidObjectAddress
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
@ OBJECT_TSPARSER
Definition: parsenodes.h:2359
@ OBJECT_COLLATION
Definition: parsenodes.h:2319
@ OBJECT_OPCLASS
Definition: parsenodes.h:2336
@ OBJECT_AGGREGATE
Definition: parsenodes.h:2313
@ OBJECT_MATVIEW
Definition: parsenodes.h:2335
@ OBJECT_OPERATOR
Definition: parsenodes.h:2337
@ OBJECT_FOREIGN_TABLE
Definition: parsenodes.h:2330
@ OBJECT_TSCONFIGURATION
Definition: parsenodes.h:2357
@ OBJECT_OPFAMILY
Definition: parsenodes.h:2338
@ OBJECT_DOMAIN
Definition: parsenodes.h:2324
@ OBJECT_ROUTINE
Definition: parsenodes.h:2346
@ OBJECT_PROCEDURE
Definition: parsenodes.h:2341
@ OBJECT_SEQUENCE
Definition: parsenodes.h:2349
@ OBJECT_TSTEMPLATE
Definition: parsenodes.h:2360
@ OBJECT_TSDICTIONARY
Definition: parsenodes.h:2358
@ OBJECT_CONVERSION
Definition: parsenodes.h:2320
@ OBJECT_TABLE
Definition: parsenodes.h:2353
@ OBJECT_VIEW
Definition: parsenodes.h:2363
@ OBJECT_TYPE
Definition: parsenodes.h:2361
@ OBJECT_FUNCTION
Definition: parsenodes.h:2331
@ OBJECT_STATISTIC_EXT
Definition: parsenodes.h:2351
ObjectAddress AlterTableNamespace(AlterObjectSchemaStmt *stmt, Oid *oldschema)
Definition: tablecmds.c:18213
ObjectAddress AlterTypeNamespace(List *names, const char *newschema, ObjectType objecttype, Oid *oldschema)
Definition: typecmds.c:4042
#define strVal(v)
Definition: value.h:82

References AccessExclusiveLock, AlterExtensionNamespace(), AlterObjectNamespace_internal(), AlterTableNamespace(), AlterTypeNamespace(), castNode, ObjectAddress::classId, elog, ERROR, get_object_address(), InvalidObjectAddress, LookupCreationNamespace(), 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, RowExclusiveLock, stmt, strVal, table_close(), and table_open().

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

◆ ExecAlterOwnerStmt()

ObjectAddress ExecAlterOwnerStmt ( AlterOwnerStmt stmt)

Definition at line 837 of file alter.c.

838{
839 Oid newowner = get_rolespec_oid(stmt->newowner, false);
840
841 switch (stmt->objectType)
842 {
843 case OBJECT_DATABASE:
844 return AlterDatabaseOwner(strVal(stmt->object), newowner);
845
846 case OBJECT_SCHEMA:
847 return AlterSchemaOwner(strVal(stmt->object), newowner);
848
849 case OBJECT_TYPE:
850 case OBJECT_DOMAIN: /* same as TYPE */
851 return AlterTypeOwner(castNode(List, stmt->object), newowner, stmt->objectType);
852 break;
853
854 case OBJECT_FDW:
856 newowner);
857
859 return AlterForeignServerOwner(strVal(stmt->object),
860 newowner);
861
863 return AlterEventTriggerOwner(strVal(stmt->object),
864 newowner);
865
867 return AlterPublicationOwner(strVal(stmt->object),
868 newowner);
869
871 return AlterSubscriptionOwner(strVal(stmt->object),
872 newowner);
873
874 /* Generic cases */
875 case OBJECT_AGGREGATE:
876 case OBJECT_COLLATION:
878 case OBJECT_FUNCTION:
879 case OBJECT_LANGUAGE:
881 case OBJECT_OPERATOR:
882 case OBJECT_OPCLASS:
883 case OBJECT_OPFAMILY:
884 case OBJECT_PROCEDURE:
885 case OBJECT_ROUTINE:
890 {
891 ObjectAddress address;
892
893 address = get_object_address(stmt->objectType,
894 stmt->object,
895 NULL,
897 false);
898
900 newowner);
901
902 return address;
903 }
904 break;
905
906 default:
907 elog(ERROR, "unrecognized AlterOwnerStmt type: %d",
908 (int) stmt->objectType);
909 return InvalidObjectAddress; /* keep compiler happy */
910 }
911}
Oid get_rolespec_oid(const RoleSpec *role, bool missing_ok)
Definition: acl.c:5570
void AlterObjectOwner_internal(Oid classId, Oid objectId, Oid new_ownerId)
Definition: alter.c:926
ObjectAddress AlterDatabaseOwner(const char *dbname, Oid newOwnerId)
Definition: dbcommands.c:2638
ObjectAddress AlterEventTriggerOwner(const char *name, Oid newOwnerId)
ObjectAddress AlterForeignServerOwner(const char *name, Oid newOwnerId)
Definition: foreigncmds.c:415
ObjectAddress AlterForeignDataWrapperOwner(const char *name, Oid newOwnerId)
Definition: foreigncmds.c:275
@ OBJECT_EVENT_TRIGGER
Definition: parsenodes.h:2326
@ OBJECT_FDW
Definition: parsenodes.h:2328
@ OBJECT_TABLESPACE
Definition: parsenodes.h:2354
@ OBJECT_LARGEOBJECT
Definition: parsenodes.h:2334
@ OBJECT_LANGUAGE
Definition: parsenodes.h:2333
@ OBJECT_FOREIGN_SERVER
Definition: parsenodes.h:2329
@ OBJECT_PUBLICATION
Definition: parsenodes.h:2342
@ OBJECT_SUBSCRIPTION
Definition: parsenodes.h:2350
ObjectAddress AlterPublicationOwner(const char *name, Oid newOwnerId)
ObjectAddress AlterSchemaOwner(const char *name, Oid newOwnerId)
Definition: schemacmds.c:330
ObjectAddress AlterSubscriptionOwner(const char *name, Oid newOwnerId)
ObjectAddress AlterTypeOwner(List *names, Oid newOwnerId, ObjectType objecttype)
Definition: typecmds.c:3809

References AccessExclusiveLock, AlterDatabaseOwner(), AlterEventTriggerOwner(), AlterForeignDataWrapperOwner(), AlterForeignServerOwner(), AlterObjectOwner_internal(), AlterPublicationOwner(), AlterSchemaOwner(), AlterSubscriptionOwner(), AlterTypeOwner(), castNode, ObjectAddress::classId, elog, ERROR, get_object_address(), get_rolespec_oid(), InvalidObjectAddress, 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, stmt, and strVal.

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

◆ ExecRenameStmt()

ObjectAddress ExecRenameStmt ( RenameStmt stmt)

Definition at line 373 of file alter.c.

374{
375 switch (stmt->renameType)
376 {
379 return RenameConstraint(stmt);
380
381 case OBJECT_DATABASE:
382 return RenameDatabase(stmt->subname, stmt->newname);
383
384 case OBJECT_ROLE:
385 return RenameRole(stmt->subname, stmt->newname);
386
387 case OBJECT_SCHEMA:
388 return RenameSchema(stmt->subname, stmt->newname);
389
391 return RenameTableSpace(stmt->subname, stmt->newname);
392
393 case OBJECT_TABLE:
394 case OBJECT_SEQUENCE:
395 case OBJECT_VIEW:
396 case OBJECT_MATVIEW:
397 case OBJECT_INDEX:
399 return RenameRelation(stmt);
400
401 case OBJECT_COLUMN:
402 case OBJECT_ATTRIBUTE:
403 return renameatt(stmt);
404
405 case OBJECT_RULE:
406 return RenameRewriteRule(stmt->relation, stmt->subname,
407 stmt->newname);
408
409 case OBJECT_TRIGGER:
410 return renametrig(stmt);
411
412 case OBJECT_POLICY:
413 return rename_policy(stmt);
414
415 case OBJECT_DOMAIN:
416 case OBJECT_TYPE:
417 return RenameType(stmt);
418
419 case OBJECT_AGGREGATE:
420 case OBJECT_COLLATION:
423 case OBJECT_FDW:
425 case OBJECT_FUNCTION:
426 case OBJECT_OPCLASS:
427 case OBJECT_OPFAMILY:
428 case OBJECT_LANGUAGE:
429 case OBJECT_PROCEDURE:
430 case OBJECT_ROUTINE:
434 case OBJECT_TSPARSER:
438 {
439 ObjectAddress address;
440 Relation catalog;
441
442 address = get_object_address(stmt->renameType,
443 stmt->object,
444 NULL,
445 AccessExclusiveLock, false);
446
447 catalog = table_open(address.classId, RowExclusiveLock);
449 address.objectId,
450 stmt->newname);
452
453 return address;
454 }
455
456 default:
457 elog(ERROR, "unrecognized rename stmt type: %d",
458 (int) stmt->renameType);
459 return InvalidObjectAddress; /* keep compiler happy */
460 }
461}
static void AlterObjectRename_internal(Relation rel, Oid objectId, const char *new_name)
Definition: alter.c:165
ObjectAddress RenameTableSpace(const char *oldname, const char *newname)
Definition: tablespace.c:930
ObjectAddress RenameDatabase(const char *oldname, const char *newname)
Definition: dbcommands.c:1877
@ OBJECT_POLICY
Definition: parsenodes.h:2340
@ OBJECT_COLUMN
Definition: parsenodes.h:2318
@ OBJECT_ROLE
Definition: parsenodes.h:2345
@ OBJECT_INDEX
Definition: parsenodes.h:2332
@ OBJECT_ATTRIBUTE
Definition: parsenodes.h:2316
@ OBJECT_RULE
Definition: parsenodes.h:2347
@ OBJECT_TABCONSTRAINT
Definition: parsenodes.h:2352
@ OBJECT_DOMCONSTRAINT
Definition: parsenodes.h:2325
@ OBJECT_TRIGGER
Definition: parsenodes.h:2356
ObjectAddress rename_policy(RenameStmt *stmt)
Definition: policy.c:1096
ObjectAddress RenameRewriteRule(RangeVar *relation, const char *oldName, const char *newName)
ObjectAddress RenameSchema(const char *oldname, const char *newname)
Definition: schemacmds.c:249
ObjectAddress RenameRelation(RenameStmt *stmt)
Definition: tablecmds.c:4167
ObjectAddress renameatt(RenameStmt *stmt)
Definition: tablecmds.c:3970
ObjectAddress RenameConstraint(RenameStmt *stmt)
Definition: tablecmds.c:4117
ObjectAddress renametrig(RenameStmt *stmt)
Definition: trigger.c:1467
ObjectAddress RenameType(RenameStmt *stmt)
Definition: typecmds.c:3728
ObjectAddress RenameRole(const char *oldname, const char *newname)
Definition: user.c:1334

References AccessExclusiveLock, AlterObjectRename_internal(), ObjectAddress::classId, elog, ERROR, get_object_address(), InvalidObjectAddress, 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, rename_policy(), renameatt(), RenameConstraint(), RenameDatabase(), RenameRelation(), RenameRewriteRule(), RenameRole(), RenameSchema(), RenameTableSpace(), renametrig(), RenameType(), RowExclusiveLock, stmt, table_close(), and table_open().

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

◆ report_name_conflict()

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

Definition at line 76 of file alter.c.

77{
78 char *msgfmt;
79
80 switch (classId)
81 {
82 case EventTriggerRelationId:
83 msgfmt = gettext_noop("event trigger \"%s\" already exists");
84 break;
85 case ForeignDataWrapperRelationId:
86 msgfmt = gettext_noop("foreign-data wrapper \"%s\" already exists");
87 break;
88 case ForeignServerRelationId:
89 msgfmt = gettext_noop("server \"%s\" already exists");
90 break;
91 case LanguageRelationId:
92 msgfmt = gettext_noop("language \"%s\" already exists");
93 break;
94 case PublicationRelationId:
95 msgfmt = gettext_noop("publication \"%s\" already exists");
96 break;
97 case SubscriptionRelationId:
98 msgfmt = gettext_noop("subscription \"%s\" already exists");
99 break;
100 default:
101 elog(ERROR, "unsupported object class: %u", classId);
102 break;
103 }
104
107 errmsg(msgfmt, name)));
108}
#define gettext_noop(x)
Definition: c.h:1167
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:30

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

Referenced by AlterObjectRename_internal().

◆ report_namespace_conflict()

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

Definition at line 111 of file alter.c.

112{
113 char *msgfmt;
114
115 Assert(OidIsValid(nspOid));
116
117 switch (classId)
118 {
119 case ConversionRelationId:
120 Assert(OidIsValid(nspOid));
121 msgfmt = gettext_noop("conversion \"%s\" already exists in schema \"%s\"");
122 break;
123 case StatisticExtRelationId:
124 Assert(OidIsValid(nspOid));
125 msgfmt = gettext_noop("statistics object \"%s\" already exists in schema \"%s\"");
126 break;
127 case TSParserRelationId:
128 Assert(OidIsValid(nspOid));
129 msgfmt = gettext_noop("text search parser \"%s\" already exists in schema \"%s\"");
130 break;
131 case TSDictionaryRelationId:
132 Assert(OidIsValid(nspOid));
133 msgfmt = gettext_noop("text search dictionary \"%s\" already exists in schema \"%s\"");
134 break;
135 case TSTemplateRelationId:
136 Assert(OidIsValid(nspOid));
137 msgfmt = gettext_noop("text search template \"%s\" already exists in schema \"%s\"");
138 break;
139 case TSConfigRelationId:
140 Assert(OidIsValid(nspOid));
141 msgfmt = gettext_noop("text search configuration \"%s\" already exists in schema \"%s\"");
142 break;
143 default:
144 elog(ERROR, "unsupported object class: %u", classId);
145 break;
146 }
147
150 errmsg(msgfmt, name, get_namespace_name(nspOid))));
151}

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

Referenced by AlterObjectNamespace_internal(), and AlterObjectRename_internal().