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

Function Documentation

◆ GetNewOid()

Oid GetNewOid ( Relation  relation)

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

306 {
307  Oid oidIndex;
308 
309  /* If relation doesn't have OIDs at all, caller is confused */
310  Assert(relation->rd_rel->relhasoids);
311 
312  /* In bootstrap mode, we don't have any indexes to use */
314  return GetNewObjectId();
315 
316  /* The relcache will cache the identity of the OID index for us */
317  oidIndex = RelationGetOidIndex(relation);
318 
319  /* If no OID index, just hand back the next OID counter value */
320  if (!OidIsValid(oidIndex))
321  {
322  /*
323  * System catalogs that have OIDs should *always* have a unique OID
324  * index; we should only take this path for user tables. Give a
325  * warning if it looks like somebody forgot an index.
326  */
327  if (IsSystemRelation(relation))
328  elog(WARNING, "generating possibly-non-unique OID for \"%s\"",
329  RelationGetRelationName(relation));
330 
331  return GetNewObjectId();
332  }
333 
334  /* Otherwise, use the index to find a nonconflicting OID */
335  return GetNewOidWithIndex(relation, oidIndex, ObjectIdAttributeNumber);
336 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:352
bool IsSystemRelation(Relation relation)
Definition: catalog.c:63
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
Form_pg_class rd_rel
Definition: rel.h:84
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
Oid GetNewObjectId(void)
Definition: varsup.c:467
#define RelationGetRelationName(relation)
Definition: rel.h:442
#define WARNING
Definition: elog.h:40
#define Assert(condition)
Definition: c.h:732
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:372
#define elog(elevel,...)
Definition: elog.h:226
Oid RelationGetOidIndex(Relation relation)
Definition: relcache.c:4544

◆ GetNewOidWithIndex()

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

Definition at line 352 of file catalog.c.

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

Referenced by GetNewOid(), and toast_save_datum().

353 {
354  Oid newOid;
355  SysScanDesc scan;
356  ScanKeyData key;
357  bool collides;
358 
359  /*
360  * We should never be asked to generate a new pg_type OID during
361  * pg_upgrade; doing so would risk collisions with the OIDs it wants to
362  * assign. Hitting this assert means there's some path where we failed to
363  * ensure that a type OID is determined by commands in the dump script.
364  */
365  Assert(!IsBinaryUpgrade || RelationGetRelid(relation) != TypeRelationId);
366 
367  /* Generate new OIDs until we find one not in the table */
368  do
369  {
371 
372  newOid = GetNewObjectId();
373 
374  ScanKeyInit(&key,
375  oidcolumn,
376  BTEqualStrategyNumber, F_OIDEQ,
377  ObjectIdGetDatum(newOid));
378 
379  /* see notes above about using SnapshotAny */
380  scan = systable_beginscan(relation, indexId, true,
381  SnapshotAny, 1, &key);
382 
383  collides = HeapTupleIsValid(systable_getnext(scan));
384 
385  systable_endscan(scan);
386  } while (collides);
387 
388  return newOid;
389 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:488
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:109
Oid GetNewObjectId(void)
Definition: varsup.c:467
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
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define RelationGetRelid(relation)
Definition: rel.h:408
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ GetNewRelFileNode()

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

Definition at line 408 of file catalog.c.

References Assert, RelFileNodeBackend::backend, BackendIdForTempRelations, CHECK_FOR_INTERRUPTS, RelFileNode::dbNode, elog, ERROR, GetNewObjectId(), GetNewOid(), 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().

409 {
410  RelFileNodeBackend rnode;
411  char *rpath;
412  bool collides;
413  BackendId backend;
414 
415  /*
416  * If we ever get here during pg_upgrade, there's something wrong; all
417  * relfilenode assignments during a binary-upgrade run should be
418  * determined by commands in the dump script.
419  */
421 
422  switch (relpersistence)
423  {
424  case RELPERSISTENCE_TEMP:
425  backend = BackendIdForTempRelations();
426  break;
427  case RELPERSISTENCE_UNLOGGED:
428  case RELPERSISTENCE_PERMANENT:
429  backend = InvalidBackendId;
430  break;
431  default:
432  elog(ERROR, "invalid relpersistence: %c", relpersistence);
433  return InvalidOid; /* placate compiler */
434  }
435 
436  /* This logic should match RelationInitPhysicalAddr */
438  rnode.node.dbNode = (rnode.node.spcNode == GLOBALTABLESPACE_OID) ? InvalidOid : MyDatabaseId;
439 
440  /*
441  * The relpath will vary based on the backend ID, so we must initialize
442  * that properly here to make sure that any collisions based on filename
443  * are properly detected.
444  */
445  rnode.backend = backend;
446 
447  do
448  {
450 
451  /* Generate the OID */
452  if (pg_class)
453  rnode.node.relNode = GetNewOid(pg_class);
454  else
455  rnode.node.relNode = GetNewObjectId();
456 
457  /* Check for existing file of same name */
458  rpath = relpath(rnode, MAIN_FORKNUM);
459 
460  if (access(rpath, F_OK) == 0)
461  {
462  /* definite collision */
463  collides = true;
464  }
465  else
466  {
467  /*
468  * Here we have a little bit of a dilemma: if errno is something
469  * other than ENOENT, should we declare a collision and loop? In
470  * practice it seems best to go ahead regardless of the errno. If
471  * there is a colliding file we will get an smgr failure when we
472  * attempt to create the new relation file.
473  */
474  collides = false;
475  }
476 
477  pfree(rpath);
478  } while (collides);
479 
480  return rnode.node.relNode;
481 }
bool IsBinaryUpgrade
Definition: globals.c:109
Oid MyDatabaseTableSpace
Definition: globals.c:86
Oid GetNewObjectId(void)
Definition: varsup.c:467
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
char relpersistence
Definition: pg_class.h:50
#define BackendIdForTempRelations()
Definition: backendid.h:34
Oid reltablespace
Definition: pg_class.h:42
#define InvalidBackendId
Definition: backendid.h:23
int BackendId
Definition: backendid.h:21
Oid MyDatabaseId
Definition: globals.c:84
Oid GetNewOid(Relation relation)
Definition: catalog.c:305
#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:98

◆ IsCatalogClass()

bool IsCatalogClass ( Oid  relid,
Form_pg_class  reltuple 
)

Definition at line 104 of file catalog.c.

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

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

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

◆ IsCatalogRelation()

bool IsCatalogRelation ( Relation  relation)

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

93 {
94  return IsCatalogClass(RelationGetRelid(relation), relation->rd_rel);
95 }
bool IsCatalogClass(Oid relid, Form_pg_class reltuple)
Definition: catalog.c:104
Form_pg_class rd_rel
Definition: rel.h:84
#define RelationGetRelid(relation)
Definition: rel.h:408

◆ IsReservedName()

bool IsReservedName ( const char *  name)

Definition at line 194 of file catalog.c.

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

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

◆ IsSharedRelation()

bool IsSharedRelation ( Oid  relationId)

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

221 {
222  /* These are the shared catalogs (look for BKI_SHARED_RELATION) */
223  if (relationId == AuthIdRelationId ||
224  relationId == AuthMemRelationId ||
225  relationId == DatabaseRelationId ||
226  relationId == PLTemplateRelationId ||
227  relationId == SharedDescriptionRelationId ||
228  relationId == SharedDependRelationId ||
229  relationId == SharedSecLabelRelationId ||
230  relationId == TableSpaceRelationId ||
231  relationId == DbRoleSettingRelationId ||
232  relationId == ReplicationOriginRelationId ||
233  relationId == SubscriptionRelationId)
234  return true;
235  /* These are their indexes (see indexing.h) */
236  if (relationId == AuthIdRolnameIndexId ||
237  relationId == AuthIdOidIndexId ||
238  relationId == AuthMemRoleMemIndexId ||
239  relationId == AuthMemMemRoleIndexId ||
240  relationId == DatabaseNameIndexId ||
241  relationId == DatabaseOidIndexId ||
242  relationId == PLTemplateNameIndexId ||
243  relationId == SharedDescriptionObjIndexId ||
244  relationId == SharedDependDependerIndexId ||
245  relationId == SharedDependReferenceIndexId ||
246  relationId == SharedSecLabelObjectIndexId ||
247  relationId == TablespaceOidIndexId ||
248  relationId == TablespaceNameIndexId ||
249  relationId == DbRoleSettingDatidRolidIndexId ||
250  relationId == ReplicationOriginIdentIndex ||
251  relationId == ReplicationOriginNameIndex ||
252  relationId == SubscriptionObjectIndexId ||
253  relationId == SubscriptionNameIndexId)
254  return true;
255  /* These are their toast tables and toast indexes (see toasting.h) */
256  if (relationId == PgAuthidToastTable ||
257  relationId == PgAuthidToastIndex ||
258  relationId == PgDatabaseToastTable ||
259  relationId == PgDatabaseToastIndex ||
260  relationId == PgDbRoleSettingToastTable ||
261  relationId == PgDbRoleSettingToastIndex ||
262  relationId == PgPlTemplateToastTable ||
263  relationId == PgPlTemplateToastIndex ||
264  relationId == PgReplicationOriginToastTable ||
265  relationId == PgReplicationOriginToastIndex ||
266  relationId == PgShdescriptionToastTable ||
267  relationId == PgShdescriptionToastIndex ||
268  relationId == PgShseclabelToastTable ||
269  relationId == PgShseclabelToastIndex ||
270  relationId == PgSubscriptionToastTable ||
271  relationId == PgSubscriptionToastIndex ||
272  relationId == PgTablespaceToastTable ||
273  relationId == PgTablespaceToastIndex)
274  return true;
275  return false;
276 }
#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 163 of file catalog.c.

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

164 {
165  return namespaceId == PG_CATALOG_NAMESPACE;
166 }

◆ IsSystemRelation()

◆ IsToastClass()

bool IsToastClass ( Form_pg_class  reltuple)

Definition at line 148 of file catalog.c.

References IsToastNamespace(), and relnamespace.

Referenced by IsSystemClass().

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

◆ IsToastNamespace()

bool IsToastNamespace ( Oid  namespaceId)

Definition at line 177 of file catalog.c.

References isTempToastNamespace().

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

178 {
179  return (namespaceId == PG_TOAST_NAMESPACE) ||
180  isTempToastNamespace(namespaceId);
181 }
bool isTempToastNamespace(Oid namespaceId)
Definition: namespace.c:3149

◆ IsToastRelation()

bool IsToastRelation ( Relation  relation)

Definition at line 136 of file catalog.c.

References IsToastNamespace(), and RelationGetNamespace.

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

137 {
138  return IsToastNamespace(RelationGetNamespace(relation));
139 }
bool IsToastNamespace(Oid namespaceId)
Definition: catalog.c:177
#define RelationGetNamespace(relation)
Definition: rel.h:449