OSDN Git Service

Cross-data-type comparisons are now indexable by btrees, pursuant to my
[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-2003, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $Id: rel.h,v 1.70 2003/11/12 21:15:59 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/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;                  /* 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           tgattr[FUNC_MAX_ARGS];
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  * 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_isnew;               /* rel was created in current xact */
114
115         /*
116          * NOTE: rd_isnew should be relied on only for optimization purposes;
117          * it is possible for new-ness to be "forgotten" (eg, after CLUSTER).
118          */
119         bool            rd_istemp;              /* rel uses the local buffer mgr */
120         char            rd_isnailed;    /* rel is nailed in cache: 0 = no, 1 = yes,
121                                                                  * 2 = yes but possibly invalid */
122         char            rd_indexvalid;  /* state of rd_indexlist: 0 = not valid,
123                                                                  * 1 = valid, 2 = temporarily forced */
124         Form_pg_class rd_rel;           /* RELATION tuple */
125         TupleDesc       rd_att;                 /* tuple descriptor */
126         Oid                     rd_id;                  /* relation's object id */
127         List       *rd_indexlist;       /* list of OIDs of indexes on relation */
128         LockInfoData rd_lockInfo;       /* lock mgr's info for locking relation */
129         RuleLock   *rd_rules;           /* rewrite rules */
130         MemoryContext rd_rulescxt;      /* private memory cxt for rd_rules, if any */
131         TriggerDesc *trigdesc;          /* Trigger info, or NULL if rel has none */
132
133         /* These are non-NULL only for an index relation: */
134         Form_pg_index rd_index;         /* pg_index tuple describing this index */
135         struct HeapTupleData *rd_indextuple;            /* all of pg_index tuple */
136         /* "struct HeapTupleData *" avoids need to include htup.h here  */
137         Form_pg_am      rd_am;                  /* pg_am tuple for index's AM */
138
139         /*
140          * index access support info (used only for an index relation)
141          *
142          * Note: only default operators and support procs for each opclass are
143          * cached, namely those with subtype zero.  The arrays are indexed by
144          * strategy or support number, which is a sufficient identifier given
145          * that restriction.
146          */
147         MemoryContext rd_indexcxt;      /* private memory cxt for this stuff */
148         Oid                *rd_operator;        /* OIDs of index operators */
149         RegProcedure *rd_support;       /* OIDs of support procedures */
150         struct FmgrInfo *rd_supportinfo;        /* lookup info for support
151                                                                                  * procedures */
152         /* "struct FmgrInfo" avoids need to include fmgr.h here */
153         List       *rd_indexprs;        /* index expression trees, if any */
154         List       *rd_indpred;         /* index predicate tree, if any */
155
156         /* statistics collection area */
157         PgStat_Info pgstat_info;
158 } RelationData;
159
160 typedef RelationData *Relation;
161
162
163 /* ----------------
164  *              RelationPtr is used in the executor to support index scans
165  *              where we have to keep track of several index relations in an
166  *              array.  -cim 9/10/89
167  * ----------------
168  */
169 typedef Relation *RelationPtr;
170
171
172 /*
173  * RelationIsValid
174  *              True iff relation descriptor is valid.
175  */
176 #define RelationIsValid(relation) PointerIsValid(relation)
177
178 #define InvalidRelation ((Relation) NULL)
179
180 /*
181  * RelationHasReferenceCountZero
182  *              True iff relation reference count is zero.
183  *
184  * Note:
185  *              Assumes relation descriptor is valid.
186  */
187 #define RelationHasReferenceCountZero(relation) \
188                 ((bool)((relation)->rd_refcnt == 0))
189
190 /*
191  * RelationSetReferenceCount
192  *              Sets relation reference count.
193  */
194 #define RelationSetReferenceCount(relation,count) \
195         ((relation)->rd_refcnt = (count))
196
197 /*
198  * RelationIncrementReferenceCount
199  *              Increments relation reference count.
200  */
201 #define RelationIncrementReferenceCount(relation) \
202         ((relation)->rd_refcnt += 1)
203
204 /*
205  * RelationDecrementReferenceCount
206  *              Decrements relation reference count.
207  */
208 #define RelationDecrementReferenceCount(relation) \
209         (AssertMacro((relation)->rd_refcnt > 0), \
210          (relation)->rd_refcnt -= 1)
211
212 /*
213  * RelationGetForm
214  *              Returns pg_class tuple for a relation.
215  *
216  * Note:
217  *              Assumes relation descriptor is valid.
218  */
219 #define RelationGetForm(relation) ((relation)->rd_rel)
220
221 /*
222  * RelationGetRelid
223  *
224  *      returns the OID of the relation
225  */
226 #define RelationGetRelid(relation) ((relation)->rd_id)
227
228 /*
229  * RelationGetFile
230  *
231  *        Returns the open file descriptor for the rel
232  */
233 #define RelationGetFile(relation) ((relation)->rd_fd)
234
235 /*
236  * RelationGetNumberOfAttributes
237  *
238  *        Returns the number of attributes.
239  */
240 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
241
242 /*
243  * RelationGetDescr
244  *              Returns tuple descriptor for a relation.
245  */
246 #define RelationGetDescr(relation) ((relation)->rd_att)
247
248 /*
249  * RelationGetRelationName
250  *
251  *        Returns the rel's name.
252  *
253  * Note that the name is only unique within the containing namespace.
254  */
255 #define RelationGetRelationName(relation) \
256         (NameStr((relation)->rd_rel->relname))
257
258 /*
259  * RelationGetNamespace
260  *
261  *        Returns the rel's namespace OID.
262  */
263 #define RelationGetNamespace(relation) \
264         ((relation)->rd_rel->relnamespace)
265
266 #endif   /* REL_H */