LCOV - code coverage report
Current view: top level - src/backend/utils/cache - relfilenumbermap.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 59 63 93.7 %
Date: 2023-12-02 15:10:53 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * relfilenumbermap.c
       4             :  *    relfilenumber to oid mapping cache.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/utils/cache/relfilenumbermap.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/relfilenumbermap.h"
      29             : #include "utils/relmapper.h"
      30             : 
      31             : /* Hash table for information about each relfilenumber <-> oid pair */
      32             : static HTAB *RelfilenumberMapHash = NULL;
      33             : 
      34             : /* built first time through in InitializeRelfilenumberMap */
      35             : static ScanKeyData relfilenumber_skey[2];
      36             : 
      37             : typedef struct
      38             : {
      39             :     Oid         reltablespace;
      40             :     RelFileNumber relfilenumber;
      41             : } RelfilenumberMapKey;
      42             : 
      43             : typedef struct
      44             : {
      45             :     RelfilenumberMapKey key;    /* lookup key - must be first */
      46             :     Oid         relid;          /* pg_class.oid */
      47             : } RelfilenumberMapEntry;
      48             : 
      49             : /*
      50             :  * RelfilenumberMapInvalidateCallback
      51             :  *      Flush mapping entries when pg_class is updated in a relevant fashion.
      52             :  */
      53             : static void
      54       34704 : RelfilenumberMapInvalidateCallback(Datum arg, Oid relid)
      55             : {
      56             :     HASH_SEQ_STATUS status;
      57             :     RelfilenumberMapEntry *entry;
      58             : 
      59             :     /* callback only gets registered after creating the hash */
      60             :     Assert(RelfilenumberMapHash != NULL);
      61             : 
      62       34704 :     hash_seq_init(&status, RelfilenumberMapHash);
      63    19106026 :     while ((entry = (RelfilenumberMapEntry *) 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    19071322 :         if (relid == InvalidOid ||  /* complete reset */
      71    19070534 :             entry->relid == InvalidOid ||    /* negative cache entry */
      72    19070428 :             entry->relid == relid)   /* individual flushed relation */
      73             :         {
      74        1276 :             if (hash_search(RelfilenumberMapHash,
      75        1276 :                             &entry->key,
      76             :                             HASH_REMOVE,
      77             :                             NULL) == NULL)
      78           0 :                 elog(ERROR, "hash table corrupted");
      79             :         }
      80             :     }
      81       34704 : }
      82             : 
      83             : /*
      84             :  * InitializeRelfilenumberMap
      85             :  *      Initialize cache, either on first use or after a reset.
      86             :  */
      87             : static void
      88         342 : InitializeRelfilenumberMap(void)
      89             : {
      90             :     HASHCTL     ctl;
      91             :     int         i;
      92             : 
      93             :     /* Make sure we've initialized CacheMemoryContext. */
      94         342 :     if (CacheMemoryContext == NULL)
      95           0 :         CreateCacheMemoryContext();
      96             : 
      97             :     /* build skey */
      98        6498 :     MemSet(&relfilenumber_skey, 0, sizeof(relfilenumber_skey));
      99             : 
     100        1026 :     for (i = 0; i < 2; i++)
     101             :     {
     102         684 :         fmgr_info_cxt(F_OIDEQ,
     103             :                       &relfilenumber_skey[i].sk_func,
     104             :                       CacheMemoryContext);
     105         684 :         relfilenumber_skey[i].sk_strategy = BTEqualStrategyNumber;
     106         684 :         relfilenumber_skey[i].sk_subtype = InvalidOid;
     107         684 :         relfilenumber_skey[i].sk_collation = InvalidOid;
     108             :     }
     109             : 
     110         342 :     relfilenumber_skey[0].sk_attno = Anum_pg_class_reltablespace;
     111         342 :     relfilenumber_skey[1].sk_attno = Anum_pg_class_relfilenode;
     112             : 
     113             :     /*
     114             :      * Only create the RelfilenumberMapHash 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         342 :     ctl.keysize = sizeof(RelfilenumberMapKey);
     119         342 :     ctl.entrysize = sizeof(RelfilenumberMapEntry);
     120         342 :     ctl.hcxt = CacheMemoryContext;
     121             : 
     122         342 :     RelfilenumberMapHash =
     123         342 :         hash_create("RelfilenumberMap cache", 64, &ctl,
     124             :                     HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
     125             : 
     126             :     /* Watch for invalidation events. */
     127         342 :     CacheRegisterRelcacheCallback(RelfilenumberMapInvalidateCallback,
     128             :                                   (Datum) 0);
     129         342 : }
     130             : 
     131             : /*
     132             :  * Map a relation's (tablespace, relfilenumber) to a relation's oid and cache
     133             :  * the result.
     134             :  *
     135             :  * Returns InvalidOid if no relation matching the criteria could be found.
     136             :  */
     137             : Oid
     138      686526 : RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
     139             : {
     140             :     RelfilenumberMapKey key;
     141             :     RelfilenumberMapEntry *entry;
     142             :     bool        found;
     143             :     SysScanDesc scandesc;
     144             :     Relation    relation;
     145             :     HeapTuple   ntp;
     146             :     ScanKeyData skey[2];
     147             :     Oid         relid;
     148             : 
     149      686526 :     if (RelfilenumberMapHash == NULL)
     150         342 :         InitializeRelfilenumberMap();
     151             : 
     152             :     /* pg_class will show 0 when the value is actually MyDatabaseTableSpace */
     153      686526 :     if (reltablespace == MyDatabaseTableSpace)
     154      678868 :         reltablespace = 0;
     155             : 
     156     1373052 :     MemSet(&key, 0, sizeof(key));
     157      686526 :     key.reltablespace = reltablespace;
     158      686526 :     key.relfilenumber = relfilenumber;
     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      686526 :     entry = hash_search(RelfilenumberMapHash, &key, HASH_FIND, &found);
     168             : 
     169      686526 :     if (found)
     170      676814 :         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        9712 :     relid = InvalidOid;
     176             : 
     177        9712 :     if (reltablespace == GLOBALTABLESPACE_OID)
     178             :     {
     179             :         /*
     180             :          * Ok, shared table, check relmapper.
     181             :          */
     182         314 :         relid = RelationMapFilenumberToOid(relfilenumber, 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        9398 :         relation = table_open(RelationRelationId, AccessShareLock);
     193             : 
     194             :         /* copy scankey to local copy, it will be modified during the scan */
     195        9392 :         memcpy(skey, relfilenumber_skey, sizeof(skey));
     196             : 
     197             :         /* set scan arguments */
     198        9392 :         skey[0].sk_argument = ObjectIdGetDatum(reltablespace);
     199        9392 :         skey[1].sk_argument = ObjectIdGetDatum(relfilenumber);
     200             : 
     201        9392 :         scandesc = systable_beginscan(relation,
     202             :                                       ClassTblspcRelfilenodeIndexId,
     203             :                                       true,
     204             :                                       NULL,
     205             :                                       2,
     206             :                                       skey);
     207             : 
     208        9392 :         found = false;
     209             : 
     210       18054 :         while (HeapTupleIsValid(ntp = systable_getnext(scandesc)))
     211             :         {
     212        8662 :             Form_pg_class classform = (Form_pg_class) GETSTRUCT(ntp);
     213             : 
     214        8662 :             if (found)
     215           0 :                 elog(ERROR,
     216             :                      "unexpected duplicate for tablespace %u, relfilenumber %u",
     217             :                      reltablespace, relfilenumber);
     218        8662 :             found = true;
     219             : 
     220             :             Assert(classform->reltablespace == reltablespace);
     221             :             Assert(classform->relfilenode == relfilenumber);
     222        8662 :             relid = classform->oid;
     223             :         }
     224             : 
     225        9384 :         systable_endscan(scandesc);
     226        9384 :         table_close(relation, AccessShareLock);
     227             : 
     228             :         /* check for tables that are mapped but not shared */
     229        9384 :         if (!found)
     230         722 :             relid = RelationMapFilenumberToOid(relfilenumber, 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        9698 :     entry = hash_search(RelfilenumberMapHash, &key, HASH_ENTER, &found);
     239        9698 :     if (found)
     240           0 :         elog(ERROR, "corrupted hashtable");
     241        9698 :     entry->relid = relid;
     242             : 
     243        9698 :     return relid;
     244             : }

Generated by: LCOV version 1.14