2002-04-15 07:22:04 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* schemacmds.c
|
|
|
|
* schema creation command support code
|
|
|
|
*
|
|
|
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2002-06-11 15:40:53 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/commands/schemacmds.c,v 1.4 2002/06/11 13:40:50 wieck Exp $
|
2002-04-15 07:22:04 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "catalog/catalog.h"
|
2002-05-17 22:53:33 +02:00
|
|
|
#include "catalog/namespace.h"
|
2002-04-15 07:22:04 +02:00
|
|
|
#include "catalog/pg_namespace.h"
|
|
|
|
#include "commands/schemacmds.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "parser/analyze.h"
|
|
|
|
#include "tcop/utility.h"
|
2002-04-27 05:45:03 +02:00
|
|
|
#include "utils/acl.h"
|
2002-04-15 07:22:04 +02:00
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CREATE SCHEMA
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
CreateSchemaCommand(CreateSchemaStmt *stmt)
|
|
|
|
{
|
|
|
|
const char *schemaName = stmt->schemaname;
|
|
|
|
const char *authId = stmt->authid;
|
2002-05-17 22:53:33 +02:00
|
|
|
Oid namespaceId;
|
2002-04-15 07:22:04 +02:00
|
|
|
List *parsetree_list;
|
|
|
|
List *parsetree_item;
|
|
|
|
const char *owner_name;
|
|
|
|
Oid owner_userid;
|
|
|
|
Oid saved_userid;
|
2002-04-27 05:45:03 +02:00
|
|
|
AclResult aclresult;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
saved_userid = GetUserId();
|
|
|
|
|
2002-04-27 05:45:03 +02:00
|
|
|
/*
|
|
|
|
* Figure out user identities.
|
|
|
|
*/
|
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
if (!authId)
|
|
|
|
{
|
|
|
|
owner_userid = saved_userid;
|
2002-06-11 15:40:53 +02:00
|
|
|
owner_name = GetUserNameFromId(owner_userid);
|
2002-04-15 07:22:04 +02:00
|
|
|
}
|
|
|
|
else if (superuser())
|
|
|
|
{
|
|
|
|
owner_name = authId;
|
|
|
|
/* The following will error out if user does not exist */
|
|
|
|
owner_userid = get_usesysid(owner_name);
|
|
|
|
/*
|
|
|
|
* Set the current user to the requested authorization so
|
|
|
|
* that objects created in the statement have the requested
|
|
|
|
* owner. (This will revert to session user on error or at
|
|
|
|
* the end of this routine.)
|
|
|
|
*/
|
|
|
|
SetUserId(owner_userid);
|
|
|
|
}
|
|
|
|
else /* not superuser */
|
|
|
|
{
|
|
|
|
owner_userid = saved_userid;
|
2002-06-11 15:40:53 +02:00
|
|
|
owner_name = GetUserNameFromId(owner_userid);
|
2002-04-15 07:22:04 +02:00
|
|
|
if (strcmp(authId, owner_name) != 0)
|
|
|
|
elog(ERROR, "CREATE SCHEMA: permission denied"
|
|
|
|
"\n\t\"%s\" is not a superuser, so cannot create a schema for \"%s\"",
|
|
|
|
owner_name, authId);
|
|
|
|
}
|
|
|
|
|
2002-04-27 05:45:03 +02:00
|
|
|
/*
|
|
|
|
* Permissions checks.
|
|
|
|
*/
|
|
|
|
aclresult = pg_database_aclcheck(MyDatabaseId, saved_userid, ACL_CREATE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
|
|
|
aclcheck_error(aclresult, DatabaseName);
|
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
if (!allowSystemTableMods && IsReservedName(schemaName))
|
|
|
|
elog(ERROR, "CREATE SCHEMA: Illegal schema name: \"%s\" -- pg_ is reserved for system schemas",
|
|
|
|
schemaName);
|
|
|
|
|
|
|
|
/* Create the schema's namespace */
|
2002-05-17 22:53:33 +02:00
|
|
|
namespaceId = NamespaceCreate(schemaName, owner_userid);
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2002-05-17 22:53:33 +02:00
|
|
|
/* Advance cmd counter to make the namespace visible */
|
2002-04-15 07:22:04 +02:00
|
|
|
CommandCounterIncrement();
|
|
|
|
|
2002-05-17 22:53:33 +02:00
|
|
|
/*
|
|
|
|
* Temporarily make the new namespace be the front of the search path,
|
|
|
|
* as well as the default creation target namespace. This will be undone
|
|
|
|
* at the end of this routine, or upon error.
|
|
|
|
*/
|
|
|
|
PushSpecialNamespace(namespaceId);
|
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/*
|
|
|
|
* Examine the list of commands embedded in the CREATE SCHEMA command,
|
|
|
|
* and reorganize them into a sequentially executable order with no
|
|
|
|
* forward references. Note that the result is still a list of raw
|
|
|
|
* parsetrees in need of parse analysis --- we cannot, in general,
|
|
|
|
* run analyze.c on one statement until we have actually executed the
|
|
|
|
* prior ones.
|
|
|
|
*/
|
|
|
|
parsetree_list = analyzeCreateSchemaStmt(stmt);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Analyze and execute each command contained in the CREATE SCHEMA
|
|
|
|
*/
|
|
|
|
foreach(parsetree_item, parsetree_list)
|
|
|
|
{
|
|
|
|
Node *parsetree = (Node *) lfirst(parsetree_item);
|
|
|
|
List *querytree_list,
|
|
|
|
*querytree_item;
|
|
|
|
|
|
|
|
querytree_list = parse_analyze(parsetree, NULL);
|
|
|
|
|
|
|
|
foreach(querytree_item, querytree_list)
|
|
|
|
{
|
|
|
|
Query *querytree = (Query *) lfirst(querytree_item);
|
|
|
|
|
|
|
|
/* schemas should contain only utility stmts */
|
|
|
|
Assert(querytree->commandType == CMD_UTILITY);
|
|
|
|
/* do this step */
|
|
|
|
ProcessUtility(querytree->utilityStmt, None, NULL);
|
|
|
|
/* make sure later steps can see the object created here */
|
|
|
|
CommandCounterIncrement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-17 22:53:33 +02:00
|
|
|
/* Reset search path to normal state */
|
|
|
|
PopSpecialNamespace(namespaceId);
|
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/* Reset current user */
|
|
|
|
SetUserId(saved_userid);
|
|
|
|
}
|