OSDN Git Service

Convert oidvector and int2vector into variable-length arrays. This
[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-2005, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $PostgreSQL: pgsql/src/include/utils/rel.h,v 1.83 2005/03/29 00:17:18 tgl Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef REL_H
15 #define REL_H
16
17 #include "access/tupdesc.h"
18 #include "catalog/pg_am.h"
19 #include "catalog/pg_class.h"
20 #include "catalog/pg_index.h"
21 #include "rewrite/prs2lock.h"
22 #include "storage/block.h"
23 #include "storage/relfilenode.h"
24
25
26 /*
27  * LockRelId and LockInfo really belong to lmgr.h, but it's more convenient
28  * to declare them here so we can have a LockInfoData field in a Relation.
29  */
30
31 typedef struct LockRelId
32 {
33         Oid                     relId;                  /* a relation identifier */
34         Oid                     dbId;                   /* a database identifier */
35 } LockRelId;
36
37 typedef struct LockInfoData
38 {
39         LockRelId       lockRelId;
40 } LockInfoData;
41
42 typedef LockInfoData *LockInfo;
43
44 /*
45  * Likewise, this struct really belongs to trigger.h, but for convenience
46  * we put it here.
47  */
48 typedef struct Trigger
49 {
50         Oid                     tgoid;                  /* OID of trigger (pg_trigger row) */
51         /* Remaining fields are copied from pg_trigger, see pg_trigger.h */
52         char       *tgname;
53         Oid                     tgfoid;
54         int16           tgtype;
55         bool            tgenabled;
56         bool            tgisconstraint;
57         Oid                     tgconstrrelid;
58         bool            tgdeferrable;
59         bool            tginitdeferred;
60         int16           tgnargs;
61         int16           tgnattr;
62         int16      *tgattr;
63         char      **tgargs;
64 } Trigger;
65
66 typedef struct TriggerDesc
67 {
68         /*
69          * Index data to identify which triggers are which.  Since each
70          * trigger can appear in more than one class, for each class we
71          * provide a list of integer indexes into the triggers array.
72          */
73 #define TRIGGER_NUM_EVENT_CLASSES  3
74
75         uint16          n_before_statement[TRIGGER_NUM_EVENT_CLASSES];
76         uint16          n_before_row[TRIGGER_NUM_EVENT_CLASSES];
77         uint16          n_after_row[TRIGGER_NUM_EVENT_CLASSES];
78         uint16          n_after_statement[TRIGGER_NUM_EVENT_CLASSES];
79         int                *tg_before_statement[TRIGGER_NUM_EVENT_CLASSES];
80         int                *tg_before_row[TRIGGER_NUM_EVENT_CLASSES];
81         int                *tg_after_row[TRIGGER_NUM_EVENT_CLASSES];
82         int                *tg_after_statement[TRIGGER_NUM_EVENT_CLASSES];
83
84         /* The actual array of triggers is here */
85         Trigger    *triggers;
86         int                     numtriggers;
87 } TriggerDesc;
88
89
90 /*
91  * Same for the statistics collector data in Relation and scan data.
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 /*
103  * Here are the contents of a relation cache entry.
104  */
105
106 typedef struct RelationData
107 {
108         RelFileNode rd_node;            /* relation physical identifier */
109         /* use "struct" here to avoid needing to include smgr.h: */
110         struct SMgrRelationData *rd_smgr;       /* cached file handle, or NULL */
111         BlockNumber rd_targblock;       /* current insertion target block, or
112                                                                  * InvalidBlockNumber */
113         int                     rd_refcnt;              /* reference count */
114         bool            rd_istemp;              /* rel uses the local buffer mgr */
115         bool            rd_isnailed;    /* rel is nailed in cache */
116         bool            rd_isvalid;             /* relcache entry is valid */
117         char            rd_indexvalid;  /* state of rd_indexlist: 0 = not valid, 1
118                                                                  * = valid, 2 = temporarily forced */
119         SubTransactionId rd_createSubid;        /* rel was created in current xact */
120
121         /*
122          * rd_createSubid is the ID of the highest subtransaction the rel has
123          * survived into; or zero if the rel was not created in the current
124          * top transaction.  This should be relied on only for optimization
125          * purposes; it is possible for new-ness to be "forgotten" (eg, after
126          * CLUSTER).
127          */
128         Form_pg_class rd_rel;           /* RELATION tuple */
129         TupleDesc       rd_att;                 /* tuple descriptor */
130         Oid                     rd_id;                  /* relation's object id */
131         List       *rd_indexlist;       /* list of OIDs of indexes on relation */
132         LockInfoData rd_lockInfo;       /* lock mgr's info for locking relation */
133         RuleLock   *rd_rules;           /* rewrite rules */
134         MemoryContext rd_rulescxt;      /* private memory cxt for rd_rules, if any */
135         TriggerDesc *trigdesc;          /* Trigger info, or NULL if rel has none */
136
137         /* These are non-NULL only for an index relation: */
138         Form_pg_index rd_index;         /* pg_index tuple describing this index */
139         struct HeapTupleData *rd_indextuple;            /* all of pg_index tuple */
140         /* "struct HeapTupleData *" avoids need to include htup.h here  */
141         oidvector  *rd_indclass;        /* extracted pointer to rd_index field */
142         Form_pg_am      rd_am;                  /* pg_am tuple for index's AM */
143
144         /*
145          * index access support info (used only for an index relation)
146          *
147          * Note: only default operators and support procs for each opclass are
148          * cached, namely those with subtype zero.      The arrays are indexed by
149          * strategy or support number, which is a sufficient identifier given
150          * that restriction.
151          */
152         MemoryContext rd_indexcxt;      /* private memory cxt for this stuff */
153         Oid                *rd_operator;        /* OIDs of index operators */
154         RegProcedure *rd_support;       /* OIDs of support procedures */
155         struct FmgrInfo *rd_supportinfo;        /* lookup info for support
156                                                                                  * procedures */
157         /* "struct FmgrInfo" avoids need to include fmgr.h here */
158         List       *rd_indexprs;        /* index expression trees, if any */
159         List       *rd_indpred;         /* index predicate tree, if any */
160
161         /* statistics collection area */
162         PgStat_Info pgstat_info;
163 } RelationData;
164
165 typedef RelationData *Relation;
166
167
168 /* ----------------
169  *              RelationPtr is used in the executor to support index scans
170  *              where we have to keep track of several index relations in an
171  *              array.  -cim 9/10/89
172  * ----------------
173  */
174 typedef Relation *RelationPtr;
175
176
177 /*
178  * RelationIsValid
179  *              True iff relation descriptor is valid.
180  */
181 #define RelationIsValid(relation) PointerIsValid(relation)
182
183 #define InvalidRelation ((Relation) NULL)
184
185 /*
186  * RelationHasReferenceCountZero
187  *              True iff relation reference count is zero.
188  *
189  * Note:
190  *              Assumes relation descriptor is valid.
191  */
192 #define RelationHasReferenceCountZero(relation) \
193                 ((bool)((relation)->rd_refcnt == 0))
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  *              Returns the OID of the relation
207  */
208 #define RelationGetRelid(relation) ((relation)->rd_id)
209
210 /*
211  * RelationGetNumberOfAttributes
212  *              Returns the number of attributes in a relation.
213  */
214 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
215
216 /*
217  * RelationGetDescr
218  *              Returns tuple descriptor for a relation.
219  */
220 #define RelationGetDescr(relation) ((relation)->rd_att)
221
222 /*
223  * RelationGetRelationName
224  *              Returns the rel's name.
225  *
226  * Note that the name is only unique within the containing namespace.
227  */
228 #define RelationGetRelationName(relation) \
229         (NameStr((relation)->rd_rel->relname))
230
231 /*
232  * RelationGetNamespace
233  *              Returns the rel's namespace OID.
234  */
235 #define RelationGetNamespace(relation) \
236         ((relation)->rd_rel->relnamespace)
237
238 /*
239  * RelationOpenSmgr
240  *              Open the relation at the smgr level, if not already done.
241  */
242 #define RelationOpenSmgr(relation) \
243         do { \
244                 if ((relation)->rd_smgr == NULL) \
245                         smgrsetowner(&((relation)->rd_smgr), smgropen((relation)->rd_node)); \
246         } while (0)
247
248 /*
249  * RelationCloseSmgr
250  *              Close the relation at the smgr level, if not already done.
251  *
252  * Note: smgrclose should unhook from owner pointer, hence the Assert.
253  */
254 #define RelationCloseSmgr(relation) \
255         do { \
256                 if ((relation)->rd_smgr != NULL) \
257                 { \
258                         smgrclose((relation)->rd_smgr); \
259                         Assert((relation)->rd_smgr == NULL); \
260                 } \
261         } while (0)
262
263 /*
264  * RELATION_IS_LOCAL
265  *              If a rel is either temp or newly created in the current transaction,
266  *              it can be assumed to be visible only to the current backend.
267  *
268  * Beware of multiple eval of argument
269  */
270 #define RELATION_IS_LOCAL(relation) \
271         ((relation)->rd_istemp || \
272          (relation)->rd_createSubid != InvalidSubTransactionId)
273
274 /* routines in utils/cache/relcache.c */
275 extern void RelationIncrementReferenceCount(Relation rel);
276 extern void RelationDecrementReferenceCount(Relation rel);
277
278 #endif   /* REL_H */