PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pg_attrdef.h File Reference
#include "catalog/genbki.h"
#include "catalog/objectaddress.h"
#include "catalog/pg_attrdef_d.h"
Include dependency graph for pg_attrdef.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef FormData_pg_attrdefForm_pg_attrdef
 

Functions

 CATALOG (pg_attrdef, 2604, AttrDefaultRelationId)
 
 DECLARE_TOAST (pg_attrdef, 2830, 2831)
 
 DECLARE_UNIQUE_INDEX (pg_attrdef_adrelid_adnum_index, 2656, AttrDefaultIndexId, pg_attrdef, btree(adrelid oid_ops, adnum int2_ops))
 
 DECLARE_UNIQUE_INDEX_PKEY (pg_attrdef_oid_index, 2657, AttrDefaultOidIndexId, pg_attrdef, btree(oid oid_ops))
 
 DECLARE_FOREIGN_KEY ((adrelid, adnum), pg_attribute,(attrelid, attnum))
 
Oid StoreAttrDefault (Relation rel, AttrNumber attnum, Node *expr, bool is_internal)
 
void RemoveAttrDefault (Oid relid, AttrNumber attnum, DropBehavior behavior, bool complain, bool internal)
 
void RemoveAttrDefaultById (Oid attrdefId)
 
Oid GetAttrDefaultOid (Oid relid, AttrNumber attnum)
 
ObjectAddress GetAttrDefaultColumnAddress (Oid attrdefoid)
 

Variables

 FormData_pg_attrdef
 

Typedef Documentation

◆ Form_pg_attrdef

Function Documentation

◆ CATALOG()

CATALOG ( pg_attrdef  ,
2604  ,
AttrDefaultRelationId   
)

Definition at line 30 of file pg_attrdef.h.

31{
32 Oid oid; /* oid */
33
34 Oid adrelid BKI_LOOKUP(pg_class); /* OID of table containing
35 * attribute */
36 int16 adnum; /* attnum of attribute */
37
38#ifdef CATALOG_VARLEN /* variable-length fields start here */
39 pg_node_tree adbin BKI_FORCE_NOT_NULL; /* nodeToString representation of
40 * default */
41#endif
int16_t int16
Definition c.h:541
#define BKI_LOOKUP(catalog)
Definition genbki.h:46
#define BKI_FORCE_NOT_NULL
Definition genbki.h:33
FormData_pg_attrdef
Definition pg_attrdef.h:42
unsigned int Oid
static int fb(int x)

References BKI_FORCE_NOT_NULL, BKI_LOOKUP, and fb().

◆ DECLARE_FOREIGN_KEY()

DECLARE_FOREIGN_KEY ( (adrelid, adnum)  ,
pg_attribute  ,
(attrelid, attnum  
)

◆ DECLARE_TOAST()

DECLARE_TOAST ( pg_attrdef  ,
2830  ,
2831   
)

◆ DECLARE_UNIQUE_INDEX()

DECLARE_UNIQUE_INDEX ( pg_attrdef_adrelid_adnum_index  ,
2656  ,
AttrDefaultIndexId  ,
pg_attrdef  ,
btree(adrelid oid_ops, adnum int2_ops  
)

◆ DECLARE_UNIQUE_INDEX_PKEY()

DECLARE_UNIQUE_INDEX_PKEY ( pg_attrdef_oid_index  ,
2657  ,
AttrDefaultOidIndexId  ,
pg_attrdef  ,
btree(oid oid_ops  
)

◆ GetAttrDefaultColumnAddress()

ObjectAddress GetAttrDefaultColumnAddress ( Oid  attrdefoid)
extern

Definition at line 322 of file pg_attrdef.c.

323{
326 ScanKeyData skey[1];
327 SysScanDesc scan;
329
331 ScanKeyInit(&skey[0],
336 NULL, 1, skey);
337
339 {
341
343 result.objectId = atdform->adrelid;
344 result.objectSubId = atdform->adnum;
345 }
346
347 systable_endscan(scan);
349
350 return result;
351}
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:603
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:514
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
#define AccessShareLock
Definition lockdefs.h:36
const ObjectAddress InvalidObjectAddress
FormData_pg_attrdef * Form_pg_attrdef
Definition pg_attrdef.h:49
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
#define BTEqualStrategyNumber
Definition stratnum.h:31
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40

References AccessShareLock, BTEqualStrategyNumber, ObjectAddress::classId, fb(), GETSTRUCT(), HeapTupleIsValid, InvalidObjectAddress, ObjectAddress::objectId, ObjectIdGetDatum(), ObjectAddress::objectSubId, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by EventTriggerSQLDropAddObject(), getObjectDescription(), getObjectIdentityParts(), and RememberAllDependentForRebuilding().

◆ GetAttrDefaultOid()

Oid GetAttrDefaultOid ( Oid  relid,
AttrNumber  attnum 
)
extern

◆ RemoveAttrDefault()

void RemoveAttrDefault ( Oid  relid,
AttrNumber  attnum,
DropBehavior  behavior,
bool  complain,
bool  internal 
)
extern

Definition at line 154 of file pg_attrdef.c.

156{
158 ScanKeyData scankeys[2];
159 SysScanDesc scan;
160 HeapTuple tuple;
161 bool found = false;
162
164
165 ScanKeyInit(&scankeys[0],
168 ObjectIdGetDatum(relid));
169 ScanKeyInit(&scankeys[1],
173
175 NULL, 2, scankeys);
176
177 /* There should be at most one matching tuple, but we loop anyway */
178 while (HeapTupleIsValid(tuple = systable_getnext(scan)))
179 {
180 ObjectAddress object;
182
184 object.objectId = attrtuple->oid;
185 object.objectSubId = 0;
186
187 performDeletion(&object, behavior,
188 internal ? PERFORM_DELETION_INTERNAL : 0);
189
190 found = true;
191 }
192
193 systable_endscan(scan);
195
196 if (complain && !found)
197 elog(ERROR, "could not find attrdef tuple for relation %u attnum %d",
198 relid, attnum);
199}
void performDeletion(const ObjectAddress *object, DropBehavior behavior, int flags)
Definition dependency.c:274
#define PERFORM_DELETION_INTERNAL
Definition dependency.h:92
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define RowExclusiveLock
Definition lockdefs.h:38

References attnum, BTEqualStrategyNumber, ObjectAddress::classId, elog, ERROR, fb(), GETSTRUCT(), HeapTupleIsValid, Int16GetDatum(), ObjectIdGetDatum(), PERFORM_DELETION_INTERNAL, performDeletion(), RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by ATExecAlterColumnType(), ATExecColumnDefault(), ATExecCookedColumnDefault(), ATExecDropExpression(), and ATExecSetExpression().

◆ RemoveAttrDefaultById()

void RemoveAttrDefaultById ( Oid  attrdefId)
extern

Definition at line 209 of file pg_attrdef.c.

210{
214 ScanKeyData scankeys[1];
215 SysScanDesc scan;
216 HeapTuple tuple;
217 Oid myrelid;
219
220 /* Grab an appropriate lock on the pg_attrdef relation */
222
223 /* Find the pg_attrdef tuple */
224 ScanKeyInit(&scankeys[0],
228
230 NULL, 1, scankeys);
231
232 tuple = systable_getnext(scan);
233 if (!HeapTupleIsValid(tuple))
234 elog(ERROR, "could not find tuple for attrdef %u", attrdefId);
235
237 myattnum = ((Form_pg_attrdef) GETSTRUCT(tuple))->adnum;
238
239 /* Get an exclusive lock on the relation owning the attribute */
241
242 /* Now we can delete the pg_attrdef row */
244
245 systable_endscan(scan);
247
248 /* Fix the pg_attribute row */
250
254 if (!HeapTupleIsValid(tuple)) /* shouldn't happen */
255 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
257
258 ((Form_pg_attribute) GETSTRUCT(tuple))->atthasdef = false;
259
260 CatalogTupleUpdate(attr_rel, &tuple->t_self, tuple);
261
262 /*
263 * Our update of the pg_attribute row will force a relcache rebuild, so
264 * there's nothing else to do here.
265 */
267
268 /* Keep lock on attribute's rel until end of xact */
270}
int16 AttrNumber
Definition attnum.h:21
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
Definition indexing.c:313
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
Definition indexing.c:365
#define NoLock
Definition lockdefs.h:34
#define AccessExclusiveLock
Definition lockdefs.h:43
FormData_pg_attribute * Form_pg_attribute
void relation_close(Relation relation, LOCKMODE lockmode)
Definition relation.c:205
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:47
ItemPointerData t_self
Definition htup.h:65
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition syscache.h:93

References AccessExclusiveLock, BTEqualStrategyNumber, CatalogTupleDelete(), CatalogTupleUpdate(), elog, ERROR, fb(), GETSTRUCT(), HeapTupleIsValid, Int16GetDatum(), NoLock, ObjectIdGetDatum(), relation_close(), relation_open(), RowExclusiveLock, ScanKeyInit(), SearchSysCacheCopy2, systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by doDeletion().

◆ StoreAttrDefault()

Oid StoreAttrDefault ( Relation  rel,
AttrNumber  attnum,
Node expr,
bool  is_internal 
)
extern

Definition at line 37 of file pg_attrdef.c.

39{
40 char *adbin;
42 HeapTuple tuple;
44 static bool nulls[Natts_pg_attrdef] = {false, false, false, false};
49 bool nullsAtt[Natts_pg_attribute] = {0};
51 char attgenerated;
55
57
58 /*
59 * Flatten expression to string form for storage.
60 */
61 adbin = nodeToString(expr);
62
63 /*
64 * Make the pg_attrdef entry.
65 */
72
73 tuple = heap_form_tuple(adrel->rd_att, values, nulls);
75
77 defobject.objectId = attrdefOid;
78 defobject.objectSubId = 0;
79
81
82 /* now can free some of the stuff allocated above */
84 heap_freetuple(tuple);
85 pfree(adbin);
86
87 /*
88 * Update the pg_attribute entry for the column to show that a default
89 * exists.
90 */
96 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
99 attgenerated = attStruct->attgenerated;
100
103
106
108
111
112 /*
113 * Make a dependency so that the pg_attrdef entry goes away if the column
114 * (or whole table) is deleted. In the case of a generated column, make
115 * it an internal dependency to prevent the default expression from being
116 * deleted separately.
117 */
119 colobject.objectId = RelationGetRelid(rel);
120 colobject.objectSubId = attnum;
121
123 attgenerated ? DEPENDENCY_INTERNAL : DEPENDENCY_AUTO);
124
125 /*
126 * Record dependencies on objects used in the expression, too.
127 */
130 DEPENDENCY_NORMAL, false);
131
132 /*
133 * Post creation hook for attribute defaults.
134 *
135 * XXX. ALTER TABLE ALTER COLUMN SET/DROP DEFAULT is implemented with a
136 * couple of deletion/creation of the attribute's default entry, so the
137 * callee should check existence of an older version of this entry if it
138 * needs to distinguish.
139 */
141 RelationGetRelid(rel), attnum, is_internal);
142
143 return attrdefOid;
144}
static Datum values[MAXATTR]
Definition bootstrap.c:155
#define CStringGetTextDatum(s)
Definition builtins.h:97
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition catalog.c:448
void recordDependencyOnSingleRelExpr(const ObjectAddress *depender, Node *expr, Oid relId, DependencyType behavior, DependencyType self_behavior, bool reverse_self)
@ DEPENDENCY_AUTO
Definition dependency.h:34
@ DEPENDENCY_INTERNAL
Definition dependency.h:35
@ DEPENDENCY_NORMAL
Definition dependency.h:33
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition heaptuple.c:1210
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1117
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1435
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition indexing.c:233
void pfree(void *pointer)
Definition mcxt.c:1616
#define InvokeObjectPostCreateHookArg(classId, objectId, subId, is_internal)
char * nodeToString(const void *obj)
Definition outfuncs.c:802
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition pg_depend.c:45
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
#define RelationGetRelid(relation)
Definition rel.h:514
#define RelationGetDescr(relation)
Definition rel.h:540

References attnum, BoolGetDatum(), CatalogTupleInsert(), CatalogTupleUpdate(), CStringGetTextDatum, DatumGetPointer(), DEPENDENCY_AUTO, DEPENDENCY_INTERNAL, DEPENDENCY_NORMAL, elog, ERROR, fb(), GetNewOidWithIndex(), GETSTRUCT(), heap_form_tuple(), heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, Int16GetDatum(), InvokeObjectPostCreateHookArg, nodeToString(), ObjectIdGetDatum(), pfree(), recordDependencyOn(), recordDependencyOnSingleRelExpr(), RelationGetDescr, RelationGetRelid, RowExclusiveLock, SearchSysCacheCopy2, table_close(), table_open(), and values.

Referenced by AddRelationNewConstraints(), ATExecAlterColumnType(), ATExecCookedColumnDefault(), createTableConstraints(), and StoreConstraints().

Variable Documentation

◆ FormData_pg_attrdef

FormData_pg_attrdef

Definition at line 42 of file pg_attrdef.h.