1 /*-------------------------------------------------------------------------
4 * POSTGRES relation descriptor (a/k/a relcache entry) definitions.
7 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
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 */
70 typedef struct TriggerDesc
73 * Index data to identify which triggers are which. Since each trigger
74 * can appear in more than one class, for each class we provide a list of
75 * integer indexes into the triggers array. The class codes are defined
76 * by TRIGGER_EVENT_xxx macros in commands/trigger.h.
78 #define TRIGGER_NUM_EVENT_CLASSES 4
80 uint16 n_before_statement
[TRIGGER_NUM_EVENT_CLASSES
];
81 uint16 n_before_row
[TRIGGER_NUM_EVENT_CLASSES
];
82 uint16 n_after_row
[TRIGGER_NUM_EVENT_CLASSES
];
83 uint16 n_after_statement
[TRIGGER_NUM_EVENT_CLASSES
];
84 int *tg_before_statement
[TRIGGER_NUM_EVENT_CLASSES
];
85 int *tg_before_row
[TRIGGER_NUM_EVENT_CLASSES
];
86 int *tg_after_row
[TRIGGER_NUM_EVENT_CLASSES
];
87 int *tg_after_statement
[TRIGGER_NUM_EVENT_CLASSES
];
89 /* The actual array of triggers is here */
96 * Cached lookup information for the index access method functions defined
97 * by the pg_am row associated with an index relation.
99 typedef struct RelationAmInfo
102 FmgrInfo ambeginscan
;
104 FmgrInfo amgetbitmap
;
110 FmgrInfo ambulkdelete
;
111 FmgrInfo amvacuumcleanup
;
112 FmgrInfo amcostestimate
;
118 * Here are the contents of a relation cache entry.
121 typedef struct RelationData
123 RelFileNode rd_node
; /* relation physical identifier */
124 /* use "struct" here to avoid needing to include smgr.h: */
125 struct SMgrRelationData
*rd_smgr
; /* cached file handle, or NULL */
126 BlockNumber rd_targblock
; /* current insertion target block, or
127 * InvalidBlockNumber */
128 int rd_refcnt
; /* reference count */
129 bool rd_istemp
; /* rel uses the local buffer mgr */
130 bool rd_isnailed
; /* rel is nailed in cache */
131 bool rd_isvalid
; /* relcache entry is valid */
132 char rd_indexvalid
; /* state of rd_indexlist: 0 = not valid, 1 =
133 * valid, 2 = temporarily forced */
134 SubTransactionId rd_createSubid
; /* rel was created in current xact */
135 SubTransactionId rd_newRelfilenodeSubid
; /* new relfilenode assigned in
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 Form_pg_class rd_rel
; /* RELATION tuple */
148 TupleDesc rd_att
; /* tuple descriptor */
149 Oid rd_id
; /* relation's object id */
150 List
*rd_indexlist
; /* list of OIDs of indexes on relation */
151 Bitmapset
*rd_indexattr
; /* identifies columns used in indexes */
152 Oid rd_oidindex
; /* OID of unique index on OID, if any */
153 LockInfoData rd_lockInfo
; /* lock mgr's info for locking relation */
154 RuleLock
*rd_rules
; /* rewrite rules */
155 MemoryContext rd_rulescxt
; /* private memory cxt for rd_rules, if any */
156 TriggerDesc
*trigdesc
; /* Trigger info, or NULL if rel has none */
159 * rd_options is set whenever rd_rel is loaded into the relcache entry.
160 * Note that you can NOT look into rd_rel for this data. NULL means "use
163 bytea
*rd_options
; /* parsed pg_class.reloptions */
165 /* These are non-NULL only for an index relation: */
166 Form_pg_index rd_index
; /* pg_index tuple describing this index */
167 struct HeapTupleData
*rd_indextuple
; /* all of pg_index tuple */
168 /* "struct HeapTupleData *" avoids need to include htup.h here */
169 Form_pg_am rd_am
; /* pg_am tuple for index's AM */
172 * index access support info (used only for an index relation)
174 * Note: only default operators and support procs for each opclass are
175 * cached, namely those with lefttype and righttype equal to the opclass's
176 * opcintype. The arrays are indexed by strategy or support number, which
177 * is a sufficient identifier given that restriction.
179 * Note: rd_amcache is available for index AMs to cache private data about
180 * an index. This must be just a cache since it may get reset at any time
181 * (in particular, it will get reset by a relcache inval message for the
182 * index). If used, it must point to a single memory chunk palloc'd in
183 * rd_indexcxt. A relcache reset will include freeing that chunk and
184 * setting rd_amcache = NULL.
186 MemoryContext rd_indexcxt
; /* private memory cxt for this stuff */
187 RelationAmInfo
*rd_aminfo
; /* lookup info for funcs found in pg_am */
188 Oid
*rd_opfamily
; /* OIDs of op families for each index col */
189 Oid
*rd_opcintype
; /* OIDs of opclass declared input data types */
190 Oid
*rd_operator
; /* OIDs of index operators */
191 RegProcedure
*rd_support
; /* OIDs of support procedures */
192 FmgrInfo
*rd_supportinfo
; /* lookup info for support procedures */
193 int16
*rd_indoption
; /* per-column AM-specific flags */
194 List
*rd_indexprs
; /* index expression trees, if any */
195 List
*rd_indpred
; /* index predicate tree, if any */
196 void *rd_amcache
; /* available for use by index AM */
199 * sizes of the free space and visibility map forks, or InvalidBlockNumber
202 BlockNumber rd_fsm_nblocks
;
203 BlockNumber rd_vm_nblocks
;
205 /* use "struct" here to avoid needing to include pgstat.h: */
206 struct PgStat_TableStatus
*pgstat_info
; /* statistics collection area */
211 * Standard contents of rd_options for heaps and generic indexes.
213 * RelationGetFillFactor() and RelationGetTargetPageFreeSpace() can only
214 * be applied to relations that use this format or a superset for
215 * private options data.
217 /* autovacuum-related reloptions. */
218 typedef struct AutoVacOpts
221 int vacuum_threshold
;
222 int analyze_threshold
;
223 int vacuum_cost_delay
;
224 int vacuum_cost_limit
;
227 int freeze_table_age
;
228 float8 vacuum_scale_factor
;
229 float8 analyze_scale_factor
;
232 typedef struct StdRdOptions
234 int32 vl_len_
; /* varlena header (do not touch directly!) */
235 int fillfactor
; /* page fill factor in percent (0..100) */
236 AutoVacOpts autovacuum
; /* autovacuum-related options */
239 #define HEAP_MIN_FILLFACTOR 10
240 #define HEAP_DEFAULT_FILLFACTOR 100
243 * RelationGetFillFactor
244 * Returns the relation's fillfactor. Note multiple eval of argument!
246 #define RelationGetFillFactor(relation, defaultff) \
247 ((relation)->rd_options ? \
248 ((StdRdOptions *) (relation)->rd_options)->fillfactor : (defaultff))
251 * RelationGetTargetPageUsage
252 * Returns the relation's desired space usage per page in bytes.
254 #define RelationGetTargetPageUsage(relation, defaultff) \
255 (BLCKSZ * RelationGetFillFactor(relation, defaultff) / 100)
258 * RelationGetTargetPageFreeSpace
259 * Returns the relation's desired freespace per page in bytes.
261 #define RelationGetTargetPageFreeSpace(relation, defaultff) \
262 (BLCKSZ * (100 - RelationGetFillFactor(relation, defaultff)) / 100)
266 * True iff relation descriptor is valid.
268 #define RelationIsValid(relation) PointerIsValid(relation)
270 #define InvalidRelation ((Relation) NULL)
273 * RelationHasReferenceCountZero
274 * True iff relation reference count is zero.
277 * Assumes relation descriptor is valid.
279 #define RelationHasReferenceCountZero(relation) \
280 ((bool)((relation)->rd_refcnt == 0))
284 * Returns pg_class tuple for a relation.
287 * Assumes relation descriptor is valid.
289 #define RelationGetForm(relation) ((relation)->rd_rel)
293 * Returns the OID of the relation
295 #define RelationGetRelid(relation) ((relation)->rd_id)
298 * RelationGetNumberOfAttributes
299 * Returns the number of attributes in a relation.
301 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
305 * Returns tuple descriptor for a relation.
307 #define RelationGetDescr(relation) ((relation)->rd_att)
310 * RelationGetRelationName
311 * Returns the rel's name.
313 * Note that the name is only unique within the containing namespace.
315 #define RelationGetRelationName(relation) \
316 (NameStr((relation)->rd_rel->relname))
319 * RelationGetNamespace
320 * Returns the rel's namespace OID.
322 #define RelationGetNamespace(relation) \
323 ((relation)->rd_rel->relnamespace)
327 * Open the relation at the smgr level, if not already done.
329 #define RelationOpenSmgr(relation) \
331 if ((relation)->rd_smgr == NULL) \
332 smgrsetowner(&((relation)->rd_smgr), smgropen((relation)->rd_node)); \
337 * Close the relation at the smgr level, if not already done.
339 * Note: smgrclose should unhook from owner pointer, hence the Assert.
341 #define RelationCloseSmgr(relation) \
343 if ((relation)->rd_smgr != NULL) \
345 smgrclose((relation)->rd_smgr); \
346 Assert((relation)->rd_smgr == NULL); \
352 * If a rel is either temp or newly created in the current transaction,
353 * it can be assumed to be visible only to the current backend.
355 * Beware of multiple eval of argument
357 #define RELATION_IS_LOCAL(relation) \
358 ((relation)->rd_istemp || \
359 (relation)->rd_createSubid != InvalidSubTransactionId)
361 /* routines in utils/cache/relcache.c */
362 extern void RelationIncrementReferenceCount(Relation rel
);
363 extern void RelationDecrementReferenceCount(Relation rel
);