OSDN Git Service

Solve the problem of OID collisions by probing for duplicate OIDs
[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.85 2005/08/12 01:36:05 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
71          * trigger can appear in more than one class, for each class we
72          * provide a list of 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         bool            no_stats;
98         bool            heap_scan_counted;
99         bool            index_scan_counted;
100 } PgStat_Info;
101
102
103 /*
104  * Cached lookup information for the index access method functions defined
105  * by the pg_am row associated with an index relation.
106  */
107 typedef struct RelationAmInfo
108 {
109         FmgrInfo        aminsert;
110         FmgrInfo        ambeginscan;
111         FmgrInfo        amgettuple;
112         FmgrInfo        amgetmulti;
113         FmgrInfo        amrescan;
114         FmgrInfo        amendscan;
115         FmgrInfo        ammarkpos;
116         FmgrInfo        amrestrpos;
117         FmgrInfo        ambuild;
118         FmgrInfo        ambulkdelete;
119         FmgrInfo        amvacuumcleanup;
120         FmgrInfo        amcostestimate;
121 } RelationAmInfo;
122
123
124 /*
125  * Here are the contents of a relation cache entry.
126  */
127
128 typedef struct RelationData
129 {
130         RelFileNode rd_node;            /* relation physical identifier */
131         /* use "struct" here to avoid needing to include smgr.h: */
132         struct SMgrRelationData *rd_smgr;       /* cached file handle, or NULL */
133         BlockNumber rd_targblock;       /* current insertion target block, or
134                                                                  * InvalidBlockNumber */
135         int                     rd_refcnt;              /* reference count */
136         bool            rd_istemp;              /* rel uses the local buffer mgr */
137         bool            rd_isnailed;    /* rel is nailed in cache */
138         bool            rd_isvalid;             /* relcache entry is valid */
139         char            rd_indexvalid;  /* state of rd_indexlist: 0 = not valid,
140                                                                  * 1 = valid, 2 = temporarily forced */
141         SubTransactionId rd_createSubid;        /* rel was created in current xact */
142
143         /*
144          * rd_createSubid is the ID of the highest subtransaction the rel has
145          * survived into; or zero if the rel was not created in the current
146          * top transaction.  This should be relied on only for optimization
147          * purposes; it is possible for new-ness to be "forgotten" (eg, after
148          * CLUSTER).
149          */
150         Form_pg_class rd_rel;           /* RELATION tuple */
151         TupleDesc       rd_att;                 /* tuple descriptor */
152         Oid                     rd_id;                  /* relation's object id */
153         List       *rd_indexlist;       /* list of OIDs of indexes on relation */
154         Oid                     rd_oidindex;    /* OID of unique index on OID, if any */
155         LockInfoData rd_lockInfo;       /* lock mgr's info for locking relation */
156         RuleLock   *rd_rules;           /* rewrite rules */
157         MemoryContext rd_rulescxt;      /* private memory cxt for rd_rules, if any */
158         TriggerDesc *trigdesc;          /* Trigger info, or NULL if rel has none */
159
160         /* These are non-NULL only for an index relation: */
161         Form_pg_index rd_index;         /* pg_index tuple describing this index */
162         struct HeapTupleData *rd_indextuple;            /* all of pg_index tuple */
163         /* "struct HeapTupleData *" avoids need to include htup.h here  */
164         oidvector  *rd_indclass;        /* extracted pointer to rd_index field */
165         Form_pg_am      rd_am;                  /* pg_am tuple for index's AM */
166
167         /*
168          * index access support info (used only for an index relation)
169          *
170          * Note: only default operators and support procs for each opclass are
171          * cached, namely those with subtype zero.      The arrays are indexed by
172          * strategy or support number, which is a sufficient identifier given
173          * that restriction.
174          */
175         MemoryContext rd_indexcxt;      /* private memory cxt for this stuff */
176         RelationAmInfo *rd_aminfo;      /* lookup info for funcs found in pg_am */
177         Oid                *rd_operator;        /* OIDs of index operators */
178         RegProcedure *rd_support;       /* OIDs of support procedures */
179         FmgrInfo   *rd_supportinfo;     /* lookup info for support procedures */
180         List       *rd_indexprs;        /* index expression trees, if any */
181         List       *rd_indpred;         /* index predicate tree, if any */
182
183         /* statistics collection area */
184         PgStat_Info pgstat_info;
185 } RelationData;
186
187 typedef RelationData *Relation;
188
189
190 /* ----------------
191  *              RelationPtr is used in the executor to support index scans
192  *              where we have to keep track of several index relations in an
193  *              array.  -cim 9/10/89
194  * ----------------
195  */
196 typedef Relation *RelationPtr;
197
198
199 /*
200  * RelationIsValid
201  *              True iff relation descriptor is valid.
202  */
203 #define RelationIsValid(relation) PointerIsValid(relation)
204
205 #define InvalidRelation ((Relation) NULL)
206
207 /*
208  * RelationHasReferenceCountZero
209  *              True iff relation reference count is zero.
210  *
211  * Note:
212  *              Assumes relation descriptor is valid.
213  */
214 #define RelationHasReferenceCountZero(relation) \
215                 ((bool)((relation)->rd_refcnt == 0))
216
217 /*
218  * RelationGetForm
219  *              Returns pg_class tuple for a relation.
220  *
221  * Note:
222  *              Assumes relation descriptor is valid.
223  */
224 #define RelationGetForm(relation) ((relation)->rd_rel)
225
226 /*
227  * RelationGetRelid
228  *              Returns the OID of the relation
229  */
230 #define RelationGetRelid(relation) ((relation)->rd_id)
231
232 /*
233  * RelationGetNumberOfAttributes
234  *              Returns the number of attributes in a relation.
235  */
236 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
237
238 /*
239  * RelationGetDescr
240  *              Returns tuple descriptor for a relation.
241  */
242 #define RelationGetDescr(relation) ((relation)->rd_att)
243
244 /*
245  * RelationGetRelationName
246  *              Returns the rel's name.
247  *
248  * Note that the name is only unique within the containing namespace.
249  */
250 #define RelationGetRelationName(relation) \
251         (NameStr((relation)->rd_rel->relname))
252
253 /*
254  * RelationGetNamespace
255  *              Returns the rel's namespace OID.
256  */
257 #define RelationGetNamespace(relation) \
258         ((relation)->rd_rel->relnamespace)
259
260 /*
261  * RelationOpenSmgr
262  *              Open the relation at the smgr level, if not already done.
263  */
264 #define RelationOpenSmgr(relation) \
265         do { \
266                 if ((relation)->rd_smgr == NULL) \
267                         smgrsetowner(&((relation)->rd_smgr), smgropen((relation)->rd_node)); \
268         } while (0)
269
270 /*
271  * RelationCloseSmgr
272  *              Close the relation at the smgr level, if not already done.
273  *
274  * Note: smgrclose should unhook from owner pointer, hence the Assert.
275  */
276 #define RelationCloseSmgr(relation) \
277         do { \
278                 if ((relation)->rd_smgr != NULL) \
279                 { \
280                         smgrclose((relation)->rd_smgr); \
281                         Assert((relation)->rd_smgr == NULL); \
282                 } \
283         } while (0)
284
285 /*
286  * RELATION_IS_LOCAL
287  *              If a rel is either temp or newly created in the current transaction,
288  *              it can be assumed to be visible only to the current backend.
289  *
290  * Beware of multiple eval of argument
291  */
292 #define RELATION_IS_LOCAL(relation) \
293         ((relation)->rd_istemp || \
294          (relation)->rd_createSubid != InvalidSubTransactionId)
295
296 /* routines in utils/cache/relcache.c */
297 extern void RelationIncrementReferenceCount(Relation rel);
298 extern void RelationDecrementReferenceCount(Relation rel);
299
300 #endif   /* REL_H */