LCOV - code coverage report
Current view: top level - src/include/utils - rel.h (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 4 4 100.0 %
Date: 2021-12-09 03:08:47 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * rel.h
       4             :  *    POSTGRES relation descriptor (a/k/a relcache entry) definitions.
       5             :  *
       6             :  *
       7             :  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  * src/include/utils/rel.h
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #ifndef REL_H
      15             : #define REL_H
      16             : 
      17             : #include "access/tupdesc.h"
      18             : #include "access/xlog.h"
      19             : #include "catalog/pg_class.h"
      20             : #include "catalog/pg_index.h"
      21             : #include "catalog/pg_publication.h"
      22             : #include "nodes/bitmapset.h"
      23             : #include "partitioning/partdefs.h"
      24             : #include "rewrite/prs2lock.h"
      25             : #include "storage/block.h"
      26             : #include "storage/relfilenode.h"
      27             : #include "storage/smgr.h"
      28             : #include "utils/relcache.h"
      29             : #include "utils/reltrigger.h"
      30             : 
      31             : 
      32             : /*
      33             :  * LockRelId and LockInfo really belong to lmgr.h, but it's more convenient
      34             :  * to declare them here so we can have a LockInfoData field in a Relation.
      35             :  */
      36             : 
      37             : typedef struct LockRelId
      38             : {
      39             :     Oid         relId;          /* a relation identifier */
      40             :     Oid         dbId;           /* a database identifier */
      41             : } LockRelId;
      42             : 
      43             : typedef struct LockInfoData
      44             : {
      45             :     LockRelId   lockRelId;
      46             : } LockInfoData;
      47             : 
      48             : typedef LockInfoData *LockInfo;
      49             : 
      50             : /*
      51             :  * Here are the contents of a relation cache entry.
      52             :  */
      53             : 
      54             : typedef struct RelationData
      55             : {
      56             :     RelFileNode rd_node;        /* relation physical identifier */
      57             :     SMgrRelation rd_smgr;       /* cached file handle, or NULL */
      58             :     int         rd_refcnt;      /* reference count */
      59             :     BackendId   rd_backend;     /* owning backend id, if temporary relation */
      60             :     bool        rd_islocaltemp; /* rel is a temp rel of this session */
      61             :     bool        rd_isnailed;    /* rel is nailed in cache */
      62             :     bool        rd_isvalid;     /* relcache entry is valid */
      63             :     bool        rd_indexvalid;  /* is rd_indexlist valid? (also rd_pkindex and
      64             :                                  * rd_replidindex) */
      65             :     bool        rd_statvalid;   /* is rd_statlist valid? */
      66             : 
      67             :     /*----------
      68             :      * rd_createSubid is the ID of the highest subtransaction the rel has
      69             :      * survived into or zero if the rel or its rd_node was created before the
      70             :      * current top transaction.  (IndexStmt.oldNode leads to the case of a new
      71             :      * rel with an old rd_node.)  rd_firstRelfilenodeSubid is the ID of the
      72             :      * highest subtransaction an rd_node change has survived into or zero if
      73             :      * rd_node matches the value it had at the start of the current top
      74             :      * transaction.  (Rolling back the subtransaction that
      75             :      * rd_firstRelfilenodeSubid denotes would restore rd_node to the value it
      76             :      * had at the start of the current top transaction.  Rolling back any
      77             :      * lower subtransaction would not.)  Their accuracy is critical to
      78             :      * RelationNeedsWAL().
      79             :      *
      80             :      * rd_newRelfilenodeSubid is the ID of the highest subtransaction the
      81             :      * most-recent relfilenode change has survived into or zero if not changed
      82             :      * in the current transaction (or we have forgotten changing it).  This
      83             :      * field is accurate when non-zero, but it can be zero when a relation has
      84             :      * multiple new relfilenodes within a single transaction, with one of them
      85             :      * occurring in a subsequently aborted subtransaction, e.g.
      86             :      *      BEGIN;
      87             :      *      TRUNCATE t;
      88             :      *      SAVEPOINT save;
      89             :      *      TRUNCATE t;
      90             :      *      ROLLBACK TO save;
      91             :      *      -- rd_newRelfilenodeSubid is now forgotten
      92             :      *
      93             :      * If every rd_*Subid field is zero, they are read-only outside
      94             :      * relcache.c.  Files that trigger rd_node changes by updating
      95             :      * pg_class.reltablespace and/or pg_class.relfilenode call
      96             :      * RelationAssumeNewRelfilenode() to update rd_*Subid.
      97             :      *
      98             :      * rd_droppedSubid is the ID of the highest subtransaction that a drop of
      99             :      * the rel has survived into.  In entries visible outside relcache.c, this
     100             :      * is always zero.
     101             :      */
     102             :     SubTransactionId rd_createSubid;    /* rel was created in current xact */
     103             :     SubTransactionId rd_newRelfilenodeSubid;    /* highest subxact changing
     104             :                                                  * rd_node to current value */
     105             :     SubTransactionId rd_firstRelfilenodeSubid;  /* highest subxact changing
     106             :                                                  * rd_node to any value */
     107             :     SubTransactionId rd_droppedSubid;   /* dropped with another Subid set */
     108             : 
     109             :     Form_pg_class rd_rel;       /* RELATION tuple */
     110             :     TupleDesc   rd_att;         /* tuple descriptor */
     111             :     Oid         rd_id;          /* relation's object id */
     112             :     LockInfoData rd_lockInfo;   /* lock mgr's info for locking relation */
     113             :     RuleLock   *rd_rules;       /* rewrite rules */
     114             :     MemoryContext rd_rulescxt;  /* private memory cxt for rd_rules, if any */
     115             :     TriggerDesc *trigdesc;      /* Trigger info, or NULL if rel has none */
     116             :     /* use "struct" here to avoid needing to include rowsecurity.h: */
     117             :     struct RowSecurityDesc *rd_rsdesc;  /* row security policies, or NULL */
     118             : 
     119             :     /* data managed by RelationGetFKeyList: */
     120             :     List       *rd_fkeylist;    /* list of ForeignKeyCacheInfo (see below) */
     121             :     bool        rd_fkeyvalid;   /* true if list has been computed */
     122             : 
     123             :     /* data managed by RelationGetPartitionKey: */
     124             :     PartitionKey rd_partkey;    /* partition key, or NULL */
     125             :     MemoryContext rd_partkeycxt;    /* private context for rd_partkey, if any */
     126             : 
     127             :     /* data managed by RelationGetPartitionDesc: */
     128             :     PartitionDesc rd_partdesc;  /* partition descriptor, or NULL */
     129             :     MemoryContext rd_pdcxt;     /* private context for rd_partdesc, if any */
     130             : 
     131             :     /* Same as above, for partdescs that omit detached partitions */
     132             :     PartitionDesc rd_partdesc_nodetached;   /* partdesc w/o detached parts */
     133             :     MemoryContext rd_pddcxt;    /* for rd_partdesc_nodetached, if any */
     134             : 
     135             :     /*
     136             :      * pg_inherits.xmin of the partition that was excluded in
     137             :      * rd_partdesc_nodetached.  This informs a future user of that partdesc:
     138             :      * if this value is not in progress for the active snapshot, then the
     139             :      * partdesc can be used, otherwise they have to build a new one.  (This
     140             :      * matches what find_inheritance_children_extended would do).
     141             :      */
     142             :     TransactionId rd_partdesc_nodetached_xmin;
     143             : 
     144             :     /* data managed by RelationGetPartitionQual: */
     145             :     List       *rd_partcheck;   /* partition CHECK quals */
     146             :     bool        rd_partcheckvalid;  /* true if list has been computed */
     147             :     MemoryContext rd_partcheckcxt;  /* private cxt for rd_partcheck, if any */
     148             : 
     149             :     /* data managed by RelationGetIndexList: */
     150             :     List       *rd_indexlist;   /* list of OIDs of indexes on relation */
     151             :     Oid         rd_pkindex;     /* OID of primary key, if any */
     152             :     Oid         rd_replidindex; /* OID of replica identity index, if any */
     153             : 
     154             :     /* data managed by RelationGetStatExtList: */
     155             :     List       *rd_statlist;    /* list of OIDs of extended stats */
     156             : 
     157             :     /* data managed by RelationGetIndexAttrBitmap: */
     158             :     bool        rd_attrsvalid;  /* are bitmaps of attrs valid? */
     159             :     Bitmapset  *rd_keyattr;     /* cols that can be ref'd by foreign keys */
     160             :     Bitmapset  *rd_pkattr;      /* cols included in primary key */
     161             :     Bitmapset  *rd_idattr;      /* included in replica identity index */
     162             :     Bitmapset  *rd_hotblockingattr; /* cols blocking HOT update */
     163             : 
     164             :     PublicationActions *rd_pubactions;  /* publication actions */
     165             : 
     166             :     /*
     167             :      * rd_options is set whenever rd_rel is loaded into the relcache entry.
     168             :      * Note that you can NOT look into rd_rel for this data.  NULL means "use
     169             :      * defaults".
     170             :      */
     171             :     bytea      *rd_options;     /* parsed pg_class.reloptions */
     172             : 
     173             :     /*
     174             :      * Oid of the handler for this relation. For an index this is a function
     175             :      * returning IndexAmRoutine, for table like relations a function returning
     176             :      * TableAmRoutine.  This is stored separately from rd_indam, rd_tableam as
     177             :      * its lookup requires syscache access, but during relcache bootstrap we
     178             :      * need to be able to initialize rd_tableam without syscache lookups.
     179             :      */
     180             :     Oid         rd_amhandler;   /* OID of index AM's handler function */
     181             : 
     182             :     /*
     183             :      * Table access method.
     184             :      */
     185             :     const struct TableAmRoutine *rd_tableam;
     186             : 
     187             :     /* These are non-NULL only for an index relation: */
     188             :     Form_pg_index rd_index;     /* pg_index tuple describing this index */
     189             :     /* use "struct" here to avoid needing to include htup.h: */
     190             :     struct HeapTupleData *rd_indextuple;    /* all of pg_index tuple */
     191             : 
     192             :     /*
     193             :      * index access support info (used only for an index relation)
     194             :      *
     195             :      * Note: only default support procs for each opclass are cached, namely
     196             :      * those with lefttype and righttype equal to the opclass's opcintype. The
     197             :      * arrays are indexed by support function number, which is a sufficient
     198             :      * identifier given that restriction.
     199             :      */
     200             :     MemoryContext rd_indexcxt;  /* private memory cxt for this stuff */
     201             :     /* use "struct" here to avoid needing to include amapi.h: */
     202             :     struct IndexAmRoutine *rd_indam;    /* index AM's API struct */
     203             :     Oid        *rd_opfamily;    /* OIDs of op families for each index col */
     204             :     Oid        *rd_opcintype;   /* OIDs of opclass declared input data types */
     205             :     RegProcedure *rd_support;   /* OIDs of support procedures */
     206             :     struct FmgrInfo *rd_supportinfo;    /* lookup info for support procedures */
     207             :     int16      *rd_indoption;   /* per-column AM-specific flags */
     208             :     List       *rd_indexprs;    /* index expression trees, if any */
     209             :     List       *rd_indpred;     /* index predicate tree, if any */
     210             :     Oid        *rd_exclops;     /* OIDs of exclusion operators, if any */
     211             :     Oid        *rd_exclprocs;   /* OIDs of exclusion ops' procs, if any */
     212             :     uint16     *rd_exclstrats;  /* exclusion ops' strategy numbers, if any */
     213             :     Oid        *rd_indcollation;    /* OIDs of index collations */
     214             :     bytea     **rd_opcoptions;  /* parsed opclass-specific options */
     215             : 
     216             :     /*
     217             :      * rd_amcache is available for index and table AMs to cache private data
     218             :      * about the relation.  This must be just a cache since it may get reset
     219             :      * at any time (in particular, it will get reset by a relcache inval
     220             :      * message for the relation).  If used, it must point to a single memory
     221             :      * chunk palloc'd in CacheMemoryContext, or in rd_indexcxt for an index
     222             :      * relation.  A relcache reset will include freeing that chunk and setting
     223             :      * rd_amcache = NULL.
     224             :      */
     225             :     void       *rd_amcache;     /* available for use by index/table AM */
     226             : 
     227             :     /*
     228             :      * foreign-table support
     229             :      *
     230             :      * rd_fdwroutine must point to a single memory chunk palloc'd in
     231             :      * CacheMemoryContext.  It will be freed and reset to NULL on a relcache
     232             :      * reset.
     233             :      */
     234             : 
     235             :     /* use "struct" here to avoid needing to include fdwapi.h: */
     236             :     struct FdwRoutine *rd_fdwroutine;   /* cached function pointers, or NULL */
     237             : 
     238             :     /*
     239             :      * Hack for CLUSTER, rewriting ALTER TABLE, etc: when writing a new
     240             :      * version of a table, we need to make any toast pointers inserted into it
     241             :      * have the existing toast table's OID, not the OID of the transient toast
     242             :      * table.  If rd_toastoid isn't InvalidOid, it is the OID to place in
     243             :      * toast pointers inserted into this rel.  (Note it's set on the new
     244             :      * version of the main heap, not the toast table itself.)  This also
     245             :      * causes toast_save_datum() to try to preserve toast value OIDs.
     246             :      */
     247             :     Oid         rd_toastoid;    /* Real TOAST table's OID, or InvalidOid */
     248             : 
     249             :     /* use "struct" here to avoid needing to include pgstat.h: */
     250             :     struct PgStat_TableStatus *pgstat_info; /* statistics collection area */
     251             : } RelationData;
     252             : 
     253             : 
     254             : /*
     255             :  * ForeignKeyCacheInfo
     256             :  *      Information the relcache can cache about foreign key constraints
     257             :  *
     258             :  * This is basically just an image of relevant columns from pg_constraint.
     259             :  * We make it a subclass of Node so that copyObject() can be used on a list
     260             :  * of these, but we also ensure it is a "flat" object without substructure,
     261             :  * so that list_free_deep() is sufficient to free such a list.
     262             :  * The per-FK-column arrays can be fixed-size because we allow at most
     263             :  * INDEX_MAX_KEYS columns in a foreign key constraint.
     264             :  *
     265             :  * Currently, we mostly cache fields of interest to the planner, but the set
     266             :  * of fields has already grown the constraint OID for other uses.
     267             :  */
     268             : typedef struct ForeignKeyCacheInfo
     269             : {
     270             :     NodeTag     type;
     271             :     Oid         conoid;         /* oid of the constraint itself */
     272             :     Oid         conrelid;       /* relation constrained by the foreign key */
     273             :     Oid         confrelid;      /* relation referenced by the foreign key */
     274             :     int         nkeys;          /* number of columns in the foreign key */
     275             :     /* these arrays each have nkeys valid entries: */
     276             :     AttrNumber  conkey[INDEX_MAX_KEYS]; /* cols in referencing table */
     277             :     AttrNumber  confkey[INDEX_MAX_KEYS];    /* cols in referenced table */
     278             :     Oid         conpfeqop[INDEX_MAX_KEYS];  /* PK = FK operator OIDs */
     279             : } ForeignKeyCacheInfo;
     280             : 
     281             : 
     282             : /*
     283             :  * StdRdOptions
     284             :  *      Standard contents of rd_options for heaps.
     285             :  *
     286             :  * RelationGetFillFactor() and RelationGetTargetPageFreeSpace() can only
     287             :  * be applied to relations that use this format or a superset for
     288             :  * private options data.
     289             :  */
     290             :  /* autovacuum-related reloptions. */
     291             : typedef struct AutoVacOpts
     292             : {
     293             :     bool        enabled;
     294             :     int         vacuum_threshold;
     295             :     int         vacuum_ins_threshold;
     296             :     int         analyze_threshold;
     297             :     int         vacuum_cost_limit;
     298             :     int         freeze_min_age;
     299             :     int         freeze_max_age;
     300             :     int         freeze_table_age;
     301             :     int         multixact_freeze_min_age;
     302             :     int         multixact_freeze_max_age;
     303             :     int         multixact_freeze_table_age;
     304             :     int         log_min_duration;
     305             :     float8      vacuum_cost_delay;
     306             :     float8      vacuum_scale_factor;
     307             :     float8      vacuum_ins_scale_factor;
     308             :     float8      analyze_scale_factor;
     309             : } AutoVacOpts;
     310             : 
     311             : /* StdRdOptions->vacuum_index_cleanup values */
     312             : typedef enum StdRdOptIndexCleanup
     313             : {
     314             :     STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO = 0,
     315             :     STDRD_OPTION_VACUUM_INDEX_CLEANUP_OFF,
     316             :     STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON
     317             : } StdRdOptIndexCleanup;
     318             : 
     319             : typedef struct StdRdOptions
     320             : {
     321             :     int32       vl_len_;        /* varlena header (do not touch directly!) */
     322             :     int         fillfactor;     /* page fill factor in percent (0..100) */
     323             :     /* fraction of newly inserted tuples prior to trigger index cleanup */
     324             :     int         toast_tuple_target; /* target for tuple toasting */
     325             :     AutoVacOpts autovacuum;     /* autovacuum-related options */
     326             :     bool        user_catalog_table; /* use as an additional catalog relation */
     327             :     int         parallel_workers;   /* max number of parallel workers */
     328             :     StdRdOptIndexCleanup vacuum_index_cleanup;  /* controls index vacuuming */
     329             :     bool        vacuum_truncate;    /* enables vacuum to truncate a relation */
     330             : } StdRdOptions;
     331             : 
     332             : #define HEAP_MIN_FILLFACTOR         10
     333             : #define HEAP_DEFAULT_FILLFACTOR     100
     334             : 
     335             : /*
     336             :  * RelationGetToastTupleTarget
     337             :  *      Returns the relation's toast_tuple_target.  Note multiple eval of argument!
     338             :  */
     339             : #define RelationGetToastTupleTarget(relation, defaulttarg) \
     340             :     ((relation)->rd_options ? \
     341             :      ((StdRdOptions *) (relation)->rd_options)->toast_tuple_target : (defaulttarg))
     342             : 
     343             : /*
     344             :  * RelationGetFillFactor
     345             :  *      Returns the relation's fillfactor.  Note multiple eval of argument!
     346             :  */
     347             : #define RelationGetFillFactor(relation, defaultff) \
     348             :     ((relation)->rd_options ? \
     349             :      ((StdRdOptions *) (relation)->rd_options)->fillfactor : (defaultff))
     350             : 
     351             : /*
     352             :  * RelationGetTargetPageUsage
     353             :  *      Returns the relation's desired space usage per page in bytes.
     354             :  */
     355             : #define RelationGetTargetPageUsage(relation, defaultff) \
     356             :     (BLCKSZ * RelationGetFillFactor(relation, defaultff) / 100)
     357             : 
     358             : /*
     359             :  * RelationGetTargetPageFreeSpace
     360             :  *      Returns the relation's desired freespace per page in bytes.
     361             :  */
     362             : #define RelationGetTargetPageFreeSpace(relation, defaultff) \
     363             :     (BLCKSZ * (100 - RelationGetFillFactor(relation, defaultff)) / 100)
     364             : 
     365             : /*
     366             :  * RelationIsUsedAsCatalogTable
     367             :  *      Returns whether the relation should be treated as a catalog table
     368             :  *      from the pov of logical decoding.  Note multiple eval of argument!
     369             :  */
     370             : #define RelationIsUsedAsCatalogTable(relation)  \
     371             :     ((relation)->rd_options && \
     372             :      ((relation)->rd_rel->relkind == RELKIND_RELATION || \
     373             :       (relation)->rd_rel->relkind == RELKIND_MATVIEW) ? \
     374             :      ((StdRdOptions *) (relation)->rd_options)->user_catalog_table : false)
     375             : 
     376             : /*
     377             :  * RelationGetParallelWorkers
     378             :  *      Returns the relation's parallel_workers reloption setting.
     379             :  *      Note multiple eval of argument!
     380             :  */
     381             : #define RelationGetParallelWorkers(relation, defaultpw) \
     382             :     ((relation)->rd_options ? \
     383             :      ((StdRdOptions *) (relation)->rd_options)->parallel_workers : (defaultpw))
     384             : 
     385             : /* ViewOptions->check_option values */
     386             : typedef enum ViewOptCheckOption
     387             : {
     388             :     VIEW_OPTION_CHECK_OPTION_NOT_SET,
     389             :     VIEW_OPTION_CHECK_OPTION_LOCAL,
     390             :     VIEW_OPTION_CHECK_OPTION_CASCADED
     391             : } ViewOptCheckOption;
     392             : 
     393             : /*
     394             :  * ViewOptions
     395             :  *      Contents of rd_options for views
     396             :  */
     397             : typedef struct ViewOptions
     398             : {
     399             :     int32       vl_len_;        /* varlena header (do not touch directly!) */
     400             :     bool        security_barrier;
     401             :     ViewOptCheckOption check_option;
     402             : } ViewOptions;
     403             : 
     404             : /*
     405             :  * RelationIsSecurityView
     406             :  *      Returns whether the relation is security view, or not.  Note multiple
     407             :  *      eval of argument!
     408             :  */
     409             : #define RelationIsSecurityView(relation)                                    \
     410             :     (AssertMacro(relation->rd_rel->relkind == RELKIND_VIEW),              \
     411             :      (relation)->rd_options ?                                                \
     412             :       ((ViewOptions *) (relation)->rd_options)->security_barrier : false)
     413             : 
     414             : /*
     415             :  * RelationHasCheckOption
     416             :  *      Returns true if the relation is a view defined with either the local
     417             :  *      or the cascaded check option.  Note multiple eval of argument!
     418             :  */
     419             : #define RelationHasCheckOption(relation)                                    \
     420             :     (AssertMacro(relation->rd_rel->relkind == RELKIND_VIEW),              \
     421             :      (relation)->rd_options &&                                               \
     422             :      ((ViewOptions *) (relation)->rd_options)->check_option !=                \
     423             :      VIEW_OPTION_CHECK_OPTION_NOT_SET)
     424             : 
     425             : /*
     426             :  * RelationHasLocalCheckOption
     427             :  *      Returns true if the relation is a view defined with the local check
     428             :  *      option.  Note multiple eval of argument!
     429             :  */
     430             : #define RelationHasLocalCheckOption(relation)                               \
     431             :     (AssertMacro(relation->rd_rel->relkind == RELKIND_VIEW),              \
     432             :      (relation)->rd_options &&                                               \
     433             :      ((ViewOptions *) (relation)->rd_options)->check_option ==                \
     434             :      VIEW_OPTION_CHECK_OPTION_LOCAL)
     435             : 
     436             : /*
     437             :  * RelationHasCascadedCheckOption
     438             :  *      Returns true if the relation is a view defined with the cascaded check
     439             :  *      option.  Note multiple eval of argument!
     440             :  */
     441             : #define RelationHasCascadedCheckOption(relation)                            \
     442             :     (AssertMacro(relation->rd_rel->relkind == RELKIND_VIEW),              \
     443             :      (relation)->rd_options &&                                               \
     444             :      ((ViewOptions *) (relation)->rd_options)->check_option ==                \
     445             :       VIEW_OPTION_CHECK_OPTION_CASCADED)
     446             : 
     447             : /*
     448             :  * RelationIsValid
     449             :  *      True iff relation descriptor is valid.
     450             :  */
     451             : #define RelationIsValid(relation) PointerIsValid(relation)
     452             : 
     453             : #define InvalidRelation ((Relation) NULL)
     454             : 
     455             : /*
     456             :  * RelationHasReferenceCountZero
     457             :  *      True iff relation reference count is zero.
     458             :  *
     459             :  * Note:
     460             :  *      Assumes relation descriptor is valid.
     461             :  */
     462             : #define RelationHasReferenceCountZero(relation) \
     463             :         ((bool)((relation)->rd_refcnt == 0))
     464             : 
     465             : /*
     466             :  * RelationGetForm
     467             :  *      Returns pg_class tuple for a relation.
     468             :  *
     469             :  * Note:
     470             :  *      Assumes relation descriptor is valid.
     471             :  */
     472             : #define RelationGetForm(relation) ((relation)->rd_rel)
     473             : 
     474             : /*
     475             :  * RelationGetRelid
     476             :  *      Returns the OID of the relation
     477             :  */
     478             : #define RelationGetRelid(relation) ((relation)->rd_id)
     479             : 
     480             : /*
     481             :  * RelationGetNumberOfAttributes
     482             :  *      Returns the total number of attributes in a relation.
     483             :  */
     484             : #define RelationGetNumberOfAttributes(relation) ((relation)->rd_rel->relnatts)
     485             : 
     486             : /*
     487             :  * IndexRelationGetNumberOfAttributes
     488             :  *      Returns the number of attributes in an index.
     489             :  */
     490             : #define IndexRelationGetNumberOfAttributes(relation) \
     491             :         ((relation)->rd_index->indnatts)
     492             : 
     493             : /*
     494             :  * IndexRelationGetNumberOfKeyAttributes
     495             :  *      Returns the number of key attributes in an index.
     496             :  */
     497             : #define IndexRelationGetNumberOfKeyAttributes(relation) \
     498             :         ((relation)->rd_index->indnkeyatts)
     499             : 
     500             : /*
     501             :  * RelationGetDescr
     502             :  *      Returns tuple descriptor for a relation.
     503             :  */
     504             : #define RelationGetDescr(relation) ((relation)->rd_att)
     505             : 
     506             : /*
     507             :  * RelationGetRelationName
     508             :  *      Returns the rel's name.
     509             :  *
     510             :  * Note that the name is only unique within the containing namespace.
     511             :  */
     512             : #define RelationGetRelationName(relation) \
     513             :     (NameStr((relation)->rd_rel->relname))
     514             : 
     515             : /*
     516             :  * RelationGetNamespace
     517             :  *      Returns the rel's namespace OID.
     518             :  */
     519             : #define RelationGetNamespace(relation) \
     520             :     ((relation)->rd_rel->relnamespace)
     521             : 
     522             : /*
     523             :  * RelationIsMapped
     524             :  *      True if the relation uses the relfilenode map.  Note multiple eval
     525             :  *      of argument!
     526             :  */
     527             : #define RelationIsMapped(relation) \
     528             :     (RELKIND_HAS_STORAGE((relation)->rd_rel->relkind) && \
     529             :      ((relation)->rd_rel->relfilenode == InvalidOid))
     530             : 
     531             : /*
     532             :  * RelationGetSmgr
     533             :  *      Returns smgr file handle for a relation, opening it if needed.
     534             :  *
     535             :  * Very little code is authorized to touch rel->rd_smgr directly.  Instead
     536             :  * use this function to fetch its value.
     537             :  *
     538             :  * Note: since a relcache flush can cause the file handle to be closed again,
     539             :  * it's unwise to hold onto the pointer returned by this function for any
     540             :  * long period.  Recommended practice is to just re-execute RelationGetSmgr
     541             :  * each time you need to access the SMgrRelation.  It's quite cheap in
     542             :  * comparison to whatever an smgr function is going to do.
     543             :  */
     544             : static inline SMgrRelation
     545   120909734 : RelationGetSmgr(Relation rel)
     546             : {
     547   120909734 :     if (unlikely(rel->rd_smgr == NULL))
     548     1555656 :         smgrsetowner(&(rel->rd_smgr), smgropen(rel->rd_node, rel->rd_backend));
     549   120909734 :     return rel->rd_smgr;
     550             : }
     551             : 
     552             : /*
     553             :  * RelationCloseSmgr
     554             :  *      Close the relation at the smgr level, if not already done.
     555             :  *
     556             :  * Note: smgrclose should unhook from owner pointer, hence the Assert.
     557             :  */
     558             : #define RelationCloseSmgr(relation) \
     559             :     do { \
     560             :         if ((relation)->rd_smgr != NULL) \
     561             :         { \
     562             :             smgrclose((relation)->rd_smgr); \
     563             :             Assert((relation)->rd_smgr == NULL); \
     564             :         } \
     565             :     } while (0)
     566             : 
     567             : /*
     568             :  * RelationGetTargetBlock
     569             :  *      Fetch relation's current insertion target block.
     570             :  *
     571             :  * Returns InvalidBlockNumber if there is no current target block.  Note
     572             :  * that the target block status is discarded on any smgr-level invalidation,
     573             :  * so there's no need to re-open the smgr handle if it's not currently open.
     574             :  */
     575             : #define RelationGetTargetBlock(relation) \
     576             :     ( (relation)->rd_smgr != NULL ? (relation)->rd_smgr->smgr_targblock : InvalidBlockNumber )
     577             : 
     578             : /*
     579             :  * RelationSetTargetBlock
     580             :  *      Set relation's current insertion target block.
     581             :  */
     582             : #define RelationSetTargetBlock(relation, targblock) \
     583             :     do { \
     584             :         RelationGetSmgr(relation)->smgr_targblock = (targblock); \
     585             :     } while (0)
     586             : 
     587             : /*
     588             :  * RelationIsPermanent
     589             :  *      True if relation is permanent.
     590             :  */
     591             : #define RelationIsPermanent(relation) \
     592             :     ((relation)->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT)
     593             : 
     594             : /*
     595             :  * RelationNeedsWAL
     596             :  *      True if relation needs WAL.
     597             :  *
     598             :  * Returns false if wal_level = minimal and this relation is created or
     599             :  * truncated in the current transaction.  See "Skipping WAL for New
     600             :  * RelFileNode" in src/backend/access/transam/README.
     601             :  */
     602             : #define RelationNeedsWAL(relation)                                      \
     603             :     (RelationIsPermanent(relation) && (XLogIsNeeded() ||                \
     604             :       (relation->rd_createSubid == InvalidSubTransactionId &&            \
     605             :        relation->rd_firstRelfilenodeSubid == InvalidSubTransactionId)))
     606             : 
     607             : /*
     608             :  * RelationUsesLocalBuffers
     609             :  *      True if relation's pages are stored in local buffers.
     610             :  */
     611             : #define RelationUsesLocalBuffers(relation) \
     612             :     ((relation)->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
     613             : 
     614             : /*
     615             :  * RELATION_IS_LOCAL
     616             :  *      If a rel is either temp or newly created in the current transaction,
     617             :  *      it can be assumed to be accessible only to the current backend.
     618             :  *      This is typically used to decide that we can skip acquiring locks.
     619             :  *
     620             :  * Beware of multiple eval of argument
     621             :  */
     622             : #define RELATION_IS_LOCAL(relation) \
     623             :     ((relation)->rd_islocaltemp || \
     624             :      (relation)->rd_createSubid != InvalidSubTransactionId)
     625             : 
     626             : /*
     627             :  * RELATION_IS_OTHER_TEMP
     628             :  *      Test for a temporary relation that belongs to some other session.
     629             :  *
     630             :  * Beware of multiple eval of argument
     631             :  */
     632             : #define RELATION_IS_OTHER_TEMP(relation) \
     633             :     ((relation)->rd_rel->relpersistence == RELPERSISTENCE_TEMP && \
     634             :      !(relation)->rd_islocaltemp)
     635             : 
     636             : 
     637             : /*
     638             :  * RelationIsScannable
     639             :  *      Currently can only be false for a materialized view which has not been
     640             :  *      populated by its query.  This is likely to get more complicated later,
     641             :  *      so use a macro which looks like a function.
     642             :  */
     643             : #define RelationIsScannable(relation) ((relation)->rd_rel->relispopulated)
     644             : 
     645             : /*
     646             :  * RelationIsPopulated
     647             :  *      Currently, we don't physically distinguish the "populated" and
     648             :  *      "scannable" properties of matviews, but that may change later.
     649             :  *      Hence, use the appropriate one of these macros in code tests.
     650             :  */
     651             : #define RelationIsPopulated(relation) ((relation)->rd_rel->relispopulated)
     652             : 
     653             : /*
     654             :  * RelationIsAccessibleInLogicalDecoding
     655             :  *      True if we need to log enough information to have access via
     656             :  *      decoding snapshot.
     657             :  */
     658             : #define RelationIsAccessibleInLogicalDecoding(relation) \
     659             :     (XLogLogicalInfoActive() && \
     660             :      RelationNeedsWAL(relation) && \
     661             :      (IsCatalogRelation(relation) || RelationIsUsedAsCatalogTable(relation)))
     662             : 
     663             : /*
     664             :  * RelationIsLogicallyLogged
     665             :  *      True if we need to log enough information to extract the data from the
     666             :  *      WAL stream.
     667             :  *
     668             :  * We don't log information for unlogged tables (since they don't WAL log
     669             :  * anyway), for foreign tables (since they don't WAL log, either),
     670             :  * and for system tables (their content is hard to make sense of, and
     671             :  * it would complicate decoding slightly for little gain). Note that we *do*
     672             :  * log information for user defined catalog tables since they presumably are
     673             :  * interesting to the user...
     674             :  */
     675             : #define RelationIsLogicallyLogged(relation) \
     676             :     (XLogLogicalInfoActive() && \
     677             :      RelationNeedsWAL(relation) && \
     678             :      (relation)->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&  \
     679             :      !IsCatalogRelation(relation))
     680             : 
     681             : /* routines in utils/cache/relcache.c */
     682             : extern void RelationIncrementReferenceCount(Relation rel);
     683             : extern void RelationDecrementReferenceCount(Relation rel);
     684             : 
     685             : #endif                          /* REL_H */

Generated by: LCOV version 1.14