1 /*-------------------------------------------------------------------------
4 * POSTGRES relation descriptor (a/k/a relcache entry) definitions.
7 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/utils/rel.h
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"
27 #include "utils/reltrigger.h"
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.
35 typedef struct LockRelId
37 Oid relId; /* a relation identifier */
38 Oid dbId; /* a database identifier */
41 typedef struct LockInfoData
46 typedef LockInfoData *LockInfo;
50 * Cached lookup information for the index access method functions defined
51 * by the pg_am row associated with an index relation.
53 typedef struct RelationAmInfo
64 FmgrInfo ambuildempty;
65 FmgrInfo ambulkdelete;
66 FmgrInfo amvacuumcleanup;
68 FmgrInfo amcostestimate;
74 * Here are the contents of a relation cache entry.
77 typedef struct RelationData
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 */
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).
99 SubTransactionId rd_createSubid; /* rel was created in current xact */
100 SubTransactionId rd_newRelfilenodeSubid; /* new relfilenode assigned in
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 */
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
119 bytea *rd_options; /* parsed pg_class.reloptions */
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 */
128 * index access support info (used only for an index relation)
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.
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.
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 */
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.
166 Oid rd_toastoid; /* Real TOAST table's OID, or InvalidOid */
168 /* use "struct" here to avoid needing to include pgstat.h: */
169 struct PgStat_TableStatus *pgstat_info; /* statistics collection area */
174 * Standard contents of rd_options for heaps and generic indexes.
176 * RelationGetFillFactor() and RelationGetTargetPageFreeSpace() can only
177 * be applied to relations that use this format or a superset for
178 * private options data.
180 /* autovacuum-related reloptions. */
181 typedef struct AutoVacOpts
184 int vacuum_threshold;
185 int analyze_threshold;
186 int vacuum_cost_delay;
187 int vacuum_cost_limit;
190 int freeze_table_age;
191 float8 vacuum_scale_factor;
192 float8 analyze_scale_factor;
195 typedef struct StdRdOptions
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 */
203 #define HEAP_MIN_FILLFACTOR 10
204 #define HEAP_DEFAULT_FILLFACTOR 100
207 * RelationGetFillFactor
208 * Returns the relation's fillfactor. Note multiple eval of argument!
210 #define RelationGetFillFactor(relation, defaultff) \
211 ((relation)->rd_options ? \
212 ((StdRdOptions *) (relation)->rd_options)->fillfactor : (defaultff))
215 * RelationGetTargetPageUsage
216 * Returns the relation's desired space usage per page in bytes.
218 #define RelationGetTargetPageUsage(relation, defaultff) \
219 (BLCKSZ * RelationGetFillFactor(relation, defaultff) / 100)
222 * RelationGetTargetPageFreeSpace
223 * Returns the relation's desired freespace per page in bytes.
225 #define RelationGetTargetPageFreeSpace(relation, defaultff) \
226 (BLCKSZ * (100 - RelationGetFillFactor(relation, defaultff)) / 100)
229 * RelationIsSecurityView
230 * Returns whether the relation is security view, or not
232 #define RelationIsSecurityView(relation) \
233 ((relation)->rd_options ? \
234 ((StdRdOptions *) (relation)->rd_options)->security_barrier : false)
238 * True iff relation descriptor is valid.
240 #define RelationIsValid(relation) PointerIsValid(relation)
242 #define InvalidRelation ((Relation) NULL)
245 * RelationHasReferenceCountZero
246 * True iff relation reference count is zero.
249 * Assumes relation descriptor is valid.
251 #define RelationHasReferenceCountZero(relation) \
252 ((bool)((relation)->rd_refcnt == 0))
256 * Returns pg_class tuple for a relation.
259 * Assumes relation descriptor is valid.
261 #define RelationGetForm(relation) ((relation)->rd_rel)
265 * Returns the OID of the relation
267 #define RelationGetRelid(relation) ((relation)->rd_id)
270 * RelationGetNumberOfAttributes
271 * Returns the number of attributes in a relation.
273 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
277 * Returns tuple descriptor for a relation.
279 #define RelationGetDescr(relation) ((relation)->rd_att)
282 * RelationGetRelationName
283 * Returns the rel's name.
285 * Note that the name is only unique within the containing namespace.
287 #define RelationGetRelationName(relation) \
288 (NameStr((relation)->rd_rel->relname))
291 * RelationGetNamespace
292 * Returns the rel's namespace OID.
294 #define RelationGetNamespace(relation) \
295 ((relation)->rd_rel->relnamespace)
299 * True if the relation uses the relfilenode map.
301 * NB: this is only meaningful for relkinds that have storage, else it
302 * will misleadingly say "true".
304 #define RelationIsMapped(relation) \
305 ((relation)->rd_rel->relfilenode == InvalidOid)
309 * Open the relation at the smgr level, if not already done.
311 #define RelationOpenSmgr(relation) \
313 if ((relation)->rd_smgr == NULL) \
314 smgrsetowner(&((relation)->rd_smgr), smgropen((relation)->rd_node, (relation)->rd_backend)); \
319 * Close the relation at the smgr level, if not already done.
321 * Note: smgrclose should unhook from owner pointer, hence the Assert.
323 #define RelationCloseSmgr(relation) \
325 if ((relation)->rd_smgr != NULL) \
327 smgrclose((relation)->rd_smgr); \
328 Assert((relation)->rd_smgr == NULL); \
333 * RelationGetTargetBlock
334 * Fetch relation's current insertion target block.
336 * Returns InvalidBlockNumber if there is no current target block. Note
337 * that the target block status is discarded on any smgr-level invalidation.
339 #define RelationGetTargetBlock(relation) \
340 ( (relation)->rd_smgr != NULL ? (relation)->rd_smgr->smgr_targblock : InvalidBlockNumber )
343 * RelationSetTargetBlock
344 * Set relation's current insertion target block.
346 #define RelationSetTargetBlock(relation, targblock) \
348 RelationOpenSmgr(relation); \
349 (relation)->rd_smgr->smgr_targblock = (targblock); \
354 * True if relation needs WAL.
356 #define RelationNeedsWAL(relation) \
357 ((relation)->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT)
360 * RelationUsesLocalBuffers
361 * True if relation's pages are stored in local buffers.
363 #define RelationUsesLocalBuffers(relation) \
364 ((relation)->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
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.
372 * Beware of multiple eval of argument
374 #define RELATION_IS_LOCAL(relation) \
375 ((relation)->rd_islocaltemp || \
376 (relation)->rd_createSubid != InvalidSubTransactionId)
379 * RELATION_IS_OTHER_TEMP
380 * Test for a temporary relation that belongs to some other session.
382 * Beware of multiple eval of argument
384 #define RELATION_IS_OTHER_TEMP(relation) \
385 ((relation)->rd_rel->relpersistence == RELPERSISTENCE_TEMP && \
386 !(relation)->rd_islocaltemp)
388 /* routines in utils/cache/relcache.c */
389 extern void RelationIncrementReferenceCount(Relation rel);
390 extern void RelationDecrementReferenceCount(Relation rel);