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

Macros

#define OIDCHARS   10 /* max chars printed by %u */
 
#define TABLESPACE_VERSION_DIRECTORY
 

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)
 

Macro Definition Documentation

◆ OIDCHARS

#define OIDCHARS   10 /* max chars printed by %u */

◆ TABLESPACE_VERSION_DIRECTORY

Function Documentation

◆ GetNewOid()

Oid GetNewOid ( Relation  relation)

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

290 {
291  Oid oidIndex;
292 
293  /* If relation doesn't have OIDs at all, caller is confused */
294  Assert(relation->rd_rel->relhasoids);
295 
296  /* In bootstrap mode, we don't have any indexes to use */
298  return GetNewObjectId();
299 
300  /* The relcache will cache the identity of the OID index for us */
301  oidIndex = RelationGetOidIndex(relation);
302 
303  /* If no OID index, just hand back the next OID counter value */
304  if (!OidIsValid(oidIndex))
305  {
306  /*
307  * System catalogs that have OIDs should *always* have a unique OID
308  * index; we should only take this path for user tables. Give a
309  * warning if it looks like somebody forgot an index.
310  */
311  if (IsSystemRelation(relation))
312  elog(WARNING, "generating possibly-non-unique OID for \"%s\"",
313  RelationGetRelationName(relation));
314 
315  return GetNewObjectId();
316  }
317 
318  /* Otherwise, use the index to find a nonconflicting OID */
319  return GetNewOidWithIndex(relation, oidIndex, ObjectIdAttributeNumber);
320 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:336
bool IsSystemRelation(Relation relation)
Definition: catalog.c:63
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:576
Oid GetNewObjectId(void)
Definition: varsup.c:467
#define RelationGetRelationName(relation)
Definition: rel.h:445
#define WARNING
Definition: elog.h:40
#define Assert(condition)
Definition: c.h:670
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:367
Oid RelationGetOidIndex(Relation relation)
Definition: relcache.c:4642
#define elog
Definition: elog.h:219

◆ GetNewOidWithIndex()

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

Definition at line 336 of file catalog.c.

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

Referenced by GetNewOid(), and toast_save_datum().

337 {
338  Oid newOid;
339  SnapshotData SnapshotDirty;
340  SysScanDesc scan;
341  ScanKeyData key;
342  bool collides;
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  */
351 
352  InitDirtySnapshot(SnapshotDirty);
353 
354  /* Generate new OIDs until we find one not in the table */
355  do
356  {
358 
359  newOid = GetNewObjectId();
360 
361  ScanKeyInit(&key,
362  oidcolumn,
363  BTEqualStrategyNumber, F_OIDEQ,
364  ObjectIdGetDatum(newOid));
365 
366  /* see notes above about using SnapshotDirty */
367  scan = systable_beginscan(relation, indexId, true,
368  &SnapshotDirty, 1, &key);
369 
370  collides = HeapTupleIsValid(systable_getnext(scan));
371 
372  systable_endscan(scan);
373  } while (collides);
374 
375  return newOid;
376 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
unsigned int Oid
Definition: postgres_ext.h:31
#define TypeRelationId
Definition: pg_type.h:34
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
bool IsBinaryUpgrade
Definition: globals.c:102
Oid GetNewObjectId(void)
Definition: varsup.c:467
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define InitDirtySnapshot(snapshotdata)
Definition: tqual.h:103
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:670
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:425
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ GetNewRelFileNode()

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

Definition at line 395 of file catalog.c.

References Assert, RelFileNodeBackend::backend, BackendIdForTempRelations, BasicOpenFile(), CHECK_FOR_INTERRUPTS, close, RelFileNode::dbNode, elog, ERROR, fd(), GetNewObjectId(), GetNewOid(), GLOBALTABLESPACE_OID, InvalidBackendId, InvalidOid, IsBinaryUpgrade, MAIN_FORKNUM, MyDatabaseId, MyDatabaseTableSpace, RelFileNodeBackend::node, pfree(), PG_BINARY, RelFileNode::relNode, relpath, RELPERSISTENCE_PERMANENT, RELPERSISTENCE_TEMP, RELPERSISTENCE_UNLOGGED, and RelFileNode::spcNode.

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

396 {
397  RelFileNodeBackend rnode;
398  char *rpath;
399  int fd;
400  bool collides;
401  BackendId backend;
402 
403  /*
404  * If we ever get here during pg_upgrade, there's something wrong; all
405  * relfilenode assignments during a binary-upgrade run should be
406  * determined by commands in the dump script.
407  */
409 
410  switch (relpersistence)
411  {
412  case RELPERSISTENCE_TEMP:
413  backend = BackendIdForTempRelations();
414  break;
417  backend = InvalidBackendId;
418  break;
419  default:
420  elog(ERROR, "invalid relpersistence: %c", relpersistence);
421  return InvalidOid; /* placate compiler */
422  }
423 
424  /* This logic should match RelationInitPhysicalAddr */
425  rnode.node.spcNode = reltablespace ? reltablespace : MyDatabaseTableSpace;
427 
428  /*
429  * The relpath will vary based on the backend ID, so we must initialize
430  * that properly here to make sure that any collisions based on filename
431  * are properly detected.
432  */
433  rnode.backend = backend;
434 
435  do
436  {
438 
439  /* Generate the OID */
440  if (pg_class)
441  rnode.node.relNode = GetNewOid(pg_class);
442  else
443  rnode.node.relNode = GetNewObjectId();
444 
445  /* Check for existing file of same name */
446  rpath = relpath(rnode, MAIN_FORKNUM);
447  fd = BasicOpenFile(rpath, O_RDONLY | PG_BINARY);
448 
449  if (fd >= 0)
450  {
451  /* definite collision */
452  close(fd);
453  collides = true;
454  }
455  else
456  {
457  /*
458  * Here we have a little bit of a dilemma: if errno is something
459  * other than ENOENT, should we declare a collision and loop? In
460  * particular one might think this advisable for, say, EPERM.
461  * However there really shouldn't be any unreadable files in a
462  * tablespace directory, and if the EPERM is actually complaining
463  * that we can't read the directory itself, we'd be in an infinite
464  * loop. In practice it seems best to go ahead regardless of the
465  * errno. If there is a colliding file we will get an smgr
466  * failure when we attempt to create the new relation file.
467  */
468  collides = false;
469  }
470 
471  pfree(rpath);
472  } while (collides);
473 
474  return rnode.node.relNode;
475 }
#define RELPERSISTENCE_UNLOGGED
Definition: pg_class.h:171
#define GLOBALTABLESPACE_OID
Definition: pg_tablespace.h:64
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1025
bool IsBinaryUpgrade
Definition: globals.c:102
Oid MyDatabaseTableSpace
Definition: globals.c:79
#define RELPERSISTENCE_PERMANENT
Definition: pg_class.h:170
Oid GetNewObjectId(void)
Definition: varsup.c:467
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
#define BackendIdForTempRelations()
Definition: backendid.h:34
#define InvalidBackendId
Definition: backendid.h:23
int BackendId
Definition: backendid.h:21
Oid MyDatabaseId
Definition: globals.c:77
Oid GetNewOid(Relation relation)
Definition: catalog.c:289
#define InvalidOid
Definition: postgres_ext.h:36
RelFileNode node
Definition: relfilenode.h:74
#define Assert(condition)
Definition: c.h:670
BackendId backend
Definition: relfilenode.h:75
int BasicOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:938
#define relpath(rnode, forknum)
Definition: relpath.h:71
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define elog
Definition: elog.h:219
#define close(a)
Definition: win32.h:12
#define RELPERSISTENCE_TEMP
Definition: pg_class.h:172

◆ IsCatalogClass()

bool IsCatalogClass ( Oid  relid,
Form_pg_class  reltuple 
)

Definition at line 104 of file catalog.c.

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

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 (c.f. 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 }
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:114
#define RelationGetRelid(relation)
Definition: rel.h:425

◆ 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, AuthIdRelationId, AuthIdRolnameIndexId, AuthMemMemRoleIndexId, AuthMemRelationId, AuthMemRoleMemIndexId, DatabaseNameIndexId, DatabaseOidIndexId, DatabaseRelationId, DbRoleSettingDatidRolidIndexId, DbRoleSettingRelationId, PgDbRoleSettingToastIndex, PgDbRoleSettingToastTable, PgShdescriptionToastIndex, PgShdescriptionToastTable, PgShseclabelToastIndex, PgShseclabelToastTable, PLTemplateNameIndexId, PLTemplateRelationId, ReplicationOriginIdentIndex, ReplicationOriginNameIndex, ReplicationOriginRelationId, SharedDependDependerIndexId, SharedDependReferenceIndexId, SharedDependRelationId, SharedDescriptionObjIndexId, SharedDescriptionRelationId, SharedSecLabelObjectIndexId, SharedSecLabelRelationId, SubscriptionNameIndexId, SubscriptionObjectIndexId, SubscriptionRelationId, TablespaceNameIndexId, TablespaceOidIndexId, and TableSpaceRelationId.

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 == PgShdescriptionToastTable ||
257  relationId == PgShdescriptionToastIndex ||
258  relationId == PgDbRoleSettingToastTable ||
259  relationId == PgDbRoleSettingToastIndex ||
260  relationId == PgShseclabelToastTable ||
261  relationId == PgShseclabelToastIndex)
262  return true;
263  return false;
264 }
#define PgShseclabelToastIndex
Definition: toasting.h:68
#define SharedDependDependerIndexId
Definition: indexing.h:229
#define SharedDependRelationId
Definition: pg_shdepend.h:29
#define AuthMemRelationId
#define DatabaseRelationId
Definition: pg_database.h:29
#define DbRoleSettingDatidRolidIndexId
Definition: indexing.h:310
#define PgShseclabelToastTable
Definition: toasting.h:67
#define AuthIdOidIndexId
Definition: indexing.h:99
#define AuthIdRelationId
Definition: pg_authid.h:42
#define ReplicationOriginIdentIndex
Definition: indexing.h:333
#define TablespaceOidIndexId
Definition: indexing.h:237
#define SubscriptionRelationId
#define PgShdescriptionToastTable
Definition: toasting.h:61
#define DatabaseOidIndexId
Definition: indexing.h:142
#define SharedDescriptionRelationId
#define PgShdescriptionToastIndex
Definition: toasting.h:62
#define PgDbRoleSettingToastTable
Definition: toasting.h:64
#define AuthMemRoleMemIndexId
Definition: indexing.h:102
#define SharedSecLabelRelationId
Definition: pg_shseclabel.h:21
#define PLTemplateNameIndexId
Definition: indexing.h:213
#define SharedDescriptionObjIndexId
Definition: indexing.h:152
#define AuthMemMemRoleIndexId
Definition: indexing.h:104
#define DatabaseNameIndexId
Definition: indexing.h:140
#define TableSpaceRelationId
Definition: pg_tablespace.h:29
#define SharedSecLabelObjectIndexId
Definition: indexing.h:316
#define PLTemplateRelationId
Definition: pg_pltemplate.h:29
#define TablespaceNameIndexId
Definition: indexing.h:239
#define SubscriptionNameIndexId
Definition: indexing.h:357
#define ReplicationOriginRelationId
#define DbRoleSettingRelationId
#define ReplicationOriginNameIndex
Definition: indexing.h:336
#define SharedDependReferenceIndexId
Definition: indexing.h:231
#define PgDbRoleSettingToastIndex
Definition: toasting.h:65
#define AuthIdRolnameIndexId
Definition: indexing.h:97
#define SubscriptionObjectIndexId
Definition: indexing.h:354

◆ IsSystemClass()

bool IsSystemClass ( Oid  relid,
Form_pg_class  reltuple 
)

◆ IsSystemNamespace()

bool IsSystemNamespace ( Oid  namespaceId)

Definition at line 163 of file catalog.c.

References PG_CATALOG_NAMESPACE.

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

164 {
165  return namespaceId == PG_CATALOG_NAMESPACE;
166 }
#define PG_CATALOG_NAMESPACE
Definition: pg_namespace.h:71

◆ IsSystemRelation()

◆ IsToastClass()

bool IsToastClass ( Form_pg_class  reltuple)

Definition at line 148 of file catalog.c.

References IsToastNamespace().

Referenced by IsSystemClass().

149 {
150  Oid relnamespace = reltuple->relnamespace;
151 
152  return IsToastNamespace(relnamespace);
153 }
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(), and PG_TOAST_NAMESPACE.

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

178 {
179  return (namespaceId == PG_TOAST_NAMESPACE) ||
180  isTempToastNamespace(namespaceId);
181 }
#define PG_TOAST_NAMESPACE
Definition: pg_namespace.h:74
bool isTempToastNamespace(Oid namespaceId)
Definition: namespace.c:3130

◆ 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:452