mirror of
https://github.com/postgres/postgres.git
synced 2025-10-25 13:17:41 +03:00
Add more ALTER <object> .. SET SCHEMA commands.
This adds support for changing the schema of a conversion, operator, operator class, operator family, text search configuration, text search dictionary, text search parser, or text search template. Dimitri Fontaine, with assorted corrections and other kibitzing.
This commit is contained in:
@@ -14,8 +14,11 @@
|
||||
*/
|
||||
#include "postgres.h"
|
||||
|
||||
#include "catalog/dependency.h"
|
||||
#include "catalog/indexing.h"
|
||||
#include "catalog/namespace.h"
|
||||
#include "catalog/pg_largeobject.h"
|
||||
#include "catalog/pg_namespace.h"
|
||||
#include "commands/alter.h"
|
||||
#include "commands/conversioncmds.h"
|
||||
#include "commands/dbcommands.h"
|
||||
@@ -33,6 +36,7 @@
|
||||
#include "utils/acl.h"
|
||||
#include "utils/builtins.h"
|
||||
#include "utils/lsyscache.h"
|
||||
#include "utils/syscache.h"
|
||||
|
||||
|
||||
/*
|
||||
@@ -178,11 +182,27 @@ ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt)
|
||||
stmt->newschema);
|
||||
break;
|
||||
|
||||
case OBJECT_CONVERSION:
|
||||
AlterConversionNamespace(stmt->object, stmt->newschema);
|
||||
break;
|
||||
|
||||
case OBJECT_FUNCTION:
|
||||
AlterFunctionNamespace(stmt->object, stmt->objarg, false,
|
||||
stmt->newschema);
|
||||
break;
|
||||
|
||||
case OBJECT_OPERATOR:
|
||||
AlterOperatorNamespace(stmt->object, stmt->objarg, stmt->newschema);
|
||||
break;
|
||||
|
||||
case OBJECT_OPCLASS:
|
||||
AlterOpClassNamespace(stmt->object, stmt->objarg, stmt->newschema);
|
||||
break;
|
||||
|
||||
case OBJECT_OPFAMILY:
|
||||
AlterOpFamilyNamespace(stmt->object, stmt->objarg, stmt->newschema);
|
||||
break;
|
||||
|
||||
case OBJECT_SEQUENCE:
|
||||
case OBJECT_TABLE:
|
||||
case OBJECT_VIEW:
|
||||
@@ -191,6 +211,22 @@ ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt)
|
||||
stmt->objectType, AccessExclusiveLock);
|
||||
break;
|
||||
|
||||
case OBJECT_TSPARSER:
|
||||
AlterTSParserNamespace(stmt->object, stmt->newschema);
|
||||
break;
|
||||
|
||||
case OBJECT_TSDICTIONARY:
|
||||
AlterTSDictionaryNamespace(stmt->object, stmt->newschema);
|
||||
break;
|
||||
|
||||
case OBJECT_TSTEMPLATE:
|
||||
AlterTSTemplateNamespace(stmt->object, stmt->newschema);
|
||||
break;
|
||||
|
||||
case OBJECT_TSCONFIGURATION:
|
||||
AlterTSConfigurationNamespace(stmt->object, stmt->newschema);
|
||||
break;
|
||||
|
||||
case OBJECT_TYPE:
|
||||
case OBJECT_DOMAIN:
|
||||
AlterTypeNamespace(stmt->object, stmt->newschema);
|
||||
@@ -202,6 +238,104 @@ ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Generic function to change the namespace of a given object, for simple
|
||||
* cases (won't work for tables or functions, objects which have more than 2
|
||||
* key-attributes to use when searching for their syscache entries --- we
|
||||
* don't want nor need to get this generic here).
|
||||
*
|
||||
* The AlterFooNamespace() calls just above will call a function whose job
|
||||
* is to lookup the arguments for the generic function here.
|
||||
*
|
||||
* Relation must already by open, it's the responsibility of the caller to
|
||||
* close it.
|
||||
*/
|
||||
void
|
||||
AlterObjectNamespace(Relation rel, int cacheId,
|
||||
Oid classId, Oid objid, Oid nspOid,
|
||||
int Anum_name, int Anum_namespace, int Anum_owner,
|
||||
AclObjectKind acl_kind,
|
||||
bool superuser_only)
|
||||
{
|
||||
Oid oldNspOid;
|
||||
Datum name, namespace;
|
||||
bool isnull;
|
||||
HeapTuple tup, newtup = NULL;
|
||||
Datum *values;
|
||||
bool *nulls;
|
||||
bool *replaces;
|
||||
|
||||
tup = SearchSysCacheCopy1(cacheId, ObjectIdGetDatum(objid));
|
||||
if (!HeapTupleIsValid(tup)) /* should not happen */
|
||||
elog(ERROR, "cache lookup failed for object %u: %s",
|
||||
objid, getObjectDescriptionOids(classId, objid));
|
||||
|
||||
name = heap_getattr(tup, Anum_name, rel->rd_att, &isnull);
|
||||
namespace = heap_getattr(tup, Anum_namespace, rel->rd_att, &isnull);
|
||||
oldNspOid = DatumGetObjectId(namespace);
|
||||
|
||||
/* Check basic namespace related issues */
|
||||
CheckSetNamespace(oldNspOid, nspOid, classId, objid);
|
||||
|
||||
/* check for duplicate name (more friendly than unique-index failure) */
|
||||
if (SearchSysCacheExists2(cacheId, name, ObjectIdGetDatum(nspOid)))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
||||
errmsg("%s already exists in schema \"%s\"",
|
||||
getObjectDescriptionOids(classId, objid),
|
||||
get_namespace_name(nspOid))));
|
||||
|
||||
/* Superusers can always do it */
|
||||
if (!superuser())
|
||||
{
|
||||
Datum owner;
|
||||
Oid ownerId;
|
||||
AclResult aclresult;
|
||||
|
||||
if (superuser_only)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
||||
(errmsg("must be superuser to SET SCHEMA of %s",
|
||||
getObjectDescriptionOids(classId, objid)))));
|
||||
|
||||
/* Otherwise, must be owner of the existing object */
|
||||
owner = heap_getattr(tup, Anum_owner, rel->rd_att, &isnull);
|
||||
ownerId = DatumGetObjectId(owner);
|
||||
|
||||
if (!has_privs_of_role(GetUserId(), ownerId))
|
||||
aclcheck_error(ACLCHECK_NOT_OWNER, acl_kind,
|
||||
NameStr(*(DatumGetName(name))));
|
||||
|
||||
/* owner must have CREATE privilege on namespace */
|
||||
aclresult = pg_namespace_aclcheck(oldNspOid, GetUserId(), ACL_CREATE);
|
||||
if (aclresult != ACLCHECK_OK)
|
||||
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
|
||||
get_namespace_name(oldNspOid));
|
||||
}
|
||||
|
||||
/* Prepare to update tuple */
|
||||
values = palloc0(rel->rd_att->natts * sizeof(Datum));
|
||||
nulls = palloc0(rel->rd_att->natts * sizeof(bool));
|
||||
replaces = palloc0(rel->rd_att->natts * sizeof(bool));
|
||||
values[Anum_namespace - 1] = nspOid;
|
||||
replaces[Anum_namespace - 1] = true;
|
||||
newtup = heap_modify_tuple(tup, rel->rd_att, values, nulls, replaces);
|
||||
|
||||
/* Perform actual update */
|
||||
simple_heap_update(rel, &tup->t_self, newtup);
|
||||
CatalogUpdateIndexes(rel, newtup);
|
||||
|
||||
/* Release memory */
|
||||
pfree(values);
|
||||
pfree(nulls);
|
||||
pfree(replaces);
|
||||
|
||||
/* update dependencies to point to the new schema */
|
||||
changeDependencyFor(classId, objid,
|
||||
NamespaceRelationId, oldNspOid, nspOid);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Executes an ALTER OBJECT / OWNER TO statement. Based on the object
|
||||
* type, the function appropriate to that type is executed.
|
||||
|
||||
@@ -19,7 +19,9 @@
|
||||
#include "catalog/indexing.h"
|
||||
#include "catalog/pg_conversion.h"
|
||||
#include "catalog/pg_conversion_fn.h"
|
||||
#include "catalog/pg_namespace.h"
|
||||
#include "catalog/pg_type.h"
|
||||
#include "commands/alter.h"
|
||||
#include "commands/conversioncmds.h"
|
||||
#include "mb/pg_wchar.h"
|
||||
#include "miscadmin.h"
|
||||
@@ -326,3 +328,29 @@ AlterConversionOwner_internal(Relation rel, Oid conversionOid, Oid newOwnerId)
|
||||
|
||||
heap_freetuple(tup);
|
||||
}
|
||||
|
||||
/*
|
||||
* Execute ALTER CONVERSION SET SCHEMA
|
||||
*/
|
||||
void
|
||||
AlterConversionNamespace(List *name, const char *newschema)
|
||||
{
|
||||
Oid convOid, nspOid;
|
||||
Relation rel;
|
||||
|
||||
rel = heap_open(ConversionRelationId, RowExclusiveLock);
|
||||
|
||||
convOid = get_conversion_oid(name, false);
|
||||
|
||||
/* get schema OID */
|
||||
nspOid = LookupCreationNamespace(newschema);
|
||||
|
||||
AlterObjectNamespace(rel, CONVOID, ConversionRelationId, convOid, nspOid,
|
||||
Anum_pg_conversion_conname,
|
||||
Anum_pg_conversion_connamespace,
|
||||
Anum_pg_conversion_conowner,
|
||||
ACL_KIND_CONVERSION,
|
||||
false);
|
||||
|
||||
heap_close(rel, NoLock);
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include "catalog/pg_opfamily.h"
|
||||
#include "catalog/pg_proc.h"
|
||||
#include "catalog/pg_type.h"
|
||||
#include "commands/alter.h"
|
||||
#include "commands/defrem.h"
|
||||
#include "miscadmin.h"
|
||||
#include "parser/parse_func.h"
|
||||
@@ -1988,6 +1989,41 @@ AlterOpClassOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* ALTER OPERATOR CLASS any_name USING access_method SET SCHEMA name
|
||||
*/
|
||||
void
|
||||
AlterOpClassNamespace(List *name, List *argam, const char *newschema)
|
||||
{
|
||||
Oid amOid;
|
||||
char *access_method = linitial(argam);
|
||||
Relation rel;
|
||||
Oid oid;
|
||||
Oid nspOid;
|
||||
|
||||
Assert(list_length(argam) == 1);
|
||||
|
||||
amOid = get_am_oid(access_method, false);
|
||||
|
||||
rel = heap_open(OperatorClassRelationId, RowExclusiveLock);
|
||||
|
||||
/* Look up the opclass. */
|
||||
oid = get_opclass_oid(amOid, name, false);
|
||||
|
||||
/* get schema OID */
|
||||
nspOid = LookupCreationNamespace(newschema);
|
||||
|
||||
AlterObjectNamespace(rel, CLAOID, OperatorClassRelationId,
|
||||
oid, nspOid,
|
||||
Anum_pg_opfamily_opfname,
|
||||
Anum_pg_opfamily_opfnamespace,
|
||||
Anum_pg_opfamily_opfowner,
|
||||
ACL_KIND_OPCLASS,
|
||||
false);
|
||||
|
||||
heap_close(rel, NoLock);
|
||||
}
|
||||
|
||||
/*
|
||||
* Change opfamily owner by name
|
||||
*/
|
||||
@@ -2144,3 +2180,37 @@ get_am_oid(const char *amname, bool missing_ok)
|
||||
errmsg("access method \"%s\" does not exist", amname)));
|
||||
return oid;
|
||||
}
|
||||
|
||||
/*
|
||||
* ALTER OPERATOR FAMILY any_name USING access_method SET SCHEMA name
|
||||
*/
|
||||
void
|
||||
AlterOpFamilyNamespace(List *name, List *argam, const char *newschema)
|
||||
{
|
||||
Oid amOid;
|
||||
char *access_method = linitial(argam);
|
||||
Relation rel;
|
||||
Oid nspOid;
|
||||
Oid oid;
|
||||
|
||||
Assert(list_length(argam) == 1);
|
||||
amOid = get_am_oid(access_method, false);
|
||||
|
||||
rel = heap_open(OperatorFamilyRelationId, RowExclusiveLock);
|
||||
|
||||
/* Look up the opfamily */
|
||||
oid = get_opfamily_oid(amOid, name, false);
|
||||
|
||||
/* get schema OID */
|
||||
nspOid = LookupCreationNamespace(newschema);
|
||||
|
||||
AlterObjectNamespace(rel, OPFAMILYOID, OperatorFamilyRelationId,
|
||||
oid, nspOid,
|
||||
Anum_pg_opfamily_opfname,
|
||||
Anum_pg_opfamily_opfnamespace,
|
||||
Anum_pg_opfamily_opfowner,
|
||||
ACL_KIND_OPFAMILY,
|
||||
false);
|
||||
|
||||
heap_close(rel, NoLock);
|
||||
}
|
||||
|
||||
@@ -39,7 +39,9 @@
|
||||
#include "catalog/indexing.h"
|
||||
#include "catalog/namespace.h"
|
||||
#include "catalog/pg_operator.h"
|
||||
#include "catalog/pg_namespace.h"
|
||||
#include "catalog/pg_type.h"
|
||||
#include "commands/alter.h"
|
||||
#include "commands/defrem.h"
|
||||
#include "miscadmin.h"
|
||||
#include "parser/parse_func.h"
|
||||
@@ -452,3 +454,35 @@ AlterOperatorOwner_internal(Relation rel, Oid operOid, Oid newOwnerId)
|
||||
|
||||
heap_freetuple(tup);
|
||||
}
|
||||
|
||||
/*
|
||||
* Execute ALTER OPERATOR SET SCHEMA
|
||||
*/
|
||||
void
|
||||
AlterOperatorNamespace(List *names, List *argtypes, const char *newschema)
|
||||
{
|
||||
List *operatorName = names;
|
||||
TypeName *typeName1 = (TypeName *) linitial(argtypes);
|
||||
TypeName *typeName2 = (TypeName *) lsecond(argtypes);
|
||||
Oid operOid, nspOid;
|
||||
Relation rel;
|
||||
|
||||
rel = heap_open(OperatorRelationId, RowExclusiveLock);
|
||||
|
||||
Assert(list_length(argtypes) == 2);
|
||||
operOid = LookupOperNameTypeNames(NULL, operatorName,
|
||||
typeName1, typeName2,
|
||||
false, -1);
|
||||
|
||||
/* get schema OID */
|
||||
nspOid = LookupCreationNamespace(newschema);
|
||||
|
||||
AlterObjectNamespace(rel, OPEROID, OperatorRelationId, operOid, nspOid,
|
||||
Anum_pg_operator_oprname,
|
||||
Anum_pg_operator_oprnamespace,
|
||||
Anum_pg_operator_oprowner,
|
||||
ACL_KIND_OPER,
|
||||
false);
|
||||
|
||||
heap_close(rel, NoLock);
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "catalog/pg_ts_parser.h"
|
||||
#include "catalog/pg_ts_template.h"
|
||||
#include "catalog/pg_type.h"
|
||||
#include "commands/alter.h"
|
||||
#include "commands/defrem.h"
|
||||
#include "miscadmin.h"
|
||||
#include "nodes/makefuncs.h"
|
||||
@@ -397,6 +398,30 @@ RenameTSParser(List *oldname, const char *newname)
|
||||
heap_freetuple(tup);
|
||||
}
|
||||
|
||||
/*
|
||||
* ALTER TEXT SEARCH PARSER any_name SET SCHEMA name
|
||||
*/
|
||||
void
|
||||
AlterTSParserNamespace(List *name, const char *newschema)
|
||||
{
|
||||
Oid prsId, nspOid;
|
||||
Relation rel;
|
||||
|
||||
rel = heap_open(TSParserRelationId, RowExclusiveLock);
|
||||
|
||||
prsId = get_ts_parser_oid(name, false);
|
||||
|
||||
/* get schema OID */
|
||||
nspOid = LookupCreationNamespace(newschema);
|
||||
|
||||
AlterObjectNamespace(rel, TSPARSEROID, TSParserRelationId, prsId, nspOid,
|
||||
Anum_pg_ts_parser_prsname,
|
||||
Anum_pg_ts_parser_prsnamespace,
|
||||
-1, -1, true);
|
||||
|
||||
heap_close(rel, NoLock);
|
||||
}
|
||||
|
||||
/* ---------------------- TS Dictionary commands -----------------------*/
|
||||
|
||||
/*
|
||||
@@ -627,6 +652,32 @@ RenameTSDictionary(List *oldname, const char *newname)
|
||||
heap_freetuple(tup);
|
||||
}
|
||||
|
||||
/*
|
||||
* ALTER TEXT SEARCH DICTIONARY any_name SET SCHEMA name
|
||||
*/
|
||||
void
|
||||
AlterTSDictionaryNamespace(List *name, const char *newschema)
|
||||
{
|
||||
Oid dictId, nspOid;
|
||||
Relation rel;
|
||||
|
||||
rel = heap_open(TSDictionaryRelationId, RowExclusiveLock);
|
||||
|
||||
dictId = get_ts_dict_oid(name, false);
|
||||
|
||||
/* get schema OID */
|
||||
nspOid = LookupCreationNamespace(newschema);
|
||||
|
||||
AlterObjectNamespace(rel, TSDICTOID, TSDictionaryRelationId, dictId, nspOid,
|
||||
Anum_pg_ts_dict_dictname,
|
||||
Anum_pg_ts_dict_dictnamespace,
|
||||
Anum_pg_ts_dict_dictowner,
|
||||
ACL_KIND_TSDICTIONARY,
|
||||
true);
|
||||
|
||||
heap_close(rel, NoLock);
|
||||
}
|
||||
|
||||
/*
|
||||
* DROP TEXT SEARCH DICTIONARY
|
||||
*/
|
||||
@@ -1110,6 +1161,31 @@ RenameTSTemplate(List *oldname, const char *newname)
|
||||
heap_freetuple(tup);
|
||||
}
|
||||
|
||||
/*
|
||||
* ALTER TEXT SEARCH TEMPLATE any_name SET SCHEMA name
|
||||
*/
|
||||
void
|
||||
AlterTSTemplateNamespace(List *name, const char *newschema)
|
||||
{
|
||||
Oid tmplId, nspOid;
|
||||
Relation rel;
|
||||
|
||||
rel = heap_open(TSTemplateRelationId, RowExclusiveLock);
|
||||
|
||||
tmplId = get_ts_template_oid(name, false);
|
||||
|
||||
/* get schema OID */
|
||||
nspOid = LookupCreationNamespace(newschema);
|
||||
|
||||
AlterObjectNamespace(rel, TSTEMPLATEOID, TSTemplateRelationId,
|
||||
tmplId, nspOid,
|
||||
Anum_pg_ts_template_tmplname,
|
||||
Anum_pg_ts_template_tmplnamespace,
|
||||
-1, -1, true);
|
||||
|
||||
heap_close(rel, NoLock);
|
||||
}
|
||||
|
||||
/*
|
||||
* DROP TEXT SEARCH TEMPLATE
|
||||
*/
|
||||
@@ -1511,6 +1587,32 @@ RenameTSConfiguration(List *oldname, const char *newname)
|
||||
heap_freetuple(tup);
|
||||
}
|
||||
|
||||
/*
|
||||
* ALTER TEXT SEARCH CONFIGURATION any_name SET SCHEMA name
|
||||
*/
|
||||
void
|
||||
AlterTSConfigurationNamespace(List *name, const char *newschema)
|
||||
{
|
||||
Oid cfgId, nspOid;
|
||||
Relation rel;
|
||||
|
||||
rel = heap_open(TSConfigRelationId, RowExclusiveLock);
|
||||
|
||||
cfgId = get_ts_config_oid(name, false);
|
||||
|
||||
/* get schema OID */
|
||||
nspOid = LookupCreationNamespace(newschema);
|
||||
|
||||
AlterObjectNamespace(rel, TSCONFIGOID, TSConfigRelationId, cfgId, nspOid,
|
||||
Anum_pg_ts_config_cfgname,
|
||||
Anum_pg_ts_config_cfgnamespace,
|
||||
Anum_pg_ts_config_cfgowner,
|
||||
ACL_KIND_TSCONFIGURATION,
|
||||
false);
|
||||
|
||||
heap_close(rel, NoLock);
|
||||
}
|
||||
|
||||
/*
|
||||
* DROP TEXT SEARCH CONFIGURATION
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user