OSDN Git Service

Restructure operator classes to allow improved handling of cross-data-type
[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-2006, 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.93 2006/12/23 00:43:13 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 "fmgr.h"
22 #include "rewrite/prs2lock.h"
23 #include "storage/block.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;                  /* OID of trigger (pg_trigger row) */
52         /* Remaining fields are copied from pg_trigger, see pg_trigger.h */
53         char       *tgname;
54         Oid                     tgfoid;
55         int16           tgtype;
56         bool            tgenabled;
57         bool            tgisconstraint;
58         Oid                     tgconstrrelid;
59         bool            tgdeferrable;
60         bool            tginitdeferred;
61         int16           tgnargs;
62         int16           tgnattr;
63         int16      *tgattr;
64         char      **tgargs;
65 } Trigger;
66
67 typedef struct TriggerDesc
68 {
69         /*
70          * Index data to identify which triggers are which.  Since each trigger
71          * can appear in more than one class, for each class we provide a list of
72          * integer indexes into the triggers array.
73          */
74 #define TRIGGER_NUM_EVENT_CLASSES  3
75
76         uint16          n_before_statement[TRIGGER_NUM_EVENT_CLASSES];
77         uint16          n_before_row[TRIGGER_NUM_EVENT_CLASSES];
78         uint16          n_after_row[TRIGGER_NUM_EVENT_CLASSES];
79         uint16          n_after_statement[TRIGGER_NUM_EVENT_CLASSES];
80         int                *tg_before_statement[TRIGGER_NUM_EVENT_CLASSES];
81         int                *tg_before_row[TRIGGER_NUM_EVENT_CLASSES];
82         int                *tg_after_row[TRIGGER_NUM_EVENT_CLASSES];
83         int                *tg_after_statement[TRIGGER_NUM_EVENT_CLASSES];
84
85         /* The actual array of triggers is here */
86         Trigger    *triggers;
87         int                     numtriggers;
88 } TriggerDesc;
89
90
91 /*
92  * Same for the statistics collector data in Relation and scan data.
93  */
94 typedef struct PgStat_Info
95 {
96         void       *tabentry;
97 } PgStat_Info;
98
99
100 /*
101  * Cached lookup information for the index access method functions defined
102  * by the pg_am row associated with an index relation.
103  */
104 typedef struct RelationAmInfo
105 {
106         FmgrInfo        aminsert;
107         FmgrInfo        ambeginscan;
108         FmgrInfo        amgettuple;
109         FmgrInfo        amgetmulti;
110         FmgrInfo        amrescan;
111         FmgrInfo        amendscan;
112         FmgrInfo        ammarkpos;
113         FmgrInfo        amrestrpos;
114         FmgrInfo        ambuild;
115         FmgrInfo        ambulkdelete;
116         FmgrInfo        amvacuumcleanup;
117         FmgrInfo        amcostestimate;
118         FmgrInfo        amoptions;
119 } RelationAmInfo;
120
121
122 /*
123  * Here are the contents of a relation cache entry.
124  */
125
126 typedef struct RelationData
127 {
128         RelFileNode rd_node;            /* relation physical identifier */
129         /* use "struct" here to avoid needing to include smgr.h: */
130         struct SMgrRelationData *rd_smgr;       /* cached file handle, or NULL */
131         BlockNumber rd_targblock;       /* current insertion target block, or
132                                                                  * InvalidBlockNumber */
133         int                     rd_refcnt;              /* reference count */
134         bool            rd_istemp;              /* rel uses the local buffer mgr */
135         bool            rd_isnailed;    /* rel is nailed in cache */
136         bool            rd_isvalid;             /* relcache entry is valid */
137         char            rd_indexvalid;  /* state of rd_indexlist: 0 = not valid, 1 =
138                                                                  * valid, 2 = temporarily forced */
139         SubTransactionId rd_createSubid;        /* rel was created in current xact */
140
141         /*
142          * rd_createSubid is the ID of the highest subtransaction the rel has
143          * survived into; or zero if the rel was not created in the current top
144          * transaction.  This should be relied on only for optimization purposes;
145          * it is possible for new-ness to be "forgotten" (eg, after CLUSTER).
146          */
147         Form_pg_class rd_rel;           /* RELATION tuple */
148         TupleDesc       rd_att;                 /* tuple descriptor */
149         Oid                     rd_id;                  /* relation's object id */
150         List       *rd_indexlist;       /* list of OIDs of indexes on relation */
151         Oid                     rd_oidindex;    /* OID of unique index on OID, if any */
152         LockInfoData rd_lockInfo;       /* lock mgr's info for locking relation */
153         RuleLock   *rd_rules;           /* rewrite rules */
154         MemoryContext rd_rulescxt;      /* private memory cxt for rd_rules, if any */
155         TriggerDesc *trigdesc;          /* Trigger info, or NULL if rel has none */
156
157         /*
158          * rd_options is set whenever rd_rel is loaded into the relcache entry.
159          * Note that you can NOT look into rd_rel for this data.  NULL means "use
160          * defaults".
161          */
162         bytea      *rd_options;         /* parsed pg_class.reloptions */
163
164         /* These are non-NULL only for an index relation: */
165         Form_pg_index rd_index;         /* pg_index tuple describing this index */
166         struct HeapTupleData *rd_indextuple;            /* all of pg_index tuple */
167         /* "struct HeapTupleData *" avoids need to include htup.h here  */
168         Form_pg_am      rd_am;                  /* pg_am tuple for index's AM */
169
170         /*
171          * index access support info (used only for an index relation)
172          *
173          * Note: only default operators and support procs for each opclass are
174          * cached, namely those with lefttype and righttype equal to the opclass's
175          * opcintype.  The arrays are indexed by strategy or support number,
176          * which is a sufficient identifier given that restriction.
177          *
178          * Note: rd_amcache is available for index AMs to cache private data about
179          * an index.  This must be just a cache since it may get reset at any time
180          * (in particular, it will get reset by a relcache inval message for the
181          * index).      If used, it must point to a single memory chunk palloc'd in
182          * rd_indexcxt.  A relcache reset will include freeing that chunk and
183          * setting rd_amcache = NULL.
184          */
185         MemoryContext rd_indexcxt;      /* private memory cxt for this stuff */
186         RelationAmInfo *rd_aminfo;      /* lookup info for funcs found in pg_am */
187         Oid                *rd_opfamily;        /* OIDs of op families for each index col */
188         Oid                *rd_opcintype;       /* OIDs of opclass declared input data types */
189         Oid                *rd_operator;        /* OIDs of index operators */
190         RegProcedure *rd_support;       /* OIDs of support procedures */
191         FmgrInfo   *rd_supportinfo; /* lookup info for support procedures */
192         List       *rd_indexprs;        /* index expression trees, if any */
193         List       *rd_indpred;         /* index predicate tree, if any */
194         void       *rd_amcache;         /* available for use by index AM */
195
196         /* statistics collection area */
197         PgStat_Info pgstat_info;
198 } RelationData;
199
200 typedef RelationData *Relation;
201
202
203 /* ----------------
204  *              RelationPtr is used in the executor to support index scans
205  *              where we have to keep track of several index relations in an
206  *              array.  -cim 9/10/89
207  * ----------------
208  */
209 typedef Relation *RelationPtr;
210
211
212 /*
213  * StdRdOptions
214  *              Standard contents of rd_options for heaps and generic indexes.
215  *
216  * RelationGetFillFactor() and RelationGetTargetPageFreeSpace() can only
217  * be applied to relations that use this format or a superset for
218  * private options data.
219  */
220 typedef struct StdRdOptions
221 {
222         int32           vl_len;                 /* required to be a bytea */
223         int                     fillfactor;             /* page fill factor in percent (0..100) */
224 } StdRdOptions;
225
226 #define HEAP_MIN_FILLFACTOR                     10
227 #define HEAP_DEFAULT_FILLFACTOR         100
228
229 /*
230  * RelationGetFillFactor
231  *              Returns the relation's fillfactor.  Note multiple eval of argument!
232  */
233 #define RelationGetFillFactor(relation, defaultff) \
234         ((relation)->rd_options ? \
235          ((StdRdOptions *) (relation)->rd_options)->fillfactor : (defaultff))
236
237 /*
238  * RelationGetTargetPageUsage
239  *              Returns the relation's desired space usage per page in bytes.
240  */
241 #define RelationGetTargetPageUsage(relation, defaultff) \
242         (BLCKSZ * RelationGetFillFactor(relation, defaultff) / 100)
243
244 /*
245  * RelationGetTargetPageFreeSpace
246  *              Returns the relation's desired freespace per page in bytes.
247  */
248 #define RelationGetTargetPageFreeSpace(relation, defaultff) \
249         (BLCKSZ * (100 - RelationGetFillFactor(relation, defaultff)) / 100)
250
251 /*
252  * RelationIsValid
253  *              True iff relation descriptor is valid.
254  */
255 #define RelationIsValid(relation) PointerIsValid(relation)
256
257 #define InvalidRelation ((Relation) NULL)
258
259 /*
260  * RelationHasReferenceCountZero
261  *              True iff relation reference count is zero.
262  *
263  * Note:
264  *              Assumes relation descriptor is valid.
265  */
266 #define RelationHasReferenceCountZero(relation) \
267                 ((bool)((relation)->rd_refcnt == 0))
268
269 /*
270  * RelationGetForm
271  *              Returns pg_class tuple for a relation.
272  *
273  * Note:
274  *              Assumes relation descriptor is valid.
275  */
276 #define RelationGetForm(relation) ((relation)->rd_rel)
277
278 /*
279  * RelationGetRelid
280  *              Returns the OID of the relation
281  */
282 #define RelationGetRelid(relation) ((relation)->rd_id)
283
284 /*
285  * RelationGetNumberOfAttributes
286  *              Returns the number of attributes in a relation.
287  */
288 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
289
290 /*
291  * RelationGetDescr
292  *              Returns tuple descriptor for a relation.
293  */
294 #define RelationGetDescr(relation) ((relation)->rd_att)
295
296 /*
297  * RelationGetRelationName
298  *              Returns the rel's name.
299  *
300  * Note that the name is only unique within the containing namespace.
301  */
302 #define RelationGetRelationName(relation) \
303         (NameStr((relation)->rd_rel->relname))
304
305 /*
306  * RelationGetNamespace
307  *              Returns the rel's namespace OID.
308  */
309 #define RelationGetNamespace(relation) \
310         ((relation)->rd_rel->relnamespace)
311
312 /*
313  * RelationOpenSmgr
314  *              Open the relation at the smgr level, if not already done.
315  */
316 #define RelationOpenSmgr(relation) \
317         do { \
318                 if ((relation)->rd_smgr == NULL) \
319                         smgrsetowner(&((relation)->rd_smgr), smgropen((relation)->rd_node)); \
320         } while (0)
321
322 /*
323  * RelationCloseSmgr
324  *              Close the relation at the smgr level, if not already done.
325  *
326  * Note: smgrclose should unhook from owner pointer, hence the Assert.
327  */
328 #define RelationCloseSmgr(relation) \
329         do { \
330                 if ((relation)->rd_smgr != NULL) \
331                 { \
332                         smgrclose((relation)->rd_smgr); \
333                         Assert((relation)->rd_smgr == NULL); \
334                 } \
335         } while (0)
336
337 /*
338  * RELATION_IS_LOCAL
339  *              If a rel is either temp or newly created in the current transaction,
340  *              it can be assumed to be visible only to the current backend.
341  *
342  * Beware of multiple eval of argument
343  */
344 #define RELATION_IS_LOCAL(relation) \
345         ((relation)->rd_istemp || \
346          (relation)->rd_createSubid != InvalidSubTransactionId)
347
348 /* routines in utils/cache/relcache.c */
349 extern void RelationIncrementReferenceCount(Relation rel);
350 extern void RelationDecrementReferenceCount(Relation rel);
351
352 #endif   /* REL_H */