1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-05 02:22:28 +03:00

Unify drop-by-OID functions

There are a number of Remove${Something}ById() functions that are
essentially identical in structure and only different in which catalog
they are working on.  Refactor this to be one generic function.  The
information about which oid column, index, etc. to use was already
available in ObjectProperty for most catalogs, in a few cases it was
easily added.

Reviewed-by: Pavel Stehule <pavel.stehule@gmail.com>
Reviewed-by: Robert Haas <robertmhaas@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/331d9661-1743-857f-1cbb-d5728bcd62cb%402ndquadrant.com
This commit is contained in:
Peter Eisentraut
2020-06-09 09:10:14 +02:00
parent b27c90bbe4
commit b1d32d3e32
23 changed files with 175 additions and 619 deletions

View File

@@ -115,33 +115,6 @@ CreateAccessMethod(CreateAmStmt *stmt)
return myself;
}
/*
* Guts of access method deletion.
*/
void
RemoveAccessMethodById(Oid amOid)
{
Relation relation;
HeapTuple tup;
if (!superuser())
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be superuser to drop access methods")));
relation = table_open(AccessMethodRelationId, RowExclusiveLock);
tup = SearchSysCache1(AMOID, ObjectIdGetDatum(amOid));
if (!HeapTupleIsValid(tup))
elog(ERROR, "cache lookup failed for access method %u", amOid);
CatalogTupleDelete(relation, &tup->t_self);
ReleaseSysCache(tup);
table_close(relation, RowExclusiveLock);
}
/*
* get_am_type_oid
* Worker for various get_am_*_oid variants

View File

@@ -355,28 +355,6 @@ filter_list_to_array(List *filterlist)
-1, false, TYPALIGN_INT));
}
/*
* Guts of event trigger deletion.
*/
void
RemoveEventTriggerById(Oid trigOid)
{
Relation tgrel;
HeapTuple tup;
tgrel = table_open(EventTriggerRelationId, RowExclusiveLock);
tup = SearchSysCache1(EVENTTRIGGEROID, ObjectIdGetDatum(trigOid));
if (!HeapTupleIsValid(tup))
elog(ERROR, "cache lookup failed for event trigger %u", trigOid);
CatalogTupleDelete(tgrel, &tup->t_self);
ReleaseSysCache(tup);
table_close(tgrel, RowExclusiveLock);
}
/*
* ALTER EVENT TRIGGER foo ENABLE|DISABLE|ENABLE ALWAYS|REPLICA
*/

View File

@@ -835,30 +835,6 @@ AlterForeignDataWrapper(AlterFdwStmt *stmt)
}
/*
* Drop foreign-data wrapper by OID
*/
void
RemoveForeignDataWrapperById(Oid fdwId)
{
HeapTuple tp;
Relation rel;
rel = table_open(ForeignDataWrapperRelationId, RowExclusiveLock);
tp = SearchSysCache1(FOREIGNDATAWRAPPEROID, ObjectIdGetDatum(fdwId));
if (!HeapTupleIsValid(tp))
elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwId);
CatalogTupleDelete(rel, &tp->t_self);
ReleaseSysCache(tp);
table_close(rel, RowExclusiveLock);
}
/*
* Create a foreign server
*/
@@ -1085,30 +1061,6 @@ AlterForeignServer(AlterForeignServerStmt *stmt)
}
/*
* Drop foreign server by OID
*/
void
RemoveForeignServerById(Oid srvId)
{
HeapTuple tp;
Relation rel;
rel = table_open(ForeignServerRelationId, RowExclusiveLock);
tp = SearchSysCache1(FOREIGNSERVEROID, ObjectIdGetDatum(srvId));
if (!HeapTupleIsValid(tp))
elog(ERROR, "cache lookup failed for foreign server %u", srvId);
CatalogTupleDelete(rel, &tp->t_self);
ReleaseSysCache(tp);
table_close(rel, RowExclusiveLock);
}
/*
* Common routine to check permission for user-mapping-related DDL
* commands. We allow server owners to operate on any mapping, and
@@ -1435,29 +1387,6 @@ RemoveUserMapping(DropUserMappingStmt *stmt)
}
/*
* Drop user mapping by OID. This is called to clean up dependencies.
*/
void
RemoveUserMappingById(Oid umId)
{
HeapTuple tp;
Relation rel;
rel = table_open(UserMappingRelationId, RowExclusiveLock);
tp = SearchSysCache1(USERMAPPINGOID, ObjectIdGetDatum(umId));
if (!HeapTupleIsValid(tp))
elog(ERROR, "cache lookup failed for user mapping %u", umId);
CatalogTupleDelete(rel, &tp->t_self);
ReleaseSysCache(tp);
table_close(rel, RowExclusiveLock);
}
/*
* Create a foreign table
* call after DefineRelation().

View File

@@ -1646,32 +1646,6 @@ CreateCast(CreateCastStmt *stmt)
return myself;
}
void
DropCastById(Oid castOid)
{
Relation relation;
ScanKeyData scankey;
SysScanDesc scan;
HeapTuple tuple;
relation = table_open(CastRelationId, RowExclusiveLock);
ScanKeyInit(&scankey,
Anum_pg_cast_oid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(castOid));
scan = systable_beginscan(relation, CastOidIndexId, true,
NULL, 1, &scankey);
tuple = systable_getnext(scan);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "could not find tuple for cast %u", castOid);
CatalogTupleDelete(relation, &tuple->t_self);
systable_endscan(scan);
table_close(relation, RowExclusiveLock);
}
static void
check_transform_function(Form_pg_proc procstruct)
@@ -1933,33 +1907,6 @@ get_transform_oid(Oid type_id, Oid lang_id, bool missing_ok)
}
void
DropTransformById(Oid transformOid)
{
Relation relation;
ScanKeyData scankey;
SysScanDesc scan;
HeapTuple tuple;
relation = table_open(TransformRelationId, RowExclusiveLock);
ScanKeyInit(&scankey,
Anum_pg_transform_oid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(transformOid));
scan = systable_beginscan(relation, TransformOidIndexId, true,
NULL, 1, &scankey);
tuple = systable_getnext(scan);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "could not find tuple for transform %u", transformOid);
CatalogTupleDelete(relation, &tuple->t_self);
systable_endscan(scan);
table_close(relation, RowExclusiveLock);
}
/*
* Subroutine for ALTER FUNCTION/AGGREGATE SET SCHEMA/RENAME
*

View File

@@ -1657,105 +1657,6 @@ dropProcedures(List *opfamilyname, Oid amoid, Oid opfamilyoid,
}
}
/*
* Deletion subroutines for use by dependency.c.
*/
void
RemoveOpFamilyById(Oid opfamilyOid)
{
Relation rel;
HeapTuple tup;
rel = table_open(OperatorFamilyRelationId, RowExclusiveLock);
tup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyOid));
if (!HeapTupleIsValid(tup)) /* should not happen */
elog(ERROR, "cache lookup failed for opfamily %u", opfamilyOid);
CatalogTupleDelete(rel, &tup->t_self);
ReleaseSysCache(tup);
table_close(rel, RowExclusiveLock);
}
void
RemoveOpClassById(Oid opclassOid)
{
Relation rel;
HeapTuple tup;
rel = table_open(OperatorClassRelationId, RowExclusiveLock);
tup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassOid));
if (!HeapTupleIsValid(tup)) /* should not happen */
elog(ERROR, "cache lookup failed for opclass %u", opclassOid);
CatalogTupleDelete(rel, &tup->t_self);
ReleaseSysCache(tup);
table_close(rel, RowExclusiveLock);
}
void
RemoveAmOpEntryById(Oid entryOid)
{
Relation rel;
HeapTuple tup;
ScanKeyData skey[1];
SysScanDesc scan;
ScanKeyInit(&skey[0],
Anum_pg_amop_oid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(entryOid));
rel = table_open(AccessMethodOperatorRelationId, RowExclusiveLock);
scan = systable_beginscan(rel, AccessMethodOperatorOidIndexId, true,
NULL, 1, skey);
/* we expect exactly one match */
tup = systable_getnext(scan);
if (!HeapTupleIsValid(tup))
elog(ERROR, "could not find tuple for amop entry %u", entryOid);
CatalogTupleDelete(rel, &tup->t_self);
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
}
void
RemoveAmProcEntryById(Oid entryOid)
{
Relation rel;
HeapTuple tup;
ScanKeyData skey[1];
SysScanDesc scan;
ScanKeyInit(&skey[0],
Anum_pg_amproc_oid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(entryOid));
rel = table_open(AccessMethodProcedureRelationId, RowExclusiveLock);
scan = systable_beginscan(rel, AccessMethodProcedureOidIndexId, true,
NULL, 1, skey);
/* we expect exactly one match */
tup = systable_getnext(scan);
if (!HeapTupleIsValid(tup))
elog(ERROR, "could not find tuple for amproc entry %u", entryOid);
CatalogTupleDelete(rel, &tup->t_self);
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
}
/*
* Subroutine for ALTER OPERATOR CLASS SET SCHEMA/RENAME
*

View File

@@ -218,28 +218,6 @@ CreateProceduralLanguage(CreatePLangStmt *stmt)
return myself;
}
/*
* Guts of language dropping.
*/
void
DropProceduralLanguageById(Oid langOid)
{
Relation rel;
HeapTuple langTup;
rel = table_open(LanguageRelationId, RowExclusiveLock);
langTup = SearchSysCache1(LANGOID, ObjectIdGetDatum(langOid));
if (!HeapTupleIsValid(langTup)) /* should not happen */
elog(ERROR, "cache lookup failed for language %u", langOid);
CatalogTupleDelete(rel, &langTup->t_self);
ReleaseSysCache(langTup);
table_close(rel, RowExclusiveLock);
}
/*
* get_language_oid - given a language name, look up the OID
*

View File

@@ -468,29 +468,6 @@ AlterPublication(AlterPublicationStmt *stmt)
table_close(rel, RowExclusiveLock);
}
/*
* Drop publication by OID
*/
void
RemovePublicationById(Oid pubid)
{
Relation rel;
HeapTuple tup;
rel = table_open(PublicationRelationId, RowExclusiveLock);
tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
if (!HeapTupleIsValid(tup))
elog(ERROR, "cache lookup failed for publication %u", pubid);
CatalogTupleDelete(rel, &tup->t_self);
ReleaseSysCache(tup);
table_close(rel, RowExclusiveLock);
}
/*
* Remove relation from publication by mapping OID.
*/

View File

@@ -210,29 +210,6 @@ CreateSchemaCommand(CreateSchemaStmt *stmt, const char *queryString,
return namespaceId;
}
/*
* Guts of schema deletion.
*/
void
RemoveSchemaById(Oid schemaOid)
{
Relation relation;
HeapTuple tup;
relation = table_open(NamespaceRelationId, RowExclusiveLock);
tup = SearchSysCache1(NAMESPACEOID,
ObjectIdGetDatum(schemaOid));
if (!HeapTupleIsValid(tup)) /* should not happen */
elog(ERROR, "cache lookup failed for namespace %u", schemaOid);
CatalogTupleDelete(relation, &tup->t_self);
ReleaseSysCache(tup);
table_close(relation, RowExclusiveLock);
}
/*
* Rename schema

View File

@@ -291,29 +291,6 @@ DefineTSParser(List *names, List *parameters)
return address;
}
/*
* Guts of TS parser deletion.
*/
void
RemoveTSParserById(Oid prsId)
{
Relation relation;
HeapTuple tup;
relation = table_open(TSParserRelationId, RowExclusiveLock);
tup = SearchSysCache1(TSPARSEROID, ObjectIdGetDatum(prsId));
if (!HeapTupleIsValid(tup))
elog(ERROR, "cache lookup failed for text search parser %u", prsId);
CatalogTupleDelete(relation, &tup->t_self);
ReleaseSysCache(tup);
table_close(relation, RowExclusiveLock);
}
/* ---------------------- TS Dictionary commands -----------------------*/
/*
@@ -504,30 +481,6 @@ DefineTSDictionary(List *names, List *parameters)
return address;
}
/*
* Guts of TS dictionary deletion.
*/
void
RemoveTSDictionaryById(Oid dictId)
{
Relation relation;
HeapTuple tup;
relation = table_open(TSDictionaryRelationId, RowExclusiveLock);
tup = SearchSysCache1(TSDICTOID, ObjectIdGetDatum(dictId));
if (!HeapTupleIsValid(tup))
elog(ERROR, "cache lookup failed for text search dictionary %u",
dictId);
CatalogTupleDelete(relation, &tup->t_self);
ReleaseSysCache(tup);
table_close(relation, RowExclusiveLock);
}
/*
* ALTER TEXT SEARCH DICTIONARY
*/
@@ -820,30 +773,6 @@ DefineTSTemplate(List *names, List *parameters)
return address;
}
/*
* Guts of TS template deletion.
*/
void
RemoveTSTemplateById(Oid tmplId)
{
Relation relation;
HeapTuple tup;
relation = table_open(TSTemplateRelationId, RowExclusiveLock);
tup = SearchSysCache1(TSTEMPLATEOID, ObjectIdGetDatum(tmplId));
if (!HeapTupleIsValid(tup))
elog(ERROR, "cache lookup failed for text search template %u",
tmplId);
CatalogTupleDelete(relation, &tup->t_self);
ReleaseSysCache(tup);
table_close(relation, RowExclusiveLock);
}
/* ---------------------- TS Configuration commands -----------------------*/
/*