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 IsCatalogRelationOid (Oid relid)
 
bool IsCatalogNamespace (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 315 of file catalog.c.

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

Referenced by AddEnumLabel(), AssignTypeArrayOid(), AssignTypeMultirangeArrayOid(), AssignTypeMultirangeOid(), CastCreate(), CollationCreate(), ConversionCreate(), CreateAccessMethod(), CreateConstraintEntry(), createdb(), CreateForeignDataWrapper(), CreateForeignServer(), CreateOpFamily(), CreatePolicy(), CreateProceduralLanguage(), 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().

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

◆ GetNewRelFileNode()

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

Definition at line 378 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, and RelFileNode::spcNode.

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

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

◆ IsCatalogNamespace()

bool IsCatalogNamespace ( Oid  namespaceId)

Definition at line 175 of file catalog.c.

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

176 {
177  return namespaceId == PG_CATALOG_NAMESPACE;
178 }

◆ IsCatalogRelation()

bool IsCatalogRelation ( Relation  relation)

◆ IsCatalogRelationOid()

bool IsCatalogRelationOid ( Oid  relid)

Definition at line 113 of file catalog.c.

References FirstBootstrapObjectId.

Referenced by check_relation_privileges(), is_publishable_class(), IsCatalogRelation(), IsSystemClass(), ReindexMultipleTables(), and ReindexRelationConcurrently().

114 {
115  /*
116  * We consider a relation to be a system catalog if it has an OID that was
117  * manually assigned or assigned by genbki.pl. This includes all the
118  * defined catalogs, their indexes, and their TOAST tables and indexes.
119  *
120  * This rule excludes the relations in information_schema, which are not
121  * integral to the system and can be treated the same as user relations.
122  * (Since it's valid to drop and recreate information_schema, any rule
123  * that did not act this way would be wrong.)
124  *
125  * This test is reliable since an OID wraparound will skip this range of
126  * OIDs; see GetNewObjectId().
127  */
128  return (relid < (Oid) FirstBootstrapObjectId);
129 }
unsigned int Oid
Definition: postgres_ext.h:31
#define FirstBootstrapObjectId
Definition: transam.h:189

◆ IsReservedName()

bool IsReservedName ( const char *  name)

Definition at line 210 of file catalog.c.

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

211 {
212  /* ugly coding for speed */
213  return (name[0] == 'p' &&
214  name[1] == 'g' &&
215  name[2] == '_');
216 }
const char * name
Definition: encode.c:515

◆ IsSharedRelation()

bool IsSharedRelation ( Oid  relationId)

Definition at line 236 of file catalog.c.

References AuthIdOidIndexId, AuthIdRolnameIndexId, AuthMemMemRoleIndexId, AuthMemRoleMemIndexId, DatabaseNameIndexId, DatabaseOidIndexId, DbRoleSettingDatidRolidIndexId, PgAuthidToastIndex, PgAuthidToastTable, PgDatabaseToastIndex, PgDatabaseToastTable, PgDbRoleSettingToastIndex, PgDbRoleSettingToastTable, PgReplicationOriginToastIndex, PgReplicationOriginToastTable, PgShdescriptionToastIndex, PgShdescriptionToastTable, PgShseclabelToastIndex, PgShseclabelToastTable, PgSubscriptionToastIndex, PgSubscriptionToastTable, PgTablespaceToastIndex, PgTablespaceToastTable, 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().

237 {
238  /* These are the shared catalogs (look for BKI_SHARED_RELATION) */
239  if (relationId == AuthIdRelationId ||
240  relationId == AuthMemRelationId ||
241  relationId == DatabaseRelationId ||
242  relationId == SharedDescriptionRelationId ||
243  relationId == SharedDependRelationId ||
244  relationId == SharedSecLabelRelationId ||
245  relationId == TableSpaceRelationId ||
246  relationId == DbRoleSettingRelationId ||
247  relationId == ReplicationOriginRelationId ||
248  relationId == SubscriptionRelationId)
249  return true;
250  /* These are their indexes */
251  if (relationId == AuthIdRolnameIndexId ||
252  relationId == AuthIdOidIndexId ||
253  relationId == AuthMemRoleMemIndexId ||
254  relationId == AuthMemMemRoleIndexId ||
255  relationId == DatabaseNameIndexId ||
256  relationId == DatabaseOidIndexId ||
257  relationId == SharedDescriptionObjIndexId ||
258  relationId == SharedDependDependerIndexId ||
259  relationId == SharedDependReferenceIndexId ||
260  relationId == SharedSecLabelObjectIndexId ||
261  relationId == TablespaceOidIndexId ||
262  relationId == TablespaceNameIndexId ||
263  relationId == DbRoleSettingDatidRolidIndexId ||
264  relationId == ReplicationOriginIdentIndex ||
265  relationId == ReplicationOriginNameIndex ||
266  relationId == SubscriptionObjectIndexId ||
267  relationId == SubscriptionNameIndexId)
268  return true;
269  /* These are their toast tables and toast indexes */
270  if (relationId == PgAuthidToastTable ||
271  relationId == PgAuthidToastIndex ||
272  relationId == PgDatabaseToastTable ||
273  relationId == PgDatabaseToastIndex ||
274  relationId == PgDbRoleSettingToastTable ||
275  relationId == PgDbRoleSettingToastIndex ||
276  relationId == PgReplicationOriginToastTable ||
277  relationId == PgReplicationOriginToastIndex ||
278  relationId == PgShdescriptionToastTable ||
279  relationId == PgShdescriptionToastIndex ||
280  relationId == PgShseclabelToastTable ||
281  relationId == PgShseclabelToastIndex ||
282  relationId == PgSubscriptionToastTable ||
283  relationId == PgSubscriptionToastIndex ||
284  relationId == PgTablespaceToastTable ||
285  relationId == PgTablespaceToastIndex)
286  return true;
287  return false;
288 }
#define PgDatabaseToastTable
Definition: pg_database.h:84
#define TablespaceNameIndexId
Definition: pg_tablespace.h:55
#define PgSubscriptionToastIndex
#define SharedDescriptionObjIndexId
#define DatabaseNameIndexId
Definition: pg_database.h:88
#define PgShseclabelToastTable
Definition: pg_shseclabel.h:42
#define ReplicationOriginIdentIndex
#define SharedSecLabelObjectIndexId
Definition: pg_shseclabel.h:46
#define PgAuthidToastIndex
Definition: pg_authid.h:60
#define PgShseclabelToastIndex
Definition: pg_shseclabel.h:43
#define AuthMemMemRoleIndexId
#define PgDatabaseToastIndex
Definition: pg_database.h:85
#define SharedDependReferenceIndexId
Definition: pg_shdepend.h:75
#define ReplicationOriginNameIndex
#define PgReplicationOriginToastIndex
#define SubscriptionObjectIndexId
#define PgShdescriptionToastTable
#define PgAuthidToastTable
Definition: pg_authid.h:59
#define DatabaseOidIndexId
Definition: pg_database.h:90
#define PgDbRoleSettingToastTable
#define PgSubscriptionToastTable
#define TablespaceOidIndexId
Definition: pg_tablespace.h:53
#define PgReplicationOriginToastTable
#define DbRoleSettingDatidRolidIndexId
#define PgTablespaceToastTable
Definition: pg_tablespace.h:49
#define AuthIdOidIndexId
Definition: pg_authid.h:65
#define PgTablespaceToastIndex
Definition: pg_tablespace.h:50
#define PgShdescriptionToastIndex
#define PgDbRoleSettingToastIndex
#define AuthIdRolnameIndexId
Definition: pg_authid.h:63
#define SharedDependDependerIndexId
Definition: pg_shdepend.h:73
#define AuthMemRoleMemIndexId
#define SubscriptionNameIndexId

◆ IsSystemClass()

bool IsSystemClass ( Oid  relid,
Form_pg_class  reltuple 
)

Definition at line 78 of file catalog.c.

References IsCatalogRelationOid(), and IsToastClass().

Referenced by IsSystemRelation(), pg_class_aclmask(), RangeVarCallbackForAlterRelation(), RangeVarCallbackForDropRelation(), RangeVarCallbackForPolicy(), RangeVarCallbackForRenameRule(), RangeVarCallbackForRenameTrigger(), RangeVarCallbackOwnsRelation(), ReindexMultipleTables(), renameatt_check(), swap_relation_files(), and truncate_check_rel().

79 {
80  /* IsCatalogRelationOid is a bit faster, so test that first */
81  return (IsCatalogRelationOid(relid) || IsToastClass(reltuple));
82 }
bool IsCatalogRelationOid(Oid relid)
Definition: catalog.c:113
bool IsToastClass(Form_pg_class reltuple)
Definition: catalog.c:158

◆ IsSystemRelation()

◆ IsToastClass()

bool IsToastClass ( Form_pg_class  reltuple)

Definition at line 158 of file catalog.c.

References IsToastNamespace().

Referenced by IsSystemClass().

159 {
160  Oid relnamespace = reltuple->relnamespace;
161 
162  return IsToastNamespace(relnamespace);
163 }
bool IsToastNamespace(Oid namespaceId)
Definition: catalog.c:193
unsigned int Oid
Definition: postgres_ext.h:31

◆ IsToastNamespace()

bool IsToastNamespace ( Oid  namespaceId)

Definition at line 193 of file catalog.c.

References isTempToastNamespace().

Referenced by AlterTableMoveAll(), heap_create(), IsToastClass(), IsToastRelation(), reindex_index(), reindex_relation(), and ReindexRelationConcurrently().

194 {
195  return (namespaceId == PG_TOAST_NAMESPACE) ||
196  isTempToastNamespace(namespaceId);
197 }
bool isTempToastNamespace(Oid namespaceId)
Definition: namespace.c:3168

◆ IsToastRelation()

bool IsToastRelation ( Relation  relation)

Definition at line 138 of file catalog.c.

References IsToastNamespace(), and RelationGetNamespace.

Referenced by CacheInvalidateHeapTuple(), heap_abort_speculative(), heap_insert(), ReorderBufferProcessTXN(), and ReorderBufferToastAppendChunk().

139 {
140  /*
141  * What we actually check is whether the relation belongs to a pg_toast
142  * namespace. This should be equivalent because of restrictions that are
143  * enforced elsewhere against creating user relations in, or moving
144  * relations into/out of, a pg_toast namespace. Notice also that this
145  * will not say "true" for toast tables belonging to other sessions' temp
146  * tables; we expect that other mechanisms will prevent access to those.
147  */
148  return IsToastNamespace(RelationGetNamespace(relation));
149 }
bool IsToastNamespace(Oid namespaceId)
Definition: catalog.c:193
#define RelationGetNamespace(relation)
Definition: rel.h:498