PostgreSQL Source Code  git master
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-2018, 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, it is used to prevent it from being
35  * linked as a member of any active extension. (If someone does CREATE
36  * TEMP TABLE in an extension script, we don't want the temp schema to
37  * become part of the extension). And to avoid checking for default ACL
38  * for temp namespace (as it is not necessary).
39  * ---------------
40  */
41 Oid
42 NamespaceCreate(const char *nspName, Oid ownerId, bool isTemp)
43 {
44  Relation nspdesc;
45  HeapTuple tup;
46  Oid nspoid;
47  bool nulls[Natts_pg_namespace];
48  Datum values[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)
66  nspacl = get_user_default_acl(OBJECT_SCHEMA, ownerId,
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 
85  nspdesc = heap_open(NamespaceRelationId, RowExclusiveLock);
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 */
96  myself.classId = NamespaceRelationId;
97  myself.objectId = nspoid;
98  myself.objectSubId = 0;
99 
100  /* dependency on owner */
101  recordDependencyOnOwner(NamespaceRelationId, nspoid, ownerId);
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 */
108  InvokeObjectPostCreateHook(NamespaceRelationId, nspoid, 0);
109 
110  return nspoid;
111 }
#define NameGetDatum(X)
Definition: postgres.h:580
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define PointerGetDatum(X)
Definition: postgres.h:541
int errcode(int sqlerrcode)
Definition: elog.c:575
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1074
#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:605
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:182
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:163
Definition: c.h:570
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:367
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
TupleDesc rd_att
Definition: rel.h:85
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:699
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:139
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define elog
Definition: elog.h:219
Acl * get_user_default_acl(ObjectType objtype, Oid ownerId, Oid nsp_oid)
Definition: aclchk.c:5451
Oid NamespaceCreate(const char *nspName, Oid ownerId, bool isTemp)
Definition: pg_namespace.c:42