PostgreSQL Source Code  git master
catalog.h File Reference
#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.

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 GetNewOidWithIndex (Relation relation, Oid indexId, AttrNumber oidcolumn)
 
Oid GetNewRelFileNode (Oid reltablespace, Relation pg_class, char relpersistence)
 

Function Documentation

◆ GetNewOidWithIndex()

Oid GetNewOidWithIndex ( Relation  relation,
Oid  indexId,
AttrNumber  oidcolumn 
)

Definition at line 314 of file catalog.c.

References Assert, BTEqualStrategyNumber, CHECK_FOR_INTERRUPTS, GetNewObjectId(), HeapTupleIsValid, IsBinaryUpgrade, IsBootstrapProcessingMode, IsSystemRelation(), ObjectIdGetDatum, RelationGetRelid, ScanKeyInit(), SnapshotAny, systable_beginscan(), systable_endscan(), and systable_getnext().

Referenced by AddEnumLabel(), AssignTypeArrayOid(), CollationCreate(), ConversionCreate(), create_proc_lang(), CreateAccessMethod(), CreateCast(), CreateConstraintEntry(), createdb(), CreateForeignDataWrapper(), CreateForeignServer(), CreateOpFamily(), CreatePolicy(), CreatePublication(), CreateRole(), CreateStatistics(), CreateSubscription(), CreateTableSpace(), CreateTransform(), CreateTrigger(), CreateUserMapping(), DefineOpClass(), DefineTSConfiguration(), DefineTSDictionary(), DefineTSParser(), DefineTSTemplate(), EnumValuesCreate(), GetNewRelFileNode(), insert_event_trigger_tuple(), InsertExtensionTuple(), InsertRule(), LargeObjectCreate(), NamespaceCreate(), OperatorCreate(), OperatorShellMake(), pg_nextoid(), ProcedureCreate(), publication_add_relation(), SetDefaultACL(), StoreAttrDefault(), storeOperators(), storeProcedures(), toast_save_datum(), TypeCreate(), and TypeShellMake().

315 {
316  Oid newOid;
317  SysScanDesc scan;
318  ScanKeyData key;
319  bool collides;
320 
321  /* Only system relations are supported */
322  Assert(IsSystemRelation(relation));
323 
324  /* In bootstrap mode, we don't have any indexes to use */
326  return GetNewObjectId();
327 
328  /*
329  * We should never be asked to generate a new pg_type OID during
330  * pg_upgrade; doing so would risk collisions with the OIDs it wants to
331  * assign. Hitting this assert means there's some path where we failed to
332  * ensure that a type OID is determined by commands in the dump script.
333  */
334  Assert(!IsBinaryUpgrade || RelationGetRelid(relation) != TypeRelationId);
335 
336  /* Generate new OIDs until we find one not in the table */
337  do
338  {
340 
341  newOid = GetNewObjectId();
342 
343  ScanKeyInit(&key,
344  oidcolumn,
345  BTEqualStrategyNumber, F_OIDEQ,
346  ObjectIdGetDatum(newOid));
347 
348  /* see notes above about using SnapshotAny */
349  scan = systable_beginscan(relation, indexId, true,
350  SnapshotAny, 1, &key);
351 
352  collides = HeapTupleIsValid(systable_getnext(scan));
353 
354  systable_endscan(scan);
355  } while (collides);
356 
357  return newOid;
358 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:488
bool IsSystemRelation(Relation relation)
Definition: catalog.c:67
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:317
bool IsBinaryUpgrade
Definition: globals.c:110
Oid GetNewObjectId(void)
Definition: varsup.c:459
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:405
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define SnapshotAny
Definition: tqual.h:28
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:732
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:374
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define RelationGetRelid(relation)
Definition: rel.h:407
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ GetNewRelFileNode()

Oid GetNewRelFileNode ( Oid  reltablespace,
Relation  pg_class,
char  relpersistence 
)

Definition at line 377 of file catalog.c.

References Assert, RelFileNodeBackend::backend, BackendIdForTempRelations, CHECK_FOR_INTERRUPTS, ClassOidIndexId, RelFileNode::dbNode, elog, ERROR, GetNewObjectId(), GetNewOidWithIndex(), InvalidBackendId, InvalidOid, IsBinaryUpgrade, MAIN_FORKNUM, MyDatabaseId, MyDatabaseTableSpace, RelFileNodeBackend::node, pfree(), RelFileNode::relNode, relpath, reltablespace, and RelFileNode::spcNode.

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

378 {
379  RelFileNodeBackend rnode;
380  char *rpath;
381  bool collides;
382  BackendId backend;
383 
384  /*
385  * If we ever get here during pg_upgrade, there's something wrong; all
386  * relfilenode assignments during a binary-upgrade run should be
387  * determined by commands in the dump script.
388  */
390 
391  switch (relpersistence)
392  {
393  case RELPERSISTENCE_TEMP:
394  backend = BackendIdForTempRelations();
395  break;
396  case RELPERSISTENCE_UNLOGGED:
397  case RELPERSISTENCE_PERMANENT:
398  backend = InvalidBackendId;
399  break;
400  default:
401  elog(ERROR, "invalid relpersistence: %c", relpersistence);
402  return InvalidOid; /* placate compiler */
403  }
404 
405  /* This logic should match RelationInitPhysicalAddr */
407  rnode.node.dbNode = (rnode.node.spcNode == GLOBALTABLESPACE_OID) ? InvalidOid : MyDatabaseId;
408 
409  /*
410  * The relpath will vary based on the backend ID, so we must initialize
411  * that properly here to make sure that any collisions based on filename
412  * are properly detected.
413  */
414  rnode.backend = backend;
415 
416  do
417  {
419 
420  /* Generate the OID */
421  if (pg_class)
423  Anum_pg_class_oid);
424  else
425  rnode.node.relNode = GetNewObjectId();
426 
427  /* Check for existing file of same name */
428  rpath = relpath(rnode, MAIN_FORKNUM);
429 
430  if (access(rpath, F_OK) == 0)
431  {
432  /* definite collision */
433  collides = true;
434  }
435  else
436  {
437  /*
438  * Here we have a little bit of a dilemma: if errno is something
439  * other than ENOENT, should we declare a collision and loop? In
440  * practice it seems best to go ahead regardless of the errno. If
441  * there is a colliding file we will get an smgr failure when we
442  * attempt to create the new relation file.
443  */
444  collides = false;
445  }
446 
447  pfree(rpath);
448  } while (collides);
449 
450  return rnode.node.relNode;
451 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:314
bool IsBinaryUpgrade
Definition: globals.c:110
Oid MyDatabaseTableSpace
Definition: globals.c:87
Oid GetNewObjectId(void)
Definition: varsup.c:459
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
char relpersistence
Definition: pg_class.h:51
#define BackendIdForTempRelations()
Definition: backendid.h:34
Oid reltablespace
Definition: pg_class.h:43
#define ClassOidIndexId
Definition: indexing.h:111
#define InvalidBackendId
Definition: backendid.h:23
int BackendId
Definition: backendid.h:21
Oid MyDatabaseId
Definition: globals.c:85
#define InvalidOid
Definition: postgres_ext.h:36
RelFileNode node
Definition: relfilenode.h:74
#define Assert(condition)
Definition: c.h:732
BackendId backend
Definition: relfilenode.h:75
#define elog(elevel,...)
Definition: elog.h:226
#define relpath(rnode, forknum)
Definition: relpath.h:87
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99

◆ IsCatalogClass()

bool IsCatalogClass ( Oid  relid,
Form_pg_class  reltuple 
)

Definition at line 108 of file catalog.c.

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

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

109 {
110  Oid relnamespace = reltuple->relnamespace;
111 
112  /*
113  * Never consider relations outside pg_catalog/pg_toast to be catalog
114  * relations.
115  */
116  if (!IsSystemNamespace(relnamespace) && !IsToastNamespace(relnamespace))
117  return false;
118 
119  /* ----
120  * Check whether the oid was assigned during initdb, when creating the
121  * initial template database. Minus the relations in information_schema
122  * excluded above, these are integral part of the system.
123  * We could instead check whether the relation is pinned in pg_depend, but
124  * this is noticeably cheaper and doesn't require catalog access.
125  *
126  * This test is safe since even an oid wraparound will preserve this
127  * property (cf. GetNewObjectId()) and it has the advantage that it works
128  * correctly even if a user decides to create a relation in the pg_catalog
129  * namespace.
130  * ----
131  */
132  return relid < FirstNormalObjectId;
133 }
Oid relnamespace
Definition: pg_class.h:33
bool IsToastNamespace(Oid namespaceId)
Definition: catalog.c:181
unsigned int Oid
Definition: postgres_ext.h:31
#define FirstNormalObjectId
Definition: transam.h:94
bool IsSystemNamespace(Oid namespaceId)
Definition: catalog.c:167

◆ IsCatalogRelation()

bool IsCatalogRelation ( Relation  relation)

Definition at line 96 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().

97 {
98  return IsCatalogClass(RelationGetRelid(relation), relation->rd_rel);
99 }
bool IsCatalogClass(Oid relid, Form_pg_class reltuple)
Definition: catalog.c:108
Form_pg_class rd_rel
Definition: rel.h:84
#define RelationGetRelid(relation)
Definition: rel.h:407

◆ IsReservedName()

bool IsReservedName ( const char *  name)

Definition at line 198 of file catalog.c.

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

199 {
200  /* ugly coding for speed */
201  return (name[0] == 'p' &&
202  name[1] == 'g' &&
203  name[2] == '_');
204 }
const char * name
Definition: encode.c:521

◆ IsSharedRelation()

bool IsSharedRelation ( Oid  relationId)

Definition at line 224 of file catalog.c.

References AuthIdOidIndexId, AuthIdRolnameIndexId, AuthMemMemRoleIndexId, AuthMemRoleMemIndexId, DatabaseNameIndexId, DatabaseOidIndexId, DbRoleSettingDatidRolidIndexId, PgAuthidToastIndex, PgAuthidToastTable, PgDatabaseToastIndex, PgDatabaseToastTable, PgDbRoleSettingToastIndex, PgDbRoleSettingToastTable, PgPlTemplateToastIndex, PgPlTemplateToastTable, PgReplicationOriginToastIndex, PgReplicationOriginToastTable, PgShdescriptionToastIndex, PgShdescriptionToastTable, PgShseclabelToastIndex, PgShseclabelToastTable, PgSubscriptionToastIndex, PgSubscriptionToastTable, PgTablespaceToastIndex, PgTablespaceToastTable, PLTemplateNameIndexId, ReplicationOriginIdentIndex, ReplicationOriginNameIndex, SharedDependDependerIndexId, SharedDependReferenceIndexId, SharedDescriptionObjIndexId, SharedSecLabelObjectIndexId, SubscriptionNameIndexId, SubscriptionObjectIndexId, TablespaceNameIndexId, and TablespaceOidIndexId.

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

225 {
226  /* These are the shared catalogs (look for BKI_SHARED_RELATION) */
227  if (relationId == AuthIdRelationId ||
228  relationId == AuthMemRelationId ||
229  relationId == DatabaseRelationId ||
230  relationId == PLTemplateRelationId ||
231  relationId == SharedDescriptionRelationId ||
232  relationId == SharedDependRelationId ||
233  relationId == SharedSecLabelRelationId ||
234  relationId == TableSpaceRelationId ||
235  relationId == DbRoleSettingRelationId ||
236  relationId == ReplicationOriginRelationId ||
237  relationId == SubscriptionRelationId)
238  return true;
239  /* These are their indexes (see indexing.h) */
240  if (relationId == AuthIdRolnameIndexId ||
241  relationId == AuthIdOidIndexId ||
242  relationId == AuthMemRoleMemIndexId ||
243  relationId == AuthMemMemRoleIndexId ||
244  relationId == DatabaseNameIndexId ||
245  relationId == DatabaseOidIndexId ||
246  relationId == PLTemplateNameIndexId ||
247  relationId == SharedDescriptionObjIndexId ||
248  relationId == SharedDependDependerIndexId ||
249  relationId == SharedDependReferenceIndexId ||
250  relationId == SharedSecLabelObjectIndexId ||
251  relationId == TablespaceOidIndexId ||
252  relationId == TablespaceNameIndexId ||
253  relationId == DbRoleSettingDatidRolidIndexId ||
254  relationId == ReplicationOriginIdentIndex ||
255  relationId == ReplicationOriginNameIndex ||
256  relationId == SubscriptionObjectIndexId ||
257  relationId == SubscriptionNameIndexId)
258  return true;
259  /* These are their toast tables and toast indexes (see toasting.h) */
260  if (relationId == PgAuthidToastTable ||
261  relationId == PgAuthidToastIndex ||
262  relationId == PgDatabaseToastTable ||
263  relationId == PgDatabaseToastIndex ||
264  relationId == PgDbRoleSettingToastTable ||
265  relationId == PgDbRoleSettingToastIndex ||
266  relationId == PgPlTemplateToastTable ||
267  relationId == PgPlTemplateToastIndex ||
268  relationId == PgReplicationOriginToastTable ||
269  relationId == PgReplicationOriginToastIndex ||
270  relationId == PgShdescriptionToastTable ||
271  relationId == PgShdescriptionToastIndex ||
272  relationId == PgShseclabelToastTable ||
273  relationId == PgShseclabelToastIndex ||
274  relationId == PgSubscriptionToastTable ||
275  relationId == PgSubscriptionToastIndex ||
276  relationId == PgTablespaceToastTable ||
277  relationId == PgTablespaceToastIndex)
278  return true;
279  return false;
280 }
#define PgShseclabelToastIndex
Definition: toasting.h:96
#define SharedDependDependerIndexId
Definition: indexing.h:230
#define PgAuthidToastTable
Definition: toasting.h:77
#define PgTablespaceToastTable
Definition: toasting.h:101
#define DbRoleSettingDatidRolidIndexId
Definition: indexing.h:311
#define PgShseclabelToastTable
Definition: toasting.h:95
#define AuthIdOidIndexId
Definition: indexing.h:98
#define PgSubscriptionToastTable
Definition: toasting.h:98
#define PgDatabaseToastTable
Definition: toasting.h:80
#define PgTablespaceToastIndex
Definition: toasting.h:102
#define ReplicationOriginIdentIndex
Definition: indexing.h:334
#define TablespaceOidIndexId
Definition: indexing.h:238
#define PgPlTemplateToastTable
Definition: toasting.h:86
#define PgShdescriptionToastTable
Definition: toasting.h:92
#define PgReplicationOriginToastTable
Definition: toasting.h:89
#define DatabaseOidIndexId
Definition: indexing.h:143
#define PgShdescriptionToastIndex
Definition: toasting.h:93
#define PgDbRoleSettingToastTable
Definition: toasting.h:83
#define AuthMemRoleMemIndexId
Definition: indexing.h:101
#define PLTemplateNameIndexId
Definition: indexing.h:214
#define PgPlTemplateToastIndex
Definition: toasting.h:87
#define SharedDescriptionObjIndexId
Definition: indexing.h:153
#define PgDatabaseToastIndex
Definition: toasting.h:81
#define PgAuthidToastIndex
Definition: toasting.h:78
#define PgReplicationOriginToastIndex
Definition: toasting.h:90
#define AuthMemMemRoleIndexId
Definition: indexing.h:103
#define DatabaseNameIndexId
Definition: indexing.h:141
#define SharedSecLabelObjectIndexId
Definition: indexing.h:317
#define TablespaceNameIndexId
Definition: indexing.h:240
#define SubscriptionNameIndexId
Definition: indexing.h:358
#define PgSubscriptionToastIndex
Definition: toasting.h:99
#define ReplicationOriginNameIndex
Definition: indexing.h:337
#define SharedDependReferenceIndexId
Definition: indexing.h:232
#define PgDbRoleSettingToastIndex
Definition: toasting.h:84
#define AuthIdRolnameIndexId
Definition: indexing.h:96
#define SubscriptionObjectIndexId
Definition: indexing.h:355

◆ IsSystemClass()

bool IsSystemClass ( Oid  relid,
Form_pg_class  reltuple 
)

◆ IsSystemNamespace()

bool IsSystemNamespace ( Oid  namespaceId)

Definition at line 167 of file catalog.c.

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

168 {
169  return namespaceId == PG_CATALOG_NAMESPACE;
170 }

◆ IsSystemRelation()

◆ IsToastClass()

bool IsToastClass ( Form_pg_class  reltuple)

Definition at line 152 of file catalog.c.

References IsToastNamespace(), and relnamespace.

Referenced by IsSystemClass().

153 {
154  Oid relnamespace = reltuple->relnamespace;
155 
156  return IsToastNamespace(relnamespace);
157 }
Oid relnamespace
Definition: pg_class.h:33
bool IsToastNamespace(Oid namespaceId)
Definition: catalog.c:181
unsigned int Oid
Definition: postgres_ext.h:31

◆ IsToastNamespace()

bool IsToastNamespace ( Oid  namespaceId)

Definition at line 181 of file catalog.c.

References isTempToastNamespace().

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

182 {
183  return (namespaceId == PG_TOAST_NAMESPACE) ||
184  isTempToastNamespace(namespaceId);
185 }
bool isTempToastNamespace(Oid namespaceId)
Definition: namespace.c:3151

◆ IsToastRelation()

bool IsToastRelation ( Relation  relation)

Definition at line 140 of file catalog.c.

References IsToastNamespace(), and RelationGetNamespace.

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

141 {
142  return IsToastNamespace(RelationGetNamespace(relation));
143 }
bool IsToastNamespace(Oid namespaceId)
Definition: catalog.c:181
#define RelationGetNamespace(relation)
Definition: rel.h:448