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.118 2010/01/02 16:58:10 momjian 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 */
137 SubTransactionId rd_createSubid; /* rel was created in current xact */
138 SubTransactionId rd_newRelfilenodeSubid; /* new relfilenode assigned in
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).
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 */
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
166 bytea *rd_options; /* parsed pg_class.reloptions */
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 */
175 * index access support info (used only for an index relation)
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.
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.
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 */
205 * sizes of the free space and visibility map forks, or InvalidBlockNumber
208 BlockNumber rd_fsm_nblocks;
209 BlockNumber rd_vm_nblocks;
211 /* use "struct" here to avoid needing to include pgstat.h: */
212 struct PgStat_TableStatus *pgstat_info; /* statistics collection area */
217 * Standard contents of rd_options for heaps and generic indexes.
219 * RelationGetFillFactor() and RelationGetTargetPageFreeSpace() can only
220 * be applied to relations that use this format or a superset for
221 * private options data.
223 /* autovacuum-related reloptions. */
224 typedef struct AutoVacOpts
227 int vacuum_threshold;
228 int analyze_threshold;
229 int vacuum_cost_delay;
230 int vacuum_cost_limit;
233 int freeze_table_age;
234 float8 vacuum_scale_factor;
235 float8 analyze_scale_factor;
238 typedef struct StdRdOptions
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 */
245 #define HEAP_MIN_FILLFACTOR 10
246 #define HEAP_DEFAULT_FILLFACTOR 100
249 * RelationGetFillFactor
250 * Returns the relation's fillfactor. Note multiple eval of argument!
252 #define RelationGetFillFactor(relation, defaultff) \
253 ((relation)->rd_options ? \
254 ((StdRdOptions *) (relation)->rd_options)->fillfactor : (defaultff))
257 * RelationGetTargetPageUsage
258 * Returns the relation's desired space usage per page in bytes.
260 #define RelationGetTargetPageUsage(relation, defaultff) \
261 (BLCKSZ * RelationGetFillFactor(relation, defaultff) / 100)
264 * RelationGetTargetPageFreeSpace
265 * Returns the relation's desired freespace per page in bytes.
267 #define RelationGetTargetPageFreeSpace(relation, defaultff) \
268 (BLCKSZ * (100 - RelationGetFillFactor(relation, defaultff)) / 100)
272 * True iff relation descriptor is valid.
274 #define RelationIsValid(relation) PointerIsValid(relation)
276 #define InvalidRelation ((Relation) NULL)
279 * RelationHasReferenceCountZero
280 * True iff relation reference count is zero.
283 * Assumes relation descriptor is valid.
285 #define RelationHasReferenceCountZero(relation) \
286 ((bool)((relation)->rd_refcnt == 0))
290 * Returns pg_class tuple for a relation.
293 * Assumes relation descriptor is valid.
295 #define RelationGetForm(relation) ((relation)->rd_rel)
299 * Returns the OID of the relation
301 #define RelationGetRelid(relation) ((relation)->rd_id)
304 * RelationGetNumberOfAttributes
305 * Returns the number of attributes in a relation.
307 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
311 * Returns tuple descriptor for a relation.
313 #define RelationGetDescr(relation) ((relation)->rd_att)
316 * RelationGetRelationName
317 * Returns the rel's name.
319 * Note that the name is only unique within the containing namespace.
321 #define RelationGetRelationName(relation) \
322 (NameStr((relation)->rd_rel->relname))
325 * RelationGetNamespace
326 * Returns the rel's namespace OID.
328 #define RelationGetNamespace(relation) \
329 ((relation)->rd_rel->relnamespace)
333 * Open the relation at the smgr level, if not already done.
335 #define RelationOpenSmgr(relation) \
337 if ((relation)->rd_smgr == NULL) \
338 smgrsetowner(&((relation)->rd_smgr), smgropen((relation)->rd_node)); \
343 * Close the relation at the smgr level, if not already done.
345 * Note: smgrclose should unhook from owner pointer, hence the Assert.
347 #define RelationCloseSmgr(relation) \
349 if ((relation)->rd_smgr != NULL) \
351 smgrclose((relation)->rd_smgr); \
352 Assert((relation)->rd_smgr == NULL); \
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.
361 * Beware of multiple eval of argument
363 #define RELATION_IS_LOCAL(relation) \
364 ((relation)->rd_islocaltemp || \
365 (relation)->rd_createSubid != InvalidSubTransactionId)
368 * RELATION_IS_OTHER_TEMP
369 * Test for a temporary relation that belongs to some other session.
371 * Beware of multiple eval of argument
373 #define RELATION_IS_OTHER_TEMP(relation) \
374 ((relation)->rd_istemp && !(relation)->rd_islocaltemp)
376 /* routines in utils/cache/relcache.c */
377 extern void RelationIncrementReferenceCount(Relation rel);
378 extern void RelationDecrementReferenceCount(Relation rel);