1 /*-------------------------------------------------------------------------
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
13 *-------------------------------------------------------------------------
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"
31 NamespaceCreate(const char *nspName
, Oid ownerId
)
36 bool nulls
[Natts_pg_namespace
];
37 Datum values
[Natts_pg_namespace
];
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
),
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
++)
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
);