]> granicus.if.org Git - postgresql/blob - src/include/utils/rel.h
Fix 3-parameter form of bit substring() to throw error for negative length,
[postgresql] / 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-2010, 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.118 2010/01/02 16:58:10 momjian 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 "nodes/bitmapset.h"
23 #include "rewrite/prs2lock.h"
24 #include "storage/block.h"
25 #include "storage/relfilenode.h"
26 #include "utils/relcache.h"
27
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;                  /* OID of trigger (pg_trigger row) */
54         /* Remaining fields are copied from pg_trigger, see pg_trigger.h */
55         char       *tgname;
56         Oid                     tgfoid;
57         int16           tgtype;
58         char            tgenabled;
59         bool            tgisconstraint;
60         Oid                     tgconstrrelid;
61         Oid                     tgconstrindid;
62         Oid                     tgconstraint;
63         bool            tgdeferrable;
64         bool            tginitdeferred;
65         int16           tgnargs;
66         int16           tgnattr;
67         int16      *tgattr;
68         char      **tgargs;
69         char       *tgqual;
70 } Trigger;
71
72 typedef struct TriggerDesc
73 {
74         /*
75          * Index data to identify which triggers are which.  Since each trigger
76          * can appear in more than one class, for each class we provide a list of
77          * integer indexes into the triggers array.  The class codes are defined
78          * by TRIGGER_EVENT_xxx macros in commands/trigger.h.
79          */
80 #define TRIGGER_NUM_EVENT_CLASSES  4
81
82         uint16          n_before_statement[TRIGGER_NUM_EVENT_CLASSES];
83         uint16          n_before_row[TRIGGER_NUM_EVENT_CLASSES];
84         uint16          n_after_row[TRIGGER_NUM_EVENT_CLASSES];
85         uint16          n_after_statement[TRIGGER_NUM_EVENT_CLASSES];
86         int                *tg_before_statement[TRIGGER_NUM_EVENT_CLASSES];
87         int                *tg_before_row[TRIGGER_NUM_EVENT_CLASSES];
88         int                *tg_after_row[TRIGGER_NUM_EVENT_CLASSES];
89         int                *tg_after_statement[TRIGGER_NUM_EVENT_CLASSES];
90
91         /* The actual array of triggers is here */
92         Trigger    *triggers;
93         int                     numtriggers;
94 } TriggerDesc;
95
96
97 /*
98  * Cached lookup information for the index access method functions defined
99  * by the pg_am row associated with an index relation.
100  */
101 typedef struct RelationAmInfo
102 {
103         FmgrInfo        aminsert;
104         FmgrInfo        ambeginscan;
105         FmgrInfo        amgettuple;
106         FmgrInfo        amgetbitmap;
107         FmgrInfo        amrescan;
108         FmgrInfo        amendscan;
109         FmgrInfo        ammarkpos;
110         FmgrInfo        amrestrpos;
111         FmgrInfo        ambuild;
112         FmgrInfo        ambulkdelete;
113         FmgrInfo        amvacuumcleanup;
114         FmgrInfo        amcostestimate;
115         FmgrInfo        amoptions;
116 } RelationAmInfo;
117
118
119 /*
120  * Here are the contents of a relation cache entry.
121  */
122
123 typedef struct RelationData
124 {
125         RelFileNode rd_node;            /* relation physical identifier */
126         /* use "struct" here to avoid needing to include smgr.h: */
127         struct SMgrRelationData *rd_smgr;       /* cached file handle, or NULL */
128         BlockNumber rd_targblock;       /* current insertion target block, or
129                                                                  * InvalidBlockNumber */
130         int                     rd_refcnt;              /* reference count */
131         bool            rd_istemp;              /* rel is a temporary relation */
132         bool            rd_islocaltemp; /* rel is a temp rel of this session */
133         bool            rd_isnailed;    /* rel is nailed in cache */
134         bool            rd_isvalid;             /* relcache entry is valid */
135         char            rd_indexvalid;  /* state of rd_indexlist: 0 = not valid, 1 =
136                                                                  * valid, 2 = temporarily forced */
137         SubTransactionId rd_createSubid;        /* rel was created in current xact */
138         SubTransactionId rd_newRelfilenodeSubid;        /* new relfilenode assigned in
139                                                                                                  * 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          * Likewise, rd_newRelfilenodeSubid is the ID of the highest
147          * subtransaction the relfilenode change has survived into, or zero if not
148          * changed in the current transaction (or we have forgotten changing it).
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         Bitmapset  *rd_indexattr;       /* identifies columns used in indexes */
155         Oid                     rd_oidindex;    /* OID of unique index on OID, if any */
156         LockInfoData rd_lockInfo;       /* lock mgr's info for locking relation */
157         RuleLock   *rd_rules;           /* rewrite rules */
158         MemoryContext rd_rulescxt;      /* private memory cxt for rd_rules, if any */
159         TriggerDesc *trigdesc;          /* Trigger info, or NULL if rel has none */
160
161         /*
162          * rd_options is set whenever rd_rel is loaded into the relcache entry.
163          * Note that you can NOT look into rd_rel for this data.  NULL means "use
164          * defaults".
165          */
166         bytea      *rd_options;         /* parsed pg_class.reloptions */
167
168         /* These are non-NULL only for an index relation: */
169         Form_pg_index rd_index;         /* pg_index tuple describing this index */
170         struct HeapTupleData *rd_indextuple;            /* all of pg_index tuple */
171         /* "struct HeapTupleData *" avoids need to include htup.h here  */
172         Form_pg_am      rd_am;                  /* pg_am tuple for index's AM */
173
174         /*
175          * index access support info (used only for an index relation)
176          *
177          * Note: only default operators and support procs for each opclass are
178          * cached, namely those with lefttype and righttype equal to the opclass's
179          * opcintype.  The arrays are indexed by strategy or support number, which
180          * is a sufficient identifier given that restriction.
181          *
182          * Note: rd_amcache is available for index AMs to cache private data about
183          * an index.  This must be just a cache since it may get reset at any time
184          * (in particular, it will get reset by a relcache inval message for the
185          * index).      If used, it must point to a single memory chunk palloc'd in
186          * rd_indexcxt.  A relcache reset will include freeing that chunk and
187          * setting rd_amcache = NULL.
188          */
189         MemoryContext rd_indexcxt;      /* private memory cxt for this stuff */
190         RelationAmInfo *rd_aminfo;      /* lookup info for funcs found in pg_am */
191         Oid                *rd_opfamily;        /* OIDs of op families for each index col */
192         Oid                *rd_opcintype;       /* OIDs of opclass declared input data types */
193         Oid                *rd_operator;        /* OIDs of index operators */
194         RegProcedure *rd_support;       /* OIDs of support procedures */
195         FmgrInfo   *rd_supportinfo; /* lookup info for support procedures */
196         int16      *rd_indoption;       /* per-column AM-specific flags */
197         List       *rd_indexprs;        /* index expression trees, if any */
198         List       *rd_indpred;         /* index predicate tree, if any */
199         Oid                *rd_exclops;         /* OIDs of exclusion operators, if any */
200         Oid                *rd_exclprocs;       /* OIDs of exclusion ops' procs, if any */
201         uint16     *rd_exclstrats;      /* exclusion ops' strategy numbers, if any */
202         void       *rd_amcache;         /* available for use by index AM */
203
204         /*
205          * sizes of the free space and visibility map forks, or InvalidBlockNumber
206          * if not known yet
207          */
208         BlockNumber rd_fsm_nblocks;
209         BlockNumber rd_vm_nblocks;
210
211         /* use "struct" here to avoid needing to include pgstat.h: */
212         struct PgStat_TableStatus *pgstat_info;         /* statistics collection area */
213 } RelationData;
214
215 /*
216  * StdRdOptions
217  *              Standard contents of rd_options for heaps and generic indexes.
218  *
219  * RelationGetFillFactor() and RelationGetTargetPageFreeSpace() can only
220  * be applied to relations that use this format or a superset for
221  * private options data.
222  */
223  /* autovacuum-related reloptions. */
224 typedef struct AutoVacOpts
225 {
226         bool            enabled;
227         int                     vacuum_threshold;
228         int                     analyze_threshold;
229         int                     vacuum_cost_delay;
230         int                     vacuum_cost_limit;
231         int                     freeze_min_age;
232         int                     freeze_max_age;
233         int                     freeze_table_age;
234         float8          vacuum_scale_factor;
235         float8          analyze_scale_factor;
236 } AutoVacOpts;
237
238 typedef struct StdRdOptions
239 {
240         int32           vl_len_;                /* varlena header (do not touch directly!) */
241         int                     fillfactor;             /* page fill factor in percent (0..100) */
242         AutoVacOpts autovacuum;         /* autovacuum-related options */
243 } StdRdOptions;
244
245 #define HEAP_MIN_FILLFACTOR                     10
246 #define HEAP_DEFAULT_FILLFACTOR         100
247
248 /*
249  * RelationGetFillFactor
250  *              Returns the relation's fillfactor.  Note multiple eval of argument!
251  */
252 #define RelationGetFillFactor(relation, defaultff) \
253         ((relation)->rd_options ? \
254          ((StdRdOptions *) (relation)->rd_options)->fillfactor : (defaultff))
255
256 /*
257  * RelationGetTargetPageUsage
258  *              Returns the relation's desired space usage per page in bytes.
259  */
260 #define RelationGetTargetPageUsage(relation, defaultff) \
261         (BLCKSZ * RelationGetFillFactor(relation, defaultff) / 100)
262
263 /*
264  * RelationGetTargetPageFreeSpace
265  *              Returns the relation's desired freespace per page in bytes.
266  */
267 #define RelationGetTargetPageFreeSpace(relation, defaultff) \
268         (BLCKSZ * (100 - RelationGetFillFactor(relation, defaultff)) / 100)
269
270 /*
271  * RelationIsValid
272  *              True iff relation descriptor is valid.
273  */
274 #define RelationIsValid(relation) PointerIsValid(relation)
275
276 #define InvalidRelation ((Relation) NULL)
277
278 /*
279  * RelationHasReferenceCountZero
280  *              True iff relation reference count is zero.
281  *
282  * Note:
283  *              Assumes relation descriptor is valid.
284  */
285 #define RelationHasReferenceCountZero(relation) \
286                 ((bool)((relation)->rd_refcnt == 0))
287
288 /*
289  * RelationGetForm
290  *              Returns pg_class tuple for a relation.
291  *
292  * Note:
293  *              Assumes relation descriptor is valid.
294  */
295 #define RelationGetForm(relation) ((relation)->rd_rel)
296
297 /*
298  * RelationGetRelid
299  *              Returns the OID of the relation
300  */
301 #define RelationGetRelid(relation) ((relation)->rd_id)
302
303 /*
304  * RelationGetNumberOfAttributes
305  *              Returns the number of attributes in a relation.
306  */
307 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
308
309 /*
310  * RelationGetDescr
311  *              Returns tuple descriptor for a relation.
312  */
313 #define RelationGetDescr(relation) ((relation)->rd_att)
314
315 /*
316  * RelationGetRelationName
317  *              Returns the rel's name.
318  *
319  * Note that the name is only unique within the containing namespace.
320  */
321 #define RelationGetRelationName(relation) \
322         (NameStr((relation)->rd_rel->relname))
323
324 /*
325  * RelationGetNamespace
326  *              Returns the rel's namespace OID.
327  */
328 #define RelationGetNamespace(relation) \
329         ((relation)->rd_rel->relnamespace)
330
331 /*
332  * RelationOpenSmgr
333  *              Open the relation at the smgr level, if not already done.
334  */
335 #define RelationOpenSmgr(relation) \
336         do { \
337                 if ((relation)->rd_smgr == NULL) \
338                         smgrsetowner(&((relation)->rd_smgr), smgropen((relation)->rd_node)); \
339         } while (0)
340
341 /*
342  * RelationCloseSmgr
343  *              Close the relation at the smgr level, if not already done.
344  *
345  * Note: smgrclose should unhook from owner pointer, hence the Assert.
346  */
347 #define RelationCloseSmgr(relation) \
348         do { \
349                 if ((relation)->rd_smgr != NULL) \
350                 { \
351                         smgrclose((relation)->rd_smgr); \
352                         Assert((relation)->rd_smgr == NULL); \
353                 } \
354         } while (0)
355
356 /*
357  * RELATION_IS_LOCAL
358  *              If a rel is either temp or newly created in the current transaction,
359  *              it can be assumed to be visible only to the current backend.
360  *
361  * Beware of multiple eval of argument
362  */
363 #define RELATION_IS_LOCAL(relation) \
364         ((relation)->rd_islocaltemp || \
365          (relation)->rd_createSubid != InvalidSubTransactionId)
366
367 /*
368  * RELATION_IS_OTHER_TEMP
369  *              Test for a temporary relation that belongs to some other session.
370  *
371  * Beware of multiple eval of argument
372  */
373 #define RELATION_IS_OTHER_TEMP(relation) \
374         ((relation)->rd_istemp && !(relation)->rd_islocaltemp)
375
376 /* routines in utils/cache/relcache.c */
377 extern void RelationIncrementReferenceCount(Relation rel);
378 extern void RelationDecrementReferenceCount(Relation rel);
379
380 #endif   /* REL_H */