Fix oversight in previous error-reporting patch; mustn't pfree path string
[PostgreSQL.git] / src / backend / catalog / pg_namespace.c
blob1fa9d9dcada49b7daac16b76734083f23f288332
1 /*-------------------------------------------------------------------------
3 * pg_namespace.c
4 * routines to support manipulation of the pg_namespace relation
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * $PostgreSQL$
13 *-------------------------------------------------------------------------
15 #include "postgres.h"
17 #include "access/heapam.h"
18 #include "catalog/dependency.h"
19 #include "catalog/indexing.h"
20 #include "catalog/pg_namespace.h"
21 #include "utils/builtins.h"
22 #include "utils/rel.h"
23 #include "utils/syscache.h"
26 /* ----------------
27 * NamespaceCreate
28 * ---------------
30 Oid
31 NamespaceCreate(const char *nspName, Oid ownerId)
33 Relation nspdesc;
34 HeapTuple tup;
35 Oid nspoid;
36 bool nulls[Natts_pg_namespace];
37 Datum values[Natts_pg_namespace];
38 NameData nname;
39 TupleDesc tupDesc;
40 int i;
42 /* sanity checks */
43 if (!nspName)
44 elog(ERROR, "no namespace name supplied");
46 /* make sure there is no existing namespace of same name */
47 if (SearchSysCacheExists(NAMESPACENAME,
48 PointerGetDatum(nspName),
49 0, 0, 0))
50 ereport(ERROR,
51 (errcode(ERRCODE_DUPLICATE_SCHEMA),
52 errmsg("schema \"%s\" already exists", nspName)));
54 /* initialize nulls and values */
55 for (i = 0; i < Natts_pg_namespace; i++)
57 nulls[i] = false;
58 values[i] = (Datum) NULL;
60 namestrcpy(&nname, nspName);
61 values[Anum_pg_namespace_nspname - 1] = NameGetDatum(&nname);
62 values[Anum_pg_namespace_nspowner - 1] = ObjectIdGetDatum(ownerId);
63 nulls[Anum_pg_namespace_nspacl - 1] = true;
65 nspdesc = heap_open(NamespaceRelationId, RowExclusiveLock);
66 tupDesc = nspdesc->rd_att;
68 tup = heap_form_tuple(tupDesc, values, nulls);
70 nspoid = simple_heap_insert(nspdesc, tup);
71 Assert(OidIsValid(nspoid));
73 CatalogUpdateIndexes(nspdesc, tup);
75 heap_close(nspdesc, RowExclusiveLock);
77 /* Record dependency on owner */
78 recordDependencyOnOwner(NamespaceRelationId, nspoid, ownerId);
80 return nspoid;