PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_namespace.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_namespace.c
4  * routines to support manipulation of the pg_namespace relation
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/catalog/pg_namespace.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/heapam.h"
18 #include "access/htup_details.h"
19 #include "catalog/dependency.h"
20 #include "catalog/indexing.h"
21 #include "catalog/objectaccess.h"
22 #include "catalog/pg_namespace.h"
23 #include "utils/builtins.h"
24 #include "utils/rel.h"
25 #include "utils/syscache.h"
26 
27 
28 /* ----------------
29  * NamespaceCreate
30  *
31  * Create a namespace (schema) with the given name and owner OID.
32  *
33  * If isTemp is true, this schema is a per-backend schema for holding
34  * temporary tables. Currently, the only effect of that is to prevent it
35  * from being linked as a member of any active extension. (If someone
36  * does CREATE TEMP TABLE in an extension script, we don't want the temp
37  * schema to become part of the extension.)
38  * ---------------
39  */
40 Oid
41 NamespaceCreate(const char *nspName, Oid ownerId, bool isTemp)
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:534
#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:489
#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:671
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
Oid NamespaceCreate(const char *nspName, Oid ownerId, bool isTemp)
Definition: pg_namespace.c:41
#define Anum_pg_namespace_nspname
Definition: pg_namespace.h:59