OSDN Git Service

Move temprel name define from temprel.h to rel.h.
[pg-rex/syncrep.git] / src / include / utils / rel.h
1 /*-------------------------------------------------------------------------
2  *
3  * rel.h
4  *        POSTGRES relation descriptor (a/k/a relcache entry) definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $Id: rel.h,v 1.48 2001/06/19 12:03:41 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef REL_H
15 #define REL_H
16
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"
24
25 /* added to prevent circular dependency.  bjm 1999/11/15 */
26 extern char *get_temp_rel_by_physicalname(const char *relname);
27
28 /*
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.
31  */
32
33 typedef struct LockRelId
34 {
35         Oid                     relId;                  /* a relation identifier */
36         Oid                     dbId;                   /* a database identifier */
37 } LockRelId;
38
39 typedef struct LockInfoData
40 {
41         LockRelId       lockRelId;
42 } LockInfoData;
43
44 typedef LockInfoData *LockInfo;
45
46 /*
47  * Likewise, this struct really belongs to trigger.h, but for convenience
48  * we put it here.
49  */
50 typedef struct Trigger
51 {
52         Oid                     tgoid;
53         char       *tgname;
54         Oid                     tgfoid;
55         int16           tgtype;
56         bool            tgenabled;
57         bool            tgisconstraint;
58         bool            tgdeferrable;
59         bool            tginitdeferred;
60         int16           tgnargs;
61         int16           tgattr[FUNC_MAX_ARGS];
62         char      **tgargs;
63 } Trigger;
64
65 typedef struct TriggerDesc
66 {
67         /*
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.
71          */
72 #define TRIGGER_NUM_EVENT_CLASSES  4
73
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];
82
83         /* The actual array of triggers is here */
84         Trigger    *triggers;
85         int                     numtriggers;
86 } TriggerDesc;
87
88 /*
89  * Here are the contents of a relation cache entry.
90  */
91
92 typedef struct RelationData
93 {
94         File            rd_fd;                  /* open file descriptor, or -1 if none */
95         RelFileNode rd_node;            /* relation file node */
96         int                     rd_nblocks;             /* number of blocks in rel */
97         uint16          rd_refcnt;              /* reference count */
98         bool            rd_myxactonly;  /* rel uses the local buffer mgr */
99         bool            rd_isnailed;    /* rel is nailed in cache */
100         bool            rd_indexfound;  /* true if rd_indexlist is valid */
101         bool            rd_uniqueindex; /* true if rel is a UNIQUE index */
102         Form_pg_am      rd_am;                  /* AM tuple */
103         Form_pg_class rd_rel;           /* RELATION tuple */
104         Oid                     rd_id;                  /* relation's object id */
105         List       *rd_indexlist;       /* list of OIDs of indexes on relation */
106         LockInfoData rd_lockInfo;       /* lock mgr's info for locking relation */
107         TupleDesc       rd_att;                 /* tuple descriptor */
108         RuleLock   *rd_rules;           /* rewrite rules */
109         MemoryContext rd_rulescxt;      /* private memory cxt for rd_rules, if any */
110         IndexStrategy rd_istrat;        /* info needed if rel is an index */
111         RegProcedure *rd_support;
112         TriggerDesc *trigdesc;          /* Trigger info, or NULL if rel has none */
113 } RelationData;
114
115 typedef RelationData *Relation;
116
117
118 /* ----------------
119  *              RelationPtr is used in the executor to support index scans
120  *              where we have to keep track of several index relations in an
121  *              array.  -cim 9/10/89
122  * ----------------
123  */
124 typedef Relation *RelationPtr;
125
126
127 /*
128  * RelationIsValid
129  *              True iff relation descriptor is valid.
130  */
131 #define RelationIsValid(relation) PointerIsValid(relation)
132
133 #define InvalidRelation ((Relation) NULL)
134
135 /*
136  * RelationHasReferenceCountZero
137  *              True iff relation reference count is zero.
138  *
139  * Note:
140  *              Assumes relation descriptor is valid.
141  */
142 #define RelationHasReferenceCountZero(relation) \
143                 ((bool)((relation)->rd_refcnt == 0))
144
145 /*
146  * RelationSetReferenceCount
147  *              Sets relation reference count.
148  */
149 #define RelationSetReferenceCount(relation,count) \
150         ((relation)->rd_refcnt = (count))
151
152 /*
153  * RelationIncrementReferenceCount
154  *              Increments relation reference count.
155  */
156 #define RelationIncrementReferenceCount(relation) \
157         ((relation)->rd_refcnt += 1)
158
159 /*
160  * RelationDecrementReferenceCount
161  *              Decrements relation reference count.
162  */
163 #define RelationDecrementReferenceCount(relation) \
164         (AssertMacro((relation)->rd_refcnt > 0), \
165          (relation)->rd_refcnt -= 1)
166
167 /*
168  * RelationGetForm
169  *              Returns pg_class tuple for a relation.
170  *
171  * Note:
172  *              Assumes relation descriptor is valid.
173  */
174 #define RelationGetForm(relation) ((relation)->rd_rel)
175
176 /*
177  * RelationGetRelid
178  *
179  *      returns the OID of the relation
180  */
181 #define RelationGetRelid(relation) ((relation)->rd_id)
182
183 /*
184  * RelationGetFile
185  *
186  *        Returns the open file descriptor for the rel
187  */
188 #define RelationGetFile(relation) ((relation)->rd_fd)
189
190 /*
191  * RelationGetNumberOfAttributes
192  *
193  *        Returns the number of attributes.
194  */
195 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
196
197 /*
198  * RelationGetDescr
199  *              Returns tuple descriptor for a relation.
200  */
201 #define RelationGetDescr(relation) ((relation)->rd_att)
202
203 /*
204  * RelationGetIndexStrategy
205  *              Returns index strategy for a relation.
206  *
207  * Note:
208  *              Assumes relation descriptor is valid.
209  *              Assumes relation descriptor is for an index relation.
210  */
211 #define RelationGetIndexStrategy(relation) ((relation)->rd_istrat)
212
213 /*
214  * Routines in utils/cache/rel.c
215  */
216 extern void RelationSetIndexSupport(Relation relation,
217                                                 IndexStrategy strategy,
218                                                 RegProcedure *support);
219
220 /*
221  * Handle temp relations
222  */
223 #define PG_TEMP_REL_PREFIX "pg_temp"
224
225 #define is_temp_relname(relname) \
226                 (strncmp(relname, PG_TEMP_REL_PREFIX, strlen(PG_TEMP_REL_PREFIX)) == 0)
227
228 /*
229  * RelationGetPhysicalRelationName
230  *
231  *        Returns the rel's physical name, ie, the name appearing in pg_class.
232  *
233  * While this name is unique across all rels in the database, it is not
234  * necessarily useful for accessing the rel, since a temp table of the
235  * same name might mask the rel.  It is useful mainly for determining if
236  * the rel is a shared system rel or not.
237  *
238  * The macro is rather unfortunately named, since the pg_class name no longer
239  * has anything to do with the file name used for physical storage of the rel.
240  */
241 #define RelationGetPhysicalRelationName(relation) \
242         (NameStr((relation)->rd_rel->relname))
243
244 /*
245  * RelationGetRelationName
246  *
247  *        Returns the relation's logical name (as seen by the user).
248  *
249  * If the rel is a temp rel, the temp name will be returned.  Therefore,
250  * this name is not unique.  But it is the name to use in heap_openr(),
251  * for example.
252  */
253 #define RelationGetRelationName(relation) \
254 (\
255         !is_temp_relname(relation) \
256         ? \
257                 RelationGetPhysicalRelationName(relation) \
258         : \
259                 get_temp_rel_by_physicalname( \
260                         RelationGetPhysicalRelationName(relation)) \
261 )
262
263
264 #endif   /* REL_H */