PostgreSQL Source Code  git master
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-2018, 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 (cf. 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 SnapshotAny in the test, so that
286  * we will see uncommitted rows. (We used to use SnapshotDirty, but that has
287  * the disadvantage that it ignores recently-deleted rows, creating a risk
288  * of transient conflicts for as long as our own MVCC snapshots think a
289  * recently-deleted row is live. The risk is far higher when selecting TOAST
290  * OIDs, because SnapshotToast considers dead rows as active indefinitely.)
291  */
292 Oid
294 {
295  Oid oidIndex;
296 
297  /* If relation doesn't have OIDs at all, caller is confused */
298  Assert(relation->rd_rel->relhasoids);
299 
300  /* In bootstrap mode, we don't have any indexes to use */
302  return GetNewObjectId();
303 
304  /* The relcache will cache the identity of the OID index for us */
305  oidIndex = RelationGetOidIndex(relation);
306 
307  /* If no OID index, just hand back the next OID counter value */
308  if (!OidIsValid(oidIndex))
309  {
310  /*
311  * System catalogs that have OIDs should *always* have a unique OID
312  * index; we should only take this path for user tables. Give a
313  * warning if it looks like somebody forgot an index.
314  */
315  if (IsSystemRelation(relation))
316  elog(WARNING, "generating possibly-non-unique OID for \"%s\"",
317  RelationGetRelationName(relation));
318 
319  return GetNewObjectId();
320  }
321 
322  /* Otherwise, use the index to find a nonconflicting OID */
323  return GetNewOidWithIndex(relation, oidIndex, ObjectIdAttributeNumber);
324 }
325 
326 /*
327  * GetNewOidWithIndex
328  * Guts of GetNewOid: use the supplied index
329  *
330  * This is exported separately because there are cases where we want to use
331  * an index that will not be recognized by RelationGetOidIndex: TOAST tables
332  * have indexes that are usable, but have multiple columns and are on
333  * ordinary columns rather than a true OID column. This code will work
334  * anyway, so long as the OID is the index's first column. The caller must
335  * pass in the actual heap attnum of the OID column, however.
336  *
337  * Caller must have a suitable lock on the relation.
338  */
339 Oid
340 GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
341 {
342  Oid newOid;
343  SysScanDesc scan;
344  ScanKeyData key;
345  bool collides;
346 
347  /*
348  * We should never be asked to generate a new pg_type OID during
349  * pg_upgrade; doing so would risk collisions with the OIDs it wants to
350  * assign. Hitting this assert means there's some path where we failed to
351  * ensure that a type OID is determined by commands in the dump script.
352  */
353  Assert(!IsBinaryUpgrade || RelationGetRelid(relation) != TypeRelationId);
354 
355  /* Generate new OIDs until we find one not in the table */
356  do
357  {
359 
360  newOid = GetNewObjectId();
361 
362  ScanKeyInit(&key,
363  oidcolumn,
364  BTEqualStrategyNumber, F_OIDEQ,
365  ObjectIdGetDatum(newOid));
366 
367  /* see notes above about using SnapshotAny */
368  scan = systable_beginscan(relation, indexId, true,
369  SnapshotAny, 1, &key);
370 
371  collides = HeapTupleIsValid(systable_getnext(scan));
372 
373  systable_endscan(scan);
374  } while (collides);
375 
376  return newOid;
377 }
378 
379 /*
380  * GetNewRelFileNode
381  * Generate a new relfilenode number that is unique within the
382  * database of the given tablespace.
383  *
384  * If the relfilenode will also be used as the relation's OID, pass the
385  * opened pg_class catalog, and this routine will guarantee that the result
386  * is also an unused OID within pg_class. If the result is to be used only
387  * as a relfilenode for an existing relation, pass NULL for pg_class.
388  *
389  * As with GetNewOid, there is some theoretical risk of a race condition,
390  * but it doesn't seem worth worrying about.
391  *
392  * Note: we don't support using this in bootstrap mode. All relations
393  * created by bootstrap have preassigned OIDs, so there's no need.
394  */
395 Oid
397 {
398  RelFileNodeBackend rnode;
399  char *rpath;
400  int fd;
401  bool collides;
402  BackendId backend;
403 
404  /*
405  * If we ever get here during pg_upgrade, there's something wrong; all
406  * relfilenode assignments during a binary-upgrade run should be
407  * determined by commands in the dump script.
408  */
410 
411  switch (relpersistence)
412  {
413  case RELPERSISTENCE_TEMP:
414  backend = BackendIdForTempRelations();
415  break;
416  case RELPERSISTENCE_UNLOGGED:
417  case RELPERSISTENCE_PERMANENT:
418  backend = InvalidBackendId;
419  break;
420  default:
421  elog(ERROR, "invalid relpersistence: %c", relpersistence);
422  return InvalidOid; /* placate compiler */
423  }
424 
425  /* This logic should match RelationInitPhysicalAddr */
426  rnode.node.spcNode = reltablespace ? reltablespace : MyDatabaseTableSpace;
427  rnode.node.dbNode = (rnode.node.spcNode == GLOBALTABLESPACE_OID) ? InvalidOid : MyDatabaseId;
428 
429  /*
430  * The relpath will vary based on the backend ID, so we must initialize
431  * that properly here to make sure that any collisions based on filename
432  * are properly detected.
433  */
434  rnode.backend = backend;
435 
436  do
437  {
439 
440  /* Generate the OID */
441  if (pg_class)
442  rnode.node.relNode = GetNewOid(pg_class);
443  else
444  rnode.node.relNode = GetNewObjectId();
445 
446  /* Check for existing file of same name */
447  rpath = relpath(rnode, MAIN_FORKNUM);
448  fd = BasicOpenFile(rpath, O_RDONLY | PG_BINARY);
449 
450  if (fd >= 0)
451  {
452  /* definite collision */
453  close(fd);
454  collides = true;
455  }
456  else
457  {
458  /*
459  * Here we have a little bit of a dilemma: if errno is something
460  * other than ENOENT, should we declare a collision and loop? In
461  * particular one might think this advisable for, say, EPERM.
462  * However there really shouldn't be any unreadable files in a
463  * tablespace directory, and if the EPERM is actually complaining
464  * that we can't read the directory itself, we'd be in an infinite
465  * loop. In practice it seems best to go ahead regardless of the
466  * errno. If there is a colliding file we will get an smgr
467  * failure when we attempt to create the new relation file.
468  */
469  collides = false;
470  }
471 
472  pfree(rpath);
473  } while (collides);
474 
475  return rnode.node.relNode;
476 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:340
bool IsToastRelation(Relation relation)
Definition: catalog.c:136
bool IsCatalogRelation(Relation relation)
Definition: catalog.c:92
Oid relnamespace
Definition: pg_class.h:32
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:502
#define PgShseclabelToastIndex
Definition: toasting.h:68
bool IsSystemRelation(Relation relation)
Definition: catalog.c:63
#define SharedDependDependerIndexId
Definition: indexing.h:230
bool IsCatalogClass(Oid relid, Form_pg_class reltuple)
Definition: catalog.c:104
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
#define DbRoleSettingDatidRolidIndexId
Definition: indexing.h:311
#define PgShseclabelToastTable
Definition: toasting.h:67
#define AuthIdOidIndexId
Definition: indexing.h:98
bool IsToastNamespace(Oid namespaceId)
Definition: catalog.c:177
Form_pg_class rd_rel
Definition: rel.h:84
unsigned int Oid
Definition: postgres_ext.h:31
bool IsReservedName(const char *name)
Definition: catalog.c:194
#define OidIsValid(objectId)
Definition: c.h:605
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:331
#define PG_BINARY
Definition: c.h:1080
bool IsBinaryUpgrade
Definition: globals.c:111
#define FirstNormalObjectId
Definition: transam.h:94
Oid MyDatabaseTableSpace
Definition: globals.c:88
bool IsSystemClass(Oid relid, Form_pg_class reltuple)
Definition: catalog.c:75
Oid GetNewObjectId(void)
Definition: varsup.c:467
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:419
void pfree(void *pointer)
Definition: mcxt.c:1031
bool IsToastClass(Form_pg_class reltuple)
Definition: catalog.c:148
#define ReplicationOriginIdentIndex
Definition: indexing.h:334
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
char relpersistence
Definition: pg_class.h:50
#define TablespaceOidIndexId
Definition: indexing.h:238
#define BackendIdForTempRelations()
Definition: backendid.h:34
#define PgShdescriptionToastTable
Definition: toasting.h:61
Oid reltablespace
Definition: pg_class.h:42
#define RelationGetRelationName(relation)
Definition: rel.h:441
#define DatabaseOidIndexId
Definition: indexing.h:143
#define PgShdescriptionToastIndex
Definition: toasting.h:62
#define PgDbRoleSettingToastTable
Definition: toasting.h:64
bool isTempToastNamespace(Oid namespaceId)
Definition: namespace.c:3149
#define AuthMemRoleMemIndexId
Definition: indexing.h:101
#define WARNING
Definition: elog.h:40
#define InvalidBackendId
Definition: backendid.h:23
#define SnapshotAny
Definition: tqual.h:28
int BackendId
Definition: backendid.h:21
Oid MyDatabaseId
Definition: globals.c:86
bool IsSharedRelation(Oid relationId)
Definition: catalog.c:220
Oid GetNewOid(Relation relation)
Definition: catalog.c:293
#define PLTemplateNameIndexId
Definition: indexing.h:214
bool IsSystemNamespace(Oid namespaceId)
Definition: catalog.c:163
#define InvalidOid
Definition: postgres_ext.h:36
#define SharedDescriptionObjIndexId
Definition: indexing.h:153
RelFileNode node
Definition: relfilenode.h:74
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:699
#define AuthMemMemRoleIndexId
Definition: indexing.h:103
BackendId backend
Definition: relfilenode.h:75
Oid GetNewRelFileNode(Oid reltablespace, Relation pg_class, char relpersistence)
Definition: catalog.c:396
#define DatabaseNameIndexId
Definition: indexing.h:141
const char * name
Definition: encode.c:521
#define SharedSecLabelObjectIndexId
Definition: indexing.h:317
int BasicOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:932
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:372
FormData_pg_class * Form_pg_class
Definition: pg_class.h:93
#define TablespaceNameIndexId
Definition: indexing.h:240
#define SubscriptionNameIndexId
Definition: indexing.h:358
#define relpath(rnode, forknum)
Definition: relpath.h:87
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Oid RelationGetOidIndex(Relation relation)
Definition: relcache.c:4472
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define ReplicationOriginNameIndex
Definition: indexing.h:337
#define elog
Definition: elog.h:219
#define SharedDependReferenceIndexId
Definition: indexing.h:232
#define close(a)
Definition: win32.h:12
#define PgDbRoleSettingToastIndex
Definition: toasting.h:65
#define AuthIdRolnameIndexId
Definition: indexing.h:96
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:407
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define RelationGetNamespace(relation)
Definition: rel.h:448
#define SubscriptionObjectIndexId
Definition: indexing.h:355