PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
catalog.h File Reference
#include "catalog/catversion.h"
#include "catalog/pg_class.h"
#include "utils/relcache.h"
Include dependency graph for catalog.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define OIDCHARS   10 /* max chars printed by %u */
 
#define TABLESPACE_VERSION_DIRECTORY
 

Functions

bool IsSystemRelation (Relation relation)
 
bool IsToastRelation (Relation relation)
 
bool IsCatalogRelation (Relation relation)
 
bool IsSystemClass (Oid relid, Form_pg_class reltuple)
 
bool IsToastClass (Form_pg_class reltuple)
 
bool IsCatalogClass (Oid relid, Form_pg_class reltuple)
 
bool IsSystemNamespace (Oid namespaceId)
 
bool IsToastNamespace (Oid namespaceId)
 
bool IsReservedName (const char *name)
 
bool IsSharedRelation (Oid relationId)
 
Oid GetNewOid (Relation relation)
 
Oid GetNewOidWithIndex (Relation relation, Oid indexId, AttrNumber oidcolumn)
 
Oid GetNewRelFileNode (Oid reltablespace, Relation pg_class, char relpersistence)
 

Macro Definition Documentation

#define OIDCHARS   10 /* max chars printed by %u */

Function Documentation

Oid GetNewOid ( Relation  relation)

Definition at line 288 of file catalog.c.

References Assert, elog, GetNewObjectId(), GetNewOidWithIndex(), IsBootstrapProcessingMode, IsSystemRelation(), ObjectIdAttributeNumber, OidIsValid, RelationData::rd_rel, RelationGetOidIndex(), RelationGetRelationName, and WARNING.

Referenced by AddEnumLabel(), AssignTypeArrayOid(), createdb(), CreateTrigger(), EnumValuesCreate(), GetNewRelFileNode(), and heap_prepare_insert().

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 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:335
bool IsSystemRelation(Relation relation)
Definition: catalog.c:62
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
Form_pg_class rd_rel
Definition: rel.h:113
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
Oid GetNewObjectId(void)
Definition: varsup.c:467
#define RelationGetRelationName(relation)
Definition: rel.h:433
#define WARNING
Definition: elog.h:40
#define Assert(condition)
Definition: c.h:675
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:365
Oid RelationGetOidIndex(Relation relation)
Definition: relcache.c:4539
#define elog
Definition: elog.h:219
Oid GetNewOidWithIndex ( Relation  relation,
Oid  indexId,
AttrNumber  oidcolumn 
)

Definition at line 335 of file catalog.c.

References BTEqualStrategyNumber, CHECK_FOR_INTERRUPTS, GetNewObjectId(), HeapTupleIsValid, InitDirtySnapshot, ObjectIdGetDatum, ScanKeyInit(), systable_beginscan(), systable_endscan(), and systable_getnext().

Referenced by GetNewOid(), and toast_save_datum().

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 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
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:328
Oid GetNewObjectId(void)
Definition: varsup.c:467
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define InitDirtySnapshot(snapshotdata)
Definition: tqual.h:100
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define BTEqualStrategyNumber
Definition: stratnum.h:31
Oid GetNewRelFileNode ( Oid  reltablespace,
Relation  pg_class,
char  relpersistence 
)

Definition at line 386 of file catalog.c.

References RelFileNodeBackend::backend, BackendIdForTempRelations, BasicOpenFile(), CHECK_FOR_INTERRUPTS, close, RelFileNode::dbNode, elog, ERROR, fd(), GetNewObjectId(), GetNewOid(), GLOBALTABLESPACE_OID, InvalidBackendId, InvalidOid, MAIN_FORKNUM, MyDatabaseId, MyDatabaseTableSpace, RelFileNodeBackend::node, pfree(), PG_BINARY, RelFileNode::relNode, relpath, RELPERSISTENCE_PERMANENT, RELPERSISTENCE_TEMP, RELPERSISTENCE_UNLOGGED, and RelFileNode::spcNode.

Referenced by ATExecSetTableSpace(), heap_create_with_catalog(), index_create(), and RelationSetNewRelfilenode().

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 }
#define RELPERSISTENCE_UNLOGGED
Definition: pg_class.h:171
#define GLOBALTABLESPACE_OID
Definition: pg_tablespace.h:64
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1038
Oid MyDatabaseTableSpace
Definition: globals.c:78
#define RELPERSISTENCE_PERMANENT
Definition: pg_class.h:170
Oid GetNewObjectId(void)
Definition: varsup.c:467
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
#define BackendIdForTempRelations()
Definition: backendid.h:34
#define InvalidBackendId
Definition: backendid.h:23
int BackendId
Definition: backendid.h:21
Oid MyDatabaseId
Definition: globals.c:76
Oid GetNewOid(Relation relation)
Definition: catalog.c:288
#define InvalidOid
Definition: postgres_ext.h:36
RelFileNode node
Definition: relfilenode.h:74
BackendId backend
Definition: relfilenode.h:75
#define relpath(rnode, forknum)
Definition: relpath.h:71
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define elog
Definition: elog.h:219
#define close(a)
Definition: win32.h:17
#define RELPERSISTENCE_TEMP
Definition: pg_class.h:172
int BasicOpenFile(FileName fileName, int fileFlags, int fileMode)
Definition: fd.c:899
bool IsCatalogClass ( Oid  relid,
Form_pg_class  reltuple 
)

Definition at line 103 of file catalog.c.

References FirstNormalObjectId, IsSystemNamespace(), and IsToastNamespace().

Referenced by is_publishable_class(), IsCatalogRelation(), and IsSystemClass().

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 }
bool IsToastNamespace(Oid namespaceId)
Definition: catalog.c:176
unsigned int Oid
Definition: postgres_ext.h:31
#define FirstNormalObjectId
Definition: transam.h:94
bool IsSystemNamespace(Oid namespaceId)
Definition: catalog.c:162
bool IsCatalogRelation ( Relation  relation)

Definition at line 91 of file catalog.c.

References IsCatalogClass(), RelationData::rd_rel, and RelationGetRelid.

Referenced by CacheInvalidateHeapTuple(), check_publication_add_relation(), heap_multi_insert(), heap_page_prune_opt(), and transformOnConflictArbiter().

92 {
93  return IsCatalogClass(RelationGetRelid(relation), relation->rd_rel);
94 }
bool IsCatalogClass(Oid relid, Form_pg_class reltuple)
Definition: catalog.c:103
Form_pg_class rd_rel
Definition: rel.h:113
#define RelationGetRelid(relation)
Definition: rel.h:413
bool IsReservedName ( const char *  name)

Definition at line 193 of file catalog.c.

Referenced by check_rolespec_name(), CreateRole(), CreateSchemaCommand(), CreateTableSpace(), RenameRole(), RenameSchema(), and RenameTableSpace().

194 {
195  /* ugly coding for speed */
196  return (name[0] == 'p' &&
197  name[1] == 'g' &&
198  name[2] == '_');
199 }
const char * name
Definition: encode.c:521
bool IsSharedRelation ( Oid  relationId)

Definition at line 219 of file catalog.c.

References AuthIdOidIndexId, AuthIdRelationId, AuthIdRolnameIndexId, AuthMemMemRoleIndexId, AuthMemRelationId, AuthMemRoleMemIndexId, DatabaseNameIndexId, DatabaseOidIndexId, DatabaseRelationId, DbRoleSettingDatidRolidIndexId, DbRoleSettingRelationId, PgDbRoleSettingToastIndex, PgDbRoleSettingToastTable, PgShdescriptionToastIndex, PgShdescriptionToastTable, PgShseclabelToastIndex, PgShseclabelToastTable, PLTemplateNameIndexId, PLTemplateRelationId, ReplicationOriginIdentIndex, ReplicationOriginNameIndex, ReplicationOriginRelationId, SharedDependDependerIndexId, SharedDependReferenceIndexId, SharedDependRelationId, SharedDescriptionObjIndexId, SharedDescriptionRelationId, SharedSecLabelObjectIndexId, SharedSecLabelRelationId, SubscriptionNameIndexId, SubscriptionObjectIndexId, SubscriptionRelationId, TablespaceNameIndexId, TablespaceOidIndexId, and TableSpaceRelationId.

Referenced by CacheInvalidateCatalog(), CacheInvalidateHeapTuple(), classIdGetDbId(), DeleteSecurityLabel(), get_object_address(), GetSecurityLabel(), RelationBuildLocalRelation(), SetLocktagRelationOid(), SetSecurityLabel(), and UpdateLogicalMappings().

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 }
#define PgShseclabelToastIndex
Definition: toasting.h:67
#define SharedDependDependerIndexId
Definition: indexing.h:222
#define SharedDependRelationId
Definition: pg_shdepend.h:29
#define AuthMemRelationId
#define DatabaseRelationId
Definition: pg_database.h:29
#define DbRoleSettingDatidRolidIndexId
Definition: indexing.h:303
#define PgShseclabelToastTable
Definition: toasting.h:66
#define AuthIdOidIndexId
Definition: indexing.h:99
#define AuthIdRelationId
Definition: pg_authid.h:42
#define ReplicationOriginIdentIndex
Definition: indexing.h:326
#define TablespaceOidIndexId
Definition: indexing.h:230
#define SubscriptionRelationId
#define PgShdescriptionToastTable
Definition: toasting.h:60
#define DatabaseOidIndexId
Definition: indexing.h:142
#define SharedDescriptionRelationId
#define PgShdescriptionToastIndex
Definition: toasting.h:61
#define PgDbRoleSettingToastTable
Definition: toasting.h:63
#define AuthMemRoleMemIndexId
Definition: indexing.h:102
#define SharedSecLabelRelationId
Definition: pg_shseclabel.h:21
#define PLTemplateNameIndexId
Definition: indexing.h:206
#define SharedDescriptionObjIndexId
Definition: indexing.h:152
#define AuthMemMemRoleIndexId
Definition: indexing.h:104
#define DatabaseNameIndexId
Definition: indexing.h:140
#define TableSpaceRelationId
Definition: pg_tablespace.h:29
#define SharedSecLabelObjectIndexId
Definition: indexing.h:309
#define PLTemplateRelationId
Definition: pg_pltemplate.h:29
#define TablespaceNameIndexId
Definition: indexing.h:232
#define SubscriptionNameIndexId
Definition: indexing.h:350
#define ReplicationOriginRelationId
#define DbRoleSettingRelationId
#define ReplicationOriginNameIndex
Definition: indexing.h:329
#define SharedDependReferenceIndexId
Definition: indexing.h:224
#define PgDbRoleSettingToastIndex
Definition: toasting.h:64
#define AuthIdRolnameIndexId
Definition: indexing.h:97
#define SubscriptionObjectIndexId
Definition: indexing.h:347
bool IsSystemClass ( Oid  relid,
Form_pg_class  reltuple 
)
bool IsSystemNamespace ( Oid  namespaceId)

Definition at line 162 of file catalog.c.

References PG_CATALOG_NAMESPACE.

Referenced by AlterTableMoveAll(), check_relation_privileges(), heap_create(), IsCatalogClass(), and RelationBuildLocalRelation().

163 {
164  return namespaceId == PG_CATALOG_NAMESPACE;
165 }
#define PG_CATALOG_NAMESPACE
Definition: pg_namespace.h:71
bool IsToastClass ( Form_pg_class  reltuple)

Definition at line 147 of file catalog.c.

References IsToastNamespace().

Referenced by IsSystemClass().

148 {
149  Oid relnamespace = reltuple->relnamespace;
150 
151  return IsToastNamespace(relnamespace);
152 }
bool IsToastNamespace(Oid namespaceId)
Definition: catalog.c:176
unsigned int Oid
Definition: postgres_ext.h:31
bool IsToastNamespace ( Oid  namespaceId)

Definition at line 176 of file catalog.c.

References isTempToastNamespace(), and PG_TOAST_NAMESPACE.

Referenced by heap_create(), IsCatalogClass(), IsToastClass(), and IsToastRelation().

177 {
178  return (namespaceId == PG_TOAST_NAMESPACE) ||
179  isTempToastNamespace(namespaceId);
180 }
#define PG_TOAST_NAMESPACE
Definition: pg_namespace.h:74
bool isTempToastNamespace(Oid namespaceId)
Definition: namespace.c:2964
bool IsToastRelation ( Relation  relation)

Definition at line 135 of file catalog.c.

References IsToastNamespace(), and RelationGetNamespace.

Referenced by CacheInvalidateHeapTuple(), heap_abort_speculative(), ReorderBufferCommit(), and ReorderBufferToastAppendChunk().

136 {
137  return IsToastNamespace(RelationGetNamespace(relation));
138 }
bool IsToastNamespace(Oid namespaceId)
Definition: catalog.c:176
#define RelationGetNamespace(relation)
Definition: rel.h:440