1/*-------------------------------------------------------------------------
4 * POSTGRES relation descriptor (a/k/a relcache entry) definitions.
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/utils/rel.h
12 *-------------------------------------------------------------------------
34 * LockRelId and LockInfo really belong to lmgr.h, but it's more convenient
35 * to declare them here so we can have a LockInfoData field in a Relation.
52 * Here are the contents of a relation cache entry.
69 * rd_createSubid is the ID of the highest subtransaction the rel has
70 * survived into or zero if the rel or its storage was created before the
71 * current top transaction. (IndexStmt.oldNumber leads to the case of a new
72 * rel with an old rd_locator.) rd_firstRelfilelocatorSubid is the ID of the
73 * highest subtransaction an rd_locator change has survived into or zero if
74 * rd_locator matches the value it had at the start of the current top
75 * transaction. (Rolling back the subtransaction that
76 * rd_firstRelfilelocatorSubid denotes would restore rd_locator to the value it
77 * had at the start of the current top transaction. Rolling back any
78 * lower subtransaction would not.) Their accuracy is critical to
81 * rd_newRelfilelocatorSubid is the ID of the highest subtransaction the
82 * most-recent relfilenumber change has survived into or zero if not changed
83 * in the current transaction (or we have forgotten changing it). This
84 * field is accurate when non-zero, but it can be zero when a relation has
85 * multiple new relfilenumbers within a single transaction, with one of them
86 * occurring in a subsequently aborted subtransaction, e.g.
92 * -- rd_newRelfilelocatorSubid is now forgotten
94 * If every rd_*Subid field is zero, they are read-only outside
95 * relcache.c. Files that trigger rd_locator changes by updating
96 * pg_class.reltablespace and/or pg_class.relfilenode call
97 * RelationAssumeNewRelfilelocator() to update rd_*Subid.
99 * rd_droppedSubid is the ID of the highest subtransaction that a drop of
100 * the rel has survived into. In entries visible outside relcache.c, this
105 * rd_locator to current value */
107 * changing rd_locator to
118 /* use "struct" here to avoid needing to include rowsecurity.h: */
121 /* data managed by RelationGetFKeyList: */
125 /* data managed by RelationGetPartitionKey: */
129 /* data managed by RelationGetPartitionDesc: */
133 /* Same as above, for partdescs that omit detached partitions */
138 * pg_inherits.xmin of the partition that was excluded in
139 * rd_partdesc_nodetached. This informs a future user of that partdesc:
140 * if this value is not in progress for the active snapshot, then the
141 * partdesc can be used, otherwise they have to build a new one. (This
142 * matches what find_inheritance_children_extended would do).
146 /* data managed by RelationGetPartitionQual: */
151 /* data managed by RelationGetIndexList: */
157 /* data managed by RelationGetStatExtList: */
160 /* data managed by RelationGetIndexAttrBitmap: */
171 * rd_options is set whenever rd_rel is loaded into the relcache entry.
172 * Note that you can NOT look into rd_rel for this data. NULL means "use
178 * Oid of the handler for this relation. For an index this is a function
179 * returning IndexAmRoutine, for table like relations a function returning
180 * TableAmRoutine. This is stored separately from rd_indam, rd_tableam as
181 * its lookup requires syscache access, but during relcache bootstrap we
182 * need to be able to initialize rd_tableam without syscache lookups.
187 * Table access method.
191 /* These are non-NULL only for an index relation: */
193 /* use "struct" here to avoid needing to include htup.h: */
197 * index access support info (used only for an index relation)
199 * Note: only default support procs for each opclass are cached, namely
200 * those with lefttype and righttype equal to the opclass's opcintype. The
201 * arrays are indexed by support function number, which is a sufficient
202 * identifier given that restriction.
205 /* use "struct" here to avoid needing to include amapi.h: */
221 * rd_amcache is available for index and table AMs to cache private data
222 * about the relation. This must be just a cache since it may get reset
223 * at any time (in particular, it will get reset by a relcache inval
224 * message for the relation). If used, it must point to a single memory
225 * chunk palloc'd in CacheMemoryContext, or in rd_indexcxt for an index
226 * relation. A relcache reset will include freeing that chunk and setting
232 * foreign-table support
234 * rd_fdwroutine must point to a single memory chunk palloc'd in
235 * CacheMemoryContext. It will be freed and reset to NULL on a relcache
239 /* use "struct" here to avoid needing to include fdwapi.h: */
243 * Hack for CLUSTER, rewriting ALTER TABLE, etc: when writing a new
244 * version of a table, we need to make any toast pointers inserted into it
245 * have the existing toast table's OID, not the OID of the transient toast
246 * table. If rd_toastoid isn't InvalidOid, it is the OID to place in
247 * toast pointers inserted into this rel. (Note it's set on the new
248 * version of the main heap, not the toast table itself.) This also
249 * causes toast_save_datum() to try to preserve toast value OIDs.
254 /* use "struct" here to avoid needing to include pgstat.h: */
260 * ForeignKeyCacheInfo
261 * Information the relcache can cache about foreign key constraints
263 * This is basically just an image of relevant columns from pg_constraint.
264 * We make it a subclass of Node so that copyObject() can be used on a list
265 * of these, but we also ensure it is a "flat" object without substructure,
266 * so that list_free_deep() is sufficient to free such a list.
267 * The per-FK-column arrays can be fixed-size because we allow at most
268 * INDEX_MAX_KEYS columns in a foreign key constraint.
270 * Currently, we mostly cache fields of interest to the planner, but the set
271 * of fields has already grown the constraint OID for other uses.
278 /* oid of the constraint itself */
280 /* relation constrained by the foreign key */
282 /* relation referenced by the foreign key */
284 /* number of columns in the foreign key */
291 * these arrays each have nkeys valid entries:
293 /* cols in referencing table */
295 /* cols in referenced table */
297 /* PK = FK operator OIDs */
304 * Standard contents of rd_options for heaps.
306 * RelationGetFillFactor() and RelationGetTargetPageFreeSpace() can only
307 * be applied to relations that use this format or a superset for
308 * private options data.
310 /* autovacuum-related reloptions. */
332/* StdRdOptions->vacuum_index_cleanup values */
353 * Fraction of pages in a relation that vacuum can eagerly scan and fail
354 * to freeze. 0 if disabled, -1 if unspecified.
359 #define HEAP_MIN_FILLFACTOR 10
360 #define HEAP_DEFAULT_FILLFACTOR 100
363 * RelationGetToastTupleTarget
364 * Returns the relation's toast_tuple_target. Note multiple eval of argument!
366 #define RelationGetToastTupleTarget(relation, defaulttarg) \
367 ((relation)->rd_options ? \
368 ((StdRdOptions *) (relation)->rd_options)->toast_tuple_target : (defaulttarg))
371 * RelationGetFillFactor
372 * Returns the relation's fillfactor. Note multiple eval of argument!
374 #define RelationGetFillFactor(relation, defaultff) \
375 ((relation)->rd_options ? \
376 ((StdRdOptions *) (relation)->rd_options)->fillfactor : (defaultff))
379 * RelationGetTargetPageUsage
380 * Returns the relation's desired space usage per page in bytes.
382 #define RelationGetTargetPageUsage(relation, defaultff) \
383 (BLCKSZ * RelationGetFillFactor(relation, defaultff) / 100)
386 * RelationGetTargetPageFreeSpace
387 * Returns the relation's desired freespace per page in bytes.
389 #define RelationGetTargetPageFreeSpace(relation, defaultff) \
390 (BLCKSZ * (100 - RelationGetFillFactor(relation, defaultff)) / 100)
393 * RelationIsUsedAsCatalogTable
394 * Returns whether the relation should be treated as a catalog table
395 * from the pov of logical decoding. Note multiple eval of argument!
397 #define RelationIsUsedAsCatalogTable(relation) \
398 ((relation)->rd_options && \
399 ((relation)->rd_rel->relkind == RELKIND_RELATION || \
400 (relation)->rd_rel->relkind == RELKIND_MATVIEW) ? \
401 ((StdRdOptions *) (relation)->rd_options)->user_catalog_table : false)
404 * RelationGetParallelWorkers
405 * Returns the relation's parallel_workers reloption setting.
406 * Note multiple eval of argument!
408 #define RelationGetParallelWorkers(relation, defaultpw) \
409 ((relation)->rd_options ? \
410 ((StdRdOptions *) (relation)->rd_options)->parallel_workers : (defaultpw))
412/* ViewOptions->check_option values */
422 * Contents of rd_options for views
433 * RelationIsSecurityView
434 * Returns whether the relation is security view, or not. Note multiple
437 #define RelationIsSecurityView(relation) \
438 (AssertMacro(relation->rd_rel->relkind == RELKIND_VIEW), \
439 (relation)->rd_options ? \
440 ((ViewOptions *) (relation)->rd_options)->security_barrier : false)
443 * RelationHasSecurityInvoker
444 * Returns true if the relation has the security_invoker property set.
445 * Note multiple eval of argument!
447 #define RelationHasSecurityInvoker(relation) \
448 (AssertMacro(relation->rd_rel->relkind == RELKIND_VIEW), \
449 (relation)->rd_options ? \
450 ((ViewOptions *) (relation)->rd_options)->security_invoker : false)
453 * RelationHasCheckOption
454 * Returns true if the relation is a view defined with either the local
455 * or the cascaded check option. Note multiple eval of argument!
457 #define RelationHasCheckOption(relation) \
458 (AssertMacro(relation->rd_rel->relkind == RELKIND_VIEW), \
459 (relation)->rd_options && \
460 ((ViewOptions *) (relation)->rd_options)->check_option != \
461 VIEW_OPTION_CHECK_OPTION_NOT_SET)
464 * RelationHasLocalCheckOption
465 * Returns true if the relation is a view defined with the local check
466 * option. Note multiple eval of argument!
468 #define RelationHasLocalCheckOption(relation) \
469 (AssertMacro(relation->rd_rel->relkind == RELKIND_VIEW), \
470 (relation)->rd_options && \
471 ((ViewOptions *) (relation)->rd_options)->check_option == \
472 VIEW_OPTION_CHECK_OPTION_LOCAL)
475 * RelationHasCascadedCheckOption
476 * Returns true if the relation is a view defined with the cascaded check
477 * option. Note multiple eval of argument!
479 #define RelationHasCascadedCheckOption(relation) \
480 (AssertMacro(relation->rd_rel->relkind == RELKIND_VIEW), \
481 (relation)->rd_options && \
482 ((ViewOptions *) (relation)->rd_options)->check_option == \
483 VIEW_OPTION_CHECK_OPTION_CASCADED)
487 * True iff relation descriptor is valid.
489 #define RelationIsValid(relation) ((relation) != NULL)
492 * RelationHasReferenceCountZero
493 * True iff relation reference count is zero.
496 * Assumes relation descriptor is valid.
498 #define RelationHasReferenceCountZero(relation) \
499 ((bool)((relation)->rd_refcnt == 0))
503 * Returns pg_class tuple for a relation.
506 * Assumes relation descriptor is valid.
508 #define RelationGetForm(relation) ((relation)->rd_rel)
512 * Returns the OID of the relation
514 #define RelationGetRelid(relation) ((relation)->rd_id)
517 * RelationGetNumberOfAttributes
518 * Returns the total number of attributes in a relation.
520 #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
523 * IndexRelationGetNumberOfAttributes
524 * Returns the number of attributes in an index.
526 #define IndexRelationGetNumberOfAttributes(relation) \
527 ((relation)->rd_index->indnatts)
530 * IndexRelationGetNumberOfKeyAttributes
531 * Returns the number of key attributes in an index.
533 #define IndexRelationGetNumberOfKeyAttributes(relation) \
534 ((relation)->rd_index->indnkeyatts)
538 * Returns tuple descriptor for a relation.
540 #define RelationGetDescr(relation) ((relation)->rd_att)
543 * RelationGetRelationName
544 * Returns the rel's name.
546 * Note that the name is only unique within the containing namespace.
548 #define RelationGetRelationName(relation) \
549 (NameStr((relation)->rd_rel->relname))
552 * RelationGetNamespace
553 * Returns the rel's namespace OID.
555 #define RelationGetNamespace(relation) \
556 ((relation)->rd_rel->relnamespace)
560 * True if the relation uses the relfilenumber map. Note multiple eval
563 #define RelationIsMapped(relation) \
564 (RELKIND_HAS_STORAGE((relation)->rd_rel->relkind) && \
565 ((relation)->rd_rel->relfilenode == InvalidRelFileNumber))
570 * Returns smgr file handle for a relation, opening it if needed.
572 * Very little code is authorized to touch rel->rd_smgr directly. Instead
573 * use this function to fetch its value.
588 * Close the relation at the smgr level, if not already done.
600#endif /* !FRONTEND */
603 * RelationGetTargetBlock
604 * Fetch relation's current insertion target block.
606 * Returns InvalidBlockNumber if there is no current target block. Note
607 * that the target block status is discarded on any smgr-level invalidation,
608 * so there's no need to re-open the smgr handle if it's not currently open.
610 #define RelationGetTargetBlock(relation) \
611 ( (relation)->rd_smgr != NULL ? (relation)->rd_smgr->smgr_targblock : InvalidBlockNumber )
614 * RelationSetTargetBlock
615 * Set relation's current insertion target block.
617 #define RelationSetTargetBlock(relation, targblock) \
619 RelationGetSmgr(relation)->smgr_targblock = (targblock); \
623 * RelationIsPermanent
624 * True if relation is permanent.
626 #define RelationIsPermanent(relation) \
627 ((relation)->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT)
631 * True if relation needs WAL.
633 * Returns false if wal_level = minimal and this relation is created or
634 * truncated in the current transaction. See "Skipping WAL for New
635 * RelFileLocator" in src/backend/access/transam/README.
637 #define RelationNeedsWAL(relation) \
638 (RelationIsPermanent(relation) && (XLogIsNeeded() || \
639 (relation->rd_createSubid == InvalidSubTransactionId && \
640 relation->rd_firstRelfilelocatorSubid == InvalidSubTransactionId)))
643 * RelationUsesLocalBuffers
644 * True if relation's pages are stored in local buffers.
646 #define RelationUsesLocalBuffers(relation) \
647 ((relation)->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
651 * If a rel is either temp or newly created in the current transaction,
652 * it can be assumed to be accessible only to the current backend.
653 * This is typically used to decide that we can skip acquiring locks.
655 * Beware of multiple eval of argument
657 #define RELATION_IS_LOCAL(relation) \
658 ((relation)->rd_islocaltemp || \
659 (relation)->rd_createSubid != InvalidSubTransactionId)
662 * RELATION_IS_OTHER_TEMP
663 * Test for a temporary relation that belongs to some other session.
665 * Beware of multiple eval of argument
667 #define RELATION_IS_OTHER_TEMP(relation) \
668 ((relation)->rd_rel->relpersistence == RELPERSISTENCE_TEMP && \
669 !(relation)->rd_islocaltemp)
673 * RelationIsScannable
674 * Currently can only be false for a materialized view which has not been
675 * populated by its query. This is likely to get more complicated later,
676 * so use a macro which looks like a function.
678 #define RelationIsScannable(relation) ((relation)->rd_rel->relispopulated)
681 * RelationIsPopulated
682 * Currently, we don't physically distinguish the "populated" and
683 * "scannable" properties of matviews, but that may change later.
684 * Hence, use the appropriate one of these macros in code tests.
686 #define RelationIsPopulated(relation) ((relation)->rd_rel->relispopulated)
689 * RelationIsAccessibleInLogicalDecoding
690 * True if we need to log enough information to have access via
693 #define RelationIsAccessibleInLogicalDecoding(relation) \
694 (XLogLogicalInfoActive() && \
695 RelationNeedsWAL(relation) && \
696 (IsCatalogRelation(relation) || RelationIsUsedAsCatalogTable(relation)))
699 * RelationIsLogicallyLogged
700 * True if we need to log enough information to extract the data from the
703 * We don't log information for unlogged tables (since they don't WAL log
704 * anyway), for foreign tables (since they don't WAL log, either),
705 * and for system tables (their content is hard to make sense of, and
706 * it would complicate decoding slightly for little gain). Note that we *do*
707 * log information for user defined catalog tables since they presumably are
708 * interesting to the user...
710 #define RelationIsLogicallyLogged(relation) \
711 (XLogLogicalInfoActive() && \
712 RelationNeedsWAL(relation) && \
713 (relation)->rd_rel->relkind != RELKIND_FOREIGN_TABLE && \
714 !IsCatalogRelation(relation))
716/* routines in utils/cache/relcache.c */
FormData_pg_class * Form_pg_class
FormData_pg_index * Form_pg_index
struct LockRelId LockRelId
void RelationDecrementReferenceCount(Relation rel)
@ VIEW_OPTION_CHECK_OPTION_NOT_SET
@ VIEW_OPTION_CHECK_OPTION_LOCAL
@ VIEW_OPTION_CHECK_OPTION_CASCADED
struct ForeignKeyCacheInfo ForeignKeyCacheInfo
static SMgrRelation RelationGetSmgr(Relation rel)
void RelationIncrementReferenceCount(Relation rel)
struct StdRdOptions StdRdOptions
struct ViewOptions ViewOptions
struct LockInfoData LockInfoData
struct AutoVacOpts AutoVacOpts
struct RelationData RelationData
static void RelationCloseSmgr(Relation relation)
@ STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO
@ STDRD_OPTION_VACUUM_INDEX_CLEANUP_OFF
@ STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON
SMgrRelation smgropen(RelFileLocator rlocator, ProcNumber backend)
void smgrpin(SMgrRelation reln)
void smgrunpin(SMgrRelation reln)
void smgrclose(SMgrRelation reln)
int multixact_freeze_max_age
float8 vacuum_scale_factor
float8 vacuum_ins_scale_factor
int multixact_freeze_table_age
int multixact_freeze_min_age
float8 analyze_scale_factor
AttrNumber conkey[INDEX_MAX_KEYS] pg_node_attr(array_size(nkeys))
AttrNumber confkey[INDEX_MAX_KEYS] pg_node_attr(array_size(nkeys))
Oid conpfeqop[INDEX_MAX_KEYS] pg_node_attr(array_size(nkeys))
pg_node_attr(no_equal, no_read, no_query_jumble) NodeTag type
MemoryContext rd_partkeycxt
const struct TableAmRoutine * rd_tableam
TransactionId rd_partdesc_nodetached_xmin
struct IndexAmRoutine * rd_indam
SubTransactionId rd_firstRelfilelocatorSubid
struct RowSecurityDesc * rd_rsdesc
PartitionDesc rd_partdesc
RegProcedure * rd_support
PartitionDesc rd_partdesc_nodetached
PublicationDesc * rd_pubdesc
struct FdwRoutine * rd_fdwroutine
struct HeapTupleData * rd_indextuple
MemoryContext rd_partcheckcxt
Bitmapset * rd_hotblockingattr
SubTransactionId rd_newRelfilelocatorSubid
SubTransactionId rd_createSubid
MemoryContext rd_indexcxt
RelFileLocator rd_locator
struct FmgrInfo * rd_supportinfo
SubTransactionId rd_droppedSubid
MemoryContext rd_rulescxt
Bitmapset * rd_summarizedattr
struct PgStat_TableStatus * pgstat_info
double vacuum_max_eager_freeze_failure_rate
StdRdOptIndexCleanup vacuum_index_cleanup
ViewOptCheckOption check_option