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 317 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(), 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().

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

◆ GetNewRelFileNode()

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

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

381 {
382  RelFileNodeBackend rnode;
383  char *rpath;
384  bool collides;
385  BackendId backend;
386 
387  /*
388  * If we ever get here during pg_upgrade, there's something wrong; all
389  * relfilenode assignments during a binary-upgrade run should be
390  * determined by commands in the dump script.
391  */
393 
394  switch (relpersistence)
395  {
396  case RELPERSISTENCE_TEMP:
397  backend = BackendIdForTempRelations();
398  break;
399  case RELPERSISTENCE_UNLOGGED:
400  case RELPERSISTENCE_PERMANENT:
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;
410  rnode.node.dbNode = (rnode.node.spcNode == GLOBALTABLESPACE_OID) ? InvalidOid : MyDatabaseId;
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)
426  Anum_pg_class_oid);
427  else
428  rnode.node.relNode = GetNewObjectId();
429 
430  /* Check for existing file of same name */
431  rpath = relpath(rnode, MAIN_FORKNUM);
432 
433  if (access(rpath, F_OK) == 0)
434  {
435  /* definite collision */
436  collides = true;
437  }
438  else
439  {
440  /*
441  * Here we have a little bit of a dilemma: if errno is something
442  * other than ENOENT, should we declare a collision and loop? In
443  * practice it seems best to go ahead regardless of the errno. If
444  * there is a colliding file we will get an smgr failure when we
445  * attempt to create the new relation file.
446  */
447  collides = false;
448  }
449 
450  pfree(rpath);
451  } while (collides);
452 
453  return rnode.node.relNode;
454 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:317
bool IsBinaryUpgrade
Definition: globals.c:110
Oid MyDatabaseTableSpace
Definition: globals.c:87
Oid GetNewObjectId(void)
Definition: varsup.c:509
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
#define BackendIdForTempRelations()
Definition: backendid.h:34
#define ClassOidIndexId
Definition: indexing.h:114
#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:738
BackendId backend
Definition: relfilenode.h:75
#define elog(elevel,...)
Definition: elog.h:214
#define relpath(rnode, forknum)
Definition: relpath.h:87
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99

◆ IsCatalogNamespace()

bool IsCatalogNamespace ( Oid  namespaceId)

Definition at line 177 of file catalog.c.

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

178 {
179  return namespaceId == PG_CATALOG_NAMESPACE;
180 }

◆ IsCatalogRelation()

bool IsCatalogRelation ( Relation  relation)

◆ IsCatalogRelationOid()

bool IsCatalogRelationOid ( Oid  relid)

Definition at line 115 of file catalog.c.

References FirstBootstrapObjectId.

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

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

◆ IsReservedName()

bool IsReservedName ( const char *  name)

Definition at line 212 of file catalog.c.

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

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

◆ IsSharedRelation()

bool IsSharedRelation ( Oid  relationId)

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

239 {
240  /* These are the shared catalogs (look for BKI_SHARED_RELATION) */
241  if (relationId == AuthIdRelationId ||
242  relationId == AuthMemRelationId ||
243  relationId == DatabaseRelationId ||
244  relationId == SharedDescriptionRelationId ||
245  relationId == SharedDependRelationId ||
246  relationId == SharedSecLabelRelationId ||
247  relationId == TableSpaceRelationId ||
248  relationId == DbRoleSettingRelationId ||
249  relationId == ReplicationOriginRelationId ||
250  relationId == SubscriptionRelationId)
251  return true;
252  /* These are their indexes (see indexing.h) */
253  if (relationId == AuthIdRolnameIndexId ||
254  relationId == AuthIdOidIndexId ||
255  relationId == AuthMemRoleMemIndexId ||
256  relationId == AuthMemMemRoleIndexId ||
257  relationId == DatabaseNameIndexId ||
258  relationId == DatabaseOidIndexId ||
259  relationId == SharedDescriptionObjIndexId ||
260  relationId == SharedDependDependerIndexId ||
261  relationId == SharedDependReferenceIndexId ||
262  relationId == SharedSecLabelObjectIndexId ||
263  relationId == TablespaceOidIndexId ||
264  relationId == TablespaceNameIndexId ||
265  relationId == DbRoleSettingDatidRolidIndexId ||
266  relationId == ReplicationOriginIdentIndex ||
267  relationId == ReplicationOriginNameIndex ||
268  relationId == SubscriptionObjectIndexId ||
269  relationId == SubscriptionNameIndexId)
270  return true;
271  /* These are their toast tables and toast indexes (see toasting.h) */
272  if (relationId == PgAuthidToastTable ||
273  relationId == PgAuthidToastIndex ||
274  relationId == PgDatabaseToastTable ||
275  relationId == PgDatabaseToastIndex ||
276  relationId == PgDbRoleSettingToastTable ||
277  relationId == PgDbRoleSettingToastIndex ||
278  relationId == PgReplicationOriginToastTable ||
279  relationId == PgReplicationOriginToastIndex ||
280  relationId == PgShdescriptionToastTable ||
281  relationId == PgShdescriptionToastIndex ||
282  relationId == PgShseclabelToastTable ||
283  relationId == PgShseclabelToastIndex ||
284  relationId == PgSubscriptionToastTable ||
285  relationId == PgSubscriptionToastIndex ||
286  relationId == PgTablespaceToastTable ||
287  relationId == PgTablespaceToastIndex)
288  return true;
289  return false;
290 }
#define PgShseclabelToastIndex
Definition: toasting.h:97
#define SharedDependDependerIndexId
Definition: indexing.h:223
#define PgAuthidToastTable
Definition: toasting.h:81
#define PgTablespaceToastTable
Definition: toasting.h:102
#define DbRoleSettingDatidRolidIndexId
Definition: indexing.h:314
#define PgShseclabelToastTable
Definition: toasting.h:96
#define AuthIdOidIndexId
Definition: indexing.h:101
#define PgSubscriptionToastTable
Definition: toasting.h:99
#define PgDatabaseToastTable
Definition: toasting.h:84
#define PgTablespaceToastIndex
Definition: toasting.h:103
#define ReplicationOriginIdentIndex
Definition: indexing.h:337
#define TablespaceOidIndexId
Definition: indexing.h:241
#define PgShdescriptionToastTable
Definition: toasting.h:93
#define PgReplicationOriginToastTable
Definition: toasting.h:90
#define DatabaseOidIndexId
Definition: indexing.h:146
#define PgShdescriptionToastIndex
Definition: toasting.h:94
#define PgDbRoleSettingToastTable
Definition: toasting.h:87
#define AuthMemRoleMemIndexId
Definition: indexing.h:104
#define SharedDescriptionObjIndexId
Definition: indexing.h:156
#define PgDatabaseToastIndex
Definition: toasting.h:85
#define PgAuthidToastIndex
Definition: toasting.h:82
#define PgReplicationOriginToastIndex
Definition: toasting.h:91
#define AuthMemMemRoleIndexId
Definition: indexing.h:106
#define DatabaseNameIndexId
Definition: indexing.h:144
#define SharedSecLabelObjectIndexId
Definition: indexing.h:320
#define TablespaceNameIndexId
Definition: indexing.h:243
#define SubscriptionNameIndexId
Definition: indexing.h:361
#define PgSubscriptionToastIndex
Definition: toasting.h:100
#define ReplicationOriginNameIndex
Definition: indexing.h:340
#define SharedDependReferenceIndexId
Definition: indexing.h:225
#define PgDbRoleSettingToastIndex
Definition: toasting.h:88
#define AuthIdRolnameIndexId
Definition: indexing.h:99
#define SubscriptionObjectIndexId
Definition: indexing.h:358

◆ IsSystemClass()

bool IsSystemClass ( Oid  relid,
Form_pg_class  reltuple 
)

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

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

◆ IsSystemRelation()

◆ IsToastClass()

bool IsToastClass ( Form_pg_class  reltuple)

Definition at line 160 of file catalog.c.

References IsToastNamespace().

Referenced by IsSystemClass().

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

◆ IsToastNamespace()

bool IsToastNamespace ( Oid  namespaceId)

Definition at line 195 of file catalog.c.

References isTempToastNamespace().

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

196 {
197  return (namespaceId == PG_TOAST_NAMESPACE) ||
198  isTempToastNamespace(namespaceId);
199 }
bool isTempToastNamespace(Oid namespaceId)
Definition: namespace.c:3169

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