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