LCOV - code coverage report
Current view: top level - src/backend/utils/cache - relcache.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 1774 1940 91.4 %
Date: 2021-12-09 04:09:06 Functions: 77 77 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * relcache.c
       4             :  *    POSTGRES relation descriptor cache code
       5             :  *
       6             :  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/utils/cache/relcache.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : /*
      16             :  * INTERFACE ROUTINES
      17             :  *      RelationCacheInitialize         - initialize relcache (to empty)
      18             :  *      RelationCacheInitializePhase2   - initialize shared-catalog entries
      19             :  *      RelationCacheInitializePhase3   - finish initializing relcache
      20             :  *      RelationIdGetRelation           - get a reldesc by relation id
      21             :  *      RelationClose                   - close an open relation
      22             :  *
      23             :  * NOTES
      24             :  *      The following code contains many undocumented hacks.  Please be
      25             :  *      careful....
      26             :  */
      27             : #include "postgres.h"
      28             : 
      29             : #include <sys/file.h>
      30             : #include <fcntl.h>
      31             : #include <unistd.h>
      32             : 
      33             : #include "access/htup_details.h"
      34             : #include "access/multixact.h"
      35             : #include "access/nbtree.h"
      36             : #include "access/parallel.h"
      37             : #include "access/reloptions.h"
      38             : #include "access/sysattr.h"
      39             : #include "access/table.h"
      40             : #include "access/tableam.h"
      41             : #include "access/tupdesc_details.h"
      42             : #include "access/xact.h"
      43             : #include "access/xlog.h"
      44             : #include "catalog/catalog.h"
      45             : #include "catalog/indexing.h"
      46             : #include "catalog/namespace.h"
      47             : #include "catalog/partition.h"
      48             : #include "catalog/pg_am.h"
      49             : #include "catalog/pg_amproc.h"
      50             : #include "catalog/pg_attrdef.h"
      51             : #include "catalog/pg_auth_members.h"
      52             : #include "catalog/pg_authid.h"
      53             : #include "catalog/pg_constraint.h"
      54             : #include "catalog/pg_database.h"
      55             : #include "catalog/pg_namespace.h"
      56             : #include "catalog/pg_opclass.h"
      57             : #include "catalog/pg_proc.h"
      58             : #include "catalog/pg_publication.h"
      59             : #include "catalog/pg_rewrite.h"
      60             : #include "catalog/pg_shseclabel.h"
      61             : #include "catalog/pg_statistic_ext.h"
      62             : #include "catalog/pg_subscription.h"
      63             : #include "catalog/pg_tablespace.h"
      64             : #include "catalog/pg_trigger.h"
      65             : #include "catalog/pg_type.h"
      66             : #include "catalog/schemapg.h"
      67             : #include "catalog/storage.h"
      68             : #include "commands/policy.h"
      69             : #include "commands/trigger.h"
      70             : #include "miscadmin.h"
      71             : #include "nodes/makefuncs.h"
      72             : #include "nodes/nodeFuncs.h"
      73             : #include "optimizer/optimizer.h"
      74             : #include "rewrite/rewriteDefine.h"
      75             : #include "rewrite/rowsecurity.h"
      76             : #include "storage/lmgr.h"
      77             : #include "storage/smgr.h"
      78             : #include "utils/array.h"
      79             : #include "utils/builtins.h"
      80             : #include "utils/datum.h"
      81             : #include "utils/fmgroids.h"
      82             : #include "utils/inval.h"
      83             : #include "utils/lsyscache.h"
      84             : #include "utils/memutils.h"
      85             : #include "utils/relmapper.h"
      86             : #include "utils/resowner_private.h"
      87             : #include "utils/snapmgr.h"
      88             : #include "utils/syscache.h"
      89             : 
      90             : #define RELCACHE_INIT_FILEMAGIC     0x573266    /* version ID value */
      91             : 
      92             : /*
      93             :  * Whether to bother checking if relation cache memory needs to be freed
      94             :  * eagerly.  See also RelationBuildDesc() and pg_config_manual.h.
      95             :  */
      96             : #if defined(RECOVER_RELATION_BUILD_MEMORY) && (RECOVER_RELATION_BUILD_MEMORY != 0)
      97             : #define MAYBE_RECOVER_RELATION_BUILD_MEMORY 1
      98             : #else
      99             : #define RECOVER_RELATION_BUILD_MEMORY 0
     100             : #ifdef DISCARD_CACHES_ENABLED
     101             : #define MAYBE_RECOVER_RELATION_BUILD_MEMORY 1
     102             : #endif
     103             : #endif
     104             : 
     105             : /*
     106             :  *      hardcoded tuple descriptors, contents generated by genbki.pl
     107             :  */
     108             : static const FormData_pg_attribute Desc_pg_class[Natts_pg_class] = {Schema_pg_class};
     109             : static const FormData_pg_attribute Desc_pg_attribute[Natts_pg_attribute] = {Schema_pg_attribute};
     110             : static const FormData_pg_attribute Desc_pg_proc[Natts_pg_proc] = {Schema_pg_proc};
     111             : static const FormData_pg_attribute Desc_pg_type[Natts_pg_type] = {Schema_pg_type};
     112             : static const FormData_pg_attribute Desc_pg_database[Natts_pg_database] = {Schema_pg_database};
     113             : static const FormData_pg_attribute Desc_pg_authid[Natts_pg_authid] = {Schema_pg_authid};
     114             : static const FormData_pg_attribute Desc_pg_auth_members[Natts_pg_auth_members] = {Schema_pg_auth_members};
     115             : static const FormData_pg_attribute Desc_pg_index[Natts_pg_index] = {Schema_pg_index};
     116             : static const FormData_pg_attribute Desc_pg_shseclabel[Natts_pg_shseclabel] = {Schema_pg_shseclabel};
     117             : static const FormData_pg_attribute Desc_pg_subscription[Natts_pg_subscription] = {Schema_pg_subscription};
     118             : 
     119             : /*
     120             :  *      Hash tables that index the relation cache
     121             :  *
     122             :  *      We used to index the cache by both name and OID, but now there
     123             :  *      is only an index by OID.
     124             :  */
     125             : typedef struct relidcacheent
     126             : {
     127             :     Oid         reloid;
     128             :     Relation    reldesc;
     129             : } RelIdCacheEnt;
     130             : 
     131             : static HTAB *RelationIdCache;
     132             : 
     133             : /*
     134             :  * This flag is false until we have prepared the critical relcache entries
     135             :  * that are needed to do indexscans on the tables read by relcache building.
     136             :  */
     137             : bool        criticalRelcachesBuilt = false;
     138             : 
     139             : /*
     140             :  * This flag is false until we have prepared the critical relcache entries
     141             :  * for shared catalogs (which are the tables needed for login).
     142             :  */
     143             : bool        criticalSharedRelcachesBuilt = false;
     144             : 
     145             : /*
     146             :  * This counter counts relcache inval events received since backend startup
     147             :  * (but only for rels that are actually in cache).  Presently, we use it only
     148             :  * to detect whether data about to be written by write_relcache_init_file()
     149             :  * might already be obsolete.
     150             :  */
     151             : static long relcacheInvalsReceived = 0L;
     152             : 
     153             : /*
     154             :  * in_progress_list is a stack of ongoing RelationBuildDesc() calls.  CREATE
     155             :  * INDEX CONCURRENTLY makes catalog changes under ShareUpdateExclusiveLock.
     156             :  * It critically relies on each backend absorbing those changes no later than
     157             :  * next transaction start.  Hence, RelationBuildDesc() loops until it finishes
     158             :  * without accepting a relevant invalidation.  (Most invalidation consumers
     159             :  * don't do this.)
     160             :  */
     161             : typedef struct inprogressent
     162             : {
     163             :     Oid         reloid;         /* OID of relation being built */
     164             :     bool        invalidated;    /* whether an invalidation arrived for it */
     165             : } InProgressEnt;
     166             : 
     167             : static InProgressEnt *in_progress_list;
     168             : static int  in_progress_list_len;
     169             : static int  in_progress_list_maxlen;
     170             : 
     171             : /*
     172             :  * eoxact_list[] stores the OIDs of relations that (might) need AtEOXact
     173             :  * cleanup work.  This list intentionally has limited size; if it overflows,
     174             :  * we fall back to scanning the whole hashtable.  There is no value in a very
     175             :  * large list because (1) at some point, a hash_seq_search scan is faster than
     176             :  * retail lookups, and (2) the value of this is to reduce EOXact work for
     177             :  * short transactions, which can't have dirtied all that many tables anyway.
     178             :  * EOXactListAdd() does not bother to prevent duplicate list entries, so the
     179             :  * cleanup processing must be idempotent.
     180             :  */
     181             : #define MAX_EOXACT_LIST 32
     182             : static Oid  eoxact_list[MAX_EOXACT_LIST];
     183             : static int  eoxact_list_len = 0;
     184             : static bool eoxact_list_overflowed = false;
     185             : 
     186             : #define EOXactListAdd(rel) \
     187             :     do { \
     188             :         if (eoxact_list_len < MAX_EOXACT_LIST) \
     189             :             eoxact_list[eoxact_list_len++] = (rel)->rd_id; \
     190             :         else \
     191             :             eoxact_list_overflowed = true; \
     192             :     } while (0)
     193             : 
     194             : /*
     195             :  * EOXactTupleDescArray stores TupleDescs that (might) need AtEOXact
     196             :  * cleanup work.  The array expands as needed; there is no hashtable because
     197             :  * we don't need to access individual items except at EOXact.
     198             :  */
     199             : static TupleDesc *EOXactTupleDescArray;
     200             : static int  NextEOXactTupleDescNum = 0;
     201             : static int  EOXactTupleDescArrayLen = 0;
     202             : 
     203             : /*
     204             :  *      macros to manipulate the lookup hashtable
     205             :  */
     206             : #define RelationCacheInsert(RELATION, replace_allowed)  \
     207             : do { \
     208             :     RelIdCacheEnt *hentry; bool found; \
     209             :     hentry = (RelIdCacheEnt *) hash_search(RelationIdCache, \
     210             :                                            (void *) &((RELATION)->rd_id), \
     211             :                                            HASH_ENTER, &found); \
     212             :     if (found) \
     213             :     { \
     214             :         /* see comments in RelationBuildDesc and RelationBuildLocalRelation */ \
     215             :         Relation _old_rel = hentry->reldesc; \
     216             :         Assert(replace_allowed); \
     217             :         hentry->reldesc = (RELATION); \
     218             :         if (RelationHasReferenceCountZero(_old_rel)) \
     219             :             RelationDestroyRelation(_old_rel, false); \
     220             :         else if (!IsBootstrapProcessingMode()) \
     221             :             elog(WARNING, "leaking still-referenced relcache entry for \"%s\"", \
     222             :                  RelationGetRelationName(_old_rel)); \
     223             :     } \
     224             :     else \
     225             :         hentry->reldesc = (RELATION); \
     226             : } while(0)
     227             : 
     228             : #define RelationIdCacheLookup(ID, RELATION) \
     229             : do { \
     230             :     RelIdCacheEnt *hentry; \
     231             :     hentry = (RelIdCacheEnt *) hash_search(RelationIdCache, \
     232             :                                            (void *) &(ID), \
     233             :                                            HASH_FIND, NULL); \
     234             :     if (hentry) \
     235             :         RELATION = hentry->reldesc; \
     236             :     else \
     237             :         RELATION = NULL; \
     238             : } while(0)
     239             : 
     240             : #define RelationCacheDelete(RELATION) \
     241             : do { \
     242             :     RelIdCacheEnt *hentry; \
     243             :     hentry = (RelIdCacheEnt *) hash_search(RelationIdCache, \
     244             :                                            (void *) &((RELATION)->rd_id), \
     245             :                                            HASH_REMOVE, NULL); \
     246             :     if (hentry == NULL) \
     247             :         elog(WARNING, "failed to delete relcache entry for OID %u", \
     248             :              (RELATION)->rd_id); \
     249             : } while(0)
     250             : 
     251             : 
     252             : /*
     253             :  * Special cache for opclass-related information
     254             :  *
     255             :  * Note: only default support procs get cached, ie, those with
     256             :  * lefttype = righttype = opcintype.
     257             :  */
     258             : typedef struct opclasscacheent
     259             : {
     260             :     Oid         opclassoid;     /* lookup key: OID of opclass */
     261             :     bool        valid;          /* set true after successful fill-in */
     262             :     StrategyNumber numSupport;  /* max # of support procs (from pg_am) */
     263             :     Oid         opcfamily;      /* OID of opclass's family */
     264             :     Oid         opcintype;      /* OID of opclass's declared input type */
     265             :     RegProcedure *supportProcs; /* OIDs of support procedures */
     266             : } OpClassCacheEnt;
     267             : 
     268             : static HTAB *OpClassCache = NULL;
     269             : 
     270             : 
     271             : /* non-export function prototypes */
     272             : 
     273             : static void RelationDestroyRelation(Relation relation, bool remember_tupdesc);
     274             : static void RelationClearRelation(Relation relation, bool rebuild);
     275             : 
     276             : static void RelationReloadIndexInfo(Relation relation);
     277             : static void RelationReloadNailed(Relation relation);
     278             : static void RelationFlushRelation(Relation relation);
     279             : static void RememberToFreeTupleDescAtEOX(TupleDesc td);
     280             : #ifdef USE_ASSERT_CHECKING
     281             : static void AssertPendingSyncConsistency(Relation relation);
     282             : #endif
     283             : static void AtEOXact_cleanup(Relation relation, bool isCommit);
     284             : static void AtEOSubXact_cleanup(Relation relation, bool isCommit,
     285             :                                 SubTransactionId mySubid, SubTransactionId parentSubid);
     286             : static bool load_relcache_init_file(bool shared);
     287             : static void write_relcache_init_file(bool shared);
     288             : static void write_item(const void *data, Size len, FILE *fp);
     289             : 
     290             : static void formrdesc(const char *relationName, Oid relationReltype,
     291             :                       bool isshared, int natts, const FormData_pg_attribute *attrs);
     292             : 
     293             : static HeapTuple ScanPgRelation(Oid targetRelId, bool indexOK, bool force_non_historic);
     294             : static Relation AllocateRelationDesc(Form_pg_class relp);
     295             : static void RelationParseRelOptions(Relation relation, HeapTuple tuple);
     296             : static void RelationBuildTupleDesc(Relation relation);
     297             : static Relation RelationBuildDesc(Oid targetRelId, bool insertIt);
     298             : static void RelationInitPhysicalAddr(Relation relation);
     299             : static void load_critical_index(Oid indexoid, Oid heapoid);
     300             : static TupleDesc GetPgClassDescriptor(void);
     301             : static TupleDesc GetPgIndexDescriptor(void);
     302             : static void AttrDefaultFetch(Relation relation, int ndef);
     303             : static int  AttrDefaultCmp(const void *a, const void *b);
     304             : static void CheckConstraintFetch(Relation relation);
     305             : static int  CheckConstraintCmp(const void *a, const void *b);
     306             : static void InitIndexAmRoutine(Relation relation);
     307             : static void IndexSupportInitialize(oidvector *indclass,
     308             :                                    RegProcedure *indexSupport,
     309             :                                    Oid *opFamily,
     310             :                                    Oid *opcInType,
     311             :                                    StrategyNumber maxSupportNumber,
     312             :                                    AttrNumber maxAttributeNumber);
     313             : static OpClassCacheEnt *LookupOpclassInfo(Oid operatorClassOid,
     314             :                                           StrategyNumber numSupport);
     315             : static void RelationCacheInitFileRemoveInDir(const char *tblspcpath);
     316             : static void unlink_initfile(const char *initfilename, int elevel);
     317             : 
     318             : 
     319             : /*
     320             :  *      ScanPgRelation
     321             :  *
     322             :  *      This is used by RelationBuildDesc to find a pg_class
     323             :  *      tuple matching targetRelId.  The caller must hold at least
     324             :  *      AccessShareLock on the target relid to prevent concurrent-update
     325             :  *      scenarios; it isn't guaranteed that all scans used to build the
     326             :  *      relcache entry will use the same snapshot.  If, for example,
     327             :  *      an attribute were to be added after scanning pg_class and before
     328             :  *      scanning pg_attribute, relnatts wouldn't match.
     329             :  *
     330             :  *      NB: the returned tuple has been copied into palloc'd storage
     331             :  *      and must eventually be freed with heap_freetuple.
     332             :  */
     333             : static HeapTuple
     334     1149408 : ScanPgRelation(Oid targetRelId, bool indexOK, bool force_non_historic)
     335             : {
     336             :     HeapTuple   pg_class_tuple;
     337             :     Relation    pg_class_desc;
     338             :     SysScanDesc pg_class_scan;
     339             :     ScanKeyData key[1];
     340     1149408 :     Snapshot    snapshot = NULL;
     341             : 
     342             :     /*
     343             :      * If something goes wrong during backend startup, we might find ourselves
     344             :      * trying to read pg_class before we've selected a database.  That ain't
     345             :      * gonna work, so bail out with a useful error message.  If this happens,
     346             :      * it probably means a relcache entry that needs to be nailed isn't.
     347             :      */
     348     1149408 :     if (!OidIsValid(MyDatabaseId))
     349           0 :         elog(FATAL, "cannot read pg_class without having selected a database");
     350             : 
     351             :     /*
     352             :      * form a scan key
     353             :      */
     354     1149408 :     ScanKeyInit(&key[0],
     355             :                 Anum_pg_class_oid,
     356             :                 BTEqualStrategyNumber, F_OIDEQ,
     357             :                 ObjectIdGetDatum(targetRelId));
     358             : 
     359             :     /*
     360             :      * Open pg_class and fetch a tuple.  Force heap scan if we haven't yet
     361             :      * built the critical relcache entries (this includes initdb and startup
     362             :      * without a pg_internal.init file).  The caller can also force a heap
     363             :      * scan by setting indexOK == false.
     364             :      */
     365     1149408 :     pg_class_desc = table_open(RelationRelationId, AccessShareLock);
     366             : 
     367             :     /*
     368             :      * The caller might need a tuple that's newer than the one the historic
     369             :      * snapshot; currently the only case requiring to do so is looking up the
     370             :      * relfilenode of non mapped system relations during decoding. That
     371             :      * snapshot can't change in the midst of a relcache build, so there's no
     372             :      * need to register the snapshot.
     373             :      */
     374     1149408 :     if (force_non_historic)
     375        2024 :         snapshot = GetNonHistoricCatalogSnapshot(RelationRelationId);
     376             : 
     377     1149408 :     pg_class_scan = systable_beginscan(pg_class_desc, ClassOidIndexId,
     378     1149408 :                                        indexOK && criticalRelcachesBuilt,
     379             :                                        snapshot,
     380             :                                        1, key);
     381             : 
     382     1149402 :     pg_class_tuple = systable_getnext(pg_class_scan);
     383             : 
     384             :     /*
     385             :      * Must copy tuple before releasing buffer.
     386             :      */
     387     1149396 :     if (HeapTupleIsValid(pg_class_tuple))
     388     1149388 :         pg_class_tuple = heap_copytuple(pg_class_tuple);
     389             : 
     390             :     /* all done */
     391     1149396 :     systable_endscan(pg_class_scan);
     392     1149396 :     table_close(pg_class_desc, AccessShareLock);
     393             : 
     394     1149396 :     return pg_class_tuple;
     395             : }
     396             : 
     397             : /*
     398             :  *      AllocateRelationDesc
     399             :  *
     400             :  *      This is used to allocate memory for a new relation descriptor
     401             :  *      and initialize the rd_rel field from the given pg_class tuple.
     402             :  */
     403             : static Relation
     404     1069398 : AllocateRelationDesc(Form_pg_class relp)
     405             : {
     406             :     Relation    relation;
     407             :     MemoryContext oldcxt;
     408             :     Form_pg_class relationForm;
     409             : 
     410             :     /* Relcache entries must live in CacheMemoryContext */
     411     1069398 :     oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
     412             : 
     413             :     /*
     414             :      * allocate and zero space for new relation descriptor
     415             :      */
     416     1069398 :     relation = (Relation) palloc0(sizeof(RelationData));
     417             : 
     418             :     /* make sure relation is marked as having no open file yet */
     419     1069398 :     relation->rd_smgr = NULL;
     420             : 
     421             :     /*
     422             :      * Copy the relation tuple form
     423             :      *
     424             :      * We only allocate space for the fixed fields, ie, CLASS_TUPLE_SIZE. The
     425             :      * variable-length fields (relacl, reloptions) are NOT stored in the
     426             :      * relcache --- there'd be little point in it, since we don't copy the
     427             :      * tuple's nulls bitmap and hence wouldn't know if the values are valid.
     428             :      * Bottom line is that relacl *cannot* be retrieved from the relcache. Get
     429             :      * it from the syscache if you need it.  The same goes for the original
     430             :      * form of reloptions (however, we do store the parsed form of reloptions
     431             :      * in rd_options).
     432             :      */
     433     1069398 :     relationForm = (Form_pg_class) palloc(CLASS_TUPLE_SIZE);
     434             : 
     435     1069398 :     memcpy(relationForm, relp, CLASS_TUPLE_SIZE);
     436             : 
     437             :     /* initialize relation tuple form */
     438     1069398 :     relation->rd_rel = relationForm;
     439             : 
     440             :     /* and allocate attribute tuple form storage */
     441     1069398 :     relation->rd_att = CreateTemplateTupleDesc(relationForm->relnatts);
     442             :     /* which we mark as a reference-counted tupdesc */
     443     1069398 :     relation->rd_att->tdrefcount = 1;
     444             : 
     445     1069398 :     MemoryContextSwitchTo(oldcxt);
     446             : 
     447     1069398 :     return relation;
     448             : }
     449             : 
     450             : /*
     451             :  * RelationParseRelOptions
     452             :  *      Convert pg_class.reloptions into pre-parsed rd_options
     453             :  *
     454             :  * tuple is the real pg_class tuple (not rd_rel!) for relation
     455             :  *
     456             :  * Note: rd_rel and (if an index) rd_indam must be valid already
     457             :  */
     458             : static void
     459     1140272 : RelationParseRelOptions(Relation relation, HeapTuple tuple)
     460             : {
     461             :     bytea      *options;
     462             :     amoptions_function amoptsfn;
     463             : 
     464     1140272 :     relation->rd_options = NULL;
     465             : 
     466             :     /*
     467             :      * Look up any AM-specific parse function; fall out if relkind should not
     468             :      * have options.
     469             :      */
     470     1140272 :     switch (relation->rd_rel->relkind)
     471             :     {
     472      710458 :         case RELKIND_RELATION:
     473             :         case RELKIND_TOASTVALUE:
     474             :         case RELKIND_VIEW:
     475             :         case RELKIND_MATVIEW:
     476             :         case RELKIND_PARTITIONED_TABLE:
     477      710458 :             amoptsfn = NULL;
     478      710458 :             break;
     479      423374 :         case RELKIND_INDEX:
     480             :         case RELKIND_PARTITIONED_INDEX:
     481      423374 :             amoptsfn = relation->rd_indam->amoptions;
     482      423374 :             break;
     483        6440 :         default:
     484        6440 :             return;
     485             :     }
     486             : 
     487             :     /*
     488             :      * Fetch reloptions from tuple; have to use a hardwired descriptor because
     489             :      * we might not have any other for pg_class yet (consider executing this
     490             :      * code for pg_class itself)
     491             :      */
     492     1133832 :     options = extractRelOptions(tuple, GetPgClassDescriptor(), amoptsfn);
     493             : 
     494             :     /*
     495             :      * Copy parsed data into CacheMemoryContext.  To guard against the
     496             :      * possibility of leaks in the reloptions code, we want to do the actual
     497             :      * parsing in the caller's memory context and copy the results into
     498             :      * CacheMemoryContext after the fact.
     499             :      */
     500     1133832 :     if (options)
     501             :     {
     502       21324 :         relation->rd_options = MemoryContextAlloc(CacheMemoryContext,
     503       10662 :                                                   VARSIZE(options));
     504       10662 :         memcpy(relation->rd_options, options, VARSIZE(options));
     505       10662 :         pfree(options);
     506             :     }
     507             : }
     508             : 
     509             : /*
     510             :  *      RelationBuildTupleDesc
     511             :  *
     512             :  *      Form the relation's tuple descriptor from information in
     513             :  *      the pg_attribute, pg_attrdef & pg_constraint system catalogs.
     514             :  */
     515             : static void
     516     1069398 : RelationBuildTupleDesc(Relation relation)
     517             : {
     518             :     HeapTuple   pg_attribute_tuple;
     519             :     Relation    pg_attribute_desc;
     520             :     SysScanDesc pg_attribute_scan;
     521             :     ScanKeyData skey[2];
     522             :     int         need;
     523             :     TupleConstr *constr;
     524     1069398 :     AttrMissing *attrmiss = NULL;
     525     1069398 :     int         ndef = 0;
     526             : 
     527             :     /* fill rd_att's type ID fields (compare heap.c's AddNewRelationTuple) */
     528     1069398 :     relation->rd_att->tdtypeid =
     529     1069398 :         relation->rd_rel->reltype ? relation->rd_rel->reltype : RECORDOID;
     530     1069398 :     relation->rd_att->tdtypmod = -1;  /* just to be sure */
     531             : 
     532     1069398 :     constr = (TupleConstr *) MemoryContextAllocZero(CacheMemoryContext,
     533             :                                                     sizeof(TupleConstr));
     534     1069398 :     constr->has_not_null = false;
     535     1069398 :     constr->has_generated_stored = false;
     536             : 
     537             :     /*
     538             :      * Form a scan key that selects only user attributes (attnum > 0).
     539             :      * (Eliminating system attribute rows at the index level is lots faster
     540             :      * than fetching them.)
     541             :      */
     542     1069398 :     ScanKeyInit(&skey[0],
     543             :                 Anum_pg_attribute_attrelid,
     544             :                 BTEqualStrategyNumber, F_OIDEQ,
     545     1069398 :                 ObjectIdGetDatum(RelationGetRelid(relation)));
     546     1069398 :     ScanKeyInit(&skey[1],
     547             :                 Anum_pg_attribute_attnum,
     548             :                 BTGreaterStrategyNumber, F_INT2GT,
     549             :                 Int16GetDatum(0));
     550             : 
     551             :     /*
     552             :      * Open pg_attribute and begin a scan.  Force heap scan if we haven't yet
     553             :      * built the critical relcache entries (this includes initdb and startup
     554             :      * without a pg_internal.init file).
     555             :      */
     556     1069398 :     pg_attribute_desc = table_open(AttributeRelationId, AccessShareLock);
     557     1069398 :     pg_attribute_scan = systable_beginscan(pg_attribute_desc,
     558             :                                            AttributeRelidNumIndexId,
     559             :                                            criticalRelcachesBuilt,
     560             :                                            NULL,
     561             :                                            2, skey);
     562             : 
     563             :     /*
     564             :      * add attribute data to relation->rd_att
     565             :      */
     566     1069398 :     need = RelationGetNumberOfAttributes(relation);
     567             : 
     568     5014266 :     while (HeapTupleIsValid(pg_attribute_tuple = systable_getnext(pg_attribute_scan)))
     569             :     {
     570             :         Form_pg_attribute attp;
     571             :         int         attnum;
     572             : 
     573     5013566 :         attp = (Form_pg_attribute) GETSTRUCT(pg_attribute_tuple);
     574             : 
     575     5013566 :         attnum = attp->attnum;
     576     5013566 :         if (attnum <= 0 || attnum > RelationGetNumberOfAttributes(relation))
     577           0 :             elog(ERROR, "invalid attribute number %d for relation \"%s\"",
     578             :                  attp->attnum, RelationGetRelationName(relation));
     579             : 
     580     5013566 :         memcpy(TupleDescAttr(relation->rd_att, attnum - 1),
     581             :                attp,
     582             :                ATTRIBUTE_FIXED_PART_SIZE);
     583             : 
     584             :         /* Update constraint/default info */
     585     5013566 :         if (attp->attnotnull)
     586     1835960 :             constr->has_not_null = true;
     587     5013566 :         if (attp->attgenerated == ATTRIBUTE_GENERATED_STORED)
     588        2726 :             constr->has_generated_stored = true;
     589     5013566 :         if (attp->atthasdef)
     590       21674 :             ndef++;
     591             : 
     592             :         /* If the column has a "missing" value, put it in the attrmiss array */
     593     5013566 :         if (attp->atthasmissing)
     594             :         {
     595             :             Datum       missingval;
     596             :             bool        missingNull;
     597             : 
     598             :             /* Do we have a missing value? */
     599        4764 :             missingval = heap_getattr(pg_attribute_tuple,
     600             :                                       Anum_pg_attribute_attmissingval,
     601             :                                       pg_attribute_desc->rd_att,
     602             :                                       &missingNull);
     603        4764 :             if (!missingNull)
     604             :             {
     605             :                 /* Yes, fetch from the array */
     606             :                 MemoryContext oldcxt;
     607             :                 bool        is_null;
     608        4764 :                 int         one = 1;
     609             :                 Datum       missval;
     610             : 
     611        4764 :                 if (attrmiss == NULL)
     612             :                     attrmiss = (AttrMissing *)
     613        2242 :                         MemoryContextAllocZero(CacheMemoryContext,
     614        2242 :                                                relation->rd_rel->relnatts *
     615             :                                                sizeof(AttrMissing));
     616             : 
     617        4764 :                 missval = array_get_element(missingval,
     618             :                                             1,
     619             :                                             &one,
     620             :                                             -1,
     621        4764 :                                             attp->attlen,
     622        4764 :                                             attp->attbyval,
     623        4764 :                                             attp->attalign,
     624             :                                             &is_null);
     625             :                 Assert(!is_null);
     626        4764 :                 if (attp->attbyval)
     627             :                 {
     628             :                     /* for copy by val just copy the datum direct */
     629        3036 :                     attrmiss[attnum - 1].am_value = missval;
     630             :                 }
     631             :                 else
     632             :                 {
     633             :                     /* otherwise copy in the correct context */
     634        1728 :                     oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
     635        3456 :                     attrmiss[attnum - 1].am_value = datumCopy(missval,
     636        1728 :                                                               attp->attbyval,
     637        1728 :                                                               attp->attlen);
     638        1728 :                     MemoryContextSwitchTo(oldcxt);
     639             :                 }
     640        4764 :                 attrmiss[attnum - 1].am_present = true;
     641             :             }
     642             :         }
     643     5013566 :         need--;
     644     5013566 :         if (need == 0)
     645     1068698 :             break;
     646             :     }
     647             : 
     648             :     /*
     649             :      * end the scan and close the attribute relation
     650             :      */
     651     1069398 :     systable_endscan(pg_attribute_scan);
     652     1069398 :     table_close(pg_attribute_desc, AccessShareLock);
     653             : 
     654     1069398 :     if (need != 0)
     655           0 :         elog(ERROR, "pg_attribute catalog is missing %d attribute(s) for relation OID %u",
     656             :              need, RelationGetRelid(relation));
     657             : 
     658             :     /*
     659             :      * The attcacheoff values we read from pg_attribute should all be -1
     660             :      * ("unknown").  Verify this if assert checking is on.  They will be
     661             :      * computed when and if needed during tuple access.
     662             :      */
     663             : #ifdef USE_ASSERT_CHECKING
     664             :     {
     665             :         int         i;
     666             : 
     667             :         for (i = 0; i < RelationGetNumberOfAttributes(relation); i++)
     668             :             Assert(TupleDescAttr(relation->rd_att, i)->attcacheoff == -1);
     669             :     }
     670             : #endif
     671             : 
     672             :     /*
     673             :      * However, we can easily set the attcacheoff value for the first
     674             :      * attribute: it must be zero.  This eliminates the need for special cases
     675             :      * for attnum=1 that used to exist in fastgetattr() and index_getattr().
     676             :      */
     677     1069398 :     if (RelationGetNumberOfAttributes(relation) > 0)
     678     1068698 :         TupleDescAttr(relation->rd_att, 0)->attcacheoff = 0;
     679             : 
     680             :     /*
     681             :      * Set up constraint/default info
     682             :      */
     683     1069398 :     if (constr->has_not_null ||
     684      739688 :         constr->has_generated_stored ||
     685      736090 :         ndef > 0 ||
     686      736074 :         attrmiss ||
     687      736074 :         relation->rd_rel->relchecks > 0)
     688             :     {
     689      336286 :         relation->rd_att->constr = constr;
     690             : 
     691      336286 :         if (ndef > 0)            /* DEFAULTs */
     692       15204 :             AttrDefaultFetch(relation, ndef);
     693             :         else
     694      321082 :             constr->num_defval = 0;
     695             : 
     696      336286 :         constr->missing = attrmiss;
     697             : 
     698      336286 :         if (relation->rd_rel->relchecks > 0)   /* CHECKs */
     699        6670 :             CheckConstraintFetch(relation);
     700             :         else
     701      329616 :             constr->num_check = 0;
     702             :     }
     703             :     else
     704             :     {
     705      733112 :         pfree(constr);
     706      733112 :         relation->rd_att->constr = NULL;
     707             :     }
     708     1069398 : }
     709             : 
     710             : /*
     711             :  *      RelationBuildRuleLock
     712             :  *
     713             :  *      Form the relation's rewrite rules from information in
     714             :  *      the pg_rewrite system catalog.
     715             :  *
     716             :  * Note: The rule parsetrees are potentially very complex node structures.
     717             :  * To allow these trees to be freed when the relcache entry is flushed,
     718             :  * we make a private memory context to hold the RuleLock information for
     719             :  * each relcache entry that has associated rules.  The context is used
     720             :  * just for rule info, not for any other subsidiary data of the relcache
     721             :  * entry, because that keeps the update logic in RelationClearRelation()
     722             :  * manageable.  The other subsidiary data structures are simple enough
     723             :  * to be easy to free explicitly, anyway.
     724             :  */
     725             : static void
     726       91672 : RelationBuildRuleLock(Relation relation)
     727             : {
     728             :     MemoryContext rulescxt;
     729             :     MemoryContext oldcxt;
     730             :     HeapTuple   rewrite_tuple;
     731             :     Relation    rewrite_desc;
     732             :     TupleDesc   rewrite_tupdesc;
     733             :     SysScanDesc rewrite_scan;
     734             :     ScanKeyData key;
     735             :     RuleLock   *rulelock;
     736             :     int         numlocks;
     737             :     RewriteRule **rules;
     738             :     int         maxlocks;
     739             : 
     740             :     /*
     741             :      * Make the private context.  Assume it'll not contain much data.
     742             :      */
     743       91672 :     rulescxt = AllocSetContextCreate(CacheMemoryContext,
     744             :                                      "relation rules",
     745             :                                      ALLOCSET_SMALL_SIZES);
     746       91672 :     relation->rd_rulescxt = rulescxt;
     747       91672 :     MemoryContextCopyAndSetIdentifier(rulescxt,
     748             :                                       RelationGetRelationName(relation));
     749             : 
     750             :     /*
     751             :      * allocate an array to hold the rewrite rules (the array is extended if
     752             :      * necessary)
     753             :      */
     754       91672 :     maxlocks = 4;
     755             :     rules = (RewriteRule **)
     756       91672 :         MemoryContextAlloc(rulescxt, sizeof(RewriteRule *) * maxlocks);
     757       91672 :     numlocks = 0;
     758             : 
     759             :     /*
     760             :      * form a scan key
     761             :      */
     762       91672 :     ScanKeyInit(&key,
     763             :                 Anum_pg_rewrite_ev_class,
     764             :                 BTEqualStrategyNumber, F_OIDEQ,
     765       91672 :                 ObjectIdGetDatum(RelationGetRelid(relation)));
     766             : 
     767             :     /*
     768             :      * open pg_rewrite and begin a scan
     769             :      *
     770             :      * Note: since we scan the rules using RewriteRelRulenameIndexId, we will
     771             :      * be reading the rules in name order, except possibly during
     772             :      * emergency-recovery operations (ie, IgnoreSystemIndexes). This in turn
     773             :      * ensures that rules will be fired in name order.
     774             :      */
     775       91672 :     rewrite_desc = table_open(RewriteRelationId, AccessShareLock);
     776       91672 :     rewrite_tupdesc = RelationGetDescr(rewrite_desc);
     777       91672 :     rewrite_scan = systable_beginscan(rewrite_desc,
     778             :                                       RewriteRelRulenameIndexId,
     779             :                                       true, NULL,
     780             :                                       1, &key);
     781             : 
     782      182870 :     while (HeapTupleIsValid(rewrite_tuple = systable_getnext(rewrite_scan)))
     783             :     {
     784       91198 :         Form_pg_rewrite rewrite_form = (Form_pg_rewrite) GETSTRUCT(rewrite_tuple);
     785             :         bool        isnull;
     786             :         Datum       rule_datum;
     787             :         char       *rule_str;
     788             :         RewriteRule *rule;
     789             : 
     790       91198 :         rule = (RewriteRule *) MemoryContextAlloc(rulescxt,
     791             :                                                   sizeof(RewriteRule));
     792             : 
     793       91198 :         rule->ruleId = rewrite_form->oid;
     794             : 
     795       91198 :         rule->event = rewrite_form->ev_type - '0';
     796       91198 :         rule->enabled = rewrite_form->ev_enabled;
     797       91198 :         rule->isInstead = rewrite_form->is_instead;
     798             : 
     799             :         /*
     800             :          * Must use heap_getattr to fetch ev_action and ev_qual.  Also, the
     801             :          * rule strings are often large enough to be toasted.  To avoid
     802             :          * leaking memory in the caller's context, do the detoasting here so
     803             :          * we can free the detoasted version.
     804             :          */
     805       91198 :         rule_datum = heap_getattr(rewrite_tuple,
     806             :                                   Anum_pg_rewrite_ev_action,
     807             :                                   rewrite_tupdesc,
     808             :                                   &isnull);
     809             :         Assert(!isnull);
     810       91198 :         rule_str = TextDatumGetCString(rule_datum);
     811       91198 :         oldcxt = MemoryContextSwitchTo(rulescxt);
     812       91198 :         rule->actions = (List *) stringToNode(rule_str);
     813       91198 :         MemoryContextSwitchTo(oldcxt);
     814       91198 :         pfree(rule_str);
     815             : 
     816       91198 :         rule_datum = heap_getattr(rewrite_tuple,
     817             :                                   Anum_pg_rewrite_ev_qual,
     818             :                                   rewrite_tupdesc,
     819             :                                   &isnull);
     820             :         Assert(!isnull);
     821       91198 :         rule_str = TextDatumGetCString(rule_datum);
     822       91198 :         oldcxt = MemoryContextSwitchTo(rulescxt);
     823       91198 :         rule->qual = (Node *) stringToNode(rule_str);
     824       91198 :         MemoryContextSwitchTo(oldcxt);
     825       91198 :         pfree(rule_str);
     826             : 
     827             :         /*
     828             :          * We want the rule's table references to be checked as though by the
     829             :          * table owner, not the user referencing the rule.  Therefore, scan
     830             :          * through the rule's actions and set the checkAsUser field on all
     831             :          * rtable entries.  We have to look at the qual as well, in case it
     832             :          * contains sublinks.
     833             :          *
     834             :          * The reason for doing this when the rule is loaded, rather than when
     835             :          * it is stored, is that otherwise ALTER TABLE OWNER would have to
     836             :          * grovel through stored rules to update checkAsUser fields. Scanning
     837             :          * the rule tree during load is relatively cheap (compared to
     838             :          * constructing it in the first place), so we do it here.
     839             :          */
     840       91198 :         setRuleCheckAsUser((Node *) rule->actions, relation->rd_rel->relowner);
     841       91198 :         setRuleCheckAsUser(rule->qual, relation->rd_rel->relowner);
     842             : 
     843       91198 :         if (numlocks >= maxlocks)
     844             :         {
     845          10 :             maxlocks *= 2;
     846             :             rules = (RewriteRule **)
     847          10 :                 repalloc(rules, sizeof(RewriteRule *) * maxlocks);
     848             :         }
     849       91198 :         rules[numlocks++] = rule;
     850             :     }
     851             : 
     852             :     /*
     853             :      * end the scan and close the attribute relation
     854             :      */
     855       91672 :     systable_endscan(rewrite_scan);
     856       91672 :     table_close(rewrite_desc, AccessShareLock);
     857             : 
     858             :     /*
     859             :      * there might not be any rules (if relhasrules is out-of-date)
     860             :      */
     861       91672 :     if (numlocks == 0)
     862             :     {
     863        1602 :         relation->rd_rules = NULL;
     864        1602 :         relation->rd_rulescxt = NULL;
     865        1602 :         MemoryContextDelete(rulescxt);
     866        1602 :         return;
     867             :     }
     868             : 
     869             :     /*
     870             :      * form a RuleLock and insert into relation
     871             :      */
     872       90070 :     rulelock = (RuleLock *) MemoryContextAlloc(rulescxt, sizeof(RuleLock));
     873       90070 :     rulelock->numLocks = numlocks;
     874       90070 :     rulelock->rules = rules;
     875             : 
     876       90070 :     relation->rd_rules = rulelock;
     877             : }
     878             : 
     879             : /*
     880             :  *      equalRuleLocks
     881             :  *
     882             :  *      Determine whether two RuleLocks are equivalent
     883             :  *
     884             :  *      Probably this should be in the rules code someplace...
     885             :  */
     886             : static bool
     887      383174 : equalRuleLocks(RuleLock *rlock1, RuleLock *rlock2)
     888             : {
     889             :     int         i;
     890             : 
     891             :     /*
     892             :      * As of 7.3 we assume the rule ordering is repeatable, because
     893             :      * RelationBuildRuleLock should read 'em in a consistent order.  So just
     894             :      * compare corresponding slots.
     895             :      */
     896      383174 :     if (rlock1 != NULL)
     897             :     {
     898        1048 :         if (rlock2 == NULL)
     899          12 :             return false;
     900        1036 :         if (rlock1->numLocks != rlock2->numLocks)
     901           0 :             return false;
     902        1942 :         for (i = 0; i < rlock1->numLocks; i++)
     903             :         {
     904        1044 :             RewriteRule *rule1 = rlock1->rules[i];
     905        1044 :             RewriteRule *rule2 = rlock2->rules[i];
     906             : 
     907        1044 :             if (rule1->ruleId != rule2->ruleId)
     908           0 :                 return false;
     909        1044 :             if (rule1->event != rule2->event)
     910           0 :                 return false;
     911        1044 :             if (rule1->enabled != rule2->enabled)
     912          12 :                 return false;
     913        1032 :             if (rule1->isInstead != rule2->isInstead)
     914           0 :                 return false;
     915        1032 :             if (!equal(rule1->qual, rule2->qual))
     916           0 :                 return false;
     917        1032 :             if (!equal(rule1->actions, rule2->actions))
     918         126 :                 return false;
     919             :         }
     920             :     }
     921      382126 :     else if (rlock2 != NULL)
     922       68306 :         return false;
     923      314718 :     return true;
     924             : }
     925             : 
     926             : /*
     927             :  *      equalPolicy
     928             :  *
     929             :  *      Determine whether two policies are equivalent
     930             :  */
     931             : static bool
     932         132 : equalPolicy(RowSecurityPolicy *policy1, RowSecurityPolicy *policy2)
     933             : {
     934             :     int         i;
     935             :     Oid        *r1,
     936             :                *r2;
     937             : 
     938         132 :     if (policy1 != NULL)
     939             :     {
     940         132 :         if (policy2 == NULL)
     941           0 :             return false;
     942             : 
     943         132 :         if (policy1->polcmd != policy2->polcmd)
     944           0 :             return false;
     945         132 :         if (policy1->hassublinks != policy2->hassublinks)
     946           0 :             return false;
     947         132 :         if (strcmp(policy1->policy_name, policy2->policy_name) != 0)
     948           0 :             return false;
     949         132 :         if (ARR_DIMS(policy1->roles)[0] != ARR_DIMS(policy2->roles)[0])
     950           0 :             return false;
     951             : 
     952         132 :         r1 = (Oid *) ARR_DATA_PTR(policy1->roles);
     953         132 :         r2 = (Oid *) ARR_DATA_PTR(policy2->roles);
     954             : 
     955         264 :         for (i = 0; i < ARR_DIMS(policy1->roles)[0]; i++)
     956             :         {
     957         132 :             if (r1[i] != r2[i])
     958           0 :                 return false;
     959             :         }
     960             : 
     961         132 :         if (!equal(policy1->qual, policy2->qual))
     962           0 :             return false;
     963         132 :         if (!equal(policy1->with_check_qual, policy2->with_check_qual))
     964           0 :             return false;
     965             :     }
     966           0 :     else if (policy2 != NULL)
     967           0 :         return false;
     968             : 
     969         132 :     return true;
     970             : }
     971             : 
     972             : /*
     973             :  *      equalRSDesc
     974             :  *
     975             :  *      Determine whether two RowSecurityDesc's are equivalent
     976             :  */
     977             : static bool
     978      383174 : equalRSDesc(RowSecurityDesc *rsdesc1, RowSecurityDesc *rsdesc2)
     979             : {
     980             :     ListCell   *lc,
     981             :                *rc;
     982             : 
     983      383174 :     if (rsdesc1 == NULL && rsdesc2 == NULL)
     984      382908 :         return true;
     985             : 
     986         266 :     if ((rsdesc1 != NULL && rsdesc2 == NULL) ||
     987         182 :         (rsdesc1 == NULL && rsdesc2 != NULL))
     988         190 :         return false;
     989             : 
     990          76 :     if (list_length(rsdesc1->policies) != list_length(rsdesc2->policies))
     991           0 :         return false;
     992             : 
     993             :     /* RelationBuildRowSecurity should build policies in order */
     994         208 :     forboth(lc, rsdesc1->policies, rc, rsdesc2->policies)
     995             :     {
     996         132 :         RowSecurityPolicy *l = (RowSecurityPolicy *) lfirst(lc);
     997         132 :         RowSecurityPolicy *r = (RowSecurityPolicy *) lfirst(rc);
     998             : 
     999         132 :         if (!equalPolicy(l, r))
    1000           0 :             return false;
    1001             :     }
    1002             : 
    1003          76 :     return true;
    1004             : }
    1005             : 
    1006             : /*
    1007             :  *      RelationBuildDesc
    1008             :  *
    1009             :  *      Build a relation descriptor.  The caller must hold at least
    1010             :  *      AccessShareLock on the target relid.
    1011             :  *
    1012             :  *      The new descriptor is inserted into the hash table if insertIt is true.
    1013             :  *
    1014             :  *      Returns NULL if no pg_class row could be found for the given relid
    1015             :  *      (suggesting we are trying to access a just-deleted relation).
    1016             :  *      Any other error is reported via elog.
    1017             :  */
    1018             : static Relation
    1019     1069404 : RelationBuildDesc(Oid targetRelId, bool insertIt)
    1020             : {
    1021             :     int         in_progress_offset;
    1022             :     Relation    relation;
    1023             :     Oid         relid;
    1024             :     HeapTuple   pg_class_tuple;
    1025             :     Form_pg_class relp;
    1026             : 
    1027             :     /*
    1028             :      * This function and its subroutines can allocate a good deal of transient
    1029             :      * data in CurrentMemoryContext.  Traditionally we've just leaked that
    1030             :      * data, reasoning that the caller's context is at worst of transaction
    1031             :      * scope, and relcache loads shouldn't happen so often that it's essential
    1032             :      * to recover transient data before end of statement/transaction.  However
    1033             :      * that's definitely not true when debug_discard_caches is active, and
    1034             :      * perhaps it's not true in other cases.
    1035             :      *
    1036             :      * When debug_discard_caches is active or when forced to by
    1037             :      * RECOVER_RELATION_BUILD_MEMORY=1, arrange to allocate the junk in a
    1038             :      * temporary context that we'll free before returning.  Make it a child of
    1039             :      * caller's context so that it will get cleaned up appropriately if we
    1040             :      * error out partway through.
    1041             :      */
    1042             : #ifdef MAYBE_RECOVER_RELATION_BUILD_MEMORY
    1043             :     MemoryContext tmpcxt = NULL;
    1044             :     MemoryContext oldcxt = NULL;
    1045             : 
    1046             :     if (RECOVER_RELATION_BUILD_MEMORY || debug_discard_caches > 0)
    1047             :     {
    1048             :         tmpcxt = AllocSetContextCreate(CurrentMemoryContext,
    1049             :                                        "RelationBuildDesc workspace",
    1050             :                                        ALLOCSET_DEFAULT_SIZES);
    1051             :         oldcxt = MemoryContextSwitchTo(tmpcxt);
    1052             :     }
    1053             : #endif
    1054             : 
    1055             :     /* Register to catch invalidation messages */
    1056     1069404 :     if (in_progress_list_len >= in_progress_list_maxlen)
    1057             :     {
    1058             :         int         allocsize;
    1059             : 
    1060           0 :         allocsize = in_progress_list_maxlen * 2;
    1061           0 :         in_progress_list = repalloc(in_progress_list,
    1062             :                                     allocsize * sizeof(*in_progress_list));
    1063           0 :         in_progress_list_maxlen = allocsize;
    1064             :     }
    1065     1069404 :     in_progress_offset = in_progress_list_len++;
    1066     1069404 :     in_progress_list[in_progress_offset].reloid = targetRelId;
    1067     1069406 : retry:
    1068     1069406 :     in_progress_list[in_progress_offset].invalidated = false;
    1069             : 
    1070             :     /*
    1071             :      * find the tuple in pg_class corresponding to the given relation id
    1072             :      */
    1073     1069406 :     pg_class_tuple = ScanPgRelation(targetRelId, true, false);
    1074             : 
    1075             :     /*
    1076             :      * if no such tuple exists, return NULL
    1077             :      */
    1078     1069406 :     if (!HeapTupleIsValid(pg_class_tuple))
    1079             :     {
    1080             : #ifdef MAYBE_RECOVER_RELATION_BUILD_MEMORY
    1081             :         if (tmpcxt)
    1082             :         {
    1083             :             /* Return to caller's context, and blow away the temporary context */
    1084             :             MemoryContextSwitchTo(oldcxt);
    1085             :             MemoryContextDelete(tmpcxt);
    1086             :         }
    1087             : #endif
    1088             :         Assert(in_progress_offset + 1 == in_progress_list_len);
    1089           8 :         in_progress_list_len--;
    1090           8 :         return NULL;
    1091             :     }
    1092             : 
    1093             :     /*
    1094             :      * get information from the pg_class_tuple
    1095             :      */
    1096     1069398 :     relp = (Form_pg_class) GETSTRUCT(pg_class_tuple);
    1097     1069398 :     relid = relp->oid;
    1098             :     Assert(relid == targetRelId);
    1099             : 
    1100             :     /*
    1101             :      * allocate storage for the relation descriptor, and copy pg_class_tuple
    1102             :      * to relation->rd_rel.
    1103             :      */
    1104     1069398 :     relation = AllocateRelationDesc(relp);
    1105             : 
    1106             :     /*
    1107             :      * initialize the relation's relation id (relation->rd_id)
    1108             :      */
    1109     1069398 :     RelationGetRelid(relation) = relid;
    1110             : 
    1111             :     /*
    1112             :      * Normal relations are not nailed into the cache.  Since we don't flush
    1113             :      * new relations, it won't be new.  It could be temp though.
    1114             :      */
    1115     1069398 :     relation->rd_refcnt = 0;
    1116     1069398 :     relation->rd_isnailed = false;
    1117     1069398 :     relation->rd_createSubid = InvalidSubTransactionId;
    1118     1069398 :     relation->rd_newRelfilenodeSubid = InvalidSubTransactionId;
    1119     1069398 :     relation->rd_firstRelfilenodeSubid = InvalidSubTransactionId;
    1120     1069398 :     relation->rd_droppedSubid = InvalidSubTransactionId;
    1121     1069398 :     switch (relation->rd_rel->relpersistence)
    1122             :     {
    1123     1054752 :         case RELPERSISTENCE_UNLOGGED:
    1124             :         case RELPERSISTENCE_PERMANENT:
    1125     1054752 :             relation->rd_backend = InvalidBackendId;
    1126     1054752 :             relation->rd_islocaltemp = false;
    1127     1054752 :             break;
    1128       14646 :         case RELPERSISTENCE_TEMP:
    1129       14646 :             if (isTempOrTempToastNamespace(relation->rd_rel->relnamespace))
    1130             :             {
    1131       14612 :                 relation->rd_backend = BackendIdForTempRelations();
    1132       14612 :                 relation->rd_islocaltemp = true;
    1133             :             }
    1134             :             else
    1135             :             {
    1136             :                 /*
    1137             :                  * If it's a temp table, but not one of ours, we have to use
    1138             :                  * the slow, grotty method to figure out the owning backend.
    1139             :                  *
    1140             :                  * Note: it's possible that rd_backend gets set to MyBackendId
    1141             :                  * here, in case we are looking at a pg_class entry left over
    1142             :                  * from a crashed backend that coincidentally had the same
    1143             :                  * BackendId we're using.  We should *not* consider such a
    1144             :                  * table to be "ours"; this is why we need the separate
    1145             :                  * rd_islocaltemp flag.  The pg_class entry will get flushed
    1146             :                  * if/when we clean out the corresponding temp table namespace
    1147             :                  * in preparation for using it.
    1148             :                  */
    1149          34 :                 relation->rd_backend =
    1150          34 :                     GetTempNamespaceBackendId(relation->rd_rel->relnamespace);
    1151             :                 Assert(relation->rd_backend != InvalidBackendId);
    1152          34 :                 relation->rd_islocaltemp = false;
    1153             :             }
    1154       14646 :             break;
    1155           0 :         default:
    1156           0 :             elog(ERROR, "invalid relpersistence: %c",
    1157             :                  relation->rd_rel->relpersistence);
    1158             :             break;
    1159             :     }
    1160             : 
    1161             :     /*
    1162             :      * initialize the tuple descriptor (relation->rd_att).
    1163             :      */
    1164     1069398 :     RelationBuildTupleDesc(relation);
    1165             : 
    1166             :     /*
    1167             :      * Fetch rules and triggers that affect this relation
    1168             :      */
    1169     1069398 :     if (relation->rd_rel->relhasrules)
    1170       91672 :         RelationBuildRuleLock(relation);
    1171             :     else
    1172             :     {
    1173      977726 :         relation->rd_rules = NULL;
    1174      977726 :         relation->rd_rulescxt = NULL;
    1175             :     }
    1176             : 
    1177     1069398 :     if (relation->rd_rel->relhastriggers)
    1178       31570 :         RelationBuildTriggers(relation);
    1179             :     else
    1180     1037828 :         relation->trigdesc = NULL;
    1181             : 
    1182     1069398 :     if (relation->rd_rel->relrowsecurity)
    1183        1144 :         RelationBuildRowSecurity(relation);
    1184             :     else
    1185     1068254 :         relation->rd_rsdesc = NULL;
    1186             : 
    1187             :     /* foreign key data is not loaded till asked for */
    1188     1069398 :     relation->rd_fkeylist = NIL;
    1189     1069398 :     relation->rd_fkeyvalid = false;
    1190             : 
    1191             :     /* partitioning data is not loaded till asked for */
    1192     1069398 :     relation->rd_partkey = NULL;
    1193     1069398 :     relation->rd_partkeycxt = NULL;
    1194     1069398 :     relation->rd_partdesc = NULL;
    1195     1069398 :     relation->rd_partdesc_nodetached = NULL;
    1196     1069398 :     relation->rd_partdesc_nodetached_xmin = InvalidTransactionId;
    1197     1069398 :     relation->rd_pdcxt = NULL;
    1198     1069398 :     relation->rd_pddcxt = NULL;
    1199     1069398 :     relation->rd_partcheck = NIL;
    1200     1069398 :     relation->rd_partcheckvalid = false;
    1201     1069398 :     relation->rd_partcheckcxt = NULL;
    1202             : 
    1203             :     /*
    1204             :      * initialize access method information
    1205             :      */
    1206     1069398 :     if (relation->rd_rel->relkind == RELKIND_INDEX ||
    1207      704926 :         relation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX)
    1208      367254 :         RelationInitIndexAccessInfo(relation);
    1209      702144 :     else if (RELKIND_HAS_TABLE_AM(relation->rd_rel->relkind) ||
    1210      191740 :              relation->rd_rel->relkind == RELKIND_SEQUENCE)
    1211      513712 :         RelationInitTableAccessMethod(relation);
    1212             :     else
    1213             :         Assert(relation->rd_rel->relam == InvalidOid);
    1214             : 
    1215             :     /* extract reloptions if any */
    1216     1069390 :     RelationParseRelOptions(relation, pg_class_tuple);
    1217             : 
    1218             :     /*
    1219             :      * initialize the relation lock manager information
    1220             :      */
    1221     1069390 :     RelationInitLockInfo(relation); /* see lmgr.c */
    1222             : 
    1223             :     /*
    1224             :      * initialize physical addressing information for the relation
    1225             :      */
    1226     1069390 :     RelationInitPhysicalAddr(relation);
    1227             : 
    1228             :     /* make sure relation is marked as having no open file yet */
    1229     1069390 :     relation->rd_smgr = NULL;
    1230             : 
    1231             :     /*
    1232             :      * now we can free the memory allocated for pg_class_tuple
    1233             :      */
    1234     1069390 :     heap_freetuple(pg_class_tuple);
    1235             : 
    1236             :     /*
    1237             :      * If an invalidation arrived mid-build, start over.  Between here and the
    1238             :      * end of this function, don't add code that does or reasonably could read
    1239             :      * system catalogs.  That range must be free from invalidation processing
    1240             :      * for the !insertIt case.  For the insertIt case, RelationCacheInsert()
    1241             :      * will enroll this relation in ordinary relcache invalidation processing,
    1242             :      */
    1243     1069390 :     if (in_progress_list[in_progress_offset].invalidated)
    1244             :     {
    1245           2 :         RelationDestroyRelation(relation, false);
    1246           2 :         goto retry;
    1247             :     }
    1248             :     Assert(in_progress_offset + 1 == in_progress_list_len);
    1249     1069388 :     in_progress_list_len--;
    1250             : 
    1251             :     /*
    1252             :      * Insert newly created relation into relcache hash table, if requested.
    1253             :      *
    1254             :      * There is one scenario in which we might find a hashtable entry already
    1255             :      * present, even though our caller failed to find it: if the relation is a
    1256             :      * system catalog or index that's used during relcache load, we might have
    1257             :      * recursively created the same relcache entry during the preceding steps.
    1258             :      * So allow RelationCacheInsert to delete any already-present relcache
    1259             :      * entry for the same OID.  The already-present entry should have refcount
    1260             :      * zero (else somebody forgot to close it); in the event that it doesn't,
    1261             :      * we'll elog a WARNING and leak the already-present entry.
    1262             :      */
    1263     1069388 :     if (insertIt)
    1264      686214 :         RelationCacheInsert(relation, true);
    1265             : 
    1266             :     /* It's fully valid */
    1267     1069388 :     relation->rd_isvalid = true;
    1268             : 
    1269             : #ifdef MAYBE_RECOVER_RELATION_BUILD_MEMORY
    1270             :     if (tmpcxt)
    1271             :     {
    1272             :         /* Return to caller's context, and blow away the temporary context */
    1273             :         MemoryContextSwitchTo(oldcxt);
    1274             :         MemoryContextDelete(tmpcxt);
    1275             :     }
    1276             : #endif
    1277             : 
    1278     1069388 :     return relation;
    1279             : }
    1280             : 
    1281             : /*
    1282             :  * Initialize the physical addressing info (RelFileNode) for a relcache entry
    1283             :  *
    1284             :  * Note: at the physical level, relations in the pg_global tablespace must
    1285             :  * be treated as shared, even if relisshared isn't set.  Hence we do not
    1286             :  * look at relisshared here.
    1287             :  */
    1288             : static void
    1289     3711478 : RelationInitPhysicalAddr(Relation relation)
    1290             : {
    1291     3711478 :     Oid         oldnode = relation->rd_node.relNode;
    1292             : 
    1293             :     /* these relations kinds never have storage */
    1294     3711478 :     if (!RELKIND_HAS_STORAGE(relation->rd_rel->relkind))
    1295      265552 :         return;
    1296             : 
    1297     3445926 :     if (relation->rd_rel->reltablespace)
    1298      466998 :         relation->rd_node.spcNode = relation->rd_rel->reltablespace;
    1299             :     else
    1300     2978928 :         relation->rd_node.spcNode = MyDatabaseTableSpace;
    1301     3445926 :     if (relation->rd_node.spcNode == GLOBALTABLESPACE_OID)
    1302      465100 :         relation->rd_node.dbNode = InvalidOid;
    1303             :     else
    1304     2980826 :         relation->rd_node.dbNode = MyDatabaseId;
    1305             : 
    1306     3445926 :     if (relation->rd_rel->relfilenode)
    1307             :     {
    1308             :         /*
    1309             :          * Even if we are using a decoding snapshot that doesn't represent the
    1310             :          * current state of the catalog we need to make sure the filenode
    1311             :          * points to the current file since the older file will be gone (or
    1312             :          * truncated). The new file will still contain older rows so lookups
    1313             :          * in them will work correctly. This wouldn't work correctly if
    1314             :          * rewrites were allowed to change the schema in an incompatible way,
    1315             :          * but those are prevented both on catalog tables and on user tables
    1316             :          * declared as additional catalog tables.
    1317             :          */
    1318     2164172 :         if (HistoricSnapshotActive()
    1319        2876 :             && RelationIsAccessibleInLogicalDecoding(relation)
    1320        2024 :             && IsTransactionState())
    1321             :         {
    1322             :             HeapTuple   phys_tuple;
    1323             :             Form_pg_class physrel;
    1324             : 
    1325        2024 :             phys_tuple = ScanPgRelation(RelationGetRelid(relation),
    1326        2024 :                                         RelationGetRelid(relation) != ClassOidIndexId,
    1327             :                                         true);
    1328        2024 :             if (!HeapTupleIsValid(phys_tuple))
    1329           0 :                 elog(ERROR, "could not find pg_class entry for %u",
    1330             :                      RelationGetRelid(relation));
    1331        2024 :             physrel = (Form_pg_class) GETSTRUCT(phys_tuple);
    1332             : 
    1333        2024 :             relation->rd_rel->reltablespace = physrel->reltablespace;
    1334        2024 :             relation->rd_rel->relfilenode = physrel->relfilenode;
    1335        2024 :             heap_freetuple(phys_tuple);
    1336             :         }
    1337             : 
    1338     2164172 :         relation->rd_node.relNode = relation->rd_rel->relfilenode;
    1339             :     }
    1340             :     else
    1341             :     {
    1342             :         /* Consult the relation mapper */
    1343     1281754 :         relation->rd_node.relNode =
    1344     1281754 :             RelationMapOidToFilenode(relation->rd_id,
    1345     1281754 :                                      relation->rd_rel->relisshared);
    1346     1281754 :         if (!OidIsValid(relation->rd_node.relNode))
    1347           0 :             elog(ERROR, "could not find relation mapping for relation \"%s\", OID %u",
    1348             :                  RelationGetRelationName(relation), relation->rd_id);
    1349             :     }
    1350             : 
    1351             :     /*
    1352             :      * For RelationNeedsWAL() to answer correctly on parallel workers, restore
    1353             :      * rd_firstRelfilenodeSubid.  No subtransactions start or end while in
    1354             :      * parallel mode, so the specific SubTransactionId does not matter.
    1355             :      */
    1356     3445926 :     if (IsParallelWorker() && oldnode != relation->rd_node.relNode)
    1357             :     {
    1358       24046 :         if (RelFileNodeSkippingWAL(relation->rd_node))
    1359           2 :             relation->rd_firstRelfilenodeSubid = TopSubTransactionId;
    1360             :         else
    1361       24044 :             relation->rd_firstRelfilenodeSubid = InvalidSubTransactionId;
    1362             :     }
    1363             : }
    1364             : 
    1365             : /*
    1366             :  * Fill in the IndexAmRoutine for an index relation.
    1367             :  *
    1368             :  * relation's rd_amhandler and rd_indexcxt must be valid already.
    1369             :  */
    1370             : static void
    1371     1439612 : InitIndexAmRoutine(Relation relation)
    1372             : {
    1373             :     IndexAmRoutine *cached,
    1374             :                *tmp;
    1375             : 
    1376             :     /*
    1377             :      * Call the amhandler in current, short-lived memory context, just in case
    1378             :      * it leaks anything (it probably won't, but let's be paranoid).
    1379             :      */
    1380     1439612 :     tmp = GetIndexAmRoutine(relation->rd_amhandler);
    1381             : 
    1382             :     /* OK, now transfer the data into relation's rd_indexcxt. */
    1383     1439612 :     cached = (IndexAmRoutine *) MemoryContextAlloc(relation->rd_indexcxt,
    1384             :                                                    sizeof(IndexAmRoutine));
    1385     1439612 :     memcpy(cached, tmp, sizeof(IndexAmRoutine));
    1386     1439612 :     relation->rd_indam = cached;
    1387             : 
    1388     1439612 :     pfree(tmp);
    1389     1439612 : }
    1390             : 
    1391             : /*
    1392             :  * Initialize index-access-method support data for an index relation
    1393             :  */
    1394             : void
    1395      441000 : RelationInitIndexAccessInfo(Relation relation)
    1396             : {
    1397             :     HeapTuple   tuple;
    1398             :     Form_pg_am  aform;
    1399             :     Datum       indcollDatum;
    1400             :     Datum       indclassDatum;
    1401             :     Datum       indoptionDatum;
    1402             :     bool        isnull;
    1403             :     oidvector  *indcoll;
    1404             :     oidvector  *indclass;
    1405             :     int2vector *indoption;
    1406             :     MemoryContext indexcxt;
    1407             :     MemoryContext oldcontext;
    1408             :     int         indnatts;
    1409             :     int         indnkeyatts;
    1410             :     uint16      amsupport;
    1411             : 
    1412             :     /*
    1413             :      * Make a copy of the pg_index entry for the index.  Since pg_index
    1414             :      * contains variable-length and possibly-null fields, we have to do this
    1415             :      * honestly rather than just treating it as a Form_pg_index struct.
    1416             :      */
    1417      441000 :     tuple = SearchSysCache1(INDEXRELID,
    1418      441000 :                             ObjectIdGetDatum(RelationGetRelid(relation)));
    1419      441000 :     if (!HeapTupleIsValid(tuple))
    1420           0 :         elog(ERROR, "cache lookup failed for index %u",
    1421             :              RelationGetRelid(relation));
    1422      441000 :     oldcontext = MemoryContextSwitchTo(CacheMemoryContext);
    1423      441000 :     relation->rd_indextuple = heap_copytuple(tuple);
    1424      441000 :     relation->rd_index = (Form_pg_index) GETSTRUCT(relation->rd_indextuple);
    1425      441000 :     MemoryContextSwitchTo(oldcontext);
    1426      441000 :     ReleaseSysCache(tuple);
    1427             : 
    1428             :     /*
    1429             :      * Look up the index's access method, save the OID of its handler function
    1430             :      */
    1431             :     Assert(relation->rd_rel->relam != InvalidOid);
    1432      441000 :     tuple = SearchSysCache1(AMOID, ObjectIdGetDatum(relation->rd_rel->relam));
    1433      440996 :     if (!HeapTupleIsValid(tuple))
    1434           0 :         elog(ERROR, "cache lookup failed for access method %u",
    1435             :              relation->rd_rel->relam);
    1436      440996 :     aform = (Form_pg_am) GETSTRUCT(tuple);
    1437      440996 :     relation->rd_amhandler = aform->amhandler;
    1438      440996 :     ReleaseSysCache(tuple);
    1439             : 
    1440      440996 :     indnatts = RelationGetNumberOfAttributes(relation);
    1441      440996 :     if (indnatts != IndexRelationGetNumberOfAttributes(relation))
    1442           0 :         elog(ERROR, "relnatts disagrees with indnatts for index %u",
    1443             :              RelationGetRelid(relation));
    1444      440996 :     indnkeyatts = IndexRelationGetNumberOfKeyAttributes(relation);
    1445             : 
    1446             :     /*
    1447             :      * Make the private context to hold index access info.  The reason we need
    1448             :      * a context, and not just a couple of pallocs, is so that we won't leak
    1449             :      * any subsidiary info attached to fmgr lookup records.
    1450             :      */
    1451      440996 :     indexcxt = AllocSetContextCreate(CacheMemoryContext,
    1452             :                                      "index info",
    1453             :                                      ALLOCSET_SMALL_SIZES);
    1454      440996 :     relation->rd_indexcxt = indexcxt;
    1455      440996 :     MemoryContextCopyAndSetIdentifier(indexcxt,
    1456             :                                       RelationGetRelationName(relation));
    1457             : 
    1458             :     /*
    1459             :      * Now we can fetch the index AM's API struct
    1460             :      */
    1461      440996 :     InitIndexAmRoutine(relation);
    1462             : 
    1463             :     /*
    1464             :      * Allocate arrays to hold data. Opclasses are not used for included
    1465             :      * columns, so allocate them for indnkeyatts only.
    1466             :      */
    1467      440996 :     relation->rd_opfamily = (Oid *)
    1468      440996 :         MemoryContextAllocZero(indexcxt, indnkeyatts * sizeof(Oid));
    1469      440996 :     relation->rd_opcintype = (Oid *)
    1470      440996 :         MemoryContextAllocZero(indexcxt, indnkeyatts * sizeof(Oid));
    1471             : 
    1472      440996 :     amsupport = relation->rd_indam->amsupport;
    1473      440996 :     if (amsupport > 0)
    1474             :     {
    1475      440996 :         int         nsupport = indnatts * amsupport;
    1476             : 
    1477      440996 :         relation->rd_support = (RegProcedure *)
    1478      440996 :             MemoryContextAllocZero(indexcxt, nsupport * sizeof(RegProcedure));
    1479      440996 :         relation->rd_supportinfo = (FmgrInfo *)
    1480      440996 :             MemoryContextAllocZero(indexcxt, nsupport * sizeof(FmgrInfo));
    1481             :     }
    1482             :     else
    1483             :     {
    1484           0 :         relation->rd_support = NULL;
    1485           0 :         relation->rd_supportinfo = NULL;
    1486             :     }
    1487             : 
    1488      440996 :     relation->rd_indcollation = (Oid *)
    1489      440996 :         MemoryContextAllocZero(indexcxt, indnkeyatts * sizeof(Oid));
    1490             : 
    1491      440996 :     relation->rd_indoption = (int16 *)
    1492      440996 :         MemoryContextAllocZero(indexcxt, indnkeyatts * sizeof(int16));
    1493             : 
    1494             :     /*
    1495             :      * indcollation cannot be referenced directly through the C struct,
    1496             :      * because it comes after the variable-width indkey field.  Must extract
    1497             :      * the datum the hard way...
    1498             :      */
    1499      440996 :     indcollDatum = fastgetattr(relation->rd_indextuple,
    1500             :                                Anum_pg_index_indcollation,
    1501             :                                GetPgIndexDescriptor(),
    1502             :                                &isnull);
    1503             :     Assert(!isnull);
    1504      440996 :     indcoll = (oidvector *) DatumGetPointer(indcollDatum);
    1505      440996 :     memcpy(relation->rd_indcollation, indcoll->values, indnkeyatts * sizeof(Oid));
    1506             : 
    1507             :     /*
    1508             :      * indclass cannot be referenced directly through the C struct, because it
    1509             :      * comes after the variable-width indkey field.  Must extract the datum
    1510             :      * the hard way...
    1511             :      */
    1512      440996 :     indclassDatum = fastgetattr(relation->rd_indextuple,
    1513             :                                 Anum_pg_index_indclass,
    1514             :                                 GetPgIndexDescriptor(),
    1515             :                                 &isnull);
    1516             :     Assert(!isnull);
    1517      440996 :     indclass = (oidvector *) DatumGetPointer(indclassDatum);
    1518             : 
    1519             :     /*
    1520             :      * Fill the support procedure OID array, as well as the info about
    1521             :      * opfamilies and opclass input types.  (aminfo and supportinfo are left
    1522             :      * as zeroes, and are filled on-the-fly when used)
    1523             :      */
    1524      440996 :     IndexSupportInitialize(indclass, relation->rd_support,
    1525             :                            relation->rd_opfamily, relation->rd_opcintype,
    1526             :                            amsupport, indnkeyatts);
    1527             : 
    1528             :     /*
    1529             :      * Similarly extract indoption and copy it to the cache entry
    1530             :      */
    1531      440996 :     indoptionDatum = fastgetattr(relation->rd_indextuple,
    1532             :                                  Anum_pg_index_indoption,
    1533             :                                  GetPgIndexDescriptor(),
    1534             :                                  &isnull);
    1535             :     Assert(!isnull);
    1536      440996 :     indoption = (int2vector *) DatumGetPointer(indoptionDatum);
    1537      440996 :     memcpy(relation->rd_indoption, indoption->values, indnkeyatts * sizeof(int16));
    1538             : 
    1539      440996 :     (void) RelationGetIndexAttOptions(relation, false);
    1540             : 
    1541             :     /*
    1542             :      * expressions, predicate, exclusion caches will be filled later
    1543             :      */
    1544      440992 :     relation->rd_indexprs = NIL;
    1545      440992 :     relation->rd_indpred = NIL;
    1546      440992 :     relation->rd_exclops = NULL;
    1547      440992 :     relation->rd_exclprocs = NULL;
    1548      440992 :     relation->rd_exclstrats = NULL;
    1549      440992 :     relation->rd_amcache = NULL;
    1550      440992 : }
    1551             : 
    1552             : /*
    1553             :  * IndexSupportInitialize
    1554             :  *      Initializes an index's cached opclass information,
    1555             :  *      given the index's pg_index.indclass entry.
    1556             :  *
    1557             :  * Data is returned into *indexSupport, *opFamily, and *opcInType,
    1558             :  * which are arrays allocated by the caller.
    1559             :  *
    1560             :  * The caller also passes maxSupportNumber and maxAttributeNumber, since these
    1561             :  * indicate the size of the arrays it has allocated --- but in practice these
    1562             :  * numbers must always match those obtainable from the system catalog entries
    1563             :  * for the index and access method.
    1564             :  */
    1565             : static void
    1566      440996 : IndexSupportInitialize(oidvector *indclass,
    1567             :                        RegProcedure *indexSupport,
    1568             :                        Oid *opFamily,
    1569             :                        Oid *opcInType,
    1570             :                        StrategyNumber maxSupportNumber,
    1571             :                        AttrNumber maxAttributeNumber)
    1572             : {
    1573             :     int         attIndex;
    1574             : 
    1575     1181848 :     for (attIndex = 0; attIndex < maxAttributeNumber; attIndex++)
    1576             :     {
    1577             :         OpClassCacheEnt *opcentry;
    1578             : 
    1579      740852 :         if (!OidIsValid(indclass->values[attIndex]))
    1580           0 :             elog(ERROR, "bogus pg_index tuple");
    1581             : 
    1582             :         /* look up the info for this opclass, using a cache */
    1583      740852 :         opcentry = LookupOpclassInfo(indclass->values[attIndex],
    1584             :                                      maxSupportNumber);
    1585             : 
    1586             :         /* copy cached data into relcache entry */
    1587      740852 :         opFamily[attIndex] = opcentry->opcfamily;
    1588      740852 :         opcInType[attIndex] = opcentry->opcintype;
    1589      740852 :         if (maxSupportNumber > 0)
    1590      740852 :             memcpy(&indexSupport[attIndex * maxSupportNumber],
    1591      740852 :                    opcentry->supportProcs,
    1592             :                    maxSupportNumber * sizeof(RegProcedure));
    1593             :     }
    1594      440996 : }
    1595             : 
    1596             : /*
    1597             :  * LookupOpclassInfo
    1598             :  *
    1599             :  * This routine maintains a per-opclass cache of the information needed
    1600             :  * by IndexSupportInitialize().  This is more efficient than relying on
    1601             :  * the catalog cache, because we can load all the info about a particular
    1602             :  * opclass in a single indexscan of pg_amproc.
    1603             :  *
    1604             :  * The information from pg_am about expected range of support function
    1605             :  * numbers is passed in, rather than being looked up, mainly because the
    1606             :  * caller will have it already.
    1607             :  *
    1608             :  * Note there is no provision for flushing the cache.  This is OK at the
    1609             :  * moment because there is no way to ALTER any interesting properties of an
    1610             :  * existing opclass --- all you can do is drop it, which will result in
    1611             :  * a useless but harmless dead entry in the cache.  To support altering
    1612             :  * opclass membership (not the same as opfamily membership!), we'd need to
    1613             :  * be able to flush this cache as well as the contents of relcache entries
    1614             :  * for indexes.
    1615             :  */
    1616             : static OpClassCacheEnt *
    1617      740852 : LookupOpclassInfo(Oid operatorClassOid,
    1618             :                   StrategyNumber numSupport)
    1619             : {
    1620             :     OpClassCacheEnt *opcentry;
    1621             :     bool        found;
    1622             :     Relation    rel;
    1623             :     SysScanDesc scan;
    1624             :     ScanKeyData skey[3];
    1625             :     HeapTuple   htup;
    1626             :     bool        indexOK;
    1627             : 
    1628      740852 :     if (OpClassCache == NULL)
    1629             :     {
    1630             :         /* First time through: initialize the opclass cache */
    1631             :         HASHCTL     ctl;
    1632             : 
    1633             :         /* Also make sure CacheMemoryContext exists */
    1634       14378 :         if (!CacheMemoryContext)
    1635           0 :             CreateCacheMemoryContext();
    1636             : 
    1637       14378 :         ctl.keysize = sizeof(Oid);
    1638       14378 :         ctl.entrysize = sizeof(OpClassCacheEnt);
    1639       14378 :         OpClassCache = hash_create("Operator class cache", 64,
    1640             :                                    &ctl, HASH_ELEM | HASH_BLOBS);
    1641             :     }
    1642             : 
    1643      740852 :     opcentry = (OpClassCacheEnt *) hash_search(OpClassCache,
    1644             :                                                (void *) &operatorClassOid,
    1645             :                                                HASH_ENTER, &found);
    1646             : 
    1647      740852 :     if (!found)
    1648             :     {
    1649             :         /* Initialize new entry */
    1650       46672 :         opcentry->valid = false; /* until known OK */
    1651       46672 :         opcentry->numSupport = numSupport;
    1652       46672 :         opcentry->supportProcs = NULL;   /* filled below */
    1653             :     }
    1654             :     else
    1655             :     {
    1656             :         Assert(numSupport == opcentry->numSupport);
    1657             :     }
    1658             : 
    1659             :     /*
    1660             :      * When aggressively testing cache-flush hazards, we disable the operator
    1661             :      * class cache and force reloading of the info on each call.  This models
    1662             :      * no real-world behavior, since the cache entries are never invalidated
    1663             :      * otherwise.  However it can be helpful for detecting bugs in the cache
    1664             :      * loading logic itself, such as reliance on a non-nailed index.  Given
    1665             :      * the limited use-case and the fact that this adds a great deal of
    1666             :      * expense, we enable it only for high values of debug_discard_caches.
    1667             :      */
    1668             : #ifdef DISCARD_CACHES_ENABLED
    1669             :     if (debug_discard_caches > 2)
    1670             :         opcentry->valid = false;
    1671             : #endif
    1672             : 
    1673      740852 :     if (opcentry->valid)
    1674      694180 :         return opcentry;
    1675             : 
    1676             :     /*
    1677             :      * Need to fill in new entry.  First allocate space, unless we already did
    1678             :      * so in some previous attempt.
    1679             :      */
    1680       46672 :     if (opcentry->supportProcs == NULL && numSupport > 0)
    1681       46672 :         opcentry->supportProcs = (RegProcedure *)
    1682       46672 :             MemoryContextAllocZero(CacheMemoryContext,
    1683             :                                    numSupport * sizeof(RegProcedure));
    1684             : 
    1685             :     /*
    1686             :      * To avoid infinite recursion during startup, force heap scans if we're
    1687             :      * looking up info for the opclasses used by the indexes we would like to
    1688             :      * reference here.
    1689             :      */
    1690       56488 :     indexOK = criticalRelcachesBuilt ||
    1691        9816 :         (operatorClassOid != OID_BTREE_OPS_OID &&
    1692        7508 :          operatorClassOid != INT2_BTREE_OPS_OID);
    1693             : 
    1694             :     /*
    1695             :      * We have to fetch the pg_opclass row to determine its opfamily and
    1696             :      * opcintype, which are needed to look up related operators and functions.
    1697             :      * It'd be convenient to use the syscache here, but that probably doesn't
    1698             :      * work while bootstrapping.
    1699             :      */
    1700       46672 :     ScanKeyInit(&skey[0],
    1701             :                 Anum_pg_opclass_oid,
    1702             :                 BTEqualStrategyNumber, F_OIDEQ,
    1703             :                 ObjectIdGetDatum(operatorClassOid));
    1704       46672 :     rel = table_open(OperatorClassRelationId, AccessShareLock);
    1705       46672 :     scan = systable_beginscan(rel, OpclassOidIndexId, indexOK,
    1706             :                               NULL, 1, skey);
    1707             : 
    1708       46672 :     if (HeapTupleIsValid(htup = systable_getnext(scan)))
    1709             :     {
    1710       46672 :         Form_pg_opclass opclassform = (Form_pg_opclass) GETSTRUCT(htup);
    1711             : 
    1712       46672 :         opcentry->opcfamily = opclassform->opcfamily;
    1713       46672 :         opcentry->opcintype = opclassform->opcintype;
    1714             :     }
    1715             :     else
    1716           0 :         elog(ERROR, "could not find tuple for opclass %u", operatorClassOid);
    1717             : 
    1718       46672 :     systable_endscan(scan);
    1719       46672 :     table_close(rel, AccessShareLock);
    1720             : 
    1721             :     /*
    1722             :      * Scan pg_amproc to obtain support procs for the opclass.  We only fetch
    1723             :      * the default ones (those with lefttype = righttype = opcintype).
    1724             :      */
    1725       46672 :     if (numSupport > 0)
    1726             :     {
    1727       46672 :         ScanKeyInit(&skey[0],
    1728             :                     Anum_pg_amproc_amprocfamily,
    1729             :                     BTEqualStrategyNumber, F_OIDEQ,
    1730       46672 :                     ObjectIdGetDatum(opcentry->opcfamily));
    1731       46672 :         ScanKeyInit(&skey[1],
    1732             :                     Anum_pg_amproc_amproclefttype,
    1733             :                     BTEqualStrategyNumber, F_OIDEQ,
    1734       46672 :                     ObjectIdGetDatum(opcentry->opcintype));
    1735       46672 :         ScanKeyInit(&skey[2],
    1736             :                     Anum_pg_amproc_amprocrighttype,
    1737             :                     BTEqualStrategyNumber, F_OIDEQ,
    1738       46672 :                     ObjectIdGetDatum(opcentry->opcintype));
    1739       46672 :         rel = table_open(AccessMethodProcedureRelationId, AccessShareLock);
    1740       46672 :         scan = systable_beginscan(rel, AccessMethodProcedureIndexId, indexOK,
    1741             :                                   NULL, 3, skey);
    1742             : 
    1743      191576 :         while (HeapTupleIsValid(htup = systable_getnext(scan)))
    1744             :         {
    1745      144904 :             Form_pg_amproc amprocform = (Form_pg_amproc) GETSTRUCT(htup);
    1746             : 
    1747      144904 :             if (amprocform->amprocnum <= 0 ||
    1748      144904 :                 (StrategyNumber) amprocform->amprocnum > numSupport)
    1749           0 :                 elog(ERROR, "invalid amproc number %d for opclass %u",
    1750             :                      amprocform->amprocnum, operatorClassOid);
    1751             : 
    1752      144904 :             opcentry->supportProcs[amprocform->amprocnum - 1] =
    1753      144904 :                 amprocform->amproc;
    1754             :         }
    1755             : 
    1756       46672 :         systable_endscan(scan);
    1757       46672 :         table_close(rel, AccessShareLock);
    1758             :     }
    1759             : 
    1760       46672 :     opcentry->valid = true;
    1761       46672 :     return opcentry;
    1762             : }
    1763             : 
    1764             : /*
    1765             :  * Fill in the TableAmRoutine for a relation
    1766             :  *
    1767             :  * relation's rd_amhandler must be valid already.
    1768             :  */
    1769             : static void
    1770     1190914 : InitTableAmRoutine(Relation relation)
    1771             : {
    1772     1190914 :     relation->rd_tableam = GetTableAmRoutine(relation->rd_amhandler);
    1773     1190914 : }
    1774             : 
    1775             : /*
    1776             :  * Initialize table access method support for a table like relation
    1777             :  */
    1778             : void
    1779     1190914 : RelationInitTableAccessMethod(Relation relation)
    1780             : {
    1781             :     HeapTuple   tuple;
    1782             :     Form_pg_am  aform;
    1783             : 
    1784     1190914 :     if (relation->rd_rel->relkind == RELKIND_SEQUENCE)
    1785             :     {
    1786             :         /*
    1787             :          * Sequences are currently accessed like heap tables, but it doesn't
    1788             :          * seem prudent to show that in the catalog. So just overwrite it
    1789             :          * here.
    1790             :          */
    1791             :         Assert(relation->rd_rel->relam == InvalidOid);
    1792        4430 :         relation->rd_amhandler = F_HEAP_TABLEAM_HANDLER;
    1793             :     }
    1794     1186484 :     else if (IsCatalogRelation(relation))
    1795             :     {
    1796             :         /*
    1797             :          * Avoid doing a syscache lookup for catalog tables.
    1798             :          */
    1799             :         Assert(relation->rd_rel->relam == HEAP_TABLE_AM_OID);
    1800      936904 :         relation->rd_amhandler = F_HEAP_TABLEAM_HANDLER;
    1801             :     }
    1802             :     else
    1803             :     {
    1804             :         /*
    1805             :          * Look up the table access method, save the OID of its handler
    1806             :          * function.
    1807             :          */
    1808             :         Assert(relation->rd_rel->relam != InvalidOid);
    1809      249580 :         tuple = SearchSysCache1(AMOID,
    1810      249580 :                                 ObjectIdGetDatum(relation->rd_rel->relam));
    1811      249580 :         if (!HeapTupleIsValid(tuple))
    1812           0 :             elog(ERROR, "cache lookup failed for access method %u",
    1813             :                  relation->rd_rel->relam);
    1814      249580 :         aform = (Form_pg_am) GETSTRUCT(tuple);
    1815      249580 :         relation->rd_amhandler = aform->amhandler;
    1816      249580 :         ReleaseSysCache(tuple);
    1817             :     }
    1818             : 
    1819             :     /*
    1820             :      * Now we can fetch the table AM's API struct
    1821             :      */
    1822     1190914 :     InitTableAmRoutine(relation);
    1823     1190914 : }
    1824             : 
    1825             : /*
    1826             :  *      formrdesc
    1827             :  *
    1828             :  *      This is a special cut-down version of RelationBuildDesc(),
    1829             :  *      used while initializing the relcache.
    1830             :  *      The relation descriptor is built just from the supplied parameters,
    1831             :  *      without actually looking at any system table entries.  We cheat
    1832             :  *      quite a lot since we only need to work for a few basic system
    1833             :  *      catalogs.
    1834             :  *
    1835             :  * The catalogs this is used for can't have constraints (except attnotnull),
    1836             :  * default values, rules, or triggers, since we don't cope with any of that.
    1837             :  * (Well, actually, this only matters for properties that need to be valid
    1838             :  * during bootstrap or before RelationCacheInitializePhase3 runs, and none of
    1839             :  * these properties matter then...)
    1840             :  *
    1841             :  * NOTE: we assume we are already switched into CacheMemoryContext.
    1842             :  */
    1843             : static void
    1844       22480 : formrdesc(const char *relationName, Oid relationReltype,
    1845             :           bool isshared,
    1846             :           int natts, const FormData_pg_attribute *attrs)
    1847             : {
    1848             :     Relation    relation;
    1849             :     int         i;
    1850             :     bool        has_not_null;
    1851             : 
    1852             :     /*
    1853             :      * allocate new relation desc, clear all fields of reldesc
    1854             :      */
    1855       22480 :     relation = (Relation) palloc0(sizeof(RelationData));
    1856             : 
    1857             :     /* make sure relation is marked as having no open file yet */
    1858       22480 :     relation->rd_smgr = NULL;
    1859             : 
    1860             :     /*
    1861             :      * initialize reference count: 1 because it is nailed in cache
    1862             :      */
    1863       22480 :     relation->rd_refcnt = 1;
    1864             : 
    1865             :     /*
    1866             :      * all entries built with this routine are nailed-in-cache; none are for
    1867             :      * new or temp relations.
    1868             :      */
    1869       22480 :     relation->rd_isnailed = true;
    1870       22480 :     relation->rd_createSubid = InvalidSubTransactionId;
    1871       22480 :     relation->rd_newRelfilenodeSubid = InvalidSubTransactionId;
    1872       22480 :     relation->rd_firstRelfilenodeSubid = InvalidSubTransactionId;
    1873       22480 :     relation->rd_droppedSubid = InvalidSubTransactionId;
    1874       22480 :     relation->rd_backend = InvalidBackendId;
    1875       22480 :     relation->rd_islocaltemp = false;
    1876             : 
    1877             :     /*
    1878             :      * initialize relation tuple form
    1879             :      *
    1880             :      * The data we insert here is pretty incomplete/bogus, but it'll serve to
    1881             :      * get us launched.  RelationCacheInitializePhase3() will read the real
    1882             :      * data from pg_class and replace what we've done here.  Note in
    1883             :      * particular that relowner is left as zero; this cues
    1884             :      * RelationCacheInitializePhase3 that the real data isn't there yet.
    1885             :      */
    1886       22480 :     relation->rd_rel = (Form_pg_class) palloc0(CLASS_TUPLE_SIZE);
    1887             : 
    1888       22480 :     namestrcpy(&relation->rd_rel->relname, relationName);
    1889       22480 :     relation->rd_rel->relnamespace = PG_CATALOG_NAMESPACE;
    1890       22480 :     relation->rd_rel->reltype = relationReltype;
    1891             : 
    1892             :     /*
    1893             :      * It's important to distinguish between shared and non-shared relations,
    1894             :      * even at bootstrap time, to make sure we know where they are stored.
    1895             :      */
    1896       22480 :     relation->rd_rel->relisshared = isshared;
    1897       22480 :     if (isshared)
    1898       13240 :         relation->rd_rel->reltablespace = GLOBALTABLESPACE_OID;
    1899             : 
    1900             :     /* formrdesc is used only for permanent relations */
    1901       22480 :     relation->rd_rel->relpersistence = RELPERSISTENCE_PERMANENT;
    1902             : 
    1903             :     /* ... and they're always populated, too */
    1904       22480 :     relation->rd_rel->relispopulated = true;
    1905             : 
    1906       22480 :     relation->rd_rel->relreplident = REPLICA_IDENTITY_NOTHING;
    1907       22480 :     relation->rd_rel->relpages = 0;
    1908       22480 :     relation->rd_rel->reltuples = -1;
    1909       22480 :     relation->rd_rel->relallvisible = 0;
    1910       22480 :     relation->rd_rel->relkind = RELKIND_RELATION;
    1911       22480 :     relation->rd_rel->relnatts = (int16) natts;
    1912       22480 :     relation->rd_rel->relam = HEAP_TABLE_AM_OID;
    1913             : 
    1914             :     /*
    1915             :      * initialize attribute tuple form
    1916             :      *
    1917             :      * Unlike the case with the relation tuple, this data had better be right
    1918             :      * because it will never be replaced.  The data comes from
    1919             :      * src/include/catalog/ headers via genbki.pl.
    1920             :      */
    1921       22480 :     relation->rd_att = CreateTemplateTupleDesc(natts);
    1922       22480 :     relation->rd_att->tdrefcount = 1; /* mark as refcounted */
    1923             : 
    1924       22480 :     relation->rd_att->tdtypeid = relationReltype;
    1925       22480 :     relation->rd_att->tdtypmod = -1;  /* just to be sure */
    1926             : 
    1927             :     /*
    1928             :      * initialize tuple desc info
    1929             :      */
    1930       22480 :     has_not_null = false;
    1931      423798 :     for (i = 0; i < natts; i++)
    1932             :     {
    1933      401318 :         memcpy(TupleDescAttr(relation->rd_att, i),
    1934      401318 :                &attrs[i],
    1935             :                ATTRIBUTE_FIXED_PART_SIZE);
    1936      401318 :         has_not_null |= attrs[i].attnotnull;
    1937             :         /* make sure attcacheoff is valid */
    1938      401318 :         TupleDescAttr(relation->rd_att, i)->attcacheoff = -1;
    1939             :     }
    1940             : 
    1941             :     /* initialize first attribute's attcacheoff, cf RelationBuildTupleDesc */
    1942       22480 :     TupleDescAttr(relation->rd_att, 0)->attcacheoff = 0;
    1943             : 
    1944             :     /* mark not-null status */
    1945       22480 :     if (has_not_null)
    1946             :     {
    1947       22480 :         TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
    1948             : 
    1949       22480 :         constr->has_not_null = true;
    1950       22480 :         relation->rd_att->constr = constr;
    1951             :     }
    1952             : 
    1953             :     /*
    1954             :      * initialize relation id from info in att array (my, this is ugly)
    1955             :      */
    1956       22480 :     RelationGetRelid(relation) = TupleDescAttr(relation->rd_att, 0)->attrelid;
    1957             : 
    1958             :     /*
    1959             :      * All relations made with formrdesc are mapped.  This is necessarily so
    1960             :      * because there is no other way to know what filenode they currently
    1961             :      * have.  In bootstrap mode, add them to the initial relation mapper data,
    1962             :      * specifying that the initial filenode is the same as the OID.
    1963             :      */
    1964       22480 :     relation->rd_rel->relfilenode = InvalidOid;
    1965       22480 :     if (IsBootstrapProcessingMode())
    1966        1928 :         RelationMapUpdateMap(RelationGetRelid(relation),
    1967             :                              RelationGetRelid(relation),
    1968             :                              isshared, true);
    1969             : 
    1970             :     /*
    1971             :      * initialize the relation lock manager information
    1972             :      */
    1973       22480 :     RelationInitLockInfo(relation); /* see lmgr.c */
    1974             : 
    1975             :     /*
    1976             :      * initialize physical addressing information for the relation
    1977             :      */
    1978       22480 :     RelationInitPhysicalAddr(relation);
    1979             : 
    1980             :     /*
    1981             :      * initialize the table am handler
    1982             :      */
    1983       22480 :     relation->rd_rel->relam = HEAP_TABLE_AM_OID;
    1984       22480 :     relation->rd_tableam = GetHeapamTableAmRoutine();
    1985             : 
    1986             :     /*
    1987             :      * initialize the rel-has-index flag, using hardwired knowledge
    1988             :      */
    1989       22480 :     if (IsBootstrapProcessingMode())
    1990             :     {
    1991             :         /* In bootstrap mode, we have no indexes */
    1992        1928 :         relation->rd_rel->relhasindex = false;
    1993             :     }
    1994             :     else
    1995             :     {
    1996             :         /* Otherwise, all the rels formrdesc is used for have indexes */
    1997       20552 :         relation->rd_rel->relhasindex = true;
    1998             :     }
    1999             : 
    2000             :     /*
    2001             :      * add new reldesc to relcache
    2002             :      */
    2003       22480 :     RelationCacheInsert(relation, false);
    2004             : 
    2005             :     /* It's fully valid */
    2006       22480 :     relation->rd_isvalid = true;
    2007       22480 : }
    2008             : 
    2009             : 
    2010             : /* ----------------------------------------------------------------
    2011             :  *               Relation Descriptor Lookup Interface
    2012             :  * ----------------------------------------------------------------
    2013             :  */
    2014             : 
    2015             : /*
    2016             :  *      RelationIdGetRelation
    2017             :  *
    2018             :  *      Lookup a reldesc by OID; make one if not already in cache.
    2019             :  *
    2020             :  *      Returns NULL if no pg_class row could be found for the given relid
    2021             :  *      (suggesting we are trying to access a just-deleted relation).
    2022             :  *      Any other error is reported via elog.
    2023             :  *
    2024             :  *      NB: caller should already have at least AccessShareLock on the
    2025             :  *      relation ID, else there are nasty race conditions.
    2026             :  *
    2027             :  *      NB: relation ref count is incremented, or set to 1 if new entry.
    2028             :  *      Caller should eventually decrement count.  (Usually,
    2029             :  *      that happens by calling RelationClose().)
    2030             :  */
    2031             : Relation
    2032    36835166 : RelationIdGetRelation(Oid relationId)
    2033             : {
    2034             :     Relation    rd;
    2035             : 
    2036             :     /* Make sure we're in an xact, even if this ends up being a cache hit */
    2037             :     Assert(IsTransactionState());
    2038             : 
    2039             :     /*
    2040             :      * first try to find reldesc in the cache
    2041             :      */
    2042    36835166 :     RelationIdCacheLookup(relationId, rd);
    2043             : 
    2044    36835166 :     if (RelationIsValid(rd))
    2045             :     {
    2046             :         /* return NULL for dropped relations */
    2047    36170664 :         if (rd->rd_droppedSubid != InvalidSubTransactionId)
    2048             :         {
    2049             :             Assert(!rd->rd_isvalid);
    2050           4 :             return NULL;
    2051             :         }
    2052             : 
    2053    36170660 :         RelationIncrementReferenceCount(rd);
    2054             :         /* revalidate cache entry if necessary */
    2055    36170660 :         if (!rd->rd_isvalid)
    2056             :         {
    2057             :             /*
    2058             :              * Indexes only have a limited number of possible schema changes,
    2059             :              * and we don't want to use the full-blown procedure because it's
    2060             :              * a headache for indexes that reload itself depends on.
    2061             :              */
    2062      595280 :             if (rd->rd_rel->relkind == RELKIND_INDEX ||
    2063      575966 :                 rd->rd_rel->relkind == RELKIND_PARTITIONED_INDEX)
    2064       19314 :                 RelationReloadIndexInfo(rd);
    2065             :             else
    2066      575966 :                 RelationClearRelation(rd, true);
    2067             : 
    2068             :             /*
    2069             :              * Normally entries need to be valid here, but before the relcache
    2070             :              * has been initialized, not enough infrastructure exists to
    2071             :              * perform pg_class lookups. The structure of such entries doesn't
    2072             :              * change, but we still want to update the rd_rel entry. So
    2073             :              * rd_isvalid = false is left in place for a later lookup.
    2074             :              */
    2075             :             Assert(rd->rd_isvalid ||
    2076             :                    (rd->rd_isnailed && !criticalRelcachesBuilt));
    2077             :         }
    2078    36170648 :         return rd;
    2079             :     }
    2080             : 
    2081             :     /*
    2082             :      * no reldesc in the cache, so have RelationBuildDesc() build one and add
    2083             :      * it.
    2084             :      */
    2085      664502 :     rd = RelationBuildDesc(relationId, true);
    2086      664500 :     if (RelationIsValid(rd))
    2087      664492 :         RelationIncrementReferenceCount(rd);
    2088      664500 :     return rd;
    2089             : }
    2090             : 
    2091             : /* ----------------------------------------------------------------
    2092             :  *              cache invalidation support routines
    2093             :  * ----------------------------------------------------------------
    2094             :  */
    2095             : 
    2096             : /*
    2097             :  * RelationIncrementReferenceCount
    2098             :  *      Increments relation reference count.
    2099             :  *
    2100             :  * Note: bootstrap mode has its own weird ideas about relation refcount
    2101             :  * behavior; we ought to fix it someday, but for now, just disable
    2102             :  * reference count ownership tracking in bootstrap mode.
    2103             :  */
    2104             : void
    2105    51353774 : RelationIncrementReferenceCount(Relation rel)
    2106             : {
    2107    51353774 :     ResourceOwnerEnlargeRelationRefs(CurrentResourceOwner);
    2108    51353774 :     rel->rd_refcnt += 1;
    2109    51353774 :     if (!IsBootstrapProcessingMode())
    2110    48706148 :         ResourceOwnerRememberRelationRef(CurrentResourceOwner, rel);
    2111    51353774 : }
    2112             : 
    2113             : /*
    2114             :  * RelationDecrementReferenceCount
    2115             :  *      Decrements relation reference count.
    2116             :  */
    2117             : void
    2118    51353774 : RelationDecrementReferenceCount(Relation rel)
    2119             : {
    2120             :     Assert(rel->rd_refcnt > 0);
    2121    51353774 :     rel->rd_refcnt -= 1;
    2122    51353774 :     if (!IsBootstrapProcessingMode())
    2123    48706148 :         ResourceOwnerForgetRelationRef(CurrentResourceOwner, rel);
    2124    51353774 : }
    2125             : 
    2126             : /*
    2127             :  * RelationClose - close an open relation
    2128             :  *
    2129             :  *  Actually, we just decrement the refcount.
    2130             :  *
    2131             :  *  NOTE: if compiled with -DRELCACHE_FORCE_RELEASE then relcache entries
    2132             :  *  will be freed as soon as their refcount goes to zero.  In combination
    2133             :  *  with aset.c's CLOBBER_FREED_MEMORY option, this provides a good test
    2134             :  *  to catch references to already-released relcache entries.  It slows
    2135             :  *  things down quite a bit, however.
    2136             :  */
    2137             : void
    2138    37084968 : RelationClose(Relation relation)
    2139             : {
    2140             :     /* Note: no locking manipulations needed */
    2141    37084968 :     RelationDecrementReferenceCount(relation);
    2142             : 
    2143             :     /*
    2144             :      * If the relation is no longer open in this session, we can clean up any
    2145             :      * stale partition descriptors it has.  This is unlikely, so check to see
    2146             :      * if there are child contexts before expending a call to mcxt.c.
    2147             :      */
    2148    37084968 :     if (RelationHasReferenceCountZero(relation))
    2149             :     {
    2150    22820314 :         if (relation->rd_pdcxt != NULL &&
    2151       45182 :             relation->rd_pdcxt->firstchild != NULL)
    2152        2112 :             MemoryContextDeleteChildren(relation->rd_pdcxt);
    2153             : 
    2154    22820314 :         if (relation->rd_pddcxt != NULL &&
    2155          94 :             relation->rd_pddcxt->firstchild != NULL)
    2156           0 :             MemoryContextDeleteChildren(relation->rd_pddcxt);
    2157             :     }
    2158             : 
    2159             : #ifdef RELCACHE_FORCE_RELEASE
    2160             :     if (RelationHasReferenceCountZero(relation) &&
    2161             :         relation->rd_createSubid == InvalidSubTransactionId &&
    2162             :         relation->rd_firstRelfilenodeSubid == InvalidSubTransactionId)
    2163             :         RelationClearRelation(relation, false);
    2164             : #endif
    2165    37084968 : }
    2166             : 
    2167             : /*
    2168             :  * RelationReloadIndexInfo - reload minimal information for an open index
    2169             :  *
    2170             :  *  This function is used only for indexes.  A relcache inval on an index
    2171             :  *  can mean that its pg_class or pg_index row changed.  There are only
    2172             :  *  very limited changes that are allowed to an existing index's schema,
    2173             :  *  so we can update the relcache entry without a complete rebuild; which
    2174             :  *  is fortunate because we can't rebuild an index entry that is "nailed"
    2175             :  *  and/or in active use.  We support full replacement of the pg_class row,
    2176             :  *  as well as updates of a few simple fields of the pg_index row.
    2177             :  *
    2178             :  *  We can't necessarily reread the catalog rows right away; we might be
    2179             :  *  in a failed transaction when we receive the SI notification.  If so,
    2180             :  *  RelationClearRelation just marks the entry as invalid by setting
    2181             :  *  rd_isvalid to false.  This routine is called to fix the entry when it
    2182             :  *  is next needed.
    2183             :  *
    2184             :  *  We assume that at the time we are called, we have at least AccessShareLock
    2185             :  *  on the target index.  (Note: in the calls from RelationClearRelation,
    2186             :  *  this is legitimate because we know the rel has positive refcount.)
    2187             :  *
    2188             :  *  If the target index is an index on pg_class or pg_index, we'd better have
    2189             :  *  previously gotten at least AccessShareLock on its underlying catalog,
    2190             :  *  else we are at risk of deadlock against someone trying to exclusive-lock
    2191             :  *  the heap and index in that order.  This is ensured in current usage by
    2192             :  *  only applying this to indexes being opened or having positive refcount.
    2193             :  */
    2194             : static void
    2195       56134 : RelationReloadIndexInfo(Relation relation)
    2196             : {
    2197             :     bool        indexOK;
    2198             :     HeapTuple   pg_class_tuple;
    2199             :     Form_pg_class relp;
    2200             : 
    2201             :     /* Should be called only for invalidated, live indexes */
    2202             :     Assert((relation->rd_rel->relkind == RELKIND_INDEX ||
    2203             :             relation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) &&
    2204             :            !relation->rd_isvalid &&
    2205             :            relation->rd_droppedSubid == InvalidSubTransactionId);
    2206             : 
    2207             :     /* Ensure it's closed at smgr level */
    2208       56134 :     RelationCloseSmgr(relation);
    2209             : 
    2210             :     /* Must free any AM cached data upon relcache flush */
    2211       56134 :     if (relation->rd_amcache)
    2212           0 :         pfree(relation->rd_amcache);
    2213       56134 :     relation->rd_amcache = NULL;
    2214             : 
    2215             :     /*
    2216             :      * If it's a shared index, we might be called before backend startup has
    2217             :      * finished selecting a database, in which case we have no way to read
    2218             :      * pg_class yet.  However, a shared index can never have any significant
    2219             :      * schema updates, so it's okay to ignore the invalidation signal.  Just
    2220             :      * mark it valid and return without doing anything more.
    2221             :      */
    2222       56134 :     if (relation->rd_rel->relisshared && !criticalRelcachesBuilt)
    2223             :     {
    2224           0 :         relation->rd_isvalid = true;
    2225           0 :         return;
    2226             :     }
    2227             : 
    2228             :     /*
    2229             :      * Read the pg_class row
    2230             :      *
    2231             :      * Don't try to use an indexscan of pg_class_oid_index to reload the info
    2232             :      * for pg_class_oid_index ...
    2233             :      */
    2234       56134 :     indexOK = (RelationGetRelid(relation) != ClassOidIndexId);
    2235       56134 :     pg_class_tuple = ScanPgRelation(RelationGetRelid(relation), indexOK, false);
    2236       56128 :     if (!HeapTupleIsValid(pg_class_tuple))
    2237           0 :         elog(ERROR, "could not find pg_class tuple for index %u",
    2238             :              RelationGetRelid(relation));
    2239       56128 :     relp = (Form_pg_class) GETSTRUCT(pg_class_tuple);
    2240       56128 :     memcpy(relation->rd_rel, relp, CLASS_TUPLE_SIZE);
    2241             :     /* Reload reloptions in case they changed */
    2242       56128 :     if (relation->rd_options)
    2243         314 :         pfree(relation->rd_options);
    2244       56128 :     RelationParseRelOptions(relation, pg_class_tuple);
    2245             :     /* done with pg_class tuple */
    2246       56128 :     heap_freetuple(pg_class_tuple);
    2247             :     /* We must recalculate physical address in case it changed */
    2248       56128 :     RelationInitPhysicalAddr(relation);
    2249             : 
    2250             :     /*
    2251             :      * For a non-system index, there are fields of the pg_index row that are
    2252             :      * allowed to change, so re-read that row and update the relcache entry.
    2253             :      * Most of the info derived from pg_index (such as support function lookup
    2254             :      * info) cannot change, and indeed the whole point of this routine is to
    2255             :      * update the relcache entry without clobbering that data; so wholesale
    2256             :      * replacement is not appropriate.
    2257             :      */
    2258       56128 :     if (!IsSystemRelation(relation))
    2259             :     {
    2260             :         HeapTuple   tuple;
    2261             :         Form_pg_index index;
    2262             : 
    2263       14188 :         tuple = SearchSysCache1(INDEXRELID,
    2264       14188 :                                 ObjectIdGetDatum(RelationGetRelid(relation)));
    2265       14188 :         if (!HeapTupleIsValid(tuple))
    2266           0 :             elog(ERROR, "cache lookup failed for index %u",
    2267             :                  RelationGetRelid(relation));
    2268       14188 :         index = (Form_pg_index) GETSTRUCT(tuple);
    2269             : 
    2270             :         /*
    2271             :          * Basically, let's just copy all the bool fields.  There are one or
    2272             :          * two of these that can't actually change in the current code, but
    2273             :          * it's not worth it to track exactly which ones they are.  None of
    2274             :          * the array fields are allowed to change, though.
    2275             :          */
    2276       14188 :         relation->rd_index->indisunique = index->indisunique;
    2277       14188 :         relation->rd_index->indisprimary = index->indisprimary;
    2278       14188 :         relation->rd_index->indisexclusion = index->indisexclusion;
    2279       14188 :         relation->rd_index->indimmediate = index->indimmediate;
    2280       14188 :         relation->rd_index->indisclustered = index->indisclustered;
    2281       14188 :         relation->rd_index->indisvalid = index->indisvalid;
    2282       14188 :         relation->rd_index->indcheckxmin = index->indcheckxmin;
    2283       14188 :         relation->rd_index->indisready = index->indisready;
    2284       14188 :         relation->rd_index->indislive = index->indislive;
    2285             : 
    2286             :         /* Copy xmin too, as that is needed to make sense of indcheckxmin */
    2287       14188 :         HeapTupleHeaderSetXmin(relation->rd_indextuple->t_data,
    2288             :                                HeapTupleHeaderGetXmin(tuple->t_data));
    2289             : 
    2290       14188 :         ReleaseSysCache(tuple);
    2291             :     }
    2292             : 
    2293             :     /* Okay, now it's valid again */
    2294       56128 :     relation->rd_isvalid = true;
    2295             : }
    2296             : 
    2297             : /*
    2298             :  * RelationReloadNailed - reload minimal information for nailed relations.
    2299             :  *
    2300             :  * The structure of a nailed relation can never change (which is good, because
    2301             :  * we rely on knowing their structure to be able to read catalog content). But
    2302             :  * some parts, e.g. pg_class.relfrozenxid, are still important to have
    2303             :  * accurate content for. Therefore those need to be reloaded after the arrival
    2304             :  * of invalidations.
    2305             :  */
    2306             : static void
    2307      675280 : RelationReloadNailed(Relation relation)
    2308             : {
    2309             :     Assert(relation->rd_isnailed);
    2310             : 
    2311             :     /*
    2312             :      * Redo RelationInitPhysicalAddr in case it is a mapped relation whose
    2313             :      * mapping changed.
    2314             :      */
    2315      675280 :     RelationInitPhysicalAddr(relation);
    2316             : 
    2317             :     /* flag as needing to be revalidated */
    2318      675280 :     relation->rd_isvalid = false;
    2319             : 
    2320             :     /*
    2321             :      * Can only reread catalog contents if in a transaction.  If the relation
    2322             :      * is currently open (not counting the nailed refcount), do so
    2323             :      * immediately. Otherwise we've already marked the entry as possibly
    2324             :      * invalid, and it'll be fixed when next opened.
    2325             :      */
    2326      675280 :     if (!IsTransactionState() || relation->rd_refcnt <= 1)
    2327       86594 :         return;
    2328             : 
    2329      588686 :     if (relation->rd_rel->relkind == RELKIND_INDEX)
    2330             :     {
    2331             :         /*
    2332             :          * If it's a nailed-but-not-mapped index, then we need to re-read the
    2333             :          * pg_class row to see if its relfilenode changed.
    2334             :          */
    2335         908 :         RelationReloadIndexInfo(relation);
    2336             :     }
    2337             :     else
    2338             :     {
    2339             :         /*
    2340             :          * Reload a non-index entry.  We can't easily do so if relcaches
    2341             :          * aren't yet built, but that's fine because at that stage the
    2342             :          * attributes that need to be current (like relfrozenxid) aren't yet
    2343             :          * accessed.  To ensure the entry will later be revalidated, we leave
    2344             :          * it in invalid state, but allow use (cf. RelationIdGetRelation()).
    2345             :          */
    2346      587778 :         if (criticalRelcachesBuilt)
    2347             :         {
    2348             :             HeapTuple   pg_class_tuple;
    2349             :             Form_pg_class relp;
    2350             : 
    2351             :             /*
    2352             :              * NB: Mark the entry as valid before starting to scan, to avoid
    2353             :              * self-recursion when re-building pg_class.
    2354             :              */
    2355       21844 :             relation->rd_isvalid = true;
    2356             : 
    2357       21844 :             pg_class_tuple = ScanPgRelation(RelationGetRelid(relation),
    2358             :                                             true, false);
    2359       21838 :             relp = (Form_pg_class) GETSTRUCT(pg_class_tuple);
    2360       21838 :             memcpy(relation->rd_rel, relp, CLASS_TUPLE_SIZE);
    2361       21838 :             heap_freetuple(pg_class_tuple);
    2362             : 
    2363             :             /*
    2364             :              * Again mark as valid, to protect against concurrently arriving
    2365             :              * invalidations.
    2366             :              */
    2367       21838 :             relation->rd_isvalid = true;
    2368             :         }
    2369             :     }
    2370             : }
    2371             : 
    2372             : /*
    2373             :  * RelationDestroyRelation
    2374             :  *
    2375             :  *  Physically delete a relation cache entry and all subsidiary data.
    2376             :  *  Caller must already have unhooked the entry from the hash table.
    2377             :  */
    2378             : static void
    2379     1007942 : RelationDestroyRelation(Relation relation, bool remember_tupdesc)
    2380             : {
    2381             :     Assert(RelationHasReferenceCountZero(relation));
    2382             : 
    2383             :     /*
    2384             :      * Make sure smgr and lower levels close the relation's files, if they
    2385             :      * weren't closed already.  (This was probably done by caller, but let's
    2386             :      * just be real sure.)
    2387             :      */
    2388     1007942 :     RelationCloseSmgr(relation);
    2389             : 
    2390             :     /*
    2391             :      * Free all the subsidiary data structures of the relcache entry, then the
    2392             :      * entry itself.
    2393             :      */
    2394     1007942 :     if (relation->rd_rel)
    2395     1007942 :         pfree(relation->rd_rel);
    2396             :     /* can't use DecrTupleDescRefCount here */
    2397             :     Assert(relation->rd_att->tdrefcount > 0);
    2398     1007942 :     if (--relation->rd_att->tdrefcount == 0)
    2399             :     {
    2400             :         /*
    2401             :          * If we Rebuilt a relcache entry during a transaction then its
    2402             :          * possible we did that because the TupDesc changed as the result of
    2403             :          * an ALTER TABLE that ran at less than AccessExclusiveLock. It's
    2404             :          * possible someone copied that TupDesc, in which case the copy would
    2405             :          * point to free'd memory. So if we rebuild an entry we keep the
    2406             :          * TupDesc around until end of transaction, to be safe.
    2407             :          */
    2408     1005862 :         if (remember_tupdesc)
    2409       10816 :             RememberToFreeTupleDescAtEOX(relation->rd_att);
    2410             :         else
    2411      995046 :             FreeTupleDesc(relation->rd_att);
    2412             :     }
    2413     1007942 :     FreeTriggerDesc(relation->trigdesc);
    2414     1007942 :     list_free_deep(relation->rd_fkeylist);
    2415     1007942 :     list_free(relation->rd_indexlist);
    2416     1007942 :     list_free(relation->rd_statlist);
    2417     1007942 :     bms_free(relation->rd_keyattr);
    2418     1007942 :     bms_free(relation->rd_pkattr);
    2419     1007942 :     bms_free(relation->rd_idattr);
    2420     1007942 :     bms_free(relation->rd_hotblockingattr);
    2421     1007942 :     if (relation->rd_pubactions)
    2422        2288 :         pfree(relation->rd_pubactions);
    2423     1007942 :     if (relation->rd_options)
    2424        7176 :         pfree(relation->rd_options);
    2425     1007942 :     if (relation->rd_indextuple)
    2426      236704 :         pfree(relation->rd_indextuple);
    2427     1007942 :     if (relation->rd_amcache)
    2428           0 :         pfree(relation->rd_amcache);
    2429     1007942 :     if (relation->rd_fdwroutine)
    2430         228 :         pfree(relation->rd_fdwroutine);
    2431     1007942 :     if (relation->rd_indexcxt)
    2432      236704 :         MemoryContextDelete(relation->rd_indexcxt);
    2433     1007942 :     if (relation->rd_rulescxt)
    2434       76384 :         MemoryContextDelete(relation->rd_rulescxt);
    2435     1007942 :     if (relation->rd_rsdesc)
    2436        1060 :         MemoryContextDelete(relation->rd_rsdesc->rscxt);
    2437     1007942 :     if (relation->rd_partkeycxt)
    2438        9244 :         MemoryContextDelete(relation->rd_partkeycxt);
    2439     1007942 :     if (relation->rd_pdcxt)
    2440        8916 :         MemoryContextDelete(relation->rd_pdcxt);
    2441     1007942 :     if (relation->rd_pddcxt)
    2442          60 :         MemoryContextDelete(relation->rd_pddcxt);
    2443     1007942 :     if (relation->rd_partcheckcxt)
    2444        1568 :         MemoryContextDelete(relation->rd_partcheckcxt);
    2445     1007942 :     pfree(relation);
    2446     1007942 : }
    2447             : 
    2448             : /*
    2449             :  * RelationClearRelation
    2450             :  *
    2451             :  *   Physically blow away a relation cache entry, or reset it and rebuild
    2452             :  *   it from scratch (that is, from catalog entries).  The latter path is
    2453             :  *   used when we are notified of a change to an open relation (one with
    2454             :  *   refcount > 0).
    2455             :  *
    2456             :  *   NB: when rebuilding, we'd better hold some lock on the relation,
    2457             :  *   else the catalog data we need to read could be changing under us.
    2458             :  *   Also, a rel to be rebuilt had better have refcnt > 0.  This is because
    2459             :  *   a sinval reset could happen while we're accessing the catalogs, and
    2460             :  *   the rel would get blown away underneath us by RelationCacheInvalidate
    2461             :  *   if it has zero refcnt.
    2462             :  *
    2463             :  *   The "rebuild" parameter is redundant in current usage because it has
    2464             :  *   to match the relation's refcnt status, but we keep it as a crosscheck
    2465             :  *   that we're doing what the caller expects.
    2466             :  */
    2467             : static void
    2468     1720124 : RelationClearRelation(Relation relation, bool rebuild)
    2469             : {
    2470             :     /*
    2471             :      * As per notes above, a rel to be rebuilt MUST have refcnt > 0; while of
    2472             :      * course it would be an equally bad idea to blow away one with nonzero
    2473             :      * refcnt, since that would leave someone somewhere with a dangling
    2474             :      * pointer.  All callers are expected to have verified that this holds.
    2475             :      */
    2476             :     Assert(rebuild ?
    2477             :            !RelationHasReferenceCountZero(relation) :
    2478             :            RelationHasReferenceCountZero(relation));
    2479             : 
    2480             :     /*
    2481             :      * Make sure smgr and lower levels close the relation's files, if they
    2482             :      * weren't closed already.  If the relation is not getting deleted, the
    2483             :      * next smgr access should reopen the files automatically.  This ensures
    2484             :      * that the low-level file access state is updated after, say, a vacuum
    2485             :      * truncation.
    2486             :      */
    2487     1720124 :     RelationCloseSmgr(relation);
    2488             : 
    2489             :     /* Free AM cached data, if any */
    2490     1720124 :     if (relation->rd_amcache)
    2491       72620 :         pfree(relation->rd_amcache);
    2492     1720124 :     relation->rd_amcache = NULL;
    2493             : 
    2494             :     /*
    2495             :      * Treat nailed-in system relations separately, they always need to be
    2496             :      * accessible, so we can't blow them away.
    2497             :      */
    2498     1720124 :     if (relation->rd_isnailed)
    2499             :     {
    2500      675280 :         RelationReloadNailed(relation);
    2501      675274 :         return;
    2502             :     }
    2503             : 
    2504             :     /* Mark it invalid until we've finished rebuild */
    2505     1044844 :     relation->rd_isvalid = false;
    2506             : 
    2507             :     /* See RelationForgetRelation(). */
    2508     1044844 :     if (relation->rd_droppedSubid != InvalidSubTransactionId)
    2509         916 :         return;
    2510             : 
    2511             :     /*
    2512             :      * Even non-system indexes should not be blown away if they are open and
    2513             :      * have valid index support information.  This avoids problems with active
    2514             :      * use of the index support information.  As with nailed indexes, we
    2515             :      * re-read the pg_class row to handle possible physical relocation of the
    2516             :      * index, and we check for pg_index updates too.
    2517             :      */
    2518     1043928 :     if ((relation->rd_rel->relkind == RELKIND_INDEX ||
    2519      755980 :          relation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) &&
    2520      293328 :         relation->rd_refcnt > 0 &&
    2521       56622 :         relation->rd_indexcxt != NULL)
    2522             :     {
    2523       35912 :         if (IsTransactionState())
    2524       35912 :             RelationReloadIndexInfo(relation);
    2525       35912 :         return;
    2526             :     }
    2527             : 
    2528             :     /*
    2529             :      * If we're really done with the relcache entry, blow it away. But if
    2530             :      * someone is still using it, reconstruct the whole deal without moving
    2531             :      * the physical RelationData record (so that the someone's pointer is
    2532             :      * still valid).
    2533             :      */
    2534     1008016 :     if (!rebuild)
    2535             :     {
    2536             :         /* Remove it from the hash table */
    2537      624766 :         RelationCacheDelete(relation);
    2538             : 
    2539             :         /* And release storage */
    2540      624766 :         RelationDestroyRelation(relation, false);
    2541             :     }
    2542      383250 :     else if (!IsTransactionState())
    2543             :     {
    2544             :         /*
    2545             :          * If we're not inside a valid transaction, we can't do any catalog
    2546             :          * access so it's not possible to rebuild yet.  Just exit, leaving
    2547             :          * rd_isvalid = false so that the rebuild will occur when the entry is
    2548             :          * next opened.
    2549             :          *
    2550             :          * Note: it's possible that we come here during subtransaction abort,
    2551             :          * and the reason for wanting to rebuild is that the rel is open in
    2552             :          * the outer transaction.  In that case it might seem unsafe to not
    2553             :          * rebuild immediately, since whatever code has the rel already open
    2554             :          * will keep on using the relcache entry as-is.  However, in such a
    2555             :          * case the outer transaction should be holding a lock that's
    2556             :          * sufficient to prevent any significant change in the rel's schema,
    2557             :          * so the existing entry contents should be good enough for its
    2558             :          * purposes; at worst we might be behind on statistics updates or the
    2559             :          * like.  (See also CheckTableNotInUse() and its callers.)  These same
    2560             :          * remarks also apply to the cases above where we exit without having
    2561             :          * done RelationReloadIndexInfo() yet.
    2562             :          */
    2563          72 :         return;
    2564             :     }
    2565             :     else
    2566             :     {
    2567             :         /*
    2568             :          * Our strategy for rebuilding an open relcache entry is to build a
    2569             :          * new entry from scratch, swap its contents with the old entry, and
    2570             :          * finally delete the new entry (along with any infrastructure swapped
    2571             :          * over from the old entry).  This is to avoid trouble in case an
    2572             :          * error causes us to lose control partway through.  The old entry
    2573             :          * will still be marked !rd_isvalid, so we'll try to rebuild it again
    2574             :          * on next access.  Meanwhile it's not any less valid than it was
    2575             :          * before, so any code that might expect to continue accessing it
    2576             :          * isn't hurt by the rebuild failure.  (Consider for example a
    2577             :          * subtransaction that ALTERs a table and then gets canceled partway
    2578             :          * through the cache entry rebuild.  The outer transaction should
    2579             :          * still see the not-modified cache entry as valid.)  The worst
    2580             :          * consequence of an error is leaking the necessarily-unreferenced new
    2581             :          * entry, and this shouldn't happen often enough for that to be a big
    2582             :          * problem.
    2583             :          *
    2584             :          * When rebuilding an open relcache entry, we must preserve ref count,
    2585             :          * rd_*Subid, and rd_toastoid state.  Also attempt to preserve the
    2586             :          * pg_class entry (rd_rel), tupledesc, rewrite-rule, partition key,
    2587             :          * and partition descriptor substructures in place, because various
    2588             :          * places assume that these structures won't move while they are
    2589             :          * working with an open relcache entry.  (Note:  the refcount
    2590             :          * mechanism for tupledescs might someday allow us to remove this hack
    2591             :          * for the tupledesc.)
    2592             :          *
    2593             :          * Note that this process does not touch CurrentResourceOwner; which
    2594             :          * is good because whatever ref counts the entry may have do not
    2595             :          * necessarily belong to that resource owner.
    2596             :          */
    2597             :         Relation    newrel;
    2598      383178 :         Oid         save_relid = RelationGetRelid(relation);
    2599             :         bool        keep_tupdesc;
    2600             :         bool        keep_rules;
    2601             :         bool        keep_policies;
    2602             :         bool        keep_partkey;
    2603             : 
    2604             :         /* Build temporary entry, but don't link it into hashtable */
    2605      383178 :         newrel = RelationBuildDesc(save_relid, false);
    2606             : 
    2607             :         /*
    2608             :          * Between here and the end of the swap, don't add code that does or
    2609             :          * reasonably could read system catalogs.  That range must be free
    2610             :          * from invalidation processing.  See RelationBuildDesc() manipulation
    2611             :          * of in_progress_list.
    2612             :          */
    2613             : 
    2614      383174 :         if (newrel == NULL)
    2615             :         {
    2616             :             /*
    2617             :              * We can validly get here, if we're using a historic snapshot in
    2618             :              * which a relation, accessed from outside logical decoding, is
    2619             :              * still invisible. In that case it's fine to just mark the
    2620             :              * relation as invalid and return - it'll fully get reloaded by
    2621             :              * the cache reset at the end of logical decoding (or at the next
    2622             :              * access).  During normal processing we don't want to ignore this
    2623             :              * case as it shouldn't happen there, as explained below.
    2624             :              */
    2625           0 :             if (HistoricSnapshotActive())
    2626           0 :                 return;
    2627             : 
    2628             :             /*
    2629             :              * This shouldn't happen as dropping a relation is intended to be
    2630             :              * impossible if still referenced (cf. CheckTableNotInUse()). But
    2631             :              * if we get here anyway, we can't just delete the relcache entry,
    2632             :              * as it possibly could get accessed later (as e.g. the error
    2633             :              * might get trapped and handled via a subtransaction rollback).
    2634             :              */
    2635           0 :             elog(ERROR, "relation %u deleted while still in use", save_relid);
    2636             :         }
    2637             : 
    2638      383174 :         keep_tupdesc = equalTupleDescs(relation->rd_att, newrel->rd_att);
    2639      383174 :         keep_rules = equalRuleLocks(relation->rd_rules, newrel->rd_rules);
    2640      383174 :         keep_policies = equalRSDesc(relation->rd_rsdesc, newrel->rd_rsdesc);
    2641             :         /* partkey is immutable once set up, so we can always keep it */
    2642      383174 :         keep_partkey = (relation->rd_partkey != NULL);
    2643             : 
    2644             :         /*
    2645             :          * Perform swapping of the relcache entry contents.  Within this
    2646             :          * process the old entry is momentarily invalid, so there *must* be no
    2647             :          * possibility of CHECK_FOR_INTERRUPTS within this sequence. Do it in
    2648             :          * all-in-line code for safety.
    2649             :          *
    2650             :          * Since the vast majority of fields should be swapped, our method is
    2651             :          * to swap the whole structures and then re-swap those few fields we
    2652             :          * didn't want swapped.
    2653             :          */
    2654             : #define SWAPFIELD(fldtype, fldname) \
    2655             :         do { \
    2656             :             fldtype _tmp = newrel->fldname; \
    2657             :             newrel->fldname = relation->fldname; \
    2658             :             relation->fldname = _tmp; \
    2659             :         } while (0)
    2660             : 
    2661             :         /* swap all Relation struct fields */
    2662             :         {
    2663             :             RelationData tmpstruct;
    2664             : 
    2665      383174 :             memcpy(&tmpstruct, newrel, sizeof(RelationData));
    2666      383174 :             memcpy(newrel, relation, sizeof(RelationData));
    2667      383174 :             memcpy(relation, &tmpstruct, sizeof(RelationData));
    2668             :         }
    2669             : 
    2670             :         /* rd_smgr must not be swapped, due to back-links from smgr level */
    2671      383174 :         SWAPFIELD(SMgrRelation, rd_smgr);
    2672             :         /* rd_refcnt must be preserved */
    2673      383174 :         SWAPFIELD(int, rd_refcnt);
    2674             :         /* isnailed shouldn't change */
    2675             :         Assert(newrel->rd_isnailed == relation->rd_isnailed);
    2676             :         /* creation sub-XIDs must be preserved */
    2677      383174 :         SWAPFIELD(SubTransactionId, rd_createSubid);
    2678      383174 :         SWAPFIELD(SubTransactionId, rd_newRelfilenodeSubid);
    2679      383174 :         SWAPFIELD(SubTransactionId, rd_firstRelfilenodeSubid);
    2680      383174 :         SWAPFIELD(SubTransactionId, rd_droppedSubid);
    2681             :         /* un-swap rd_rel pointers, swap contents instead */
    2682      383174 :         SWAPFIELD(Form_pg_class, rd_rel);
    2683             :         /* ... but actually, we don't have to update newrel->rd_rel */
    2684      383174 :         memcpy(relation->rd_rel, newrel->rd_rel, CLASS_TUPLE_SIZE);
    2685             :         /* preserve old tupledesc, rules, policies if no logical change */
    2686      383174 :         if (keep_tupdesc)
    2687      372220 :             SWAPFIELD(TupleDesc, rd_att);
    2688      383174 :         if (keep_rules)
    2689             :         {
    2690      314718 :             SWAPFIELD(RuleLock *, rd_rules);
    2691      314718 :             SWAPFIELD(MemoryContext, rd_rulescxt);
    2692             :         }
    2693      383174 :         if (keep_policies)
    2694      382984 :             SWAPFIELD(RowSecurityDesc *, rd_rsdesc);
    2695             :         /* toast OID override must be preserved */
    2696      383174 :         SWAPFIELD(Oid, rd_toastoid);
    2697             :         /* pgstat_info must be preserved */
    2698      383174 :         SWAPFIELD(struct PgStat_TableStatus *, pgstat_info);
    2699             :         /* preserve old partition key if we have one */
    2700      383174 :         if (keep_partkey)
    2701             :         {
    2702        6154 :             SWAPFIELD(PartitionKey, rd_partkey);
    2703        6154 :             SWAPFIELD(MemoryContext, rd_partkeycxt);
    2704             :         }
    2705      383174 :         if (newrel->rd_pdcxt != NULL || newrel->rd_pddcxt != NULL)
    2706             :         {
    2707             :             /*
    2708             :              * We are rebuilding a partitioned relation with a non-zero
    2709             :              * reference count, so we must keep the old partition descriptor
    2710             :              * around, in case there's a PartitionDirectory with a pointer to
    2711             :              * it.  This means we can't free the old rd_pdcxt yet.  (This is
    2712             :              * necessary because RelationGetPartitionDesc hands out direct
    2713             :              * pointers to the relcache's data structure, unlike our usual
    2714             :              * practice which is to hand out copies.  We'd have the same
    2715             :              * problem with rd_partkey, except that we always preserve that
    2716             :              * once created.)
    2717             :              *
    2718             :              * To ensure that it's not leaked completely, re-attach it to the
    2719             :              * new reldesc, or make it a child of the new reldesc's rd_pdcxt
    2720             :              * in the unlikely event that there is one already.  (Compare hack
    2721             :              * in RelationBuildPartitionDesc.)  RelationClose will clean up
    2722             :              * any such contexts once the reference count reaches zero.
    2723             :              *
    2724             :              * In the case where the reference count is zero, this code is not
    2725             :              * reached, which should be OK because in that case there should
    2726             :              * be no PartitionDirectory with a pointer to the old entry.
    2727             :              *
    2728             :              * Note that newrel and relation have already been swapped, so the
    2729             :              * "old" partition descriptor is actually the one hanging off of
    2730             :              * newrel.
    2731             :              */
    2732        4922 :             relation->rd_partdesc = NULL;    /* ensure rd_partdesc is invalid */
    2733        4922 :             relation->rd_partdesc_nodetached = NULL;
    2734        4922 :             relation->rd_partdesc_nodetached_xmin = InvalidTransactionId;
    2735        4922 :             if (relation->rd_pdcxt != NULL) /* probably never happens */
    2736           0 :                 MemoryContextSetParent(newrel->rd_pdcxt, relation->rd_pdcxt);
    2737             :             else
    2738        4922 :                 relation->rd_pdcxt = newrel->rd_pdcxt;
    2739        4922 :             if (relation->rd_pddcxt != NULL)
    2740           0 :                 MemoryContextSetParent(newrel->rd_pddcxt, relation->rd_pddcxt);
    2741             :             else
    2742        4922 :                 relation->rd_pddcxt = newrel->rd_pddcxt;
    2743             :             /* drop newrel's pointers so we don't destroy it below */
    2744        4922 :             newrel->rd_partdesc = NULL;
    2745        4922 :             newrel->rd_partdesc_nodetached = NULL;
    2746        4922 :             newrel->rd_partdesc_nodetached_xmin = InvalidTransactionId;
    2747        4922 :             newrel->rd_pdcxt = NULL;
    2748        4922 :             newrel->rd_pddcxt = NULL;
    2749             :         }
    2750             : 
    2751             : #undef SWAPFIELD
    2752             : 
    2753             :         /* And now we can throw away the temporary entry */
    2754      383174 :         RelationDestroyRelation(newrel, !keep_tupdesc);
    2755             :     }
    2756             : }
    2757             : 
    2758             : /*
    2759             :  * RelationFlushRelation
    2760             :  *
    2761             :  *   Rebuild the relation if it is open (refcount > 0), else blow it away.
    2762             :  *   This is used when we receive a cache invalidation event for the rel.
    2763             :  */
    2764             : static void
    2765      814740 : RelationFlushRelation(Relation relation)
    2766             : {
    2767      814740 :     if (relation->rd_createSubid != InvalidSubTransactionId ||
    2768      438918 :         relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId)
    2769             :     {
    2770             :         /*
    2771             :          * New relcache entries are always rebuilt, not flushed; else we'd
    2772             :          * forget the "new" status of the relation.  Ditto for the
    2773             :          * new-relfilenode status.
    2774             :          *
    2775             :          * The rel could have zero refcnt here, so temporarily increment the
    2776             :          * refcnt to ensure it's safe to rebuild it.  We can assume that the
    2777             :          * current transaction has some lock on the rel already.
    2778             :          */
    2779      386634 :         RelationIncrementReferenceCount(relation);
    2780      386634 :         RelationClearRelation(relation, true);
    2781      386630 :         RelationDecrementReferenceCount(relation);
    2782             :     }
    2783             :     else
    2784             :     {
    2785             :         /*
    2786             :          * Pre-existing rels can be dropped from the relcache if not open.
    2787             :          */
    2788      428106 :         bool        rebuild = !RelationHasReferenceCountZero(relation);
    2789             : 
    2790      428106 :         RelationClearRelation(relation, rebuild);
    2791             :     }
    2792      814736 : }
    2793             : 
    2794             : /*
    2795             :  * RelationForgetRelation - caller reports that it dropped the relation
    2796             :  */
    2797             : void
    2798       39410 : RelationForgetRelation(Oid rid)
    2799             : {
    2800             :     Relation    relation;
    2801             : 
    2802       39410 :     RelationIdCacheLookup(rid, relation);
    2803             : 
    2804       39410 :     if (!PointerIsValid(relation))
    2805           0 :         return;                 /* not in cache, nothing to do */
    2806             : 
    2807       39410 :     if (!RelationHasReferenceCountZero(relation))
    2808           0 :         elog(ERROR, "relation %u is still open", rid);
    2809             : 
    2810             :     Assert(relation->rd_droppedSubid == InvalidSubTransactionId);
    2811       39410 :     if (relation->rd_createSubid != InvalidSubTransactionId ||
    2812       38974 :         relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId)
    2813             :     {
    2814             :         /*
    2815             :          * In the event of subtransaction rollback, we must not forget
    2816             :          * rd_*Subid.  Mark the entry "dropped" so RelationClearRelation()
    2817             :          * invalidates it in lieu of destroying it.  (If we're in a top
    2818             :          * transaction, we could opt to destroy the entry.)
    2819             :          */
    2820         458 :         relation->rd_droppedSubid = GetCurrentSubTransactionId();
    2821             :     }
    2822             : 
    2823       39410 :     RelationClearRelation(relation, false);
    2824             : }
    2825             : 
    2826             : /*
    2827             :  *      RelationCacheInvalidateEntry
    2828             :  *
    2829             :  *      This routine is invoked for SI cache flush messages.
    2830             :  *
    2831             :  * Any relcache entry matching the relid must be flushed.  (Note: caller has
    2832             :  * already determined that the relid belongs to our database or is a shared
    2833             :  * relation.)
    2834             :  *
    2835             :  * We used to skip local relations, on the grounds that they could
    2836             :  * not be targets of cross-backend SI update messages; but it seems
    2837             :  * safer to process them, so that our *own* SI update messages will
    2838             :  * have the same effects during CommandCounterIncrement for both
    2839             :  * local and nonlocal relations.
    2840             :  */
    2841             : void
    2842     1840694 : RelationCacheInvalidateEntry(Oid relationId)
    2843             : {
    2844             :     Relation    relation;
    2845             : 
    2846     1840694 :     RelationIdCacheLookup(relationId, relation);
    2847             : 
    2848     1840694 :     if (PointerIsValid(relation))
    2849             :     {
    2850      814740 :         relcacheInvalsReceived++;
    2851      814740 :         RelationFlushRelation(relation);
    2852             :     }
    2853             :     else
    2854             :     {
    2855             :         int         i;
    2856             : 
    2857     1035244 :         for (i = 0; i < in_progress_list_len; i++)
    2858        9290 :             if (in_progress_list[i].reloid == relationId)
    2859           2 :                 in_progress_list[i].invalidated = true;
    2860             :     }
    2861     1840690 : }
    2862             : 
    2863             : /*
    2864             :  * RelationCacheInvalidate
    2865             :  *   Blow away cached relation descriptors that have zero reference counts,
    2866             :  *   and rebuild those with positive reference counts.  Also reset the smgr
    2867             :  *   relation cache and re-read relation mapping data.
    2868             :  *
    2869             :  *   Apart from debug_discard_caches, this is currently used only to recover
    2870             :  *   from SI message buffer overflow, so we do not touch relations having
    2871             :  *   new-in-transaction relfilenodes; they cannot be targets of cross-backend
    2872             :  *   SI updates (and our own updates now go through a separate linked list
    2873             :  *   that isn't limited by the SI message buffer size).
    2874             :  *
    2875             :  *   We do this in two phases: the first pass deletes deletable items, and
    2876             :  *   the second one rebuilds the rebuildable items.  This is essential for
    2877             :  *   safety, because hash_seq_search only copes with concurrent deletion of
    2878             :  *   the element it is currently visiting.  If a second SI overflow were to
    2879             :  *   occur while we are walking the table, resulting in recursive entry to
    2880             :  *   this routine, we could crash because the inner invocation blows away
    2881             :  *   the entry next to be visited by the outer scan.  But this way is OK,
    2882             :  *   because (a) during the first pass we won't process any more SI messages,
    2883             :  *   so hash_seq_search will complete safely; (b) during the second pass we
    2884             :  *   only hold onto pointers to nondeletable entries.
    2885             :  *
    2886             :  *   The two-phase approach also makes it easy to update relfilenodes for
    2887             :  *   mapped relations before we do anything else, and to ensure that the
    2888             :  *   second pass processes nailed-in-cache items before other nondeletable
    2889             :  *   items.  This should ensure that system catalogs are up to date before
    2890             :  *   we attempt to use them to reload information about other open relations.
    2891             :  *
    2892             :  *   After those two phases of work having immediate effects, we normally
    2893             :  *   signal any RelationBuildDesc() on the stack to start over.  However, we
    2894             :  *   don't do this if called as part of debug_discard_caches.  Otherwise,
    2895             :  *   RelationBuildDesc() would become an infinite loop.
    2896             :  */
    2897             : void
    2898        2764 : RelationCacheInvalidate(bool debug_discard)
    2899             : {
    2900             :     HASH_SEQ_STATUS status;
    2901             :     RelIdCacheEnt *idhentry;
    2902             :     Relation    relation;
    2903        2764 :     List       *rebuildFirstList = NIL;
    2904        2764 :     List       *rebuildList = NIL;
    2905             :     ListCell   *l;
    2906             :     int         i;
    2907             : 
    2908             :     /*
    2909             :      * Reload relation mapping data before starting to reconstruct cache.
    2910             :      */
    2911        2764 :     RelationMapInvalidateAll();
    2912             : 
    2913             :     /* Phase 1 */
    2914        2764 :     hash_seq_init(&status, RelationIdCache);
    2915             : 
    2916      290456 :     while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
    2917             :     {
    2918      287692 :         relation = idhentry->reldesc;
    2919             : 
    2920             :         /* Must close all smgr references to avoid leaving dangling ptrs */
    2921      287692 :         RelationCloseSmgr(relation);
    2922             : 
    2923             :         /*
    2924             :          * Ignore new relations; no other backend will manipulate them before
    2925             :          * we commit.  Likewise, before replacing a relation's relfilenode, we
    2926             :          * shall have acquired AccessExclusiveLock and drained any applicable
    2927             :          * pending invalidations.
    2928             :          */
    2929      287692 :         if (relation->rd_createSubid != InvalidSubTransactionId ||
    2930      287662 :             relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId)
    2931          30 :             continue;
    2932             : 
    2933      287662 :         relcacheInvalsReceived++;
    2934             : 
    2935      287662 :         if (RelationHasReferenceCountZero(relation))
    2936             :         {
    2937             :             /* Delete this entry immediately */
    2938             :             Assert(!relation->rd_isnailed);
    2939      227756 :             RelationClearRelation(relation, false);
    2940             :         }
    2941             :         else
    2942             :         {
    2943             :             /*
    2944             :              * If it's a mapped relation, immediately update its rd_node in
    2945             :              * case its relfilenode changed.  We must do this during phase 1
    2946             :              * in case the relation is consulted during rebuild of other
    2947             :              * relcache entries in phase 2.  It's safe since consulting the
    2948             :              * map doesn't involve any access to relcache entries.
    2949             :              */
    2950       59906 :             if (RelationIsMapped(relation))
    2951       46268 :                 RelationInitPhysicalAddr(relation);
    2952             : 
    2953             :             /*
    2954             :              * Add this entry to list of stuff to rebuild in second pass.
    2955             :              * pg_class goes to the front of rebuildFirstList while
    2956             :              * pg_class_oid_index goes to the back of rebuildFirstList, so
    2957             :              * they are done first and second respectively.  Other nailed
    2958             :              * relations go to the front of rebuildList, so they'll be done
    2959             :              * next in no particular order; and everything else goes to the
    2960             :              * back of rebuildList.
    2961             :              */
    2962       59906 :             if (RelationGetRelid(relation) == RelationRelationId)
    2963        2704 :                 rebuildFirstList = lcons(relation, rebuildFirstList);
    2964       57202 :             else if (RelationGetRelid(relation) == ClassOidIndexId)
    2965        2704 :                 rebuildFirstList = lappend(rebuildFirstList, relation);
    2966       54498 :             else if (relation->rd_isnailed)
    2967       54380 :                 rebuildList = lcons(relation, rebuildList);
    2968             :             else
    2969         118 :                 rebuildList = lappend(rebuildList, relation);
    2970             :         }
    2971             :     }
    2972             : 
    2973             :     /*
    2974             :      * Now zap any remaining smgr cache entries.  This must happen before we
    2975             :      * start to rebuild entries, since that may involve catalog fetches which
    2976             :      * will re-open catalog files.
    2977             :      */
    2978        2764 :     smgrcloseall();
    2979             : 
    2980             :     /* Phase 2: rebuild the items found to need rebuild in phase 1 */
    2981        8172 :     foreach(l, rebuildFirstList)
    2982             :     {
    2983        5408 :         relation = (Relation) lfirst(l);
    2984        5408 :         RelationClearRelation(relation, true);
    2985             :     }
    2986        2764 :     list_free(rebuildFirstList);
    2987       57262 :     foreach(l, rebuildList)
    2988             :     {
    2989       54498 :         relation = (Relation) lfirst(l);
    2990       54498 :         RelationClearRelation(relation, true);
    2991             :     }
    2992        2764 :     list_free(rebuildList);
    2993             : 
    2994        2764 :     if (!debug_discard)
    2995             :         /* Any RelationBuildDesc() on the stack must start over. */
    2996        2764 :         for (i = 0; i < in_progress_list_len; i++)
    2997           0 :             in_progress_list[i].invalidated = true;
    2998        2764 : }
    2999             : 
    3000             : /*
    3001             :  * RelationCloseSmgrByOid - close a relcache entry's smgr link
    3002             :  *
    3003             :  * Needed in some cases where we are changing a relation's physical mapping.
    3004             :  * The link will be automatically reopened on next use.
    3005             :  */
    3006             : void
    3007        2352 : RelationCloseSmgrByOid(Oid relationId)
    3008             : {
    3009             :     Relation    relation;
    3010             : 
    3011        2352 :     RelationIdCacheLookup(relationId, relation);
    3012             : 
    3013        2352 :     if (!PointerIsValid(relation))
    3014           0 :         return;                 /* not in cache, nothing to do */
    3015             : 
    3016        2352 :     RelationCloseSmgr(relation);
    3017             : }
    3018             : 
    3019             : static void
    3020       10816 : RememberToFreeTupleDescAtEOX(TupleDesc td)
    3021             : {
    3022       10816 :     if (EOXactTupleDescArray == NULL)
    3023             :     {
    3024             :         MemoryContext oldcxt;
    3025             : 
    3026        6996 :         oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
    3027             : 
    3028        6996 :         EOXactTupleDescArray = (TupleDesc *) palloc(16 * sizeof(TupleDesc));
    3029        6996 :         EOXactTupleDescArrayLen = 16;
    3030        6996 :         NextEOXactTupleDescNum = 0;
    3031        6996 :         MemoryContextSwitchTo(oldcxt);
    3032             :     }
    3033        3820 :     else if (NextEOXactTupleDescNum >= EOXactTupleDescArrayLen)
    3034             :     {
    3035           8 :         int32       newlen = EOXactTupleDescArrayLen * 2;
    3036             : 
    3037             :         Assert(EOXactTupleDescArrayLen > 0);
    3038             : 
    3039           8 :         EOXactTupleDescArray = (TupleDesc *) repalloc(EOXactTupleDescArray,
    3040             :                                                       newlen * sizeof(TupleDesc));
    3041           8 :         EOXactTupleDescArrayLen = newlen;
    3042             :     }
    3043             : 
    3044       10816 :     EOXactTupleDescArray[NextEOXactTupleDescNum++] = td;
    3045       10816 : }
    3046             : 
    3047             : #ifdef USE_ASSERT_CHECKING
    3048             : static void
    3049             : AssertPendingSyncConsistency(Relation relation)
    3050             : {
    3051             :     bool        relcache_verdict =
    3052             :     RelationIsPermanent(relation) &&
    3053             :     ((relation->rd_createSubid != InvalidSubTransactionId &&
    3054             :       RELKIND_HAS_STORAGE(relation->rd_rel->relkind)) ||
    3055             :      relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId);
    3056             : 
    3057             :     Assert(relcache_verdict == RelFileNodeSkippingWAL(relation->rd_node));
    3058             : 
    3059             :     if (relation->rd_droppedSubid != InvalidSubTransactionId)
    3060             :         Assert(!relation->rd_isvalid &&
    3061             :                (relation->rd_createSubid != InvalidSubTransactionId ||
    3062             :                 relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId));
    3063             : }
    3064             : 
    3065             : /*
    3066             :  * AssertPendingSyncs_RelationCache
    3067             :  *
    3068             :  *  Assert that relcache.c and storage.c agree on whether to skip WAL.
    3069             :  */
    3070             : void
    3071             : AssertPendingSyncs_RelationCache(void)
    3072             : {
    3073             :     HASH_SEQ_STATUS status;
    3074             :     LOCALLOCK  *locallock;
    3075             :     Relation   *rels;
    3076             :     int         maxrels;
    3077             :     int         nrels;
    3078             :     RelIdCacheEnt *idhentry;
    3079             :     int         i;
    3080             : 
    3081             :     /*
    3082             :      * Open every relation that this transaction has locked.  If, for some
    3083             :      * relation, storage.c is skipping WAL and relcache.c is not skipping WAL,
    3084             :      * a CommandCounterIncrement() typically yields a local invalidation
    3085             :      * message that destroys the relcache entry.  By recreating such entries
    3086             :      * here, we detect the problem.
    3087             :      */
    3088             :     PushActiveSnapshot(GetTransactionSnapshot());
    3089             :     maxrels = 1;
    3090             :     rels = palloc(maxrels * sizeof(*rels));
    3091             :     nrels = 0;
    3092             :     hash_seq_init(&status, GetLockMethodLocalHash());
    3093             :     while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
    3094             :     {
    3095             :         Oid         relid;
    3096             :         Relation    r;
    3097             : 
    3098             :         if (locallock->nLocks <= 0)
    3099             :             continue;
    3100             :         if ((LockTagType) locallock->tag.lock.locktag_type !=
    3101             :             LOCKTAG_RELATION)
    3102             :             continue;
    3103             :         relid = ObjectIdGetDatum(locallock->tag.lock.locktag_field2);
    3104             :         r = RelationIdGetRelation(relid);
    3105             :         if (!RelationIsValid(r))
    3106             :             continue;
    3107             :         if (nrels >= maxrels)
    3108             :         {
    3109             :             maxrels *= 2;
    3110             :             rels = repalloc(rels, maxrels * sizeof(*rels));
    3111             :         }
    3112             :         rels[nrels++] = r;
    3113             :     }
    3114             : 
    3115             :     hash_seq_init(&status, RelationIdCache);
    3116             :     while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
    3117             :         AssertPendingSyncConsistency(idhentry->reldesc);
    3118             : 
    3119             :     for (i = 0; i < nrels; i++)
    3120             :         RelationClose(rels[i]);
    3121             :     PopActiveSnapshot();
    3122             : }
    3123             : #endif
    3124             : 
    3125             : /*
    3126             :  * AtEOXact_RelationCache
    3127             :  *
    3128             :  *  Clean up the relcache at main-transaction commit or abort.
    3129             :  *
    3130             :  * Note: this must be called *before* processing invalidation messages.
    3131             :  * In the case of abort, we don't want to try to rebuild any invalidated
    3132             :  * cache entries (since we can't safely do database accesses).  Therefore
    3133             :  * we must reset refcnts before handling pending invalidations.
    3134             :  *
    3135             :  * As of PostgreSQL 8.1, relcache refcnts should get released by the
    3136             :  * ResourceOwner mechanism.  This routine just does a debugging
    3137             :  * cross-check that no pins remain.  However, we also need to do special
    3138             :  * cleanup when the current transaction created any relations or made use
    3139             :  * of forced index lists.
    3140             :  */
    3141             : void
    3142      732198 : AtEOXact_RelationCache(bool isCommit)
    3143             : {
    3144             :     HASH_SEQ_STATUS status;
    3145             :     RelIdCacheEnt *idhentry;
    3146             :     int         i;
    3147             : 
    3148             :     /*
    3149             :      * Forget in_progress_list.  This is relevant when we're aborting due to
    3150             :      * an error during RelationBuildDesc().
    3151             :      */
    3152             :     Assert(in_progress_list_len == 0 || !isCommit);
    3153      732198 :     in_progress_list_len = 0;
    3154             : 
    3155             :     /*
    3156             :      * Unless the eoxact_list[] overflowed, we only need to examine the rels
    3157             :      * listed in it.  Otherwise fall back on a hash_seq_search scan.
    3158             :      *
    3159             :      * For simplicity, eoxact_list[] entries are not deleted till end of
    3160             :      * top-level transaction, even though we could remove them at
    3161             :      * subtransaction end in some cases, or remove relations from the list if
    3162             :      * they are cleared for other reasons.  Therefore we should expect the
    3163             :      * case that list entries are not found in the hashtable; if not, there's
    3164             :      * nothing to do for them.
    3165             :      */
    3166      732198 :     if (eoxact_list_overflowed)
    3167             :     {
    3168         486 :         hash_seq_init(&status, RelationIdCache);
    3169      121866 :         while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
    3170             :         {
    3171      121380 :             AtEOXact_cleanup(idhentry->reldesc, isCommit);
    3172             :         }
    3173             :     }
    3174             :     else
    3175             :     {
    3176      868950 :         for (i = 0; i < eoxact_list_len; i++)
    3177             :         {
    3178      137238 :             idhentry = (RelIdCacheEnt *) hash_search(RelationIdCache,
    3179      137238 :                                                      (void *) &eoxact_list[i],
    3180             :                                                      HASH_FIND,
    3181             :                                                      NULL);
    3182      137238 :             if (idhentry != NULL)
    3183      135950 :                 AtEOXact_cleanup(idhentry->reldesc, isCommit);
    3184             :         }
    3185             :     }
    3186             : 
    3187      732198 :     if (EOXactTupleDescArrayLen > 0)
    3188             :     {
    3189             :         Assert(EOXactTupleDescArray != NULL);
    3190       17812 :         for (i = 0; i < NextEOXactTupleDescNum; i++)
    3191       10816 :             FreeTupleDesc(EOXactTupleDescArray[i]);
    3192        6996 :         pfree(EOXactTupleDescArray);
    3193        6996 :         EOXactTupleDescArray = NULL;
    3194             :     }
    3195             : 
    3196             :     /* Now we're out of the transaction and can clear the lists */
    3197      732198 :     eoxact_list_len = 0;
    3198      732198 :     eoxact_list_overflowed = false;
    3199      732198 :     NextEOXactTupleDescNum = 0;
    3200      732198 :     EOXactTupleDescArrayLen = 0;
    3201      732198 : }
    3202             : 
    3203             : /*
    3204             :  * AtEOXact_cleanup
    3205             :  *
    3206             :  *  Clean up a single rel at main-transaction commit or abort
    3207             :  *
    3208             :  * NB: this processing must be idempotent, because EOXactListAdd() doesn't
    3209             :  * bother to prevent duplicate entries in eoxact_list[].
    3210             :  */
    3211             : static void
    3212      257330 : AtEOXact_cleanup(Relation relation, bool isCommit)
    3213             : {
    3214      257330 :     bool        clear_relcache = false;
    3215             : 
    3216             :     /*
    3217             :      * The relcache entry's ref count should be back to its normal
    3218             :      * not-in-a-transaction state: 0 unless it's nailed in cache.
    3219             :      *
    3220             :      * In bootstrap mode, this is NOT true, so don't check it --- the
    3221             :      * bootstrap code expects relations to stay open across start/commit
    3222             :      * transaction calls.  (That seems bogus, but it's not worth fixing.)
    3223             :      *
    3224             :      * Note: ideally this check would be applied to every relcache entry, not
    3225             :      * just those that have eoxact work to do.  But it's not worth forcing a
    3226             :      * scan of the whole relcache just for this.  (Moreover, doing so would
    3227             :      * mean that assert-enabled testing never tests the hash_search code path
    3228             :      * above, which seems a bad idea.)
    3229             :      */
    3230             : #ifdef USE_ASSERT_CHECKING
    3231             :     if (!IsBootstrapProcessingMode())
    3232             :     {
    3233             :         int         expected_refcnt;
    3234             : 
    3235             :         expected_refcnt = relation->rd_isnailed ? 1 : 0;
    3236             :         Assert(relation->rd_refcnt == expected_refcnt);
    3237             :     }
    3238             : #endif
    3239             : 
    3240             :     /*
    3241             :      * Is the relation live after this transaction ends?
    3242             :      *
    3243             :      * During commit, clear the relcache entry if it is preserved after
    3244             :      * relation drop, in order not to orphan the entry.  During rollback,
    3245             :      * clear the relcache entry if the relation is created in the current
    3246             :      * transaction since it isn't interesting any longer once we are out of
    3247             :      * the transaction.
    3248             :      */
    3249      257330 :     clear_relcache =
    3250             :         (isCommit ?
    3251      257330 :          relation->rd_droppedSubid != InvalidSubTransactionId :
    3252        2264 :          relation->rd_createSubid != InvalidSubTransactionId);
    3253             : 
    3254             :     /*
    3255             :      * Since we are now out of the transaction, reset the subids to zero. That
    3256             :      * also lets RelationClearRelation() drop the relcache entry.
    3257             :      */
    3258      257330 :     relation->rd_createSubid = InvalidSubTransactionId;
    3259      257330 :     relation->rd_newRelfilenodeSubid = InvalidSubTransactionId;
    3260      257330 :     relation->rd_firstRelfilenodeSubid = InvalidSubTransactionId;
    3261      257330 :     relation->rd_droppedSubid = InvalidSubTransactionId;
    3262             : 
    3263      257330 :     if (clear_relcache)
    3264             :     {
    3265        2268 :         if (RelationHasReferenceCountZero(relation))
    3266             :         {
    3267        2268 :             RelationClearRelation(relation, false);
    3268        2268 :             return;
    3269             :         }
    3270             :         else
    3271             :         {
    3272             :             /*
    3273             :              * Hmm, somewhere there's a (leaked?) reference to the relation.
    3274             :              * We daren't remove the entry for fear of dereferencing a
    3275             :              * dangling pointer later.  Bleat, and mark it as not belonging to
    3276             :              * the current transaction.  Hopefully it'll get cleaned up
    3277             :              * eventually.  This must be just a WARNING to avoid
    3278             :              * error-during-error-recovery loops.
    3279             :              */
    3280           0 :             elog(WARNING, "cannot remove relcache entry for \"%s\" because it has nonzero refcount",
    3281             :                  RelationGetRelationName(relation));
    3282             :         }
    3283             :     }
    3284             : }
    3285             : 
    3286             : /*
    3287             :  * AtEOSubXact_RelationCache
    3288             :  *
    3289             :  *  Clean up the relcache at sub-transaction commit or abort.
    3290             :  *
    3291             :  * Note: this must be called *before* processing invalidation messages.
    3292             :  */
    3293             : void
    3294       12206 : AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid,
    3295             :                           SubTransactionId parentSubid)
    3296             : {
    3297             :     HASH_SEQ_STATUS status;
    3298             :     RelIdCacheEnt *idhentry;
    3299             :     int         i;
    3300             : 
    3301             :     /*
    3302             :      * Forget in_progress_list.  This is relevant when we're aborting due to
    3303             :      * an error during RelationBuildDesc().  We don't commit subtransactions
    3304             :      * during RelationBuildDesc().
    3305             :      */
    3306             :     Assert(in_progress_list_len == 0 || !isCommit);
    3307       12206 :     in_progress_list_len = 0;
    3308             : 
    3309             :     /*
    3310             :      * Unless the eoxact_list[] overflowed, we only need to examine the rels
    3311             :      * listed in it.  Otherwise fall back on a hash_seq_search scan.  Same
    3312             :      * logic as in AtEOXact_RelationCache.
    3313             :      */
    3314       12206 :     if (eoxact_list_overflowed)
    3315             :     {
    3316           0 :         hash_seq_init(&status, RelationIdCache);
    3317           0 :         while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
    3318             :         {
    3319           0 :             AtEOSubXact_cleanup(idhentry->reldesc, isCommit,
    3320             :                                 mySubid, parentSubid);
    3321             :         }
    3322             :     }
    3323             :     else
    3324             :     {
    3325       17796 :         for (i = 0; i < eoxact_list_len; i++)
    3326             :         {
    3327        5590 :             idhentry = (RelIdCacheEnt *) hash_search(RelationIdCache,
    3328        5590 :                                                      (void *) &eoxact_list[i],
    3329             :                                                      HASH_FIND,
    3330             :                                                      NULL);
    3331        5590 :             if (idhentry != NULL)
    3332        5160 :                 AtEOSubXact_cleanup(idhentry->reldesc, isCommit,
    3333             :                                     mySubid, parentSubid);
    3334             :         }
    3335             :     }
    3336             : 
    3337             :     /* Don't reset the list; we still need more cleanup later */
    3338       12206 : }
    3339             : 
    3340             : /*
    3341             :  * AtEOSubXact_cleanup
    3342             :  *
    3343             :  *  Clean up a single rel at subtransaction commit or abort
    3344             :  *
    3345             :  * NB: this processing must be idempotent, because EOXactListAdd() doesn't
    3346             :  * bother to prevent duplicate entries in eoxact_list[].
    3347             :  */
    3348             : static void
    3349        5160 : AtEOSubXact_cleanup(Relation relation, bool isCommit,
    3350             :                     SubTransactionId mySubid, SubTransactionId parentSubid)
    3351             : {
    3352             :     /*
    3353             :      * Is it a relation created in the current subtransaction?
    3354             :      *
    3355             :      * During subcommit, mark it as belonging to the parent, instead, as long
    3356             :      * as it has not been dropped. Otherwise simply delete the relcache entry.
    3357             :      * --- it isn't interesting any longer.
    3358             :      */
    3359        5160 :     if (relation->rd_createSubid == mySubid)
    3360             :     {
    3361             :         /*
    3362             :          * Valid rd_droppedSubid means the corresponding relation is dropped
    3363             :          * but the relcache entry is preserved for at-commit pending sync. We
    3364             :          * need to drop it explicitly here not to make the entry orphan.
    3365             :          */
    3366             :         Assert(relation->rd_droppedSubid == mySubid ||
    3367             :                relation->rd_droppedSubid == InvalidSubTransactionId);
    3368         112 :         if (isCommit && relation->rd_droppedSubid == InvalidSubTransactionId)
    3369          34 :             relation->rd_createSubid = parentSubid;
    3370          78 :         else if (RelationHasReferenceCountZero(relation))
    3371             :         {
    3372             :             /* allow the entry to be removed */
    3373          78 :             relation->rd_createSubid = InvalidSubTransactionId;
    3374          78 :             relation->rd_newRelfilenodeSubid = InvalidSubTransactionId;
    3375          78 :             relation->rd_firstRelfilenodeSubid = InvalidSubTransactionId;
    3376          78 :             relation->rd_droppedSubid = InvalidSubTransactionId;
    3377          78 :             RelationClearRelation(relation, false);
    3378          78 :             return;
    3379             :         }
    3380             :         else
    3381             :         {
    3382             :             /*
    3383             :              * Hmm, somewhere there's a (leaked?) reference to the relation.
    3384             :              * We daren't remove the entry for fear of dereferencing a
    3385             :              * dangling pointer later.  Bleat, and transfer it to the parent
    3386             :              * subtransaction so we can try again later.  This must be just a
    3387             :              * WARNING to avoid error-during-error-recovery loops.
    3388             :              */
    3389           0 :             relation->rd_createSubid = parentSubid;
    3390           0 :             elog(WARNING, "cannot remove relcache entry for \"%s\" because it has nonzero refcount",
    3391             :                  RelationGetRelationName(relation));
    3392             :         }
    3393             :     }
    3394             : 
    3395             :     /*
    3396             :      * Likewise, update or drop any new-relfilenode-in-subtransaction record
    3397             :      * or drop record.
    3398             :      */
    3399        5082 :     if (relation->rd_newRelfilenodeSubid == mySubid)
    3400             :     {
    3401         106 :         if (isCommit)
    3402          50 :             relation->rd_newRelfilenodeSubid = parentSubid;
    3403             :         else
    3404          56 :             relation->rd_newRelfilenodeSubid = InvalidSubTransactionId;
    3405             :     }
    3406             : 
    3407        5082 :     if (relation->rd_firstRelfilenodeSubid == mySubid)
    3408             :     {
    3409          74 :         if (isCommit)
    3410          22 :             relation->rd_firstRelfilenodeSubid = parentSubid;
    3411             :         else
    3412          52 :             relation->rd_firstRelfilenodeSubid = InvalidSubTransactionId;
    3413             :     }
    3414             : 
    3415        5082 :     if (relation->rd_droppedSubid == mySubid)
    3416             :     {
    3417          14 :         if (isCommit)
    3418           2 :             relation->rd_droppedSubid = parentSubid;
    3419             :         else
    3420          12 :             relation->rd_droppedSubid = InvalidSubTransactionId;
    3421             :     }
    3422             : }
    3423             : 
    3424             : 
    3425             : /*
    3426             :  *      RelationBuildLocalRelation
    3427             :  *          Build a relcache entry for an about-to-be-created relation,
    3428             :  *          and enter it into the relcache.
    3429             :  */
    3430             : Relation
    3431      246706 : RelationBuildLocalRelation(const char *relname,
    3432             :                            Oid relnamespace,
    3433             :                            TupleDesc tupDesc,
    3434             :                            Oid relid,
    3435             :                            Oid accessmtd,
    3436             :                            Oid relfilenode,
    3437             :                            Oid reltablespace,
    3438             :                            bool shared_relation,
    3439             :                            bool mapped_relation,
    3440             :                            char relpersistence,
    3441             :                            char relkind)
    3442             : {
    3443             :     Relation    rel;
    3444             :     MemoryContext oldcxt;
    3445      246706 :     int         natts = tupDesc->natts;
    3446             :     int         i;
    3447             :     bool        has_not_null;
    3448             :     bool        nailit;
    3449             : 
    3450             :     AssertArg(natts >= 0);
    3451             : 
    3452             :     /*
    3453             :      * check for creation of a rel that must be nailed in cache.
    3454             :      *
    3455             :      * XXX this list had better match the relations specially handled in
    3456             :      * RelationCacheInitializePhase2/3.
    3457             :      */
    3458      246706 :     switch (relid)
    3459             :     {
    3460        3374 :         case DatabaseRelationId:
    3461             :         case AuthIdRelationId:
    3462             :         case AuthMemRelationId:
    3463             :         case RelationRelationId:
    3464             :         case AttributeRelationId:
    3465             :         case ProcedureRelationId:
    3466             :         case TypeRelationId:
    3467        3374 :             nailit = true;
    3468        3374 :             break;
    3469      243332 :         default:
    3470      243332 :             nailit = false;
    3471      243332 :             break;
    3472             :     }
    3473             : 
    3474             :     /*
    3475             :      * check that hardwired list of shared rels matches what's in the
    3476             :      * bootstrap .bki file.  If you get a failure here during initdb, you
    3477             :      * probably need to fix IsSharedRelation() to match whatever you've done
    3478             :      * to the set of shared relations.
    3479             :      */
    3480      246706 :     if (shared_relation != IsSharedRelation(relid))
    3481           0 :         elog(ERROR, "shared_relation flag for \"%s\" does not match IsSharedRelation(%u)",
    3482             :              relname, relid);
    3483             : 
    3484             :     /* Shared relations had better be mapped, too */
    3485             :     Assert(mapped_relation || !shared_relation);
    3486             : 
    3487             :     /*
    3488             :      * switch to the cache context to create the relcache entry.
    3489             :      */
    3490      246706 :     if (!CacheMemoryContext)
    3491           0 :         CreateCacheMemoryContext();
    3492             : 
    3493      246706 :     oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
    3494             : 
    3495             :     /*
    3496             :      * allocate a new relation descriptor and fill in basic state fields.
    3497             :      */
    3498      246706 :     rel = (Relation) palloc0(sizeof(RelationData));
    3499             : 
    3500             :     /* make sure relation is marked as having no open file yet */
    3501      246706 :     rel->rd_smgr = NULL;
    3502             : 
    3503             :     /* mark it nailed if appropriate */
    3504      246706 :     rel->rd_isnailed = nailit;
    3505             : 
    3506      246706 :     rel->rd_refcnt = nailit ? 1 : 0;
    3507             : 
    3508             :     /* it's being created in this transaction */
    3509      246706 :     rel->rd_createSubid = GetCurrentSubTransactionId();
    3510      246706 :     rel->rd_newRelfilenodeSubid = InvalidSubTransactionId;
    3511      246706 :     rel->rd_firstRelfilenodeSubid = InvalidSubTransactionId;
    3512      246706 :     rel->rd_droppedSubid = InvalidSubTransactionId;
    3513             : 
    3514             :     /*
    3515             :      * create a new tuple descriptor from the one passed in.  We do this
    3516             :      * partly to copy it into the cache context, and partly because the new
    3517             :      * relation can't have any defaults or constraints yet; they have to be
    3518             :      * added in later steps, because they require additions to multiple system
    3519             :      * catalogs.  We can copy attnotnull constraints here, however.
    3520             :      */
    3521      246706 :     rel->rd_att = CreateTupleDescCopy(tupDesc);
    3522      246706 :     rel->rd_att->tdrefcount = 1;  /* mark as refcounted */
    3523      246706 :     has_not_null = false;
    3524     1506872 :     for (i = 0; i < natts; i++)
    3525             :     {
    3526     1260166 :         Form_pg_attribute satt = TupleDescAttr(tupDesc, i);
    3527     1260166 :         Form_pg_attribute datt = TupleDescAttr(rel->rd_att, i);
    3528             : 
    3529     1260166 :         datt->attidentity = satt->attidentity;
    3530     1260166 :         datt->attgenerated = satt->attgenerated;
    3531     1260166 :         datt->attnotnull = satt->attnotnull;
    3532     1260166 :         has_not_null |= satt->attnotnull;
    3533             :     }
    3534             : 
    3535      246706 :     if (has_not_null)
    3536             :     {
    3537       38718 :         TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
    3538             : 
    3539       38718 :         constr->has_not_null = true;
    3540       38718 :         rel->rd_att->constr = constr;
    3541             :     }
    3542             : 
    3543             :     /*
    3544             :      * initialize relation tuple form (caller may add/override data later)
    3545             :      */
    3546      246706 :     rel->rd_rel = (Form_pg_class) palloc0(CLASS_TUPLE_SIZE);
    3547             : 
    3548      246706 :     namestrcpy(&rel->rd_rel->relname, relname);
    3549      246706 :     rel->rd_rel->relnamespace = relnamespace;
    3550             : 
    3551      246706 :     rel->rd_rel->relkind = relkind;
    3552      246706 :     rel->rd_rel->relnatts = natts;
    3553      246706 :     rel->rd_rel->reltype = InvalidOid;
    3554             :     /* needed when bootstrapping: */
    3555      246706 :     rel->rd_rel->relowner = BOOTSTRAP_SUPERUSERID;
    3556             : 
    3557             :     /* set up persistence and relcache fields dependent on it */
    3558      246706 :     rel->rd_rel->relpersistence = relpersistence;
    3559      246706 :     switch (relpersistence)
    3560             :     {
    3561      243252 :         case RELPERSISTENCE_UNLOGGED:
    3562             :         case RELPERSISTENCE_PERMANENT:
    3563      243252 :             rel->rd_backend = InvalidBackendId;
    3564      243252 :             rel->rd_islocaltemp = false;
    3565      243252 :             break;
    3566        3454 :         case RELPERSISTENCE_TEMP:
    3567             :             Assert(isTempOrTempToastNamespace(relnamespace));
    3568        3454 :             rel->rd_backend = BackendIdForTempRelations();
    3569        3454 :             rel->rd_islocaltemp = true;
    3570        3454 :             break;
    3571           0 :         default:
    3572           0 :             elog(ERROR, "invalid relpersistence: %c", relpersistence);
    3573             :             break;
    3574             :     }
    3575             : 
    3576             :     /* if it's a materialized view, it's not populated initially */
    3577      246706 :     if (relkind == RELKIND_MATVIEW)
    3578         274 :         rel->rd_rel->relispopulated = false;
    3579             :     else
    3580      246432 :         rel->rd_rel->relispopulated = true;
    3581             : 
    3582             :     /* set replica identity -- system catalogs and non-tables don't have one */
    3583      246706 :     if (!IsCatalogNamespace(relnamespace) &&
    3584      101174 :         (relkind == RELKIND_RELATION ||
    3585      100900 :          relkind == RELKIND_MATVIEW ||
    3586             :          relkind == RELKIND_PARTITIONED_TABLE))
    3587       25560 :         rel->rd_rel->relreplident = REPLICA_IDENTITY_DEFAULT;
    3588             :     else
    3589      221146 :         rel->rd_rel->relreplident = REPLICA_IDENTITY_NOTHING;
    3590             : 
    3591             :     /*
    3592             :      * Insert relation physical and logical identifiers (OIDs) into the right
    3593             :      * places.  For a mapped relation, we set relfilenode to zero and rely on
    3594             :      * RelationInitPhysicalAddr to consult the map.
    3595             :      */
    3596      246706 :     rel->rd_rel->relisshared = shared_relation;
    3597             : 
    3598      246706 :     RelationGetRelid(rel) = relid;
    3599             : 
    3600     1506872 :     for (i = 0; i < natts; i++)
    3601     1260166 :         TupleDescAttr(rel->rd_att, i)->attrelid = relid;
    3602             : 
    3603      246706 :     rel->rd_rel->reltablespace = reltablespace;
    3604             : 
    3605      246706 :     if (mapped_relation)
    3606             :     {
    3607       29060 :         rel->rd_rel->relfilenode = InvalidOid;
    3608             :         /* Add it to the active mapping information */
    3609       29060 :         RelationMapUpdateMap(relid, relfilenode, shared_relation, true);
    3610             :     }
    3611             :     else
    3612      217646 :         rel->rd_rel->relfilenode = relfilenode;
    3613             : 
    3614      246706 :     RelationInitLockInfo(rel);  /* see lmgr.c */
    3615             : 
    3616      246706 :     RelationInitPhysicalAddr(rel);
    3617             : 
    3618      246706 :     rel->rd_rel->relam = accessmtd;
    3619             : 
    3620             :     /*
    3621             :      * RelationInitTableAccessMethod will do syscache lookups, so we mustn't
    3622             :      * run it in CacheMemoryContext.  Fortunately, the remaining steps don't
    3623             :      * require a long-lived current context.
    3624             :      */
    3625      246706 :     MemoryContextSwitchTo(oldcxt);
    3626             : 
    3627      246706 :     if (RELKIND_HAS_TABLE_AM(relkind) || relkind == RELKIND_SEQUENCE)
    3628       80592 :         RelationInitTableAccessMethod(rel);
    3629             : 
    3630             :     /*
    3631             :      * Okay to insert into the relcache hash table.
    3632             :      *
    3633             :      * Ordinarily, there should certainly not be an existing hash entry for
    3634             :      * the same OID; but during bootstrap, when we create a "real" relcache
    3635             :      * entry for one of the bootstrap relations, we'll be overwriting the
    3636             :      * phony one created with formrdesc.  So allow that to happen for nailed
    3637             :      * rels.
    3638             :      */
    3639      246706 :     RelationCacheInsert(rel, nailit);
    3640             : 
    3641             :     /*
    3642             :      * Flag relation as needing eoxact cleanup (to clear rd_createSubid). We
    3643             :      * can't do this before storing relid in it.
    3644             :      */
    3645      246706 :     EOXactListAdd(rel);
    3646             : 
    3647             :     /* It's fully valid */
    3648      246706 :     rel->rd_isvalid = true;
    3649             : 
    3650             :     /*
    3651             :      * Caller expects us to pin the returned entry.
    3652             :      */
    3653      246706 :     RelationIncrementReferenceCount(rel);
    3654             : 
    3655      246706 :     return rel;
    3656             : }
    3657             : 
    3658             : 
    3659             : /*
    3660             :  * RelationSetNewRelfilenode
    3661             :  *
    3662             :  * Assign a new relfilenode (physical file name), and possibly a new
    3663             :  * persistence setting, to the relation.
    3664             :  *
    3665             :  * This allows a full rewrite of the relation to be done with transactional
    3666             :  * safety (since the filenode assignment can be rolled back).  Note however
    3667             :  * that there is no simple way to access the relation's old data for the
    3668             :  * remainder of the current transaction.  This limits the usefulness to cases
    3669             :  * such as TRUNCATE or rebuilding an index from scratch.
    3670             :  *
    3671             :  * Caller must already hold exclusive lock on the relation.
    3672             :  */
    3673             : void
    3674        9912 : RelationSetNewRelfilenode(Relation relation, char persistence)
    3675             : {
    3676             :     Oid         newrelfilenode;
    3677             :     Relation    pg_class;
    3678             :     HeapTuple   tuple;
    3679             :     Form_pg_class classform;
    3680        9912 :     MultiXactId minmulti = InvalidMultiXactId;
    3681        9912 :     TransactionId freezeXid = InvalidTransactionId;
    3682             :     RelFileNode newrnode;
    3683             : 
    3684             :     /* Allocate a new relfilenode */
    3685        9912 :     newrelfilenode = GetNewRelFileNode(relation->rd_rel->reltablespace, NULL,
    3686             :                                        persistence);
    3687             : 
    3688             :     /*
    3689             :      * Get a writable copy of the pg_class tuple for the given relation.
    3690             :      */
    3691        9912 :     pg_class = table_open(RelationRelationId, RowExclusiveLock);
    3692             : 
    3693        9912 :     tuple = SearchSysCacheCopy1(RELOID,
    3694             :                                 ObjectIdGetDatum(RelationGetRelid(relation)));
    3695        9912 :     if (!HeapTupleIsValid(tuple))
    3696           0 :         elog(ERROR, "could not find tuple for relation %u",
    3697             :              RelationGetRelid(relation));
    3698        9912 :     classform = (Form_pg_class) GETSTRUCT(tuple);
    3699             : 
    3700             :     /*
    3701             :      * Schedule unlinking of the old storage at transaction commit.
    3702             :      */
    3703        9912 :     RelationDropStorage(relation);
    3704             : 
    3705             :     /*
    3706             :      * Create storage for the main fork of the new relfilenode.  If it's a
    3707             :      * table-like object, call into the table AM to do so, which'll also
    3708             :      * create the table's init fork if needed.
    3709             :      *
    3710             :      * NOTE: If relevant for the AM, any conflict in relfilenode value will be
    3711             :      * caught here, if GetNewRelFileNode messes up for any reason.
    3712             :      */
    3713        9912 :     newrnode = relation->rd_node;
    3714        9912 :     newrnode.relNode = newrelfilenode;
    3715             : 
    3716        9912 :     if (RELKIND_HAS_TABLE_AM(relation->rd_rel->relkind))
    3717             :     {
    3718        2498 :         table_relation_set_new_filenode(relation, &newrnode,
    3719             :                                         persistence,
    3720             :                                         &freezeXid, &minmulti);
    3721             :     }
    3722        7414 :     else if (RELKIND_HAS_STORAGE(relation->rd_rel->relkind))
    3723        7414 :     {
    3724             :         /* handle these directly, at least for now */
    3725             :         SMgrRelation srel;
    3726             : 
    3727        7414 :         srel = RelationCreateStorage(newrnode, persistence);
    3728        7414 :         smgrclose(srel);
    3729             :     }
    3730             :     else
    3731             :     {
    3732             :         /* we shouldn't be called for anything else */
    3733           0 :         elog(ERROR, "relation \"%s\" does not have storage",
    3734             :              RelationGetRelationName(relation));
    3735             :     }
    3736             : 
    3737             :     /*
    3738             :      * If we're dealing with a mapped index, pg_class.relfilenode doesn't
    3739             :      * change; instead we have to send the update to the relation mapper.
    3740             :      *
    3741             :      * For mapped indexes, we don't actually change the pg_class entry at all;
    3742             :      * this is essential when reindexing pg_class itself.  That leaves us with
    3743             :      * possibly-inaccurate values of relpages etc, but those will be fixed up
    3744             :      * later.
    3745             :      */
    3746        9912 :     if (RelationIsMapped(relation))
    3747             :     {
    3748             :         /* This case is only supported for indexes */
    3749             :         Assert(relation->rd_rel->relkind == RELKIND_INDEX);
    3750             : 
    3751             :         /* Since we're not updating pg_class, these had better not change */
    3752             :         Assert(classform->relfrozenxid == freezeXid);
    3753             :         Assert(classform->relminmxid == minmulti);
    3754             :         Assert(classform->relpersistence == persistence);
    3755             : 
    3756             :         /*
    3757             :          * In some code paths it's possible that the tuple update we'd
    3758             :          * otherwise do here is the only thing that would assign an XID for
    3759             :          * the current transaction.  However, we must have an XID to delete
    3760             :          * files, so make sure one is assigned.
    3761             :          */
    3762        1228 :         (void) GetCurrentTransactionId();
    3763             : 
    3764             :         /* Do the deed */
    3765        1228 :         RelationMapUpdateMap(RelationGetRelid(relation),
    3766             :                              newrelfilenode,
    3767        1228 :                              relation->rd_rel->relisshared,
    3768             :                              false);
    3769             : 
    3770             :         /* Since we're not updating pg_class, must trigger inval manually */
    3771        1228 :         CacheInvalidateRelcache(relation);
    3772             :     }
    3773             :     else
    3774             :     {
    3775             :         /* Normal case, update the pg_class entry */
    3776        8684 :         classform->relfilenode = newrelfilenode;
    3777             : 
    3778             :         /* relpages etc. never change for sequences */
    3779        8684 :         if (relation->rd_rel->relkind != RELKIND_SEQUENCE)
    3780             :         {
    3781        8556 :             classform->relpages = 0; /* it's empty until further notice */
    3782        8556 :             classform->reltuples = -1;
    3783        8556 :             classform->relallvisible = 0;
    3784             :         }
    3785        8684 :         classform->relfrozenxid = freezeXid;
    3786        8684 :         classform->relminmxid = minmulti;
    3787        8684 :         classform->relpersistence = persistence;
    3788             : 
    3789        8684 :         CatalogTupleUpdate(pg_class, &tuple->t_self, tuple);
    3790             :     }
    3791             : 
    3792        9912 :     heap_freetuple(tuple);
    3793             : 
    3794        9912 :     table_close(pg_class, RowExclusiveLock);
    3795             : 
    3796             :     /*
    3797             :      * Make the pg_class row change or relation map change visible.  This will
    3798             :      * cause the relcache entry to get updated, too.
    3799             :      */
    3800        9912 :     CommandCounterIncrement();
    3801             : 
    3802        9912 :     RelationAssumeNewRelfilenode(relation);
    3803        9912 : }
    3804             : 
    3805             : /*
    3806             :  * RelationAssumeNewRelfilenode
    3807             :  *
    3808             :  * Code that modifies pg_class.reltablespace or pg_class.relfilenode must call
    3809             :  * this.  The call shall precede any code that might insert WAL records whose
    3810             :  * replay would modify bytes in the new RelFileNode, and the call shall follow
    3811             :  * any WAL modifying bytes in the prior RelFileNode.  See struct RelationData.
    3812             :  * Ideally, call this as near as possible to the CommandCounterIncrement()
    3813             :  * that makes the pg_class change visible (before it or after it); that
    3814             :  * minimizes the chance of future development adding a forbidden WAL insertion
    3815             :  * between RelationAssumeNewRelfilenode() and CommandCounterIncrement().
    3816             :  */
    3817             : void
    3818       11200 : RelationAssumeNewRelfilenode(Relation relation)
    3819             : {
    3820       11200 :     relation->rd_newRelfilenodeSubid = GetCurrentSubTransactionId();
    3821       11200 :     if (relation->rd_firstRelfilenodeSubid == InvalidSubTransactionId)
    3822       11130 :         relation->rd_firstRelfilenodeSubid = relation->rd_newRelfilenodeSubid;
    3823             : 
    3824             :     /* Flag relation as needing eoxact cleanup (to clear these fields) */
    3825       11200 :     EOXactListAdd(relation);
    3826       11200 : }
    3827             : 
    3828             : 
    3829             : /*
    3830             :  *      RelationCacheInitialize
    3831             :  *
    3832             :  *      This initializes the relation descriptor cache.  At the time
    3833             :  *      that this is invoked, we can't do database access yet (mainly
    3834             :  *      because the transaction subsystem is not up); all we are doing
    3835             :  *      is making an empty cache hashtable.  This must be done before
    3836             :  *      starting the initialization transaction, because otherwise
    3837             :  *      AtEOXact_RelationCache would crash if that transaction aborts
    3838             :  *      before we can get the relcache set up.
    3839             :  */
    3840             : 
    3841             : #define INITRELCACHESIZE        400
    3842             : 
    3843             : void
    3844       15928 : RelationCacheInitialize(void)
    3845             : {
    3846             :     HASHCTL     ctl;
    3847             :     int         allocsize;
    3848             : 
    3849             :     /*
    3850             :      * make sure cache memory context exists
    3851             :      */
    3852       15928 :     if (!CacheMemoryContext)
    3853       15928 :         CreateCacheMemoryContext();
    3854             : 
    3855             :     /*
    3856             :      * create hashtable that indexes the relcache
    3857             :      */
    3858       15928 :     ctl.keysize = sizeof(Oid);
    3859       15928 :     ctl.entrysize = sizeof(RelIdCacheEnt);
    3860       15928 :     RelationIdCache = hash_create("Relcache by OID", INITRELCACHESIZE,
    3861             :                                   &ctl, HASH_ELEM | HASH_BLOBS);
    3862             : 
    3863             :     /*
    3864             :      * reserve enough in_progress_list slots for many cases
    3865             :      */
    3866       15928 :     allocsize = 4;
    3867       15928 :     in_progress_list =
    3868       15928 :         MemoryContextAlloc(CacheMemoryContext,
    3869             :                            allocsize * sizeof(*in_progress_list));
    3870       15928 :     in_progress_list_maxlen = allocsize;
    3871             : 
    3872             :     /*
    3873             :      * relation mapper needs to be initialized too
    3874             :      */
    3875       15928 :     RelationMapInitialize();
    3876       15928 : }
    3877             : 
    3878             : /*
    3879             :  *      RelationCacheInitializePhase2
    3880             :  *
    3881             :  *      This is called to prepare for access to shared catalogs during startup.
    3882             :  *      We must at least set up nailed reldescs for pg_database, pg_authid,
    3883             :  *      pg_auth_members, and pg_shseclabel. Ideally we'd like to have reldescs
    3884             :  *      for their indexes, too.  We attempt to load this information from the
    3885             :  *      shared relcache init file.  If that's missing or broken, just make
    3886             :  *      phony entries for the catalogs themselves.
    3887             :  *      RelationCacheInitializePhase3 will clean up as needed.
    3888             :  */
    3889             : void
    3890       15928 : RelationCacheInitializePhase2(void)
    3891             : {
    3892             :     MemoryContext oldcxt;
    3893             : 
    3894             :     /*
    3895             :      * relation mapper needs initialized too
    3896             :      */
    3897       15928 :     RelationMapInitializePhase2();
    3898             : 
    3899             :     /*
    3900             :      * In bootstrap mode, the shared catalogs aren't there yet anyway, so do
    3901             :      * nothing.
    3902             :      */
    3903       15928 :     if (IsBootstrapProcessingMode())
    3904         482 :         return;
    3905             : 
    3906             :     /*
    3907             :      * switch to cache memory context
    3908             :      */
    3909       15446 :     oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
    3910             : 
    3911             :     /*
    3912             :      * Try to load the shared relcache cache file.  If unsuccessful, bootstrap
    3913             :      * the cache with pre-made descriptors for the critical shared catalogs.
    3914             :      */
    3915       15446 :     if (!load_relcache_init_file(true))
    3916             :     {
    3917        2648 :         formrdesc("pg_database", DatabaseRelation_Rowtype_Id, true,
    3918             :                   Natts_pg_database, Desc_pg_database);
    3919        2648 :         formrdesc("pg_authid", AuthIdRelation_Rowtype_Id, true,
    3920             :                   Natts_pg_authid, Desc_pg_authid);
    3921        2648 :         formrdesc("pg_auth_members", AuthMemRelation_Rowtype_Id, true,
    3922             :                   Natts_pg_auth_members, Desc_pg_auth_members);
    3923        2648 :         formrdesc("pg_shseclabel", SharedSecLabelRelation_Rowtype_Id, true,
    3924             :                   Natts_pg_shseclabel, Desc_pg_shseclabel);
    3925        2648 :         formrdesc("pg_subscription", SubscriptionRelation_Rowtype_Id, true,
    3926             :                   Natts_pg_subscription, Desc_pg_subscription);
    3927             : 
    3928             : #define NUM_CRITICAL_SHARED_RELS    5   /* fix if you change list above */
    3929             :     }
    3930             : 
    3931       15446 :     MemoryContextSwitchTo(oldcxt);
    3932             : }
    3933             : 
    3934             : /*
    3935             :  *      RelationCacheInitializePhase3
    3936             :  *
    3937             :  *      This is called as soon as the catcache and transaction system
    3938             :  *      are functional and we have determined MyDatabaseId.  At this point
    3939             :  *      we can actually read data from the database's system catalogs.
    3940             :  *      We first try to read pre-computed relcache entries from the local
    3941             :  *      relcache init file.  If that's missing or broken, make phony entries
    3942             :  *      for the minimum set of nailed-in-cache relations.  Then (unless
    3943             :  *      bootstrapping) make sure we have entries for the critical system
    3944             :  *      indexes.  Once we've done all this, we have enough infrastructure to
    3945             :  *      open any system catalog or use any catcache.  The last step is to
    3946             :  *      rewrite the cache files if needed.
    3947             :  */
    3948             : void
    3949       14382 : RelationCacheInitializePhase3(void)
    3950             : {
    3951             :     HASH_SEQ_STATUS status;
    3952             :     RelIdCacheEnt *idhentry;
    3953             :     MemoryContext oldcxt;
    3954       14382 :     bool        needNewCacheFile = !criticalSharedRelcachesBuilt;
    3955             : 
    3956             :     /*
    3957             :      * relation mapper needs initialized too
    3958             :      */
    3959       14382 :     RelationMapInitializePhase3();
    3960             : 
    3961             :     /*
    3962             :      * switch to cache memory context
    3963             :      */
    3964       14382 :     oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
    3965             : 
    3966             :     /*
    3967             :      * Try to load the local relcache cache file.  If unsuccessful, bootstrap
    3968             :      * the cache with pre-made descriptors for the critical "nailed-in" system
    3969             :      * catalogs.
    3970             :      */
    3971       14382 :     if (IsBootstrapProcessingMode() ||
    3972       13900 :         !load_relcache_init_file(false))
    3973             :     {
    3974        2310 :         needNewCacheFile = true;
    3975             : 
    3976        2310 :         formrdesc("pg_class", RelationRelation_Rowtype_Id, false,
    3977             :                   Natts_pg_class, Desc_pg_class);
    3978        2310 :         formrdesc("pg_attribute", AttributeRelation_Rowtype_Id, false,
    3979             :                   Natts_pg_attribute, Desc_pg_attribute);
    3980        2310 :         formrdesc("pg_proc", ProcedureRelation_Rowtype_Id, false,
    3981             :                   Natts_pg_proc, Desc_pg_proc);
    3982        2310 :         formrdesc("pg_type", TypeRelation_Rowtype_Id, false,
    3983             :                   Natts_pg_type, Desc_pg_type);
    3984             : 
    3985             : #define NUM_CRITICAL_LOCAL_RELS 4   /* fix if you change list above */
    3986             :     }
    3987             : 
    3988       14382 :     MemoryContextSwitchTo(oldcxt);
    3989             : 
    3990             :     /* In bootstrap mode, the faked-up formrdesc info is all we'll have */
    3991       14382 :     if (IsBootstrapProcessingMode())
    3992         482 :         return;
    3993             : 
    3994             :     /*
    3995             :      * If we didn't get the critical system indexes loaded into relcache, do
    3996             :      * so now.  These are critical because the catcache and/or opclass cache
    3997             :      * depend on them for fetches done during relcache load.  Thus, we have an
    3998             :      * infinite-recursion problem.  We can break the recursion by doing
    3999             :      * heapscans instead of indexscans at certain key spots. To avoid hobbling
    4000             :      * performance, we only want to do that until we have the critical indexes
    4001             :      * loaded into relcache.  Thus, the flag criticalRelcachesBuilt is used to
    4002             :      * decide whether to do heapscan or indexscan at the key spots, and we set
    4003             :      * it true after we've loaded the critical indexes.
    4004             :      *
    4005             :      * The critical indexes are marked as "nailed in cache", partly to make it
    4006             :      * easy for load_relcache_init_file to count them, but mainly because we
    4007             :      * cannot flush and rebuild them once we've set criticalRelcachesBuilt to
    4008             :      * true.  (NOTE: perhaps it would be possible to reload them by
    4009             :      * temporarily setting criticalRelcachesBuilt to false again.  For now,
    4010             :      * though, we just nail 'em in.)
    4011             :      *
    4012             :      * RewriteRelRulenameIndexId and TriggerRelidNameIndexId are not critical
    4013             :      * in the same way as the others, because the critical catalogs don't
    4014             :      * (currently) have any rules or triggers, and so these indexes can be
    4015             :      * rebuilt without inducing recursion.  However they are used during
    4016             :      * relcache load when a rel does have rules or triggers, so we choose to
    4017             :      * nail them for performance reasons.
    4018             :      */
    4019       13900 :     if (!criticalRelcachesBuilt)
    4020             :     {
    4021        1828 :         load_critical_index(ClassOidIndexId,
    4022             :                             RelationRelationId);
    4023        1826 :         load_critical_index(AttributeRelidNumIndexId,
    4024             :                             AttributeRelationId);
    4025        1826 :         load_critical_index(IndexRelidIndexId,
    4026             :                             IndexRelationId);
    4027        1826 :         load_critical_index(OpclassOidIndexId,
    4028             :                             OperatorClassRelationId);
    4029        1826 :         load_critical_index(AccessMethodProcedureIndexId,
    4030             :                             AccessMethodProcedureRelationId);
    4031        1826 :         load_critical_index(RewriteRelRulenameIndexId,
    4032             :                             RewriteRelationId);
    4033        1826 :         load_critical_index(TriggerRelidNameIndexId,
    4034             :                             TriggerRelationId);
    4035             : 
    4036             : #define NUM_CRITICAL_LOCAL_INDEXES  7   /* fix if you change list above */
    4037             : 
    4038        1826 :         criticalRelcachesBuilt = true;
    4039             :     }
    4040             : 
    4041             :     /*
    4042             :      * Process critical shared indexes too.
    4043             :      *
    4044             :      * DatabaseNameIndexId isn't critical for relcache loading, but rather for
    4045             :      * initial lookup of MyDatabaseId, without which we'll never find any
    4046             :      * non-shared catalogs at all.  Autovacuum calls InitPostgres with a
    4047             :      * database OID, so it instead depends on DatabaseOidIndexId.  We also
    4048             :      * need to nail up some indexes on pg_authid and pg_auth_members for use
    4049             :      * during client authentication.  SharedSecLabelObjectIndexId isn't
    4050             :      * critical for the core system, but authentication hooks might be
    4051             :      * interested in it.
    4052             :      */
    4053       13898 :     if (!criticalSharedRelcachesBuilt)
    4054             :     {
    4055        1490 :         load_critical_index(DatabaseNameIndexId,
    4056             :                             DatabaseRelationId);
    4057        1490 :         load_critical_index(DatabaseOidIndexId,
    4058             :                             DatabaseRelationId);
    4059        1490 :         load_critical_index(AuthIdRolnameIndexId,
    4060             :                             AuthIdRelationId);
    4061        1490 :         load_critical_index(AuthIdOidIndexId,
    4062             :                             AuthIdRelationId);
    4063        1490 :         load_critical_index(AuthMemMemRoleIndexId,
    4064             :                             AuthMemRelationId);
    4065        1490 :         load_critical_index(SharedSecLabelObjectIndexId,
    4066             :                             SharedSecLabelRelationId);
    4067             : 
    4068             : #define NUM_CRITICAL_SHARED_INDEXES 6   /* fix if you change list above */
    4069             : 
    4070        1490 :         criticalSharedRelcachesBuilt = true;
    4071             :     }
    4072             : 
    4073             :     /*
    4074             :      * Now, scan all the relcache entries and update anything that might be
    4075             :      * wrong in the results from formrdesc or the relcache cache file. If we
    4076             :      * faked up relcache entries using formrdesc, then read the real pg_class
    4077             :      * rows and replace the fake entries with them. Also, if any of the
    4078             :      * relcache entries have rules, triggers, or security policies, load that
    4079             :      * info the hard way since it isn't recorded in the cache file.
    4080             :      *
    4081             :      * Whenever we access the catalogs to read data, there is a possibility of
    4082             :      * a shared-inval cache flush causing relcache entries to be removed.
    4083             :      * Since hash_seq_search only guarantees to still work after the *current*
    4084             :      * entry is removed, it's unsafe to continue the hashtable scan afterward.
    4085             :      * We handle this by restarting the scan from scratch after each access.
    4086             :      * This is theoretically O(N^2), but the number of entries that actually
    4087             :      * need to be fixed is small enough that it doesn't matter.
    4088             :      */
    4089       13898 :     hash_seq_init(&status, RelationIdCache);
    4090             : 
    4091     1888378 :     while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
    4092             :     {
    4093     1874480 :         Relation    relation = idhentry->reldesc;
    4094     1874480 :         bool        restart = false;
    4095             : 
    4096             :         /*
    4097             :          * Make sure *this* entry doesn't get flushed while we work with it.
    4098             :          */
    4099     1874480 :         RelationIncrementReferenceCount(relation);
    4100             : 
    4101             :         /*
    4102             :          * If it's a faked-up entry, read the real pg_class tuple.
    4103             :          */
    4104     1874480 :         if (relation->rd_rel->relowner == InvalidOid)
    4105             :         {
    4106             :             HeapTuple   htup;
    4107             :             Form_pg_class relp;
    4108             : 
    4109       14754 :             htup = SearchSysCache1(RELOID,
    4110       14754 :                                    ObjectIdGetDatum(RelationGetRelid(relation)));
    4111       14754 :             if (!HeapTupleIsValid(htup))
    4112           0 :                 elog(FATAL, "cache lookup failed for relation %u",
    4113             :                      RelationGetRelid(relation));
    4114       14754 :             relp = (Form_pg_class) GETSTRUCT(htup);
    4115             : 
    4116             :             /*
    4117             :              * Copy tuple to relation->rd_rel. (See notes in
    4118             :              * AllocateRelationDesc())
    4119             :              */
    4120       14754 :             memcpy((char *) relation->rd_rel, (char *) relp, CLASS_TUPLE_SIZE);
    4121             : 
    4122             :             /* Update rd_options while we have the tuple */
    4123       14754 :             if (relation->rd_options)
    4124           0 :                 pfree(relation->rd_options);
    4125       14754 :             RelationParseRelOptions(relation, htup);
    4126             : 
    4127             :             /*
    4128             :              * Check the values in rd_att were set up correctly.  (We cannot
    4129             :              * just copy them over now: formrdesc must have set up the rd_att
    4130             :              * data correctly to start with, because it may already have been
    4131             :              * copied into one or more catcache entries.)
    4132             :              */
    4133             :             Assert(relation->rd_att->tdtypeid == relp->reltype);
    4134             :             Assert(relation->rd_att->tdtypmod == -1);
    4135             : 
    4136       14754 :             ReleaseSysCache(htup);
    4137             : 
    4138             :             /* relowner had better be OK now, else we'll loop forever */
    4139       14754 :             if (relation->rd_rel->relowner == InvalidOid)
    4140           0 :                 elog(ERROR, "invalid relowner in pg_class entry for \"%s\"",
    4141             :                      RelationGetRelationName(relation));
    4142             : 
    4143       14754 :             restart = true;
    4144             :         }
    4145             : 
    4146             :         /*
    4147             :          * Fix data that isn't saved in relcache cache file.
    4148             :          *
    4149             :          * relhasrules or relhastriggers could possibly be wrong or out of
    4150             :          * date.  If we don't actually find any rules or triggers, clear the
    4151             :          * local copy of the flag so that we don't get into an infinite loop
    4152             :          * here.  We don't make any attempt to fix the pg_class entry, though.
    4153             :          */
    4154     1874480 :         if (relation->rd_rel->relhasrules && relation->rd_rules == NULL)
    4155             :         {
    4156           0 :             RelationBuildRuleLock(relation);
    4157           0 :             if (relation->rd_rules == NULL)
    4158           0 :                 relation->rd_rel->relhasrules = false;
    4159           0 :             restart = true;
    4160             :         }
    4161     1874480 :         if (relation->rd_rel->relhastriggers && relation->trigdesc == NULL)
    4162             :         {
    4163           0 :             RelationBuildTriggers(relation);
    4164           0 :             if (relation->trigdesc == NULL)
    4165           0 :                 relation->rd_rel->relhastriggers = false;
    4166           0 :             restart = true;
    4167             :         }
    4168             : 
    4169             :         /*
    4170             :          * Re-load the row security policies if the relation has them, since
    4171             :          * they are not preserved in the cache.  Note that we can never NOT
    4172             :          * have a policy while relrowsecurity is true,
    4173             :          * RelationBuildRowSecurity will create a single default-deny policy
    4174             :          * if there is no policy defined in pg_policy.
    4175             :          */
    4176     1874480 :         if (relation->rd_rel->relrowsecurity && relation->rd_rsdesc == NULL)
    4177             :         {
    4178           0 :             RelationBuildRowSecurity(relation);
    4179             : 
    4180             :             Assert(relation->rd_rsdesc != NULL);
    4181           0 :             restart = true;
    4182             :         }
    4183             : 
    4184             :         /* Reload tableam data if needed */
    4185     1874480 :         if (relation->rd_tableam == NULL &&
    4186     1133068 :             (RELKIND_HAS_TABLE_AM(relation->rd_rel->relkind) || relation->rd_rel->relkind == RELKIND_SEQUENCE))
    4187             :         {
    4188           0 :             RelationInitTableAccessMethod(relation);
    4189             :             Assert(relation->rd_tableam != NULL);
    4190             : 
    4191           0 :             restart = true;
    4192             :         }
    4193             : 
    4194             :         /* Release hold on the relation */
    4195     1874480 :         RelationDecrementReferenceCount(relation);
    4196             : 
    4197             :         /* Now, restart the hashtable scan if needed */
    4198     1874480 :         if (restart)
    4199             :         {
    4200       14754 :             hash_seq_term(&status);
    4201       14754 :             hash_seq_init(&status, RelationIdCache);
    4202             :         }
    4203             :     }
    4204             : 
    4205             :     /*
    4206             :      * Lastly, write out new relcache cache files if needed.  We don't bother
    4207             :      * to distinguish cases where only one of the two needs an update.
    4208             :      */
    4209       13898 :     if (needNewCacheFile)
    4210             :     {
    4211             :         /*
    4212             :          * Force all the catcaches to finish initializing and thereby open the
    4213             :          * catalogs and indexes they use.  This will preload the relcache with
    4214             :          * entries for all the most important system catalogs and indexes, so
    4215             :          * that the init files will be most useful for future backends.
    4216             :          */
    4217        1842 :         InitCatalogCachePhase2();
    4218             : 
    4219             :         /* now write the files */
    4220        1840 :         write_relcache_init_file(true);
    4221        1840 :         write_relcache_init_file(false);
    4222             :     }
    4223             : }
    4224             : 
    4225             : /*
    4226             :  * Load one critical system index into the relcache
    4227             :  *
    4228             :  * indexoid is the OID of the target index, heapoid is the OID of the catalog
    4229             :  * it belongs to.
    4230             :  */
    4231             : static void
    4232       21724 : load_critical_index(Oid indexoid, Oid heapoid)
    4233             : {
    4234             :     Relation    ird;
    4235             : 
    4236             :     /*
    4237             :      * We must lock the underlying catalog before locking the index to avoid
    4238             :      * deadlock, since RelationBuildDesc might well need to read the catalog,
    4239             :      * and if anyone else is exclusive-locking this catalog and index they'll
    4240             :      * be doing it in that order.
    4241             :      */
    4242       21724 :     LockRelationOid(heapoid, AccessShareLock);
    4243       21724 :     LockRelationOid(indexoid, AccessShareLock);
    4244       21724 :     ird = RelationBuildDesc(indexoid, true);
    4245       21722 :     if (ird == NULL)
    4246           0 :         elog(PANIC, "could not open critical system index %u", indexoid);
    4247       21722 :     ird->rd_isnailed = true;
    4248       21722 :     ird->rd_refcnt = 1;
    4249       21722 :     UnlockRelationOid(indexoid, AccessShareLock);
    4250       21722 :     UnlockRelationOid(heapoid, AccessShareLock);
    4251             : 
    4252       21722 :     (void) RelationGetIndexAttOptions(ird, false);
    4253       21722 : }
    4254             : 
    4255             : /*
    4256             :  * GetPgClassDescriptor -- get a predefined tuple descriptor for pg_class
    4257             :  * GetPgIndexDescriptor -- get a predefined tuple descriptor for pg_index
    4258             :  *
    4259             :  * We need this kluge because we have to be able to access non-fixed-width
    4260             :  * fields of pg_class and pg_index before we have the standard catalog caches
    4261             :  * available.  We use predefined data that's set up in just the same way as
    4262             :  * the bootstrapped reldescs used by formrdesc().  The resulting tupdesc is
    4263             :  * not 100% kosher: it does not have the correct rowtype OID in tdtypeid, nor
    4264             :  * does it have a TupleConstr field.  But it's good enough for the purpose of
    4265             :  * extracting fields.
    4266             :  */
    4267             : static TupleDesc
    4268       28760 : BuildHardcodedDescriptor(int natts, const FormData_pg_attribute *attrs)
    4269             : {
    4270             :     TupleDesc   result;
    4271             :     MemoryContext oldcxt;
    4272             :     int         i;
    4273             : 
    4274       28760 :     oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
    4275             : 
    4276       28760 :     result = CreateTemplateTupleDesc(natts);
    4277       28760 :     result->tdtypeid = RECORDOID;    /* not right, but we don't care */
    4278       28760 :     result->tdtypmod = -1;
    4279             : 
    4280      790926 :     for (i = 0; i < natts; i++)
    4281             :     {
    4282      762166 :         memcpy(TupleDescAttr(result, i), &attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
    4283             :         /* make sure attcacheoff is valid */
    4284      762166 :         TupleDescAttr(result, i)->attcacheoff = -1;
    4285             :     }
    4286             : 
    4287             :     /* initialize first attribute's attcacheoff, cf RelationBuildTupleDesc */
    4288       28760 :     TupleDescAttr(result, 0)->attcacheoff = 0;
    4289             : 
    4290             :     /* Note: we don't bother to set up a TupleConstr entry */
    4291             : 
    4292       28760 :     MemoryContextSwitchTo(oldcxt);
    4293             : 
    4294       28760 :     return result;
    4295             : }
    4296             : 
    4297             : static TupleDesc
    4298     1133832 : GetPgClassDescriptor(void)
    4299             : {
    4300             :     static TupleDesc pgclassdesc = NULL;
    4301             : 
    4302             :     /* Already done? */
    4303     1133832 :     if (pgclassdesc == NULL)
    4304       14382 :         pgclassdesc = BuildHardcodedDescriptor(Natts_pg_class,
    4305             :                                                Desc_pg_class);
    4306             : 
    4307     1133832 :     return pgclassdesc;
    4308             : }
    4309             : 
    4310             : static TupleDesc
    4311     1324804 : GetPgIndexDescriptor(void)
    4312             : {
    4313             :     static TupleDesc pgindexdesc = NULL;
    4314             : 
    4315             :     /* Already done? */
    4316     1324804 :     if (pgindexdesc == NULL)
    4317       14378 :         pgindexdesc = BuildHardcodedDescriptor(Natts_pg_index,
    4318             :                                                Desc_pg_index);
    4319             : 
    4320     1324804 :     return pgindexdesc;
    4321             : }
    4322             : 
    4323             : /*
    4324             :  * Load any default attribute value definitions for the relation.
    4325             :  *
    4326             :  * ndef is the number of attributes that were marked atthasdef.
    4327             :  *
    4328             :  * Note: we don't make it a hard error to be missing some pg_attrdef records.
    4329             :  * We can limp along as long as nothing needs to use the default value.  Code
    4330             :  * that fails to find an expected AttrDefault record should throw an error.
    4331             :  */
    4332             : static void
    4333       15204 : AttrDefaultFetch(Relation relation, int ndef)
    4334             : {
    4335             :     AttrDefault *attrdef;
    4336             :     Relation    adrel;
    4337             :     SysScanDesc adscan;
    4338             :     ScanKeyData skey;
    4339             :     HeapTuple   htup;
    4340       15204 :     int         found = 0;
    4341             : 
    4342             :     /* Allocate array with room for as many entries as expected */
    4343             :     attrdef = (AttrDefault *)
    4344       15204 :         MemoryContextAllocZero(CacheMemoryContext,
    4345             :                                ndef * sizeof(AttrDefault));
    4346             : 
    4347             :     /* Search pg_attrdef for relevant entries */
    4348       15204 :     ScanKeyInit(&skey,
    4349             :                 Anum_pg_attrdef_adrelid,
    4350             :                 BTEqualStrategyNumber, F_OIDEQ,
    4351       15204 :                 ObjectIdGetDatum(RelationGetRelid(relation)));
    4352             : 
    4353       15204 :     adrel = table_open(AttrDefaultRelationId, AccessShareLock);
    4354       15204 :     adscan = systable_beginscan(adrel, AttrDefaultIndexId, true,
    4355             :                                 NULL, 1, &skey);
    4356             : 
    4357       36878 :     while (HeapTupleIsValid(htup = systable_getnext(adscan)))
    4358             :     {
    4359       21674 :         Form_pg_attrdef adform = (Form_pg_attrdef) GETSTRUCT(htup);
    4360             :         Datum       val;
    4361             :         bool        isnull;
    4362             : 
    4363             :         /* protect limited size of array */
    4364       21674 :         if (found >= ndef)
    4365             :         {
    4366           0 :             elog(WARNING, "unexpected pg_attrdef record found for attribute %d of relation \"%s\"",
    4367             :                  adform->adnum, RelationGetRelationName(relation));
    4368           0 :             break;
    4369             :         }
    4370             : 
    4371       21674 :         val = fastgetattr(htup,
    4372             :                           Anum_pg_attrdef_adbin,
    4373             :                           adrel->rd_att, &isnull);
    4374       21674 :         if (isnull)
    4375           0 :             elog(WARNING, "null adbin for attribute %d of relation \"%s\"",
    4376             :                  adform->adnum, RelationGetRelationName(relation));
    4377             :         else
    4378             :         {
    4379             :             /* detoast and convert to cstring in caller's context */
    4380       21674 :             char       *s = TextDatumGetCString(val);
    4381             : 
    4382       21674 :             attrdef[found].adnum = adform->adnum;
    4383       21674 :             attrdef[found].adbin = MemoryContextStrdup(CacheMemoryContext, s);
    4384       21674 :             pfree(s);
    4385       21674 :             found++;
    4386             :         }
    4387             :     }
    4388             : 
    4389       15204 :     systable_endscan(adscan);
    4390       15204 :     table_close(adrel, AccessShareLock);
    4391             : 
    4392       15204 :     if (found != ndef)
    4393           0 :         elog(WARNING, "%d pg_attrdef record(s) missing for relation \"%s\"",
    4394             :              ndef - found, RelationGetRelationName(relation));
    4395             : 
    4396             :     /*
    4397             :      * Sort the AttrDefault entries by adnum, for the convenience of
    4398             :      * equalTupleDescs().  (Usually, they already will be in order, but this
    4399             :      * might not be so if systable_getnext isn't using an index.)
    4400             :      */
    4401       15204 :     if (found > 1)
    4402        3152 :         qsort(attrdef, found, sizeof(AttrDefault), AttrDefaultCmp);
    4403             : 
    4404             :     /* Install array only after it's fully valid */
    4405       15204 :     relation->rd_att->constr->defval = attrdef;
    4406       15204 :     relation->rd_att->constr->num_defval = found;
    4407       15204 : }
    4408             : 
    4409             : /*
    4410             :  * qsort comparator to sort AttrDefault entries by adnum
    4411             :  */
    4412             : static int
    4413        6470 : AttrDefaultCmp(const void *a, const void *b)
    4414             : {
    4415        6470 :     const AttrDefault *ada = (const AttrDefault *) a;
    4416        6470 :     const AttrDefault *adb = (const AttrDefault *) b;
    4417             : 
    4418        6470 :     return ada->adnum - adb->adnum;
    4419             : }
    4420             : 
    4421             : /*
    4422             :  * Load any check constraints for the relation.
    4423             :  *
    4424             :  * As with defaults, if we don't find the expected number of them, just warn
    4425             :  * here.  The executor should throw an error if an INSERT/UPDATE is attempted.
    4426             :  */
    4427             : static void
    4428        6670 : CheckConstraintFetch(Relation relation)
    4429             : {
    4430             :     ConstrCheck *check;
    4431        6670 :     int         ncheck = relation->rd_rel->relchecks;
    4432             :     Relation    conrel;
    4433             :     SysScanDesc conscan;
    4434             :     ScanKeyData skey[1];
    4435             :     HeapTuple   htup;
    4436        6670 :     int         found = 0;
    4437             : 
    4438             :     /* Allocate array with room for as many entries as expected */
    4439             :     check = (ConstrCheck *)
    4440        6670 :         MemoryContextAllocZero(CacheMemoryContext,
    4441             :                                ncheck * sizeof(ConstrCheck));
    4442             : 
    4443             :     /* Search pg_constraint for relevant entries */
    4444        6670 :     ScanKeyInit(&skey[0],
    4445             :                 Anum_pg_constraint_conrelid,
    4446             :                 BTEqualStrategyNumber, F_OIDEQ,
    4447        6670 :                 ObjectIdGetDatum(RelationGetRelid(relation)));
    4448             : 
    4449        6670 :     conrel = table_open(ConstraintRelationId, AccessShareLock);
    4450        6670 :     conscan = systable_beginscan(conrel, ConstraintRelidTypidNameIndexId, true,
    4451             :                                  NULL, 1, skey);
    4452             : 
    4453       17166 :     while (HeapTupleIsValid(htup = systable_getnext(conscan)))
    4454             :     {
    4455       10496 :         Form_pg_constraint conform = (Form_pg_constraint) GETSTRUCT(htup);
    4456             :         Datum       val;
    4457             :         bool        isnull;
    4458             : 
    4459             :         /* We want check constraints only */
    4460       10496 :         if (conform->contype != CONSTRAINT_CHECK)
    4461        1898 :             continue;
    4462             : 
    4463             :         /* protect limited size of array */
    4464        8598 :         if (found >= ncheck)
    4465             :         {
    4466           0 :             elog(WARNING, "unexpected pg_constraint record found for relation \"%s\"",
    4467             :                  RelationGetRelationName(relation));
    4468           0 :             break;
    4469             :         }
    4470             : 
    4471        8598 :         check[found].ccvalid = conform->convalidated;
    4472        8598 :         check[found].ccnoinherit = conform->connoinherit;
    4473       17196 :         check[found].ccname = MemoryContextStrdup(CacheMemoryContext,
    4474        8598 :                                                   NameStr(conform->conname));
    4475             : 
    4476             :         /* Grab and test conbin is actually set */
    4477        8598 :         val = fastgetattr(htup,
    4478             :                           Anum_pg_constraint_conbin,
    4479             :                           conrel->rd_att, &isnull);
    4480        8598 :         if (isnull)
    4481           0 :             elog(WARNING, "null conbin for relation \"%s\"",
    4482             :                  RelationGetRelationName(relation));
    4483             :         else
    4484             :         {
    4485             :             /* detoast and convert to cstring in caller's context */
    4486        8598 :             char       *s = TextDatumGetCString(val);
    4487             : 
    4488        8598 :             check[found].ccbin = MemoryContextStrdup(CacheMemoryContext, s);
    4489        8598 :             pfree(s);
    4490        8598 :             found++;
    4491             :         }
    4492             :     }
    4493             : 
    4494        6670 :     systable_endscan(conscan);
    4495        6670 :     table_close(conrel, AccessShareLock);
    4496             : 
    4497        6670 :     if (found != ncheck)
    4498           0 :         elog(WARNING, "%d pg_constraint record(s) missing for relation \"%s\"",
    4499             :              ncheck - found, RelationGetRelationName(relation));
    4500             : 
    4501             :     /*
    4502             :      * Sort the records by name.  This ensures that CHECKs are applied in a
    4503             :      * deterministic order, and it also makes equalTupleDescs() faster.
    4504             :      */
    4505        6670 :     if (found > 1)
    4506        1612 :         qsort(check, found, sizeof(ConstrCheck), CheckConstraintCmp);
    4507             : 
    4508             :     /* Install array only after it's fully valid */
    4509        6670 :     relation->rd_att->constr->check = check;
    4510        6670 :     relation->rd_att->constr->num_check = found;
    4511        6670 : }
    4512             : 
    4513             : /*
    4514             :  * qsort comparator to sort ConstrCheck entries by name
    4515             :  */
    4516             : static int
    4517        1928 : CheckConstraintCmp(const void *a, const void *b)
    4518             : {
    4519        1928 :     const ConstrCheck *ca = (const ConstrCheck *) a;
    4520        1928 :     const ConstrCheck *cb = (const ConstrCheck *) b;
    4521             : 
    4522        1928 :     return strcmp(ca->ccname, cb->ccname);
    4523             : }
    4524             : 
    4525             : /*
    4526             :  * RelationGetFKeyList -- get a list of foreign key info for the relation
    4527             :  *
    4528             :  * Returns a list of ForeignKeyCacheInfo structs, one per FK constraining
    4529             :  * the given relation.  This data is a direct copy of relevant fields from
    4530             :  * pg_constraint.  The list items are in no particular order.
    4531             :  *
    4532             :  * CAUTION: the returned list is part of the relcache's data, and could
    4533             :  * vanish in a relcache entry reset.  Callers must inspect or copy it
    4534             :  * before doing anything that might trigger a cache flush, such as
    4535             :  * system catalog accesses.  copyObject() can be used if desired.
    4536             :  * (We define it this way because current callers want to filter and
    4537             :  * modify the list entries anyway, so copying would be a waste of time.)
    4538             :  */
    4539             : List *
    4540      116106 : RelationGetFKeyList(Relation relation)
    4541             : {
    4542             :     List       *result;
    4543             :     Relation    conrel;
    4544             :     SysScanDesc conscan;
    4545             :     ScanKeyData skey;
    4546             :     HeapTuple   htup;
    4547             :     List       *oldlist;
    4548             :     MemoryContext oldcxt;
    4549             : 
    4550             :     /* Quick exit if we already computed the list. */
    4551      116106 :     if (relation->rd_fkeyvalid)
    4552         688 :         return relation->rd_fkeylist;
    4553             : 
    4554             :     /* Fast path: non-partitioned tables without triggers can't have FKs */
    4555      115418 :     if (!relation->rd_rel->relhastriggers &&
    4556      113720 :         relation->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
    4557      108018 :         return NIL;
    4558             : 
    4559             :     /*
    4560             :      * We build the list we intend to return (in the caller's context) while
    4561             :      * doing the scan.  After successfully completing the scan, we copy that
    4562             :      * list into the relcache entry.  This avoids cache-context memory leakage
    4563             :      * if we get some sort of error partway through.
    4564             :      */
    4565        7400 :     result = NIL;
    4566             : 
    4567             :     /* Prepare to scan pg_constraint for entries having conrelid = this rel. */
    4568        7400 :     ScanKeyInit(&skey,
    4569             :                 Anum_pg_constraint_conrelid,
    4570             :                 BTEqualStrategyNumber, F_OIDEQ,
    4571        7400 :                 ObjectIdGetDatum(RelationGetRelid(relation)));
    4572             : 
    4573        7400 :     conrel = table_open(ConstraintRelationId, AccessShareLock);
    4574        7400 :     conscan = systable_beginscan(conrel, ConstraintRelidTypidNameIndexId, true,
    4575             :                                  NULL, 1, &skey);
    4576             : 
    4577       10742 :     while (HeapTupleIsValid(htup = systable_getnext(conscan)))
    4578             :     {
    4579        3342 :         Form_pg_constraint constraint = (Form_pg_constraint) GETSTRUCT(htup);
    4580             :         ForeignKeyCacheInfo *info;
    4581             : 
    4582             :         /* consider only foreign keys */
    4583        3342 :         if (constraint->contype != CONSTRAINT_FOREIGN)
    4584        1542 :             continue;
    4585             : 
    4586        1800 :         info = makeNode(ForeignKeyCacheInfo);
    4587        1800 :         info->conoid = constraint->oid;
    4588        1800 :         info->conrelid = constraint->conrelid;
    4589        1800 :         info->confrelid = constraint->confrelid;
    4590             : 
    4591        1800 :         DeconstructFkConstraintRow(htup, &info->nkeys,
    4592        1800 :                                    info->conkey,
    4593        1800 :                                    info->confkey,
    4594        1800 :                                    info->conpfeqop,
    4595             :                                    NULL, NULL, NULL, NULL);
    4596             : 
    4597             :         /* Add FK's node to the result list */
    4598        1800 :         result = lappend(result, info);
    4599             :     }
    4600             : 
    4601        7400 :     systable_endscan(conscan);
    4602        7400 :     table_close(conrel, AccessShareLock);
    4603             : 
    4604             :     /* Now save a copy of the completed list in the relcache entry. */
    4605        7400 :     oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
    4606        7400 :     oldlist = relation->rd_fkeylist;
    4607        7400 :     relation->rd_fkeylist = copyObject(result);
    4608        7400 :     relation->rd_fkeyvalid = true;
    4609        7400 :     MemoryContextSwitchTo(oldcxt);
    4610             : 
    4611             :     /* Don't leak the old list, if there is one */
    4612        7400 :     list_free_deep(oldlist);
    4613             : 
    4614        7400 :     return result;
    4615             : }
    4616             : 
    4617             : /*
    4618             :  * RelationGetIndexList -- get a list of OIDs of indexes on this relation
    4619             :  *
    4620             :  * The index list is created only if someone requests it.  We scan pg_index
    4621             :  * to find relevant indexes, and add the list to the relcache entry so that
    4622             :  * we won't have to compute it again.  Note that shared cache inval of a
    4623             :  * relcache entry will delete the old list and set rd_indexvalid to false,
    4624             :  * so that we must recompute the index list on next request.  This handles
    4625             :  * creation or deletion of an index.
    4626             :  *
    4627             :  * Indexes that are marked not indislive are omitted from the returned list.
    4628             :  * Such indexes are expected to be dropped momentarily, and should not be
    4629             :  * touched at all by any caller of this function.
    4630             :  *
    4631             :  * The returned list is guaranteed to be sorted in order by OID.  This is
    4632             :  * needed by the executor, since for index types that we obtain exclusive
    4633             :  * locks on when updating the index, all backends must lock the indexes in
    4634             :  * the same order or we will get deadlocks (see ExecOpenIndices()).  Any
    4635             :  * consistent ordering would do, but ordering by OID is easy.
    4636             :  *
    4637             :  * Since shared cache inval causes the relcache's copy of the list to go away,
    4638             :  * we return a copy of the list palloc'd in the caller's context.  The caller
    4639             :  * may list_free() the returned list after scanning it. This is necessary
    4640             :  * since the caller will typically be doing syscache lookups on the relevant
    4641             :  * indexes, and syscache lookup could cause SI messages to be processed!
    4642             :  *
    4643             :  * In exactly the same way, we update rd_pkindex, which is the OID of the
    4644             :  * relation's primary key index if any, else InvalidOid; and rd_replidindex,
    4645             :  * which is the pg_class OID of an index to be used as the relation's
    4646             :  * replication identity index, or InvalidOid if there is no such index.
    4647             :  */
    4648             : List *
    4649     3339646 : RelationGetIndexList(Relation relation)
    4650             : {
    4651             :     Relation    indrel;
    4652             :     SysScanDesc indscan;
    4653             :     ScanKeyData skey;
    4654             :     HeapTuple   htup;
    4655             :     List       *result;
    4656             :     List       *oldlist;
    4657     3339646 :     char        replident = relation->rd_rel->relreplident;
    4658     3339646 :     Oid         pkeyIndex = InvalidOid;
    4659     3339646 :     Oid         candidateIndex = InvalidOid;
    4660             :     MemoryContext oldcxt;
    4661             : 
    4662             :     /* Quick exit if we already computed the list. */
    4663     3339646 :     if (relation->rd_indexvalid)
    4664     3151226 :         return list_copy(relation->rd_indexlist);
    4665             : 
    4666             :     /*
    4667             :      * We build the list we intend to return (in the caller's context) while
    4668             :      * doing the scan.  After successfully completing the scan, we copy that
    4669             :      * list into the relcache entry.  This avoids cache-context memory leakage
    4670             :      * if we get some sort of error partway through.
    4671             :      */
    4672      188420 :     result = NIL;
    4673             : 
    4674             :     /* Prepare to scan pg_index for entries having indrelid = this rel. */
    4675      188420 :     ScanKeyInit(&skey,
    4676             :                 Anum_pg_index_indrelid,
    4677             :                 BTEqualStrategyNumber, F_OIDEQ,
    4678      188420 :                 ObjectIdGetDatum(RelationGetRelid(relation)));
    4679             : 
    4680      188420 :     indrel = table_open(IndexRelationId, AccessShareLock);
    4681      188420 :     indscan = systable_beginscan(indrel, IndexIndrelidIndexId, true,
    4682             :                                  NULL, 1, &skey);
    4683             : 
    4684      473606 :     while (HeapTupleIsValid(htup = systable_getnext(indscan)))
    4685             :     {
    4686      285186 :         Form_pg_index index = (Form_pg_index) GETSTRUCT(htup);
    4687             : 
    4688             :         /*
    4689             :          * Ignore any indexes that are currently being dropped.  This will
    4690             :          * prevent them from being searched, inserted into, or considered in
    4691             :          * HOT-safety decisions.  It's unsafe to touch such an index at all
    4692             :          * since its catalog entries could disappear at any instant.
    4693             :          */
    4694      285186 :         if (!index->indislive)
    4695          56 :             continue;
    4696             : 
    4697             :         /* add index's OID to result list */
    4698      285130 :         result = lappend_oid(result, index->indexrelid);
    4699             : 
    4700             :         /*
    4701             :          * Invalid, non-unique, non-immediate or predicate indexes aren't
    4702             :          * interesting for either oid indexes or replication identity indexes,
    4703             :          * so don't check them.
    4704             :          */
    4705      285130 :         if (!index->indisvalid || !index->indisunique ||
    4706      242636 :             !index->indimmediate ||
    4707      242574 :             !heap_attisnull(htup, Anum_pg_index_indpred, NULL))
    4708       42644 :             continue;
    4709             : 
    4710             :         /* remember primary key index if any */
    4711      242486 :         if (index->indisprimary)
    4712      127414 :             pkeyIndex = index->indexrelid;
    4713             : 
    4714             :         /* remember explicitly chosen replica index */
    4715      242486 :         if (index->indisreplident)
    4716          48 :             candidateIndex = index->indexrelid;
    4717             :     }
    4718             : 
    4719      188420 :     systable_endscan(indscan);
    4720             : 
    4721      188420 :     table_close(indrel, AccessShareLock);
    4722             : 
    4723             :     /* Sort the result list into OID order, per API spec. */
    4724      188420 :     list_sort(result, list_oid_cmp);
    4725             : 
    4726             :     /* Now save a copy of the completed list in the relcache entry. */
    4727      188420 :     oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
    4728      188420 :     oldlist = relation->rd_indexlist;
    4729      188420 :     relation->rd_indexlist = list_copy(result);
    4730      188420 :     relation->rd_pkindex = pkeyIndex;
    4731      188420 :     if (replident == REPLICA_IDENTITY_DEFAULT && OidIsValid(pkeyIndex))
    4732       16410 :         relation->rd_replidindex = pkeyIndex;
    4733      172010 :     else if (replident == REPLICA_IDENTITY_INDEX && OidIsValid(candidateIndex))
    4734          48 :         relation->rd_replidindex = candidateIndex;
    4735             :     else
    4736      171962 :         relation->rd_replidindex = InvalidOid;
    4737      188420 :     relation->rd_indexvalid = true;
    4738      188420 :     MemoryContextSwitchTo(oldcxt);
    4739             : 
    4740             :     /* Don't leak the old list, if there is one */
    4741      188420 :     list_free(oldlist);
    4742             : 
    4743      188420 :     return result;
    4744             : }
    4745             : 
    4746             : /*
    4747             :  * RelationGetStatExtList
    4748             :  *      get a list of OIDs of statistics objects on this relation
    4749             :  *
    4750             :  * The statistics list is created only if someone requests it, in a way
    4751             :  * similar to RelationGetIndexList().  We scan pg_statistic_ext to find
    4752             :  * relevant statistics, and add the list to the relcache entry so that we
    4753             :  * won't have to compute it again.  Note that shared cache inval of a
    4754             :  * relcache entry will delete the old list and set rd_statvalid to 0,
    4755             :  * so that we must recompute the statistics list on next request.  This
    4756             :  * handles creation or deletion of a statistics object.
    4757             :  *
    4758             :  * The returned list is guaranteed to be sorted in order by OID, although
    4759             :  * this is not currently needed.
    4760             :  *
    4761             :  * Since shared cache inval causes the relcache's copy of the list to go away,
    4762             :  * we return a copy of the list palloc'd in the caller's context.  The caller
    4763             :  * may list_free() the returned list after scanning it. This is necessary
    4764             :  * since the caller will typically be doing syscache lookups on the relevant
    4765             :  * statistics, and syscache lookup could cause SI messages to be processed!
    4766             :  */
    4767             : List *
    4768      262650 : RelationGetStatExtList(Relation relation)
    4769             : {
    4770             :     Relation    indrel;
    4771             :     SysScanDesc indscan;
    4772             :     ScanKeyData skey;
    4773             :     HeapTuple   htup;
    4774             :     List       *result;
    4775             :     List       *oldlist;
    4776             :     MemoryContext oldcxt;
    4777             : 
    4778             :     /* Quick exit if we already computed the list. */
    4779      262650 :     if (relation->rd_statvalid != 0)
    4780      189604 :         return list_copy(relation->rd_statlist);
    4781             : 
    4782             :     /*
    4783             :      * We build the list we intend to return (in the caller's context) while
    4784             :      * doing the scan.  After successfully completing the scan, we copy that
    4785             :      * list into the relcache entry.  This avoids cache-context memory leakage
    4786             :      * if we get some sort of error partway through.
    4787             :      */
    4788       73046 :     result = NIL;
    4789             : 
    4790             :     /*
    4791             :      * Prepare to scan pg_statistic_ext for entries having stxrelid = this
    4792             :      * rel.
    4793             :      */
    4794       73046 :     ScanKeyInit(&skey,
    4795             :                 Anum_pg_statistic_ext_stxrelid,
    4796             :                 BTEqualStrategyNumber, F_OIDEQ,
    4797       73046 :                 ObjectIdGetDatum(RelationGetRelid(relation)));
    4798             : 
    4799       73046 :     indrel = table_open(StatisticExtRelationId, AccessShareLock);
    4800       73046 :     indscan = systable_beginscan(indrel, StatisticExtRelidIndexId, true,
    4801             :                                  NULL, 1, &skey);
    4802             : 
    4803       73262 :     while (HeapTupleIsValid(htup = systable_getnext(indscan)))
    4804             :     {
    4805         216 :         Oid         oid = ((Form_pg_statistic_ext) GETSTRUCT(htup))->oid;
    4806             : 
    4807         216 :         result = lappend_oid(result, oid);
    4808             :     }
    4809             : 
    4810       73046 :     systable_endscan(indscan);
    4811             : 
    4812       73046 :     table_close(indrel, AccessShareLock);
    4813             : 
    4814             :     /* Sort the result list into OID order, per API spec. */
    4815       73046 :     list_sort(result, list_oid_cmp);
    4816             : 
    4817             :     /* Now save a copy of the completed list in the relcache entry. */
    4818       73046 :     oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
    4819       73046 :     oldlist = relation->rd_statlist;
    4820       73046 :     relation->rd_statlist = list_copy(result);
    4821             : 
    4822       73046 :     relation->rd_statvalid = true;
    4823       73046 :     MemoryContextSwitchTo(oldcxt);
    4824             : 
    4825             :     /* Don't leak the old list, if there is one */
    4826       73046 :     list_free(oldlist);
    4827             : 
    4828       73046 :     return result;
    4829             : }
    4830             : 
    4831             : /*
    4832             :  * RelationGetPrimaryKeyIndex -- get OID of the relation's primary key index
    4833             :  *
    4834             :  * Returns InvalidOid if there is no such index.
    4835             :  */
    4836             : Oid
    4837         250 : RelationGetPrimaryKeyIndex(Relation relation)
    4838             : {
    4839             :     List       *ilist;
    4840             : 
    4841         250 :     if (!relation->rd_indexvalid)
    4842             :     {
    4843             :         /* RelationGetIndexList does the heavy lifting. */
    4844           0 :         ilist = RelationGetIndexList(relation);
    4845           0 :         list_free(ilist);
    4846             :         Assert(relation->rd_indexvalid);
    4847             :     }
    4848             : 
    4849         250 :     return relation->rd_pkindex;
    4850             : }
    4851             : 
    4852             : /*
    4853             :  * RelationGetReplicaIndex -- get OID of the relation's replica identity index
    4854             :  *
    4855             :  * Returns InvalidOid if there is no such index.
    4856             :  */
    4857             : Oid
    4858      247086 : RelationGetReplicaIndex(Relation relation)
    4859             : {
    4860             :     List       *ilist;
    4861             : 
    4862      247086 :     if (!relation->rd_indexvalid)
    4863             :     {
    4864             :         /* RelationGetIndexList does the heavy lifting. */
    4865        3774 :         ilist = RelationGetIndexList(relation);
    4866        3774 :         list_free(ilist);
    4867             :         Assert(relation->rd_indexvalid);
    4868             :     }
    4869             : 
    4870      247086 :     return relation->rd_replidindex;
    4871             : }
    4872             : 
    4873             : /*
    4874             :  * RelationGetIndexExpressions -- get the index expressions for an index
    4875             :  *
    4876             :  * We cache the result of transforming pg_index.indexprs into a node tree.
    4877             :  * If the rel is not an index or has no expressional columns, we return NIL.
    4878             :  * Otherwise, the returned tree is copied into the caller's memory context.
    4879             :  * (We don't want to return a pointer to the relcache copy, since it could
    4880             :  * disappear due to relcache invalidation.)
    4881             :  */
    4882             : List *
    4883     6283846 : RelationGetIndexExpressions(Relation relation)
    4884             : {
    4885             :     List       *result;
    4886             :     Datum       exprsDatum;
    4887             :     bool        isnull;
    4888             :     char       *exprsString;
    4889             :     MemoryContext oldcxt;
    4890             : 
    4891             :     /* Quick exit if we already computed the result. */
    4892     6283846 :     if (relation->rd_indexprs)
    4893        1700 :         return copyObject(relation->rd_indexprs);
    4894             : 
    4895             :     /* Quick exit if there is nothing to do. */
    4896    12564292 :     if (relation->rd_indextuple == NULL ||
    4897     6282146 :         heap_attisnull(relation->rd_indextuple, Anum_pg_index_indexprs, NULL))
    4898     6281400 :         return NIL;
    4899             : 
    4900             :     /*
    4901             :      * We build the tree we intend to return in the caller's context. After
    4902             :      * successfully completing the work, we copy it into the relcache entry.
    4903             :      * This avoids problems if we get some sort of error partway through.
    4904             :      */
    4905         746 :     exprsDatum = heap_getattr(relation->rd_indextuple,
    4906             :                               Anum_pg_index_indexprs,
    4907             :                               GetPgIndexDescriptor(),
    4908             :                               &isnull);
    4909             :     Assert(!isnull);
    4910         746 :     exprsString = TextDatumGetCString(exprsDatum);
    4911         746 :     result = (List *) stringToNode(exprsString);
    4912         746 :     pfree(exprsString);
    4913             : 
    4914             :     /*
    4915             :      * Run the expressions through eval_const_expressions. This is not just an
    4916             :      * optimization, but is necessary, because the planner will be comparing
    4917             :      * them to similarly-processed qual clauses, and may fail to detect valid
    4918             :      * matches without this.  We must not use canonicalize_qual, however,
    4919             :      * since these aren't qual expressions.
    4920             :      */
    4921         746 :     result = (List *) eval_const_expressions(NULL, (Node *) result);
    4922             : 
    4923             :     /* May as well fix opfuncids too */
    4924         744 :     fix_opfuncids((Node *) result);
    4925             : 
    4926             :     /* Now save a copy of the completed tree in the relcache entry. */
    4927         744 :     oldcxt = MemoryContextSwitchTo(relation->rd_indexcxt);
    4928         744 :     relation->rd_indexprs = copyObject(result);
    4929         744 :     MemoryContextSwitchTo(oldcxt);
    4930             : 
    4931         744 :     return result;
    4932             : }
    4933             : 
    4934             : /*
    4935             :  * RelationGetDummyIndexExpressions -- get dummy expressions for an index
    4936             :  *
    4937             :  * Return a list of dummy expressions (just Const nodes) with the same
    4938             :  * types/typmods/collations as the index's real expressions.  This is
    4939             :  * useful in situations where we don't want to run any user-defined code.
    4940             :  */
    4941             : List *
    4942         172 : RelationGetDummyIndexExpressions(Relation relation)
    4943             : {
    4944             :     List       *result;
    4945             :     Datum       exprsDatum;
    4946             :     bool        isnull;
    4947             :     char       *exprsString;
    4948             :     List       *rawExprs;
    4949             :     ListCell   *lc;
    4950             : 
    4951             :     /* Quick exit if there is nothing to do. */
    4952         344 :     if (relation->rd_indextuple == NULL ||
    4953         172 :         heap_attisnull(relation->rd_indextuple, Anum_pg_index_indexprs, NULL))
    4954         160 :         return NIL;
    4955             : 
    4956             :     /* Extract raw node tree(s) from index tuple. */
    4957          12 :     exprsDatum = heap_getattr(relation->rd_indextuple,
    4958             :                               Anum_pg_index_indexprs,
    4959             :                               GetPgIndexDescriptor(),
    4960             :                               &isnull);
    4961             :     Assert(!isnull);
    4962          12 :     exprsString = TextDatumGetCString(exprsDatum);
    4963          12 :     rawExprs = (List *) stringToNode(exprsString);
    4964          12 :     pfree(exprsString);
    4965             : 
    4966             :     /* Construct null Consts; the typlen and typbyval are arbitrary. */
    4967          12 :     result = NIL;
    4968          24 :     foreach(lc, rawExprs)
    4969             :     {
    4970          12 :         Node       *rawExpr = (Node *) lfirst(lc);
    4971             : 
    4972          12 :         result = lappend(result,
    4973          12 :                          makeConst(exprType(rawExpr),
    4974             :                                    exprTypmod(rawExpr),
    4975             :                                    exprCollation(rawExpr),
    4976             :                                    1,
    4977             :                                    (Datum) 0,
    4978             :                                    true,
    4979             :                                    true));
    4980             :     }
    4981             : 
    4982          12 :     return result;
    4983             : }
    4984             : 
    4985             : /*
    4986             :  * RelationGetIndexPredicate -- get the index predicate for an index
    4987             :  *
    4988             :  * We cache the result of transforming pg_index.indpred into an implicit-AND
    4989             :  * node tree (suitable for use in planning).
    4990             :  * If the rel is not an index or has no predicate, we return NIL.
    4991             :  * Otherwise, the returned tree is copied into the caller's memory context.
    4992             :  * (We don't want to return a pointer to the relcache copy, since it could
    4993             :  * disappear due to relcache invalidation.)
    4994             :  */
    4995             : List *
    4996     6283804 : RelationGetIndexPredicate(Relation relation)
    4997             : {
    4998             :     List       *result;
    4999             :     Datum       predDatum;
    5000             :     bool        isnull;
    5001             :     char       *predString;
    5002             :     MemoryContext oldcxt;
    5003             : 
    5004             :     /* Quick exit if we already computed the result. */
    5005     6283804 :     if (relation->rd_indpred)
    5006         930 :         return copyObject(relation->rd_indpred);
    5007             : 
    5008             :     /* Quick exit if there is nothing to do. */
    5009    12565748 :     if (relation->rd_indextuple == NULL ||
    5010     6282874 :         heap_attisnull(relation->rd_indextuple, Anum_pg_index_indpred, NULL))
    5011     6282350 :         return NIL;
    5012             : 
    5013             :     /*
    5014             :      * We build the tree we intend to return in the caller's context. After
    5015             :      * successfully completing the work, we copy it into the relcache entry.
    5016             :      * This avoids problems if we get some sort of error partway through.
    5017             :      */
    5018         524 :     predDatum = heap_getattr(relation->rd_indextuple,
    5019             :                              Anum_pg_index_indpred,
    5020             :                              GetPgIndexDescriptor(),
    5021             :                              &isnull);
    5022             :     Assert(!isnull);
    5023         524 :     predString = TextDatumGetCString(predDatum);
    5024         524 :     result = (List *) stringToNode(predString);
    5025         524 :     pfree(predString);
    5026             : 
    5027             :     /*
    5028             :      * Run the expression through const-simplification and canonicalization.
    5029             :      * This is not just an optimization, but is necessary, because the planner
    5030             :      * will be comparing it to similarly-processed qual clauses, and may fail
    5031             :      * to detect valid matches without this.  This must match the processing
    5032             :      * done to qual clauses in preprocess_expression()!  (We can skip the
    5033             :      * stuff involving subqueries, however, since we don't allow any in index
    5034             :      * predicates.)
    5035             :      */
    5036         524 :     result = (List *) eval_const_expressions(NULL, (Node *) result);
    5037             : 
    5038         524 :     result = (List *) canonicalize_qual((Expr *) result, false);
    5039             : 
    5040             :     /* Also convert to implicit-AND format */
    5041         524 :     result = make_ands_implicit((Expr *) result);
    5042             : 
    5043             :     /* May as well fix opfuncids too */
    5044         524 :     fix_opfuncids((Node *) result);
    5045             : 
    5046             :     /* Now save a copy of the completed tree in the relcache entry. */
    5047         524 :     oldcxt = MemoryContextSwitchTo(relation->rd_indexcxt);
    5048         524 :     relation->rd_indpred = copyObject(result);
    5049         524 :     MemoryContextSwitchTo(oldcxt);
    5050             : 
    5051         524 :     return result;
    5052             : }
    5053             : 
    5054             : /*
    5055             :  * RelationGetIndexAttrBitmap -- get a bitmap of index attribute numbers
    5056             :  *
    5057             :  * The result has a bit set for each attribute used anywhere in the index
    5058             :  * definitions of all the indexes on this relation.  (This includes not only
    5059             :  * simple index keys, but attributes used in expressions and partial-index
    5060             :  * predicates.)
    5061             :  *
    5062             :  * Depending on attrKind, a bitmap covering the attnums for all index columns,
    5063             :  * for all potential foreign key columns, or for all columns in the configured
    5064             :  * replica identity index is returned.
    5065             :  *
    5066             :  * Attribute numbers are offset by FirstLowInvalidHeapAttributeNumber so that
    5067             :  * we can include system attributes (e.g., OID) in the bitmap representation.
    5068             :  *
    5069             :  * Caller had better hold at least RowExclusiveLock on the target relation
    5070             :  * to ensure it is safe (deadlock-free) for us to take locks on the relation's
    5071             :  * indexes.  Note that since the introduction of CREATE INDEX CONCURRENTLY,
    5072             :  * that lock level doesn't guarantee a stable set of indexes, so we have to
    5073             :  * be prepared to retry here in case of a change in the set of indexes.
    5074             :  *
    5075             :  * The returned result is palloc'd in the caller's memory context and should
    5076             :  * be bms_free'd when not needed anymore.
    5077             :  */
    5078             : Bitmapset *
    5079     2052120 : RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
    5080             : {
    5081             :     Bitmapset  *uindexattrs;    /* columns in unique indexes */
    5082             :     Bitmapset  *pkindexattrs;   /* columns in the primary index */
    5083             :     Bitmapset  *idindexattrs;   /* columns in the replica identity */
    5084             :     Bitmapset  *hotblockingattrs;   /* columns with HOT blocking indexes */
    5085             :     List       *indexoidlist;
    5086             :     List       *newindexoidlist;
    5087             :     Oid         relpkindex;
    5088             :     Oid         relreplindex;
    5089             :     ListCell   *l;
    5090             :     MemoryContext oldcxt;
    5091             : 
    5092             :     /* Quick exit if we already computed the result. */
    5093     2052120 :     if (relation->rd_attrsvalid)
    5094             :     {
    5095     1879408 :         switch (attrKind)
    5096             :         {
    5097      591232 :             case INDEX_ATTR_BITMAP_KEY:
    5098      591232 :                 return bms_copy(relation->rd_keyattr);
    5099           8 :             case INDEX_ATTR_BITMAP_PRIMARY_KEY:
    5100           8 :                 return bms_copy(relation->rd_pkattr);
    5101      712662 :             case INDEX_ATTR_BITMAP_IDENTITY_KEY:
    5102      712662 :                 return bms_copy(relation->rd_idattr);
    5103      575506 :             case INDEX_ATTR_BITMAP_HOT_BLOCKING:
    5104      575506 :                 return bms_copy(relation->rd_hotblockingattr);
    5105           0 :             default:
    5106           0 :                 elog(ERROR, "unknown attrKind %u", attrKind);
    5107             :         }
    5108             :     }
    5109             : 
    5110             :     /* Fast path if definitely no indexes */
    5111      172712 :     if (!RelationGetForm(relation)->relhasindex)
    5112      161778 :         return NULL;
    5113             : 
    5114             :     /*
    5115             :      * Get cached list of index OIDs. If we have to start over, we do so here.
    5116             :      */
    5117       10934 : restart:
    5118       10934 :     indexoidlist = RelationGetIndexList(relation);
    5119             : 
    5120             :     /* Fall out if no indexes (but relhasindex was set) */
    5121       10934 :     if (indexoidlist == NIL)
    5122         300 :         return NULL;
    5123             : 
    5124             :     /*
    5125             :      * Copy the rd_pkindex and rd_replidindex values computed by
    5126             :      * RelationGetIndexList before proceeding.  This is needed because a
    5127             :      * relcache flush could occur inside index_open below, resetting the
    5128             :      * fields managed by RelationGetIndexList.  We need to do the work with
    5129             :      * stable values of these fields.
    5130             :      */
    5131       10634 :     relpkindex = relation->rd_pkindex;
    5132       10634 :     relreplindex = relation->rd_replidindex;
    5133             : 
    5134             :     /*
    5135             :      * For each index, add referenced attributes to appropriate bitmaps.
    5136             :      *
    5137             :      * Note: we consider all indexes returned by RelationGetIndexList, even if
    5138             :      * they are not indisready or indisvalid.  This is important because an
    5139             :      * index for which CREATE INDEX CONCURRENTLY has just started must be
    5140             :      * included in HOT-safety decisions (see README.HOT).  If a DROP INDEX
    5141             :      * CONCURRENTLY is far enough along that we should ignore the index, it
    5142             :      * won't be returned at all by RelationGetIndexList.
    5143             :      */
    5144       10634 :     uindexattrs = NULL;
    5145       10634 :     pkindexattrs = NULL;
    5146       10634 :     idindexattrs = NULL;
    5147       10634 :     hotblockingattrs = NULL;
    5148       30518 :     foreach(l, indexoidlist)
    5149             :     {
    5150       19884 :         Oid         indexOid = lfirst_oid(l);
    5151             :         Relation    indexDesc;
    5152             :         Datum       datum;
    5153             :         bool        isnull;
    5154             :         Node       *indexExpressions;
    5155             :         Node       *indexPredicate;
    5156             :         int         i;
    5157             :         bool        isKey;      /* candidate key */
    5158             :         bool        isPK;       /* primary key */
    5159             :         bool        isIDKey;    /* replica identity index */
    5160             : 
    5161       19884 :         indexDesc = index_open(indexOid, AccessShareLock);
    5162             : 
    5163             :         /*
    5164             :          * Extract index expressions and index predicate.  Note: Don't use
    5165             :          * RelationGetIndexExpressions()/RelationGetIndexPredicate(), because
    5166             :          * those might run constant expressions evaluation, which needs a
    5167             :          * snapshot, which we might not have here.  (Also, it's probably more
    5168             :          * sound to collect the bitmaps before any transformations that might
    5169             :          * eliminate columns, but the practical impact of this is limited.)
    5170             :          */
    5171             : 
    5172       19884 :         datum = heap_getattr(indexDesc->rd_indextuple, Anum_pg_index_indexprs,
    5173             :                              GetPgIndexDescriptor(), &isnull);
    5174       19884 :         if (!isnull)
    5175          20 :             indexExpressions = stringToNode(TextDatumGetCString(datum));
    5176             :         else
    5177       19864 :             indexExpressions = NULL;
    5178             : 
    5179       19884 :         datum = heap_getattr(indexDesc->rd_indextuple, Anum_pg_index_indpred,
    5180             :                              GetPgIndexDescriptor(), &isnull);
    5181       19884 :         if (!isnull)
    5182          94 :             indexPredicate = stringToNode(TextDatumGetCString(datum));
    5183             :         else
    5184       19790 :             indexPredicate = NULL;
    5185             : 
    5186             :         /* Can this index be referenced by a foreign key? */
    5187       16224 :         isKey = indexDesc->rd_index->indisunique &&
    5188       36108 :             indexExpressions == NULL &&
    5189             :             indexPredicate == NULL;
    5190             : 
    5191             :         /* Is this a primary key? */
    5192       19884 :         isPK = (indexOid == relpkindex);
    5193             : 
    5194             :         /* Is this index the configured (or default) replica identity? */
    5195       19884 :         isIDKey = (indexOid == relreplindex);
    5196             : 
    5197             :         /* Collect simple attribute references */
    5198       50402 :         for (i = 0; i < indexDesc->rd_index->indnatts; i++)
    5199             :         {
    5200       30518 :             int         attrnum = indexDesc->rd_index->indkey.values[i];
    5201             : 
    5202             :             /*
    5203             :              * Since we have covering indexes with non-key columns, we must
    5204             :              * handle them accurately here. non-key columns must be added into
    5205             :              * indexattrs, since they are in index, and HOT-update shouldn't
    5206             :              * miss them. Obviously, non-key columns couldn't be referenced by
    5207             :              * foreign key or identity key. Hence we do not include them into
    5208             :              * uindexattrs, pkindexattrs and idindexattrs bitmaps.
    5209             :              */
    5210       30518 :             if (attrnum != 0)
    5211             :             {
    5212       30498 :                 if (indexDesc->rd_indam->amhotblocking)
    5213       30190 :                     hotblockingattrs = bms_add_member(hotblockingattrs,
    5214             :                                                  attrnum - FirstLowInvalidHeapAttributeNumber);
    5215             : 
    5216       30498 :                 if (isKey && i < indexDesc->rd_index->indnkeyatts)
    5217       23758 :                     uindexattrs = bms_add_member(uindexattrs,
    5218             :                                                  attrnum - FirstLowInvalidHeapAttributeNumber);
    5219             : 
    5220       30498 :                 if (isPK && i < indexDesc->rd_index->indnkeyatts)
    5221        9138 :                     pkindexattrs = bms_add_member(pkindexattrs,
    5222             :                                                   attrnum - FirstLowInvalidHeapAttributeNumber);
    5223             : 
    5224       30498 :                 if (isIDKey && i < indexDesc->rd_index->indnkeyatts)
    5225        2640 :                     idindexattrs = bms_add_member(idindexattrs,
    5226             :                                                   attrnum - FirstLowInvalidHeapAttributeNumber);
    5227             :             }
    5228             :         }
    5229             : 
    5230             :         /* Collect all attributes used in expressions, too */
    5231       19884 :         if (indexDesc->rd_indam->amhotblocking)
    5232       19856 :             pull_varattnos(indexExpressions, 1, &hotblockingattrs);
    5233             : 
    5234             :         /*
    5235             :          * Collect all attributes in the index predicate, too. We have to ignore
    5236             :          * amhotblocking flag, because the row might become indexable, in which
    5237             :          * case we have to add it to the index.
    5238             :          */
    5239       19884 :         pull_varattnos(indexPredicate, 1, &hotblockingattrs);
    5240             : 
    5241       19884 :         index_close(indexDesc, AccessShareLock);
    5242             :     }
    5243             : 
    5244             :     /*
    5245             :      * During one of the index_opens in the above loop, we might have received
    5246             :      * a relcache flush event on this relcache entry, which might have been
    5247             :      * signaling a change in the rel's index list.  If so, we'd better start
    5248             :      * over to ensure we deliver up-to-date attribute bitmaps.
    5249             :      */
    5250       10634 :     newindexoidlist = RelationGetIndexList(relation);
    5251       10634 :     if (equal(indexoidlist, newindexoidlist) &&
    5252       10634 :         relpkindex == relation->rd_pkindex &&
    5253       10634 :         relreplindex == relation->rd_replidindex)
    5254             :     {
    5255             :         /* Still the same index set, so proceed */
    5256       10634 :         list_free(newindexoidlist);
    5257       10634 :         list_free(indexoidlist);
    5258             :     }
    5259             :     else
    5260             :     {
    5261             :         /* Gotta do it over ... might as well not leak memory */
    5262           0 :         list_free(newindexoidlist);
    5263           0 :         list_free(indexoidlist);
    5264           0 :         bms_free(uindexattrs);
    5265           0 :         bms_free(pkindexattrs);
    5266           0 :         bms_free(idindexattrs);
    5267           0 :         bms_free(hotblockingattrs);
    5268             : 
    5269           0 :         goto restart;
    5270             :     }
    5271             : 
    5272             :     /* Don't leak the old values of these bitmaps, if any */
    5273       10634 :     bms_free(relation->rd_keyattr);
    5274       10634 :     relation->rd_keyattr = NULL;
    5275       10634 :     bms_free(relation->rd_pkattr);
    5276       10634 :     relation->rd_pkattr = NULL;
    5277       10634 :     bms_free(relation->rd_idattr);
    5278       10634 :     relation->rd_idattr = NULL;
    5279       10634 :     bms_free(relation->rd_hotblockingattr);
    5280       10634 :     relation->rd_hotblockingattr = NULL;
    5281             : 
    5282             :     /*
    5283             :      * Now save copies of the bitmaps in the relcache entry.  We intentionally
    5284             :      * set rd_attrsvalid last, because that's what signals validity of the
    5285             :      * values; if we run out of memory before making that copy, we won't
    5286             :      * leave the relcache entry looking like the other ones are valid but
    5287             :      * empty.
    5288             :      */
    5289       10634 :     oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
    5290       10634 :     relation->rd_keyattr = bms_copy(uindexattrs);
    5291       10634 :     relation->rd_pkattr = bms_copy(pkindexattrs);
    5292       10634 :     relation->rd_idattr = bms_copy(idindexattrs);
    5293       10634 :     relation->rd_hotblockingattr = bms_copy(hotblockingattrs);
    5294       10634 :     relation->rd_attrsvalid = true;
    5295       10634 :     MemoryContextSwitchTo(oldcxt);
    5296             : 
    5297             :     /* We return our original working copy for caller to play with */
    5298       10634 :     switch (attrKind)
    5299             :     {
    5300         586 :         case INDEX_ATTR_BITMAP_KEY:
    5301         586 :             return uindexattrs;
    5302           0 :         case INDEX_ATTR_BITMAP_PRIMARY_KEY:
    5303           0 :             return pkindexattrs;
    5304         380 :         case INDEX_ATTR_BITMAP_IDENTITY_KEY:
    5305         380 :             return idindexattrs;
    5306        9668 :         case INDEX_ATTR_BITMAP_HOT_BLOCKING:
    5307        9668 :             return hotblockingattrs;
    5308           0 :         default:
    5309           0 :             elog(ERROR, "unknown attrKind %u", attrKind);
    5310             :             return NULL;
    5311             :     }
    5312             : }
    5313             : 
    5314             : /*
    5315             :  * RelationGetIdentityKeyBitmap -- get a bitmap of replica identity attribute
    5316             :  * numbers
    5317             :  *
    5318             :  * A bitmap of index attribute numbers for the configured replica identity
    5319             :  * index is returned.
    5320             :  *
    5321             :  * See also comments of RelationGetIndexAttrBitmap().
    5322             :  *
    5323             :  * This is a special purpose function used during logical replication. Here,
    5324             :  * unlike RelationGetIndexAttrBitmap(), we don't acquire a lock on the required
    5325             :  * index as we build the cache entry using a historic snapshot and all the
    5326             :  * later changes are absorbed while decoding WAL. Due to this reason, we don't
    5327             :  * need to retry here in case of a change in the set of indexes.
    5328             :  */
    5329             : Bitmapset *
    5330         250 : RelationGetIdentityKeyBitmap(Relation relation)
    5331             : {
    5332         250 :     Bitmapset  *idindexattrs = NULL;    /* columns in the replica identity */
    5333             :     Relation    indexDesc;
    5334             :     int         i;
    5335             :     Oid         replidindex;
    5336             :     MemoryContext oldcxt;
    5337             : 
    5338             :     /* Quick exit if we already computed the result */
    5339         250 :     if (relation->rd_idattr != NULL)
    5340          30 :         return bms_copy(relation->rd_idattr);
    5341             : 
    5342             :     /* Fast path if definitely no indexes */
    5343         220 :     if (!RelationGetForm(relation)->relhasindex)
    5344          42 :         return NULL;
    5345             : 
    5346             :     /* Historic snapshot must be set. */
    5347             :     Assert(HistoricSnapshotActive());
    5348             : 
    5349         178 :     replidindex = RelationGetReplicaIndex(relation);
    5350             : 
    5351             :     /* Fall out if there is no replica identity index */
    5352         178 :     if (!OidIsValid(replidindex))
    5353           2 :         return NULL;
    5354             : 
    5355             :     /* Look up the description for the replica identity index */
    5356         176 :     indexDesc = RelationIdGetRelation(replidindex);
    5357             : 
    5358         176 :     if (!RelationIsValid(indexDesc))
    5359           0 :         elog(ERROR, "could not open relation with OID %u",
    5360             :              relation->rd_replidindex);
    5361             : 
    5362             :     /* Add referenced attributes to idindexattrs */
    5363         354 :     for (i = 0; i < indexDesc->rd_index->indnatts; i++)
    5364             :     {
    5365         178 :         int         attrnum = indexDesc->rd_index->indkey.values[i];
    5366             : 
    5367             :         /*
    5368             :          * We don't include non-key columns into idindexattrs bitmaps. See
    5369             :          * RelationGetIndexAttrBitmap.
    5370             :          */
    5371         178 :         if (attrnum != 0)
    5372             :         {
    5373         178 :             if (i < indexDesc->rd_index->indnkeyatts)
    5374         176 :                 idindexattrs = bms_add_member(idindexattrs,
    5375             :                                               attrnum - FirstLowInvalidHeapAttributeNumber);
    5376             :         }
    5377             :     }
    5378             : 
    5379         176 :     RelationClose(indexDesc);
    5380             : 
    5381             :     /* Don't leak the old values of these bitmaps, if any */
    5382         176 :     bms_free(relation->rd_idattr);
    5383         176 :     relation->rd_idattr = NULL;
    5384             : 
    5385             :     /* Now save copy of the bitmap in the relcache entry */
    5386         176 :     oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
    5387         176 :     relation->rd_idattr = bms_copy(idindexattrs);
    5388         176 :     MemoryContextSwitchTo(oldcxt);
    5389             : 
    5390             :     /* We return our original working copy for caller to play with */
    5391         176 :     return idindexattrs;
    5392             : }
    5393             : 
    5394             : /*
    5395             :  * RelationGetExclusionInfo -- get info about index's exclusion constraint
    5396             :  *
    5397             :  * This should be called only for an index that is known to have an
    5398             :  * associated exclusion constraint.  It returns arrays (palloc'd in caller's
    5399             :  * context) of the exclusion operator OIDs, their underlying functions'
    5400             :  * OIDs, and their strategy numbers in the index's opclasses.  We cache
    5401             :  * all this information since it requires a fair amount of work to get.
    5402             :  */
    5403             : void
    5404         216 : RelationGetExclusionInfo(Relation indexRelation,
    5405             :                          Oid **operators,
    5406             :                          Oid **procs,
    5407             :                          uint16 **strategies)
    5408             : {
    5409             :     int         indnkeyatts;
    5410             :     Oid        *ops;
    5411             :     Oid        *funcs;
    5412             :     uint16     *strats;
    5413             :     Relation    conrel;
    5414             :     SysScanDesc conscan;
    5415             :     ScanKeyData skey[1];
    5416             :     HeapTuple   htup;
    5417             :     bool        found;
    5418             :     MemoryContext oldcxt;
    5419             :     int         i;
    5420             : 
    5421         216 :     indnkeyatts = IndexRelationGetNumberOfKeyAttributes(indexRelation);
    5422             : 
    5423             :     /* Allocate result space in caller context */
    5424         216 :     *operators = ops = (Oid *) palloc(sizeof(Oid) * indnkeyatts);
    5425         216 :     *procs = funcs = (Oid *) palloc(sizeof(Oid) * indnkeyatts);
    5426         216 :     *strategies = strats = (uint16 *) palloc(sizeof(uint16) * indnkeyatts);
    5427             : 
    5428             :     /* Quick exit if we have the data cached already */
    5429         216 :     if (indexRelation->rd_exclstrats != NULL)
    5430             :     {
    5431         146 :         memcpy(ops, indexRelation->rd_exclops, sizeof(Oid) * indnkeyatts);
    5432         146 :         memcpy(funcs, indexRelation->rd_exclprocs, sizeof(Oid) * indnkeyatts);
    5433         146 :         memcpy(strats, indexRelation->rd_exclstrats, sizeof(uint16) * indnkeyatts);
    5434         146 :         return;
    5435             :     }
    5436             : 
    5437             :     /*
    5438             :      * Search pg_constraint for the constraint associated with the index. To
    5439             :      * make this not too painfully slow, we use the index on conrelid; that
    5440             :      * will hold the parent relation's OID not the index's own OID.
    5441             :      *
    5442             :      * Note: if we wanted to rely on the constraint name matching the index's
    5443             :      * name, we could just do a direct lookup using pg_constraint's unique
    5444             :      * index.  For the moment it doesn't seem worth requiring that.
    5445             :      */
    5446          70 :     ScanKeyInit(&skey[0],
    5447             :                 Anum_pg_constraint_conrelid,
    5448             :                 BTEqualStrategyNumber, F_OIDEQ,
    5449          70 :                 ObjectIdGetDatum(indexRelation->rd_index->indrelid));
    5450             : 
    5451          70 :     conrel = table_open(ConstraintRelationId, AccessShareLock);
    5452          70 :     conscan = systable_beginscan(conrel, ConstraintRelidTypidNameIndexId, true,
    5453             :                                  NULL, 1, skey);
    5454          70 :     found = false;
    5455             : 
    5456         332 :     while (HeapTupleIsValid(htup = systable_getnext(conscan)))
    5457             :     {
    5458         262 :         Form_pg_constraint conform = (Form_pg_constraint) GETSTRUCT(htup);
    5459             :         Datum       val;
    5460             :         bool        isnull;
    5461             :         ArrayType  *arr;
    5462             :         int         nelem;
    5463             : 
    5464             :         /* We want the exclusion constraint owning the index */
    5465         262 :         if (conform->contype != CONSTRAINT_EXCLUSION ||
    5466         150 :             conform->conindid != RelationGetRelid(indexRelation))
    5467         192 :             continue;
    5468             : 
    5469             :         /* There should be only one */
    5470          70 :         if (found)
    5471           0 :             elog(ERROR, "unexpected exclusion constraint record found for rel %s",
    5472             :                  RelationGetRelationName(indexRelation));
    5473          70 :         found = true;
    5474             : 
    5475             :         /* Extract the operator OIDS from conexclop */
    5476          70 :         val = fastgetattr(htup,
    5477             :                           Anum_pg_constraint_conexclop,
    5478             :                           conrel->rd_att, &isnull);
    5479          70 :         if (isnull)
    5480           0 :             elog(ERROR, "null conexclop for rel %s",
    5481             :                  RelationGetRelationName(indexRelation));
    5482             : 
    5483          70 :         arr = DatumGetArrayTypeP(val);  /* ensure not toasted */
    5484          70 :         nelem = ARR_DIMS(arr)[0];
    5485          70 :         if (ARR_NDIM(arr) != 1 ||
    5486          70 :             nelem != indnkeyatts ||
    5487          70 :             ARR_HASNULL(arr) ||
    5488          70 :             ARR_ELEMTYPE(arr) != OIDOID)
    5489           0 :             elog(ERROR, "conexclop is not a 1-D Oid array");
    5490             : 
    5491          70 :         memcpy(ops, ARR_DATA_PTR(arr), sizeof(Oid) * indnkeyatts);
    5492             :     }
    5493             : 
    5494          70 :     systable_endscan(conscan);
    5495          70 :     table_close(conrel, AccessShareLock);
    5496             : 
    5497          70 :     if (!found)
    5498           0 :         elog(ERROR, "exclusion constraint record missing for rel %s",
    5499             :              RelationGetRelationName(indexRelation));
    5500             : 
    5501             :     /* We need the func OIDs and strategy numbers too */
    5502         154 :     for (i = 0; i < indnkeyatts; i++)
    5503             :     {
    5504          84 :         funcs[i] = get_opcode(ops[i]);
    5505         168 :         strats[i] = get_op_opfamily_strategy(ops[i],
    5506          84 :                                              indexRelation->rd_opfamily[i]);
    5507             :         /* shouldn't fail, since it was checked at index creation */
    5508          84 :         if (strats[i] == InvalidStrategy)
    5509           0 :             elog(ERROR, "could not find strategy for operator %u in family %u",
    5510             :                  ops[i], indexRelation->rd_opfamily[i]);
    5511             :     }
    5512             : 
    5513             :     /* Save a copy of the results in the relcache entry. */
    5514          70 :     oldcxt = MemoryContextSwitchTo(indexRelation->rd_indexcxt);
    5515          70 :     indexRelation->rd_exclops = (Oid *) palloc(sizeof(Oid) * indnkeyatts);
    5516          70 :     indexRelation->rd_exclprocs = (Oid *) palloc(sizeof(Oid) * indnkeyatts);
    5517          70 :     indexRelation->rd_exclstrats = (uint16 *) palloc(sizeof(uint16) * indnkeyatts);
    5518          70 :     memcpy(indexRelation->rd_exclops, ops, sizeof(Oid) * indnkeyatts);
    5519          70 :     memcpy(indexRelation->rd_exclprocs, funcs, sizeof(Oid) * indnkeyatts);
    5520          70 :     memcpy(indexRelation->rd_exclstrats, strats, sizeof(uint16) * indnkeyatts);
    5521          70 :     MemoryContextSwitchTo(oldcxt);
    5522             : }
    5523             : 
    5524             : /*
    5525             :  * Get publication actions for the given relation.
    5526             :  */
    5527             : struct PublicationActions *
    5528       16584 : GetRelationPublicationActions(Relation relation)
    5529             : {
    5530             :     List       *puboids;
    5531             :     ListCell   *lc;
    5532             :     MemoryContext oldcxt;
    5533             :     Oid         schemaid;
    5534       16584 :     PublicationActions *pubactions = palloc0(sizeof(PublicationActions));
    5535             : 
    5536             :     /*
    5537             :      * If not publishable, it publishes no actions.  (pgoutput_change() will
    5538             :      * ignore it.)
    5539             :      */
    5540       16584 :     if (!is_publishable_relation(relation))
    5541        5674 :         return pubactions;
    5542             : 
    5543       10910 :     if (relation->rd_pubactions)
    5544        8200 :         return memcpy(pubactions, relation->rd_pubactions,
    5545             :                       sizeof(PublicationActions));
    5546             : 
    5547             :     /* Fetch the publication membership info. */
    5548        2710 :     puboids = GetRelationPublications(RelationGetRelid(relation));
    5549        2710 :     schemaid = RelationGetNamespace(relation);
    5550        2710 :     puboids = list_concat_unique_oid(puboids, GetSchemaPublications(schemaid));
    5551             : 
    5552        2710 :     if (relation->rd_rel->relispartition)
    5553             :     {
    5554             :         /* Add publications that the ancestors are in too. */
    5555         768 :         List       *ancestors = get_partition_ancestors(RelationGetRelid(relation));
    5556             :         ListCell   *lc;
    5557             : 
    5558        1848 :         foreach(lc, ancestors)
    5559             :         {
    5560        1080 :             Oid         ancestor = lfirst_oid(lc);
    5561             : 
    5562        1080 :             puboids = list_concat_unique_oid(puboids,
    5563        1080 :                                              GetRelationPublications(ancestor));
    5564        1080 :             schemaid = get_rel_namespace(ancestor);
    5565        1080 :             puboids = list_concat_unique_oid(puboids,
    5566        1080 :                                              GetSchemaPublications(schemaid));
    5567             :         }
    5568             :     }
    5569        2710 :     puboids = list_concat_unique_oid(puboids, GetAllTablesPublications());
    5570             : 
    5571        2714 :     foreach(lc, puboids)
    5572             :     {
    5573          36 :         Oid         pubid = lfirst_oid(lc);
    5574             :         HeapTuple   tup;
    5575             :         Form_pg_publication pubform;
    5576             : 
    5577          36 :         tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
    5578             : 
    5579          36 :         if (!HeapTupleIsValid(tup))
    5580           0 :             elog(ERROR, "cache lookup failed for publication %u", pubid);
    5581             : 
    5582          36 :         pubform = (Form_pg_publication) GETSTRUCT(tup);
    5583             : 
    5584          36 :         pubactions->pubinsert |= pubform->pubinsert;
    5585          36 :         pubactions->pubupdate |= pubform->pubupdate;
    5586          36 :         pubactions->pubdelete |= pubform->pubdelete;
    5587          36 :         pubactions->pubtruncate |= pubform->pubtruncate;
    5588             : 
    5589          36 :         ReleaseSysCache(tup);
    5590             : 
    5591             :         /*
    5592             :          * If we know everything is replicated, there is no point to check for
    5593             :          * other publications.
    5594             :          */
    5595          36 :         if (pubactions->pubinsert && pubactions->pubupdate &&
    5596          32 :             pubactions->pubdelete && pubactions->pubtruncate)
    5597          32 :             break;
    5598             :     }
    5599             : 
    5600        2710 :     if (relation->rd_pubactions)
    5601             :     {
    5602           0 :         pfree(relation->rd_pubactions);
    5603           0 :         relation->rd_pubactions = NULL;
    5604             :     }
    5605             : 
    5606             :     /* Now save copy of the actions in the relcache entry. */
    5607        2710 :     oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
    5608        2710 :     relation->rd_pubactions = palloc(sizeof(PublicationActions));
    5609        2710 :     memcpy(relation->rd_pubactions, pubactions, sizeof(PublicationActions));
    5610        2710 :     MemoryContextSwitchTo(oldcxt);
    5611             : 
    5612        2710 :     return pubactions;
    5613             : }
    5614             : 
    5615             : /*
    5616             :  * RelationGetIndexRawAttOptions -- get AM/opclass-specific options for the index
    5617             :  */
    5618             : Datum *
    5619     5832916 : RelationGetIndexRawAttOptions(Relation indexrel)
    5620             : {
    5621     5832916 :     Oid         indexrelid = RelationGetRelid(indexrel);
    5622     5832916 :     int16       natts = RelationGetNumberOfAttributes(indexrel);
    5623     5832916 :     Datum      *options = NULL;
    5624             :     int16       attnum;
    5625             : 
    5626    18399194 :     for (attnum = 1; attnum <= natts; attnum++)
    5627             :     {
    5628    12566278 :         if (indexrel->rd_indam->amoptsprocnum == 0)
    5629           0 :             continue;
    5630             : 
    5631    12566278 :         if (!OidIsValid(index_getprocid(indexrel, attnum,
    5632             :                                         indexrel->rd_indam->amoptsprocnum)))
    5633    12565462 :             continue;
    5634             : 
    5635         816 :         if (!options)
    5636          60 :             options = palloc0(sizeof(Datum) * natts);
    5637             : 
    5638         816 :         options[attnum - 1] = get_attoptions(indexrelid, attnum);
    5639             :     }
    5640             : 
    5641     5832916 :     return options;
    5642             : }
    5643             : 
    5644             : static bytea **
    5645      817108 : CopyIndexAttOptions(bytea **srcopts, int natts)
    5646             : {
    5647      817108 :     bytea     **opts = palloc(sizeof(*opts) * natts);
    5648             : 
    5649     2290550 :     for (int i = 0; i < natts; i++)
    5650             :     {
    5651     1473442 :         bytea      *opt = srcopts[i];
    5652             : 
    5653     1534186 :         opts[i] = !opt ? NULL : (bytea *)
    5654       60744 :             DatumGetPointer(datumCopy(PointerGetDatum(opt), false, -1));
    5655             :     }
    5656             : 
    5657      817108 :     return opts;
    5658             : }
    5659             : 
    5660             : /*
    5661             :  * RelationGetIndexAttOptions
    5662             :  *      get AM/opclass-specific options for an index parsed into a binary form
    5663             :  */
    5664             : bytea     **
    5665     1577982 : RelationGetIndexAttOptions(Relation relation, bool copy)
    5666             : {
    5667             :     MemoryContext oldcxt;
    5668     1577982 :     bytea     **opts = relation->rd_opcoptions;
    5669     1577982 :     Oid         relid = RelationGetRelid(relation);
    5670     1577982 :     int         natts = RelationGetNumberOfAttributes(relation);    /* XXX
    5671             :                                                                      * IndexRelationGetNumberOfKeyAttributes */
    5672             :     int         i;
    5673             : 
    5674             :     /* Try to copy cached options. */
    5675     1577982 :     if (opts)
    5676     1136986 :         return copy ? CopyIndexAttOptions(opts, natts) : opts;
    5677             : 
    5678             :     /* Get and parse opclass options. */
    5679      440996 :     opts = palloc0(sizeof(*opts) * natts);
    5680             : 
    5681     1182950 :     for (i = 0; i < natts; i++)
    5682             :     {
    5683      741958 :         if (criticalRelcachesBuilt && relid != AttributeRelidNumIndexId)
    5684             :         {
    5685      590490 :             Datum       attoptions = get_attoptions(relid, i + 1);
    5686             : 
    5687      590490 :             opts[i] = index_opclass_options(relation, i + 1, attoptions, false);
    5688             : 
    5689      590486 :             if (attoptions != (Datum) 0)
    5690         200 :                 pfree(DatumGetPointer(attoptions));
    5691             :         }
    5692             :     }
    5693             : 
    5694             :     /* Copy parsed options to the cache. */
    5695      440992 :     oldcxt = MemoryContextSwitchTo(relation->rd_indexcxt);
    5696      440992 :     relation->rd_opcoptions = CopyIndexAttOptions(opts, natts);
    5697      440992 :     MemoryContextSwitchTo(oldcxt);
    5698             : 
    5699      440992 :     if (copy)
    5700           0 :         return opts;
    5701             : 
    5702     1182946 :     for (i = 0; i < natts; i++)
    5703             :     {
    5704      741954 :         if (opts[i])
    5705        1054 :             pfree(opts[i]);
    5706             :     }
    5707             : 
    5708      440992 :     pfree(opts);
    5709             : 
    5710      440992 :     return relation->rd_opcoptions;
    5711             : }
    5712             : 
    5713             : /*
    5714             :  * Routines to support ereport() reports of relation-related errors
    5715             :  *
    5716             :  * These could have been put into elog.c, but it seems like a module layering
    5717             :  * violation to have elog.c calling relcache or syscache stuff --- and we
    5718             :  * definitely don't want elog.h including rel.h.  So we put them here.
    5719             :  */
    5720             : 
    5721             : /*
    5722             :  * errtable --- stores schema_name and table_name of a table
    5723             :  * within the current errordata.
    5724             :  */
    5725             : int
    5726        1938 : errtable(Relation rel)
    5727             : {
    5728        1938 :     err_generic_string(PG_DIAG_SCHEMA_NAME,
    5729        1938 :                        get_namespace_name(RelationGetNamespace(rel)));
    5730        1938 :     err_generic_string(PG_DIAG_TABLE_NAME, RelationGetRelationName(rel));
    5731             : 
    5732        1938 :     return 0;                   /* return value does not matter */
    5733             : }
    5734             : 
    5735             : /*
    5736             :  * errtablecol --- stores schema_name, table_name and column_name
    5737             :  * of a table column within the current errordata.
    5738             :  *
    5739             :  * The column is specified by attribute number --- for most callers, this is
    5740             :  * easier and less error-prone than getting the column name for themselves.
    5741             :  */
    5742             : int
    5743         242 : errtablecol(Relation rel, int attnum)
    5744             : {
    5745         242 :     TupleDesc   reldesc = RelationGetDescr(rel);
    5746             :     const char *colname;
    5747             : 
    5748             :     /* Use reldesc if it's a user attribute, else consult the catalogs */
    5749         242 :     if (attnum > 0 && attnum <= reldesc->natts)
    5750         242 :         colname = NameStr(TupleDescAttr(reldesc, attnum - 1)->attname);
    5751             :     else
    5752           0 :         colname = get_attname(RelationGetRelid(rel), attnum, false);
    5753             : 
    5754         242 :     return errtablecolname(rel, colname);
    5755             : }
    5756             : 
    5757             : /*
    5758             :  * errtablecolname --- stores schema_name, table_name and column_name
    5759             :  * of a table column within the current errordata, where the column name is
    5760             :  * given directly rather than extracted from the relation's catalog data.
    5761             :  *
    5762             :  * Don't use this directly unless errtablecol() is inconvenient for some
    5763             :  * reason.  This might possibly be needed during intermediate states in ALTER
    5764             :  * TABLE, for instance.
    5765             :  */
    5766             : int
    5767         242 : errtablecolname(Relation rel, const char *colname)
    5768             : {
    5769         242 :     errtable(rel);
    5770         242 :     err_generic_string(PG_DIAG_COLUMN_NAME, colname);
    5771             : 
    5772         242 :     return 0;                   /* return value does not matter */
    5773             : }
    5774             : 
    5775             : /*
    5776             :  * errtableconstraint --- stores schema_name, table_name and constraint_name
    5777             :  * of a table-related constraint within the current errordata.
    5778             :  */
    5779             : int
    5780        1366 : errtableconstraint(Relation rel, const char *conname)
    5781             : {
    5782        1366 :     errtable(rel);
    5783        1366 :     err_generic_string(PG_DIAG_CONSTRAINT_NAME, conname);
    5784             : 
    5785        1366 :     return 0;                   /* return value does not matter */
    5786             : }
    5787             : 
    5788             : 
    5789             : /*
    5790             :  *  load_relcache_init_file, write_relcache_init_file
    5791             :  *
    5792             :  *      In late 1992, we started regularly having databases with more than
    5793             :  *      a thousand classes in them.  With this number of classes, it became
    5794             :  *      critical to do indexed lookups on the system catalogs.
    5795             :  *
    5796             :  *      Bootstrapping these lookups is very hard.  We want to be able to
    5797             :  *      use an index on pg_attribute, for example, but in order to do so,
    5798             :  *      we must have read pg_attribute for the attributes in the index,
    5799             :  *      which implies that we need to use the index.
    5800             :  *
    5801             :  *      In order to get around the problem, we do the following:
    5802             :  *
    5803             :  *         +  When the database system is initialized (at initdb time), we
    5804             :  *            don't use indexes.  We do sequential scans.
    5805             :  *
    5806             :  *         +  When the backend is started up in normal mode, we load an image
    5807             :  *            of the appropriate relation descriptors, in internal format,
    5808             :  *            from an initialization file in the data/base/... directory.
    5809             :  *
    5810             :  *         +  If the initialization file isn't there, then we create the
    5811             :  *            relation descriptors using sequential scans and write 'em to
    5812             :  *            the initialization file for use by subsequent backends.
    5813             :  *
    5814             :  *      As of Postgres 9.0, there is one local initialization file in each
    5815             :  *      database, plus one shared initialization file for shared catalogs.
    5816             :  *
    5817             :  *      We could dispense with the initialization files and just build the
    5818             :  *      critical reldescs the hard way on every backend startup, but that
    5819             :  *      slows down backend startup noticeably.
    5820             :  *
    5821             :  *      We can in fact go further, and save more relcache entries than
    5822             :  *      just the ones that are absolutely critical; this allows us to speed
    5823             :  *      up backend startup by not having to build such entries the hard way.
    5824             :  *      Presently, all the catalog and index entries that are referred to
    5825             :  *      by catcaches are stored in the initialization files.
    5826             :  *
    5827             :  *      The same mechanism that detects when catcache and relcache entries
    5828             :  *      need to be invalidated (due to catalog updates) also arranges to
    5829             :  *      unlink the initialization files when the contents may be out of date.
    5830             :  *      The files will then be rebuilt during the next backend startup.
    5831             :  */
    5832             : 
    5833             : /*
    5834             :  * load_relcache_init_file -- attempt to load cache from the shared
    5835             :  * or local cache init file
    5836             :  *
    5837             :  * If successful, return true and set criticalRelcachesBuilt or
    5838             :  * criticalSharedRelcachesBuilt to true.
    5839             :  * If not successful, return false.
    5840             :  *
    5841             :  * NOTE: we assume we are already switched into CacheMemoryContext.
    5842             :  */
    5843             : static bool
    5844       29346 : load_relcache_init_file(bool shared)
    5845             : {
    5846             :     FILE       *fp;
    5847             :     char        initfilename[MAXPGPATH];
    5848             :     Relation   *rels;
    5849             :     int         relno,
    5850             :                 num_rels,
    5851             :                 max_rels,
    5852             :                 nailed_rels,
    5853             :                 nailed_indexes,
    5854             :                 magic;
    5855             :     int         i;
    5856             : 
    5857       29346 :     if (shared)
    5858       15446 :         snprintf(initfilename, sizeof(initfilename), "global/%s",
    5859             :                  RELCACHE_INIT_FILENAME);
    5860             :     else
    5861       13900 :         snprintf(initfilename, sizeof(initfilename), "%s/%s",
    5862             :                  DatabasePath, RELCACHE_INIT_FILENAME);
    5863             : 
    5864       29346 :     fp = AllocateFile(initfilename, PG_BINARY_R);
    5865       29346 :     if (fp == NULL)
    5866        4476 :         return false;
    5867             : 
    5868             :     /*
    5869             :      * Read the index relcache entries from the file.  Note we will not enter
    5870             :      * any of them into the cache if the read fails partway through; this
    5871             :      * helps to guard against broken init files.
    5872             :      */
    5873       24870 :     max_rels = 100;
    5874       24870 :     rels = (Relation *) palloc(max_rels * sizeof(Relation));
    5875       24870 :     num_rels = 0;
    5876       24870 :     nailed_rels = nailed_indexes = 0;
    5877             : 
    5878             :     /* check for correct magic number (compatible version) */
    5879       24870 :     if (fread(&magic, 1, sizeof(magic), fp) != sizeof(magic))
    5880           0 :         goto read_failed;
    5881       24870 :     if (magic != RELCACHE_INIT_FILEMAGIC)
    5882           0 :         goto read_failed;
    5883             : 
    5884       24870 :     for (relno = 0;; relno++)
    5885     1595226 :     {
    5886             :         Size        len;
    5887             :         size_t      nread;
    5888             :         Relation    rel;
    5889             :         Form_pg_class relform;
    5890             :         bool        has_not_null;
    5891             : 
    5892             :         /* first read the relation descriptor length */
    5893     1620096 :         nread = fread(&len, 1, sizeof(len), fp);
    5894     1620096 :         if (nread != sizeof(len))
    5895             :         {
    5896       24870 :             if (nread == 0)
    5897       24870 :                 break;          /* end of file */
    5898           0 :             goto read_failed;
    5899             :         }
    5900             : 
    5901             :         /* safety check for incompatible relcache layout */
    5902     1595226 :         if (len != sizeof(RelationData))
    5903           0 :             goto read_failed;
    5904             : 
    5905             :         /* allocate another relcache header */
    5906     1595226 :         if (num_rels >= max_rels)
    5907             :         {
    5908       12072 :             max_rels *= 2;
    5909       12072 :             rels = (Relation *) repalloc(rels, max_rels * sizeof(Relation));
    5910             :         }
    5911             : 
    5912     1595226 :         rel = rels[num_rels++] = (Relation) palloc(len);
    5913             : 
    5914             :         /* then, read the Relation structure */
    5915     1595226 :         if (fread(rel, 1, len, fp) != len)
    5916           0 :             goto read_failed;
    5917             : 
    5918             :         /* next read the relation tuple form */
    5919     1595226 :         if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
    5920           0 :             goto read_failed;
    5921             : 
    5922     1595226 :         relform = (Form_pg_class) palloc(len);
    5923     1595226 :         if (fread(relform, 1, len, fp) != len)
    5924           0 :             goto read_failed;
    5925             : 
    5926     1595226 :         rel->rd_rel = relform;
    5927             : 
    5928             :         /* initialize attribute tuple forms */
    5929     1595226 :         rel->rd_att = CreateTemplateTupleDesc(relform->relnatts);
    5930     1595226 :         rel->rd_att->tdrefcount = 1;  /* mark as refcounted */
    5931             : 
    5932     1595226 :         rel->rd_att->tdtypeid = relform->reltype ? relform->reltype : RECORDOID;
    5933     1595226 :         rel->rd_att->tdtypmod = -1; /* just to be sure */
    5934             : 
    5935             :         /* next read all the attribute tuple form data entries */
    5936     1595226 :         has_not_null = false;
    5937     9215190 :         for (i = 0; i < relform->relnatts; i++)
    5938             :         {
    5939     7619964 :             Form_pg_attribute attr = TupleDescAttr(rel->rd_att, i);
    5940             : 
    5941     7619964 :             if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
    5942           0 :                 goto read_failed;
    5943     7619964 :             if (len != ATTRIBUTE_FIXED_PART_SIZE)
    5944           0 :                 goto read_failed;
    5945     7619964 :             if (fread(attr, 1, len, fp) != len)
    5946           0 :                 goto read_failed;
    5947             : 
    5948     7619964 :             has_not_null |= attr->attnotnull;
    5949             :         }
    5950             : 
    5951             :         /* next read the access method specific field */
    5952     1595226 :         if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
    5953           0 :             goto read_failed;
    5954     1595226 :         if (len > 0)
    5955             :         {
    5956           0 :             rel->rd_options = palloc(len);
    5957           0 :             if (fread(rel->rd_options, 1, len, fp) != len)
    5958           0 :                 goto read_failed;
    5959           0 :             if (len != VARSIZE(rel->rd_options))
    5960           0 :                 goto read_failed;   /* sanity check */
    5961             :         }
    5962             :         else
    5963             :         {
    5964     1595226 :             rel->rd_options = NULL;
    5965             :         }
    5966             : 
    5967             :         /* mark not-null status */
    5968     1595226 :         if (has_not_null)
    5969             :         {
    5970      596610 :             TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
    5971             : 
    5972      596610 :             constr->has_not_null = true;
    5973      596610 :             rel->rd_att->constr = constr;
    5974             :         }
    5975             : 
    5976             :         /*
    5977             :          * If it's an index, there's more to do.  Note we explicitly ignore
    5978             :          * partitioned indexes here.
    5979             :          */
    5980     1595226 :         if (rel->rd_rel->relkind == RELKIND_INDEX)
    5981             :         {
    5982             :             MemoryContext indexcxt;
    5983             :             Oid        *opfamily;
    5984             :             Oid        *opcintype;
    5985             :             RegProcedure *support;
    5986             :             int         nsupport;
    5987             :             int16      *indoption;
    5988             :             Oid        *indcollation;
    5989             : 
    5990             :             /* Count nailed indexes to ensure we have 'em all */
    5991      998616 :             if (rel->rd_isnailed)
    5992      161292 :                 nailed_indexes++;
    5993             : 
    5994             :             /* next, read the pg_index tuple */
    5995      998616 :             if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
    5996           0 :                 goto read_failed;
    5997             : 
    5998      998616 :             rel->rd_indextuple = (HeapTuple) palloc(len);
    5999      998616 :             if (fread(rel->rd_indextuple, 1, len, fp) != len)
    6000           0 :                 goto read_failed;
    6001             : 
    6002             :             /* Fix up internal pointers in the tuple -- see heap_copytuple */
    6003      998616 :             rel->rd_indextuple->t_data = (HeapTupleHeader) ((char *) rel->rd_indextuple + HEAPTUPLESIZE);
    6004      998616 :             rel->rd_index = (Form_pg_index) GETSTRUCT(rel->rd_indextuple);
    6005             : 
    6006             :             /*
    6007             :              * prepare index info context --- parameters should match
    6008             :              * RelationInitIndexAccessInfo
    6009             :              */
    6010      998616 :             indexcxt = AllocSetContextCreate(CacheMemoryContext,
    6011             :                                              "index info",
    6012             :                                              ALLOCSET_SMALL_SIZES);
    6013      998616 :             rel->rd_indexcxt = indexcxt;
    6014      998616 :             MemoryContextCopyAndSetIdentifier(indexcxt,
    6015             :                                               RelationGetRelationName(rel));
    6016             : 
    6017             :             /*
    6018             :              * Now we can fetch the index AM's API struct.  (We can't store
    6019             :              * that in the init file, since it contains function pointers that
    6020             :              * might vary across server executions.  Fortunately, it should be
    6021             :              * safe to call the amhandler even while bootstrapping indexes.)
    6022             :              */
    6023      998616 :             InitIndexAmRoutine(rel);
    6024             : 
    6025             :             /* next, read the vector of opfamily OIDs */
    6026      998616 :             if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
    6027           0 :                 goto read_failed;
    6028             : 
    6029      998616 :             opfamily = (Oid *) MemoryContextAlloc(indexcxt, len);
    6030      998616 :             if (fread(opfamily, 1, len, fp) != len)
    6031           0 :                 goto read_failed;
    6032             : 
    6033      998616 :             rel->rd_opfamily = opfamily;
    6034             : 
    6035             :             /* next, read the vector of opcintype OIDs */
    6036      998616 :             if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
    6037           0 :                 goto read_failed;
    6038             : 
    6039      998616 :             opcintype = (Oid *) MemoryContextAlloc(indexcxt, len);
    6040      998616 :             if (fread(opcintype, 1, len, fp) != len)
    6041           0 :                 goto read_failed;
    6042             : 
    6043      998616 :             rel->rd_opcintype = opcintype;
    6044             : 
    6045             :             /* next, read the vector of support procedure OIDs */
    6046      998616 :             if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
    6047           0 :                 goto read_failed;
    6048      998616 :             support = (RegProcedure *) MemoryContextAlloc(indexcxt, len);
    6049      998616 :             if (fread(support, 1, len, fp) != len)
    6050           0 :                 goto read_failed;
    6051             : 
    6052      998616 :             rel->rd_support = support;
    6053             : 
    6054             :             /* next, read the vector of collation OIDs */
    6055      998616 :             if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
    6056           0 :                 goto read_failed;
    6057             : 
    6058      998616 :             indcollation = (Oid *) MemoryContextAlloc(indexcxt, len);
    6059      998616 :             if (fread(indcollation, 1, len, fp) != len)
    6060           0 :                 goto read_failed;
    6061             : 
    6062      998616 :             rel->rd_indcollation = indcollation;
    6063             : 
    6064             :             /* finally, read the vector of indoption values */
    6065      998616 :             if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
    6066           0 :                 goto read_failed;
    6067             : 
    6068      998616 :             indoption = (int16 *) MemoryContextAlloc(indexcxt, len);
    6069      998616 :             if (fread(indoption, 1, len, fp) != len)
    6070           0 :                 goto read_failed;
    6071             : 
    6072      998616 :             rel->rd_indoption = indoption;
    6073             : 
    6074             :             /* finally, read the vector of opcoptions values */
    6075      998616 :             rel->rd_opcoptions = (bytea **)
    6076      998616 :                 MemoryContextAllocZero(indexcxt, sizeof(*rel->rd_opcoptions) * relform->relnatts);
    6077             : 
    6078     2628606 :             for (i = 0; i < relform->relnatts; i++)
    6079             :             {
    6080     1629990 :                 if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
    6081           0 :                     goto read_failed;
    6082             : 
    6083     1629990 :                 if (len > 0)
    6084             :                 {
    6085           0 :                     rel->rd_opcoptions[i] = (bytea *) MemoryContextAlloc(indexcxt, len);
    6086           0 :                     if (fread(rel->rd_opcoptions[i], 1, len, fp) != len)
    6087           0 :                         goto read_failed;
    6088             :                 }
    6089             :             }
    6090             : 
    6091             :             /* set up zeroed fmgr-info vector */
    6092      998616 :             nsupport = relform->relnatts * rel->rd_indam->amsupport;
    6093      998616 :             rel->rd_supportinfo = (FmgrInfo *)
    6094      998616 :                 MemoryContextAllocZero(indexcxt, nsupport * sizeof(FmgrInfo));
    6095             :         }
    6096             :         else
    6097             :         {
    6098             :             /* Count nailed rels to ensure we have 'em all */
    6099      596610 :             if (rel->rd_isnailed)
    6100      112278 :                 nailed_rels++;
    6101             : 
    6102             :             /* Load table AM data */
    6103      596610 :             if (RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind) || rel->rd_rel->relkind == RELKIND_SEQUENCE)
    6104      596610 :                 RelationInitTableAccessMethod(rel);
    6105             : 
    6106             :             Assert(rel->rd_index == NULL);
    6107             :             Assert(rel->rd_indextuple == NULL);
    6108             :             Assert(rel->rd_indexcxt == NULL);
    6109             :             Assert(rel->rd_indam == NULL);
    6110             :             Assert(rel->rd_opfamily == NULL);
    6111             :             Assert(rel->rd_opcintype == NULL);
    6112             :             Assert(rel->rd_support == NULL);
    6113             :             Assert(rel->rd_supportinfo == NULL);
    6114             :             Assert(rel->rd_indoption == NULL);
    6115             :             Assert(rel->rd_indcollation == NULL);
    6116             :             Assert(rel->rd_opcoptions == NULL);
    6117             :         }
    6118             : 
    6119             :         /*
    6120             :          * Rules and triggers are not saved (mainly because the internal
    6121             :          * format is complex and subject to change).  They must be rebuilt if
    6122             :          * needed by RelationCacheInitializePhase3.  This is not expected to
    6123             :          * be a big performance hit since few system catalogs have such. Ditto
    6124             :          * for RLS policy data, partition info, index expressions, predicates,
    6125             :          * exclusion info, and FDW info.
    6126             :          */
    6127     1595226 :         rel->rd_rules = NULL;
    6128     1595226 :         rel->rd_rulescxt = NULL;
    6129     1595226 :         rel->trigdesc = NULL;
    6130     1595226 :         rel->rd_rsdesc = NULL;
    6131     1595226 :         rel->rd_partkey = NULL;
    6132     1595226 :         rel->rd_partkeycxt = NULL;
    6133     1595226 :         rel->rd_partdesc = NULL;
    6134     1595226 :         rel->rd_partdesc_nodetached = NULL;
    6135     1595226 :         rel->rd_partdesc_nodetached_xmin = InvalidTransactionId;
    6136     1595226 :         rel->rd_pdcxt = NULL;
    6137     1595226 :         rel->rd_pddcxt = NULL;
    6138     1595226 :         rel->rd_partcheck = NIL;
    6139     1595226 :         rel->rd_partcheckvalid = false;
    6140     1595226 :         rel->rd_partcheckcxt = NULL;
    6141     1595226 :         rel->rd_indexprs = NIL;
    6142     1595226 :         rel->rd_indpred = NIL;
    6143     1595226 :         rel->rd_exclops = NULL;
    6144     1595226 :         rel->rd_exclprocs = NULL;
    6145     1595226 :         rel->rd_exclstrats = NULL;
    6146     1595226 :         rel->rd_fdwroutine = NULL;
    6147             : 
    6148             :         /*
    6149             :          * Reset transient-state fields in the relcache entry
    6150             :          */
    6151     1595226 :         rel->rd_smgr = NULL;
    6152     1595226 :         if (rel->rd_isnailed)
    6153      273570 :             rel->rd_refcnt = 1;
    6154             :         else
    6155     1321656 :             rel->rd_refcnt = 0;
    6156     1595226 :         rel->rd_indexvalid = false;
    6157     1595226 :         rel->rd_indexlist = NIL;
    6158     1595226 :         rel->rd_pkindex = InvalidOid;
    6159     1595226 :         rel->rd_replidindex = InvalidOid;
    6160     1595226 :         rel->rd_attrsvalid = false;
    6161     1595226 :         rel->rd_keyattr = NULL;
    6162     1595226 :         rel->rd_pkattr = NULL;
    6163     1595226 :         rel->rd_idattr = NULL;
    6164     1595226 :         rel->rd_hotblockingattr = NULL;
    6165     1595226 :         rel->rd_pubactions = NULL;
    6166     1595226 :         rel->rd_statvalid = false;
    6167     1595226 :         rel->rd_statlist = NIL;
    6168     1595226 :         rel->rd_fkeyvalid = false;
    6169     1595226 :         rel->rd_fkeylist = NIL;
    6170     1595226 :         rel->rd_createSubid = InvalidSubTransactionId;
    6171     1595226 :         rel->rd_newRelfilenodeSubid = InvalidSubTransactionId;
    6172     1595226 :         rel->rd_firstRelfilenodeSubid = InvalidSubTransactionId;
    6173     1595226 :         rel->rd_droppedSubid = InvalidSubTransactionId;
    6174     1595226 :         rel->rd_amcache = NULL;
    6175     3190452 :         MemSet(&rel->pgstat_info, 0, sizeof(rel->pgstat_info));
    6176             : 
    6177             :         /*
    6178             :          * Recompute lock and physical addressing info.  This is needed in
    6179             :          * case the pg_internal.init file was copied from some other database
    6180             :          * by CREATE DATABASE.
    6181             :          */
    6182     1595226 :         RelationInitLockInfo(rel);
    6183     1595226 :         RelationInitPhysicalAddr(rel);
    6184             :     }
    6185             : 
    6186             :     /*
    6187             :      * We reached the end of the init file without apparent problem.  Did we
    6188             :      * get the right number of nailed items?  This is a useful crosscheck in
    6189             :      * case the set of critical rels or indexes changes.  However, that should
    6190             :      * not happen in a normally-running system, so let's bleat if it does.
    6191             :      *
    6192             :      * For the shared init file, we're called before client authentication is
    6193             :      * done, which means that elog(WARNING) will go only to the postmaster
    6194             :      * log, where it's easily missed.  To ensure that developers notice bad
    6195             :      * values of NUM_CRITICAL_SHARED_RELS/NUM_CRITICAL_SHARED_INDEXES, we put
    6196             :      * an Assert(false) there.
    6197             :      */
    6198       24870 :     if (shared)
    6199             :     {
    6200       12798 :         if (nailed_rels != NUM_CRITICAL_SHARED_RELS ||
    6201             :             nailed_indexes != NUM_CRITICAL_SHARED_INDEXES)
    6202             :         {
    6203           0 :             elog(WARNING, "found %d nailed shared rels and %d nailed shared indexes in init file, but expected %d and %d respectively",
    6204             :                  nailed_rels, nailed_indexes,
    6205             :                  NUM_CRITICAL_SHARED_RELS, NUM_CRITICAL_SHARED_INDEXES);
    6206             :             /* Make sure we get developers' attention about this */
    6207             :             Assert(false);
    6208             :             /* In production builds, recover by bootstrapping the relcache */
    6209           0 :             goto read_failed;
    6210             :         }
    6211             :     }
    6212             :     else
    6213             :     {
    6214       12072 :         if (nailed_rels != NUM_CRITICAL_LOCAL_RELS ||
    6215             :             nailed_indexes != NUM_CRITICAL_LOCAL_INDEXES)
    6216             :         {
    6217           0 :             elog(WARNING, "found %d nailed rels and %d nailed indexes in init file, but expected %d and %d respectively",
    6218             :                  nailed_rels, nailed_indexes,
    6219             :                  NUM_CRITICAL_LOCAL_RELS, NUM_CRITICAL_LOCAL_INDEXES);
    6220             :             /* We don't need an Assert() in this case */
    6221           0 :             goto read_failed;
    6222             :         }
    6223             :     }
    6224             : 
    6225             :     /*
    6226             :      * OK, all appears well.
    6227             :      *
    6228             :      * Now insert all the new relcache entries into the cache.
    6229             :      */
    6230     1620096 :     for (relno = 0; relno < num_rels; relno++)
    6231             :     {
    6232     1595226 :         RelationCacheInsert(rels[relno], false);
    6233             :     }
    6234             : 
    6235       24870 :     pfree(rels);
    6236       24870 :     FreeFile(fp);
    6237             : 
    6238       24870 :     if (shared)
    6239       12798 :         criticalSharedRelcachesBuilt = true;
    6240             :     else
    6241       12072 :         criticalRelcachesBuilt = true;
    6242       24870 :     return true;
    6243             : 
    6244             :     /*
    6245             :      * init file is broken, so do it the hard way.  We don't bother trying to
    6246             :      * free the clutter we just allocated; it's not in the relcache so it
    6247             :      * won't hurt.
    6248             :      */
    6249           0 : read_failed:
    6250           0 :     pfree(rels);
    6251           0 :     FreeFile(fp);
    6252             : 
    6253           0 :     return false;
    6254             : }
    6255             : 
    6256             : /*
    6257             :  * Write out a new initialization file with the current contents
    6258             :  * of the relcache (either shared rels or local rels, as indicated).
    6259             :  */
    6260             : static void
    6261        3680 : write_relcache_init_file(bool shared)
    6262             : {
    6263             :     FILE       *fp;
    6264             :     char        tempfilename[MAXPGPATH];
    6265             :     char        finalfilename[MAXPGPATH];
    6266             :     int         magic;
    6267             :     HASH_SEQ_STATUS status;
    6268             :     RelIdCacheEnt *idhentry;
    6269             :     int         i;
    6270             : 
    6271             :     /*
    6272             :      * If we have already received any relcache inval events, there's no
    6273             :      * chance of succeeding so we may as well skip the whole thing.
    6274             :      */
    6275        3680 :     if (relcacheInvalsReceived != 0L)
    6276          12 :         return;
    6277             : 
    6278             :     /*
    6279             :      * We must write a temporary file and rename it into place. Otherwise,
    6280             :      * another backend starting at about the same time might crash trying to
    6281             :      * read the partially-complete file.
    6282             :      */
    6283        3668 :     if (shared)
    6284             :     {
    6285        1834 :         snprintf(tempfilename, sizeof(tempfilename), "global/%s.%d",
    6286             :                  RELCACHE_INIT_FILENAME, MyProcPid);
    6287        1834 :         snprintf(finalfilename, sizeof(finalfilename), "global/%s",
    6288             :                  RELCACHE_INIT_FILENAME);
    6289             :     }
    6290             :     else
    6291             :     {
    6292        1834 :         snprintf(tempfilename, sizeof(tempfilename), "%s/%s.%d",
    6293             :                  DatabasePath, RELCACHE_INIT_FILENAME, MyProcPid);
    6294        1834 :         snprintf(finalfilename, sizeof(finalfilename), "%s/%s",
    6295             :                  DatabasePath, RELCACHE_INIT_FILENAME);
    6296             :     }
    6297             : 
    6298        3668 :     unlink(tempfilename);       /* in case it exists w/wrong permissions */
    6299             : 
    6300        3668 :     fp = AllocateFile(tempfilename, PG_BINARY_W);
    6301        3668 :     if (fp == NULL)
    6302             :     {
    6303             :         /*
    6304             :          * We used to consider this a fatal error, but we might as well
    6305             :          * continue with backend startup ...
    6306             :          */
    6307           0 :         ereport(WARNING,
    6308             :                 (errcode_for_file_access(),
    6309             :                  errmsg("could not create relation-cache initialization file \"%s\": %m",
    6310             :                         tempfilename),
    6311             :                  errdetail("Continuing anyway, but there's something wrong.")));
    6312           0 :         return;
    6313             :     }
    6314             : 
    6315             :     /*
    6316             :      * Write a magic number to serve as a file version identifier.  We can
    6317             :      * change the magic number whenever the relcache layout changes.
    6318             :      */
    6319        3668 :     magic = RELCACHE_INIT_FILEMAGIC;
    6320        3668 :     if (fwrite(&magic, 1, sizeof(magic), fp) != sizeof(magic))
    6321           0 :         elog(FATAL, "could not write init file");
    6322             : 
    6323             :     /*
    6324             :      * Write all the appropriate reldescs (in no particular order).
    6325             :      */
    6326        3668 :     hash_seq_init(&status, RelationIdCache);
    6327             : 
    6328      484176 :     while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
    6329             :     {
    6330      480508 :         Relation    rel = idhentry->reldesc;
    6331      480508 :         Form_pg_class relform = rel->rd_rel;
    6332             : 
    6333             :         /* ignore if not correct group */
    6334      480508 :         if (relform->relisshared != shared)
    6335      240254 :             continue;
    6336             : 
    6337             :         /*
    6338             :          * Ignore if not supposed to be in init file.  We can allow any shared
    6339             :          * relation that's been loaded so far to be in the shared init file,
    6340             :          * but unshared relations must be ones that should be in the local
    6341             :          * file per RelationIdIsInInitFile.  (Note: if you want to change the
    6342             :          * criterion for rels to be kept in the init file, see also inval.c.
    6343             :          * The reason for filtering here is to be sure that we don't put
    6344             :          * anything into the local init file for which a relcache inval would
    6345             :          * not cause invalidation of that init file.)
    6346             :          */
    6347      240254 :         if (!shared && !RelationIdIsInInitFile(RelationGetRelid(rel)))
    6348             :         {
    6349             :             /* Nailed rels had better get stored. */
    6350             :             Assert(!rel->rd_isnailed);
    6351           0 :             continue;
    6352             :         }
    6353             : 
    6354             :         /* first write the relcache entry proper */
    6355      240254 :         write_item(rel, sizeof(RelationData), fp);
    6356             : 
    6357             :         /* next write the relation tuple form */
    6358      240254 :         write_item(relform, CLASS_TUPLE_SIZE, fp);
    6359             : 
    6360             :         /* next, do all the attribute tuple form data entries */
    6361     1390172 :         for (i = 0; i < relform->relnatts; i++)
    6362             :         {
    6363     1149918 :             write_item(TupleDescAttr(rel->rd_att, i),
    6364             :                        ATTRIBUTE_FIXED_PART_SIZE, fp);
    6365             :         }
    6366             : 
    6367             :         /* next, do the access method specific field */
    6368      240254 :         write_item(rel->rd_options,
    6369      240254 :                    (rel->rd_options ? VARSIZE(rel->rd_options) : 0),
    6370             :                    fp);
    6371             : 
    6372             :         /*
    6373             :          * If it's an index, there's more to do. Note we explicitly ignore
    6374             :          * partitioned indexes here.
    6375             :          */
    6376      240254 :         if (rel->rd_rel->relkind == RELKIND_INDEX)
    6377             :         {
    6378             :             /* write the pg_index tuple */
    6379             :             /* we assume this was created by heap_copytuple! */
    6380      150388 :             write_item(rel->rd_indextuple,
    6381      150388 :                        HEAPTUPLESIZE + rel->rd_indextuple->t_len,
    6382             :                        fp);
    6383             : 
    6384             :             /* next, write the vector of opfamily OIDs */
    6385      150388 :             write_item(rel->rd_opfamily,
    6386      150388 :                        relform->relnatts * sizeof(Oid),
    6387             :                        fp);
    6388             : 
    6389             :             /* next, write the vector of opcintype OIDs */
    6390      150388 :             write_item(rel->rd_opcintype,
    6391      150388 :                        relform->relnatts * sizeof(Oid),
    6392             :                        fp);
    6393             : 
    6394             :             /* next, write the vector of support procedure OIDs */
    6395      150388 :             write_item(rel->rd_support,
    6396      150388 :                        relform->relnatts * (rel->rd_indam->amsupport * sizeof(RegProcedure)),
    6397             :                        fp);
    6398             : 
    6399             :             /* next, write the vector of collation OIDs */
    6400      150388 :             write_item(rel->rd_indcollation,
    6401      150388 :                        relform->relnatts * sizeof(Oid),
    6402             :                        fp);
    6403             : 
    6404             :             /* finally, write the vector of indoption values */
    6405      150388 :             write_item(rel->rd_indoption,
    6406      150388 :                        relform->relnatts * sizeof(int16),
    6407             :                        fp);
    6408             : 
    6409             :             Assert(rel->rd_opcoptions);
    6410             : 
    6411             :             /* finally, write the vector of opcoptions values */
    6412      396144 :             for (i = 0; i < relform->relnatts; i++)
    6413             :             {
    6414      245756 :                 bytea      *opt = rel->rd_opcoptions[i];
    6415             : 
    6416      245756 :                 write_item(opt, opt ? VARSIZE(opt) : 0, fp);
    6417             :             }
    6418             :         }
    6419             :     }
    6420             : 
    6421        3668 :     if (FreeFile(fp))
    6422           0 :         elog(FATAL, "could not write init file");
    6423             : 
    6424             :     /*
    6425             :      * Now we have to check whether the data we've so painstakingly
    6426             :      * accumulated is already obsolete due to someone else's just-committed
    6427             :      * catalog changes.  If so, we just delete the temp file and leave it to
    6428             :      * the next backend to try again.  (Our own relcache entries will be
    6429             :      * updated by SI message processing, but we can't be sure whether what we
    6430             :      * wrote out was up-to-date.)
    6431             :      *
    6432             :      * This mustn't run concurrently with the code that unlinks an init file
    6433             :      * and sends SI messages, so grab a serialization lock for the duration.
    6434             :      */
    6435        3668 :     LWLockAcquire(RelCacheInitLock, LW_EXCLUSIVE);
    6436             : 
    6437             :     /* Make sure we have seen all incoming SI messages */
    6438        3668 :     AcceptInvalidationMessages();
    6439             : 
    6440             :     /*
    6441             :      * If we have received any SI relcache invals since backend start, assume
    6442             :      * we may have written out-of-date data.
    6443             :      */
    6444        3668 :     if (relcacheInvalsReceived == 0L)
    6445             :     {
    6446             :         /*
    6447             :          * OK, rename the temp file to its final name, deleting any
    6448             :          * previously-existing init file.
    6449             :          *
    6450             :          * Note: a failure here is possible under Cygwin, if some other
    6451             :          * backend is holding open an unlinked-but-not-yet-gone init file. So
    6452             :          * treat this as a noncritical failure; just remove the useless temp
    6453             :          * file on failure.
    6454             :          */
    6455        3668 :         if (rename(tempfilename, finalfilename) < 0)
    6456           0 :             unlink(tempfilename);
    6457             :     }
    6458             :     else
    6459             :     {
    6460             :         /* Delete the already-obsolete temp file */
    6461           0 :         unlink(tempfilename);
    6462             :     }
    6463             : 
    6464        3668 :     LWLockRelease(RelCacheInitLock);
    6465             : }
    6466             : 
    6467             : /* write a chunk of data preceded by its length */
    6468             : static void
    6469     3018764 : write_item(const void *data, Size len, FILE *fp)
    6470             : {
    6471     3018764 :     if (fwrite(&len, 1, sizeof(len), fp) != sizeof(len))
    6472           0 :         elog(FATAL, "could not write init file");
    6473     3018764 :     if (fwrite(data, 1, len, fp) != len)
    6474           0 :         elog(FATAL, "could not write init file");
    6475     3018764 : }
    6476             : 
    6477             : /*
    6478             :  * Determine whether a given relation (identified by OID) is one of the ones
    6479             :  * we should store in a relcache init file.
    6480             :  *
    6481             :  * We must cache all nailed rels, and for efficiency we should cache every rel
    6482             :  * that supports a syscache.  The former set is almost but not quite a subset
    6483             :  * of the latter. The special cases are relations where
    6484             :  * RelationCacheInitializePhase2/3 chooses to nail for efficiency reasons, but
    6485             :  * which do not support any syscache.
    6486             :  */
    6487             : bool
    6488     2264960 : RelationIdIsInInitFile(Oid relationId)
    6489             : {
    6490     2264960 :     if (relationId == SharedSecLabelRelationId ||
    6491     2261064 :         relationId == TriggerRelidNameIndexId ||
    6492     2260966 :         relationId == DatabaseNameIndexId ||
    6493             :         relationId == SharedSecLabelObjectIndexId)
    6494             :     {
    6495             :         /*
    6496             :          * If this Assert fails, we don't need the applicable special case
    6497             :          * anymore.
    6498             :          */
    6499             :         Assert(!RelationSupportsSysCache(relationId));
    6500        4540 :         return true;
    6501             :     }
    6502     2260420 :     return RelationSupportsSysCache(relationId);
    6503             : }
    6504             : 
    6505             : /*
    6506             :  * Invalidate (remove) the init file during commit of a transaction that
    6507             :  * changed one or more of the relation cache entries that are kept in the
    6508             :  * local init file.
    6509             :  *
    6510             :  * To be safe against concurrent inspection or rewriting of the init file,
    6511             :  * we must take RelCacheInitLock, then remove the old init file, then send
    6512             :  * the SI messages that include relcache inval for such relations, and then
    6513             :  * release RelCacheInitLock.  This serializes the whole affair against
    6514             :  * write_relcache_init_file, so that we can be sure that any other process
    6515             :  * that's concurrently trying to create a new init file won't move an
    6516             :  * already-stale version into place after we unlink.  Also, because we unlink
    6517             :  * before sending the SI messages, a backend that's currently starting cannot
    6518             :  * read the now-obsolete init file and then miss the SI messages that will
    6519             :  * force it to update its relcache entries.  (This works because the backend
    6520             :  * startup sequence gets into the sinval array before trying to load the init
    6521             :  * file.)
    6522             :  *
    6523             :  * We take the lock and do the unlink in RelationCacheInitFilePreInvalidate,
    6524             :  * then release the lock in RelationCacheInitFilePostInvalidate.  Caller must
    6525             :  * send any pending SI messages between those calls.
    6526             :  */
    6527             : void
    6528       67596 : RelationCacheInitFilePreInvalidate(void)
    6529             : {
    6530             :     char        localinitfname[MAXPGPATH];
    6531             :     char        sharedinitfname[MAXPGPATH];
    6532             : 
    6533       67596 :     if (DatabasePath)
    6534       67596 :         snprintf(localinitfname, sizeof(localinitfname), "%s/%s",
    6535             :                  DatabasePath, RELCACHE_INIT_FILENAME);
    6536       67596 :     snprintf(sharedinitfname, sizeof(sharedinitfname), "global/%s",
    6537             :              RELCACHE_INIT_FILENAME);
    6538             : 
    6539       67596 :     LWLockAcquire(RelCacheInitLock, LW_EXCLUSIVE);
    6540             : 
    6541             :     /*
    6542             :      * The files might not be there if no backend has been started since the
    6543             :      * last removal.  But complain about failures other than ENOENT with
    6544             :      * ERROR.  Fortunately, it's not too late to abort the transaction if we
    6545             :      * can't get rid of the would-be-obsolete init file.
    6546             :      */
    6547       67596 :     if (DatabasePath)
    6548       67596 :         unlink_initfile(localinitfname, ERROR);
    6549       67596 :     unlink_initfile(sharedinitfname, ERROR);
    6550       67596 : }
    6551             : 
    6552             : void
    6553       67596 : RelationCacheInitFilePostInvalidate(void)
    6554             : {
    6555       67596 :     LWLockRelease(RelCacheInitLock);
    6556       67596 : }
    6557             : 
    6558             : /*
    6559             :  * Remove the init files during postmaster startup.
    6560             :  *
    6561             :  * We used to keep the init files across restarts, but that is unsafe in PITR
    6562             :  * scenarios, and even in simple crash-recovery cases there are windows for
    6563             :  * the init files to become out-of-sync with the database.  So now we just
    6564             :  * remove them during startup and expect the first backend launch to rebuild
    6565             :  * them.  Of course, this has to happen in each database of the cluster.
    6566             :  */
    6567             : void
    6568        1860 : RelationCacheInitFileRemove(void)
    6569             : {
    6570        1860 :     const char *tblspcdir = "pg_tblspc";
    6571             :     DIR        *dir;
    6572             :     struct dirent *de;
    6573             :     char        path[MAXPGPATH + 10 + sizeof(TABLESPACE_VERSION_DIRECTORY)];
    6574             : 
    6575        1860 :     snprintf(path, sizeof(path), "global/%s",
    6576             :              RELCACHE_INIT_FILENAME);
    6577        1860 :     unlink_initfile(path, LOG);
    6578             : 
    6579             :     /* Scan everything in the default tablespace */
    6580        1860 :     RelationCacheInitFileRemoveInDir("base");
    6581             : 
    6582             :     /* Scan the tablespace link directory to find non-default tablespaces */
    6583        1860 :     dir = AllocateDir(tblspcdir);
    6584             : 
    6585        5656 :     while ((de = ReadDirExtended(dir, tblspcdir, LOG)) != NULL)
    6586             :     {
    6587        3796 :         if (strspn(de->d_name, "0123456789") == strlen(de->d_name))
    6588             :         {
    6589             :             /* Scan the tablespace dir for per-database dirs */
    6590          76 :             snprintf(path, sizeof(path), "%s/%s/%s",
    6591          76 :                      tblspcdir, de->d_name, TABLESPACE_VERSION_DIRECTORY);
    6592          76 :             RelationCacheInitFileRemoveInDir(path);
    6593             :         }
    6594             :     }
    6595             : 
    6596        1860 :     FreeDir(dir);
    6597        1860 : }
    6598             : 
    6599             : /* Process one per-tablespace directory for RelationCacheInitFileRemove */
    6600             : static void
    6601        1936 : RelationCacheInitFileRemoveInDir(const char *tblspcpath)
    6602             : {
    6603             :     DIR        *dir;
    6604             :     struct dirent *de;
    6605             :     char        initfilename[MAXPGPATH * 2];
    6606             : 
    6607             :     /* Scan the tablespace directory to find per-database directories */
    6608        1936 :     dir = AllocateDir(tblspcpath);
    6609             : 
    6610        9846 :     while ((de = ReadDirExtended(dir, tblspcpath, LOG)) != NULL)
    6611             :     {
    6612        7910 :         if (strspn(de->d_name, "0123456789") == strlen(de->d_name))
    6613             :         {
    6614             :             /* Try to remove the init file in each database */
    6615        3956 :             snprintf(initfilename, sizeof(initfilename), "%s/%s/%s",
    6616        3956 :                      tblspcpath, de->d_name, RELCACHE_INIT_FILENAME);
    6617        3956 :             unlink_initfile(initfilename, LOG);
    6618             :         }
    6619             :     }
    6620             : 
    6621        1936 :     FreeDir(dir);
    6622        1936 : }
    6623             : 
    6624             : static void
    6625      141008 : unlink_initfile(const char *initfilename, int elevel)
    6626             : {
    6627      141008 :     if (unlink(initfilename) < 0)
    6628             :     {
    6629             :         /* It might not be there, but log any error other than ENOENT */
    6630      139068 :         if (errno != ENOENT)
    6631           0 :             ereport(elevel,
    6632             :                     (errcode_for_file_access(),
    6633             :                      errmsg("could not remove cache file \"%s\": %m",
    6634             :                             initfilename)));
    6635             :     }
    6636      141008 : }

Generated by: LCOV version 1.14