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.50 2001/06/22 19:16:24 wieck 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/relfilenode.h"
23 #include "storage/fd.h"
25 /* added to prevent circular dependency. bjm 1999/11/15 */
26 extern char *get_temp_rel_by_physicalname(const char *relname);
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
61 int16 tgattr[FUNC_MAX_ARGS];
65 typedef struct TriggerDesc
68 * Index data to identify which triggers are which. Since each trigger
69 * can appear in more than one class, for each class we provide a list
70 * of integer indexes into the triggers array.
72 #define TRIGGER_NUM_EVENT_CLASSES 4
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.
93 typedef struct PgStat_Info
97 bool heap_scan_counted;
98 bool index_scan_counted;
102 * Here are the contents of a relation cache entry.
105 typedef struct RelationData
107 File rd_fd; /* open file descriptor, or -1 if none */
108 RelFileNode rd_node; /* relation file node */
109 int rd_nblocks; /* number of blocks in rel */
110 uint16 rd_refcnt; /* reference count */
111 bool rd_myxactonly; /* rel uses the local buffer mgr */
112 bool rd_isnailed; /* rel is nailed in cache */
113 bool rd_indexfound; /* true if rd_indexlist is valid */
114 bool rd_uniqueindex; /* true if rel is a UNIQUE index */
115 Form_pg_am rd_am; /* AM tuple */
116 Form_pg_class rd_rel; /* RELATION tuple */
117 Oid rd_id; /* relation's object id */
118 List *rd_indexlist; /* list of OIDs of indexes on relation */
119 LockInfoData rd_lockInfo; /* lock mgr's info for locking relation */
120 TupleDesc rd_att; /* tuple descriptor */
121 RuleLock *rd_rules; /* rewrite rules */
122 MemoryContext rd_rulescxt; /* private memory cxt for rd_rules, if any */
123 IndexStrategy rd_istrat; /* info needed if rel is an index */
124 RegProcedure *rd_support;
125 TriggerDesc *trigdesc; /* Trigger info, or NULL if rel has none */
127 PgStat_Info pgstat_info;
130 typedef RelationData *Relation;
134 * RelationPtr is used in the executor to support index scans
135 * where we have to keep track of several index relations in an
136 * array. -cim 9/10/89
139 typedef Relation *RelationPtr;
144 * True iff relation descriptor is valid.
146 #define RelationIsValid(relation) PointerIsValid(relation)
148 #define InvalidRelation ((Relation) NULL)
151 * RelationHasReferenceCountZero
152 * True iff relation reference count is zero.
155 * Assumes relation descriptor is valid.
157 #define RelationHasReferenceCountZero(relation) \
158 ((bool)((relation)->rd_refcnt == 0))
161 * RelationSetReferenceCount
162 * Sets relation reference count.
164 #define RelationSetReferenceCount(relation,count) \
165 ((relation)->rd_refcnt = (count))
168 * RelationIncrementReferenceCount
169 * Increments relation reference count.
171 #define RelationIncrementReferenceCount(relation) \
172 ((relation)->rd_refcnt += 1)
175 * RelationDecrementReferenceCount
176 * Decrements relation reference count.
178 #define RelationDecrementReferenceCount(relation) \
179 (AssertMacro((relation)->rd_refcnt > 0), \
180 (relation)->rd_refcnt -= 1)
184 * Returns pg_class tuple for a relation.
187 * Assumes relation descriptor is valid.
189 #define RelationGetForm(relation) ((relation)->rd_rel)
194 * returns the OID of the relation
196 #define RelationGetRelid(relation) ((relation)->rd_id)
201 * Returns the open file descriptor for the rel
203 #define RelationGetFile(relation) ((relation)->rd_fd)
206 * RelationGetNumberOfAttributes
208 * Returns the number of attributes.
210 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
214 * Returns tuple descriptor for a relation.
216 #define RelationGetDescr(relation) ((relation)->rd_att)
219 * RelationGetIndexStrategy
220 * Returns index strategy for a relation.
223 * Assumes relation descriptor is valid.
224 * Assumes relation descriptor is for an index relation.
226 #define RelationGetIndexStrategy(relation) ((relation)->rd_istrat)
229 * Routines in utils/cache/rel.c
231 extern void RelationSetIndexSupport(Relation relation,
232 IndexStrategy strategy,
233 RegProcedure *support);
236 * Handle temp relations
238 #define PG_TEMP_REL_PREFIX "pg_temp"
239 #define PG_TEMP_REL_PREFIX_LEN 7
241 #define is_temp_relname(relname) \
242 (strncmp(relname, PG_TEMP_REL_PREFIX, PG_TEMP_REL_PREFIX_LEN) == 0)
245 * RelationGetPhysicalRelationName
247 * Returns the rel's physical name, ie, the name appearing in pg_class.
249 * While this name is unique across all rels in the database, it is not
250 * necessarily useful for accessing the rel, since a temp table of the
251 * same name might mask the rel. It is useful mainly for determining if
252 * the rel is a shared system rel or not.
254 * The macro is rather unfortunately named, since the pg_class name no longer
255 * has anything to do with the file name used for physical storage of the rel.
257 #define RelationGetPhysicalRelationName(relation) \
258 (NameStr((relation)->rd_rel->relname))
261 * RelationGetRelationName
263 * Returns the relation's logical name (as seen by the user).
265 * If the rel is a temp rel, the temp name will be returned. Therefore,
266 * this name is not unique. But it is the name to use in heap_openr(),
269 #define RelationGetRelationName(relation) \
271 is_temp_relname(RelationGetPhysicalRelationName(relation)) \
273 get_temp_rel_by_physicalname( \
274 RelationGetPhysicalRelationName(relation)) \
276 RelationGetPhysicalRelationName(relation) \