LCOV - code coverage report
Current view: top level - src/backend/utils/cache - relfilenodemap.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14devel Lines: 59 63 93.7 %
Date: 2021-01-26 02:06:48 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * relfilenodemap.c
       4             :  *    relfilenode to oid mapping cache.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/utils/cache/relfilenodemap.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #include "postgres.h"
      15             : 
      16             : #include "access/genam.h"
      17             : #include "access/htup_details.h"
      18             : #include "access/table.h"
      19             : #include "catalog/pg_class.h"
      20             : #include "catalog/pg_tablespace.h"
      21             : #include "miscadmin.h"
      22             : #include "utils/builtins.h"
      23             : #include "utils/catcache.h"
      24             : #include "utils/fmgroids.h"
      25             : #include "utils/hsearch.h"
      26             : #include "utils/inval.h"
      27             : #include "utils/rel.h"
      28             : #include "utils/relfilenodemap.h"
      29             : #include "utils/relmapper.h"
      30             : 
      31             : /* Hash table for information about each relfilenode <-> oid pair */
      32             : static HTAB *RelfilenodeMapHash = NULL;
      33             : 
      34             : /* built first time through in InitializeRelfilenodeMap */
      35             : static ScanKeyData relfilenode_skey[2];
      36             : 
      37             : typedef struct
      38             : {
      39             :     Oid         reltablespace;
      40             :     Oid         relfilenode;
      41             : } RelfilenodeMapKey;
      42             : 
      43             : typedef struct
      44             : {
      45             :     RelfilenodeMapKey key;      /* lookup key - must be first */
      46             :     Oid         relid;          /* pg_class.oid */
      47             : } RelfilenodeMapEntry;
      48             : 
      49             : /*
      50             :  * RelfilenodeMapInvalidateCallback
      51             :  *      Flush mapping entries when pg_class is updated in a relevant fashion.
      52             :  */
      53             : static void
      54       23408 : RelfilenodeMapInvalidateCallback(Datum arg, Oid relid)
      55             : {
      56             :     HASH_SEQ_STATUS status;
      57             :     RelfilenodeMapEntry *entry;
      58             : 
      59             :     /* callback only gets registered after creating the hash */
      60             :     Assert(RelfilenodeMapHash != NULL);
      61             : 
      62       23408 :     hash_seq_init(&status, RelfilenodeMapHash);
      63     9413546 :     while ((entry = (RelfilenodeMapEntry *) hash_seq_search(&status)) != NULL)
      64             :     {
      65             :         /*
      66             :          * If relid is InvalidOid, signaling a complete reset, we must remove
      67             :          * all entries, otherwise just remove the specific relation's entry.
      68             :          * Always remove negative cache entries.
      69             :          */
      70     9390138 :         if (relid == InvalidOid ||  /* complete reset */
      71     9389438 :             entry->relid == InvalidOid ||    /* negative cache entry */
      72     9389332 :             entry->relid == relid)   /* individual flushed relation */
      73             :         {
      74        1068 :             if (hash_search(RelfilenodeMapHash,
      75        1068 :                             (void *) &entry->key,
      76             :                             HASH_REMOVE,
      77             :                             NULL) == NULL)
      78           0 :                 elog(ERROR, "hash table corrupted");
      79             :         }
      80             :     }
      81       23408 : }
      82             : 
      83             : /*
      84             :  * InitializeRelfilenodeMap
      85             :  *      Initialize cache, either on first use or after a reset.
      86             :  */
      87             : static void
      88         118 : InitializeRelfilenodeMap(void)
      89             : {
      90             :     HASHCTL     ctl;
      91             :     int         i;
      92             : 
      93             :     /* Make sure we've initialized CacheMemoryContext. */
      94         118 :     if (CacheMemoryContext == NULL)
      95           0 :         CreateCacheMemoryContext();
      96             : 
      97             :     /* build skey */
      98        2242 :     MemSet(&relfilenode_skey, 0, sizeof(relfilenode_skey));
      99             : 
     100         354 :     for (i = 0; i < 2; i++)
     101             :     {
     102         236 :         fmgr_info_cxt(F_OIDEQ,
     103             :                       &relfilenode_skey[i].sk_func,
     104             :                       CacheMemoryContext);
     105         236 :         relfilenode_skey[i].sk_strategy = BTEqualStrategyNumber;
     106         236 :         relfilenode_skey[i].sk_subtype = InvalidOid;
     107         236 :         relfilenode_skey[i].sk_collation = InvalidOid;
     108             :     }
     109             : 
     110         118 :     relfilenode_skey[0].sk_attno = Anum_pg_class_reltablespace;
     111         118 :     relfilenode_skey[1].sk_attno = Anum_pg_class_relfilenode;
     112             : 
     113             :     /*
     114             :      * Only create the RelfilenodeMapHash now, so we don't end up partially
     115             :      * initialized when fmgr_info_cxt() above ERRORs out with an out of memory
     116             :      * error.
     117             :      */
     118         118 :     ctl.keysize = sizeof(RelfilenodeMapKey);
     119         118 :     ctl.entrysize = sizeof(RelfilenodeMapEntry);
     120         118 :     ctl.hcxt = CacheMemoryContext;
     121             : 
     122         118 :     RelfilenodeMapHash =
     123         118 :         hash_create("RelfilenodeMap cache", 64, &ctl,
     124             :                     HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
     125             : 
     126             :     /* Watch for invalidation events. */
     127         118 :     CacheRegisterRelcacheCallback(RelfilenodeMapInvalidateCallback,
     128             :                                   (Datum) 0);
     129         118 : }
     130             : 
     131             : /*
     132             :  * Map a relation's (tablespace, filenode) to a relation's oid and cache the
     133             :  * result.
     134             :  *
     135             :  * Returns InvalidOid if no relation matching the criteria could be found.
     136             :  */
     137             : Oid
     138      440206 : RelidByRelfilenode(Oid reltablespace, Oid relfilenode)
     139             : {
     140             :     RelfilenodeMapKey key;
     141             :     RelfilenodeMapEntry *entry;
     142             :     bool        found;
     143             :     SysScanDesc scandesc;
     144             :     Relation    relation;
     145             :     HeapTuple   ntp;
     146             :     ScanKeyData skey[2];
     147             :     Oid         relid;
     148             : 
     149      440206 :     if (RelfilenodeMapHash == NULL)
     150         118 :         InitializeRelfilenodeMap();
     151             : 
     152             :     /* pg_class will show 0 when the value is actually MyDatabaseTableSpace */
     153      440206 :     if (reltablespace == MyDatabaseTableSpace)
     154      435586 :         reltablespace = 0;
     155             : 
     156      880412 :     MemSet(&key, 0, sizeof(key));
     157      440206 :     key.reltablespace = reltablespace;
     158      440206 :     key.relfilenode = relfilenode;
     159             : 
     160             :     /*
     161             :      * Check cache and return entry if one is found. Even if no target
     162             :      * relation can be found later on we store the negative match and return a
     163             :      * InvalidOid from cache. That's not really necessary for performance
     164             :      * since querying invalid values isn't supposed to be a frequent thing,
     165             :      * but it's basically free.
     166             :      */
     167      440206 :     entry = hash_search(RelfilenodeMapHash, (void *) &key, HASH_FIND, &found);
     168             : 
     169      440206 :     if (found)
     170      434404 :         return entry->relid;
     171             : 
     172             :     /* ok, no previous cache entry, do it the hard way */
     173             : 
     174             :     /* initialize empty/negative cache entry before doing the actual lookups */
     175        5802 :     relid = InvalidOid;
     176             : 
     177        5802 :     if (reltablespace == GLOBALTABLESPACE_OID)
     178             :     {
     179             :         /*
     180             :          * Ok, shared table, check relmapper.
     181             :          */
     182         172 :         relid = RelationMapFilenodeToOid(relfilenode, true);
     183             :     }
     184             :     else
     185             :     {
     186             :         /*
     187             :          * Not a shared table, could either be a plain relation or a
     188             :          * non-shared, nailed one, like e.g. pg_class.
     189             :          */
     190             : 
     191             :         /* check for plain relations by looking in pg_class */
     192        5630 :         relation = table_open(RelationRelationId, AccessShareLock);
     193             : 
     194             :         /* copy scankey to local copy, it will be modified during the scan */
     195        5624 :         memcpy(skey, relfilenode_skey, sizeof(skey));
     196             : 
     197             :         /* set scan arguments */
     198        5624 :         skey[0].sk_argument = ObjectIdGetDatum(reltablespace);
     199        5624 :         skey[1].sk_argument = ObjectIdGetDatum(relfilenode);
     200             : 
     201        5624 :         scandesc = systable_beginscan(relation,
     202             :                                       ClassTblspcRelfilenodeIndexId,
     203             :                                       true,
     204             :                                       NULL,
     205             :                                       2,
     206             :                                       skey);
     207             : 
     208        5624 :         found = false;
     209             : 
     210       10744 :         while (HeapTupleIsValid(ntp = systable_getnext(scandesc)))
     211             :         {
     212        5120 :             Form_pg_class classform = (Form_pg_class) GETSTRUCT(ntp);
     213             : 
     214        5120 :             if (found)
     215           0 :                 elog(ERROR,
     216             :                      "unexpected duplicate for tablespace %u, relfilenode %u",
     217             :                      reltablespace, relfilenode);
     218        5120 :             found = true;
     219             : 
     220             :             Assert(classform->reltablespace == reltablespace);
     221             :             Assert(classform->relfilenode == relfilenode);
     222        5120 :             relid = classform->oid;
     223             :         }
     224             : 
     225        5618 :         systable_endscan(scandesc);
     226        5618 :         table_close(relation, AccessShareLock);
     227             : 
     228             :         /* check for tables that are mapped but not shared */
     229        5618 :         if (!found)
     230         498 :             relid = RelationMapFilenodeToOid(relfilenode, false);
     231             :     }
     232             : 
     233             :     /*
     234             :      * Only enter entry into cache now, our opening of pg_class could have
     235             :      * caused cache invalidations to be executed which would have deleted a
     236             :      * new entry if we had entered it above.
     237             :      */
     238        5790 :     entry = hash_search(RelfilenodeMapHash, (void *) &key, HASH_ENTER, &found);
     239        5790 :     if (found)
     240           0 :         elog(ERROR, "corrupted hashtable");
     241        5790 :     entry->relid = relid;
     242             : 
     243        5790 :     return relid;
     244             : }

Generated by: LCOV version 1.13