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

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

◆ GetNewRelFileNode()

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

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

394 {
395  RelFileNodeBackend rnode;
396  char *rpath;
397  bool collides;
398  BackendId backend;
399 
400  /*
401  * If we ever get here during pg_upgrade, there's something wrong; all
402  * relfilenode assignments during a binary-upgrade run should be
403  * determined by commands in the dump script.
404  */
406 
407  switch (relpersistence)
408  {
409  case RELPERSISTENCE_TEMP:
410  backend = BackendIdForTempRelations();
411  break;
412  case RELPERSISTENCE_UNLOGGED:
413  case RELPERSISTENCE_PERMANENT:
414  backend = InvalidBackendId;
415  break;
416  default:
417  elog(ERROR, "invalid relpersistence: %c", relpersistence);
418  return InvalidOid; /* placate compiler */
419  }
420 
421  /* This logic should match RelationInitPhysicalAddr */
422  rnode.node.spcNode = reltablespace ? reltablespace : MyDatabaseTableSpace;
423  rnode.node.dbNode = (rnode.node.spcNode == GLOBALTABLESPACE_OID) ? InvalidOid : MyDatabaseId;
424 
425  /*
426  * The relpath will vary based on the backend ID, so we must initialize
427  * that properly here to make sure that any collisions based on filename
428  * are properly detected.
429  */
430  rnode.backend = backend;
431 
432  do
433  {
435 
436  /* Generate the OID */
437  if (pg_class)
439  Anum_pg_class_oid);
440  else
441  rnode.node.relNode = GetNewObjectId();
442 
443  /* Check for existing file of same name */
444  rpath = relpath(rnode, MAIN_FORKNUM);
445 
446  if (access(rpath, F_OK) == 0)
447  {
448  /* definite collision */
449  collides = true;
450  }
451  else
452  {
453  /*
454  * Here we have a little bit of a dilemma: if errno is something
455  * other than ENOENT, should we declare a collision and loop? In
456  * practice it seems best to go ahead regardless of the errno. If
457  * there is a colliding file we will get an smgr failure when we
458  * attempt to create the new relation file.
459  */
460  collides = false;
461  }
462 
463  pfree(rpath);
464  } while (collides);
465 
466  return rnode.node.relNode;
467 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:330
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:1031
#define ERROR
Definition: elog.h:43
char relpersistence
Definition: pg_class.h:78
#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: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

◆ IsCatalogNamespace()

bool IsCatalogNamespace ( Oid  namespaceId)

Definition at line 179 of file catalog.c.

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

180 {
181  return namespaceId == PG_CATALOG_NAMESPACE;
182 }

◆ IsCatalogRelation()

bool IsCatalogRelation ( Relation  relation)

◆ IsCatalogRelationOid()

bool IsCatalogRelationOid ( Oid  relid)

Definition at line 117 of file catalog.c.

References FirstBootstrapObjectId.

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

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

◆ IsReservedName()

bool IsReservedName ( const char *  name)

Definition at line 214 of file catalog.c.

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

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

◆ IsSharedRelation()

bool IsSharedRelation ( Oid  relationId)

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

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

◆ IsSystemClass()

bool IsSystemClass ( Oid  relid,
Form_pg_class  reltuple 
)

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

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

◆ IsSystemRelation()

◆ IsToastClass()

bool IsToastClass ( Form_pg_class  reltuple)

Definition at line 162 of file catalog.c.

References IsToastNamespace().

Referenced by IsSystemClass().

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

◆ IsToastNamespace()

bool IsToastNamespace ( Oid  namespaceId)

Definition at line 197 of file catalog.c.

References isTempToastNamespace().

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

198 {
199  return (namespaceId == PG_TOAST_NAMESPACE) ||
200  isTempToastNamespace(namespaceId);
201 }
bool isTempToastNamespace(Oid namespaceId)
Definition: namespace.c:3148

◆ IsToastRelation()

bool IsToastRelation ( Relation  relation)

Definition at line 142 of file catalog.c.

References IsToastNamespace(), and RelationGetNamespace.

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

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