PostgreSQL Source Code  git master
relfilenumbermap.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * relfilenumbermap.c
4  * relfilenumber to oid mapping cache.
5  *
6  * Portions Copyright (c) 1996-2022, 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 */
36 
37 typedef struct
38 {
42 
43 typedef struct
44 {
45  RelfilenumberMapKey key; /* lookup key - must be first */
46  Oid relid; /* pg_class.oid */
48 
49 /*
50  * RelfilenumberMapInvalidateCallback
51  * Flush mapping entries when pg_class is updated in a relevant fashion.
52  */
53 static void
55 {
57  RelfilenumberMapEntry *entry;
58 
59  /* callback only gets registered after creating the hash */
61 
63  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  if (relid == InvalidOid || /* complete reset */
71  entry->relid == InvalidOid || /* negative cache entry */
72  entry->relid == relid) /* individual flushed relation */
73  {
75  (void *) &entry->key,
77  NULL) == NULL)
78  elog(ERROR, "hash table corrupted");
79  }
80  }
81 }
82 
83 /*
84  * InitializeRelfilenumberMap
85  * Initialize cache, either on first use or after a reset.
86  */
87 static void
89 {
90  HASHCTL ctl;
91  int i;
92 
93  /* Make sure we've initialized CacheMemoryContext. */
94  if (CacheMemoryContext == NULL)
96 
97  /* build skey */
99 
100  for (i = 0; i < 2; i++)
101  {
102  fmgr_info_cxt(F_OIDEQ,
103  &relfilenumber_skey[i].sk_func,
108  }
109 
110  relfilenumber_skey[0].sk_attno = Anum_pg_class_reltablespace;
111  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  ctl.keysize = sizeof(RelfilenumberMapKey);
119  ctl.entrysize = sizeof(RelfilenumberMapEntry);
120  ctl.hcxt = CacheMemoryContext;
121 
123  hash_create("RelfilenumberMap cache", 64, &ctl,
125 
126  /* Watch for invalidation events. */
128  (Datum) 0);
129 }
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 RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
139 {
141  RelfilenumberMapEntry *entry;
142  bool found;
143  SysScanDesc scandesc;
144  Relation relation;
145  HeapTuple ntp;
146  ScanKeyData skey[2];
147  Oid relid;
148 
149  if (RelfilenumberMapHash == NULL)
151 
152  /* pg_class will show 0 when the value is actually MyDatabaseTableSpace */
153  if (reltablespace == MyDatabaseTableSpace)
154  reltablespace = 0;
155 
156  MemSet(&key, 0, sizeof(key));
157  key.reltablespace = reltablespace;
158  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  entry = hash_search(RelfilenumberMapHash, (void *) &key, HASH_FIND, &found);
168 
169  if (found)
170  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  relid = InvalidOid;
176 
177  if (reltablespace == GLOBALTABLESPACE_OID)
178  {
179  /*
180  * Ok, shared table, check relmapper.
181  */
182  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  relation = table_open(RelationRelationId, AccessShareLock);
193 
194  /* copy scankey to local copy, it will be modified during the scan */
195  memcpy(skey, relfilenumber_skey, sizeof(skey));
196 
197  /* set scan arguments */
198  skey[0].sk_argument = ObjectIdGetDatum(reltablespace);
199  skey[1].sk_argument = ObjectIdGetDatum(relfilenumber);
200 
201  scandesc = systable_beginscan(relation,
202  ClassTblspcRelfilenodeIndexId,
203  true,
204  NULL,
205  2,
206  skey);
207 
208  found = false;
209 
210  while (HeapTupleIsValid(ntp = systable_getnext(scandesc)))
211  {
212  Form_pg_class classform = (Form_pg_class) GETSTRUCT(ntp);
213 
214  if (found)
215  elog(ERROR,
216  "unexpected duplicate for tablespace %u, relfilenumber %u",
217  reltablespace, relfilenumber);
218  found = true;
219 
220  Assert(classform->reltablespace == reltablespace);
221  Assert(classform->relfilenode == relfilenumber);
222  relid = classform->oid;
223  }
224 
225  systable_endscan(scandesc);
226  table_close(relation, AccessShareLock);
227 
228  /* check for tables that are mapped but not shared */
229  if (!found)
230  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  entry = hash_search(RelfilenumberMapHash, (void *) &key, HASH_ENTER, &found);
239  if (found)
240  elog(ERROR, "corrupted hashtable");
241  entry->relid = relid;
242 
243  return relid;
244 }
#define MemSet(start, val, len)
Definition: c.h:953
void CreateCacheMemoryContext(void)
Definition: catcache.c:614
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:953
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:350
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1431
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1421
#define ERROR
Definition: elog.h:35
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:136
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:599
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:506
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:387
Oid MyDatabaseTableSpace
Definition: globals.c:91
@ HASH_FIND
Definition: hsearch.h:113
@ HASH_REMOVE
Definition: hsearch.h:115
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1561
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
#define AccessShareLock
Definition: lockdefs.h:36
MemoryContext CacheMemoryContext
Definition: mcxt.c:133
void * arg
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
uintptr_t Datum
Definition: postgres.h:412
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:600
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
static ScanKeyData relfilenumber_skey[2]
static void RelfilenumberMapInvalidateCallback(Datum arg, Oid relid)
static void InitializeRelfilenumberMap(void)
static HTAB * RelfilenumberMapHash
Oid RelidByRelfilenumber(Oid reltablespace, RelFileNumber relfilenumber)
Oid RelationMapFilenumberToOid(RelFileNumber filenumber, bool shared)
Definition: relmapper.c:219
Oid RelFileNumber
Definition: relpath.h:25
#define BTEqualStrategyNumber
Definition: stratnum.h:31
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
MemoryContext hcxt
Definition: hsearch.h:86
Definition: dynahash.c:220
RelfilenumberMapKey key
RelFileNumber relfilenumber
Datum sk_argument
Definition: skey.h:72
Oid sk_subtype
Definition: skey.h:69
Oid sk_collation
Definition: skey.h:70
StrategyNumber sk_strategy
Definition: skey.h:68
AttrNumber sk_attno
Definition: skey.h:67
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40