OSDN Git Service

Change Copyright from PostgreSQL, Inc to PostgreSQL Global Development Group.
[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.44 2001/01/24 19:43:29 momjian 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 "rewrite/prs2lock.h"
22 #include "storage/relfilenode.h"
23 #include "storage/fd.h"
24
25 /* added to prevent circular dependency.  bjm 1999/11/15 */
26 extern char *get_temp_rel_by_physicalname(const char *relname);
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
51 typedef struct Trigger
52 {
53         Oid                     tgoid;
54         char       *tgname;
55         Oid                     tgfoid;
56         FmgrInfo        tgfunc;
57         int16           tgtype;
58         bool            tgenabled;
59         bool            tgisconstraint;
60         bool            tgdeferrable;
61         bool            tginitdeferred;
62         int16           tgnargs;
63         int16           tgattr[FUNC_MAX_ARGS];
64         char      **tgargs;
65 } Trigger;
66
67 typedef struct TriggerDesc
68 {
69         /* index data to identify which triggers are which */
70         uint16          n_before_statement[4];
71         uint16          n_before_row[4];
72         uint16          n_after_row[4];
73         uint16          n_after_statement[4];
74         Trigger   **tg_before_statement[4];
75         Trigger   **tg_before_row[4];
76         Trigger   **tg_after_row[4];
77         Trigger   **tg_after_statement[4];
78         /* the actual array of triggers is here */
79         Trigger    *triggers;
80         int                     numtriggers;
81 } TriggerDesc;
82
83 /*
84  * Here are the contents of a relation cache entry.
85  */
86
87 typedef struct RelationData
88 {
89         File            rd_fd;                  /* open file descriptor, or -1 if none */
90         RelFileNode     rd_node;                /* relation file node */
91         int                     rd_nblocks;             /* number of blocks in rel */
92         uint16          rd_refcnt;              /* reference count */
93         bool            rd_myxactonly;  /* rel uses the local buffer mgr */
94         bool            rd_isnailed;    /* rel is nailed in cache */
95         bool            rd_indexfound;  /* true if rd_indexlist is valid */
96         bool            rd_uniqueindex; /* true if rel is a UNIQUE index */
97         Form_pg_am      rd_am;                  /* AM tuple */
98         Form_pg_class rd_rel;           /* RELATION tuple */
99         Oid                     rd_id;                  /* relation's object id */
100         List       *rd_indexlist;       /* list of OIDs of indexes on relation */
101         LockInfoData rd_lockInfo;       /* lock mgr's info for locking relation */
102         TupleDesc       rd_att;                 /* tuple descriptor */
103         RuleLock   *rd_rules;           /* rewrite rules */
104         MemoryContext rd_rulescxt;      /* private memory cxt for rd_rules, if any */
105         IndexStrategy rd_istrat;        /* info needed if rel is an index */
106         RegProcedure *rd_support;
107         TriggerDesc *trigdesc;          /* Trigger info, or NULL if rel has none */
108 } RelationData;
109
110 typedef RelationData *Relation;
111
112
113 /* ----------------
114  *              RelationPtr is used in the executor to support index scans
115  *              where we have to keep track of several index relations in an
116  *              array.  -cim 9/10/89
117  * ----------------
118  */
119 typedef Relation *RelationPtr;
120
121
122 /*
123  * RelationIsValid
124  *              True iff relation descriptor is valid.
125  */
126 #define RelationIsValid(relation) PointerIsValid(relation)
127
128 #define InvalidRelation ((Relation) NULL)
129
130 /*
131  * RelationHasReferenceCountZero
132  *              True iff relation reference count is zero.
133  *
134  * Note:
135  *              Assumes relation descriptor is valid.
136  */
137 #define RelationHasReferenceCountZero(relation) \
138                 ((bool)((relation)->rd_refcnt == 0))
139
140 /*
141  * RelationSetReferenceCount
142  *              Sets relation reference count.
143  */
144 #define RelationSetReferenceCount(relation,count) \
145         ((relation)->rd_refcnt = (count))
146
147 /*
148  * RelationIncrementReferenceCount
149  *              Increments relation reference count.
150  */
151 #define RelationIncrementReferenceCount(relation) \
152         ((relation)->rd_refcnt += 1)
153
154 /*
155  * RelationDecrementReferenceCount
156  *              Decrements relation reference count.
157  */
158 #define RelationDecrementReferenceCount(relation) \
159         (AssertMacro((relation)->rd_refcnt > 0), \
160          (relation)->rd_refcnt -= 1)
161
162 /*
163  * RelationGetForm
164  *              Returns pg_class tuple for a relation.
165  *
166  * Note:
167  *              Assumes relation descriptor is valid.
168  */
169 #define RelationGetForm(relation) ((relation)->rd_rel)
170
171 /*
172  * RelationGetRelid
173  *
174  *      returns the OID of the relation
175  */
176 #define RelationGetRelid(relation) ((relation)->rd_id)
177
178 /*
179  * RelationGetFile
180  *
181  *        Returns the open file descriptor for the rel
182  */
183 #define RelationGetFile(relation) ((relation)->rd_fd)
184
185 /*
186  * RelationGetRelationName
187  *
188  *        Returns the relation's logical name (as seen by the user).
189  *
190  * If the rel is a temp rel, the temp name will be returned.  Therefore,
191  * this name is not unique.  But it is the name to use in heap_openr(),
192  * for example.
193  */
194 #define RelationGetRelationName(relation) \
195 (\
196         (strncmp(RelationGetPhysicalRelationName(relation), \
197                          "pg_temp.", 8) != 0) \
198         ? \
199                 RelationGetPhysicalRelationName(relation) \
200         : \
201                 get_temp_rel_by_physicalname( \
202                         RelationGetPhysicalRelationName(relation)) \
203 )
204
205
206 /*
207  * RelationGetPhysicalRelationName
208  *
209  *        Returns the rel's physical name, ie, the name appearing in pg_class.
210  *
211  * While this name is unique across all rels in the database, it is not
212  * necessarily useful for accessing the rel, since a temp table of the
213  * same name might mask the rel.  It is useful mainly for determining if
214  * the rel is a shared system rel or not.
215  *
216  * The macro is rather unfortunately named, since the pg_class name no longer
217  * has anything to do with the file name used for physical storage of the rel.
218  */
219 #define RelationGetPhysicalRelationName(relation) \
220         (NameStr((relation)->rd_rel->relname))
221
222 /*
223  * RelationGetNumberOfAttributes
224  *
225  *        Returns the number of attributes.
226  */
227 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
228
229 /*
230  * RelationGetDescr
231  *              Returns tuple descriptor for a relation.
232  */
233 #define RelationGetDescr(relation) ((relation)->rd_att)
234
235 /*
236  * RelationGetIndexStrategy
237  *              Returns index strategy for a relation.
238  *
239  * Note:
240  *              Assumes relation descriptor is valid.
241  *              Assumes relation descriptor is for an index relation.
242  */
243 #define RelationGetIndexStrategy(relation) ((relation)->rd_istrat)
244
245 /*
246  * Routines in utils/cache/rel.c
247  */
248 extern void RelationSetIndexSupport(Relation relation,
249                                                                         IndexStrategy strategy,
250                                                                         RegProcedure *support);
251
252 #endif   /* REL_H */