OSDN Git Service

Install infrastructure for shared-memory free space map. Doesn't actually
[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.51 2001/06/27 23:31:40 tgl 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/block.h"
23 #include "storage/fd.h"
24 #include "storage/relfilenode.h"
25
26 /* added to prevent circular dependency.  bjm 1999/11/15 */
27 extern char *get_temp_rel_by_physicalname(const char *relname);
28
29 /*
30  * LockRelId and LockInfo really belong to lmgr.h, but it's more convenient
31  * to declare them here so we can have a LockInfoData field in a Relation.
32  */
33
34 typedef struct LockRelId
35 {
36         Oid                     relId;                  /* a relation identifier */
37         Oid                     dbId;                   /* a database identifier */
38 } LockRelId;
39
40 typedef struct LockInfoData
41 {
42         LockRelId       lockRelId;
43 } LockInfoData;
44
45 typedef LockInfoData *LockInfo;
46
47 /*
48  * Likewise, this struct really belongs to trigger.h, but for convenience
49  * we put it here.
50  */
51 typedef struct Trigger
52 {
53         Oid                     tgoid;
54         char       *tgname;
55         Oid                     tgfoid;
56         int16           tgtype;
57         bool            tgenabled;
58         bool            tgisconstraint;
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 trigger
70          * can appear in more than one class, for each class we provide a list
71          * of integer indexes into the triggers array.
72          */
73 #define TRIGGER_NUM_EVENT_CLASSES  4
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  */
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  * Here are the contents of a relation cache entry.
104  */
105
106 typedef struct RelationData
107 {
108         File            rd_fd;                  /* open file descriptor, or -1 if none */
109         RelFileNode rd_node;            /* file node (physical identifier) */
110         BlockNumber     rd_nblocks;             /* number of blocks in rel */
111         BlockNumber     rd_targblock;   /* current insertion target block,
112                                                                  * or InvalidBlockNumber */
113         int                     rd_refcnt;              /* reference count */
114         bool            rd_myxactonly;  /* rel uses the local buffer mgr */
115         bool            rd_isnailed;    /* rel is nailed in cache */
116         bool            rd_indexfound;  /* true if rd_indexlist is valid */
117         bool            rd_uniqueindex; /* true if rel is a UNIQUE index */
118         Form_pg_am      rd_am;                  /* AM tuple */
119         Form_pg_class rd_rel;           /* RELATION tuple */
120         Oid                     rd_id;                  /* relation's object id */
121         List       *rd_indexlist;       /* list of OIDs of indexes on relation */
122         LockInfoData rd_lockInfo;       /* lock mgr's info for locking relation */
123         TupleDesc       rd_att;                 /* tuple descriptor */
124         RuleLock   *rd_rules;           /* rewrite rules */
125         MemoryContext rd_rulescxt;      /* private memory cxt for rd_rules, if any */
126         IndexStrategy rd_istrat;        /* info needed if rel is an index */
127         RegProcedure *rd_support;
128         TriggerDesc *trigdesc;          /* Trigger info, or NULL if rel has none */
129
130         PgStat_Info     pgstat_info;
131 } RelationData;
132
133 typedef RelationData *Relation;
134
135
136 /* ----------------
137  *              RelationPtr is used in the executor to support index scans
138  *              where we have to keep track of several index relations in an
139  *              array.  -cim 9/10/89
140  * ----------------
141  */
142 typedef Relation *RelationPtr;
143
144
145 /*
146  * RelationIsValid
147  *              True iff relation descriptor is valid.
148  */
149 #define RelationIsValid(relation) PointerIsValid(relation)
150
151 #define InvalidRelation ((Relation) NULL)
152
153 /*
154  * RelationHasReferenceCountZero
155  *              True iff relation reference count is zero.
156  *
157  * Note:
158  *              Assumes relation descriptor is valid.
159  */
160 #define RelationHasReferenceCountZero(relation) \
161                 ((bool)((relation)->rd_refcnt == 0))
162
163 /*
164  * RelationSetReferenceCount
165  *              Sets relation reference count.
166  */
167 #define RelationSetReferenceCount(relation,count) \
168         ((relation)->rd_refcnt = (count))
169
170 /*
171  * RelationIncrementReferenceCount
172  *              Increments relation reference count.
173  */
174 #define RelationIncrementReferenceCount(relation) \
175         ((relation)->rd_refcnt += 1)
176
177 /*
178  * RelationDecrementReferenceCount
179  *              Decrements relation reference count.
180  */
181 #define RelationDecrementReferenceCount(relation) \
182         (AssertMacro((relation)->rd_refcnt > 0), \
183          (relation)->rd_refcnt -= 1)
184
185 /*
186  * RelationGetForm
187  *              Returns pg_class tuple for a relation.
188  *
189  * Note:
190  *              Assumes relation descriptor is valid.
191  */
192 #define RelationGetForm(relation) ((relation)->rd_rel)
193
194 /*
195  * RelationGetRelid
196  *
197  *      returns the OID of the relation
198  */
199 #define RelationGetRelid(relation) ((relation)->rd_id)
200
201 /*
202  * RelationGetFile
203  *
204  *        Returns the open file descriptor for the rel
205  */
206 #define RelationGetFile(relation) ((relation)->rd_fd)
207
208 /*
209  * RelationGetNumberOfAttributes
210  *
211  *        Returns the number of attributes.
212  */
213 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
214
215 /*
216  * RelationGetDescr
217  *              Returns tuple descriptor for a relation.
218  */
219 #define RelationGetDescr(relation) ((relation)->rd_att)
220
221 /*
222  * RelationGetIndexStrategy
223  *              Returns index strategy for a relation.
224  *
225  * Note:
226  *              Assumes relation descriptor is valid.
227  *              Assumes relation descriptor is for an index relation.
228  */
229 #define RelationGetIndexStrategy(relation) ((relation)->rd_istrat)
230
231 /*
232  * Routines in utils/cache/rel.c
233  */
234 extern void RelationSetIndexSupport(Relation relation,
235                                                 IndexStrategy strategy,
236                                                 RegProcedure *support);
237
238 /*
239  * Handle temp relations
240  */
241 #define PG_TEMP_REL_PREFIX "pg_temp"
242 #define PG_TEMP_REL_PREFIX_LEN 7
243
244 #define is_temp_relname(relname) \
245                 (strncmp(relname, PG_TEMP_REL_PREFIX, PG_TEMP_REL_PREFIX_LEN) == 0)
246
247 /*
248  * RelationGetPhysicalRelationName
249  *
250  *        Returns the rel's physical name, ie, the name appearing in pg_class.
251  *
252  * While this name is unique across all rels in the database, it is not
253  * necessarily useful for accessing the rel, since a temp table of the
254  * same name might mask the rel.  It is useful mainly for determining if
255  * the rel is a shared system rel or not.
256  *
257  * The macro is rather unfortunately named, since the pg_class name no longer
258  * has anything to do with the file name used for physical storage of the rel.
259  */
260 #define RelationGetPhysicalRelationName(relation) \
261         (NameStr((relation)->rd_rel->relname))
262
263 /*
264  * RelationGetRelationName
265  *
266  *        Returns the relation's logical name (as seen by the user).
267  *
268  * If the rel is a temp rel, the temp name will be returned.  Therefore,
269  * this name is not unique.  But it is the name to use in heap_openr(),
270  * for example.
271  */
272 #define RelationGetRelationName(relation) \
273 (\
274         is_temp_relname(RelationGetPhysicalRelationName(relation)) \
275         ? \
276                 get_temp_rel_by_physicalname( \
277                         RelationGetPhysicalRelationName(relation)) \
278         : \
279                 RelationGetPhysicalRelationName(relation) \
280 )
281
282
283 #endif   /* REL_H */