1 /*-------------------------------------------------------------------------
4 * POSTGRES relation descriptor (a/k/a relcache entry) definitions.
7 * Portions Copyright (c) 1996-2003, 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.77 2004/08/28 20:31:44 tgl 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"
21 #include "rewrite/prs2lock.h"
22 #include "storage/block.h"
23 #include "storage/relfilenode.h"
27 * LockRelId and LockInfo really belong to lmgr.h, but it's more convenient
28 * to declare them here so we can have a LockInfoData field in a Relation.
31 typedef struct LockRelId
33 Oid relId; /* a relation identifier */
34 Oid dbId; /* a database identifier */
37 typedef struct LockInfoData
42 typedef LockInfoData *LockInfo;
45 * Likewise, this struct really belongs to trigger.h, but for convenience
48 typedef struct Trigger
50 Oid tgoid; /* OID of trigger (pg_trigger row) */
51 /* Remaining fields are copied from pg_trigger, see pg_trigger.h */
61 int16 tgattr[FUNC_MAX_ARGS];
65 typedef struct TriggerDesc
68 * Index data to identify which triggers are which. Since each
69 * trigger can appear in more than one class, for each class we
70 * provide a list of integer indexes into the triggers array.
72 #define TRIGGER_NUM_EVENT_CLASSES 3
74 uint16 n_before_statement[TRIGGER_NUM_EVENT_CLASSES];
75 uint16 n_before_row[TRIGGER_NUM_EVENT_CLASSES];
76 uint16 n_after_row[TRIGGER_NUM_EVENT_CLASSES];
77 uint16 n_after_statement[TRIGGER_NUM_EVENT_CLASSES];
78 int *tg_before_statement[TRIGGER_NUM_EVENT_CLASSES];
79 int *tg_before_row[TRIGGER_NUM_EVENT_CLASSES];
80 int *tg_after_row[TRIGGER_NUM_EVENT_CLASSES];
81 int *tg_after_statement[TRIGGER_NUM_EVENT_CLASSES];
83 /* The actual array of triggers is here */
90 * Same for the statistics collector data in Relation and scan data.
92 typedef struct PgStat_Info
96 bool heap_scan_counted;
97 bool index_scan_counted;
102 * Here are the contents of a relation cache entry.
105 typedef struct RelationData
107 RelFileNode rd_node; /* relation physical identifier */
108 /* use "struct" here to avoid needing to include smgr.h: */
109 struct SMgrRelationData *rd_smgr; /* cached file handle, or NULL */
110 BlockNumber rd_targblock; /* current insertion target block, or
111 * InvalidBlockNumber */
112 int rd_refcnt; /* reference count */
113 bool rd_istemp; /* rel uses the local buffer mgr */
114 bool rd_isnailed; /* rel is nailed in cache */
115 bool rd_isvalid; /* relcache entry is valid */
116 char rd_indexvalid; /* state of rd_indexlist: 0 = not valid,
117 * 1 = valid, 2 = temporarily forced */
118 TransactionId rd_createxact; /* rel was created in current xact */
120 * rd_createxact is the XID of the highest subtransaction the rel has
121 * survived into; or zero if the rel was not created in the current
122 * transaction. This should be relied on only for optimization purposes;
123 * it is possible for new-ness to be "forgotten" (eg, after CLUSTER).
125 Form_pg_class rd_rel; /* RELATION tuple */
126 TupleDesc rd_att; /* tuple descriptor */
127 Oid rd_id; /* relation's object id */
128 List *rd_indexlist; /* list of OIDs of indexes on relation */
129 LockInfoData rd_lockInfo; /* lock mgr's info for locking relation */
130 RuleLock *rd_rules; /* rewrite rules */
131 MemoryContext rd_rulescxt; /* private memory cxt for rd_rules, if any */
132 TriggerDesc *trigdesc; /* Trigger info, or NULL if rel has none */
134 /* These are non-NULL only for an index relation: */
135 Form_pg_index rd_index; /* pg_index tuple describing this index */
136 struct HeapTupleData *rd_indextuple; /* all of pg_index tuple */
137 /* "struct HeapTupleData *" avoids need to include htup.h here */
138 Form_pg_am rd_am; /* pg_am tuple for index's AM */
141 * index access support info (used only for an index relation)
143 * Note: only default operators and support procs for each opclass are
144 * cached, namely those with subtype zero. The arrays are indexed by
145 * strategy or support number, which is a sufficient identifier given
148 MemoryContext rd_indexcxt; /* private memory cxt for this stuff */
149 Oid *rd_operator; /* OIDs of index operators */
150 RegProcedure *rd_support; /* OIDs of support procedures */
151 struct FmgrInfo *rd_supportinfo; /* lookup info for support
153 /* "struct FmgrInfo" avoids need to include fmgr.h here */
154 List *rd_indexprs; /* index expression trees, if any */
155 List *rd_indpred; /* index predicate tree, if any */
157 /* statistics collection area */
158 PgStat_Info pgstat_info;
161 typedef RelationData *Relation;
165 * RelationPtr is used in the executor to support index scans
166 * where we have to keep track of several index relations in an
167 * array. -cim 9/10/89
170 typedef Relation *RelationPtr;
175 * True iff relation descriptor is valid.
177 #define RelationIsValid(relation) PointerIsValid(relation)
179 #define InvalidRelation ((Relation) NULL)
182 * RelationHasReferenceCountZero
183 * True iff relation reference count is zero.
186 * Assumes relation descriptor is valid.
188 #define RelationHasReferenceCountZero(relation) \
189 ((bool)((relation)->rd_refcnt == 0))
193 * Returns pg_class tuple for a relation.
196 * Assumes relation descriptor is valid.
198 #define RelationGetForm(relation) ((relation)->rd_rel)
202 * Returns the OID of the relation
204 #define RelationGetRelid(relation) ((relation)->rd_id)
207 * RelationGetNumberOfAttributes
208 * Returns the number of attributes in a relation.
210 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
214 * Returns tuple descriptor for a relation.
216 #define RelationGetDescr(relation) ((relation)->rd_att)
219 * RelationGetRelationName
220 * Returns the rel's name.
222 * Note that the name is only unique within the containing namespace.
224 #define RelationGetRelationName(relation) \
225 (NameStr((relation)->rd_rel->relname))
228 * RelationGetNamespace
229 * Returns the rel's namespace OID.
231 #define RelationGetNamespace(relation) \
232 ((relation)->rd_rel->relnamespace)
236 * If a rel is either temp or newly created in the current transaction,
237 * it can be assumed to be visible only to the current backend.
239 * Beware of multiple eval of argument
241 #define RELATION_IS_LOCAL(relation) \
242 ((relation)->rd_istemp || TransactionIdIsValid((relation)->rd_createxact))
244 /* routines in utils/cache/relcache.c */
245 extern void RelationIncrementReferenceCount(Relation rel);
246 extern void RelationDecrementReferenceCount(Relation rel);