Line data Source code
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, 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 82 : 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 82 : if (!nspName)
57 0 : elog(ERROR, "no namespace name supplied");
58 :
59 : /* make sure there is no existing namespace of same name */
60 82 : if (SearchSysCacheExists1(NAMESPACENAME, PointerGetDatum(nspName)))
61 1 : ereport(ERROR,
62 : (errcode(ERRCODE_DUPLICATE_SCHEMA),
63 : errmsg("schema \"%s\" already exists", nspName)));
64 :
65 81 : if (!isTemp)
66 49 : nspacl = get_user_default_acl(ACL_OBJECT_NAMESPACE, ownerId,
67 : InvalidOid);
68 : else
69 32 : nspacl = NULL;
70 :
71 : /* initialize nulls and values */
72 324 : for (i = 0; i < Natts_pg_namespace; i++)
73 : {
74 243 : nulls[i] = false;
75 243 : values[i] = (Datum) NULL;
76 : }
77 81 : namestrcpy(&nname, nspName);
78 81 : values[Anum_pg_namespace_nspname - 1] = NameGetDatum(&nname);
79 81 : values[Anum_pg_namespace_nspowner - 1] = ObjectIdGetDatum(ownerId);
80 81 : if (nspacl != NULL)
81 2 : values[Anum_pg_namespace_nspacl - 1] = PointerGetDatum(nspacl);
82 : else
83 79 : nulls[Anum_pg_namespace_nspacl - 1] = true;
84 :
85 81 : nspdesc = heap_open(NamespaceRelationId, RowExclusiveLock);
86 81 : tupDesc = nspdesc->rd_att;
87 :
88 81 : tup = heap_form_tuple(tupDesc, values, nulls);
89 :
90 81 : nspoid = CatalogTupleInsert(nspdesc, tup);
91 81 : Assert(OidIsValid(nspoid));
92 :
93 81 : heap_close(nspdesc, RowExclusiveLock);
94 :
95 : /* Record dependencies */
96 81 : myself.classId = NamespaceRelationId;
97 81 : myself.objectId = nspoid;
98 81 : myself.objectSubId = 0;
99 :
100 : /* dependency on owner */
101 81 : recordDependencyOnOwner(NamespaceRelationId, nspoid, ownerId);
102 :
103 : /* dependency on extension ... but not for magic temp schemas */
104 81 : if (!isTemp)
105 49 : recordDependencyOnCurrentExtension(&myself, false);
106 :
107 : /* Post creation hook for new schema */
108 81 : InvokeObjectPostCreateHook(NamespaceRelationId, nspoid, 0);
109 :
110 81 : return nspoid;
111 : }
|