PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
catalog.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * catalog.c
4  * routines concerned with catalog naming conventions and other
5  * bits of hard-wired knowledge
6  *
7  *
8  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  *
12  * IDENTIFICATION
13  * src/backend/catalog/catalog.c
14  *
15  *-------------------------------------------------------------------------
16  */
17 
18 #include "postgres.h"
19 
20 #include <fcntl.h>
21 #include <unistd.h>
22 
23 #include "access/genam.h"
24 #include "access/sysattr.h"
25 #include "access/transam.h"
26 #include "catalog/catalog.h"
27 #include "catalog/indexing.h"
28 #include "catalog/namespace.h"
30 #include "catalog/pg_authid.h"
31 #include "catalog/pg_database.h"
32 #include "catalog/pg_namespace.h"
33 #include "catalog/pg_pltemplate.h"
36 #include "catalog/pg_shdepend.h"
38 #include "catalog/pg_shseclabel.h"
40 #include "catalog/pg_tablespace.h"
41 #include "catalog/toasting.h"
42 #include "miscadmin.h"
43 #include "storage/fd.h"
44 #include "utils/fmgroids.h"
45 #include "utils/rel.h"
46 #include "utils/tqual.h"
47 
48 
49 /*
50  * IsSystemRelation
51  * True iff the relation is either a system catalog or toast table.
52  * By a system catalog, we mean one that created in the pg_catalog schema
53  * during initdb. User-created relations in pg_catalog don't count as
54  * system catalogs.
55  *
56  * NB: TOAST relations are considered system relations by this test
57  * for compatibility with the old IsSystemRelationName function.
58  * This is appropriate in many places but not all. Where it's not,
59  * also check IsToastRelation or use IsCatalogRelation().
60  */
61 bool
63 {
64  return IsSystemClass(RelationGetRelid(relation), relation->rd_rel);
65 }
66 
67 /*
68  * IsSystemClass
69  * Like the above, but takes a Form_pg_class as argument.
70  * Used when we do not want to open the relation and have to
71  * search pg_class directly.
72  */
73 bool
75 {
76  return IsToastClass(reltuple) || IsCatalogClass(relid, reltuple);
77 }
78 
79 /*
80  * IsCatalogRelation
81  * True iff the relation is a system catalog, or the toast table for
82  * a system catalog. By a system catalog, we mean one that created
83  * in the pg_catalog schema during initdb. As with IsSystemRelation(),
84  * user-created relations in pg_catalog don't count as system catalogs.
85  *
86  * Note that IsSystemRelation() returns true for ALL toast relations,
87  * but this function returns true only for toast relations of system
88  * catalogs.
89  */
90 bool
92 {
93  return IsCatalogClass(RelationGetRelid(relation), relation->rd_rel);
94 }
95 
96 /*
97  * IsCatalogClass
98  * True iff the relation is a system catalog relation.
99  *
100  * Check IsCatalogRelation() for details.
101  */
102 bool
104 {
105  Oid relnamespace = reltuple->relnamespace;
106 
107  /*
108  * Never consider relations outside pg_catalog/pg_toast to be catalog
109  * relations.
110  */
111  if (!IsSystemNamespace(relnamespace) && !IsToastNamespace(relnamespace))
112  return false;
113 
114  /* ----
115  * Check whether the oid was assigned during initdb, when creating the
116  * initial template database. Minus the relations in information_schema
117  * excluded above, these are integral part of the system.
118  * We could instead check whether the relation is pinned in pg_depend, but
119  * this is noticeably cheaper and doesn't require catalog access.
120  *
121  * This test is safe since even an oid wraparound will preserve this
122  * property (c.f. GetNewObjectId()) and it has the advantage that it works
123  * correctly even if a user decides to create a relation in the pg_catalog
124  * namespace.
125  * ----
126  */
127  return relid < FirstNormalObjectId;
128 }
129 
130 /*
131  * IsToastRelation
132  * True iff relation is a TOAST support relation (or index).
133  */
134 bool
136 {
137  return IsToastNamespace(RelationGetNamespace(relation));
138 }
139 
140 /*
141  * IsToastClass
142  * Like the above, but takes a Form_pg_class as argument.
143  * Used when we do not want to open the relation and have to
144  * search pg_class directly.
145  */
146 bool
148 {
149  Oid relnamespace = reltuple->relnamespace;
150 
151  return IsToastNamespace(relnamespace);
152 }
153 
154 /*
155  * IsSystemNamespace
156  * True iff namespace is pg_catalog.
157  *
158  * NOTE: the reason this isn't a macro is to avoid having to include
159  * catalog/pg_namespace.h in a lot of places.
160  */
161 bool
162 IsSystemNamespace(Oid namespaceId)
163 {
164  return namespaceId == PG_CATALOG_NAMESPACE;
165 }
166 
167 /*
168  * IsToastNamespace
169  * True iff namespace is pg_toast or my temporary-toast-table namespace.
170  *
171  * Note: this will return false for temporary-toast-table namespaces belonging
172  * to other backends. Those are treated the same as other backends' regular
173  * temp table namespaces, and access is prevented where appropriate.
174  */
175 bool
176 IsToastNamespace(Oid namespaceId)
177 {
178  return (namespaceId == PG_TOAST_NAMESPACE) ||
179  isTempToastNamespace(namespaceId);
180 }
181 
182 
183 /*
184  * IsReservedName
185  * True iff name starts with the pg_ prefix.
186  *
187  * For some classes of objects, the prefix pg_ is reserved for
188  * system objects only. As of 8.0, this was only true for
189  * schema and tablespace names. With 9.6, this is also true
190  * for roles.
191  */
192 bool
193 IsReservedName(const char *name)
194 {
195  /* ugly coding for speed */
196  return (name[0] == 'p' &&
197  name[1] == 'g' &&
198  name[2] == '_');
199 }
200 
201 
202 /*
203  * IsSharedRelation
204  * Given the OID of a relation, determine whether it's supposed to be
205  * shared across an entire database cluster.
206  *
207  * In older releases, this had to be hard-wired so that we could compute the
208  * locktag for a relation and lock it before examining its catalog entry.
209  * Since we now have MVCC catalog access, the race conditions that made that
210  * a hard requirement are gone, so we could look at relaxing this restriction.
211  * However, if we scanned the pg_class entry to find relisshared, and only
212  * then locked the relation, pg_class could get updated in the meantime,
213  * forcing us to scan the relation again, which would definitely be complex
214  * and might have undesirable performance consequences. Fortunately, the set
215  * of shared relations is fairly static, so a hand-maintained list of their
216  * OIDs isn't completely impractical.
217  */
218 bool
220 {
221  /* These are the shared catalogs (look for BKI_SHARED_RELATION) */
222  if (relationId == AuthIdRelationId ||
223  relationId == AuthMemRelationId ||
224  relationId == DatabaseRelationId ||
225  relationId == PLTemplateRelationId ||
226  relationId == SharedDescriptionRelationId ||
227  relationId == SharedDependRelationId ||
228  relationId == SharedSecLabelRelationId ||
229  relationId == TableSpaceRelationId ||
230  relationId == DbRoleSettingRelationId ||
231  relationId == ReplicationOriginRelationId ||
232  relationId == SubscriptionRelationId)
233  return true;
234  /* These are their indexes (see indexing.h) */
235  if (relationId == AuthIdRolnameIndexId ||
236  relationId == AuthIdOidIndexId ||
237  relationId == AuthMemRoleMemIndexId ||
238  relationId == AuthMemMemRoleIndexId ||
239  relationId == DatabaseNameIndexId ||
240  relationId == DatabaseOidIndexId ||
241  relationId == PLTemplateNameIndexId ||
242  relationId == SharedDescriptionObjIndexId ||
243  relationId == SharedDependDependerIndexId ||
244  relationId == SharedDependReferenceIndexId ||
245  relationId == SharedSecLabelObjectIndexId ||
246  relationId == TablespaceOidIndexId ||
247  relationId == TablespaceNameIndexId ||
248  relationId == DbRoleSettingDatidRolidIndexId ||
249  relationId == ReplicationOriginIdentIndex ||
250  relationId == ReplicationOriginNameIndex ||
251  relationId == SubscriptionObjectIndexId ||
252  relationId == SubscriptionNameIndexId)
253  return true;
254  /* These are their toast tables and toast indexes (see toasting.h) */
255  if (relationId == PgShdescriptionToastTable ||
256  relationId == PgShdescriptionToastIndex ||
257  relationId == PgDbRoleSettingToastTable ||
258  relationId == PgDbRoleSettingToastIndex ||
259  relationId == PgShseclabelToastTable ||
260  relationId == PgShseclabelToastIndex)
261  return true;
262  return false;
263 }
264 
265 
266 /*
267  * GetNewOid
268  * Generate a new OID that is unique within the given relation.
269  *
270  * Caller must have a suitable lock on the relation.
271  *
272  * Uniqueness is promised only if the relation has a unique index on OID.
273  * This is true for all system catalogs that have OIDs, but might not be
274  * true for user tables. Note that we are effectively assuming that the
275  * table has a relatively small number of entries (much less than 2^32)
276  * and there aren't very long runs of consecutive existing OIDs. Again,
277  * this is reasonable for system catalogs but less so for user tables.
278  *
279  * Since the OID is not immediately inserted into the table, there is a
280  * race condition here; but a problem could occur only if someone else
281  * managed to cycle through 2^32 OIDs and generate the same OID before we
282  * finish inserting our row. This seems unlikely to be a problem. Note
283  * that if we had to *commit* the row to end the race condition, the risk
284  * would be rather higher; therefore we use SnapshotDirty in the test,
285  * so that we will see uncommitted rows.
286  */
287 Oid
289 {
290  Oid oidIndex;
291 
292  /* If relation doesn't have OIDs at all, caller is confused */
293  Assert(relation->rd_rel->relhasoids);
294 
295  /* In bootstrap mode, we don't have any indexes to use */
297  return GetNewObjectId();
298 
299  /* The relcache will cache the identity of the OID index for us */
300  oidIndex = RelationGetOidIndex(relation);
301 
302  /* If no OID index, just hand back the next OID counter value */
303  if (!OidIsValid(oidIndex))
304  {
305  /*
306  * System catalogs that have OIDs should *always* have a unique OID
307  * index; we should only take this path for user tables. Give a
308  * warning if it looks like somebody forgot an index.
309  */
310  if (IsSystemRelation(relation))
311  elog(WARNING, "generating possibly-non-unique OID for \"%s\"",
312  RelationGetRelationName(relation));
313 
314  return GetNewObjectId();
315  }
316 
317  /* Otherwise, use the index to find a nonconflicting OID */
318  return GetNewOidWithIndex(relation, oidIndex, ObjectIdAttributeNumber);
319 }
320 
321 /*
322  * GetNewOidWithIndex
323  * Guts of GetNewOid: use the supplied index
324  *
325  * This is exported separately because there are cases where we want to use
326  * an index that will not be recognized by RelationGetOidIndex: TOAST tables
327  * have indexes that are usable, but have multiple columns and are on
328  * ordinary columns rather than a true OID column. This code will work
329  * anyway, so long as the OID is the index's first column. The caller must
330  * pass in the actual heap attnum of the OID column, however.
331  *
332  * Caller must have a suitable lock on the relation.
333  */
334 Oid
335 GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
336 {
337  Oid newOid;
338  SnapshotData SnapshotDirty;
339  SysScanDesc scan;
340  ScanKeyData key;
341  bool collides;
342 
343  InitDirtySnapshot(SnapshotDirty);
344 
345  /* Generate new OIDs until we find one not in the table */
346  do
347  {
349 
350  newOid = GetNewObjectId();
351 
352  ScanKeyInit(&key,
353  oidcolumn,
354  BTEqualStrategyNumber, F_OIDEQ,
355  ObjectIdGetDatum(newOid));
356 
357  /* see notes above about using SnapshotDirty */
358  scan = systable_beginscan(relation, indexId, true,
359  &SnapshotDirty, 1, &key);
360 
361  collides = HeapTupleIsValid(systable_getnext(scan));
362 
363  systable_endscan(scan);
364  } while (collides);
365 
366  return newOid;
367 }
368 
369 /*
370  * GetNewRelFileNode
371  * Generate a new relfilenode number that is unique within the
372  * database of the given tablespace.
373  *
374  * If the relfilenode will also be used as the relation's OID, pass the
375  * opened pg_class catalog, and this routine will guarantee that the result
376  * is also an unused OID within pg_class. If the result is to be used only
377  * as a relfilenode for an existing relation, pass NULL for pg_class.
378  *
379  * As with GetNewOid, there is some theoretical risk of a race condition,
380  * but it doesn't seem worth worrying about.
381  *
382  * Note: we don't support using this in bootstrap mode. All relations
383  * created by bootstrap have preassigned OIDs, so there's no need.
384  */
385 Oid
386 GetNewRelFileNode(Oid reltablespace, Relation pg_class, char relpersistence)
387 {
388  RelFileNodeBackend rnode;
389  char *rpath;
390  int fd;
391  bool collides;
392  BackendId backend;
393 
394  switch (relpersistence)
395  {
396  case RELPERSISTENCE_TEMP:
397  backend = BackendIdForTempRelations();
398  break;
401  backend = InvalidBackendId;
402  break;
403  default:
404  elog(ERROR, "invalid relpersistence: %c", relpersistence);
405  return InvalidOid; /* placate compiler */
406  }
407 
408  /* This logic should match RelationInitPhysicalAddr */
409  rnode.node.spcNode = reltablespace ? reltablespace : MyDatabaseTableSpace;
411 
412  /*
413  * The relpath will vary based on the backend ID, so we must initialize
414  * that properly here to make sure that any collisions based on filename
415  * are properly detected.
416  */
417  rnode.backend = backend;
418 
419  do
420  {
422 
423  /* Generate the OID */
424  if (pg_class)
425  rnode.node.relNode = GetNewOid(pg_class);
426  else
427  rnode.node.relNode = GetNewObjectId();
428 
429  /* Check for existing file of same name */
430  rpath = relpath(rnode, MAIN_FORKNUM);
431  fd = BasicOpenFile(rpath, O_RDONLY | PG_BINARY, 0);
432 
433  if (fd >= 0)
434  {
435  /* definite collision */
436  close(fd);
437  collides = true;
438  }
439  else
440  {
441  /*
442  * Here we have a little bit of a dilemma: if errno is something
443  * other than ENOENT, should we declare a collision and loop? In
444  * particular one might think this advisable for, say, EPERM.
445  * However there really shouldn't be any unreadable files in a
446  * tablespace directory, and if the EPERM is actually complaining
447  * that we can't read the directory itself, we'd be in an infinite
448  * loop. In practice it seems best to go ahead regardless of the
449  * errno. If there is a colliding file we will get an smgr
450  * failure when we attempt to create the new relation file.
451  */
452  collides = false;
453  }
454 
455  pfree(rpath);
456  } while (collides);
457 
458  return rnode.node.relNode;
459 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:335
bool IsToastRelation(Relation relation)
Definition: catalog.c:135
bool IsCatalogRelation(Relation relation)
Definition: catalog.c:91
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:493
#define PgShseclabelToastIndex
Definition: toasting.h:67
bool IsSystemRelation(Relation relation)
Definition: catalog.c:62
#define SharedDependDependerIndexId
Definition: indexing.h:222
#define RELPERSISTENCE_UNLOGGED
Definition: pg_class.h:171
bool IsCatalogClass(Oid relid, Form_pg_class reltuple)
Definition: catalog.c:103
#define SharedDependRelationId
Definition: pg_shdepend.h:29
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
#define AuthMemRelationId
#define DatabaseRelationId
Definition: pg_database.h:29
#define DbRoleSettingDatidRolidIndexId
Definition: indexing.h:303
#define GLOBALTABLESPACE_OID
Definition: pg_tablespace.h:64
#define PgShseclabelToastTable
Definition: toasting.h:66
#define AuthIdOidIndexId
Definition: indexing.h:99
bool IsToastNamespace(Oid namespaceId)
Definition: catalog.c:176
Form_pg_class rd_rel
Definition: rel.h:113
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_TOAST_NAMESPACE
Definition: pg_namespace.h:74
bool IsReservedName(const char *name)
Definition: catalog.c:193
#define OidIsValid(objectId)
Definition: c.h:534
static int fd(const char *x, int i)
Definition: preproc-init.c:105
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:322
#define PG_BINARY
Definition: c.h:1038
#define FirstNormalObjectId
Definition: transam.h:94
Oid MyDatabaseTableSpace
Definition: globals.c:78
bool IsSystemClass(Oid relid, Form_pg_class reltuple)
Definition: catalog.c:74
#define AuthIdRelationId
Definition: pg_authid.h:42
#define RELPERSISTENCE_PERMANENT
Definition: pg_class.h:170
Oid GetNewObjectId(void)
Definition: varsup.c:446
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:410
void pfree(void *pointer)
Definition: mcxt.c:992
bool IsToastClass(Form_pg_class reltuple)
Definition: catalog.c:147
#define ReplicationOriginIdentIndex
Definition: indexing.h:326
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
#define InitDirtySnapshot(snapshotdata)
Definition: tqual.h:100
#define TablespaceOidIndexId
Definition: indexing.h:230
#define BackendIdForTempRelations()
Definition: backendid.h:34
#define SubscriptionRelationId
#define PgShdescriptionToastTable
Definition: toasting.h:60
#define RelationGetRelationName(relation)
Definition: rel.h:433
#define DatabaseOidIndexId
Definition: indexing.h:142
#define SharedDescriptionRelationId
#define PG_CATALOG_NAMESPACE
Definition: pg_namespace.h:71
#define PgShdescriptionToastIndex
Definition: toasting.h:61
#define PgDbRoleSettingToastTable
Definition: toasting.h:63
bool isTempToastNamespace(Oid namespaceId)
Definition: namespace.c:2964
#define AuthMemRoleMemIndexId
Definition: indexing.h:102
#define WARNING
Definition: elog.h:40
#define SharedSecLabelRelationId
Definition: pg_shseclabel.h:21
#define InvalidBackendId
Definition: backendid.h:23
int BackendId
Definition: backendid.h:21
Oid MyDatabaseId
Definition: globals.c:76
bool IsSharedRelation(Oid relationId)
Definition: catalog.c:219
Oid GetNewOid(Relation relation)
Definition: catalog.c:288
#define PLTemplateNameIndexId
Definition: indexing.h:206
bool IsSystemNamespace(Oid namespaceId)
Definition: catalog.c:162
#define InvalidOid
Definition: postgres_ext.h:36
#define SharedDescriptionObjIndexId
Definition: indexing.h:152
RelFileNode node
Definition: relfilenode.h:74
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:671
#define AuthMemMemRoleIndexId
Definition: indexing.h:104
BackendId backend
Definition: relfilenode.h:75
Oid GetNewRelFileNode(Oid reltablespace, Relation pg_class, char relpersistence)
Definition: catalog.c:386
#define DatabaseNameIndexId
Definition: indexing.h:140
const char * name
Definition: encode.c:521
#define TableSpaceRelationId
Definition: pg_tablespace.h:29
#define SharedSecLabelObjectIndexId
Definition: indexing.h:309
#define PLTemplateRelationId
Definition: pg_pltemplate.h:29
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:365
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
#define TablespaceNameIndexId
Definition: indexing.h:232
#define SubscriptionNameIndexId
Definition: indexing.h:350
#define ReplicationOriginRelationId
#define DbRoleSettingRelationId
#define relpath(rnode, forknum)
Definition: relpath.h:71
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Oid RelationGetOidIndex(Relation relation)
Definition: relcache.c:4539
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define ReplicationOriginNameIndex
Definition: indexing.h:329
#define elog
Definition: elog.h:219
#define SharedDependReferenceIndexId
Definition: indexing.h:224
#define close(a)
Definition: win32.h:17
#define RELPERSISTENCE_TEMP
Definition: pg_class.h:172
#define PgDbRoleSettingToastIndex
Definition: toasting.h:64
#define AuthIdRolnameIndexId
Definition: indexing.h:97
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:413
#define BTEqualStrategyNumber
Definition: stratnum.h:31
int BasicOpenFile(FileName fileName, int fileFlags, int fileMode)
Definition: fd.c:899
#define RelationGetNamespace(relation)
Definition: rel.h:440
#define SubscriptionObjectIndexId
Definition: indexing.h:347