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 42 of file pg_namespace.c.

References ACL_OBJECT_NAMESPACE, Anum_pg_namespace_nspacl, Anum_pg_namespace_nspname, Anum_pg_namespace_nspowner, Assert, CatalogTupleInsert(), ObjectAddress::classId, elog, ereport, errcode(), errmsg(), ERROR, get_user_default_acl(), heap_close, heap_form_tuple(), heap_open(), i, InvalidOid, 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().

43 {
44  Relation nspdesc;
45  HeapTuple tup;
46  Oid nspoid;
47  bool nulls[Natts_pg_namespace];
49  NameData nname;
50  TupleDesc tupDesc;
51  ObjectAddress myself;
52  int i;
53  Acl *nspacl;
54 
55  /* sanity checks */
56  if (!nspName)
57  elog(ERROR, "no namespace name supplied");
58 
59  /* make sure there is no existing namespace of same name */
61  ereport(ERROR,
62  (errcode(ERRCODE_DUPLICATE_SCHEMA),
63  errmsg("schema \"%s\" already exists", nspName)));
64 
65  if (!isTemp)
67  InvalidOid);
68  else
69  nspacl = NULL;
70 
71  /* initialize nulls and values */
72  for (i = 0; i < Natts_pg_namespace; i++)
73  {
74  nulls[i] = false;
75  values[i] = (Datum) NULL;
76  }
77  namestrcpy(&nname, nspName);
78  values[Anum_pg_namespace_nspname - 1] = NameGetDatum(&nname);
79  values[Anum_pg_namespace_nspowner - 1] = ObjectIdGetDatum(ownerId);
80  if (nspacl != NULL)
81  values[Anum_pg_namespace_nspacl - 1] = PointerGetDatum(nspacl);
82  else
83  nulls[Anum_pg_namespace_nspacl - 1] = true;
84 
86  tupDesc = nspdesc->rd_att;
87 
88  tup = heap_form_tuple(tupDesc, values, nulls);
89 
90  nspoid = CatalogTupleInsert(nspdesc, tup);
91  Assert(OidIsValid(nspoid));
92 
93  heap_close(nspdesc, RowExclusiveLock);
94 
95  /* Record dependencies */
97  myself.objectId = nspoid;
98  myself.objectSubId = 0;
99 
100  /* dependency on owner */
102 
103  /* dependency on extension ... but not for magic temp schemas */
104  if (!isTemp)
105  recordDependencyOnCurrentExtension(&myself, false);
106 
107  /* Post creation hook for new schema */
109 
110  return nspoid;
111 }
#define NamespaceRelationId
Definition: pg_namespace.h:34
#define NameGetDatum(X)
Definition: postgres.h:601
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define PointerGetDatum(X)
Definition: postgres.h:562
int errcode(int sqlerrcode)
Definition: elog.c:575
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
#define heap_close(r, l)
Definition: heapam.h:97
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:159
unsigned int Oid
Definition: postgres_ext.h:31
int namestrcpy(Name name, const char *str)
Definition: name.c:216
#define OidIsValid(objectId)
Definition: c.h:538
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:174
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:162
Definition: c.h:493
#define RowExclusiveLock
Definition: lockdefs.h:38
Acl * get_user_default_acl(GrantObjectType objtype, Oid ownerId, Oid nsp_oid)
Definition: aclchk.c:5246
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
TupleDesc rd_att
Definition: rel.h:115
#define Natts_pg_namespace
Definition: pg_namespace.h:58
#define InvalidOid
Definition: postgres_ext.h:36
#define Anum_pg_namespace_nspacl
Definition: pg_namespace.h:61
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:139
static Datum values[MAXATTR]
Definition: bootstrap.c:163
#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