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

Generated by: LCOV version 1.13