OSDN Git Service

Rearrange fmgr.c and relcache so that it's possible to keep FmgrInfo
[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.52 2001/10/06 23:21:44 tgl 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/block.h"
23 #include "storage/fd.h"
24 #include "storage/relfilenode.h"
25
26
27 /*
28  * LockRelId and LockInfo really belong to lmgr.h, but it's more convenient
29  * to declare them here so we can have a LockInfoData field in a Relation.
30  */
31
32 typedef struct LockRelId
33 {
34         Oid                     relId;                  /* a relation identifier */
35         Oid                     dbId;                   /* a database identifier */
36 } LockRelId;
37
38 typedef struct LockInfoData
39 {
40         LockRelId       lockRelId;
41 } LockInfoData;
42
43 typedef LockInfoData *LockInfo;
44
45 /*
46  * Likewise, this struct really belongs to trigger.h, but for convenience
47  * we put it here.
48  */
49 typedef struct Trigger
50 {
51         Oid                     tgoid;
52         char       *tgname;
53         Oid                     tgfoid;
54         int16           tgtype;
55         bool            tgenabled;
56         bool            tgisconstraint;
57         bool            tgdeferrable;
58         bool            tginitdeferred;
59         int16           tgnargs;
60         int16           tgattr[FUNC_MAX_ARGS];
61         char      **tgargs;
62 } Trigger;
63
64 typedef struct TriggerDesc
65 {
66         /*
67          * Index data to identify which triggers are which.  Since each trigger
68          * can appear in more than one class, for each class we provide a list
69          * of integer indexes into the triggers array.
70          */
71 #define TRIGGER_NUM_EVENT_CLASSES  4
72
73         uint16          n_before_statement[TRIGGER_NUM_EVENT_CLASSES];
74         uint16          n_before_row[TRIGGER_NUM_EVENT_CLASSES];
75         uint16          n_after_row[TRIGGER_NUM_EVENT_CLASSES];
76         uint16          n_after_statement[TRIGGER_NUM_EVENT_CLASSES];
77         int                *tg_before_statement[TRIGGER_NUM_EVENT_CLASSES];
78         int                *tg_before_row[TRIGGER_NUM_EVENT_CLASSES];
79         int                *tg_after_row[TRIGGER_NUM_EVENT_CLASSES];
80         int                *tg_after_statement[TRIGGER_NUM_EVENT_CLASSES];
81
82         /* The actual array of triggers is here */
83         Trigger    *triggers;
84         int                     numtriggers;
85 } TriggerDesc;
86
87
88 /* ----------
89  * Same for the statistics collector data in Relation and scan data.
90  * ----------
91  */
92 typedef struct  PgStat_Info
93 {
94         void                       *tabentry;
95         bool                            no_stats;
96         bool                            heap_scan_counted;
97         bool                            index_scan_counted;
98 } PgStat_Info;
99
100 /*
101  * Here are the contents of a relation cache entry.
102  */
103
104 typedef struct RelationData
105 {
106         File            rd_fd;                  /* open file descriptor, or -1 if none */
107         RelFileNode rd_node;            /* file node (physical identifier) */
108         BlockNumber     rd_nblocks;             /* number of blocks in rel */
109         BlockNumber     rd_targblock;   /* current insertion target block,
110                                                                  * or InvalidBlockNumber */
111         int                     rd_refcnt;              /* reference count */
112         bool            rd_myxactonly;  /* rel uses the local buffer mgr */
113         bool            rd_isnailed;    /* rel is nailed in cache */
114         bool            rd_indexfound;  /* true if rd_indexlist is valid */
115         bool            rd_uniqueindex; /* true if rel is a UNIQUE index */
116         Form_pg_am      rd_am;                  /* AM tuple (if an index) */
117         Form_pg_class rd_rel;           /* RELATION tuple */
118         Oid                     rd_id;                  /* relation's object id */
119         List       *rd_indexlist;       /* list of OIDs of indexes on relation */
120         LockInfoData rd_lockInfo;       /* lock mgr's info for locking relation */
121         TupleDesc       rd_att;                 /* tuple descriptor */
122         RuleLock   *rd_rules;           /* rewrite rules */
123         MemoryContext rd_rulescxt;      /* private memory cxt for rd_rules, if any */
124         TriggerDesc *trigdesc;          /* Trigger info, or NULL if rel has none */
125
126         /* index access support info (used only for an index relation) */
127         MemoryContext rd_indexcxt;      /* private memory cxt for this stuff */
128         IndexStrategy rd_istrat;        /* operator strategy map */
129         RegProcedure *rd_support;       /* OIDs of support procedures */
130         struct FmgrInfo *rd_supportinfo; /* lookup info for support procedures */
131         /* "struct FmgrInfo" avoids need to include fmgr.h here */
132
133         /* statistics collection area */
134         PgStat_Info     pgstat_info;
135 } RelationData;
136
137 typedef RelationData *Relation;
138
139
140 /* ----------------
141  *              RelationPtr is used in the executor to support index scans
142  *              where we have to keep track of several index relations in an
143  *              array.  -cim 9/10/89
144  * ----------------
145  */
146 typedef Relation *RelationPtr;
147
148
149 /*
150  * RelationIsValid
151  *              True iff relation descriptor is valid.
152  */
153 #define RelationIsValid(relation) PointerIsValid(relation)
154
155 #define InvalidRelation ((Relation) NULL)
156
157 /*
158  * RelationHasReferenceCountZero
159  *              True iff relation reference count is zero.
160  *
161  * Note:
162  *              Assumes relation descriptor is valid.
163  */
164 #define RelationHasReferenceCountZero(relation) \
165                 ((bool)((relation)->rd_refcnt == 0))
166
167 /*
168  * RelationSetReferenceCount
169  *              Sets relation reference count.
170  */
171 #define RelationSetReferenceCount(relation,count) \
172         ((relation)->rd_refcnt = (count))
173
174 /*
175  * RelationIncrementReferenceCount
176  *              Increments relation reference count.
177  */
178 #define RelationIncrementReferenceCount(relation) \
179         ((relation)->rd_refcnt += 1)
180
181 /*
182  * RelationDecrementReferenceCount
183  *              Decrements relation reference count.
184  */
185 #define RelationDecrementReferenceCount(relation) \
186         (AssertMacro((relation)->rd_refcnt > 0), \
187          (relation)->rd_refcnt -= 1)
188
189 /*
190  * RelationGetForm
191  *              Returns pg_class tuple for a relation.
192  *
193  * Note:
194  *              Assumes relation descriptor is valid.
195  */
196 #define RelationGetForm(relation) ((relation)->rd_rel)
197
198 /*
199  * RelationGetRelid
200  *
201  *      returns the OID of the relation
202  */
203 #define RelationGetRelid(relation) ((relation)->rd_id)
204
205 /*
206  * RelationGetFile
207  *
208  *        Returns the open file descriptor for the rel
209  */
210 #define RelationGetFile(relation) ((relation)->rd_fd)
211
212 /*
213  * RelationGetNumberOfAttributes
214  *
215  *        Returns the number of attributes.
216  */
217 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
218
219 /*
220  * RelationGetDescr
221  *              Returns tuple descriptor for a relation.
222  */
223 #define RelationGetDescr(relation) ((relation)->rd_att)
224
225 /*
226  * RelationGetIndexStrategy
227  *              Returns index strategy for a relation.
228  *
229  * Note:
230  *              Assumes relation descriptor is valid.
231  *              Assumes relation descriptor is for an index relation.
232  */
233 #define RelationGetIndexStrategy(relation) ((relation)->rd_istrat)
234
235 /*
236  * Handle temp relations
237  */
238 #define PG_TEMP_REL_PREFIX "pg_temp"
239 #define PG_TEMP_REL_PREFIX_LEN 7
240
241 #define is_temp_relname(relname) \
242                 (strncmp(relname, PG_TEMP_REL_PREFIX, PG_TEMP_REL_PREFIX_LEN) == 0)
243
244 /*
245  * RelationGetPhysicalRelationName
246  *
247  *        Returns the rel's physical name, ie, the name appearing in pg_class.
248  *
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.
253  *
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.
256  */
257 #define RelationGetPhysicalRelationName(relation) \
258         (NameStr((relation)->rd_rel->relname))
259
260 /*
261  * RelationGetRelationName
262  *
263  *        Returns the relation's logical name (as seen by the user).
264  *
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(),
267  * for example.
268  */
269 #define RelationGetRelationName(relation) \
270 (\
271         is_temp_relname(RelationGetPhysicalRelationName(relation)) \
272         ? \
273                 get_temp_rel_by_physicalname( \
274                         RelationGetPhysicalRelationName(relation)) \
275         : \
276                 RelationGetPhysicalRelationName(relation) \
277 )
278
279 /* added to prevent circular dependency.  bjm 1999/11/15 */
280 extern char *get_temp_rel_by_physicalname(const char *relname);
281
282 #endif   /* REL_H */