Skip to content

Commit 05f3f9c

Browse files
committed
Extend object-access hook machinery to support post-alter events.
This also slightly widens the scope of what we support in terms of post-create events. KaiGai Kohei, with a few changes, mostly to the comments, by me
1 parent 6ac7fac commit 05f3f9c

31 files changed

+375
-53
lines changed

src/backend/catalog/aclchk.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "catalog/catalog.h"
2626
#include "catalog/dependency.h"
2727
#include "catalog/indexing.h"
28+
#include "catalog/objectaccess.h"
2829
#include "catalog/pg_authid.h"
2930
#include "catalog/pg_collation.h"
3031
#include "catalog/pg_conversion.h"
@@ -1290,6 +1291,13 @@ SetDefaultACL(InternalDefaultACL *iacls)
12901291
iacls->roleid,
12911292
noldmembers, oldmembers,
12921293
nnewmembers, newmembers);
1294+
1295+
if (isNew)
1296+
InvokeObjectPostCreateHook(DefaultAclRelationId,
1297+
HeapTupleGetOid(newtuple), 0);
1298+
else
1299+
InvokeObjectPostAlterHook(DefaultAclRelationId,
1300+
HeapTupleGetOid(newtuple), 0);
12931301
}
12941302

12951303
if (HeapTupleIsValid(tuple))

src/backend/catalog/heap.c

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ static Oid AddNewRelationType(const char *typeName,
9696
static void RelationRemoveInheritance(Oid relid);
9797
static void StoreRelCheck(Relation rel, char *ccname, Node *expr,
9898
bool is_validated, bool is_local, int inhcount,
99-
bool is_no_inherit);
100-
static void StoreConstraints(Relation rel, List *cooked_constraints);
99+
bool is_no_inherit, bool is_internal);
100+
static void StoreConstraints(Relation rel, List *cooked_constraints,
101+
bool is_internal);
101102
static bool MergeWithExistingConstraint(Relation rel, char *ccname, Node *expr,
102103
bool allow_merge, bool is_local,
103104
bool is_no_inherit);
@@ -1302,7 +1303,7 @@ heap_create_with_catalog(const char *relname,
13021303
* entry, so the relation must be valid and self-consistent at this point.
13031304
* In particular, there are not yet constraints and defaults anywhere.
13041305
*/
1305-
StoreConstraints(new_rel_desc, cooked_constraints);
1306+
StoreConstraints(new_rel_desc, cooked_constraints, is_internal);
13061307

13071308
/*
13081309
* If there's a special on-commit action, remember it
@@ -1836,7 +1837,8 @@ heap_drop_with_catalog(Oid relid)
18361837
* Store a default expression for column attnum of relation rel.
18371838
*/
18381839
void
1839-
StoreAttrDefault(Relation rel, AttrNumber attnum, Node *expr)
1840+
StoreAttrDefault(Relation rel, AttrNumber attnum,
1841+
Node *expr, bool is_internal)
18401842
{
18411843
char *adbin;
18421844
char *adsrc;
@@ -1928,6 +1930,17 @@ StoreAttrDefault(Relation rel, AttrNumber attnum, Node *expr)
19281930
* Record dependencies on objects used in the expression, too.
19291931
*/
19301932
recordDependencyOnExpr(&defobject, expr, NIL, DEPENDENCY_NORMAL);
1933+
1934+
/*
1935+
* Post creation hook for attribute defaults.
1936+
*
1937+
* XXX. ALTER TABLE ALTER COLUMN SET/DROP DEFAULT is implemented
1938+
* with a couple of deletion/creation of the attribute's default entry,
1939+
* so the callee should check existence of an older version of this
1940+
* entry if it needs to distinguish.
1941+
*/
1942+
InvokeObjectPostCreateHookArg(AttrDefaultRelationId,
1943+
RelationGetRelid(rel), attnum, is_internal);
19311944
}
19321945

19331946
/*
@@ -1939,7 +1952,7 @@ StoreAttrDefault(Relation rel, AttrNumber attnum, Node *expr)
19391952
static void
19401953
StoreRelCheck(Relation rel, char *ccname, Node *expr,
19411954
bool is_validated, bool is_local, int inhcount,
1942-
bool is_no_inherit)
1955+
bool is_no_inherit, bool is_internal)
19431956
{
19441957
char *ccbin;
19451958
char *ccsrc;
@@ -2023,7 +2036,8 @@ StoreRelCheck(Relation rel, char *ccname, Node *expr,
20232036
ccsrc, /* Source form of check constraint */
20242037
is_local, /* conislocal */
20252038
inhcount, /* coninhcount */
2026-
is_no_inherit); /* connoinherit */
2039+
is_no_inherit, /* connoinherit */
2040+
is_internal); /* internally constructed? */
20272041

20282042
pfree(ccbin);
20292043
pfree(ccsrc);
@@ -2038,7 +2052,7 @@ StoreRelCheck(Relation rel, char *ccname, Node *expr,
20382052
* and StoreRelCheck (see AddRelationNewConstraints()).
20392053
*/
20402054
static void
2041-
StoreConstraints(Relation rel, List *cooked_constraints)
2055+
StoreConstraints(Relation rel, List *cooked_constraints, bool is_internal)
20422056
{
20432057
int numchecks = 0;
20442058
ListCell *lc;
@@ -2060,11 +2074,12 @@ StoreConstraints(Relation rel, List *cooked_constraints)
20602074
switch (con->contype)
20612075
{
20622076
case CONSTR_DEFAULT:
2063-
StoreAttrDefault(rel, con->attnum, con->expr);
2077+
StoreAttrDefault(rel, con->attnum, con->expr, is_internal);
20642078
break;
20652079
case CONSTR_CHECK:
20662080
StoreRelCheck(rel, con->name, con->expr, !con->skip_validation,
2067-
con->is_local, con->inhcount, con->is_no_inherit);
2081+
con->is_local, con->inhcount,
2082+
con->is_no_inherit, is_internal);
20682083
numchecks++;
20692084
break;
20702085
default:
@@ -2090,6 +2105,7 @@ StoreConstraints(Relation rel, List *cooked_constraints)
20902105
* newConstraints: list of Constraint nodes
20912106
* allow_merge: TRUE if check constraints may be merged with existing ones
20922107
* is_local: TRUE if definition is local, FALSE if it's inherited
2108+
* is_internal: TRUE if result of some internal process, not a user request
20932109
*
20942110
* All entries in newColDefaults will be processed. Entries in newConstraints
20952111
* will be processed only if they are CONSTR_CHECK type.
@@ -2107,7 +2123,8 @@ AddRelationNewConstraints(Relation rel,
21072123
List *newColDefaults,
21082124
List *newConstraints,
21092125
bool allow_merge,
2110-
bool is_local)
2126+
bool is_local,
2127+
bool is_internal)
21112128
{
21122129
List *cookedConstraints = NIL;
21132130
TupleDesc tupleDesc;
@@ -2170,7 +2187,7 @@ AddRelationNewConstraints(Relation rel,
21702187
(IsA(expr, Const) &&((Const *) expr)->constisnull))
21712188
continue;
21722189

2173-
StoreAttrDefault(rel, colDef->attnum, expr);
2190+
StoreAttrDefault(rel, colDef->attnum, expr, is_internal);
21742191

21752192
cooked = (CookedConstraint *) palloc(sizeof(CookedConstraint));
21762193
cooked->contype = CONSTR_DEFAULT;
@@ -2296,7 +2313,7 @@ AddRelationNewConstraints(Relation rel,
22962313
* OK, store it.
22972314
*/
22982315
StoreRelCheck(rel, ccname, expr, !cdef->skip_validation, is_local,
2299-
is_local ? 0 : 1, cdef->is_no_inherit);
2316+
is_local ? 0 : 1, cdef->is_no_inherit, is_internal);
23002317

23012318
numchecks++;
23022319

src/backend/catalog/index.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,8 @@ index_create(Relation heapRelation,
933933
false, /* already marked primary */
934934
false, /* pg_index entry is OK */
935935
false, /* no old dependencies */
936-
allow_system_table_mods);
936+
allow_system_table_mods,
937+
is_internal);
937938
}
938939
else
939940
{
@@ -1107,6 +1108,7 @@ index_create(Relation heapRelation,
11071108
* remove_old_dependencies: if true, remove existing dependencies of index
11081109
* on table's columns
11091110
* allow_system_table_mods: allow table to be a system catalog
1111+
* is_internal: index is constructed due to internal process
11101112
*/
11111113
void
11121114
index_constraint_create(Relation heapRelation,
@@ -1119,7 +1121,8 @@ index_constraint_create(Relation heapRelation,
11191121
bool mark_as_primary,
11201122
bool update_pgindex,
11211123
bool remove_old_dependencies,
1122-
bool allow_system_table_mods)
1124+
bool allow_system_table_mods,
1125+
bool is_internal)
11231126
{
11241127
Oid namespaceId = RelationGetNamespace(heapRelation);
11251128
ObjectAddress myself,
@@ -1184,7 +1187,8 @@ index_constraint_create(Relation heapRelation,
11841187
NULL,
11851188
true, /* islocal */
11861189
0, /* inhcount */
1187-
true); /* noinherit */
1190+
true, /* noinherit */
1191+
is_internal);
11881192

11891193
/*
11901194
* Register the index as internally dependent on the constraint.
@@ -1293,6 +1297,9 @@ index_constraint_create(Relation heapRelation,
12931297
{
12941298
simple_heap_update(pg_index, &indexTuple->t_self, indexTuple);
12951299
CatalogUpdateIndexes(pg_index, indexTuple);
1300+
1301+
InvokeObjectPostAlterHookArg(IndexRelationId, indexRelationId, 0,
1302+
InvalidOid, is_internal);
12961303
}
12971304

12981305
heap_freetuple(indexTuple);

src/backend/catalog/objectaccess.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,26 @@ RunObjectDropHook(Oid classId, Oid objectId, int subId,
6161
classId, objectId, subId,
6262
(void *) &drop_arg);
6363
}
64+
65+
/*
66+
* RunObjectPostAlterHook
67+
*
68+
* It is entrypoint of OAT_POST_ALTER event
69+
*/
70+
void
71+
RunObjectPostAlterHook(Oid classId, Oid objectId, int subId,
72+
Oid auxiliaryId, bool is_internal)
73+
{
74+
ObjectAccessPostAlter pa_arg;
75+
76+
/* caller should check, but just in case... */
77+
Assert(object_access_hook != NULL);
78+
79+
memset(&pa_arg, 0, sizeof(ObjectAccessPostAlter));
80+
pa_arg.auxiliary_id = auxiliaryId;
81+
pa_arg.is_internal = is_internal;
82+
83+
(*object_access_hook)(OAT_POST_ALTER,
84+
classId, objectId, subId,
85+
(void *) &pa_arg);
86+
}

src/backend/catalog/pg_constraint.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ CreateConstraintEntry(const char *constraintName,
6868
const char *conSrc,
6969
bool conIsLocal,
7070
int conInhCount,
71-
bool conNoInherit)
71+
bool conNoInherit,
72+
bool is_internal)
7273
{
7374
Relation conDesc;
7475
Oid conOid;
@@ -367,7 +368,8 @@ CreateConstraintEntry(const char *constraintName,
367368
}
368369

369370
/* Post creation hook for new constraint */
370-
InvokeObjectPostCreateHook(ConstraintRelationId, conOid, 0);
371+
InvokeObjectPostCreateHookArg(ConstraintRelationId, conOid, 0,
372+
is_internal);
371373

372374
return conOid;
373375
}
@@ -665,6 +667,8 @@ RenameConstraintById(Oid conId, const char *newname)
665667
/* update the system catalog indexes */
666668
CatalogUpdateIndexes(conDesc, tuple);
667669

670+
InvokeObjectPostAlterHook(ConstraintRelationId, conId, 0);
671+
668672
heap_freetuple(tuple);
669673
heap_close(conDesc, RowExclusiveLock);
670674
}
@@ -737,6 +741,8 @@ AlterConstraintNamespaces(Oid ownerId, Oid oldNspId,
737741
*/
738742
}
739743

744+
InvokeObjectPostAlterHook(ConstraintRelationId, thisobj.objectId, 0);
745+
740746
add_exact_object_address(&thisobj, objsMoved);
741747
}
742748

src/backend/catalog/pg_db_role_setting.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "access/heapam.h"
1515
#include "access/htup_details.h"
1616
#include "catalog/indexing.h"
17+
#include "catalog/objectaccess.h"
1718
#include "catalog/pg_db_role_setting.h"
1819
#include "utils/fmgroids.h"
1920
#include "utils/rel.h"
@@ -160,6 +161,9 @@ AlterSetting(Oid databaseid, Oid roleid, VariableSetStmt *setstmt)
160161
CatalogUpdateIndexes(rel, newtuple);
161162
}
162163

164+
InvokeObjectPostAlterHookArg(DbRoleSettingRelationId,
165+
databaseid, 0, roleid, false);
166+
163167
systable_endscan(scan);
164168

165169
/* Close pg_db_role_setting, but keep lock till commit */

src/backend/catalog/pg_type.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,8 @@ RenameTypeInternal(Oid typeOid, const char *newTypeName, Oid typeNamespace)
712712
/* update the system catalog indexes */
713713
CatalogUpdateIndexes(pg_type_desc, tuple);
714714

715+
InvokeObjectPostAlterHook(TypeRelationId, typeOid, 0);
716+
715717
heap_freetuple(tuple);
716718
heap_close(pg_type_desc, RowExclusiveLock);
717719

src/backend/commands/alter.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "catalog/dependency.h"
2020
#include "catalog/indexing.h"
2121
#include "catalog/namespace.h"
22+
#include "catalog/objectaccess.h"
2223
#include "catalog/pg_collation.h"
2324
#include "catalog/pg_conversion.h"
2425
#include "catalog/pg_event_trigger.h"
@@ -281,6 +282,8 @@ AlterObjectRename_internal(Relation rel, Oid objectId, const char *new_name)
281282
simple_heap_update(rel, &oldtup->t_self, newtup);
282283
CatalogUpdateIndexes(rel, newtup);
283284

285+
InvokeObjectPostAlterHook(classId, objectId, 0);
286+
284287
/* Release memory */
285288
pfree(values);
286289
pfree(nulls);
@@ -657,6 +660,8 @@ AlterObjectNamespace_internal(Relation rel, Oid objid, Oid nspOid)
657660
changeDependencyFor(classId, objid,
658661
NamespaceRelationId, oldNspOid, nspOid);
659662

663+
InvokeObjectPostAlterHook(classId, objid, 0);
664+
660665
return oldNspOid;
661666
}
662667

@@ -934,4 +939,6 @@ AlterObjectOwner_internal(Relation rel, Oid objectId, Oid new_ownerId)
934939
pfree(nulls);
935940
pfree(replaces);
936941
}
942+
943+
InvokeObjectPostAlterHook(classId, objectId, 0);
937944
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy