Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2164f9a

Browse files
committed
Refactor "ALTER some-obj SET SCHEMA" implementation
Instead of having each object type implement the catalog mungingindependently, centralize knowledge about how to do it and expand theexisting table in objectaddress.c with enough data about each objecttype to support this operation.Author: KaiGai KoheiTweaks by meReviewed by Robert Haas
1 parenta563d94 commit2164f9a

File tree

17 files changed

+316
-552
lines changed

17 files changed

+316
-552
lines changed

‎src/backend/catalog/objectaddress.c

Lines changed: 241 additions & 27 deletions
Large diffs are not rendered by default.

‎src/backend/commands/alter.c

Lines changed: 50 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,6 @@ ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt)
173173
AlterCollationNamespace(stmt->object,stmt->newschema);
174174
break;
175175

176-
caseOBJECT_CONVERSION:
177-
AlterConversionNamespace(stmt->object,stmt->newschema);
178-
break;
179-
180176
caseOBJECT_EXTENSION:
181177
AlterExtensionNamespace(stmt->object,stmt->newschema);
182178
break;
@@ -186,44 +182,49 @@ ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt)
186182
stmt->newschema);
187183
break;
188184

189-
caseOBJECT_OPERATOR:
190-
AlterOperatorNamespace(stmt->object,stmt->objarg,stmt->newschema);
191-
break;
192-
193-
caseOBJECT_OPCLASS:
194-
AlterOpClassNamespace(stmt->object,stmt->addname,stmt->newschema);
195-
break;
196-
197-
caseOBJECT_OPFAMILY:
198-
AlterOpFamilyNamespace(stmt->object,stmt->addname,stmt->newschema);
199-
break;
200-
201185
caseOBJECT_SEQUENCE:
202186
caseOBJECT_TABLE:
203187
caseOBJECT_VIEW:
204188
caseOBJECT_FOREIGN_TABLE:
205189
AlterTableNamespace(stmt);
206190
break;
207191

208-
caseOBJECT_TSPARSER:
209-
AlterTSParserNamespace(stmt->object,stmt->newschema);
192+
caseOBJECT_TYPE:
193+
caseOBJECT_DOMAIN:
194+
AlterTypeNamespace(stmt->object,stmt->newschema,stmt->objectType);
210195
break;
211196

197+
/* generic code path */
198+
caseOBJECT_CONVERSION:
199+
caseOBJECT_OPERATOR:
200+
caseOBJECT_OPCLASS:
201+
caseOBJECT_OPFAMILY:
202+
caseOBJECT_TSPARSER:
212203
caseOBJECT_TSDICTIONARY:
213-
AlterTSDictionaryNamespace(stmt->object,stmt->newschema);
214-
break;
215-
216204
caseOBJECT_TSTEMPLATE:
217-
AlterTSTemplateNamespace(stmt->object,stmt->newschema);
218-
break;
219-
220205
caseOBJECT_TSCONFIGURATION:
221-
AlterTSConfigurationNamespace(stmt->object,stmt->newschema);
222-
break;
223-
224-
caseOBJECT_TYPE:
225-
caseOBJECT_DOMAIN:
226-
AlterTypeNamespace(stmt->object,stmt->newschema,stmt->objectType);
206+
{
207+
Relationcatalog;
208+
Relationrelation;
209+
OidclassId;
210+
OidnspOid;
211+
ObjectAddressaddress;
212+
213+
address=get_object_address(stmt->objectType,
214+
stmt->object,
215+
stmt->objarg,
216+
&relation,
217+
AccessExclusiveLock,
218+
false);
219+
Assert(relation==NULL);
220+
classId=address.classId;
221+
catalog=heap_open(classId,RowExclusiveLock);
222+
nspOid=LookupCreationNamespace(stmt->newschema);
223+
224+
AlterObjectNamespace_internal(catalog,address.objectId,
225+
nspOid);
226+
heap_close(catalog,RowExclusiveLock);
227+
}
227228
break;
228229

229230
default:
@@ -293,35 +294,23 @@ AlterObjectNamespace_oid(Oid classId, Oid objid, Oid nspOid)
293294
break;
294295

295296
caseOCLASS_CONVERSION:
296-
oldNspOid=AlterConversionNamespace_oid(objid,nspOid);
297-
break;
298-
299297
caseOCLASS_OPERATOR:
300-
oldNspOid=AlterOperatorNamespace_oid(objid,nspOid);
301-
break;
302-
303298
caseOCLASS_OPCLASS:
304-
oldNspOid=AlterOpClassNamespace_oid(objid,nspOid);
305-
break;
306-
307299
caseOCLASS_OPFAMILY:
308-
oldNspOid=AlterOpFamilyNamespace_oid(objid,nspOid);
309-
break;
310-
311300
caseOCLASS_TSPARSER:
312-
oldNspOid=AlterTSParserNamespace_oid(objid,nspOid);
313-
break;
314-
315301
caseOCLASS_TSDICT:
316-
oldNspOid=AlterTSDictionaryNamespace_oid(objid,nspOid);
317-
break;
318-
319302
caseOCLASS_TSTEMPLATE:
320-
oldNspOid=AlterTSTemplateNamespace_oid(objid,nspOid);
321-
break;
322-
323303
caseOCLASS_TSCONFIG:
324-
oldNspOid=AlterTSConfigurationNamespace_oid(objid,nspOid);
304+
{
305+
Relationcatalog;
306+
307+
catalog=heap_open(classId,RowExclusiveLock);
308+
309+
oldNspOid=AlterObjectNamespace_internal(catalog,objid,
310+
nspOid);
311+
312+
heap_close(catalog,RowExclusiveLock);
313+
}
325314
break;
326315

327316
default:
@@ -336,32 +325,22 @@ AlterObjectNamespace_oid(Oid classId, Oid objid, Oid nspOid)
336325
* cases (won't work for tables, nor other cases where we need to do more
337326
* than change the namespace column of a single catalog entry).
338327
*
339-
* The AlterFooNamespace() calls just above will call a function whose job
340-
* is to lookup the arguments for the generic function here.
341-
*
342328
* rel: catalog relation containing object (RowExclusiveLock'd by caller)
343-
* oidCacheId: syscache that indexes this catalog by OID
344-
* nameCacheId: syscache that indexes this catalog by name and namespace
345-
*(pass -1 if there is none)
346329
* objid: OID of object to change the namespace of
347330
* nspOid: OID of new namespace
348-
* Anum_name: column number of catalog's name column
349-
* Anum_namespace: column number of catalog's namespace column
350-
* Anum_owner: column number of catalog's owner column, or -1 if none
351-
* acl_kind: ACL type for object, or -1 if none assigned
352-
*
353-
* If the object does not have an owner or permissions, pass -1 for
354-
* Anum_owner and acl_kind. In this case the calling user must be superuser.
355331
*
356332
* Returns the OID of the object's previous namespace.
357333
*/
358334
Oid
359-
AlterObjectNamespace(Relationrel,intoidCacheId,intnameCacheId,
360-
Oidobjid,OidnspOid,
361-
intAnum_name,intAnum_namespace,intAnum_owner,
362-
AclObjectKindacl_kind)
335+
AlterObjectNamespace_internal(Relationrel,Oidobjid,OidnspOid)
363336
{
364337
OidclassId=RelationGetRelid(rel);
338+
intoidCacheId=get_object_catcache_oid(classId);
339+
intnameCacheId=get_object_catcache_name(classId);
340+
AttrNumberAnum_name=get_object_attnum_name(classId);
341+
AttrNumberAnum_namespace=get_object_attnum_namespace(classId);
342+
AttrNumberAnum_owner=get_object_attnum_owner(classId);
343+
AclObjectKindacl_kind=get_object_aclkind(classId);
365344
OidoldNspOid;
366345
Datumname,
367346
namespace;
@@ -379,7 +358,8 @@ AlterObjectNamespace(Relation rel, int oidCacheId, int nameCacheId,
379358

380359
name=heap_getattr(tup,Anum_name,RelationGetDescr(rel),&isnull);
381360
Assert(!isnull);
382-
namespace=heap_getattr(tup,Anum_namespace,RelationGetDescr(rel),&isnull);
361+
namespace=heap_getattr(tup,Anum_namespace,RelationGetDescr(rel),
362+
&isnull);
383363
Assert(!isnull);
384364
oldNspOid=DatumGetObjectId(namespace);
385365

‎src/backend/commands/collationcmds.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ AlterCollationNamespace_oid(Oid collOid, Oid newNspOid)
339339

340340
/*
341341
* We have to check for name collision ourselves, because
342-
*AlterObjectNamespace doesn't know how to deal with the encoding
342+
*AlterObjectNamespace_internal doesn't know how to deal with the encoding
343343
* considerations.
344344
*/
345345
collation_name=get_collation_name(collOid);
@@ -370,12 +370,7 @@ AlterCollationNamespace_oid(Oid collOid, Oid newNspOid)
370370
get_namespace_name(newNspOid))));
371371

372372
/* OK, do the work */
373-
oldNspOid=AlterObjectNamespace(rel,COLLOID,-1,
374-
collOid,newNspOid,
375-
Anum_pg_collation_collname,
376-
Anum_pg_collation_collnamespace,
377-
Anum_pg_collation_collowner,
378-
ACL_KIND_COLLATION);
373+
oldNspOid=AlterObjectNamespace_internal(rel,collOid,newNspOid);
379374

380375
heap_close(rel,RowExclusiveLock);
381376

‎src/backend/commands/conversioncmds.c

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -266,53 +266,3 @@ AlterConversionOwner_internal(Relation rel, Oid conversionOid, Oid newOwnerId)
266266

267267
heap_freetuple(tup);
268268
}
269-
270-
/*
271-
* Execute ALTER CONVERSION SET SCHEMA
272-
*/
273-
void
274-
AlterConversionNamespace(List*name,constchar*newschema)
275-
{
276-
OidconvOid,
277-
nspOid;
278-
Relationrel;
279-
280-
rel=heap_open(ConversionRelationId,RowExclusiveLock);
281-
282-
convOid=get_conversion_oid(name, false);
283-
284-
/* get schema OID */
285-
nspOid=LookupCreationNamespace(newschema);
286-
287-
AlterObjectNamespace(rel,CONVOID,CONNAMENSP,
288-
convOid,nspOid,
289-
Anum_pg_conversion_conname,
290-
Anum_pg_conversion_connamespace,
291-
Anum_pg_conversion_conowner,
292-
ACL_KIND_CONVERSION);
293-
294-
heap_close(rel,RowExclusiveLock);
295-
}
296-
297-
/*
298-
* Change conversion schema, by oid
299-
*/
300-
Oid
301-
AlterConversionNamespace_oid(OidconvOid,OidnewNspOid)
302-
{
303-
OidoldNspOid;
304-
Relationrel;
305-
306-
rel=heap_open(ConversionRelationId,RowExclusiveLock);
307-
308-
oldNspOid=AlterObjectNamespace(rel,CONVOID,CONNAMENSP,
309-
convOid,newNspOid,
310-
Anum_pg_conversion_conname,
311-
Anum_pg_conversion_connamespace,
312-
Anum_pg_conversion_conowner,
313-
ACL_KIND_CONVERSION);
314-
315-
heap_close(rel,RowExclusiveLock);
316-
317-
returnoldNspOid;
318-
}

‎src/backend/commands/dropcmds.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include"miscadmin.h"
2727
#include"nodes/makefuncs.h"
2828
#include"parser/parse_type.h"
29-
#include"utils/acl.h"
3029
#include"utils/builtins.h"
3130
#include"utils/syscache.h"
3231

‎src/backend/commands/functioncmds.c

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include"catalog/pg_proc_fn.h"
4848
#include"catalog/pg_type.h"
4949
#include"catalog/pg_type_fn.h"
50+
#include"commands/alter.h"
5051
#include"commands/defrem.h"
5152
#include"commands/proclang.h"
5253
#include"miscadmin.h"
@@ -1851,21 +1852,16 @@ AlterFunctionNamespace_oid(Oid procOid, Oid nspOid)
18511852

18521853
procRel=heap_open(ProcedureRelationId,RowExclusiveLock);
18531854

1855+
/*
1856+
* We have to check for name collisions ourselves, because
1857+
* AlterObjectNamespace_internal doesn't know how to deal with the
1858+
* argument types.
1859+
*/
18541860
tup=SearchSysCacheCopy1(PROCOID,ObjectIdGetDatum(procOid));
18551861
if (!HeapTupleIsValid(tup))
18561862
elog(ERROR,"cache lookup failed for function %u",procOid);
18571863
proc= (Form_pg_proc)GETSTRUCT(tup);
18581864

1859-
/* check permissions on function */
1860-
if (!pg_proc_ownercheck(procOid,GetUserId()))
1861-
aclcheck_error(ACLCHECK_NOT_OWNER,ACL_KIND_PROC,
1862-
NameStr(proc->proname));
1863-
1864-
oldNspOid=proc->pronamespace;
1865-
1866-
/* common checks on switching namespaces */
1867-
CheckSetNamespace(oldNspOid,nspOid,ProcedureRelationId,procOid);
1868-
18691865
/* check for duplicate name (more friendly than unique-index failure) */
18701866
if (SearchSysCacheExists3(PROCNAMEARGSNSP,
18711867
CStringGetDatum(NameStr(proc->proname)),
@@ -1877,21 +1873,8 @@ AlterFunctionNamespace_oid(Oid procOid, Oid nspOid)
18771873
NameStr(proc->proname),
18781874
get_namespace_name(nspOid))));
18791875

1880-
/* OK, modify the pg_proc row */
1881-
1882-
/* tup is a copy, so we can scribble directly on it */
1883-
proc->pronamespace=nspOid;
1884-
1885-
simple_heap_update(procRel,&tup->t_self,tup);
1886-
CatalogUpdateIndexes(procRel,tup);
1887-
1888-
/* Update dependency on schema */
1889-
if (changeDependencyFor(ProcedureRelationId,procOid,
1890-
NamespaceRelationId,oldNspOid,nspOid)!=1)
1891-
elog(ERROR,"failed to change schema dependency for function \"%s\"",
1892-
NameStr(proc->proname));
1893-
1894-
heap_freetuple(tup);
1876+
/* OK, do the work */
1877+
oldNspOid=AlterObjectNamespace_internal(procRel,procOid,nspOid);
18951878

18961879
heap_close(procRel,RowExclusiveLock);
18971880

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp