1 /*-------------------------------------------------------------------------
4 * POSTGRES relation descriptor (a/k/a relcache entry) definitions.
7 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/include/utils/rel.h,v 1.120 2010/01/17 22:56:23 tgl Exp $
12 *-------------------------------------------------------------------------
17 #include "access/tupdesc.h"
18 #include "catalog/pg_am.h"
19 #include "catalog/pg_class.h"
20 #include "catalog/pg_index.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"
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.
34 typedef struct LockRelId
36 Oid relId; /* a relation identifier */
37 Oid dbId; /* a database identifier */
40 typedef struct LockInfoData
45 typedef LockInfoData *LockInfo;
48 * Likewise, this struct really belongs to trigger.h, but for convenience
51 typedef struct Trigger
53 Oid tgoid; /* OID of trigger (pg_trigger row) */
54 /* Remaining fields are copied from pg_trigger, see pg_trigger.h */
72 typedef struct TriggerDesc
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.
80 #define TRIGGER_NUM_EVENT_CLASSES 4
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];
91 /* The actual array of triggers is here */
98 * Cached lookup information for the index access method functions defined
99 * by the pg_am row associated with an index relation.
101 typedef struct RelationAmInfo
104 FmgrInfo ambeginscan;
106 FmgrInfo amgetbitmap;
112 FmgrInfo ambulkdelete;
113 FmgrInfo amvacuumcleanup;
114 FmgrInfo amcostestimate;
120 * Here are the contents of a relation cache entry.
123 typedef struct RelationData
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 */
139 * rd_createSubid is the ID of the highest subtransaction the rel has
140 * survived into; or zero if the rel was not created in the current top
141 * transaction. This should be relied on only for optimization purposes;
142 * it is possible for new-ness to be "forgotten" (eg, after CLUSTER).
143 * Likewise, rd_newRelfilenodeSubid is the ID of the highest
144 * subtransaction the relfilenode change has survived into, or zero if not
145 * changed in the current transaction (or we have forgotten changing it).
147 SubTransactionId rd_createSubid; /* rel was created in current xact */
148 SubTransactionId rd_newRelfilenodeSubid; /* new relfilenode assigned in
151 Form_pg_class rd_rel; /* RELATION tuple */
152 TupleDesc rd_att; /* tuple descriptor */
153 Oid rd_id; /* relation's object id */
154 List *rd_indexlist; /* list of OIDs of indexes on relation */
155 Bitmapset *rd_indexattr; /* identifies columns used in indexes */
156 Oid rd_oidindex; /* OID of unique index on OID, if any */
157 LockInfoData rd_lockInfo; /* lock mgr's info for locking relation */
158 RuleLock *rd_rules; /* rewrite rules */
159 MemoryContext rd_rulescxt; /* private memory cxt for rd_rules, if any */
160 TriggerDesc *trigdesc; /* Trigger info, or NULL if rel has none */
163 * rd_options is set whenever rd_rel is loaded into the relcache entry.
164 * Note that you can NOT look into rd_rel for this data. NULL means "use
167 bytea *rd_options; /* parsed pg_class.reloptions */
169 /* These are non-NULL only for an index relation: */
170 Form_pg_index rd_index; /* pg_index tuple describing this index */
171 /* use "struct" here to avoid needing to include htup.h: */
172 struct HeapTupleData *rd_indextuple; /* all of pg_index tuple */
173 Form_pg_am rd_am; /* pg_am tuple for index's AM */
176 * index access support info (used only for an index relation)
178 * Note: only default operators and support procs for each opclass are
179 * cached, namely those with lefttype and righttype equal to the opclass's
180 * opcintype. The arrays are indexed by strategy or support number, which
181 * is a sufficient identifier given that restriction.
183 * Note: rd_amcache is available for index AMs to cache private data about
184 * an index. This must be just a cache since it may get reset at any time
185 * (in particular, it will get reset by a relcache inval message for the
186 * index). If used, it must point to a single memory chunk palloc'd in
187 * rd_indexcxt. A relcache reset will include freeing that chunk and
188 * setting rd_amcache = NULL.
190 MemoryContext rd_indexcxt; /* private memory cxt for this stuff */
191 RelationAmInfo *rd_aminfo; /* lookup info for funcs found in pg_am */
192 Oid *rd_opfamily; /* OIDs of op families for each index col */
193 Oid *rd_opcintype; /* OIDs of opclass declared input data types */
194 Oid *rd_operator; /* OIDs of index operators */
195 RegProcedure *rd_support; /* OIDs of support procedures */
196 FmgrInfo *rd_supportinfo; /* lookup info for support procedures */
197 int16 *rd_indoption; /* per-column AM-specific flags */
198 List *rd_indexprs; /* index expression trees, if any */
199 List *rd_indpred; /* index predicate tree, if any */
200 Oid *rd_exclops; /* OIDs of exclusion operators, if any */
201 Oid *rd_exclprocs; /* OIDs of exclusion ops' procs, if any */
202 uint16 *rd_exclstrats; /* exclusion ops' strategy numbers, if any */
203 void *rd_amcache; /* available for use by index AM */
206 * sizes of the free space and visibility map forks, or InvalidBlockNumber
209 BlockNumber rd_fsm_nblocks;
210 BlockNumber rd_vm_nblocks;
212 /* use "struct" here to avoid needing to include pgstat.h: */
213 struct PgStat_TableStatus *pgstat_info; /* statistics collection area */
218 * Standard contents of rd_options for heaps and generic indexes.
220 * RelationGetFillFactor() and RelationGetTargetPageFreeSpace() can only
221 * be applied to relations that use this format or a superset for
222 * private options data.
224 /* autovacuum-related reloptions. */
225 typedef struct AutoVacOpts
228 int vacuum_threshold;
229 int analyze_threshold;
230 int vacuum_cost_delay;
231 int vacuum_cost_limit;
234 int freeze_table_age;
235 float8 vacuum_scale_factor;
236 float8 analyze_scale_factor;
239 typedef struct StdRdOptions
241 int32 vl_len_; /* varlena header (do not touch directly!) */
242 int fillfactor; /* page fill factor in percent (0..100) */
243 AutoVacOpts autovacuum; /* autovacuum-related options */
246 #define HEAP_MIN_FILLFACTOR 10
247 #define HEAP_DEFAULT_FILLFACTOR 100
250 * RelationGetFillFactor
251 * Returns the relation's fillfactor. Note multiple eval of argument!
253 #define RelationGetFillFactor(relation, defaultff) \
254 ((relation)->rd_options ? \
255 ((StdRdOptions *) (relation)->rd_options)->fillfactor : (defaultff))
258 * RelationGetTargetPageUsage
259 * Returns the relation's desired space usage per page in bytes.
261 #define RelationGetTargetPageUsage(relation, defaultff) \
262 (BLCKSZ * RelationGetFillFactor(relation, defaultff) / 100)
265 * RelationGetTargetPageFreeSpace
266 * Returns the relation's desired freespace per page in bytes.
268 #define RelationGetTargetPageFreeSpace(relation, defaultff) \
269 (BLCKSZ * (100 - RelationGetFillFactor(relation, defaultff)) / 100)
273 * True iff relation descriptor is valid.
275 #define RelationIsValid(relation) PointerIsValid(relation)
277 #define InvalidRelation ((Relation) NULL)
280 * RelationHasReferenceCountZero
281 * True iff relation reference count is zero.
284 * Assumes relation descriptor is valid.
286 #define RelationHasReferenceCountZero(relation) \
287 ((bool)((relation)->rd_refcnt == 0))
291 * Returns pg_class tuple for a relation.
294 * Assumes relation descriptor is valid.
296 #define RelationGetForm(relation) ((relation)->rd_rel)
300 * Returns the OID of the relation
302 #define RelationGetRelid(relation) ((relation)->rd_id)
305 * RelationGetNumberOfAttributes
306 * Returns the number of attributes in a relation.
308 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
312 * Returns tuple descriptor for a relation.
314 #define RelationGetDescr(relation) ((relation)->rd_att)
317 * RelationGetRelationName
318 * Returns the rel's name.
320 * Note that the name is only unique within the containing namespace.
322 #define RelationGetRelationName(relation) \
323 (NameStr((relation)->rd_rel->relname))
326 * RelationGetNamespace
327 * Returns the rel's namespace OID.
329 #define RelationGetNamespace(relation) \
330 ((relation)->rd_rel->relnamespace)
334 * Open the relation at the smgr level, if not already done.
336 #define RelationOpenSmgr(relation) \
338 if ((relation)->rd_smgr == NULL) \
339 smgrsetowner(&((relation)->rd_smgr), smgropen((relation)->rd_node)); \
344 * Close the relation at the smgr level, if not already done.
346 * Note: smgrclose should unhook from owner pointer, hence the Assert.
348 #define RelationCloseSmgr(relation) \
350 if ((relation)->rd_smgr != NULL) \
352 smgrclose((relation)->rd_smgr); \
353 Assert((relation)->rd_smgr == NULL); \
359 * If a rel is either temp or newly created in the current transaction,
360 * it can be assumed to be visible only to the current backend.
362 * Beware of multiple eval of argument
364 #define RELATION_IS_LOCAL(relation) \
365 ((relation)->rd_islocaltemp || \
366 (relation)->rd_createSubid != InvalidSubTransactionId)
369 * RELATION_IS_OTHER_TEMP
370 * Test for a temporary relation that belongs to some other session.
372 * Beware of multiple eval of argument
374 #define RELATION_IS_OTHER_TEMP(relation) \
375 ((relation)->rd_istemp && !(relation)->rd_islocaltemp)
377 /* routines in utils/cache/relcache.c */
378 extern void RelationIncrementReferenceCount(Relation rel);
379 extern void RelationDecrementReferenceCount(Relation rel);