|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * pg_namespace.c |
| 4 | + * routines to support manipulation of the pg_namespace relation |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group |
| 7 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 8 | + * |
| 9 | + * |
| 10 | + * IDENTIFICATION |
| 11 | + * $Header: /cvsroot/pgsql/src/backend/catalog/pg_namespace.c,v 1.1 2002/03/22 21:34:44 tgl Exp $ |
| 12 | + * |
| 13 | + *------------------------------------------------------------------------- |
| 14 | + */ |
| 15 | +#include "postgres.h" |
| 16 | + |
| 17 | +#include "access/heapam.h" |
| 18 | +#include "catalog/catname.h" |
| 19 | +#include "catalog/indexing.h" |
| 20 | +#include "catalog/pg_namespace.h" |
| 21 | +#include "miscadmin.h" |
| 22 | +#include "utils/builtins.h" |
| 23 | +#include "utils/syscache.h" |
| 24 | + |
| 25 | + |
| 26 | +/* ---------------- |
| 27 | + * NamespaceCreate |
| 28 | + * --------------- |
| 29 | + */ |
| 30 | +Oid |
| 31 | +NamespaceCreate(const char *nspName) |
| 32 | +{ |
| 33 | + Relation nspdesc; |
| 34 | + HeapTuple tup; |
| 35 | + Oid nspoid; |
| 36 | + char 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 | + elog(ERROR, "namespace \"%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(GetUserId()); |
| 61 | + nulls[Anum_pg_namespace_nspacl - 1] = 'n'; |
| 62 | + |
| 63 | + nspdesc = heap_openr(NamespaceRelationName, RowExclusiveLock); |
| 64 | + tupDesc = nspdesc->rd_att; |
| 65 | + if (!HeapTupleIsValid(tup = heap_formtuple(tupDesc, |
| 66 | + values, |
| 67 | + nulls))) |
| 68 | + elog(ERROR, "NamespaceCreate: heap_formtuple failed"); |
| 69 | + nspoid = heap_insert(nspdesc, tup); |
| 70 | + if (!OidIsValid(nspoid)) |
| 71 | + elog(ERROR, "NamespaceCreate: heap_insert failed"); |
| 72 | + |
| 73 | + if (RelationGetForm(nspdesc)->relhasindex) |
| 74 | + { |
| 75 | + Relation idescs[Num_pg_namespace_indices]; |
| 76 | + |
| 77 | + CatalogOpenIndices(Num_pg_namespace_indices, Name_pg_namespace_indices, idescs); |
| 78 | + CatalogIndexInsert(idescs, Num_pg_namespace_indices, nspdesc, tup); |
| 79 | + CatalogCloseIndices(Num_pg_namespace_indices, idescs); |
| 80 | + } |
| 81 | + |
| 82 | + heap_close(nspdesc, RowExclusiveLock); |
| 83 | + |
| 84 | + return nspoid; |
| 85 | +} |
0 commit comments