]> granicus.if.org Git - postgresql/blob - src/backend/catalog/pg_namespace.c
Teach the planner to remove SubqueryScan nodes from the plan if they
[postgresql] / 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-2005, 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.13 2005/04/14 20:03:23 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include "access/heapam.h"
18 #include "catalog/indexing.h"
19 #include "catalog/pg_namespace.h"
20 #include "utils/builtins.h"
21 #include "utils/syscache.h"
22
23
24 /* ----------------
25  * NamespaceCreate
26  * ---------------
27  */
28 Oid
29 NamespaceCreate(const char *nspName, int32 ownerSysId)
30 {
31         Relation        nspdesc;
32         HeapTuple       tup;
33         Oid                     nspoid;
34         char            nulls[Natts_pg_namespace];
35         Datum           values[Natts_pg_namespace];
36         NameData        nname;
37         TupleDesc       tupDesc;
38         int                     i;
39
40         /* sanity checks */
41         if (!nspName)
42                 elog(ERROR, "no namespace name supplied");
43
44         /* make sure there is no existing namespace of same name */
45         if (SearchSysCacheExists(NAMESPACENAME,
46                                                          PointerGetDatum(nspName),
47                                                          0, 0, 0))
48                 ereport(ERROR,
49                                 (errcode(ERRCODE_DUPLICATE_SCHEMA),
50                                  errmsg("schema \"%s\" already exists", nspName)));
51
52         /* initialize nulls and values */
53         for (i = 0; i < Natts_pg_namespace; i++)
54         {
55                 nulls[i] = ' ';
56                 values[i] = (Datum) NULL;
57         }
58         namestrcpy(&nname, nspName);
59         values[Anum_pg_namespace_nspname - 1] = NameGetDatum(&nname);
60         values[Anum_pg_namespace_nspowner - 1] = Int32GetDatum(ownerSysId);
61         nulls[Anum_pg_namespace_nspacl - 1] = 'n';
62
63         nspdesc = heap_open(NamespaceRelationId, RowExclusiveLock);
64         tupDesc = nspdesc->rd_att;
65
66         tup = heap_formtuple(tupDesc, values, nulls);
67
68         nspoid = simple_heap_insert(nspdesc, tup);
69         Assert(OidIsValid(nspoid));
70
71         CatalogUpdateIndexes(nspdesc, tup);
72
73         heap_close(nspdesc, RowExclusiveLock);
74
75         return nspoid;
76 }