OSDN Git Service

Update copyright for 2009.
[pg-rex/syncrep.git] / src / backend / catalog / pg_namespace.c
1 /*-------------------------------------------------------------------------
2  *
3  * pg_namespace.c
4  *        routines to support manipulation of the pg_namespace relation
5  *
6  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/catalog/pg_namespace.c,v 1.21 2009/01/01 17:23:37 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
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"
24
25
26 /* ----------------
27  * NamespaceCreate
28  * ---------------
29  */
30 Oid
31 NamespaceCreate(const char *nspName, Oid ownerId)
32 {
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;
41
42         /* sanity checks */
43         if (!nspName)
44                 elog(ERROR, "no namespace name supplied");
45
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)));
53
54         /* initialize nulls and values */
55         for (i = 0; i < Natts_pg_namespace; i++)
56         {
57                 nulls[i] = false;
58                 values[i] = (Datum) NULL;
59         }
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;
64
65         nspdesc = heap_open(NamespaceRelationId, RowExclusiveLock);
66         tupDesc = nspdesc->rd_att;
67
68         tup = heap_form_tuple(tupDesc, values, nulls);
69
70         nspoid = simple_heap_insert(nspdesc, tup);
71         Assert(OidIsValid(nspoid));
72
73         CatalogUpdateIndexes(nspdesc, tup);
74
75         heap_close(nspdesc, RowExclusiveLock);
76
77         /* Record dependency on owner */
78         recordDependencyOnOwner(NamespaceRelationId, nspoid, ownerId);
79
80         return nspoid;
81 }