Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bdc3d7f

Browse files
committed
Return ObjectAddress in many ALTER TABLE sub-routines
Since commit a2e35b5, most CREATE and ALTER commands return the ObjectAddress of the affected object. This is useful for event triggers to try to figure out exactly what happened. This patch extends this idea a bit further to cover ALTER TABLE as well: an auxiliary ObjectAddress is returned for each of several subcommands of ALTER TABLE. This makes it possible to decode with precision what happened during execution of any ALTER TABLE command; for instance, which constraint was added by ALTER TABLE ADD CONSTRAINT, or which parent got dropped from the parents list by ALTER TABLE NO INHERIT. As with the previous patch, there is no immediate user-visible change here. This is all really just continuing what c504513 started. Reviewed by Stephen Frost.
1 parent 06bf0dd commit bdc3d7f

File tree

6 files changed

+380
-154
lines changed

6 files changed

+380
-154
lines changed

src/backend/catalog/heap.c

+59-39
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static ObjectAddress AddNewRelationType(const char *typeName,
9797
Oid new_row_type,
9898
Oid new_array_type);
9999
static void RelationRemoveInheritance(Oid relid);
100-
static void StoreRelCheck(Relation rel, char *ccname, Node *expr,
100+
static Oid StoreRelCheck(Relation rel, char *ccname, Node *expr,
101101
bool is_validated, bool is_local, int inhcount,
102102
bool is_no_inherit, bool is_internal);
103103
static void StoreConstraints(Relation rel, List *cooked_constraints,
@@ -1852,8 +1852,10 @@ heap_drop_with_catalog(Oid relid)
18521852

18531853
/*
18541854
* Store a default expression for column attnum of relation rel.
1855+
*
1856+
* Returns the OID of the new pg_attrdef tuple.
18551857
*/
1856-
void
1858+
Oid
18571859
StoreAttrDefault(Relation rel, AttrNumber attnum,
18581860
Node *expr, bool is_internal)
18591861
{
@@ -1958,15 +1960,19 @@ StoreAttrDefault(Relation rel, AttrNumber attnum,
19581960
*/
19591961
InvokeObjectPostCreateHookArg(AttrDefaultRelationId,
19601962
RelationGetRelid(rel), attnum, is_internal);
1963+
1964+
return attrdefOid;
19611965
}
19621966

19631967
/*
19641968
* Store a check-constraint expression for the given relation.
19651969
*
19661970
* Caller is responsible for updating the count of constraints
19671971
* in the pg_class entry for the relation.
1972+
*
1973+
* The OID of the new constraint is returned.
19681974
*/
1969-
static void
1975+
static Oid
19701976
StoreRelCheck(Relation rel, char *ccname, Node *expr,
19711977
bool is_validated, bool is_local, int inhcount,
19721978
bool is_no_inherit, bool is_internal)
@@ -1976,6 +1982,7 @@ StoreRelCheck(Relation rel, char *ccname, Node *expr,
19761982
List *varList;
19771983
int keycount;
19781984
int16 *attNos;
1985+
Oid constrOid;
19791986

19801987
/*
19811988
* Flatten expression to string form for storage.
@@ -2027,42 +2034,47 @@ StoreRelCheck(Relation rel, char *ccname, Node *expr,
20272034
/*
20282035
* Create the Check Constraint
20292036
*/
2030-
CreateConstraintEntry(ccname, /* Constraint Name */
2031-
RelationGetNamespace(rel), /* namespace */
2032-
CONSTRAINT_CHECK, /* Constraint Type */
2033-
false, /* Is Deferrable */
2034-
false, /* Is Deferred */
2035-
is_validated,
2036-
RelationGetRelid(rel), /* relation */
2037-
attNos, /* attrs in the constraint */
2038-
keycount, /* # attrs in the constraint */
2039-
InvalidOid, /* not a domain constraint */
2040-
InvalidOid, /* no associated index */
2041-
InvalidOid, /* Foreign key fields */
2042-
NULL,
2043-
NULL,
2044-
NULL,
2045-
NULL,
2046-
0,
2047-
' ',
2048-
' ',
2049-
' ',
2050-
NULL, /* not an exclusion constraint */
2051-
expr, /* Tree form of check constraint */
2052-
ccbin, /* Binary form of check constraint */
2053-
ccsrc, /* Source form of check constraint */
2054-
is_local, /* conislocal */
2055-
inhcount, /* coninhcount */
2056-
is_no_inherit, /* connoinherit */
2057-
is_internal); /* internally constructed? */
2037+
constrOid =
2038+
CreateConstraintEntry(ccname, /* Constraint Name */
2039+
RelationGetNamespace(rel), /* namespace */
2040+
CONSTRAINT_CHECK, /* Constraint Type */
2041+
false, /* Is Deferrable */
2042+
false, /* Is Deferred */
2043+
is_validated,
2044+
RelationGetRelid(rel), /* relation */
2045+
attNos, /* attrs in the constraint */
2046+
keycount, /* # attrs in the constraint */
2047+
InvalidOid, /* not a domain constraint */
2048+
InvalidOid, /* no associated index */
2049+
InvalidOid, /* Foreign key fields */
2050+
NULL,
2051+
NULL,
2052+
NULL,
2053+
NULL,
2054+
0,
2055+
' ',
2056+
' ',
2057+
' ',
2058+
NULL, /* not an exclusion constraint */
2059+
expr, /* Tree form of check constraint */
2060+
ccbin, /* Binary form of check constraint */
2061+
ccsrc, /* Source form of check constraint */
2062+
is_local, /* conislocal */
2063+
inhcount, /* coninhcount */
2064+
is_no_inherit, /* connoinherit */
2065+
is_internal); /* internally constructed? */
20582066

20592067
pfree(ccbin);
20602068
pfree(ccsrc);
2069+
2070+
return constrOid;
20612071
}
20622072

20632073
/*
20642074
* Store defaults and constraints (passed as a list of CookedConstraint).
20652075
*
2076+
* Each CookedConstraint struct is modified to store the new catalog tuple OID.
2077+
*
20662078
* NOTE: only pre-cooked expressions will be passed this way, which is to
20672079
* say expressions inherited from an existing relation. Newly parsed
20682080
* expressions can be added later, by direct calls to StoreAttrDefault
@@ -2074,7 +2086,7 @@ StoreConstraints(Relation rel, List *cooked_constraints, bool is_internal)
20742086
int numchecks = 0;
20752087
ListCell *lc;
20762088

2077-
if (!cooked_constraints)
2089+
if (cooked_constraints == NIL)
20782090
return; /* nothing to do */
20792091

20802092
/*
@@ -2091,12 +2103,15 @@ StoreConstraints(Relation rel, List *cooked_constraints, bool is_internal)
20912103
switch (con->contype)
20922104
{
20932105
case CONSTR_DEFAULT:
2094-
StoreAttrDefault(rel, con->attnum, con->expr, is_internal);
2106+
con->conoid = StoreAttrDefault(rel, con->attnum, con->expr,
2107+
is_internal);
20952108
break;
20962109
case CONSTR_CHECK:
2097-
StoreRelCheck(rel, con->name, con->expr, !con->skip_validation,
2098-
con->is_local, con->inhcount,
2099-
con->is_no_inherit, is_internal);
2110+
con->conoid =
2111+
StoreRelCheck(rel, con->name, con->expr,
2112+
!con->skip_validation, con->is_local,
2113+
con->inhcount, con->is_no_inherit,
2114+
is_internal);
21002115
numchecks++;
21012116
break;
21022117
default:
@@ -2184,6 +2199,7 @@ AddRelationNewConstraints(Relation rel,
21842199
{
21852200
RawColumnDefault *colDef = (RawColumnDefault *) lfirst(cell);
21862201
Form_pg_attribute atp = rel->rd_att->attrs[colDef->attnum - 1];
2202+
Oid defOid;
21872203

21882204
expr = cookDefault(pstate, colDef->raw_default,
21892205
atp->atttypid, atp->atttypmod,
@@ -2204,10 +2220,11 @@ AddRelationNewConstraints(Relation rel,
22042220
(IsA(expr, Const) &&((Const *) expr)->constisnull))
22052221
continue;
22062222

2207-
StoreAttrDefault(rel, colDef->attnum, expr, is_internal);
2223+
defOid = StoreAttrDefault(rel, colDef->attnum, expr, is_internal);
22082224

22092225
cooked = (CookedConstraint *) palloc(sizeof(CookedConstraint));
22102226
cooked->contype = CONSTR_DEFAULT;
2227+
cooked->conoid = defOid;
22112228
cooked->name = NULL;
22122229
cooked->attnum = colDef->attnum;
22132230
cooked->expr = expr;
@@ -2227,6 +2244,7 @@ AddRelationNewConstraints(Relation rel,
22272244
{
22282245
Constraint *cdef = (Constraint *) lfirst(cell);
22292246
char *ccname;
2247+
Oid constrOid;
22302248

22312249
if (cdef->contype != CONSTR_CHECK)
22322250
continue;
@@ -2329,13 +2347,15 @@ AddRelationNewConstraints(Relation rel,
23292347
/*
23302348
* OK, store it.
23312349
*/
2332-
StoreRelCheck(rel, ccname, expr, !cdef->skip_validation, is_local,
2333-
is_local ? 0 : 1, cdef->is_no_inherit, is_internal);
2350+
constrOid =
2351+
StoreRelCheck(rel, ccname, expr, !cdef->skip_validation, is_local,
2352+
is_local ? 0 : 1, cdef->is_no_inherit, is_internal);
23342353

23352354
numchecks++;
23362355

23372356
cooked = (CookedConstraint *) palloc(sizeof(CookedConstraint));
23382357
cooked->contype = CONSTR_CHECK;
2358+
cooked->conoid = constrOid;
23392359
cooked->name = ccname;
23402360
cooked->attnum = 0;
23412361
cooked->expr = expr;

src/backend/catalog/index.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ UpdateIndexRelation(Oid indexoid,
675675
* to fix it up.
676676
* is_internal: if true, post creation hook for new index
677677
* if_not_exists: if true, do not throw an error if a relation with
678-
* the same name already exists.
678+
* the same name already exists.
679679
*
680680
* Returns the OID of the created index.
681681
*/
@@ -1111,7 +1111,8 @@ index_create(Relation heapRelation,
11111111
/*
11121112
* index_constraint_create
11131113
*
1114-
* Set up a constraint associated with an index
1114+
* Set up a constraint associated with an index. Return the new constraint's
1115+
* address.
11151116
*
11161117
* heapRelation: table owning the index (must be suitably locked by caller)
11171118
* indexRelationId: OID of the index
@@ -1128,7 +1129,7 @@ index_create(Relation heapRelation,
11281129
* allow_system_table_mods: allow table to be a system catalog
11291130
* is_internal: index is constructed due to internal process
11301131
*/
1131-
void
1132+
ObjectAddress
11321133
index_constraint_create(Relation heapRelation,
11331134
Oid indexRelationId,
11341135
IndexInfo *indexInfo,
@@ -1316,6 +1317,8 @@ index_constraint_create(Relation heapRelation,
13161317
heap_freetuple(indexTuple);
13171318
heap_close(pg_index, RowExclusiveLock);
13181319
}
1320+
1321+
return referenced;
13191322
}
13201323

13211324
/*

src/backend/catalog/pg_constraint.c

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
* Subsidiary records (such as triggers or indexes to implement the
4141
* constraint) are *not* created here. But we do make dependency links
4242
* from the constraint to the things it depends on.
43+
*
44+
* The new constraint's OID is returned.
4345
*/
4446
Oid
4547
CreateConstraintEntry(const char *constraintName,

0 commit comments

Comments
 (0)