1 /*-------------------------------------------------------------------------
4 * POSTGRES relation descriptor (a/k/a relcache entry) definitions.
7 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $Id: rel.h,v 1.62 2002/08/11 21:17:35 tgl Exp $
12 *-------------------------------------------------------------------------
17 #include "access/strat.h"
18 #include "access/tupdesc.h"
19 #include "catalog/pg_am.h"
20 #include "catalog/pg_class.h"
21 #include "catalog/pg_index.h"
22 #include "rewrite/prs2lock.h"
23 #include "storage/block.h"
24 #include "storage/fd.h"
25 #include "storage/relfilenode.h"
29 * LockRelId and LockInfo really belong to lmgr.h, but it's more convenient
30 * to declare them here so we can have a LockInfoData field in a Relation.
33 typedef struct LockRelId
35 Oid relId; /* a relation identifier */
36 Oid dbId; /* a database identifier */
39 typedef struct LockInfoData
44 typedef LockInfoData *LockInfo;
47 * Likewise, this struct really belongs to trigger.h, but for convenience
50 typedef struct Trigger
52 Oid tgoid; /* OID of trigger (pg_trigger row) */
53 /* Remaining fields are copied from pg_trigger, see pg_trigger.h */
63 int16 tgattr[FUNC_MAX_ARGS];
67 typedef struct TriggerDesc
70 * Index data to identify which triggers are which. Since each
71 * trigger can appear in more than one class, for each class we
72 * provide a list of integer indexes into the triggers array.
74 #define TRIGGER_NUM_EVENT_CLASSES 4
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.
95 typedef struct PgStat_Info
99 bool heap_scan_counted;
100 bool index_scan_counted;
104 * Here are the contents of a relation cache entry.
107 typedef struct RelationData
109 File rd_fd; /* open file descriptor, or -1 if none */
110 RelFileNode rd_node; /* file node (physical identifier) */
111 BlockNumber rd_nblocks; /* number of blocks in rel */
112 BlockNumber rd_targblock; /* current insertion target block, or
113 * InvalidBlockNumber */
114 int rd_refcnt; /* reference count */
115 bool rd_isnew; /* rel was created in current xact */
117 * NOTE: rd_isnew should be relied on only for optimization purposes;
118 * it is possible for new-ness to be "forgotten" (eg, after CLUSTER).
120 bool rd_istemp; /* rel uses the local buffer mgr */
121 bool rd_isnailed; /* rel is nailed in cache */
122 bool rd_indexfound; /* true if rd_indexlist is valid */
123 Form_pg_class rd_rel; /* RELATION tuple */
124 TupleDesc rd_att; /* tuple descriptor */
125 Oid rd_id; /* relation's object id */
126 List *rd_indexlist; /* list of OIDs of indexes on relation */
127 LockInfoData rd_lockInfo; /* lock mgr's info for locking relation */
128 RuleLock *rd_rules; /* rewrite rules */
129 MemoryContext rd_rulescxt; /* private memory cxt for rd_rules, if any */
130 TriggerDesc *trigdesc; /* Trigger info, or NULL if rel has none */
132 /* These are non-NULL only for an index relation: */
133 Form_pg_index rd_index; /* pg_index tuple describing this index */
134 Form_pg_am rd_am; /* pg_am tuple for index's AM */
136 /* index access support info (used only for an index relation) */
137 MemoryContext rd_indexcxt; /* private memory cxt for this stuff */
138 IndexStrategy rd_istrat; /* operator strategy map */
139 Oid *rd_operator; /* OIDs of index operators */
140 RegProcedure *rd_support; /* OIDs of support procedures */
141 struct FmgrInfo *rd_supportinfo; /* lookup info for support
143 /* "struct FmgrInfo" avoids need to include fmgr.h here */
145 /* statistics collection area */
146 PgStat_Info pgstat_info;
149 typedef RelationData *Relation;
153 * RelationPtr is used in the executor to support index scans
154 * where we have to keep track of several index relations in an
155 * array. -cim 9/10/89
158 typedef Relation *RelationPtr;
163 * True iff relation descriptor is valid.
165 #define RelationIsValid(relation) PointerIsValid(relation)
167 #define InvalidRelation ((Relation) NULL)
170 * RelationHasReferenceCountZero
171 * True iff relation reference count is zero.
174 * Assumes relation descriptor is valid.
176 #define RelationHasReferenceCountZero(relation) \
177 ((bool)((relation)->rd_refcnt == 0))
180 * RelationSetReferenceCount
181 * Sets relation reference count.
183 #define RelationSetReferenceCount(relation,count) \
184 ((relation)->rd_refcnt = (count))
187 * RelationIncrementReferenceCount
188 * Increments relation reference count.
190 #define RelationIncrementReferenceCount(relation) \
191 ((relation)->rd_refcnt += 1)
194 * RelationDecrementReferenceCount
195 * Decrements relation reference count.
197 #define RelationDecrementReferenceCount(relation) \
198 (AssertMacro((relation)->rd_refcnt > 0), \
199 (relation)->rd_refcnt -= 1)
203 * Returns pg_class tuple for a relation.
206 * Assumes relation descriptor is valid.
208 #define RelationGetForm(relation) ((relation)->rd_rel)
213 * returns the OID of the relation
215 #define RelationGetRelid(relation) ((relation)->rd_id)
220 * Returns the open file descriptor for the rel
222 #define RelationGetFile(relation) ((relation)->rd_fd)
225 * RelationGetNumberOfAttributes
227 * Returns the number of attributes.
229 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
233 * Returns tuple descriptor for a relation.
235 #define RelationGetDescr(relation) ((relation)->rd_att)
238 * RelationGetIndexStrategy
239 * Returns index strategy for a relation.
242 * Assumes relation descriptor is valid.
243 * Assumes relation descriptor is for an index relation.
245 #define RelationGetIndexStrategy(relation) ((relation)->rd_istrat)
248 * RelationGetRelationName
250 * Returns the rel's name.
252 * Note that the name is only unique within the containing namespace.
254 #define RelationGetRelationName(relation) \
255 (NameStr((relation)->rd_rel->relname))
258 * RelationGetNamespace
260 * Returns the rel's namespace OID.
262 #define RelationGetNamespace(relation) \
263 ((relation)->rd_rel->relnamespace)