1 /*-------------------------------------------------------------------------
4 * POSTGRES relation descriptor (a/k/a relcache entry) definitions.
7 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $Id: rel.h,v 1.51 2001/06/27 23:31:40 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 "rewrite/prs2lock.h"
22 #include "storage/block.h"
23 #include "storage/fd.h"
24 #include "storage/relfilenode.h"
26 /* added to prevent circular dependency. bjm 1999/11/15 */
27 extern char *get_temp_rel_by_physicalname(const char *relname);
30 * LockRelId and LockInfo really belong to lmgr.h, but it's more convenient
31 * to declare them here so we can have a LockInfoData field in a Relation.
34 typedef struct LockRelId
36 Oid relId; /* a relation identifier */
37 Oid dbId; /* a database identifier */
40 typedef struct LockInfoData
45 typedef LockInfoData *LockInfo;
48 * Likewise, this struct really belongs to trigger.h, but for convenience
51 typedef struct Trigger
62 int16 tgattr[FUNC_MAX_ARGS];
66 typedef struct TriggerDesc
69 * Index data to identify which triggers are which. Since each trigger
70 * can appear in more than one class, for each class we provide a list
71 * of integer indexes into the triggers array.
73 #define TRIGGER_NUM_EVENT_CLASSES 4
75 uint16 n_before_statement[TRIGGER_NUM_EVENT_CLASSES];
76 uint16 n_before_row[TRIGGER_NUM_EVENT_CLASSES];
77 uint16 n_after_row[TRIGGER_NUM_EVENT_CLASSES];
78 uint16 n_after_statement[TRIGGER_NUM_EVENT_CLASSES];
79 int *tg_before_statement[TRIGGER_NUM_EVENT_CLASSES];
80 int *tg_before_row[TRIGGER_NUM_EVENT_CLASSES];
81 int *tg_after_row[TRIGGER_NUM_EVENT_CLASSES];
82 int *tg_after_statement[TRIGGER_NUM_EVENT_CLASSES];
84 /* The actual array of triggers is here */
91 * Same for the statistics collector data in Relation and scan data.
94 typedef struct PgStat_Info
98 bool heap_scan_counted;
99 bool index_scan_counted;
103 * Here are the contents of a relation cache entry.
106 typedef struct RelationData
108 File rd_fd; /* open file descriptor, or -1 if none */
109 RelFileNode rd_node; /* file node (physical identifier) */
110 BlockNumber rd_nblocks; /* number of blocks in rel */
111 BlockNumber rd_targblock; /* current insertion target block,
112 * or InvalidBlockNumber */
113 int rd_refcnt; /* reference count */
114 bool rd_myxactonly; /* rel uses the local buffer mgr */
115 bool rd_isnailed; /* rel is nailed in cache */
116 bool rd_indexfound; /* true if rd_indexlist is valid */
117 bool rd_uniqueindex; /* true if rel is a UNIQUE index */
118 Form_pg_am rd_am; /* AM tuple */
119 Form_pg_class rd_rel; /* RELATION tuple */
120 Oid rd_id; /* relation's object id */
121 List *rd_indexlist; /* list of OIDs of indexes on relation */
122 LockInfoData rd_lockInfo; /* lock mgr's info for locking relation */
123 TupleDesc rd_att; /* tuple descriptor */
124 RuleLock *rd_rules; /* rewrite rules */
125 MemoryContext rd_rulescxt; /* private memory cxt for rd_rules, if any */
126 IndexStrategy rd_istrat; /* info needed if rel is an index */
127 RegProcedure *rd_support;
128 TriggerDesc *trigdesc; /* Trigger info, or NULL if rel has none */
130 PgStat_Info pgstat_info;
133 typedef RelationData *Relation;
137 * RelationPtr is used in the executor to support index scans
138 * where we have to keep track of several index relations in an
139 * array. -cim 9/10/89
142 typedef Relation *RelationPtr;
147 * True iff relation descriptor is valid.
149 #define RelationIsValid(relation) PointerIsValid(relation)
151 #define InvalidRelation ((Relation) NULL)
154 * RelationHasReferenceCountZero
155 * True iff relation reference count is zero.
158 * Assumes relation descriptor is valid.
160 #define RelationHasReferenceCountZero(relation) \
161 ((bool)((relation)->rd_refcnt == 0))
164 * RelationSetReferenceCount
165 * Sets relation reference count.
167 #define RelationSetReferenceCount(relation,count) \
168 ((relation)->rd_refcnt = (count))
171 * RelationIncrementReferenceCount
172 * Increments relation reference count.
174 #define RelationIncrementReferenceCount(relation) \
175 ((relation)->rd_refcnt += 1)
178 * RelationDecrementReferenceCount
179 * Decrements relation reference count.
181 #define RelationDecrementReferenceCount(relation) \
182 (AssertMacro((relation)->rd_refcnt > 0), \
183 (relation)->rd_refcnt -= 1)
187 * Returns pg_class tuple for a relation.
190 * Assumes relation descriptor is valid.
192 #define RelationGetForm(relation) ((relation)->rd_rel)
197 * returns the OID of the relation
199 #define RelationGetRelid(relation) ((relation)->rd_id)
204 * Returns the open file descriptor for the rel
206 #define RelationGetFile(relation) ((relation)->rd_fd)
209 * RelationGetNumberOfAttributes
211 * Returns the number of attributes.
213 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
217 * Returns tuple descriptor for a relation.
219 #define RelationGetDescr(relation) ((relation)->rd_att)
222 * RelationGetIndexStrategy
223 * Returns index strategy for a relation.
226 * Assumes relation descriptor is valid.
227 * Assumes relation descriptor is for an index relation.
229 #define RelationGetIndexStrategy(relation) ((relation)->rd_istrat)
232 * Routines in utils/cache/rel.c
234 extern void RelationSetIndexSupport(Relation relation,
235 IndexStrategy strategy,
236 RegProcedure *support);
239 * Handle temp relations
241 #define PG_TEMP_REL_PREFIX "pg_temp"
242 #define PG_TEMP_REL_PREFIX_LEN 7
244 #define is_temp_relname(relname) \
245 (strncmp(relname, PG_TEMP_REL_PREFIX, PG_TEMP_REL_PREFIX_LEN) == 0)
248 * RelationGetPhysicalRelationName
250 * Returns the rel's physical name, ie, the name appearing in pg_class.
252 * While this name is unique across all rels in the database, it is not
253 * necessarily useful for accessing the rel, since a temp table of the
254 * same name might mask the rel. It is useful mainly for determining if
255 * the rel is a shared system rel or not.
257 * The macro is rather unfortunately named, since the pg_class name no longer
258 * has anything to do with the file name used for physical storage of the rel.
260 #define RelationGetPhysicalRelationName(relation) \
261 (NameStr((relation)->rd_rel->relname))
264 * RelationGetRelationName
266 * Returns the relation's logical name (as seen by the user).
268 * If the rel is a temp rel, the temp name will be returned. Therefore,
269 * this name is not unique. But it is the name to use in heap_openr(),
272 #define RelationGetRelationName(relation) \
274 is_temp_relname(RelationGetPhysicalRelationName(relation)) \
276 get_temp_rel_by_physicalname( \
277 RelationGetPhysicalRelationName(relation)) \
279 RelationGetPhysicalRelationName(relation) \