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.44 2001/01/24 19:43:29 momjian 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
51 typedef struct Trigger
63 int16 tgattr[FUNC_MAX_ARGS];
67 typedef struct TriggerDesc
69 /* index data to identify which triggers are which */
70 uint16 n_before_statement[4];
71 uint16 n_before_row[4];
72 uint16 n_after_row[4];
73 uint16 n_after_statement[4];
74 Trigger **tg_before_statement[4];
75 Trigger **tg_before_row[4];
76 Trigger **tg_after_row[4];
77 Trigger **tg_after_statement[4];
78 /* the actual array of triggers is here */
84 * Here are the contents of a relation cache entry.
87 typedef struct RelationData
89 File rd_fd; /* open file descriptor, or -1 if none */
90 RelFileNode rd_node; /* relation file node */
91 int rd_nblocks; /* number of blocks in rel */
92 uint16 rd_refcnt; /* reference count */
93 bool rd_myxactonly; /* rel uses the local buffer mgr */
94 bool rd_isnailed; /* rel is nailed in cache */
95 bool rd_indexfound; /* true if rd_indexlist is valid */
96 bool rd_uniqueindex; /* true if rel is a UNIQUE index */
97 Form_pg_am rd_am; /* AM tuple */
98 Form_pg_class rd_rel; /* RELATION tuple */
99 Oid rd_id; /* relation's object id */
100 List *rd_indexlist; /* list of OIDs of indexes on relation */
101 LockInfoData rd_lockInfo; /* lock mgr's info for locking relation */
102 TupleDesc rd_att; /* tuple descriptor */
103 RuleLock *rd_rules; /* rewrite rules */
104 MemoryContext rd_rulescxt; /* private memory cxt for rd_rules, if any */
105 IndexStrategy rd_istrat; /* info needed if rel is an index */
106 RegProcedure *rd_support;
107 TriggerDesc *trigdesc; /* Trigger info, or NULL if rel has none */
110 typedef RelationData *Relation;
114 * RelationPtr is used in the executor to support index scans
115 * where we have to keep track of several index relations in an
116 * array. -cim 9/10/89
119 typedef Relation *RelationPtr;
124 * True iff relation descriptor is valid.
126 #define RelationIsValid(relation) PointerIsValid(relation)
128 #define InvalidRelation ((Relation) NULL)
131 * RelationHasReferenceCountZero
132 * True iff relation reference count is zero.
135 * Assumes relation descriptor is valid.
137 #define RelationHasReferenceCountZero(relation) \
138 ((bool)((relation)->rd_refcnt == 0))
141 * RelationSetReferenceCount
142 * Sets relation reference count.
144 #define RelationSetReferenceCount(relation,count) \
145 ((relation)->rd_refcnt = (count))
148 * RelationIncrementReferenceCount
149 * Increments relation reference count.
151 #define RelationIncrementReferenceCount(relation) \
152 ((relation)->rd_refcnt += 1)
155 * RelationDecrementReferenceCount
156 * Decrements relation reference count.
158 #define RelationDecrementReferenceCount(relation) \
159 (AssertMacro((relation)->rd_refcnt > 0), \
160 (relation)->rd_refcnt -= 1)
164 * Returns pg_class tuple for a relation.
167 * Assumes relation descriptor is valid.
169 #define RelationGetForm(relation) ((relation)->rd_rel)
174 * returns the OID of the relation
176 #define RelationGetRelid(relation) ((relation)->rd_id)
181 * Returns the open file descriptor for the rel
183 #define RelationGetFile(relation) ((relation)->rd_fd)
186 * RelationGetRelationName
188 * Returns the relation's logical name (as seen by the user).
190 * If the rel is a temp rel, the temp name will be returned. Therefore,
191 * this name is not unique. But it is the name to use in heap_openr(),
194 #define RelationGetRelationName(relation) \
196 (strncmp(RelationGetPhysicalRelationName(relation), \
197 "pg_temp.", 8) != 0) \
199 RelationGetPhysicalRelationName(relation) \
201 get_temp_rel_by_physicalname( \
202 RelationGetPhysicalRelationName(relation)) \
207 * RelationGetPhysicalRelationName
209 * Returns the rel's physical name, ie, the name appearing in pg_class.
211 * While this name is unique across all rels in the database, it is not
212 * necessarily useful for accessing the rel, since a temp table of the
213 * same name might mask the rel. It is useful mainly for determining if
214 * the rel is a shared system rel or not.
216 * The macro is rather unfortunately named, since the pg_class name no longer
217 * has anything to do with the file name used for physical storage of the rel.
219 #define RelationGetPhysicalRelationName(relation) \
220 (NameStr((relation)->rd_rel->relname))
223 * RelationGetNumberOfAttributes
225 * Returns the number of attributes.
227 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
231 * Returns tuple descriptor for a relation.
233 #define RelationGetDescr(relation) ((relation)->rd_att)
236 * RelationGetIndexStrategy
237 * Returns index strategy for a relation.
240 * Assumes relation descriptor is valid.
241 * Assumes relation descriptor is for an index relation.
243 #define RelationGetIndexStrategy(relation) ((relation)->rd_istrat)
246 * Routines in utils/cache/rel.c
248 extern void RelationSetIndexSupport(Relation relation,
249 IndexStrategy strategy,
250 RegProcedure *support);