Code beautification for object-access hook machinery.

KaiGai Kohei
This commit is contained in:
Robert Haas 2013-03-06 20:52:06 -05:00
parent f11af2bcab
commit f90cc26982
29 changed files with 123 additions and 119 deletions

View File

@ -11,7 +11,8 @@ top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
OBJS = catalog.o dependency.o heap.o index.o indexing.o namespace.o aclchk.o \
objectaddress.o pg_aggregate.o pg_collation.o pg_constraint.o pg_conversion.o \
objectaccess.o objectaddress.o pg_aggregate.o pg_collation.o \
pg_constraint.o pg_conversion.o \
pg_depend.o pg_enum.o pg_inherits.o pg_largeobject.o pg_namespace.o \
pg_operator.o pg_proc.o pg_range.o pg_db_role_setting.o pg_shdepend.o \
pg_type.o storage.o toasting.o

View File

@ -997,14 +997,8 @@ deleteOneObject(const ObjectAddress *object, Relation *depRel, int flags)
HeapTuple tup;
/* DROP hook of the objects being removed */
if (object_access_hook)
{
ObjectAccessDrop drop_arg;
drop_arg.dropflags = flags;
InvokeObjectAccessHook(OAT_DROP, object->classId, object->objectId,
object->objectSubId, &drop_arg);
}
InvokeObjectDropHookArg(object->classId, object->objectId,
object->objectSubId, flags);
/*
* Close depRel if we are doing a drop concurrently. The object deletion

View File

@ -1293,15 +1293,7 @@ heap_create_with_catalog(const char *relname,
}
/* Post creation hook for new relation */
if (object_access_hook)
{
ObjectAccessPostCreate post_create_args;
memset(&post_create_args, 0, sizeof(ObjectAccessPostCreate));
post_create_args.is_internal = is_internal;
(*object_access_hook)(OAT_POST_CREATE, RelationRelationId,
relid, 0, &post_create_args);
}
InvokeObjectPostCreateHookArg(RelationRelationId, relid, 0, is_internal);
/*
* Store any supplied constraints and defaults.

View File

@ -1028,15 +1028,8 @@ index_create(Relation heapRelation,
}
/* Post creation hook for new index */
if (object_access_hook)
{
ObjectAccessPostCreate post_create_args;
memset(&post_create_args, 0, sizeof(ObjectAccessPostCreate));
post_create_args.is_internal = is_internal;
(*object_access_hook)(OAT_POST_CREATE, RelationRelationId,
indexRelationId, 0, &post_create_args);
}
InvokeObjectPostCreateHookArg(RelationRelationId,
indexRelationId, 0, is_internal);
/*
* Advance the command counter so that we can see the newly-entered

View File

@ -0,0 +1,63 @@
/* -------------------------------------------------------------------------
*
* objectaccess.c
* functions for object_access_hook on various events
*
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* -------------------------------------------------------------------------
*/
#include "postgres.h"
#include "catalog/objectaccess.h"
/*
* Hook on object accesses. This is intended as infrastructure for security
* and logging plugins.
*/
object_access_hook_type object_access_hook = NULL;
/*
* RunObjectPostCreateHook
*
* It is entrypoint of OAT_POST_CREATE event
*/
void
RunObjectPostCreateHook(Oid classId, Oid objectId, int subId,
bool is_internal)
{
ObjectAccessPostCreate pc_arg;
/* caller should check, but just in case... */
Assert(object_access_hook != NULL);
memset(&pc_arg, 0, sizeof(ObjectAccessPostCreate));
pc_arg.is_internal = is_internal;
(*object_access_hook)(OAT_POST_CREATE,
classId, objectId, subId,
(void *) &pc_arg);
}
/*
* RunObjectDropHook
*
* It is entrypoint of OAT_DROP event
*/
void
RunObjectDropHook(Oid classId, Oid objectId, int subId,
int dropflags)
{
ObjectAccessDrop drop_arg;
/* caller should check, but just in case... */
Assert(object_access_hook != NULL);
memset(&drop_arg, 0, sizeof(ObjectAccessDrop));
drop_arg.dropflags = dropflags;
(*object_access_hook)(OAT_DROP,
classId, objectId, subId,
(void *) &drop_arg);
}

View File

@ -136,8 +136,7 @@ CollationCreate(const char *collname, Oid collnamespace,
recordDependencyOnCurrentExtension(&myself, false);
/* Post creation hook for new collation */
InvokeObjectAccessHook(OAT_POST_CREATE,
CollationRelationId, oid, 0, NULL);
InvokeObjectPostCreateHook(CollationRelationId, oid, 0);
heap_freetuple(tup);
heap_close(rel, RowExclusiveLock);

View File

@ -367,8 +367,7 @@ CreateConstraintEntry(const char *constraintName,
}
/* Post creation hook for new constraint */
InvokeObjectAccessHook(OAT_POST_CREATE,
ConstraintRelationId, conOid, 0, NULL);
InvokeObjectPostCreateHook(ConstraintRelationId, conOid, 0);
return conOid;
}

View File

@ -136,8 +136,7 @@ ConversionCreate(const char *conname, Oid connamespace,
recordDependencyOnCurrentExtension(&myself, false);
/* Post creation hook for new conversion */
InvokeObjectAccessHook(OAT_POST_CREATE, ConversionRelationId,
HeapTupleGetOid(tup), 0, NULL);
InvokeObjectPostCreateHook(ConversionRelationId, HeapTupleGetOid(tup), 0);
heap_freetuple(tup);
heap_close(rel, RowExclusiveLock);

View File

@ -96,8 +96,7 @@ NamespaceCreate(const char *nspName, Oid ownerId, bool isTemp)
recordDependencyOnCurrentExtension(&myself, false);
/* Post creation hook for new schema */
InvokeObjectAccessHook(OAT_POST_CREATE,
NamespaceRelationId, nspoid, 0, NULL);
InvokeObjectPostCreateHook(NamespaceRelationId, nspoid, 0);
return nspoid;
}

View File

@ -275,8 +275,7 @@ OperatorShellMake(const char *operatorName,
heap_freetuple(tup);
/* Post creation hook for new shell operator */
InvokeObjectAccessHook(OAT_POST_CREATE,
OperatorRelationId, operatorObjectId, 0, NULL);
InvokeObjectPostCreateHook(OperatorRelationId, operatorObjectId, 0);
/*
* Make sure the tuple is visible for subsequent lookups/updates.
@ -544,8 +543,7 @@ OperatorCreate(const char *operatorName,
makeOperatorDependencies(tup);
/* Post creation hook for new operator */
InvokeObjectAccessHook(OAT_POST_CREATE,
OperatorRelationId, operatorObjectId, 0, NULL);
InvokeObjectPostCreateHook(OperatorRelationId, operatorObjectId, 0);
heap_close(pg_operator_desc, RowExclusiveLock);

View File

@ -661,8 +661,7 @@ ProcedureCreate(const char *procedureName,
heap_freetuple(tup);
/* Post creation hook for new function */
InvokeObjectAccessHook(OAT_POST_CREATE,
ProcedureRelationId, retval, 0, NULL);
InvokeObjectPostCreateHook(ProcedureRelationId, retval, 0);
heap_close(rel, RowExclusiveLock);

View File

@ -163,8 +163,7 @@ TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
false);
/* Post creation hook for new shell type */
InvokeObjectAccessHook(OAT_POST_CREATE,
TypeRelationId, typoid, 0, NULL);
InvokeObjectPostCreateHook(TypeRelationId, typoid, 0);
/*
* clean up and return the type-oid
@ -476,8 +475,7 @@ TypeCreate(Oid newTypeOid,
rebuildDeps);
/* Post creation hook for new type */
InvokeObjectAccessHook(OAT_POST_CREATE,
TypeRelationId, typeObjectId, 0, NULL);
InvokeObjectPostCreateHook(TypeRelationId, typeObjectId, 0);
/*
* finish up

View File

@ -29,6 +29,7 @@
#include "catalog/pg_aggregate.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"

View File

@ -524,8 +524,7 @@ createdb(const CreatedbStmt *stmt)
copyTemplateDependencies(src_dboid, dboid);
/* Post creation hook for new database */
InvokeObjectAccessHook(OAT_POST_CREATE,
DatabaseRelationId, dboid, 0, NULL);
InvokeObjectPostCreateHook(DatabaseRelationId, dboid, 0);
/*
* Force a checkpoint before starting the copy. This will force dirty
@ -816,14 +815,7 @@ dropdb(const char *dbname, bool missing_ok)
dbname);
/* DROP hook for the database being removed */
if (object_access_hook)
{
ObjectAccessDrop drop_arg;
memset(&drop_arg, 0, sizeof(ObjectAccessDrop));
InvokeObjectAccessHook(OAT_DROP,
DatabaseRelationId, db_id, 0, &drop_arg);
}
InvokeObjectDropHook(DatabaseRelationId, db_id, 0);
/*
* Disallow dropping a DB that is marked istemplate. This is just to

View File

@ -310,8 +310,7 @@ insert_event_trigger_tuple(char *trigname, char *eventname, Oid evtOwner,
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
/* Post creation hook for new operator family */
InvokeObjectAccessHook(OAT_POST_CREATE,
EventTriggerRelationId, trigoid, 0, NULL);
InvokeObjectPostCreateHook(EventTriggerRelationId, trigoid, 0);
/* Close pg_event_trigger. */
heap_close(tgrel, RowExclusiveLock);

View File

@ -1562,8 +1562,7 @@ InsertExtensionTuple(const char *extName, Oid extOwner,
recordDependencyOn(&myself, &otherext, DEPENDENCY_NORMAL);
}
/* Post creation hook for new extension */
InvokeObjectAccessHook(OAT_POST_CREATE,
ExtensionRelationId, extensionOid, 0, NULL);
InvokeObjectPostCreateHook(ExtensionRelationId, extensionOid, 0);
return extensionOid;
}

View File

@ -599,8 +599,7 @@ CreateForeignDataWrapper(CreateFdwStmt *stmt)
recordDependencyOnCurrentExtension(&myself, false);
/* Post creation hook for new foreign data wrapper */
InvokeObjectAccessHook(OAT_POST_CREATE,
ForeignDataWrapperRelationId, fdwId, 0, NULL);
InvokeObjectPostCreateHook(ForeignDataWrapperRelationId, fdwId, 0);
heap_close(rel, RowExclusiveLock);
@ -900,8 +899,7 @@ CreateForeignServer(CreateForeignServerStmt *stmt)
recordDependencyOnCurrentExtension(&myself, false);
/* Post creation hook for new foreign server */
InvokeObjectAccessHook(OAT_POST_CREATE,
ForeignServerRelationId, srvId, 0, NULL);
InvokeObjectPostCreateHook(ForeignServerRelationId, srvId, 0);
heap_close(rel, RowExclusiveLock);
@ -1145,8 +1143,7 @@ CreateUserMapping(CreateUserMappingStmt *stmt)
recordDependencyOnCurrentExtension(&myself, false);
/* Post creation hook for new user mapping */
InvokeObjectAccessHook(OAT_POST_CREATE,
UserMappingRelationId, umId, 0, NULL);
InvokeObjectPostCreateHook(UserMappingRelationId, umId, 0);
heap_close(rel, RowExclusiveLock);

View File

@ -1558,8 +1558,7 @@ CreateCast(CreateCastStmt *stmt)
recordDependencyOnCurrentExtension(&myself, false);
/* Post creation hook for new cast */
InvokeObjectAccessHook(OAT_POST_CREATE,
CastRelationId, castid, 0, NULL);
InvokeObjectPostCreateHook(CastRelationId, castid, 0);
heap_freetuple(tuple);

View File

@ -309,8 +309,7 @@ CreateOpFamily(char *amname, char *opfname, Oid namespaceoid, Oid amoid)
recordDependencyOnCurrentExtension(&myself, false);
/* Post creation hook for new operator family */
InvokeObjectAccessHook(OAT_POST_CREATE,
OperatorFamilyRelationId, opfamilyoid, 0, NULL);
InvokeObjectPostCreateHook(OperatorFamilyRelationId, opfamilyoid, 0);
heap_close(rel, RowExclusiveLock);
@ -710,8 +709,7 @@ DefineOpClass(CreateOpClassStmt *stmt)
recordDependencyOnCurrentExtension(&myself, false);
/* Post creation hook for new operator class */
InvokeObjectAccessHook(OAT_POST_CREATE,
OperatorClassRelationId, opclassoid, 0, NULL);
InvokeObjectPostCreateHook(OperatorClassRelationId, opclassoid, 0);
heap_close(rel, RowExclusiveLock);

View File

@ -429,8 +429,7 @@ create_proc_lang(const char *languageName, bool replace,
}
/* Post creation hook for new procedural language */
InvokeObjectAccessHook(OAT_POST_CREATE,
LanguageRelationId, myself.objectId, 0, NULL);
InvokeObjectPostCreateHook(LanguageRelationId, myself.objectId, 0);
heap_close(rel, RowExclusiveLock);

View File

@ -4514,8 +4514,7 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
heap_freetuple(reltup);
/* Post creation hook for new attribute */
InvokeObjectAccessHook(OAT_POST_CREATE,
RelationRelationId, myrelid, newattnum, NULL);
InvokeObjectPostCreateHook(RelationRelationId, myrelid, newattnum);
heap_close(pgclass, RowExclusiveLock);

View File

@ -331,8 +331,7 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
recordDependencyOnOwner(TableSpaceRelationId, tablespaceoid, ownerId);
/* Post creation hook for new tablespace */
InvokeObjectAccessHook(OAT_POST_CREATE,
TableSpaceRelationId, tablespaceoid, 0, NULL);
InvokeObjectPostCreateHook(TableSpaceRelationId, tablespaceoid, 0);
create_tablespace_directories(location, tablespaceoid);
@ -439,14 +438,7 @@ DropTableSpace(DropTableSpaceStmt *stmt)
tablespacename);
/* DROP hook for the tablespace being removed */
if (object_access_hook)
{
ObjectAccessDrop drop_arg;
memset(&drop_arg, 0, sizeof(ObjectAccessDrop));
InvokeObjectAccessHook(OAT_DROP, TableSpaceRelationId,
tablespaceoid, 0, &drop_arg);
}
InvokeObjectDropHook(TableSpaceRelationId, tablespaceoid, 0);
/*
* Remove the pg_tablespace tuple (this will roll back if we fail below)

View File

@ -742,8 +742,7 @@ CreateTrigger(CreateTrigStmt *stmt, const char *queryString,
DEPENDENCY_NORMAL);
/* Post creation hook for new trigger */
InvokeObjectAccessHook(OAT_POST_CREATE,
TriggerRelationId, trigoid, 0, NULL);
InvokeObjectPostCreateHook(TriggerRelationId, trigoid, 0);
/* Keep lock on target rel until end of xact */
heap_close(rel, NoLock);

View File

@ -272,8 +272,7 @@ DefineTSParser(List *names, List *parameters)
makeParserDependencies(tup);
/* Post creation hook for new text search parser */
InvokeObjectAccessHook(OAT_POST_CREATE,
TSParserRelationId, prsOid, 0, NULL);
InvokeObjectPostCreateHook(TSParserRelationId, prsOid, 0);
heap_freetuple(tup);
@ -479,8 +478,7 @@ DefineTSDictionary(List *names, List *parameters)
makeDictionaryDependencies(tup);
/* Post creation hook for new text search dictionary */
InvokeObjectAccessHook(OAT_POST_CREATE,
TSDictionaryRelationId, dictOid, 0, NULL);
InvokeObjectPostCreateHook(TSDictionaryRelationId, dictOid, 0);
heap_freetuple(tup);
@ -796,8 +794,7 @@ DefineTSTemplate(List *names, List *parameters)
makeTSTemplateDependencies(tup);
/* Post creation hook for new text search template */
InvokeObjectAccessHook(OAT_POST_CREATE,
TSTemplateRelationId, tmplOid, 0, NULL);
InvokeObjectPostCreateHook(TSTemplateRelationId, tmplOid, 0);
heap_freetuple(tup);
@ -1092,8 +1089,7 @@ DefineTSConfiguration(List *names, List *parameters)
makeConfigurationDependencies(tup, false, mapRel);
/* Post creation hook for new text search configuration */
InvokeObjectAccessHook(OAT_POST_CREATE,
TSConfigRelationId, cfgOid, 0, NULL);
InvokeObjectPostCreateHook(TSConfigRelationId, cfgOid, 0);
heap_freetuple(tup);

View File

@ -426,8 +426,7 @@ CreateRole(CreateRoleStmt *stmt)
GetUserId(), false);
/* Post creation hook for new role */
InvokeObjectAccessHook(OAT_POST_CREATE,
AuthIdRelationId, roleid, 0, NULL);
InvokeObjectPostCreateHook(AuthIdRelationId, roleid, 0);
/*
* Close pg_authid, but keep lock till commit.
@ -968,14 +967,7 @@ DropRole(DropRoleStmt *stmt)
errmsg("must be superuser to drop superusers")));
/* DROP hook for the role being removed */
if (object_access_hook)
{
ObjectAccessDrop drop_arg;
memset(&drop_arg, 0, sizeof(ObjectAccessDrop));
InvokeObjectAccessHook(OAT_DROP,
AuthIdRelationId, roleid, 0, &drop_arg);
}
InvokeObjectDropHook(AuthIdRelationId, roleid, 0);
/*
* Lock the role, so nobody can add dependencies to her while we drop

View File

@ -182,8 +182,7 @@ InsertRule(char *rulname,
}
/* Post creation hook for new rule */
InvokeObjectAccessHook(OAT_POST_CREATE,
RewriteRelationId, rewriteObjectId, 0, NULL);
InvokeObjectPostCreateHook(RewriteRelationId, rewriteObjectId, 0);
heap_close(pg_rewrite_desc, RowExclusiveLock);

View File

@ -218,8 +218,7 @@ inv_create(Oid lobjId)
lobjId_new, GetUserId());
/* Post creation hook for new large object */
InvokeObjectAccessHook(OAT_POST_CREATE,
LargeObjectRelationId, lobjId_new, 0, NULL);
InvokeObjectPostCreateHook(LargeObjectRelationId, lobjId_new, 0);
/*
* Advance command counter to make new tuple visible to later operations.

View File

@ -18,7 +18,6 @@
*/
#include "postgres.h"
#include "catalog/objectaccess.h"
#include "libpq/pqcomm.h"
#include "miscadmin.h"
#include "storage/backendid.h"
@ -126,9 +125,3 @@ int VacuumCostBalance = 0; /* working state for vacuum */
bool VacuumCostActive = false;
int GinFuzzySearchLimit = 0;
/*
* Hook on object accesses. This is intended as infrastructure for security
* and logging plugins.
*/
object_access_hook_type object_access_hook = NULL;

View File

@ -28,6 +28,7 @@ typedef enum ObjectAccessType
{
OAT_POST_CREATE,
OAT_DROP,
OAT_POST_ALTER,
} ObjectAccessType;
/*
@ -66,11 +67,27 @@ typedef void (*object_access_hook_type) (ObjectAccessType access,
extern PGDLLIMPORT object_access_hook_type object_access_hook;
#define InvokeObjectAccessHook(access,classId,objectId,subId,arg) \
extern void RunObjectPostCreateHook(Oid classId, Oid objectId, int subId,
bool is_internal);
extern void RunObjectDropHook(Oid classId, Oid objectId, int subId,
int dropflags);
#define InvokeObjectPostCreateHook(classId,objectId,subId) \
InvokeObjectPostCreateHookArg((classId),(objectId),(subId),false)
#define InvokeObjectPostCreateHookArg(classId,objectId,subId,is_internal) \
do { \
if (object_access_hook) \
(*object_access_hook)((access),(classId), \
(objectId),(subId),(arg)); \
RunObjectPostCreateHook((classId),(objectId),(subId), \
(is_internal)); \
} while(0)
#define InvokeObjectDropHook(classId,objectId,subId) \
InvokeObjectDropHookArg((classId),(objectId),(subId),0)
#define InvokeObjectDropHookArg(classId,objectId,subId,dropflags) \
do { \
if (object_access_hook) \
RunObjectDropHook((classId),(objectId),(subId), \
(dropflags)); \
} while(0)
#endif /* OBJECTACCESS_H */