|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * relfilenodemap.c |
| 4 | + * relfilenode to oid mapping cache. |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group |
| 7 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 8 | + * |
| 9 | + * IDENTIFICATION |
| 10 | + * src/backend/utils/cache/relfilenode.c |
| 11 | + * |
| 12 | + *------------------------------------------------------------------------- |
| 13 | + */ |
| 14 | +#include "postgres.h" |
| 15 | + |
| 16 | +#include "access/genam.h" |
| 17 | +#include "access/heapam.h" |
| 18 | +#include "access/htup_details.h" |
| 19 | +#include "catalog/indexing.h" |
| 20 | +#include "catalog/pg_class.h" |
| 21 | +#include "catalog/pg_tablespace.h" |
| 22 | +#include "miscadmin.h" |
| 23 | +#include "utils/builtins.h" |
| 24 | +#include "utils/catcache.h" |
| 25 | +#include "utils/hsearch.h" |
| 26 | +#include "utils/inval.h" |
| 27 | +#include "utils/fmgroids.h" |
| 28 | +#include "utils/rel.h" |
| 29 | +#include "utils/relfilenodemap.h" |
| 30 | +#include "utils/relmapper.h" |
| 31 | + |
| 32 | +/* Hash table for informations about each relfilenode <-> oid pair */ |
| 33 | +static HTAB *RelfilenodeMapHash = NULL; |
| 34 | + |
| 35 | +/* built first time through in InitializeRelfilenodeMap */ |
| 36 | +ScanKeyData relfilenode_skey[2]; |
| 37 | + |
| 38 | +typedef struct |
| 39 | +{ |
| 40 | + Oid reltablespace; |
| 41 | + Oid relfilenode; |
| 42 | +} RelfilenodeMapKey; |
| 43 | + |
| 44 | +typedef struct |
| 45 | +{ |
| 46 | + RelfilenodeMapKey key; /* lookup key - must be first */ |
| 47 | + Oid relid; /* pg_class.oid */ |
| 48 | +} RelfilenodeMapEntry; |
| 49 | + |
| 50 | +/* |
| 51 | + * RelfilenodeMapInvalidateCallback |
| 52 | + * Flush mapping entries when pg_class is updated in a relevant fashion. |
| 53 | + */ |
| 54 | +static void |
| 55 | +RelfilenodeMapInvalidateCallback(Datum arg, Oid relid) |
| 56 | +{ |
| 57 | + HASH_SEQ_STATUS status; |
| 58 | + RelfilenodeMapEntry *entry; |
| 59 | + |
| 60 | + /* nothing to do if not active or deleted */ |
| 61 | + if (RelfilenodeMapHash == NULL) |
| 62 | + return; |
| 63 | + |
| 64 | + /* if relid is InvalidOid, we must invalidate the entire cache */ |
| 65 | + if (relid == InvalidOid) |
| 66 | + { |
| 67 | + hash_destroy(RelfilenodeMapHash); |
| 68 | + RelfilenodeMapHash = NULL; |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + hash_seq_init(&status, RelfilenodeMapHash); |
| 73 | + while ((entry = (RelfilenodeMapEntry *) hash_seq_search(&status)) != NULL) |
| 74 | + { |
| 75 | + /* Same OID may occur in more than one tablespace. */ |
| 76 | + if (entry->relid == relid) |
| 77 | + { |
| 78 | + if (hash_search(RelfilenodeMapHash, |
| 79 | + (void *) &entry->key, |
| 80 | + HASH_REMOVE, |
| 81 | + NULL) == NULL) |
| 82 | + elog(ERROR, "hash table corrupted"); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/* |
| 88 | + * RelfilenodeMapInvalidateCallback |
| 89 | + * Initialize cache, either on first use or after a reset. |
| 90 | + */ |
| 91 | +static void |
| 92 | +InitializeRelfilenodeMap(void) |
| 93 | +{ |
| 94 | + HASHCTL ctl; |
| 95 | + static bool initial_init_done = false; |
| 96 | + int i; |
| 97 | + |
| 98 | + /* Make sure we've initialized CacheMemoryContext. */ |
| 99 | + if (CacheMemoryContext == NULL) |
| 100 | + CreateCacheMemoryContext(); |
| 101 | + |
| 102 | + /* Initialize the hash table. */ |
| 103 | + MemSet(&ctl, 0, sizeof(ctl)); |
| 104 | + ctl.keysize = sizeof(RelfilenodeMapKey); |
| 105 | + ctl.entrysize = sizeof(RelfilenodeMapEntry); |
| 106 | + ctl.hash = tag_hash; |
| 107 | + ctl.hcxt = CacheMemoryContext; |
| 108 | + |
| 109 | + RelfilenodeMapHash = |
| 110 | + hash_create("RelfilenodeMap cache", 1024, &ctl, |
| 111 | + HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT); |
| 112 | + |
| 113 | + /* |
| 114 | + * For complete resets we simply delete the entire hash, but there's no |
| 115 | + * need to do the other stuff multiple times. Especially the initialization |
| 116 | + * of the relcche invalidation should only be done once. |
| 117 | + */ |
| 118 | + if (initial_init_done) |
| 119 | + return; |
| 120 | + |
| 121 | + /* build skey */ |
| 122 | + MemSet(&relfilenode_skey, 0, sizeof(relfilenode_skey)); |
| 123 | + |
| 124 | + for (i = 0; i < 2; i++) |
| 125 | + { |
| 126 | + fmgr_info_cxt(F_OIDEQ, |
| 127 | + &relfilenode_skey[i].sk_func, |
| 128 | + CacheMemoryContext); |
| 129 | + relfilenode_skey[i].sk_strategy = BTEqualStrategyNumber; |
| 130 | + relfilenode_skey[i].sk_subtype = InvalidOid; |
| 131 | + relfilenode_skey[i].sk_collation = InvalidOid; |
| 132 | + } |
| 133 | + |
| 134 | + relfilenode_skey[0].sk_attno = Anum_pg_class_reltablespace; |
| 135 | + relfilenode_skey[1].sk_attno = Anum_pg_class_relfilenode; |
| 136 | + |
| 137 | + /* Watch for invalidation events. */ |
| 138 | + CacheRegisterRelcacheCallback(RelfilenodeMapInvalidateCallback, |
| 139 | + (Datum) 0); |
| 140 | + initial_init_done = true; |
| 141 | +} |
| 142 | + |
| 143 | +/* |
| 144 | + * Map a relation's (tablespace, filenode) to a relation's oid and cache the |
| 145 | + * result. |
| 146 | + * |
| 147 | + * Returns InvalidOid if no relation matching the criteria could be found. |
| 148 | + */ |
| 149 | +Oid |
| 150 | +RelidByRelfilenode(Oid reltablespace, Oid relfilenode) |
| 151 | +{ |
| 152 | + RelfilenodeMapKey key; |
| 153 | + RelfilenodeMapEntry *entry; |
| 154 | + bool found; |
| 155 | + SysScanDesc scandesc; |
| 156 | + Relation relation; |
| 157 | + HeapTuple ntp; |
| 158 | + ScanKeyData skey[2]; |
| 159 | + |
| 160 | + if (RelfilenodeMapHash == NULL) |
| 161 | + InitializeRelfilenodeMap(); |
| 162 | + |
| 163 | + /* pg_class will show 0 when the value is actually MyDatabaseTableSpace */ |
| 164 | + if (reltablespace == MyDatabaseTableSpace) |
| 165 | + reltablespace = 0; |
| 166 | + |
| 167 | + MemSet(&key, 0, sizeof(key)); |
| 168 | + key.reltablespace = reltablespace; |
| 169 | + key.relfilenode = relfilenode; |
| 170 | + |
| 171 | + /* |
| 172 | + * Check cache and enter entry if nothing could be found. Even if no target |
| 173 | + * relation can be found later on we store the negative match and return a |
| 174 | + * InvalidOid from cache. That's not really necessary for performance since |
| 175 | + * querying invalid values isn't supposed to be a frequent thing, but the |
| 176 | + * implementation is simpler this way. |
| 177 | + */ |
| 178 | + entry = hash_search(RelfilenodeMapHash, (void *) &key, HASH_ENTER, &found); |
| 179 | + |
| 180 | + if (found) |
| 181 | + return entry->relid; |
| 182 | + |
| 183 | + /* ok, no previous cache entry, do it the hard way */ |
| 184 | + |
| 185 | + /* check shared tables */ |
| 186 | + if (reltablespace == GLOBALTABLESPACE_OID) |
| 187 | + { |
| 188 | + entry->relid = RelationMapFilenodeToOid(relfilenode, true); |
| 189 | + return entry->relid; |
| 190 | + } |
| 191 | + |
| 192 | + /* check plain relations by looking in pg_class */ |
| 193 | + relation = heap_open(RelationRelationId, AccessShareLock); |
| 194 | + |
| 195 | + /* copy scankey to local copy, it will be modified during the scan */ |
| 196 | + memcpy(skey, relfilenode_skey, sizeof(skey)); |
| 197 | + |
| 198 | + /* set scan arguments */ |
| 199 | + skey[0].sk_argument = ObjectIdGetDatum(reltablespace); |
| 200 | + skey[1].sk_argument = ObjectIdGetDatum(relfilenode); |
| 201 | + |
| 202 | + scandesc = systable_beginscan(relation, |
| 203 | + ClassTblspcRelfilenodeIndexId, |
| 204 | + true, |
| 205 | + NULL, |
| 206 | + 2, |
| 207 | + skey); |
| 208 | + |
| 209 | + found = false; |
| 210 | + |
| 211 | + while (HeapTupleIsValid(ntp = systable_getnext(scandesc))) |
| 212 | + { |
| 213 | + bool isnull; |
| 214 | + |
| 215 | + if (found) |
| 216 | + elog(ERROR, |
| 217 | + "unexpected duplicate for tablespace %u, relfilenode %u", |
| 218 | + reltablespace, relfilenode); |
| 219 | + found = true; |
| 220 | + |
| 221 | +#ifdef USE_ASSERT_CHECKING |
| 222 | + if (assert_enabled) |
| 223 | + { |
| 224 | + Oid check; |
| 225 | + check = fastgetattr(ntp, Anum_pg_class_reltablespace, |
| 226 | + RelationGetDescr(relation), |
| 227 | + &isnull); |
| 228 | + Assert(!isnull && check == reltablespace); |
| 229 | + |
| 230 | + check = fastgetattr(ntp, Anum_pg_class_relfilenode, |
| 231 | + RelationGetDescr(relation), |
| 232 | + &isnull); |
| 233 | + Assert(!isnull && check == relfilenode); |
| 234 | + } |
| 235 | +#endif |
| 236 | + entry->relid = HeapTupleGetOid(ntp); |
| 237 | + } |
| 238 | + |
| 239 | + systable_endscan(scandesc); |
| 240 | + heap_close(relation, AccessShareLock); |
| 241 | + |
| 242 | + /* check for tables that are mapped but not shared */ |
| 243 | + if (!found) |
| 244 | + entry->relid = RelationMapFilenodeToOid(relfilenode, false); |
| 245 | + |
| 246 | + return entry->relid; |
| 247 | +} |
0 commit comments