]> granicus.if.org Git - postgresql/blob - src/include/utils/rel.h
f7f84e6f71e8e4c36e66dbb3b5b8614b846a0bae
[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-2012, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/utils/rel.h
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 #include "utils/reltrigger.h"
28
29
30 /*
31  * LockRelId and LockInfo really belong to lmgr.h, but it's more convenient
32  * to declare them here so we can have a LockInfoData field in a Relation.
33  */
34
35 typedef struct LockRelId
36 {
37         Oid                     relId;                  /* a relation identifier */
38         Oid                     dbId;                   /* a database identifier */
39 } LockRelId;
40
41 typedef struct LockInfoData
42 {
43         LockRelId       lockRelId;
44 } LockInfoData;
45
46 typedef LockInfoData *LockInfo;
47
48
49 /*
50  * Cached lookup information for the index access method functions defined
51  * by the pg_am row associated with an index relation.
52  */
53 typedef struct RelationAmInfo
54 {
55         FmgrInfo        aminsert;
56         FmgrInfo        ambeginscan;
57         FmgrInfo        amgettuple;
58         FmgrInfo        amgetbitmap;
59         FmgrInfo        amrescan;
60         FmgrInfo        amendscan;
61         FmgrInfo        ammarkpos;
62         FmgrInfo        amrestrpos;
63         FmgrInfo        ambuild;
64         FmgrInfo        ambuildempty;
65         FmgrInfo        ambulkdelete;
66         FmgrInfo        amvacuumcleanup;
67         FmgrInfo        amcanreturn;
68         FmgrInfo        amcostestimate;
69         FmgrInfo        amoptions;
70 } RelationAmInfo;
71
72
73 /*
74  * Here are the contents of a relation cache entry.
75  */
76
77 typedef struct RelationData
78 {
79         RelFileNode rd_node;            /* relation physical identifier */
80         /* use "struct" here to avoid needing to include smgr.h: */
81         struct SMgrRelationData *rd_smgr;       /* cached file handle, or NULL */
82         int                     rd_refcnt;              /* reference count */
83         BackendId       rd_backend;             /* owning backend id, if temporary relation */
84         bool            rd_islocaltemp; /* rel is a temp rel of this session */
85         bool            rd_isnailed;    /* rel is nailed in cache */
86         bool            rd_isvalid;             /* relcache entry is valid */
87         char            rd_indexvalid;  /* state of rd_indexlist: 0 = not valid, 1 =
88                                                                  * valid, 2 = temporarily forced */
89
90         /*
91          * rd_createSubid is the ID of the highest subtransaction the rel has
92          * survived into; or zero if the rel was not created in the current top
93          * transaction.  This should be relied on only for optimization purposes;
94          * it is possible for new-ness to be "forgotten" (eg, after CLUSTER).
95          * Likewise, rd_newRelfilenodeSubid is the ID of the highest
96          * subtransaction the relfilenode change has survived into, or zero if not
97          * changed in the current transaction (or we have forgotten changing it).
98          */
99         SubTransactionId rd_createSubid;        /* rel was created in current xact */
100         SubTransactionId rd_newRelfilenodeSubid;        /* new relfilenode assigned in
101                                                                                                  * current xact */
102
103         Form_pg_class rd_rel;           /* RELATION tuple */
104         TupleDesc       rd_att;                 /* tuple descriptor */
105         Oid                     rd_id;                  /* relation's object id */
106         List       *rd_indexlist;       /* list of OIDs of indexes on relation */
107         Bitmapset  *rd_indexattr;       /* identifies columns used in indexes */
108         Oid                     rd_oidindex;    /* OID of unique index on OID, if any */
109         LockInfoData rd_lockInfo;       /* lock mgr's info for locking relation */
110         RuleLock   *rd_rules;           /* rewrite rules */
111         MemoryContext rd_rulescxt;      /* private memory cxt for rd_rules, if any */
112         TriggerDesc *trigdesc;          /* Trigger info, or NULL if rel has none */
113
114         /*
115          * rd_options is set whenever rd_rel is loaded into the relcache entry.
116          * Note that you can NOT look into rd_rel for this data.  NULL means "use
117          * defaults".
118          */
119         bytea      *rd_options;         /* parsed pg_class.reloptions */
120
121         /* These are non-NULL only for an index relation: */
122         Form_pg_index rd_index;         /* pg_index tuple describing this index */
123         /* use "struct" here to avoid needing to include htup.h: */
124         struct HeapTupleData *rd_indextuple;            /* all of pg_index tuple */
125         Form_pg_am      rd_am;                  /* pg_am tuple for index's AM */
126
127         /*
128          * index access support info (used only for an index relation)
129          *
130          * Note: only default support procs for each opclass are cached, namely
131          * those with lefttype and righttype equal to the opclass's opcintype. The
132          * arrays are indexed by support function number, which is a sufficient
133          * identifier given that restriction.
134          *
135          * Note: rd_amcache is available for index AMs to cache private data about
136          * an index.  This must be just a cache since it may get reset at any time
137          * (in particular, it will get reset by a relcache inval message for the
138          * index).      If used, it must point to a single memory chunk palloc'd in
139          * rd_indexcxt.  A relcache reset will include freeing that chunk and
140          * setting rd_amcache = NULL.
141          */
142         MemoryContext rd_indexcxt;      /* private memory cxt for this stuff */
143         RelationAmInfo *rd_aminfo;      /* lookup info for funcs found in pg_am */
144         Oid                *rd_opfamily;        /* OIDs of op families for each index col */
145         Oid                *rd_opcintype;       /* OIDs of opclass declared input data types */
146         RegProcedure *rd_support;       /* OIDs of support procedures */
147         FmgrInfo   *rd_supportinfo; /* lookup info for support procedures */
148         int16      *rd_indoption;       /* per-column AM-specific flags */
149         List       *rd_indexprs;        /* index expression trees, if any */
150         List       *rd_indpred;         /* index predicate tree, if any */
151         Oid                *rd_exclops;         /* OIDs of exclusion operators, if any */
152         Oid                *rd_exclprocs;       /* OIDs of exclusion ops' procs, if any */
153         uint16     *rd_exclstrats;      /* exclusion ops' strategy numbers, if any */
154         void       *rd_amcache;         /* available for use by index AM */
155         Oid                *rd_indcollation;    /* OIDs of index collations */
156
157         /*
158          * Hack for CLUSTER, rewriting ALTER TABLE, etc: when writing a new
159          * version of a table, we need to make any toast pointers inserted into it
160          * have the existing toast table's OID, not the OID of the transient toast
161          * table.  If rd_toastoid isn't InvalidOid, it is the OID to place in
162          * toast pointers inserted into this rel.  (Note it's set on the new
163          * version of the main heap, not the toast table itself.)  This also
164          * causes toast_save_datum() to try to preserve toast value OIDs.
165          */
166         Oid                     rd_toastoid;    /* Real TOAST table's OID, or InvalidOid */
167
168         /* use "struct" here to avoid needing to include pgstat.h: */
169         struct PgStat_TableStatus *pgstat_info;         /* statistics collection area */
170 } RelationData;
171
172 /*
173  * StdRdOptions
174  *              Standard contents of rd_options for heaps and generic indexes.
175  *
176  * RelationGetFillFactor() and RelationGetTargetPageFreeSpace() can only
177  * be applied to relations that use this format or a superset for
178  * private options data.
179  */
180  /* autovacuum-related reloptions. */
181 typedef struct AutoVacOpts
182 {
183         bool            enabled;
184         int                     vacuum_threshold;
185         int                     analyze_threshold;
186         int                     vacuum_cost_delay;
187         int                     vacuum_cost_limit;
188         int                     freeze_min_age;
189         int                     freeze_max_age;
190         int                     freeze_table_age;
191         float8          vacuum_scale_factor;
192         float8          analyze_scale_factor;
193 } AutoVacOpts;
194
195 typedef struct StdRdOptions
196 {
197         int32           vl_len_;                /* varlena header (do not touch directly!) */
198         int                     fillfactor;             /* page fill factor in percent (0..100) */
199         AutoVacOpts autovacuum;         /* autovacuum-related options */
200         bool            security_barrier;               /* for views */
201 } StdRdOptions;
202
203 #define HEAP_MIN_FILLFACTOR                     10
204 #define HEAP_DEFAULT_FILLFACTOR         100
205
206 /*
207  * RelationGetFillFactor
208  *              Returns the relation's fillfactor.  Note multiple eval of argument!
209  */
210 #define RelationGetFillFactor(relation, defaultff) \
211         ((relation)->rd_options ? \
212          ((StdRdOptions *) (relation)->rd_options)->fillfactor : (defaultff))
213
214 /*
215  * RelationGetTargetPageUsage
216  *              Returns the relation's desired space usage per page in bytes.
217  */
218 #define RelationGetTargetPageUsage(relation, defaultff) \
219         (BLCKSZ * RelationGetFillFactor(relation, defaultff) / 100)
220
221 /*
222  * RelationGetTargetPageFreeSpace
223  *              Returns the relation's desired freespace per page in bytes.
224  */
225 #define RelationGetTargetPageFreeSpace(relation, defaultff) \
226         (BLCKSZ * (100 - RelationGetFillFactor(relation, defaultff)) / 100)
227
228 /*
229  * RelationIsSecurityView
230  *              Returns whether the relation is security view, or not
231  */
232 #define RelationIsSecurityView(relation)        \
233         ((relation)->rd_options ?                               \
234          ((StdRdOptions *) (relation)->rd_options)->security_barrier : false)
235
236 /*
237  * RelationIsValid
238  *              True iff relation descriptor is valid.
239  */
240 #define RelationIsValid(relation) PointerIsValid(relation)
241
242 #define InvalidRelation ((Relation) NULL)
243
244 /*
245  * RelationHasReferenceCountZero
246  *              True iff relation reference count is zero.
247  *
248  * Note:
249  *              Assumes relation descriptor is valid.
250  */
251 #define RelationHasReferenceCountZero(relation) \
252                 ((bool)((relation)->rd_refcnt == 0))
253
254 /*
255  * RelationGetForm
256  *              Returns pg_class tuple for a relation.
257  *
258  * Note:
259  *              Assumes relation descriptor is valid.
260  */
261 #define RelationGetForm(relation) ((relation)->rd_rel)
262
263 /*
264  * RelationGetRelid
265  *              Returns the OID of the relation
266  */
267 #define RelationGetRelid(relation) ((relation)->rd_id)
268
269 /*
270  * RelationGetNumberOfAttributes
271  *              Returns the number of attributes in a relation.
272  */
273 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
274
275 /*
276  * RelationGetDescr
277  *              Returns tuple descriptor for a relation.
278  */
279 #define RelationGetDescr(relation) ((relation)->rd_att)
280
281 /*
282  * RelationGetRelationName
283  *              Returns the rel's name.
284  *
285  * Note that the name is only unique within the containing namespace.
286  */
287 #define RelationGetRelationName(relation) \
288         (NameStr((relation)->rd_rel->relname))
289
290 /*
291  * RelationGetNamespace
292  *              Returns the rel's namespace OID.
293  */
294 #define RelationGetNamespace(relation) \
295         ((relation)->rd_rel->relnamespace)
296
297 /*
298  * RelationIsMapped
299  *              True if the relation uses the relfilenode map.
300  *
301  * NB: this is only meaningful for relkinds that have storage, else it
302  * will misleadingly say "true".
303  */
304 #define RelationIsMapped(relation) \
305         ((relation)->rd_rel->relfilenode == InvalidOid)
306
307 /*
308  * RelationOpenSmgr
309  *              Open the relation at the smgr level, if not already done.
310  */
311 #define RelationOpenSmgr(relation) \
312         do { \
313                 if ((relation)->rd_smgr == NULL) \
314                         smgrsetowner(&((relation)->rd_smgr), smgropen((relation)->rd_node, (relation)->rd_backend)); \
315         } while (0)
316
317 /*
318  * RelationCloseSmgr
319  *              Close the relation at the smgr level, if not already done.
320  *
321  * Note: smgrclose should unhook from owner pointer, hence the Assert.
322  */
323 #define RelationCloseSmgr(relation) \
324         do { \
325                 if ((relation)->rd_smgr != NULL) \
326                 { \
327                         smgrclose((relation)->rd_smgr); \
328                         Assert((relation)->rd_smgr == NULL); \
329                 } \
330         } while (0)
331
332 /*
333  * RelationGetTargetBlock
334  *              Fetch relation's current insertion target block.
335  *
336  * Returns InvalidBlockNumber if there is no current target block.      Note
337  * that the target block status is discarded on any smgr-level invalidation.
338  */
339 #define RelationGetTargetBlock(relation) \
340         ( (relation)->rd_smgr != NULL ? (relation)->rd_smgr->smgr_targblock : InvalidBlockNumber )
341
342 /*
343  * RelationSetTargetBlock
344  *              Set relation's current insertion target block.
345  */
346 #define RelationSetTargetBlock(relation, targblock) \
347         do { \
348                 RelationOpenSmgr(relation); \
349                 (relation)->rd_smgr->smgr_targblock = (targblock); \
350         } while (0)
351
352 /*
353  * RelationNeedsWAL
354  *              True if relation needs WAL.
355  */
356 #define RelationNeedsWAL(relation) \
357         ((relation)->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT)
358
359 /*
360  * RelationUsesLocalBuffers
361  *              True if relation's pages are stored in local buffers.
362  */
363 #define RelationUsesLocalBuffers(relation) \
364         ((relation)->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
365
366 /*
367  * RELATION_IS_LOCAL
368  *              If a rel is either temp or newly created in the current transaction,
369  *              it can be assumed to be accessible only to the current backend.
370  *              This is typically used to decide that we can skip acquiring locks.
371  *
372  * Beware of multiple eval of argument
373  */
374 #define RELATION_IS_LOCAL(relation) \
375         ((relation)->rd_islocaltemp || \
376          (relation)->rd_createSubid != InvalidSubTransactionId)
377
378 /*
379  * RELATION_IS_OTHER_TEMP
380  *              Test for a temporary relation that belongs to some other session.
381  *
382  * Beware of multiple eval of argument
383  */
384 #define RELATION_IS_OTHER_TEMP(relation) \
385         ((relation)->rd_rel->relpersistence == RELPERSISTENCE_TEMP && \
386          !(relation)->rd_islocaltemp)
387
388 /* routines in utils/cache/relcache.c */
389 extern void RelationIncrementReferenceCount(Relation rel);
390 extern void RelationDecrementReferenceCount(Relation rel);
391
392 #endif   /* REL_H */