OSDN Git Service

A bunch of changes aimed at reducing backend startup time...
[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.56 2002/02/19 20:11:19 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 "catalog/pg_index.h"
22 #include "rewrite/prs2lock.h"
23 #include "storage/block.h"
24 #include "storage/fd.h"
25 #include "storage/relfilenode.h"
26
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
69          * trigger can appear in more than one class, for each class we
70          * provide a list 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 /* ----------
90  * Same for the statistics collector data in Relation and scan data.
91  * ----------
92  */
93 typedef struct PgStat_Info
94 {
95         void       *tabentry;
96         bool            no_stats;
97         bool            heap_scan_counted;
98         bool            index_scan_counted;
99 } PgStat_Info;
100
101 /*
102  * Here are the contents of a relation cache entry.
103  */
104
105 typedef struct RelationData
106 {
107         File            rd_fd;                  /* open file descriptor, or -1 if none */
108         RelFileNode rd_node;            /* file node (physical identifier) */
109         BlockNumber rd_nblocks;         /* number of blocks in rel */
110         BlockNumber rd_targblock;       /* current insertion target block, or
111                                                                  * InvalidBlockNumber */
112         int                     rd_refcnt;              /* reference count */
113         bool            rd_myxactonly;  /* rel uses the local buffer mgr */
114         bool            rd_isnailed;    /* rel is nailed in cache */
115         bool            rd_indexfound;  /* true if rd_indexlist is valid */
116         bool            rd_uniqueindex; /* true if rel is a UNIQUE index */
117         Form_pg_class rd_rel;           /* RELATION tuple */
118         TupleDesc       rd_att;                 /* tuple descriptor */
119         Oid                     rd_id;                  /* relation's object id */
120         List       *rd_indexlist;       /* list of OIDs of indexes on relation */
121         LockInfoData rd_lockInfo;       /* lock mgr's info for locking relation */
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         /* These are non-NULL only for an index relation: */
127         Form_pg_index rd_index;         /* pg_index tuple describing this index */
128         Form_pg_am      rd_am;                  /* pg_am tuple for index's AM */
129
130         /* index access support info (used only for an index relation) */
131         MemoryContext rd_indexcxt;      /* private memory cxt for this stuff */
132         IndexStrategy rd_istrat;        /* operator strategy map */
133         Oid                *rd_operator;        /* OIDs of index operators */
134         RegProcedure *rd_support;       /* OIDs of support procedures */
135         struct FmgrInfo *rd_supportinfo;        /* lookup info for support
136                                                                                  * procedures */
137         /* "struct FmgrInfo" avoids need to include fmgr.h here */
138
139         /* statistics collection area */
140         PgStat_Info pgstat_info;
141 } RelationData;
142
143 typedef RelationData *Relation;
144
145
146 /* ----------------
147  *              RelationPtr is used in the executor to support index scans
148  *              where we have to keep track of several index relations in an
149  *              array.  -cim 9/10/89
150  * ----------------
151  */
152 typedef Relation *RelationPtr;
153
154
155 /*
156  * RelationIsValid
157  *              True iff relation descriptor is valid.
158  */
159 #define RelationIsValid(relation) PointerIsValid(relation)
160
161 #define InvalidRelation ((Relation) NULL)
162
163 /*
164  * RelationHasReferenceCountZero
165  *              True iff relation reference count is zero.
166  *
167  * Note:
168  *              Assumes relation descriptor is valid.
169  */
170 #define RelationHasReferenceCountZero(relation) \
171                 ((bool)((relation)->rd_refcnt == 0))
172
173 /*
174  * RelationSetReferenceCount
175  *              Sets relation reference count.
176  */
177 #define RelationSetReferenceCount(relation,count) \
178         ((relation)->rd_refcnt = (count))
179
180 /*
181  * RelationIncrementReferenceCount
182  *              Increments relation reference count.
183  */
184 #define RelationIncrementReferenceCount(relation) \
185         ((relation)->rd_refcnt += 1)
186
187 /*
188  * RelationDecrementReferenceCount
189  *              Decrements relation reference count.
190  */
191 #define RelationDecrementReferenceCount(relation) \
192         (AssertMacro((relation)->rd_refcnt > 0), \
193          (relation)->rd_refcnt -= 1)
194
195 /*
196  * RelationGetForm
197  *              Returns pg_class tuple for a relation.
198  *
199  * Note:
200  *              Assumes relation descriptor is valid.
201  */
202 #define RelationGetForm(relation) ((relation)->rd_rel)
203
204 /*
205  * RelationGetRelid
206  *
207  *      returns the OID of the relation
208  */
209 #define RelationGetRelid(relation) ((relation)->rd_id)
210
211 /*
212  * RelationGetFile
213  *
214  *        Returns the open file descriptor for the rel
215  */
216 #define RelationGetFile(relation) ((relation)->rd_fd)
217
218 /*
219  * RelationGetNumberOfAttributes
220  *
221  *        Returns the number of attributes.
222  */
223 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
224
225 /*
226  * RelationGetDescr
227  *              Returns tuple descriptor for a relation.
228  */
229 #define RelationGetDescr(relation) ((relation)->rd_att)
230
231 /*
232  * RelationGetIndexStrategy
233  *              Returns index strategy for a relation.
234  *
235  * Note:
236  *              Assumes relation descriptor is valid.
237  *              Assumes relation descriptor is for an index relation.
238  */
239 #define RelationGetIndexStrategy(relation) ((relation)->rd_istrat)
240
241 /*
242  * Handle temp relations
243  */
244 #define PG_TEMP_REL_PREFIX "pg_temp"
245 #define PG_TEMP_REL_PREFIX_LEN 7
246
247 #define is_temp_relname(relname) \
248                 (strncmp(relname, PG_TEMP_REL_PREFIX, PG_TEMP_REL_PREFIX_LEN) == 0)
249
250 /*
251  * RelationGetPhysicalRelationName
252  *
253  *        Returns the rel's physical name, ie, the name appearing in pg_class.
254  *
255  * While this name is unique across all rels in the database, it is not
256  * necessarily useful for accessing the rel, since a temp table of the
257  * same name might mask the rel.  It is useful mainly for determining if
258  * the rel is a shared system rel or not.
259  *
260  * The macro is rather unfortunately named, since the pg_class name no longer
261  * has anything to do with the file name used for physical storage of the rel.
262  */
263 #define RelationGetPhysicalRelationName(relation) \
264         (NameStr((relation)->rd_rel->relname))
265
266 /*
267  * RelationGetRelationName
268  *
269  *        Returns the relation's logical name (as seen by the user).
270  *
271  * If the rel is a temp rel, the temp name will be returned.  Therefore,
272  * this name is not unique.  But it is the name to use in heap_openr(),
273  * for example.
274  */
275 #define RelationGetRelationName(relation) \
276 (\
277         is_temp_relname(RelationGetPhysicalRelationName(relation)) \
278         ? \
279                 get_temp_rel_by_physicalname( \
280                         RelationGetPhysicalRelationName(relation)) \
281         : \
282                 RelationGetPhysicalRelationName(relation) \
283 )
284
285 /* added to prevent circular dependency.  bjm 1999/11/15 */
286 extern char *get_temp_rel_by_physicalname(const char *relname);
287
288 #endif   /* REL_H */