PostgreSQL Source Code  git master
relfilenodemap.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

Oid RelidByRelfilenode (Oid reltablespace, Oid relfilenode)
 

Function Documentation

◆ RelidByRelfilenode()

Oid RelidByRelfilenode ( Oid  reltablespace,
Oid  relfilenode 
)

Definition at line 138 of file relfilenodemap.c.

References AccessShareLock, Assert, ClassTblspcRelfilenodeIndexId, elog, ERROR, GETSTRUCT, HASH_ENTER, HASH_FIND, hash_search(), HeapTupleIsValid, InitializeRelfilenodeMap(), InvalidOid, sort-test::key, MemSet, MyDatabaseTableSpace, ObjectIdGetDatum, RelationMapFilenodeToOid(), RelfilenodeMapKey::relfilenode, RelfilenodeMapEntry::relid, RelfilenodeMapKey::reltablespace, ScanKeyData::sk_argument, systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by autoprewarm_database_main(), pg_filenode_relation(), and ReorderBufferProcessTXN().

139 {
141  RelfilenodeMapEntry *entry;
142  bool found;
143  SysScanDesc scandesc;
144  Relation relation;
145  HeapTuple ntp;
146  ScanKeyData skey[2];
147  Oid relid;
148 
149  if (RelfilenodeMapHash == 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.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  entry = hash_search(RelfilenodeMapHash, (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 = 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  relation = table_open(RelationRelationId, AccessShareLock);
193 
194  /* copy scankey to local copy, it will be modified during the scan */
195  memcpy(skey, relfilenode_skey, sizeof(skey));
196 
197  /* set scan arguments */
198  skey[0].sk_argument = ObjectIdGetDatum(reltablespace);
199  skey[1].sk_argument = ObjectIdGetDatum(relfilenode);
200 
201  scandesc = systable_beginscan(relation,
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, relfilenode %u",
217  reltablespace, relfilenode);
218  found = true;
219 
220  Assert(classform->reltablespace == reltablespace);
221  Assert(classform->relfilenode == relfilenode);
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 = 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  entry = hash_search(RelfilenodeMapHash, (void *) &key, HASH_ENTER, &found);
239  if (found)
240  elog(ERROR, "corrupted hashtable");
241  entry->relid = relid;
242 
243  return relid;
244 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define AccessShareLock
Definition: lockdefs.h:36
#define MemSet(start, val, len)
Definition: c.h:1008
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
unsigned int Oid
Definition: postgres_ext.h:31
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:383
Oid MyDatabaseTableSpace
Definition: globals.c:90
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
static HTAB * RelfilenodeMapHash
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
static ScanKeyData relfilenode_skey[2]
#define ClassTblspcRelfilenodeIndexId
Definition: pg_class.h:160
#define InvalidOid
Definition: postgres_ext.h:36
Oid RelationMapFilenodeToOid(Oid filenode, bool shared)
Definition: relmapper.c:212
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
static void InitializeRelfilenodeMap(void)
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#define elog(elevel,...)
Definition: elog.h:232
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Datum sk_argument
Definition: skey.h:72