Header And Logo

PostgreSQL
| The world's most advanced open source database.

Functions

pg_namespace.c File Reference

#include "postgres.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_namespace.h"
#include "utils/builtins.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for pg_namespace.c:

Go to the source code of this file.

Functions

Oid NamespaceCreate (const char *nspName, Oid ownerId, bool isTemp)

Function Documentation

Oid NamespaceCreate ( const char *  nspName,
Oid  ownerId,
bool  isTemp 
)

Definition at line 41 of file pg_namespace.c.

References Anum_pg_namespace_nspacl, Anum_pg_namespace_nspname, Anum_pg_namespace_nspowner, Assert, CatalogUpdateIndexes(), ObjectAddress::classId, elog, ereport, errcode(), errmsg(), ERROR, heap_close, heap_form_tuple(), heap_open(), i, InvokeObjectPostCreateHook, NameGetDatum, NAMESPACENAME, NamespaceRelationId, namestrcpy(), NULL, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, OidIsValid, PointerGetDatum, RelationData::rd_att, recordDependencyOnCurrentExtension(), recordDependencyOnOwner(), RowExclusiveLock, SearchSysCacheExists1, simple_heap_insert(), and values.

Referenced by CreateSchemaCommand(), and InitTempTableNamespace().

{
    Relation    nspdesc;
    HeapTuple   tup;
    Oid         nspoid;
    bool        nulls[Natts_pg_namespace];
    Datum       values[Natts_pg_namespace];
    NameData    nname;
    TupleDesc   tupDesc;
    ObjectAddress myself;
    int         i;

    /* sanity checks */
    if (!nspName)
        elog(ERROR, "no namespace name supplied");

    /* make sure there is no existing namespace of same name */
    if (SearchSysCacheExists1(NAMESPACENAME, PointerGetDatum(nspName)))
        ereport(ERROR,
                (errcode(ERRCODE_DUPLICATE_SCHEMA),
                 errmsg("schema \"%s\" already exists", nspName)));

    /* initialize nulls and values */
    for (i = 0; i < Natts_pg_namespace; i++)
    {
        nulls[i] = false;
        values[i] = (Datum) NULL;
    }
    namestrcpy(&nname, nspName);
    values[Anum_pg_namespace_nspname - 1] = NameGetDatum(&nname);
    values[Anum_pg_namespace_nspowner - 1] = ObjectIdGetDatum(ownerId);
    nulls[Anum_pg_namespace_nspacl - 1] = true;

    nspdesc = heap_open(NamespaceRelationId, RowExclusiveLock);
    tupDesc = nspdesc->rd_att;

    tup = heap_form_tuple(tupDesc, values, nulls);

    nspoid = simple_heap_insert(nspdesc, tup);
    Assert(OidIsValid(nspoid));

    CatalogUpdateIndexes(nspdesc, tup);

    heap_close(nspdesc, RowExclusiveLock);

    /* Record dependencies */
    myself.classId = NamespaceRelationId;
    myself.objectId = nspoid;
    myself.objectSubId = 0;

    /* dependency on owner */
    recordDependencyOnOwner(NamespaceRelationId, nspoid, ownerId);

    /* dependency on extension ... but not for magic temp schemas */
    if (!isTemp)
        recordDependencyOnCurrentExtension(&myself, false);

    /* Post creation hook for new schema */
    InvokeObjectPostCreateHook(NamespaceRelationId, nspoid, 0);

    return nspoid;
}