1 /*-------------------------------------------------------------------------
4 * POSTGRES relation descriptor (a/k/a relcache entry) definitions.
7 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/include/utils/rel.h,v 1.88 2006/03/05 15:59:07 momjian Exp $
12 *-------------------------------------------------------------------------
17 #include "access/tupdesc.h"
18 #include "catalog/pg_am.h"
19 #include "catalog/pg_class.h"
20 #include "catalog/pg_index.h"
22 #include "rewrite/prs2lock.h"
23 #include "storage/block.h"
24 #include "storage/relfilenode.h"
28 * LockRelId and LockInfo really belong to lmgr.h, but it's more convenient
29 * to declare them here so we can have a LockInfoData field in a Relation.
32 typedef struct LockRelId
34 Oid relId; /* a relation identifier */
35 Oid dbId; /* a database identifier */
38 typedef struct LockInfoData
43 typedef LockInfoData *LockInfo;
46 * Likewise, this struct really belongs to trigger.h, but for convenience
49 typedef struct Trigger
51 Oid tgoid; /* OID of trigger (pg_trigger row) */
52 /* Remaining fields are copied from pg_trigger, see pg_trigger.h */
67 typedef struct TriggerDesc
70 * Index data to identify which triggers are which. Since each trigger
71 * can appear in more than one class, for each class we provide a list of
72 * integer indexes into the triggers array.
74 #define TRIGGER_NUM_EVENT_CLASSES 3
76 uint16 n_before_statement[TRIGGER_NUM_EVENT_CLASSES];
77 uint16 n_before_row[TRIGGER_NUM_EVENT_CLASSES];
78 uint16 n_after_row[TRIGGER_NUM_EVENT_CLASSES];
79 uint16 n_after_statement[TRIGGER_NUM_EVENT_CLASSES];
80 int *tg_before_statement[TRIGGER_NUM_EVENT_CLASSES];
81 int *tg_before_row[TRIGGER_NUM_EVENT_CLASSES];
82 int *tg_after_row[TRIGGER_NUM_EVENT_CLASSES];
83 int *tg_after_statement[TRIGGER_NUM_EVENT_CLASSES];
85 /* The actual array of triggers is here */
92 * Same for the statistics collector data in Relation and scan data.
94 typedef struct PgStat_Info
101 * Cached lookup information for the index access method functions defined
102 * by the pg_am row associated with an index relation.
104 typedef struct RelationAmInfo
107 FmgrInfo ambeginscan;
115 FmgrInfo ambulkdelete;
116 FmgrInfo amvacuumcleanup;
117 FmgrInfo amcostestimate;
122 * Here are the contents of a relation cache entry.
125 typedef struct RelationData
127 RelFileNode rd_node; /* relation physical identifier */
128 /* use "struct" here to avoid needing to include smgr.h: */
129 struct SMgrRelationData *rd_smgr; /* cached file handle, or NULL */
130 BlockNumber rd_targblock; /* current insertion target block, or
131 * InvalidBlockNumber */
132 int rd_refcnt; /* reference count */
133 bool rd_istemp; /* rel uses the local buffer mgr */
134 bool rd_isnailed; /* rel is nailed in cache */
135 bool rd_isvalid; /* relcache entry is valid */
136 char rd_indexvalid; /* state of rd_indexlist: 0 = not valid, 1 =
137 * valid, 2 = temporarily forced */
138 SubTransactionId rd_createSubid; /* rel was created in current xact */
141 * rd_createSubid is the ID of the highest subtransaction the rel has
142 * survived into; or zero if the rel was not created in the current top
143 * transaction. This should be relied on only for optimization purposes;
144 * it is possible for new-ness to be "forgotten" (eg, after CLUSTER).
146 Form_pg_class rd_rel; /* RELATION tuple */
147 TupleDesc rd_att; /* tuple descriptor */
148 Oid rd_id; /* relation's object id */
149 List *rd_indexlist; /* list of OIDs of indexes on relation */
150 Oid rd_oidindex; /* OID of unique index on OID, if any */
151 LockInfoData rd_lockInfo; /* lock mgr's info for locking relation */
152 RuleLock *rd_rules; /* rewrite rules */
153 MemoryContext rd_rulescxt; /* private memory cxt for rd_rules, if any */
154 TriggerDesc *trigdesc; /* Trigger info, or NULL if rel has none */
156 /* These are non-NULL only for an index relation: */
157 Form_pg_index rd_index; /* pg_index tuple describing this index */
158 struct HeapTupleData *rd_indextuple; /* all of pg_index tuple */
159 /* "struct HeapTupleData *" avoids need to include htup.h here */
160 oidvector *rd_indclass; /* extracted pointer to rd_index field */
161 Form_pg_am rd_am; /* pg_am tuple for index's AM */
164 * index access support info (used only for an index relation)
166 * Note: only default operators and support procs for each opclass are
167 * cached, namely those with subtype zero. The arrays are indexed by
168 * strategy or support number, which is a sufficient identifier given that
171 MemoryContext rd_indexcxt; /* private memory cxt for this stuff */
172 RelationAmInfo *rd_aminfo; /* lookup info for funcs found in pg_am */
173 Oid *rd_operator; /* OIDs of index operators */
174 RegProcedure *rd_support; /* OIDs of support procedures */
175 FmgrInfo *rd_supportinfo; /* lookup info for support procedures */
176 List *rd_indexprs; /* index expression trees, if any */
177 List *rd_indpred; /* index predicate tree, if any */
179 /* statistics collection area */
180 PgStat_Info pgstat_info;
183 typedef RelationData *Relation;
187 * RelationPtr is used in the executor to support index scans
188 * where we have to keep track of several index relations in an
189 * array. -cim 9/10/89
192 typedef Relation *RelationPtr;
197 * True iff relation descriptor is valid.
199 #define RelationIsValid(relation) PointerIsValid(relation)
201 #define InvalidRelation ((Relation) NULL)
204 * RelationHasReferenceCountZero
205 * True iff relation reference count is zero.
208 * Assumes relation descriptor is valid.
210 #define RelationHasReferenceCountZero(relation) \
211 ((bool)((relation)->rd_refcnt == 0))
215 * Returns pg_class tuple for a relation.
218 * Assumes relation descriptor is valid.
220 #define RelationGetForm(relation) ((relation)->rd_rel)
224 * Returns the OID of the relation
226 #define RelationGetRelid(relation) ((relation)->rd_id)
229 * RelationGetNumberOfAttributes
230 * Returns the number of attributes in a relation.
232 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
236 * Returns tuple descriptor for a relation.
238 #define RelationGetDescr(relation) ((relation)->rd_att)
241 * RelationGetRelationName
242 * Returns the rel's name.
244 * Note that the name is only unique within the containing namespace.
246 #define RelationGetRelationName(relation) \
247 (NameStr((relation)->rd_rel->relname))
250 * RelationGetNamespace
251 * Returns the rel's namespace OID.
253 #define RelationGetNamespace(relation) \
254 ((relation)->rd_rel->relnamespace)
258 * Open the relation at the smgr level, if not already done.
260 #define RelationOpenSmgr(relation) \
262 if ((relation)->rd_smgr == NULL) \
263 smgrsetowner(&((relation)->rd_smgr), smgropen((relation)->rd_node)); \
268 * Close the relation at the smgr level, if not already done.
270 * Note: smgrclose should unhook from owner pointer, hence the Assert.
272 #define RelationCloseSmgr(relation) \
274 if ((relation)->rd_smgr != NULL) \
276 smgrclose((relation)->rd_smgr); \
277 Assert((relation)->rd_smgr == NULL); \
283 * If a rel is either temp or newly created in the current transaction,
284 * it can be assumed to be visible only to the current backend.
286 * Beware of multiple eval of argument
288 #define RELATION_IS_LOCAL(relation) \
289 ((relation)->rd_istemp || \
290 (relation)->rd_createSubid != InvalidSubTransactionId)
292 /* routines in utils/cache/relcache.c */
293 extern void RelationIncrementReferenceCount(Relation rel);
294 extern void RelationDecrementReferenceCount(Relation rel);