PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_namespace.c File Reference
#include "postgres.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_namespace.h"
#include "utils/builtins.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for pg_namespace.c:

Go to the source code of this file.

Functions

Oid NamespaceCreate (const char *nspName, Oid ownerId, bool isTemp)
 

Function Documentation

Oid NamespaceCreate ( const char *  nspName,
Oid  ownerId,
bool  isTemp 
)

Definition at line 41 of file pg_namespace.c.

References Anum_pg_namespace_nspacl, Anum_pg_namespace_nspname, Anum_pg_namespace_nspowner, Assert, CatalogTupleInsert(), ObjectAddress::classId, elog, ereport, errcode(), errmsg(), ERROR, heap_close, heap_form_tuple(), heap_open(), i, InvokeObjectPostCreateHook, NameGetDatum, NAMESPACENAME, NamespaceRelationId, namestrcpy(), Natts_pg_namespace, NULL, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, OidIsValid, PointerGetDatum, RelationData::rd_att, recordDependencyOnCurrentExtension(), recordDependencyOnOwner(), RowExclusiveLock, SearchSysCacheExists1, and values.

Referenced by CreateSchemaCommand(), and InitTempTableNamespace().

42 {
43  Relation nspdesc;
44  HeapTuple tup;
45  Oid nspoid;
46  bool nulls[Natts_pg_namespace];
48  NameData nname;
49  TupleDesc tupDesc;
50  ObjectAddress myself;
51  int i;
52 
53  /* sanity checks */
54  if (!nspName)
55  elog(ERROR, "no namespace name supplied");
56 
57  /* make sure there is no existing namespace of same name */
59  ereport(ERROR,
60  (errcode(ERRCODE_DUPLICATE_SCHEMA),
61  errmsg("schema \"%s\" already exists", nspName)));
62 
63  /* initialize nulls and values */
64  for (i = 0; i < Natts_pg_namespace; i++)
65  {
66  nulls[i] = false;
67  values[i] = (Datum) NULL;
68  }
69  namestrcpy(&nname, nspName);
70  values[Anum_pg_namespace_nspname - 1] = NameGetDatum(&nname);
71  values[Anum_pg_namespace_nspowner - 1] = ObjectIdGetDatum(ownerId);
72  nulls[Anum_pg_namespace_nspacl - 1] = true;
73 
75  tupDesc = nspdesc->rd_att;
76 
77  tup = heap_form_tuple(tupDesc, values, nulls);
78 
79  nspoid = CatalogTupleInsert(nspdesc, tup);
80  Assert(OidIsValid(nspoid));
81 
82  heap_close(nspdesc, RowExclusiveLock);
83 
84  /* Record dependencies */
86  myself.objectId = nspoid;
87  myself.objectSubId = 0;
88 
89  /* dependency on owner */
91 
92  /* dependency on extension ... but not for magic temp schemas */
93  if (!isTemp)
94  recordDependencyOnCurrentExtension(&myself, false);
95 
96  /* Post creation hook for new schema */
98 
99  return nspoid;
100 }
#define NamespaceRelationId
Definition: pg_namespace.h:34
#define NameGetDatum(X)
Definition: postgres.h:603
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define PointerGetDatum(X)
Definition: postgres.h:564
int errcode(int sqlerrcode)
Definition: elog.c:575
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
#define heap_close(r, l)
Definition: heapam.h:97
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:158
unsigned int Oid
Definition: postgres_ext.h:31
int namestrcpy(Name name, const char *str)
Definition: name.c:217
#define OidIsValid(objectId)
Definition: c.h:533
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:167
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:162
Definition: c.h:488
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:374
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
TupleDesc rd_att
Definition: rel.h:114
#define Natts_pg_namespace
Definition: pg_namespace.h:58
#define Anum_pg_namespace_nspacl
Definition: pg_namespace.h:61
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:139
static Datum values[MAXATTR]
Definition: bootstrap.c:162
#define Anum_pg_namespace_nspowner
Definition: pg_namespace.h:60
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define elog
Definition: elog.h:219
#define Anum_pg_namespace_nspname
Definition: pg_namespace.h:59